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 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1352 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1353 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1354 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1355 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1356 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1357 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1358 | |
| 1359 | /* Byte size accesses are always allowed. */ |
| 1360 | if (!strict || size == 1) |
| 1361 | return 0; |
| 1362 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1363 | /* For platforms that do not have a Kconfig enabling |
| 1364 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1365 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1366 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1367 | * to this code only in strict mode where we want to emulate |
| 1368 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1369 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1370 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1371 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1372 | |
| 1373 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1374 | if (!tnum_is_aligned(reg_off, size)) { |
| 1375 | char tn_buf[48]; |
| 1376 | |
| 1377 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1378 | verbose(env, |
| 1379 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1380 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1381 | return -EACCES; |
| 1382 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1383 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1384 | return 0; |
| 1385 | } |
| 1386 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1387 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1388 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1389 | const char *pointer_desc, |
| 1390 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1391 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1392 | struct tnum reg_off; |
| 1393 | |
| 1394 | /* Byte size accesses are always allowed. */ |
| 1395 | if (!strict || size == 1) |
| 1396 | return 0; |
| 1397 | |
| 1398 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1399 | if (!tnum_is_aligned(reg_off, size)) { |
| 1400 | char tn_buf[48]; |
| 1401 | |
| 1402 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1403 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1404 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1405 | return -EACCES; |
| 1406 | } |
| 1407 | |
| 1408 | return 0; |
| 1409 | } |
| 1410 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1411 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
| 1412 | const struct bpf_reg_state *reg, |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1413 | int off, int size) |
| 1414 | { |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1415 | bool strict = env->strict_alignment; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1416 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1417 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1418 | switch (reg->type) { |
| 1419 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1420 | case PTR_TO_PACKET_META: |
| 1421 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1422 | * right in front, treat it the very same way. |
| 1423 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1424 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1425 | case PTR_TO_MAP_VALUE: |
| 1426 | pointer_desc = "value "; |
| 1427 | break; |
| 1428 | case PTR_TO_CTX: |
| 1429 | pointer_desc = "context "; |
| 1430 | break; |
| 1431 | case PTR_TO_STACK: |
| 1432 | pointer_desc = "stack "; |
Jann Horn | a5ec6ae | 2017-12-18 20:11:58 -0800 | [diff] [blame] | 1433 | /* The stack spill tracking logic in check_stack_write() |
| 1434 | * and check_stack_read() relies on stack accesses being |
| 1435 | * aligned. |
| 1436 | */ |
| 1437 | strict = true; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1438 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1439 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1440 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1441 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1442 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 1443 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1446 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 1447 | const struct bpf_func_state *func, |
| 1448 | int off) |
| 1449 | { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1450 | u16 stack = env->subprog_stack_depth[func->subprogno]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1451 | |
| 1452 | if (stack >= -off) |
| 1453 | return 0; |
| 1454 | |
| 1455 | /* update known max for given subprogram */ |
| 1456 | env->subprog_stack_depth[func->subprogno] = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1457 | return 0; |
| 1458 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1459 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1460 | /* starting from main bpf function walk all instructions of the function |
| 1461 | * and recursively walk all callees that given function can call. |
| 1462 | * Ignore jump and exit insns. |
| 1463 | * Since recursion is prevented by check_cfg() this algorithm |
| 1464 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 1465 | */ |
| 1466 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 1467 | { |
| 1468 | int depth = 0, frame = 0, subprog = 0, i = 0, subprog_end; |
| 1469 | struct bpf_insn *insn = env->prog->insnsi; |
| 1470 | int insn_cnt = env->prog->len; |
| 1471 | int ret_insn[MAX_CALL_FRAMES]; |
| 1472 | int ret_prog[MAX_CALL_FRAMES]; |
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 | process_func: |
| 1475 | /* round up to 32-bytes, since this is granularity |
| 1476 | * of interpreter stack size |
| 1477 | */ |
| 1478 | depth += round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32); |
| 1479 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1480 | 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] | 1481 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1482 | return -EACCES; |
| 1483 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 1484 | continue_func: |
| 1485 | if (env->subprog_cnt == subprog) |
| 1486 | subprog_end = insn_cnt; |
| 1487 | else |
| 1488 | subprog_end = env->subprog_starts[subprog]; |
| 1489 | for (; i < subprog_end; i++) { |
| 1490 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1491 | continue; |
| 1492 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1493 | continue; |
| 1494 | /* remember insn and function to return to */ |
| 1495 | ret_insn[frame] = i + 1; |
| 1496 | ret_prog[frame] = subprog; |
| 1497 | |
| 1498 | /* find the callee */ |
| 1499 | i = i + insn[i].imm + 1; |
| 1500 | subprog = find_subprog(env, i); |
| 1501 | if (subprog < 0) { |
| 1502 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1503 | i); |
| 1504 | return -EFAULT; |
| 1505 | } |
| 1506 | subprog++; |
| 1507 | frame++; |
| 1508 | if (frame >= MAX_CALL_FRAMES) { |
| 1509 | WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); |
| 1510 | return -EFAULT; |
| 1511 | } |
| 1512 | goto process_func; |
| 1513 | } |
| 1514 | /* end of for() loop means the last insn of the 'subprog' |
| 1515 | * was reached. Doesn't matter whether it was JA or EXIT |
| 1516 | */ |
| 1517 | if (frame == 0) |
| 1518 | return 0; |
| 1519 | depth -= round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32); |
| 1520 | frame--; |
| 1521 | i = ret_insn[frame]; |
| 1522 | subprog = ret_prog[frame]; |
| 1523 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1524 | } |
| 1525 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1526 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1527 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 1528 | const struct bpf_insn *insn, int idx) |
| 1529 | { |
| 1530 | int start = idx + insn->imm + 1, subprog; |
| 1531 | |
| 1532 | subprog = find_subprog(env, start); |
| 1533 | if (subprog < 0) { |
| 1534 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1535 | start); |
| 1536 | return -EFAULT; |
| 1537 | } |
| 1538 | subprog++; |
| 1539 | return env->subprog_stack_depth[subprog]; |
| 1540 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 1541 | #endif |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1542 | |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1543 | /* truncate register to smaller size (in bytes) |
| 1544 | * must be called with size < BPF_REG_SIZE |
| 1545 | */ |
| 1546 | static void coerce_reg_to_size(struct bpf_reg_state *reg, int size) |
| 1547 | { |
| 1548 | u64 mask; |
| 1549 | |
| 1550 | /* clear high bits in bit representation */ |
| 1551 | reg->var_off = tnum_cast(reg->var_off, size); |
| 1552 | |
| 1553 | /* fix arithmetic bounds */ |
| 1554 | mask = ((u64)1 << (size * 8)) - 1; |
| 1555 | if ((reg->umin_value & ~mask) == (reg->umax_value & ~mask)) { |
| 1556 | reg->umin_value &= mask; |
| 1557 | reg->umax_value &= mask; |
| 1558 | } else { |
| 1559 | reg->umin_value = 0; |
| 1560 | reg->umax_value = mask; |
| 1561 | } |
| 1562 | reg->smin_value = reg->umin_value; |
| 1563 | reg->smax_value = reg->umax_value; |
| 1564 | } |
| 1565 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1566 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 1567 | * if t==write, value_regno is a register which value is stored into memory |
| 1568 | * if t==read, value_regno is a register which will receive the value from memory |
| 1569 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 1570 | * if t==read && value_regno==-1, don't care what we read from memory |
| 1571 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1572 | static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regno, int off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1573 | int bpf_size, enum bpf_access_type t, |
| 1574 | int value_regno) |
| 1575 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1576 | struct bpf_reg_state *regs = cur_regs(env); |
| 1577 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1578 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1579 | int size, err = 0; |
| 1580 | |
| 1581 | size = bpf_size_to_bytes(bpf_size); |
| 1582 | if (size < 0) |
| 1583 | return size; |
| 1584 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1585 | /* alignment checks will add in reg->off themselves */ |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1586 | err = check_ptr_alignment(env, reg, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1587 | if (err) |
| 1588 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1589 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1590 | /* for access checks, reg->off is just part of off */ |
| 1591 | off += reg->off; |
| 1592 | |
| 1593 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1594 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1595 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1596 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1597 | return -EACCES; |
| 1598 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1599 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1600 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1601 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1602 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1603 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1604 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1605 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1606 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1607 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1608 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1609 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1610 | return -EACCES; |
| 1611 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1612 | /* ctx accesses must be at a fixed offset, so that we can |
| 1613 | * determine what type of data were returned. |
| 1614 | */ |
Jakub Kicinski | 28e33f9 | 2017-10-16 11:16:55 -0700 | [diff] [blame] | 1615 | if (reg->off) { |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 1616 | verbose(env, |
| 1617 | "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] | 1618 | regno, reg->off, off - reg->off); |
| 1619 | return -EACCES; |
| 1620 | } |
| 1621 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1622 | char tn_buf[48]; |
| 1623 | |
| 1624 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1625 | verbose(env, |
| 1626 | "variable ctx access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1627 | tn_buf, off, size); |
| 1628 | return -EACCES; |
| 1629 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1630 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1631 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1632 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1633 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 1634 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1635 | */ |
| 1636 | if (reg_type == SCALAR_VALUE) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1637 | mark_reg_unknown(env, regs, value_regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1638 | else |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1639 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1640 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1641 | regs[value_regno].id = 0; |
| 1642 | regs[value_regno].off = 0; |
| 1643 | regs[value_regno].range = 0; |
| 1644 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1645 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1646 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1647 | } else if (reg->type == PTR_TO_STACK) { |
| 1648 | /* stack accesses must be at a fixed offset, so that we can |
| 1649 | * determine what type of data were returned. |
| 1650 | * See check_stack_read(). |
| 1651 | */ |
| 1652 | if (!tnum_is_const(reg->var_off)) { |
| 1653 | char tn_buf[48]; |
| 1654 | |
| 1655 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1656 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1657 | tn_buf, off, size); |
| 1658 | return -EACCES; |
| 1659 | } |
| 1660 | off += reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1661 | if (off >= 0 || off < -MAX_BPF_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1662 | verbose(env, "invalid stack off=%d size=%d\n", off, |
| 1663 | size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1664 | return -EACCES; |
| 1665 | } |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1666 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1667 | state = func(env, reg); |
| 1668 | err = update_stack_depth(env, state, off); |
| 1669 | if (err) |
| 1670 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1671 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1672 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1673 | err = check_stack_write(env, state, off, size, |
| 1674 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1675 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1676 | err = check_stack_read(env, state, off, size, |
| 1677 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1678 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1679 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1680 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1681 | return -EACCES; |
| 1682 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1683 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1684 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1685 | verbose(env, "R%d leaks addr into packet\n", |
| 1686 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1687 | return -EACCES; |
| 1688 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1689 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1690 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1691 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1692 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1693 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 1694 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1695 | return -EACCES; |
| 1696 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1697 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1698 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1699 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1700 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 1701 | coerce_reg_to_size(®s[value_regno], size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1702 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1703 | return err; |
| 1704 | } |
| 1705 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1706 | 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] | 1707 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1708 | int err; |
| 1709 | |
| 1710 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 1711 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1712 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1713 | return -EINVAL; |
| 1714 | } |
| 1715 | |
| 1716 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1717 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1718 | if (err) |
| 1719 | return err; |
| 1720 | |
| 1721 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1722 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1723 | if (err) |
| 1724 | return err; |
| 1725 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1726 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1727 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1728 | return -EACCES; |
| 1729 | } |
| 1730 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1731 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1732 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1733 | BPF_SIZE(insn->code), BPF_READ, -1); |
| 1734 | if (err) |
| 1735 | return err; |
| 1736 | |
| 1737 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1738 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1739 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
| 1740 | } |
| 1741 | |
| 1742 | /* when register 'regno' is passed into function that will read 'access_size' |
| 1743 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1744 | * and all elements of stack are initialized. |
| 1745 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 1746 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1747 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1748 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1749 | int access_size, bool zero_size_allowed, |
| 1750 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1751 | { |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1752 | struct bpf_reg_state *reg = cur_regs(env) + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1753 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1754 | int off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1755 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1756 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1757 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1758 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1759 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1760 | return 0; |
| 1761 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1762 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1763 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1764 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1765 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1766 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1767 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1768 | /* Only allow fixed-offset stack reads */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1769 | if (!tnum_is_const(reg->var_off)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1770 | char tn_buf[48]; |
| 1771 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1772 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1773 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1774 | regno, tn_buf); |
Jann Horn | ea25f91 | 2017-12-18 20:11:57 -0800 | [diff] [blame] | 1775 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1776 | } |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1777 | off = reg->off + reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1778 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1779 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1780 | 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] | 1781 | regno, off, access_size); |
| 1782 | return -EACCES; |
| 1783 | } |
| 1784 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1785 | if (meta && meta->raw_mode) { |
| 1786 | meta->access_size = access_size; |
| 1787 | meta->regno = regno; |
| 1788 | return 0; |
| 1789 | } |
| 1790 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1791 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1792 | u8 *stype; |
| 1793 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1794 | slot = -(off + i) - 1; |
| 1795 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1796 | if (state->allocated_stack <= slot) |
| 1797 | goto err; |
| 1798 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 1799 | if (*stype == STACK_MISC) |
| 1800 | goto mark; |
| 1801 | if (*stype == STACK_ZERO) { |
| 1802 | /* helper can write anything into the stack */ |
| 1803 | *stype = STACK_MISC; |
| 1804 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1805 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1806 | err: |
| 1807 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 1808 | off, i, access_size); |
| 1809 | return -EACCES; |
| 1810 | mark: |
| 1811 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 1812 | * the whole slot to be marked as 'read' |
| 1813 | */ |
| 1814 | mark_stack_slot_read(env, env->cur_state, env->cur_state->parent, |
| 1815 | spi, state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1816 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1817 | return update_stack_depth(env, state, off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1820 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 1821 | int access_size, bool zero_size_allowed, |
| 1822 | struct bpf_call_arg_meta *meta) |
| 1823 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1824 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1825 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1826 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1827 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1828 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1829 | return check_packet_access(env, regno, reg->off, access_size, |
| 1830 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1831 | case PTR_TO_MAP_VALUE: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1832 | return check_map_access(env, regno, reg->off, access_size, |
| 1833 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1834 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1835 | return check_stack_boundary(env, regno, access_size, |
| 1836 | zero_size_allowed, meta); |
| 1837 | } |
| 1838 | } |
| 1839 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 1840 | static bool arg_type_is_mem_ptr(enum bpf_arg_type type) |
| 1841 | { |
| 1842 | return type == ARG_PTR_TO_MEM || |
| 1843 | type == ARG_PTR_TO_MEM_OR_NULL || |
| 1844 | type == ARG_PTR_TO_UNINIT_MEM; |
| 1845 | } |
| 1846 | |
| 1847 | static bool arg_type_is_mem_size(enum bpf_arg_type type) |
| 1848 | { |
| 1849 | return type == ARG_CONST_SIZE || |
| 1850 | type == ARG_CONST_SIZE_OR_ZERO; |
| 1851 | } |
| 1852 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1853 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1854 | enum bpf_arg_type arg_type, |
| 1855 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1856 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1857 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1858 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1859 | int err = 0; |
| 1860 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1861 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1862 | return 0; |
| 1863 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1864 | err = check_reg_arg(env, regno, SRC_OP); |
| 1865 | if (err) |
| 1866 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1867 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1868 | if (arg_type == ARG_ANYTHING) { |
| 1869 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1870 | verbose(env, "R%d leaks addr into helper function\n", |
| 1871 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1872 | return -EACCES; |
| 1873 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1874 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1875 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1876 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1877 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1878 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1879 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1880 | return -EACCES; |
| 1881 | } |
| 1882 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1883 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1884 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1885 | expected_type = PTR_TO_STACK; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1886 | if (!type_is_pkt_pointer(type) && |
| 1887 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1888 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1889 | } else if (arg_type == ARG_CONST_SIZE || |
| 1890 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1891 | expected_type = SCALAR_VALUE; |
| 1892 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1893 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1894 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 1895 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1896 | if (type != expected_type) |
| 1897 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1898 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 1899 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1900 | if (type != expected_type) |
| 1901 | goto err_type; |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 1902 | } else if (arg_type_is_mem_ptr(arg_type)) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1903 | expected_type = PTR_TO_STACK; |
| 1904 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1905 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1906 | * happens during stack boundary checking. |
| 1907 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1908 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 1909 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1910 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1911 | else if (!type_is_pkt_pointer(type) && |
| 1912 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1913 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1914 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1915 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1916 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1917 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1918 | return -EFAULT; |
| 1919 | } |
| 1920 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1921 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 1922 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1923 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1924 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 1925 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 1926 | * check that [key, key + map->key_size) are within |
| 1927 | * stack limits and initialized |
| 1928 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1929 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1930 | /* in function declaration map_ptr must come before |
| 1931 | * map_key, so that it's verified and known before |
| 1932 | * we have to check map_key here. Otherwise it means |
| 1933 | * that kernel subsystem misconfigured verifier |
| 1934 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1935 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1936 | return -EACCES; |
| 1937 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1938 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1939 | err = check_packet_access(env, regno, reg->off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1940 | meta->map_ptr->key_size, |
| 1941 | false); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1942 | else |
| 1943 | err = check_stack_boundary(env, regno, |
| 1944 | meta->map_ptr->key_size, |
| 1945 | false, NULL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1946 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1947 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 1948 | * check [value, value + map->value_size) validity |
| 1949 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1950 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1951 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1952 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1953 | return -EACCES; |
| 1954 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1955 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1956 | err = check_packet_access(env, regno, reg->off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1957 | meta->map_ptr->value_size, |
| 1958 | false); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1959 | else |
| 1960 | err = check_stack_boundary(env, regno, |
| 1961 | meta->map_ptr->value_size, |
| 1962 | false, NULL); |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 1963 | } else if (arg_type_is_mem_size(arg_type)) { |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1964 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1965 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1966 | /* The register is SCALAR_VALUE; the access check |
| 1967 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1968 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1969 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1970 | /* For unprivileged variable accesses, disable raw |
| 1971 | * mode so that the program is required to |
| 1972 | * initialize all the memory that the helper could |
| 1973 | * just partially fill up. |
| 1974 | */ |
| 1975 | meta = NULL; |
| 1976 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1977 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1978 | 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] | 1979 | regno); |
| 1980 | return -EACCES; |
| 1981 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1982 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1983 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1984 | err = check_helper_mem_access(env, regno - 1, 0, |
| 1985 | zero_size_allowed, |
| 1986 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1987 | if (err) |
| 1988 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1989 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1990 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1991 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1992 | 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] | 1993 | regno); |
| 1994 | return -EACCES; |
| 1995 | } |
| 1996 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1997 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1998 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2002 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2003 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 2004 | reg_type_str[type], reg_type_str[expected_type]); |
| 2005 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2008 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 2009 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2010 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2011 | if (!map) |
| 2012 | return 0; |
| 2013 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2014 | /* We need a two way check, first is from map perspective ... */ |
| 2015 | switch (map->map_type) { |
| 2016 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 2017 | if (func_id != BPF_FUNC_tail_call) |
| 2018 | goto error; |
| 2019 | break; |
| 2020 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 2021 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2022 | func_id != BPF_FUNC_perf_event_output && |
| 2023 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2024 | goto error; |
| 2025 | break; |
| 2026 | case BPF_MAP_TYPE_STACK_TRACE: |
| 2027 | if (func_id != BPF_FUNC_get_stackid) |
| 2028 | goto error; |
| 2029 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 2030 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 2031 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2032 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2033 | goto error; |
| 2034 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2035 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 2036 | * allow to be modified from bpf side. So do not allow lookup elements |
| 2037 | * for now. |
| 2038 | */ |
| 2039 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2040 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2041 | goto error; |
| 2042 | break; |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2043 | /* Restrict bpf side of cpumap, open when use-cases appear */ |
| 2044 | case BPF_MAP_TYPE_CPUMAP: |
| 2045 | if (func_id != BPF_FUNC_redirect_map) |
| 2046 | goto error; |
| 2047 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2048 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2049 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2050 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2051 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2052 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2053 | case BPF_MAP_TYPE_SOCKMAP: |
| 2054 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2055 | func_id != BPF_FUNC_sock_map_update && |
| 2056 | func_id != BPF_FUNC_map_delete_elem) |
| 2057 | goto error; |
| 2058 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2059 | default: |
| 2060 | break; |
| 2061 | } |
| 2062 | |
| 2063 | /* ... and second from the function itself. */ |
| 2064 | switch (func_id) { |
| 2065 | case BPF_FUNC_tail_call: |
| 2066 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 2067 | goto error; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2068 | if (env->subprog_cnt) { |
| 2069 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 2070 | return -EINVAL; |
| 2071 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2072 | break; |
| 2073 | case BPF_FUNC_perf_event_read: |
| 2074 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2075 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2076 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 2077 | goto error; |
| 2078 | break; |
| 2079 | case BPF_FUNC_get_stackid: |
| 2080 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 2081 | goto error; |
| 2082 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2083 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2084 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2085 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 2086 | goto error; |
| 2087 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2088 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 2089 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
| 2090 | map->map_type != BPF_MAP_TYPE_CPUMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2091 | goto error; |
| 2092 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2093 | case BPF_FUNC_sk_redirect_map: |
| 2094 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2095 | goto error; |
| 2096 | break; |
| 2097 | case BPF_FUNC_sock_map_update: |
| 2098 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2099 | goto error; |
| 2100 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2101 | default: |
| 2102 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2106 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2107 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2108 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2109 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2112 | static bool check_raw_mode_ok(const struct bpf_func_proto *fn) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2113 | { |
| 2114 | int count = 0; |
| 2115 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2116 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2117 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2118 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2119 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2120 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2121 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2122 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2123 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2124 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2125 | count++; |
| 2126 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2127 | /* We only support one arg being in raw mode at the moment, |
| 2128 | * which is sufficient for the helper functions we have |
| 2129 | * right now. |
| 2130 | */ |
| 2131 | return count <= 1; |
| 2132 | } |
| 2133 | |
| 2134 | static bool check_args_pair_invalid(enum bpf_arg_type arg_curr, |
| 2135 | enum bpf_arg_type arg_next) |
| 2136 | { |
| 2137 | return (arg_type_is_mem_ptr(arg_curr) && |
| 2138 | !arg_type_is_mem_size(arg_next)) || |
| 2139 | (!arg_type_is_mem_ptr(arg_curr) && |
| 2140 | arg_type_is_mem_size(arg_next)); |
| 2141 | } |
| 2142 | |
| 2143 | static bool check_arg_pair_ok(const struct bpf_func_proto *fn) |
| 2144 | { |
| 2145 | /* bpf_xxx(..., buf, len) call will access 'len' |
| 2146 | * bytes from memory 'buf'. Both arg types need |
| 2147 | * to be paired, so make sure there's no buggy |
| 2148 | * helper function specification. |
| 2149 | */ |
| 2150 | if (arg_type_is_mem_size(fn->arg1_type) || |
| 2151 | arg_type_is_mem_ptr(fn->arg5_type) || |
| 2152 | check_args_pair_invalid(fn->arg1_type, fn->arg2_type) || |
| 2153 | check_args_pair_invalid(fn->arg2_type, fn->arg3_type) || |
| 2154 | check_args_pair_invalid(fn->arg3_type, fn->arg4_type) || |
| 2155 | check_args_pair_invalid(fn->arg4_type, fn->arg5_type)) |
| 2156 | return false; |
| 2157 | |
| 2158 | return true; |
| 2159 | } |
| 2160 | |
| 2161 | static int check_func_proto(const struct bpf_func_proto *fn) |
| 2162 | { |
| 2163 | return check_raw_mode_ok(fn) && |
| 2164 | check_arg_pair_ok(fn) ? 0 : -EINVAL; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2165 | } |
| 2166 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2167 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 2168 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2169 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2170 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 2171 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2172 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2173 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2174 | int i; |
| 2175 | |
| 2176 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2177 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2178 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2179 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2180 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 2181 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2182 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2183 | reg = &state->stack[i].spilled_ptr; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2184 | if (reg_is_pkt_pointer_any(reg)) |
| 2185 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2189 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 2190 | { |
| 2191 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2192 | int i; |
| 2193 | |
| 2194 | for (i = 0; i <= vstate->curframe; i++) |
| 2195 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 2196 | } |
| 2197 | |
| 2198 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 2199 | int *insn_idx) |
| 2200 | { |
| 2201 | struct bpf_verifier_state *state = env->cur_state; |
| 2202 | struct bpf_func_state *caller, *callee; |
| 2203 | int i, subprog, target_insn; |
| 2204 | |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2205 | if (state->curframe + 1 >= MAX_CALL_FRAMES) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2206 | verbose(env, "the call stack of %d frames is too deep\n", |
Alexei Starovoitov | aada9ce | 2017-12-25 13:15:42 -0800 | [diff] [blame] | 2207 | state->curframe + 2); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2208 | return -E2BIG; |
| 2209 | } |
| 2210 | |
| 2211 | target_insn = *insn_idx + insn->imm; |
| 2212 | subprog = find_subprog(env, target_insn + 1); |
| 2213 | if (subprog < 0) { |
| 2214 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 2215 | target_insn + 1); |
| 2216 | return -EFAULT; |
| 2217 | } |
| 2218 | |
| 2219 | caller = state->frame[state->curframe]; |
| 2220 | if (state->frame[state->curframe + 1]) { |
| 2221 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 2222 | state->curframe + 1); |
| 2223 | return -EFAULT; |
| 2224 | } |
| 2225 | |
| 2226 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 2227 | if (!callee) |
| 2228 | return -ENOMEM; |
| 2229 | state->frame[state->curframe + 1] = callee; |
| 2230 | |
| 2231 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 2232 | * into its own stack before reading from it. |
| 2233 | * callee can read/write into caller's stack |
| 2234 | */ |
| 2235 | init_func_state(env, callee, |
| 2236 | /* remember the callsite, it will be used by bpf_exit */ |
| 2237 | *insn_idx /* callsite */, |
| 2238 | state->curframe + 1 /* frameno within this callchain */, |
| 2239 | subprog + 1 /* subprog number within this prog */); |
| 2240 | |
| 2241 | /* copy r1 - r5 args that callee can access */ |
| 2242 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 2243 | callee->regs[i] = caller->regs[i]; |
| 2244 | |
| 2245 | /* after the call regsiters r0 - r5 were scratched */ |
| 2246 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 2247 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 2248 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2249 | } |
| 2250 | |
| 2251 | /* only increment it after check_reg_arg() finished */ |
| 2252 | state->curframe++; |
| 2253 | |
| 2254 | /* and go analyze first insn of the callee */ |
| 2255 | *insn_idx = target_insn; |
| 2256 | |
| 2257 | if (env->log.level) { |
| 2258 | verbose(env, "caller:\n"); |
| 2259 | print_verifier_state(env, caller); |
| 2260 | verbose(env, "callee:\n"); |
| 2261 | print_verifier_state(env, callee); |
| 2262 | } |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
| 2266 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 2267 | { |
| 2268 | struct bpf_verifier_state *state = env->cur_state; |
| 2269 | struct bpf_func_state *caller, *callee; |
| 2270 | struct bpf_reg_state *r0; |
| 2271 | |
| 2272 | callee = state->frame[state->curframe]; |
| 2273 | r0 = &callee->regs[BPF_REG_0]; |
| 2274 | if (r0->type == PTR_TO_STACK) { |
| 2275 | /* technically it's ok to return caller's stack pointer |
| 2276 | * (or caller's caller's pointer) back to the caller, |
| 2277 | * since these pointers are valid. Only current stack |
| 2278 | * pointer will be invalid as soon as function exits, |
| 2279 | * but let's be conservative |
| 2280 | */ |
| 2281 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 2282 | return -EINVAL; |
| 2283 | } |
| 2284 | |
| 2285 | state->curframe--; |
| 2286 | caller = state->frame[state->curframe]; |
| 2287 | /* return to the caller whatever r0 had in the callee */ |
| 2288 | caller->regs[BPF_REG_0] = *r0; |
| 2289 | |
| 2290 | *insn_idx = callee->callsite + 1; |
| 2291 | if (env->log.level) { |
| 2292 | verbose(env, "returning from callee:\n"); |
| 2293 | print_verifier_state(env, callee); |
| 2294 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 2295 | print_verifier_state(env, caller); |
| 2296 | } |
| 2297 | /* clear everything in the callee */ |
| 2298 | free_func_state(callee); |
| 2299 | state->frame[state->curframe + 1] = NULL; |
| 2300 | return 0; |
| 2301 | } |
| 2302 | |
| 2303 | 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] | 2304 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2305 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2306 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2307 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2308 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2309 | int i, err; |
| 2310 | |
| 2311 | /* find function prototype */ |
| 2312 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2313 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 2314 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2315 | return -EINVAL; |
| 2316 | } |
| 2317 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 2318 | if (env->ops->get_func_proto) |
| 2319 | fn = env->ops->get_func_proto(func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2320 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2321 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 2322 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2323 | return -EINVAL; |
| 2324 | } |
| 2325 | |
| 2326 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 2327 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2328 | verbose(env, "cannot call GPL only function from proprietary program\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2329 | return -EINVAL; |
| 2330 | } |
| 2331 | |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2332 | /* With LD_ABS/IND some JITs save/restore skb from r1. */ |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 2333 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Daniel Borkmann | 04514d1 | 2017-12-14 21:07:25 +0100 | [diff] [blame] | 2334 | if (changes_data && fn->arg1_type != ARG_PTR_TO_CTX) { |
| 2335 | verbose(env, "kernel subsystem misconfigured func %s#%d: r1 != ctx\n", |
| 2336 | func_id_name(func_id), func_id); |
| 2337 | return -EINVAL; |
| 2338 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2339 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2340 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2341 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2342 | |
Daniel Borkmann | 9013341 | 2018-01-20 01:24:29 +0100 | [diff] [blame] | 2343 | err = check_func_proto(fn); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2344 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2345 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2346 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2347 | return err; |
| 2348 | } |
| 2349 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2350 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2351 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2352 | if (err) |
| 2353 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2354 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2355 | if (err) |
| 2356 | return err; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 2357 | if (func_id == BPF_FUNC_tail_call) { |
| 2358 | if (meta.map_ptr == NULL) { |
| 2359 | verbose(env, "verifier bug\n"); |
| 2360 | return -EINVAL; |
| 2361 | } |
| 2362 | env->insn_aux_data[insn_idx].map_ptr = meta.map_ptr; |
| 2363 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2364 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2365 | if (err) |
| 2366 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2367 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2368 | if (err) |
| 2369 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2370 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2371 | if (err) |
| 2372 | return err; |
| 2373 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2374 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 2375 | * is inferred from register state. |
| 2376 | */ |
| 2377 | for (i = 0; i < meta.access_size; i++) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2378 | err = check_mem_access(env, insn_idx, meta.regno, i, BPF_B, BPF_WRITE, -1); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2379 | if (err) |
| 2380 | return err; |
| 2381 | } |
| 2382 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2383 | regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2384 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2385 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2386 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2387 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2388 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2389 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2390 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2391 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2392 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2393 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2394 | } else if (fn->ret_type == RET_VOID) { |
| 2395 | regs[BPF_REG_0].type = NOT_INIT; |
| 2396 | } 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] | 2397 | struct bpf_insn_aux_data *insn_aux; |
| 2398 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2399 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2400 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2401 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2402 | regs[BPF_REG_0].off = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2403 | /* remember map_ptr, so that check_map_access() |
| 2404 | * can check 'value_size' boundary of memory access |
| 2405 | * to map element returned from bpf_map_lookup_elem() |
| 2406 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2407 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2408 | verbose(env, |
| 2409 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2410 | return -EINVAL; |
| 2411 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2412 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2413 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 2414 | insn_aux = &env->insn_aux_data[insn_idx]; |
| 2415 | if (!insn_aux->map_ptr) |
| 2416 | insn_aux->map_ptr = meta.map_ptr; |
| 2417 | else if (insn_aux->map_ptr != meta.map_ptr) |
| 2418 | insn_aux->map_ptr = BPF_MAP_PTR_POISON; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2419 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2420 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2421 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2422 | return -EINVAL; |
| 2423 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2424 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2425 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2426 | if (err) |
| 2427 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2428 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2429 | if (changes_data) |
| 2430 | clear_all_pkt_pointers(env); |
| 2431 | return 0; |
| 2432 | } |
| 2433 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2434 | static bool signed_add_overflows(s64 a, s64 b) |
| 2435 | { |
| 2436 | /* Do the add in u64, where overflow is well-defined */ |
| 2437 | s64 res = (s64)((u64)a + (u64)b); |
| 2438 | |
| 2439 | if (b < 0) |
| 2440 | return res > a; |
| 2441 | return res < a; |
| 2442 | } |
| 2443 | |
| 2444 | static bool signed_sub_overflows(s64 a, s64 b) |
| 2445 | { |
| 2446 | /* Do the sub in u64, where overflow is well-defined */ |
| 2447 | s64 res = (s64)((u64)a - (u64)b); |
| 2448 | |
| 2449 | if (b < 0) |
| 2450 | return res < a; |
| 2451 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2452 | } |
| 2453 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2454 | static bool check_reg_sane_offset(struct bpf_verifier_env *env, |
| 2455 | const struct bpf_reg_state *reg, |
| 2456 | enum bpf_reg_type type) |
| 2457 | { |
| 2458 | bool known = tnum_is_const(reg->var_off); |
| 2459 | s64 val = reg->var_off.value; |
| 2460 | s64 smin = reg->smin_value; |
| 2461 | |
| 2462 | if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) { |
| 2463 | verbose(env, "math between %s pointer and %lld is not allowed\n", |
| 2464 | reg_type_str[type], val); |
| 2465 | return false; |
| 2466 | } |
| 2467 | |
| 2468 | if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) { |
| 2469 | verbose(env, "%s pointer offset %d is not allowed\n", |
| 2470 | reg_type_str[type], reg->off); |
| 2471 | return false; |
| 2472 | } |
| 2473 | |
| 2474 | if (smin == S64_MIN) { |
| 2475 | verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n", |
| 2476 | reg_type_str[type]); |
| 2477 | return false; |
| 2478 | } |
| 2479 | |
| 2480 | if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) { |
| 2481 | verbose(env, "value %lld makes %s pointer be out of bounds\n", |
| 2482 | smin, reg_type_str[type]); |
| 2483 | return false; |
| 2484 | } |
| 2485 | |
| 2486 | return true; |
| 2487 | } |
| 2488 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2489 | /* 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] | 2490 | * Caller should also handle BPF_MOV case separately. |
| 2491 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 2492 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 2493 | */ |
| 2494 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 2495 | struct bpf_insn *insn, |
| 2496 | const struct bpf_reg_state *ptr_reg, |
| 2497 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2498 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2499 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2500 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2501 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2502 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2503 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 2504 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 2505 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 2506 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2507 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2508 | u32 dst = insn->dst_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2509 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2510 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2511 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2512 | if (WARN_ON_ONCE(known && (smin_val != smax_val))) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2513 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2514 | verbose(env, |
| 2515 | "verifier internal error: known but bad sbounds\n"); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2516 | return -EINVAL; |
| 2517 | } |
| 2518 | if (WARN_ON_ONCE(known && (umin_val != umax_val))) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2519 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2520 | verbose(env, |
| 2521 | "verifier internal error: known but bad ubounds\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2522 | return -EINVAL; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2523 | } |
| 2524 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2525 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 2526 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2527 | verbose(env, |
| 2528 | "R%d 32-bit pointer arithmetic prohibited\n", |
| 2529 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2530 | return -EACCES; |
| 2531 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2532 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2533 | if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2534 | verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n", |
| 2535 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2536 | return -EACCES; |
| 2537 | } |
| 2538 | if (ptr_reg->type == CONST_PTR_TO_MAP) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2539 | verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n", |
| 2540 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2541 | return -EACCES; |
| 2542 | } |
| 2543 | if (ptr_reg->type == PTR_TO_PACKET_END) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2544 | verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n", |
| 2545 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2546 | return -EACCES; |
| 2547 | } |
| 2548 | |
| 2549 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 2550 | * 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] | 2551 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2552 | dst_reg->type = ptr_reg->type; |
| 2553 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2554 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2555 | if (!check_reg_sane_offset(env, off_reg, ptr_reg->type) || |
| 2556 | !check_reg_sane_offset(env, ptr_reg, ptr_reg->type)) |
| 2557 | return -EINVAL; |
| 2558 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2559 | switch (opcode) { |
| 2560 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2561 | /* We can take a fixed offset as long as it doesn't overflow |
| 2562 | * the s32 'off' field |
| 2563 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2564 | if (known && (ptr_reg->off + smin_val == |
| 2565 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2566 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2567 | dst_reg->smin_value = smin_ptr; |
| 2568 | dst_reg->smax_value = smax_ptr; |
| 2569 | dst_reg->umin_value = umin_ptr; |
| 2570 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2571 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2572 | dst_reg->off = ptr_reg->off + smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2573 | dst_reg->range = ptr_reg->range; |
| 2574 | break; |
| 2575 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2576 | /* A new variable offset is created. Note that off_reg->off |
| 2577 | * == 0, since it's a scalar. |
| 2578 | * dst_reg gets the pointer type and since some positive |
| 2579 | * integer value was added to the pointer, give it a new 'id' |
| 2580 | * if it's a PTR_TO_PACKET. |
| 2581 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 2582 | * added into the variable offset, and we copy the fixed offset |
| 2583 | * from ptr_reg. |
| 2584 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2585 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 2586 | signed_add_overflows(smax_ptr, smax_val)) { |
| 2587 | dst_reg->smin_value = S64_MIN; |
| 2588 | dst_reg->smax_value = S64_MAX; |
| 2589 | } else { |
| 2590 | dst_reg->smin_value = smin_ptr + smin_val; |
| 2591 | dst_reg->smax_value = smax_ptr + smax_val; |
| 2592 | } |
| 2593 | if (umin_ptr + umin_val < umin_ptr || |
| 2594 | umax_ptr + umax_val < umax_ptr) { |
| 2595 | dst_reg->umin_value = 0; |
| 2596 | dst_reg->umax_value = U64_MAX; |
| 2597 | } else { |
| 2598 | dst_reg->umin_value = umin_ptr + umin_val; |
| 2599 | dst_reg->umax_value = umax_ptr + umax_val; |
| 2600 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2601 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 2602 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2603 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2604 | dst_reg->id = ++env->id_gen; |
| 2605 | /* something was added to pkt_ptr, set range to zero */ |
| 2606 | dst_reg->range = 0; |
| 2607 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2608 | break; |
| 2609 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2610 | if (dst_reg == off_reg) { |
| 2611 | /* scalar -= pointer. Creates an unknown scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2612 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
| 2613 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2614 | return -EACCES; |
| 2615 | } |
| 2616 | /* We don't allow subtraction from FP, because (according to |
| 2617 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 2618 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 2619 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2620 | if (ptr_reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2621 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
| 2622 | dst); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2623 | return -EACCES; |
| 2624 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2625 | if (known && (ptr_reg->off - smin_val == |
| 2626 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2627 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2628 | dst_reg->smin_value = smin_ptr; |
| 2629 | dst_reg->smax_value = smax_ptr; |
| 2630 | dst_reg->umin_value = umin_ptr; |
| 2631 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2632 | dst_reg->var_off = ptr_reg->var_off; |
| 2633 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2634 | dst_reg->off = ptr_reg->off - smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2635 | dst_reg->range = ptr_reg->range; |
| 2636 | break; |
| 2637 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2638 | /* A new variable offset is created. If the subtrahend is known |
| 2639 | * nonnegative, then any reg->range we had before is still good. |
| 2640 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2641 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 2642 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 2643 | /* Overflow possible, we know nothing */ |
| 2644 | dst_reg->smin_value = S64_MIN; |
| 2645 | dst_reg->smax_value = S64_MAX; |
| 2646 | } else { |
| 2647 | dst_reg->smin_value = smin_ptr - smax_val; |
| 2648 | dst_reg->smax_value = smax_ptr - smin_val; |
| 2649 | } |
| 2650 | if (umin_ptr < umax_val) { |
| 2651 | /* Overflow possible, we know nothing */ |
| 2652 | dst_reg->umin_value = 0; |
| 2653 | dst_reg->umax_value = U64_MAX; |
| 2654 | } else { |
| 2655 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2656 | dst_reg->umin_value = umin_ptr - umax_val; |
| 2657 | dst_reg->umax_value = umax_ptr - umin_val; |
| 2658 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2659 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 2660 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2661 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2662 | dst_reg->id = ++env->id_gen; |
| 2663 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2664 | if (smin_val < 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2665 | dst_reg->range = 0; |
| 2666 | } |
| 2667 | break; |
| 2668 | case BPF_AND: |
| 2669 | case BPF_OR: |
| 2670 | case BPF_XOR: |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2671 | /* bitwise ops on pointers are troublesome, prohibit. */ |
| 2672 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
| 2673 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2674 | return -EACCES; |
| 2675 | default: |
| 2676 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2677 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
| 2678 | dst, bpf_alu_string[opcode >> 4]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2679 | return -EACCES; |
| 2680 | } |
| 2681 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2682 | if (!check_reg_sane_offset(env, dst_reg, ptr_reg->type)) |
| 2683 | return -EINVAL; |
| 2684 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2685 | __update_reg_bounds(dst_reg); |
| 2686 | __reg_deduce_bounds(dst_reg); |
| 2687 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2688 | return 0; |
| 2689 | } |
| 2690 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2691 | /* WARNING: This function does calculations on 64-bit values, but the actual |
| 2692 | * execution may occur on 32-bit values. Therefore, things like bitshifts |
| 2693 | * need extra checks in the 32-bit case. |
| 2694 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2695 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 2696 | struct bpf_insn *insn, |
| 2697 | struct bpf_reg_state *dst_reg, |
| 2698 | struct bpf_reg_state src_reg) |
| 2699 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2700 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2701 | u8 opcode = BPF_OP(insn->code); |
| 2702 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2703 | s64 smin_val, smax_val; |
| 2704 | u64 umin_val, umax_val; |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2705 | u64 insn_bitness = (BPF_CLASS(insn->code) == BPF_ALU64) ? 64 : 32; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2706 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2707 | smin_val = src_reg.smin_value; |
| 2708 | smax_val = src_reg.smax_value; |
| 2709 | umin_val = src_reg.umin_value; |
| 2710 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2711 | src_known = tnum_is_const(src_reg.var_off); |
| 2712 | dst_known = tnum_is_const(dst_reg->var_off); |
| 2713 | |
Alexei Starovoitov | bb7f0f9 | 2017-12-18 20:12:00 -0800 | [diff] [blame] | 2714 | if (!src_known && |
| 2715 | opcode != BPF_ADD && opcode != BPF_SUB && opcode != BPF_AND) { |
| 2716 | __mark_reg_unknown(dst_reg); |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2720 | switch (opcode) { |
| 2721 | case BPF_ADD: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2722 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 2723 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 2724 | dst_reg->smin_value = S64_MIN; |
| 2725 | dst_reg->smax_value = S64_MAX; |
| 2726 | } else { |
| 2727 | dst_reg->smin_value += smin_val; |
| 2728 | dst_reg->smax_value += smax_val; |
| 2729 | } |
| 2730 | if (dst_reg->umin_value + umin_val < umin_val || |
| 2731 | dst_reg->umax_value + umax_val < umax_val) { |
| 2732 | dst_reg->umin_value = 0; |
| 2733 | dst_reg->umax_value = U64_MAX; |
| 2734 | } else { |
| 2735 | dst_reg->umin_value += umin_val; |
| 2736 | dst_reg->umax_value += umax_val; |
| 2737 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2738 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 2739 | break; |
| 2740 | case BPF_SUB: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2741 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 2742 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 2743 | /* Overflow possible, we know nothing */ |
| 2744 | dst_reg->smin_value = S64_MIN; |
| 2745 | dst_reg->smax_value = S64_MAX; |
| 2746 | } else { |
| 2747 | dst_reg->smin_value -= smax_val; |
| 2748 | dst_reg->smax_value -= smin_val; |
| 2749 | } |
| 2750 | if (dst_reg->umin_value < umax_val) { |
| 2751 | /* Overflow possible, we know nothing */ |
| 2752 | dst_reg->umin_value = 0; |
| 2753 | dst_reg->umax_value = U64_MAX; |
| 2754 | } else { |
| 2755 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2756 | dst_reg->umin_value -= umax_val; |
| 2757 | dst_reg->umax_value -= umin_val; |
| 2758 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2759 | 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] | 2760 | break; |
| 2761 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2762 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 2763 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2764 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2765 | __mark_reg_unbounded(dst_reg); |
| 2766 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2767 | break; |
| 2768 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2769 | /* Both values are positive, so we can work with unsigned and |
| 2770 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2771 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2772 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 2773 | /* Potential overflow, we know nothing */ |
| 2774 | __mark_reg_unbounded(dst_reg); |
| 2775 | /* (except what we can learn from the var_off) */ |
| 2776 | __update_reg_bounds(dst_reg); |
| 2777 | break; |
| 2778 | } |
| 2779 | dst_reg->umin_value *= umin_val; |
| 2780 | dst_reg->umax_value *= umax_val; |
| 2781 | if (dst_reg->umax_value > S64_MAX) { |
| 2782 | /* Overflow possible, we know nothing */ |
| 2783 | dst_reg->smin_value = S64_MIN; |
| 2784 | dst_reg->smax_value = S64_MAX; |
| 2785 | } else { |
| 2786 | dst_reg->smin_value = dst_reg->umin_value; |
| 2787 | dst_reg->smax_value = dst_reg->umax_value; |
| 2788 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2789 | break; |
| 2790 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2791 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2792 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 2793 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2794 | break; |
| 2795 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2796 | /* We get our minimum from the var_off, since that's inherently |
| 2797 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2798 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2799 | 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] | 2800 | dst_reg->umin_value = dst_reg->var_off.value; |
| 2801 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 2802 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2803 | /* Lose signed bounds when ANDing negative numbers, |
| 2804 | * ain't nobody got time for that. |
| 2805 | */ |
| 2806 | dst_reg->smin_value = S64_MIN; |
| 2807 | dst_reg->smax_value = S64_MAX; |
| 2808 | } else { |
| 2809 | /* ANDing two positives gives a positive, so safe to |
| 2810 | * cast result into s64. |
| 2811 | */ |
| 2812 | dst_reg->smin_value = dst_reg->umin_value; |
| 2813 | dst_reg->smax_value = dst_reg->umax_value; |
| 2814 | } |
| 2815 | /* We may learn something more from the var_off */ |
| 2816 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2817 | break; |
| 2818 | case BPF_OR: |
| 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 maximum from the var_off, and our minimum is the |
| 2825 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2826 | */ |
| 2827 | 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] | 2828 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 2829 | dst_reg->umax_value = dst_reg->var_off.value | |
| 2830 | dst_reg->var_off.mask; |
| 2831 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2832 | /* Lose signed bounds when ORing negative numbers, |
| 2833 | * ain't nobody got time for that. |
| 2834 | */ |
| 2835 | dst_reg->smin_value = S64_MIN; |
| 2836 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2837 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2838 | /* ORing two positives gives a positive, so safe to |
| 2839 | * cast result into s64. |
| 2840 | */ |
| 2841 | dst_reg->smin_value = dst_reg->umin_value; |
| 2842 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2843 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2844 | /* We may learn something more from the var_off */ |
| 2845 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2846 | break; |
| 2847 | case BPF_LSH: |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2848 | if (umax_val >= insn_bitness) { |
| 2849 | /* Shifts greater than 31 or 63 are undefined. |
| 2850 | * This includes shifts by a negative number. |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2851 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2852 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2853 | break; |
| 2854 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2855 | /* We lose all sign bit information (except what we can pick |
| 2856 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2857 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2858 | dst_reg->smin_value = S64_MIN; |
| 2859 | dst_reg->smax_value = S64_MAX; |
| 2860 | /* If we might shift our top bit out, then we know nothing */ |
| 2861 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 2862 | dst_reg->umin_value = 0; |
| 2863 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2864 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2865 | dst_reg->umin_value <<= umin_val; |
| 2866 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2867 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2868 | if (src_known) |
| 2869 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 2870 | else |
| 2871 | dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val); |
| 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_RSH: |
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 | 4374f25 | 2017-12-18 20:11:53 -0800 | [diff] [blame] | 2883 | /* BPF_RSH is an unsigned shift. If the value in dst_reg might |
| 2884 | * be negative, then either: |
| 2885 | * 1) src_reg might be zero, so the sign bit of the result is |
| 2886 | * unknown, so we lose our signed bounds |
| 2887 | * 2) it's known negative, thus the unsigned bounds capture the |
| 2888 | * signed bounds |
| 2889 | * 3) the signed bounds cross zero, so they tell us nothing |
| 2890 | * about the result |
| 2891 | * If the value in dst_reg is known nonnegative, then again the |
| 2892 | * unsigned bounts capture the signed bounds. |
| 2893 | * Thus, in all cases it suffices to blow away our signed bounds |
| 2894 | * and rely on inferring new ones from the unsigned bounds and |
| 2895 | * var_off of the result. |
| 2896 | */ |
| 2897 | dst_reg->smin_value = S64_MIN; |
| 2898 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2899 | if (src_known) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2900 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, |
| 2901 | umin_val); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2902 | else |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2903 | dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val); |
| 2904 | dst_reg->umin_value >>= umax_val; |
| 2905 | dst_reg->umax_value >>= umin_val; |
| 2906 | /* We may learn something more from the var_off */ |
| 2907 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2908 | break; |
| 2909 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2910 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2911 | break; |
| 2912 | } |
| 2913 | |
Jann Horn | 468f6ea | 2017-12-18 20:11:56 -0800 | [diff] [blame] | 2914 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 2915 | /* 32-bit ALU ops are (32,32)->32 */ |
| 2916 | coerce_reg_to_size(dst_reg, 4); |
| 2917 | coerce_reg_to_size(&src_reg, 4); |
| 2918 | } |
| 2919 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2920 | __reg_deduce_bounds(dst_reg); |
| 2921 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2922 | return 0; |
| 2923 | } |
| 2924 | |
| 2925 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 2926 | * and var_off. |
| 2927 | */ |
| 2928 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 2929 | struct bpf_insn *insn) |
| 2930 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2931 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2932 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2933 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2934 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 2935 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2936 | |
| 2937 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2938 | src_reg = NULL; |
| 2939 | if (dst_reg->type != SCALAR_VALUE) |
| 2940 | ptr_reg = dst_reg; |
| 2941 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2942 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2943 | if (src_reg->type != SCALAR_VALUE) { |
| 2944 | if (dst_reg->type != SCALAR_VALUE) { |
| 2945 | /* Combining two pointers by any ALU op yields |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2946 | * an arbitrary scalar. Disallow all math except |
| 2947 | * pointer subtraction |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2948 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2949 | if (opcode == BPF_SUB){ |
| 2950 | mark_reg_unknown(env, regs, insn->dst_reg); |
| 2951 | return 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2952 | } |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2953 | verbose(env, "R%d pointer %s pointer prohibited\n", |
| 2954 | insn->dst_reg, |
| 2955 | bpf_alu_string[opcode >> 4]); |
| 2956 | return -EACCES; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2957 | } else { |
| 2958 | /* scalar += pointer |
| 2959 | * This is legal, but we have to reverse our |
| 2960 | * src/dest handling in computing the range |
| 2961 | */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2962 | return adjust_ptr_min_max_vals(env, insn, |
| 2963 | src_reg, dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2964 | } |
| 2965 | } else if (ptr_reg) { |
| 2966 | /* pointer += scalar */ |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2967 | return adjust_ptr_min_max_vals(env, insn, |
| 2968 | dst_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2969 | } |
| 2970 | } else { |
| 2971 | /* Pretend the src is a reg with a known value, since we only |
| 2972 | * need to be able to read from this state. |
| 2973 | */ |
| 2974 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2975 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2976 | src_reg = &off_reg; |
Alexei Starovoitov | 82abbf8 | 2017-12-18 20:15:20 -0800 | [diff] [blame] | 2977 | if (ptr_reg) /* pointer += K */ |
| 2978 | return adjust_ptr_min_max_vals(env, insn, |
| 2979 | ptr_reg, src_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2980 | } |
| 2981 | |
| 2982 | /* Got here implies adding two SCALAR_VALUEs */ |
| 2983 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2984 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2985 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2986 | return -EINVAL; |
| 2987 | } |
| 2988 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2989 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2990 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2991 | return -EINVAL; |
| 2992 | } |
| 2993 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2994 | } |
| 2995 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2996 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2997 | 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] | 2998 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2999 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3000 | u8 opcode = BPF_OP(insn->code); |
| 3001 | int err; |
| 3002 | |
| 3003 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 3004 | if (opcode == BPF_NEG) { |
| 3005 | if (BPF_SRC(insn->code) != 0 || |
| 3006 | insn->src_reg != BPF_REG_0 || |
| 3007 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3008 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3009 | return -EINVAL; |
| 3010 | } |
| 3011 | } else { |
| 3012 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 3013 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 3014 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3015 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3016 | return -EINVAL; |
| 3017 | } |
| 3018 | } |
| 3019 | |
| 3020 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3021 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3022 | if (err) |
| 3023 | return err; |
| 3024 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3025 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3026 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3027 | insn->dst_reg); |
| 3028 | return -EACCES; |
| 3029 | } |
| 3030 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3031 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3032 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3033 | if (err) |
| 3034 | return err; |
| 3035 | |
| 3036 | } else if (opcode == BPF_MOV) { |
| 3037 | |
| 3038 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3039 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3040 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3041 | return -EINVAL; |
| 3042 | } |
| 3043 | |
| 3044 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3045 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3046 | if (err) |
| 3047 | return err; |
| 3048 | } else { |
| 3049 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3050 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3051 | return -EINVAL; |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3056 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3057 | if (err) |
| 3058 | return err; |
| 3059 | |
| 3060 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3061 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3062 | /* case: R1 = R2 |
| 3063 | * copy register state to dest reg |
| 3064 | */ |
| 3065 | regs[insn->dst_reg] = regs[insn->src_reg]; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 3066 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3067 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3068 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3069 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3070 | verbose(env, |
| 3071 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3072 | insn->src_reg); |
| 3073 | return -EACCES; |
| 3074 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3075 | mark_reg_unknown(env, regs, insn->dst_reg); |
Jann Horn | 0c17d1d | 2017-12-18 20:11:55 -0800 | [diff] [blame] | 3076 | coerce_reg_to_size(®s[insn->dst_reg], 4); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3077 | } |
| 3078 | } else { |
| 3079 | /* case: R = imm |
| 3080 | * remember the value we stored into this reg |
| 3081 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3082 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Jann Horn | 95a762e | 2017-12-18 20:11:54 -0800 | [diff] [blame] | 3083 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 3084 | __mark_reg_known(regs + insn->dst_reg, |
| 3085 | insn->imm); |
| 3086 | } else { |
| 3087 | __mark_reg_known(regs + insn->dst_reg, |
| 3088 | (u32)insn->imm); |
| 3089 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3093 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3094 | return -EINVAL; |
| 3095 | |
| 3096 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 3097 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3098 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3099 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3100 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3101 | return -EINVAL; |
| 3102 | } |
| 3103 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3104 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3105 | if (err) |
| 3106 | return err; |
| 3107 | } else { |
| 3108 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3109 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3110 | return -EINVAL; |
| 3111 | } |
| 3112 | } |
| 3113 | |
| 3114 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3115 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3116 | if (err) |
| 3117 | return err; |
| 3118 | |
| 3119 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 3120 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3121 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3122 | return -EINVAL; |
| 3123 | } |
| 3124 | |
Daniel Borkmann | 7891a87 | 2018-01-10 20:04:37 +0100 | [diff] [blame] | 3125 | if (opcode == BPF_ARSH && BPF_CLASS(insn->code) != BPF_ALU64) { |
| 3126 | verbose(env, "BPF_ARSH not supported for 32 bit ALU\n"); |
| 3127 | return -EINVAL; |
| 3128 | } |
| 3129 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3130 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 3131 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 3132 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 3133 | |
| 3134 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3135 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3136 | return -EINVAL; |
| 3137 | } |
| 3138 | } |
| 3139 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3140 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3141 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3142 | if (err) |
| 3143 | return err; |
| 3144 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3145 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | return 0; |
| 3149 | } |
| 3150 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3151 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3152 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 3153 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3154 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3155 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3156 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3157 | struct bpf_reg_state *regs = state->regs, *reg; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3158 | u16 new_range; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3159 | int i, j; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3160 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3161 | if (dst_reg->off < 0 || |
| 3162 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3163 | /* This doesn't give us any range */ |
| 3164 | return; |
| 3165 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3166 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 3167 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3168 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 3169 | * than pkt_end, but that's because it's also less than pkt. |
| 3170 | */ |
| 3171 | return; |
| 3172 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3173 | new_range = dst_reg->off; |
| 3174 | if (range_right_open) |
| 3175 | new_range--; |
| 3176 | |
| 3177 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3178 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3179 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3180 | * |
| 3181 | * r2 = r3; |
| 3182 | * r2 += 8; |
| 3183 | * if (r2 > pkt_end) goto <handle exception> |
| 3184 | * <access okay> |
| 3185 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3186 | * r2 = r3; |
| 3187 | * r2 += 8; |
| 3188 | * if (r2 < pkt_end) goto <access okay> |
| 3189 | * <handle exception> |
| 3190 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3191 | * Where: |
| 3192 | * r2 == dst_reg, pkt_end == src_reg |
| 3193 | * r2=pkt(id=n,off=8,r=0) |
| 3194 | * r3=pkt(id=n,off=0,r=0) |
| 3195 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3196 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3197 | * |
| 3198 | * r2 = r3; |
| 3199 | * r2 += 8; |
| 3200 | * if (pkt_end >= r2) goto <access okay> |
| 3201 | * <handle exception> |
| 3202 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3203 | * r2 = r3; |
| 3204 | * r2 += 8; |
| 3205 | * if (pkt_end <= r2) goto <handle exception> |
| 3206 | * <access okay> |
| 3207 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3208 | * Where: |
| 3209 | * pkt_end == dst_reg, r2 == src_reg |
| 3210 | * r2=pkt(id=n,off=8,r=0) |
| 3211 | * r3=pkt(id=n,off=0,r=0) |
| 3212 | * |
| 3213 | * 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] | 3214 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 3215 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 3216 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3217 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3218 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3219 | /* If our ids match, then we must have the same max_value. And we |
| 3220 | * don't care about the other reg's fixed offset, since if it's too big |
| 3221 | * the range won't allow anything. |
| 3222 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 3223 | */ |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3224 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3225 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
Alexei Starovoitov | b197768 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 3226 | /* keep the maximum range already checked */ |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3227 | regs[i].range = max(regs[i].range, new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3228 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3229 | for (j = 0; j <= vstate->curframe; j++) { |
| 3230 | state = vstate->frame[j]; |
| 3231 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 3232 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 3233 | continue; |
| 3234 | reg = &state->stack[i].spilled_ptr; |
| 3235 | if (reg->type == type && reg->id == dst_reg->id) |
| 3236 | reg->range = max(reg->range, new_range); |
| 3237 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3241 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 3242 | * variable register that we are working on, and src_reg is a constant or we're |
| 3243 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3244 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3245 | */ |
| 3246 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 3247 | struct bpf_reg_state *false_reg, u64 val, |
| 3248 | u8 opcode) |
| 3249 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3250 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 3251 | * variable offset from the compare (unless src_reg were a pointer into |
| 3252 | * the same object, but we don't bother with that. |
| 3253 | * Since false_reg and true_reg have the same type by construction, we |
| 3254 | * only need to check one of them for pointerness. |
| 3255 | */ |
| 3256 | if (__is_pointer_value(false, false_reg)) |
| 3257 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3258 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3259 | switch (opcode) { |
| 3260 | case BPF_JEQ: |
| 3261 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3262 | * true then we know for sure. |
| 3263 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3264 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3265 | break; |
| 3266 | case BPF_JNE: |
| 3267 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3268 | * we know the value for sure; |
| 3269 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3270 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3271 | break; |
| 3272 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3273 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3274 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3275 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3276 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3277 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3278 | 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] | 3279 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3280 | case BPF_JLT: |
| 3281 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3282 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3283 | break; |
| 3284 | case BPF_JSLT: |
| 3285 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
| 3286 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3287 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3288 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3289 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3290 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3291 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3292 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3293 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3294 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3295 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3296 | case BPF_JLE: |
| 3297 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3298 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3299 | break; |
| 3300 | case BPF_JSLE: |
| 3301 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
| 3302 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3303 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3304 | default: |
| 3305 | break; |
| 3306 | } |
| 3307 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3308 | __reg_deduce_bounds(false_reg); |
| 3309 | __reg_deduce_bounds(true_reg); |
| 3310 | /* We might have learned some bits from the bounds. */ |
| 3311 | __reg_bound_offset(false_reg); |
| 3312 | __reg_bound_offset(true_reg); |
| 3313 | /* Intersecting with the old var_off might have improved our bounds |
| 3314 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3315 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3316 | */ |
| 3317 | __update_reg_bounds(false_reg); |
| 3318 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3319 | } |
| 3320 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3321 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 3322 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3323 | */ |
| 3324 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 3325 | struct bpf_reg_state *false_reg, u64 val, |
| 3326 | u8 opcode) |
| 3327 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3328 | if (__is_pointer_value(false, false_reg)) |
| 3329 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3330 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3331 | switch (opcode) { |
| 3332 | case BPF_JEQ: |
| 3333 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3334 | * true then we know for sure. |
| 3335 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3336 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3337 | break; |
| 3338 | case BPF_JNE: |
| 3339 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3340 | * we know the value for sure; |
| 3341 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3342 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3343 | break; |
| 3344 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3345 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3346 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3347 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3348 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3349 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3350 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3351 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3352 | case BPF_JLT: |
| 3353 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3354 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3355 | break; |
| 3356 | case BPF_JSLT: |
| 3357 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
| 3358 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3359 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3360 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3361 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3362 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3363 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3364 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3365 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3366 | 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] | 3367 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3368 | case BPF_JLE: |
| 3369 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3370 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3371 | break; |
| 3372 | case BPF_JSLE: |
| 3373 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
| 3374 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3375 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3376 | default: |
| 3377 | break; |
| 3378 | } |
| 3379 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3380 | __reg_deduce_bounds(false_reg); |
| 3381 | __reg_deduce_bounds(true_reg); |
| 3382 | /* We might have learned some bits from the bounds. */ |
| 3383 | __reg_bound_offset(false_reg); |
| 3384 | __reg_bound_offset(true_reg); |
| 3385 | /* Intersecting with the old var_off might have improved our bounds |
| 3386 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3387 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3388 | */ |
| 3389 | __update_reg_bounds(false_reg); |
| 3390 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 3394 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 3395 | struct bpf_reg_state *dst_reg) |
| 3396 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3397 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 3398 | dst_reg->umin_value); |
| 3399 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 3400 | dst_reg->umax_value); |
| 3401 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 3402 | dst_reg->smin_value); |
| 3403 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 3404 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3405 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 3406 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3407 | /* We might have learned new bounds from the var_off. */ |
| 3408 | __update_reg_bounds(src_reg); |
| 3409 | __update_reg_bounds(dst_reg); |
| 3410 | /* We might have learned something about the sign bit. */ |
| 3411 | __reg_deduce_bounds(src_reg); |
| 3412 | __reg_deduce_bounds(dst_reg); |
| 3413 | /* We might have learned some bits from the bounds. */ |
| 3414 | __reg_bound_offset(src_reg); |
| 3415 | __reg_bound_offset(dst_reg); |
| 3416 | /* Intersecting with the old var_off might have improved our bounds |
| 3417 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3418 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3419 | */ |
| 3420 | __update_reg_bounds(src_reg); |
| 3421 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 3425 | struct bpf_reg_state *true_dst, |
| 3426 | struct bpf_reg_state *false_src, |
| 3427 | struct bpf_reg_state *false_dst, |
| 3428 | u8 opcode) |
| 3429 | { |
| 3430 | switch (opcode) { |
| 3431 | case BPF_JEQ: |
| 3432 | __reg_combine_min_max(true_src, true_dst); |
| 3433 | break; |
| 3434 | case BPF_JNE: |
| 3435 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3436 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3437 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3438 | } |
| 3439 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3440 | 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] | 3441 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3442 | { |
| 3443 | struct bpf_reg_state *reg = ®s[regno]; |
| 3444 | |
| 3445 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3446 | /* Old offset (both fixed and variable parts) should |
| 3447 | * have been known-zero, because we don't allow pointer |
| 3448 | * arithmetic on pointers that might be NULL. |
| 3449 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3450 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 3451 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3452 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3453 | __mark_reg_known_zero(reg); |
| 3454 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3455 | } |
| 3456 | if (is_null) { |
| 3457 | reg->type = SCALAR_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3458 | } else if (reg->map_ptr->inner_map_meta) { |
| 3459 | reg->type = CONST_PTR_TO_MAP; |
| 3460 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 3461 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3462 | reg->type = PTR_TO_MAP_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3463 | } |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 3464 | /* We don't need id from this point onwards anymore, thus we |
| 3465 | * should better reset it, so that state pruning has chances |
| 3466 | * to take effect. |
| 3467 | */ |
| 3468 | reg->id = 0; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 3473 | * be folded together at some point. |
| 3474 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3475 | static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3476 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3477 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3478 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3479 | struct bpf_reg_state *regs = state->regs; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 3480 | u32 id = regs[regno].id; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3481 | int i, j; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3482 | |
| 3483 | for (i = 0; i < MAX_BPF_REG; i++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3484 | mark_map_reg(regs, i, id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3485 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3486 | for (j = 0; j <= vstate->curframe; j++) { |
| 3487 | state = vstate->frame[j]; |
| 3488 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 3489 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 3490 | continue; |
| 3491 | mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null); |
| 3492 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3493 | } |
| 3494 | } |
| 3495 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 3496 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 3497 | struct bpf_reg_state *dst_reg, |
| 3498 | struct bpf_reg_state *src_reg, |
| 3499 | struct bpf_verifier_state *this_branch, |
| 3500 | struct bpf_verifier_state *other_branch) |
| 3501 | { |
| 3502 | if (BPF_SRC(insn->code) != BPF_X) |
| 3503 | return false; |
| 3504 | |
| 3505 | switch (BPF_OP(insn->code)) { |
| 3506 | case BPF_JGT: |
| 3507 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3508 | src_reg->type == PTR_TO_PACKET_END) || |
| 3509 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3510 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3511 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 3512 | find_good_pkt_pointers(this_branch, dst_reg, |
| 3513 | dst_reg->type, false); |
| 3514 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3515 | src_reg->type == PTR_TO_PACKET) || |
| 3516 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3517 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3518 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 3519 | find_good_pkt_pointers(other_branch, src_reg, |
| 3520 | src_reg->type, true); |
| 3521 | } else { |
| 3522 | return false; |
| 3523 | } |
| 3524 | break; |
| 3525 | case BPF_JLT: |
| 3526 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3527 | src_reg->type == PTR_TO_PACKET_END) || |
| 3528 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3529 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3530 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 3531 | find_good_pkt_pointers(other_branch, dst_reg, |
| 3532 | dst_reg->type, true); |
| 3533 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3534 | src_reg->type == PTR_TO_PACKET) || |
| 3535 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3536 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3537 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 3538 | find_good_pkt_pointers(this_branch, src_reg, |
| 3539 | src_reg->type, false); |
| 3540 | } else { |
| 3541 | return false; |
| 3542 | } |
| 3543 | break; |
| 3544 | case BPF_JGE: |
| 3545 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3546 | src_reg->type == PTR_TO_PACKET_END) || |
| 3547 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3548 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3549 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 3550 | find_good_pkt_pointers(this_branch, dst_reg, |
| 3551 | dst_reg->type, true); |
| 3552 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3553 | src_reg->type == PTR_TO_PACKET) || |
| 3554 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3555 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3556 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 3557 | find_good_pkt_pointers(other_branch, src_reg, |
| 3558 | src_reg->type, false); |
| 3559 | } else { |
| 3560 | return false; |
| 3561 | } |
| 3562 | break; |
| 3563 | case BPF_JLE: |
| 3564 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3565 | src_reg->type == PTR_TO_PACKET_END) || |
| 3566 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3567 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3568 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 3569 | find_good_pkt_pointers(other_branch, dst_reg, |
| 3570 | dst_reg->type, false); |
| 3571 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3572 | src_reg->type == PTR_TO_PACKET) || |
| 3573 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3574 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3575 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 3576 | find_good_pkt_pointers(this_branch, src_reg, |
| 3577 | src_reg->type, true); |
| 3578 | } else { |
| 3579 | return false; |
| 3580 | } |
| 3581 | break; |
| 3582 | default: |
| 3583 | return false; |
| 3584 | } |
| 3585 | |
| 3586 | return true; |
| 3587 | } |
| 3588 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3589 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3590 | struct bpf_insn *insn, int *insn_idx) |
| 3591 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3592 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 3593 | struct bpf_verifier_state *other_branch; |
| 3594 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
| 3595 | struct bpf_reg_state *dst_reg, *other_branch_regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3596 | u8 opcode = BPF_OP(insn->code); |
| 3597 | int err; |
| 3598 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3599 | if (opcode > BPF_JSLE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3600 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3601 | return -EINVAL; |
| 3602 | } |
| 3603 | |
| 3604 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3605 | if (insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3606 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3607 | return -EINVAL; |
| 3608 | } |
| 3609 | |
| 3610 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3611 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3612 | if (err) |
| 3613 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3614 | |
| 3615 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3616 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3617 | insn->src_reg); |
| 3618 | return -EACCES; |
| 3619 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3620 | } else { |
| 3621 | if (insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3622 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3623 | return -EINVAL; |
| 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3628 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3629 | if (err) |
| 3630 | return err; |
| 3631 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3632 | dst_reg = ®s[insn->dst_reg]; |
| 3633 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3634 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 3635 | if (BPF_SRC(insn->code) == BPF_K && |
| 3636 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3637 | dst_reg->type == SCALAR_VALUE && |
Alexei Starovoitov | 3bf1592 | 2017-11-30 21:31:39 -0800 | [diff] [blame] | 3638 | tnum_is_const(dst_reg->var_off)) { |
| 3639 | if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) || |
| 3640 | (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3641 | /* if (imm == imm) goto pc+off; |
| 3642 | * only follow the goto, ignore fall-through |
| 3643 | */ |
| 3644 | *insn_idx += insn->off; |
| 3645 | return 0; |
| 3646 | } else { |
| 3647 | /* if (imm != imm) goto pc+off; |
| 3648 | * only follow fall-through branch, since |
| 3649 | * that's where the program will go |
| 3650 | */ |
| 3651 | return 0; |
| 3652 | } |
| 3653 | } |
| 3654 | |
| 3655 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 3656 | if (!other_branch) |
| 3657 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3658 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3659 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3660 | /* detect if we are comparing against a constant value so we can adjust |
| 3661 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3662 | * this is only legit if both are scalars (or pointers to the same |
| 3663 | * object, I suppose, but we don't support that right now), because |
| 3664 | * otherwise the different base pointers mean the offsets aren't |
| 3665 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3666 | */ |
| 3667 | if (BPF_SRC(insn->code) == BPF_X) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3668 | if (dst_reg->type == SCALAR_VALUE && |
| 3669 | regs[insn->src_reg].type == SCALAR_VALUE) { |
| 3670 | if (tnum_is_const(regs[insn->src_reg].var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3671 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3672 | dst_reg, regs[insn->src_reg].var_off.value, |
| 3673 | opcode); |
| 3674 | else if (tnum_is_const(dst_reg->var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3675 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3676 | ®s[insn->src_reg], |
| 3677 | dst_reg->var_off.value, opcode); |
| 3678 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) |
| 3679 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3680 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 3681 | &other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3682 | ®s[insn->src_reg], |
| 3683 | ®s[insn->dst_reg], opcode); |
| 3684 | } |
| 3685 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3686 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3687 | dst_reg, insn->imm, opcode); |
| 3688 | } |
| 3689 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3690 | /* 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] | 3691 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3692 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 3693 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3694 | /* Mark all identical map registers in each branch as either |
| 3695 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 3696 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3697 | mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE); |
| 3698 | mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 3699 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 3700 | this_branch, other_branch) && |
| 3701 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3702 | verbose(env, "R%d pointer comparison prohibited\n", |
| 3703 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3704 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3705 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3706 | if (env->log.level) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3707 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3708 | return 0; |
| 3709 | } |
| 3710 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3711 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 3712 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 3713 | { |
| 3714 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 3715 | |
| 3716 | return (struct bpf_map *) (unsigned long) imm64; |
| 3717 | } |
| 3718 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3719 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3720 | 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] | 3721 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3722 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3723 | int err; |
| 3724 | |
| 3725 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3726 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3727 | return -EINVAL; |
| 3728 | } |
| 3729 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3730 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3731 | return -EINVAL; |
| 3732 | } |
| 3733 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3734 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3735 | if (err) |
| 3736 | return err; |
| 3737 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3738 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3739 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 3740 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3741 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3742 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3743 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3744 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3745 | |
| 3746 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 3747 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 3748 | |
| 3749 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 3750 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 3751 | return 0; |
| 3752 | } |
| 3753 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3754 | static bool may_access_skb(enum bpf_prog_type type) |
| 3755 | { |
| 3756 | switch (type) { |
| 3757 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 3758 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 3759 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3760 | return true; |
| 3761 | default: |
| 3762 | return false; |
| 3763 | } |
| 3764 | } |
| 3765 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3766 | /* verify safety of LD_ABS|LD_IND instructions: |
| 3767 | * - they can only appear in the programs where ctx == skb |
| 3768 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 3769 | * preserve R6-R9, and store return value into R0 |
| 3770 | * |
| 3771 | * Implicit input: |
| 3772 | * ctx == skb == R6 == CTX |
| 3773 | * |
| 3774 | * Explicit input: |
| 3775 | * SRC == any register |
| 3776 | * IMM == 32-bit immediate |
| 3777 | * |
| 3778 | * Output: |
| 3779 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 3780 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3781 | 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] | 3782 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3783 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3784 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3785 | int i, err; |
| 3786 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 3787 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3788 | 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] | 3789 | return -EINVAL; |
| 3790 | } |
| 3791 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3792 | if (env->subprog_cnt) { |
| 3793 | /* when program has LD_ABS insn JITs and interpreter assume |
| 3794 | * that r1 == ctx == skb which is not the case for callees |
| 3795 | * that can have arbitrary arguments. It's problematic |
| 3796 | * for main prog as well since JITs would need to analyze |
| 3797 | * all functions in order to make proper register save/restore |
| 3798 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 3799 | */ |
| 3800 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 3801 | return -EINVAL; |
| 3802 | } |
| 3803 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3804 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 3805 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3806 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3807 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3808 | return -EINVAL; |
| 3809 | } |
| 3810 | |
| 3811 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3812 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3813 | if (err) |
| 3814 | return err; |
| 3815 | |
| 3816 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3817 | verbose(env, |
| 3818 | "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] | 3819 | return -EINVAL; |
| 3820 | } |
| 3821 | |
| 3822 | if (mode == BPF_IND) { |
| 3823 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3824 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3825 | if (err) |
| 3826 | return err; |
| 3827 | } |
| 3828 | |
| 3829 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3830 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3831 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3832 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3833 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3834 | |
| 3835 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3836 | * the value fetched from the packet. |
| 3837 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3838 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3839 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3840 | return 0; |
| 3841 | } |
| 3842 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3843 | static int check_return_code(struct bpf_verifier_env *env) |
| 3844 | { |
| 3845 | struct bpf_reg_state *reg; |
| 3846 | struct tnum range = tnum_range(0, 1); |
| 3847 | |
| 3848 | switch (env->prog->type) { |
| 3849 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 3850 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 3851 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 3852 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3853 | break; |
| 3854 | default: |
| 3855 | return 0; |
| 3856 | } |
| 3857 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3858 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3859 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3860 | 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] | 3861 | reg_type_str[reg->type]); |
| 3862 | return -EINVAL; |
| 3863 | } |
| 3864 | |
| 3865 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3866 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3867 | if (!tnum_is_unknown(reg->var_off)) { |
| 3868 | char tn_buf[48]; |
| 3869 | |
| 3870 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3871 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3872 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3873 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3874 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3875 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3876 | return -EINVAL; |
| 3877 | } |
| 3878 | return 0; |
| 3879 | } |
| 3880 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3881 | /* non-recursive DFS pseudo code |
| 3882 | * 1 procedure DFS-iterative(G,v): |
| 3883 | * 2 label v as discovered |
| 3884 | * 3 let S be a stack |
| 3885 | * 4 S.push(v) |
| 3886 | * 5 while S is not empty |
| 3887 | * 6 t <- S.pop() |
| 3888 | * 7 if t is what we're looking for: |
| 3889 | * 8 return t |
| 3890 | * 9 for all edges e in G.adjacentEdges(t) do |
| 3891 | * 10 if edge e is already labelled |
| 3892 | * 11 continue with the next edge |
| 3893 | * 12 w <- G.adjacentVertex(t,e) |
| 3894 | * 13 if vertex w is not discovered and not explored |
| 3895 | * 14 label e as tree-edge |
| 3896 | * 15 label w as discovered |
| 3897 | * 16 S.push(w) |
| 3898 | * 17 continue at 5 |
| 3899 | * 18 else if vertex w is discovered |
| 3900 | * 19 label e as back-edge |
| 3901 | * 20 else |
| 3902 | * 21 // vertex w is explored |
| 3903 | * 22 label e as forward- or cross-edge |
| 3904 | * 23 label t as explored |
| 3905 | * 24 S.pop() |
| 3906 | * |
| 3907 | * convention: |
| 3908 | * 0x10 - discovered |
| 3909 | * 0x11 - discovered and fall-through edge labelled |
| 3910 | * 0x12 - discovered and fall-through and branch edges labelled |
| 3911 | * 0x20 - explored |
| 3912 | */ |
| 3913 | |
| 3914 | enum { |
| 3915 | DISCOVERED = 0x10, |
| 3916 | EXPLORED = 0x20, |
| 3917 | FALLTHROUGH = 1, |
| 3918 | BRANCH = 2, |
| 3919 | }; |
| 3920 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3921 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3922 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3923 | static int *insn_stack; /* stack of insns to process */ |
| 3924 | static int cur_stack; /* current stack index */ |
| 3925 | static int *insn_state; |
| 3926 | |
| 3927 | /* t, w, e - match pseudo-code above: |
| 3928 | * t - index of current instruction |
| 3929 | * w - next instruction |
| 3930 | * e - edge |
| 3931 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3932 | 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] | 3933 | { |
| 3934 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 3935 | return 0; |
| 3936 | |
| 3937 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 3938 | return 0; |
| 3939 | |
| 3940 | if (w < 0 || w >= env->prog->len) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3941 | 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] | 3942 | return -EINVAL; |
| 3943 | } |
| 3944 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3945 | if (e == BRANCH) |
| 3946 | /* mark branch target for state pruning */ |
| 3947 | env->explored_states[w] = STATE_LIST_MARK; |
| 3948 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3949 | if (insn_state[w] == 0) { |
| 3950 | /* tree-edge */ |
| 3951 | insn_state[t] = DISCOVERED | e; |
| 3952 | insn_state[w] = DISCOVERED; |
| 3953 | if (cur_stack >= env->prog->len) |
| 3954 | return -E2BIG; |
| 3955 | insn_stack[cur_stack++] = w; |
| 3956 | return 1; |
| 3957 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3958 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3959 | return -EINVAL; |
| 3960 | } else if (insn_state[w] == EXPLORED) { |
| 3961 | /* forward- or cross-edge */ |
| 3962 | insn_state[t] = DISCOVERED | e; |
| 3963 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3964 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3965 | return -EFAULT; |
| 3966 | } |
| 3967 | return 0; |
| 3968 | } |
| 3969 | |
| 3970 | /* non-recursive depth-first-search to detect loops in BPF program |
| 3971 | * loop == back-edge in directed graph |
| 3972 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3973 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3974 | { |
| 3975 | struct bpf_insn *insns = env->prog->insnsi; |
| 3976 | int insn_cnt = env->prog->len; |
| 3977 | int ret = 0; |
| 3978 | int i, t; |
| 3979 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 3980 | ret = check_subprogs(env); |
| 3981 | if (ret < 0) |
| 3982 | return ret; |
| 3983 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3984 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 3985 | if (!insn_state) |
| 3986 | return -ENOMEM; |
| 3987 | |
| 3988 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 3989 | if (!insn_stack) { |
| 3990 | kfree(insn_state); |
| 3991 | return -ENOMEM; |
| 3992 | } |
| 3993 | |
| 3994 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 3995 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 3996 | cur_stack = 1; |
| 3997 | |
| 3998 | peek_stack: |
| 3999 | if (cur_stack == 0) |
| 4000 | goto check_state; |
| 4001 | t = insn_stack[cur_stack - 1]; |
| 4002 | |
| 4003 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 4004 | u8 opcode = BPF_OP(insns[t].code); |
| 4005 | |
| 4006 | if (opcode == BPF_EXIT) { |
| 4007 | goto mark_explored; |
| 4008 | } else if (opcode == BPF_CALL) { |
| 4009 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4010 | if (ret == 1) |
| 4011 | goto peek_stack; |
| 4012 | else if (ret < 0) |
| 4013 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4014 | if (t + 1 < insn_cnt) |
| 4015 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4016 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
| 4017 | env->explored_states[t] = STATE_LIST_MARK; |
| 4018 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 4019 | if (ret == 1) |
| 4020 | goto peek_stack; |
| 4021 | else if (ret < 0) |
| 4022 | goto err_free; |
| 4023 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4024 | } else if (opcode == BPF_JA) { |
| 4025 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 4026 | ret = -EINVAL; |
| 4027 | goto err_free; |
| 4028 | } |
| 4029 | /* unconditional jump with single edge */ |
| 4030 | ret = push_insn(t, t + insns[t].off + 1, |
| 4031 | FALLTHROUGH, env); |
| 4032 | if (ret == 1) |
| 4033 | goto peek_stack; |
| 4034 | else if (ret < 0) |
| 4035 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4036 | /* tell verifier to check for equivalent states |
| 4037 | * after every call and jump |
| 4038 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 4039 | if (t + 1 < insn_cnt) |
| 4040 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4041 | } else { |
| 4042 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4043 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4044 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4045 | if (ret == 1) |
| 4046 | goto peek_stack; |
| 4047 | else if (ret < 0) |
| 4048 | goto err_free; |
| 4049 | |
| 4050 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 4051 | if (ret == 1) |
| 4052 | goto peek_stack; |
| 4053 | else if (ret < 0) |
| 4054 | goto err_free; |
| 4055 | } |
| 4056 | } else { |
| 4057 | /* all other non-branch instructions with single |
| 4058 | * fall-through edge |
| 4059 | */ |
| 4060 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 4061 | if (ret == 1) |
| 4062 | goto peek_stack; |
| 4063 | else if (ret < 0) |
| 4064 | goto err_free; |
| 4065 | } |
| 4066 | |
| 4067 | mark_explored: |
| 4068 | insn_state[t] = EXPLORED; |
| 4069 | if (cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4070 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4071 | ret = -EFAULT; |
| 4072 | goto err_free; |
| 4073 | } |
| 4074 | goto peek_stack; |
| 4075 | |
| 4076 | check_state: |
| 4077 | for (i = 0; i < insn_cnt; i++) { |
| 4078 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4079 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4080 | ret = -EINVAL; |
| 4081 | goto err_free; |
| 4082 | } |
| 4083 | } |
| 4084 | ret = 0; /* cfg looks good */ |
| 4085 | |
| 4086 | err_free: |
| 4087 | kfree(insn_state); |
| 4088 | kfree(insn_stack); |
| 4089 | return ret; |
| 4090 | } |
| 4091 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4092 | /* check %cur's range satisfies %old's */ |
| 4093 | static bool range_within(struct bpf_reg_state *old, |
| 4094 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4095 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4096 | return old->umin_value <= cur->umin_value && |
| 4097 | old->umax_value >= cur->umax_value && |
| 4098 | old->smin_value <= cur->smin_value && |
| 4099 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | /* Maximum number of register states that can exist at once */ |
| 4103 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 4104 | struct idpair { |
| 4105 | u32 old; |
| 4106 | u32 cur; |
| 4107 | }; |
| 4108 | |
| 4109 | /* If in the old state two registers had the same id, then they need to have |
| 4110 | * the same id in the new state as well. But that id could be different from |
| 4111 | * the old state, so we need to track the mapping from old to new ids. |
| 4112 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 4113 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 4114 | * regs with a different old id could still have new id 9, we don't care about |
| 4115 | * that. |
| 4116 | * So we look through our idmap to see if this old id has been seen before. If |
| 4117 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 4118 | */ |
| 4119 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 4120 | { |
| 4121 | unsigned int i; |
| 4122 | |
| 4123 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 4124 | if (!idmap[i].old) { |
| 4125 | /* Reached an empty slot; haven't seen this id before */ |
| 4126 | idmap[i].old = old_id; |
| 4127 | idmap[i].cur = cur_id; |
| 4128 | return true; |
| 4129 | } |
| 4130 | if (idmap[i].old == old_id) |
| 4131 | return idmap[i].cur == cur_id; |
| 4132 | } |
| 4133 | /* We ran out of idmap slots, which should be impossible */ |
| 4134 | WARN_ON_ONCE(1); |
| 4135 | return false; |
| 4136 | } |
| 4137 | |
| 4138 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4139 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 4140 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4141 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4142 | bool equal; |
| 4143 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4144 | if (!(rold->live & REG_LIVE_READ)) |
| 4145 | /* explored state didn't use this */ |
| 4146 | return true; |
| 4147 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4148 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, frameno)) == 0; |
| 4149 | |
| 4150 | if (rold->type == PTR_TO_STACK) |
| 4151 | /* two stack pointers are equal only if they're pointing to |
| 4152 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 4153 | */ |
| 4154 | return equal && rold->frameno == rcur->frameno; |
| 4155 | |
| 4156 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4157 | return true; |
| 4158 | |
| 4159 | if (rold->type == NOT_INIT) |
| 4160 | /* explored state can't have used this */ |
| 4161 | return true; |
| 4162 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4163 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4164 | switch (rold->type) { |
| 4165 | case SCALAR_VALUE: |
| 4166 | if (rcur->type == SCALAR_VALUE) { |
| 4167 | /* new val must satisfy old val knowledge */ |
| 4168 | return range_within(rold, rcur) && |
| 4169 | tnum_in(rold->var_off, rcur->var_off); |
| 4170 | } else { |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4171 | /* We're trying to use a pointer in place of a scalar. |
| 4172 | * Even if the scalar was unbounded, this could lead to |
| 4173 | * pointer leaks because scalars are allowed to leak |
| 4174 | * while pointers are not. We could make this safe in |
| 4175 | * special cases if root is calling us, but it's |
| 4176 | * probably not worth the hassle. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4177 | */ |
Jann Horn | 179d1c5 | 2017-12-18 20:11:59 -0800 | [diff] [blame] | 4178 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4179 | } |
| 4180 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4181 | /* If the new min/max/var_off satisfy the old ones and |
| 4182 | * everything else matches, we are OK. |
| 4183 | * We don't care about the 'id' value, because nothing |
| 4184 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) |
| 4185 | */ |
| 4186 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 4187 | range_within(rold, rcur) && |
| 4188 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4189 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 4190 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 4191 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 4192 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 4193 | * checked, doing so could have affected others with the same |
| 4194 | * id, and we can't check for that because we lost the id when |
| 4195 | * we converted to a PTR_TO_MAP_VALUE. |
| 4196 | */ |
| 4197 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 4198 | return false; |
| 4199 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 4200 | return false; |
| 4201 | /* Check our ids match any regs they're supposed to */ |
| 4202 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4203 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4204 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4205 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4206 | return false; |
| 4207 | /* We must have at least as much range as the old ptr |
| 4208 | * did, so that any accesses which were safe before are |
| 4209 | * still safe. This is true even if old range < old off, |
| 4210 | * since someone could have accessed through (ptr - k), or |
| 4211 | * even done ptr -= k in a register, to get a safe access. |
| 4212 | */ |
| 4213 | if (rold->range > rcur->range) |
| 4214 | return false; |
| 4215 | /* If the offsets don't match, we can't trust our alignment; |
| 4216 | * nor can we be sure that we won't fall out of range. |
| 4217 | */ |
| 4218 | if (rold->off != rcur->off) |
| 4219 | return false; |
| 4220 | /* id relations must be preserved */ |
| 4221 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 4222 | return false; |
| 4223 | /* new val must satisfy old val knowledge */ |
| 4224 | return range_within(rold, rcur) && |
| 4225 | tnum_in(rold->var_off, rcur->var_off); |
| 4226 | case PTR_TO_CTX: |
| 4227 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4228 | case PTR_TO_PACKET_END: |
| 4229 | /* Only valid matches are exact, which memcmp() above |
| 4230 | * would have accepted |
| 4231 | */ |
| 4232 | default: |
| 4233 | /* Don't know what's going on, just say it's not safe */ |
| 4234 | return false; |
| 4235 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4236 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4237 | /* Shouldn't get here; if we do, say it's not safe */ |
| 4238 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4239 | return false; |
| 4240 | } |
| 4241 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4242 | static bool stacksafe(struct bpf_func_state *old, |
| 4243 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4244 | struct idpair *idmap) |
| 4245 | { |
| 4246 | int i, spi; |
| 4247 | |
| 4248 | /* if explored stack has more populated slots than current stack |
| 4249 | * such stacks are not equivalent |
| 4250 | */ |
| 4251 | if (old->allocated_stack > cur->allocated_stack) |
| 4252 | return false; |
| 4253 | |
| 4254 | /* walk slots of the explored stack and ignore any additional |
| 4255 | * slots in the current stack, since explored(safe) state |
| 4256 | * didn't use them |
| 4257 | */ |
| 4258 | for (i = 0; i < old->allocated_stack; i++) { |
| 4259 | spi = i / BPF_REG_SIZE; |
| 4260 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4261 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) |
| 4262 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 4263 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4264 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4265 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 4266 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4267 | /* if old state was safe with misc data in the stack |
| 4268 | * it will be safe with zero-initialized stack. |
| 4269 | * The opposite is not true |
| 4270 | */ |
| 4271 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 4272 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 4273 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4274 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 4275 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 4276 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 4277 | * this stack slot, but current has has STACK_MISC -> |
| 4278 | * this verifier states are not equivalent, |
| 4279 | * return false to continue verification of this path |
| 4280 | */ |
| 4281 | return false; |
| 4282 | if (i % BPF_REG_SIZE) |
| 4283 | continue; |
| 4284 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 4285 | continue; |
| 4286 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 4287 | &cur->stack[spi].spilled_ptr, |
| 4288 | idmap)) |
| 4289 | /* when explored and current stack slot are both storing |
| 4290 | * spilled registers, check that stored pointers types |
| 4291 | * are the same as well. |
| 4292 | * Ex: explored safe path could have stored |
| 4293 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 4294 | * but current path has stored: |
| 4295 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 4296 | * such verifier states are not equivalent. |
| 4297 | * return false to continue verification of this path |
| 4298 | */ |
| 4299 | return false; |
| 4300 | } |
| 4301 | return true; |
| 4302 | } |
| 4303 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4304 | /* compare two verifier states |
| 4305 | * |
| 4306 | * all states stored in state_list are known to be valid, since |
| 4307 | * verifier reached 'bpf_exit' instruction through them |
| 4308 | * |
| 4309 | * this function is called when verifier exploring different branches of |
| 4310 | * execution popped from the state stack. If it sees an old state that has |
| 4311 | * more strict register state and more strict stack state then this execution |
| 4312 | * branch doesn't need to be explored further, since verifier already |
| 4313 | * concluded that more strict state leads to valid finish. |
| 4314 | * |
| 4315 | * Therefore two states are equivalent if register state is more conservative |
| 4316 | * and explored stack state is more conservative than the current one. |
| 4317 | * Example: |
| 4318 | * explored current |
| 4319 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 4320 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 4321 | * |
| 4322 | * In other words if current stack state (one being explored) has more |
| 4323 | * valid slots than old one that already passed validation, it means |
| 4324 | * the verifier can stop exploring and conclude that current state is valid too |
| 4325 | * |
| 4326 | * Similarly with registers. If explored state has register type as invalid |
| 4327 | * whereas register type in current state is meaningful, it means that |
| 4328 | * the current state will reach 'bpf_exit' instruction safely |
| 4329 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4330 | static bool func_states_equal(struct bpf_func_state *old, |
| 4331 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4332 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4333 | struct idpair *idmap; |
| 4334 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4335 | int i; |
| 4336 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4337 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 4338 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 4339 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4340 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4341 | |
| 4342 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4343 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4344 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4345 | } |
| 4346 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4347 | if (!stacksafe(old, cur, idmap)) |
| 4348 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4349 | ret = true; |
| 4350 | out_free: |
| 4351 | kfree(idmap); |
| 4352 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4353 | } |
| 4354 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4355 | static bool states_equal(struct bpf_verifier_env *env, |
| 4356 | struct bpf_verifier_state *old, |
| 4357 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4358 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4359 | int i; |
| 4360 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4361 | if (old->curframe != cur->curframe) |
| 4362 | return false; |
| 4363 | |
| 4364 | /* for states to be equal callsites have to be the same |
| 4365 | * and all frame states need to be equivalent |
| 4366 | */ |
| 4367 | for (i = 0; i <= old->curframe; i++) { |
| 4368 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 4369 | return false; |
| 4370 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 4371 | return false; |
| 4372 | } |
| 4373 | return true; |
| 4374 | } |
| 4375 | |
| 4376 | /* A write screens off any subsequent reads; but write marks come from the |
| 4377 | * straight-line code between a state and its parent. When we arrive at an |
| 4378 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 4379 | * code, so read marks in the state must propagate to the parent regardless |
| 4380 | * of the state's write marks. That's what 'parent == state->parent' comparison |
| 4381 | * in mark_reg_read() and mark_stack_slot_read() is for. |
| 4382 | */ |
| 4383 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 4384 | const struct bpf_verifier_state *vstate, |
| 4385 | struct bpf_verifier_state *vparent) |
| 4386 | { |
| 4387 | int i, frame, err = 0; |
| 4388 | struct bpf_func_state *state, *parent; |
| 4389 | |
| 4390 | if (vparent->curframe != vstate->curframe) { |
| 4391 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 4392 | vparent->curframe, vstate->curframe); |
| 4393 | return -EFAULT; |
| 4394 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4395 | /* Propagate read liveness of registers... */ |
| 4396 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
| 4397 | /* We don't need to worry about FP liveness because it's read-only */ |
| 4398 | for (i = 0; i < BPF_REG_FP; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4399 | if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4400 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4401 | if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) { |
| 4402 | err = mark_reg_read(env, vstate, vparent, i); |
| 4403 | if (err) |
| 4404 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4405 | } |
| 4406 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4407 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4408 | /* ... and stack slots */ |
| 4409 | for (frame = 0; frame <= vstate->curframe; frame++) { |
| 4410 | state = vstate->frame[frame]; |
| 4411 | parent = vparent->frame[frame]; |
| 4412 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 4413 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4414 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 4415 | continue; |
| 4416 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 4417 | mark_stack_slot_read(env, vstate, vparent, i, frame); |
| 4418 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4419 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4420 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4421 | } |
| 4422 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4423 | 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] | 4424 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4425 | struct bpf_verifier_state_list *new_sl; |
| 4426 | struct bpf_verifier_state_list *sl; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4427 | struct bpf_verifier_state *cur = env->cur_state; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4428 | int i, j, err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4429 | |
| 4430 | sl = env->explored_states[insn_idx]; |
| 4431 | if (!sl) |
| 4432 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 4433 | * be doing state search here |
| 4434 | */ |
| 4435 | return 0; |
| 4436 | |
| 4437 | while (sl != STATE_LIST_MARK) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4438 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4439 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4440 | * prune the search. |
| 4441 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 4442 | * If we have any write marks in env->cur_state, they |
| 4443 | * will prevent corresponding reads in the continuation |
| 4444 | * from reaching our parent (an explored_state). Our |
| 4445 | * own state will get the read marks recorded, but |
| 4446 | * they'll be immediately forgotten as we're pruning |
| 4447 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4448 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4449 | err = propagate_liveness(env, &sl->state, cur); |
| 4450 | if (err) |
| 4451 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4452 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4453 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4454 | sl = sl->next; |
| 4455 | } |
| 4456 | |
| 4457 | /* there were no equivalent states, remember current one. |
| 4458 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4459 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 4460 | * or it will be rejected. Since there are no loops, we won't be |
| 4461 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 4462 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4463 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4464 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4465 | if (!new_sl) |
| 4466 | return -ENOMEM; |
| 4467 | |
| 4468 | /* add new state to the head of linked list */ |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 4469 | err = copy_verifier_state(&new_sl->state, cur); |
| 4470 | if (err) { |
| 4471 | free_verifier_state(&new_sl->state, false); |
| 4472 | kfree(new_sl); |
| 4473 | return err; |
| 4474 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4475 | new_sl->next = env->explored_states[insn_idx]; |
| 4476 | env->explored_states[insn_idx] = new_sl; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4477 | /* connect new state to parentage chain */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4478 | cur->parent = &new_sl->state; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 4479 | /* clear write marks in current state: the writes we did are not writes |
| 4480 | * our child did, so they don't screen off its reads from us. |
| 4481 | * (There are no read marks in current state, because reads always mark |
| 4482 | * their parent and current state never has children yet. Only |
| 4483 | * explored_states can get read marks.) |
| 4484 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4485 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4486 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 4487 | |
| 4488 | /* all stack frames are accessible from callee, clear them all */ |
| 4489 | for (j = 0; j <= cur->curframe; j++) { |
| 4490 | struct bpf_func_state *frame = cur->frame[j]; |
| 4491 | |
| 4492 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4493 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4494 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4495 | return 0; |
| 4496 | } |
| 4497 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4498 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4499 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4500 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4501 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4502 | struct bpf_reg_state *regs; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4503 | int insn_cnt = env->prog->len, i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4504 | int insn_idx, prev_insn_idx = 0; |
| 4505 | int insn_processed = 0; |
| 4506 | bool do_print_state = false; |
| 4507 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4508 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 4509 | if (!state) |
| 4510 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4511 | state->curframe = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4512 | state->parent = NULL; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4513 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 4514 | if (!state->frame[0]) { |
| 4515 | kfree(state); |
| 4516 | return -ENOMEM; |
| 4517 | } |
| 4518 | env->cur_state = state; |
| 4519 | init_func_state(env, state->frame[0], |
| 4520 | BPF_MAIN_FUNC /* callsite */, |
| 4521 | 0 /* frameno */, |
| 4522 | 0 /* subprogno, zero == main subprog */); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4523 | insn_idx = 0; |
| 4524 | for (;;) { |
| 4525 | struct bpf_insn *insn; |
| 4526 | u8 class; |
| 4527 | int err; |
| 4528 | |
| 4529 | if (insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4530 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4531 | insn_idx, insn_cnt); |
| 4532 | return -EFAULT; |
| 4533 | } |
| 4534 | |
| 4535 | insn = &insns[insn_idx]; |
| 4536 | class = BPF_CLASS(insn->code); |
| 4537 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4538 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4539 | verbose(env, |
| 4540 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4541 | insn_processed); |
| 4542 | return -E2BIG; |
| 4543 | } |
| 4544 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4545 | err = is_state_visited(env, insn_idx); |
| 4546 | if (err < 0) |
| 4547 | return err; |
| 4548 | if (err == 1) { |
| 4549 | /* found equivalent state, can prune the search */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4550 | if (env->log.level) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4551 | if (do_print_state) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4552 | verbose(env, "\nfrom %d to %d: safe\n", |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4553 | prev_insn_idx, insn_idx); |
| 4554 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4555 | verbose(env, "%d: safe\n", insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4556 | } |
| 4557 | goto process_bpf_exit; |
| 4558 | } |
| 4559 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4560 | if (need_resched()) |
| 4561 | cond_resched(); |
| 4562 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4563 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
| 4564 | if (env->log.level > 1) |
| 4565 | verbose(env, "%d:", insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 4566 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4567 | verbose(env, "\nfrom %d to %d:", |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 4568 | prev_insn_idx, insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4569 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4570 | do_print_state = false; |
| 4571 | } |
| 4572 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4573 | if (env->log.level) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 4574 | const struct bpf_insn_cbs cbs = { |
| 4575 | .cb_print = verbose, |
| 4576 | }; |
| 4577 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4578 | verbose(env, "%d: ", insn_idx); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 4579 | print_bpf_insn(&cbs, env, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4580 | } |
| 4581 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 4582 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
| 4583 | err = bpf_prog_offload_verify_insn(env, insn_idx, |
| 4584 | prev_insn_idx); |
| 4585 | if (err) |
| 4586 | return err; |
| 4587 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 4588 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4589 | regs = cur_regs(env); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4590 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4591 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4592 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4593 | if (err) |
| 4594 | return err; |
| 4595 | |
| 4596 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4597 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4598 | |
| 4599 | /* check for reserved fields is already done */ |
| 4600 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4601 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4602 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4603 | if (err) |
| 4604 | return err; |
| 4605 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4606 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4607 | if (err) |
| 4608 | return err; |
| 4609 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 4610 | src_reg_type = regs[insn->src_reg].type; |
| 4611 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4612 | /* check that memory (src_reg + off) is readable, |
| 4613 | * the state of dst_reg will be updated by this func |
| 4614 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4615 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4616 | BPF_SIZE(insn->code), BPF_READ, |
| 4617 | insn->dst_reg); |
| 4618 | if (err) |
| 4619 | return err; |
| 4620 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4621 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 4622 | |
| 4623 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4624 | /* saw a valid insn |
| 4625 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4626 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4627 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4628 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4629 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4630 | } else if (src_reg_type != *prev_src_type && |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4631 | (src_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4632 | *prev_src_type == PTR_TO_CTX)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4633 | /* ABuser program is trying to use the same insn |
| 4634 | * dst_reg = *(u32*) (src_reg + off) |
| 4635 | * with different pointer types: |
| 4636 | * src_reg == ctx in one branch and |
| 4637 | * src_reg == stack|map in some other branch. |
| 4638 | * Reject it. |
| 4639 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4640 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4641 | return -EINVAL; |
| 4642 | } |
| 4643 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4644 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4645 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4646 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4647 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4648 | err = check_xadd(env, insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4649 | if (err) |
| 4650 | return err; |
| 4651 | insn_idx++; |
| 4652 | continue; |
| 4653 | } |
| 4654 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4655 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4656 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4657 | if (err) |
| 4658 | return err; |
| 4659 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4660 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4661 | if (err) |
| 4662 | return err; |
| 4663 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4664 | dst_reg_type = regs[insn->dst_reg].type; |
| 4665 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4666 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4667 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4668 | BPF_SIZE(insn->code), BPF_WRITE, |
| 4669 | insn->src_reg); |
| 4670 | if (err) |
| 4671 | return err; |
| 4672 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4673 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 4674 | |
| 4675 | if (*prev_dst_type == NOT_INIT) { |
| 4676 | *prev_dst_type = dst_reg_type; |
| 4677 | } else if (dst_reg_type != *prev_dst_type && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4678 | (dst_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4679 | *prev_dst_type == PTR_TO_CTX)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4680 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4681 | return -EINVAL; |
| 4682 | } |
| 4683 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4684 | } else if (class == BPF_ST) { |
| 4685 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 4686 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4687 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4688 | return -EINVAL; |
| 4689 | } |
| 4690 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4691 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4692 | if (err) |
| 4693 | return err; |
| 4694 | |
| 4695 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4696 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4697 | BPF_SIZE(insn->code), BPF_WRITE, |
| 4698 | -1); |
| 4699 | if (err) |
| 4700 | return err; |
| 4701 | |
| 4702 | } else if (class == BPF_JMP) { |
| 4703 | u8 opcode = BPF_OP(insn->code); |
| 4704 | |
| 4705 | if (opcode == BPF_CALL) { |
| 4706 | if (BPF_SRC(insn->code) != BPF_K || |
| 4707 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4708 | (insn->src_reg != BPF_REG_0 && |
| 4709 | insn->src_reg != BPF_PSEUDO_CALL) || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4710 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4711 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4712 | return -EINVAL; |
| 4713 | } |
| 4714 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4715 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 4716 | err = check_func_call(env, insn, &insn_idx); |
| 4717 | else |
| 4718 | err = check_helper_call(env, insn->imm, insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4719 | if (err) |
| 4720 | return err; |
| 4721 | |
| 4722 | } else if (opcode == BPF_JA) { |
| 4723 | if (BPF_SRC(insn->code) != BPF_K || |
| 4724 | insn->imm != 0 || |
| 4725 | insn->src_reg != BPF_REG_0 || |
| 4726 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4727 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4728 | return -EINVAL; |
| 4729 | } |
| 4730 | |
| 4731 | insn_idx += insn->off + 1; |
| 4732 | continue; |
| 4733 | |
| 4734 | } else if (opcode == BPF_EXIT) { |
| 4735 | if (BPF_SRC(insn->code) != BPF_K || |
| 4736 | insn->imm != 0 || |
| 4737 | insn->src_reg != BPF_REG_0 || |
| 4738 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4739 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4740 | return -EINVAL; |
| 4741 | } |
| 4742 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4743 | if (state->curframe) { |
| 4744 | /* exit from nested function */ |
| 4745 | prev_insn_idx = insn_idx; |
| 4746 | err = prepare_func_exit(env, &insn_idx); |
| 4747 | if (err) |
| 4748 | return err; |
| 4749 | do_print_state = true; |
| 4750 | continue; |
| 4751 | } |
| 4752 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4753 | /* eBPF calling convetion is such that R0 is used |
| 4754 | * to return the value from eBPF program. |
| 4755 | * Make sure that it's readable at this time |
| 4756 | * of bpf_exit, which means that program wrote |
| 4757 | * something into it earlier |
| 4758 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4759 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4760 | if (err) |
| 4761 | return err; |
| 4762 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4763 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4764 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4765 | return -EACCES; |
| 4766 | } |
| 4767 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4768 | err = check_return_code(env); |
| 4769 | if (err) |
| 4770 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4771 | process_bpf_exit: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4772 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
| 4773 | if (err < 0) { |
| 4774 | if (err != -ENOENT) |
| 4775 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4776 | break; |
| 4777 | } else { |
| 4778 | do_print_state = true; |
| 4779 | continue; |
| 4780 | } |
| 4781 | } else { |
| 4782 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 4783 | if (err) |
| 4784 | return err; |
| 4785 | } |
| 4786 | } else if (class == BPF_LD) { |
| 4787 | u8 mode = BPF_MODE(insn->code); |
| 4788 | |
| 4789 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4790 | err = check_ld_abs(env, insn); |
| 4791 | if (err) |
| 4792 | return err; |
| 4793 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4794 | } else if (mode == BPF_IMM) { |
| 4795 | err = check_ld_imm(env, insn); |
| 4796 | if (err) |
| 4797 | return err; |
| 4798 | |
| 4799 | insn_idx++; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4800 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4801 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4802 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4803 | return -EINVAL; |
| 4804 | } |
| 4805 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4806 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4807 | return -EINVAL; |
| 4808 | } |
| 4809 | |
| 4810 | insn_idx++; |
| 4811 | } |
| 4812 | |
Daniel Borkmann | 4bd95f4 | 2018-01-20 01:24:36 +0100 | [diff] [blame^] | 4813 | verbose(env, "processed %d insns (limit %d), stack depth ", |
| 4814 | insn_processed, BPF_COMPLEXITY_LIMIT_INSNS); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4815 | for (i = 0; i < env->subprog_cnt + 1; i++) { |
| 4816 | u32 depth = env->subprog_stack_depth[i]; |
| 4817 | |
| 4818 | verbose(env, "%d", depth); |
| 4819 | if (i + 1 < env->subprog_cnt + 1) |
| 4820 | verbose(env, "+"); |
| 4821 | } |
| 4822 | verbose(env, "\n"); |
| 4823 | env->prog->aux->stack_depth = env->subprog_stack_depth[0]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4824 | return 0; |
| 4825 | } |
| 4826 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4827 | static int check_map_prealloc(struct bpf_map *map) |
| 4828 | { |
| 4829 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 4830 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 4831 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4832 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 4833 | } |
| 4834 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4835 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 4836 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4837 | struct bpf_prog *prog) |
| 4838 | |
| 4839 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4840 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 4841 | * preallocated hash maps, since doing memory allocation |
| 4842 | * in overflow_handler can crash depending on where nmi got |
| 4843 | * triggered. |
| 4844 | */ |
| 4845 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 4846 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4847 | 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] | 4848 | return -EINVAL; |
| 4849 | } |
| 4850 | if (map->inner_map_meta && |
| 4851 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4852 | 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] | 4853 | return -EINVAL; |
| 4854 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4855 | } |
Jakub Kicinski | a388457 | 2018-01-11 20:29:09 -0800 | [diff] [blame] | 4856 | |
| 4857 | if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) && |
| 4858 | !bpf_offload_dev_match(prog, map)) { |
| 4859 | verbose(env, "offload device mismatch between prog and map\n"); |
| 4860 | return -EINVAL; |
| 4861 | } |
| 4862 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4863 | return 0; |
| 4864 | } |
| 4865 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4866 | /* look for pseudo eBPF instructions that access map FDs and |
| 4867 | * replace them with actual map pointers |
| 4868 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4869 | 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] | 4870 | { |
| 4871 | struct bpf_insn *insn = env->prog->insnsi; |
| 4872 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4873 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4874 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 4875 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 4876 | if (err) |
| 4877 | return err; |
| 4878 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4879 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4880 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4881 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4882 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4883 | return -EINVAL; |
| 4884 | } |
| 4885 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4886 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 4887 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 4888 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4889 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4890 | return -EINVAL; |
| 4891 | } |
| 4892 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4893 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 4894 | struct bpf_map *map; |
| 4895 | struct fd f; |
| 4896 | |
| 4897 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 4898 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 4899 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4900 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4901 | return -EINVAL; |
| 4902 | } |
| 4903 | |
| 4904 | if (insn->src_reg == 0) |
| 4905 | /* valid generic load 64-bit imm */ |
| 4906 | goto next_insn; |
| 4907 | |
| 4908 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4909 | verbose(env, |
| 4910 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4911 | return -EINVAL; |
| 4912 | } |
| 4913 | |
| 4914 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 4915 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4916 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4917 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4918 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4919 | return PTR_ERR(map); |
| 4920 | } |
| 4921 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4922 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4923 | if (err) { |
| 4924 | fdput(f); |
| 4925 | return err; |
| 4926 | } |
| 4927 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4928 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 4929 | insn[0].imm = (u32) (unsigned long) map; |
| 4930 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 4931 | |
| 4932 | /* check whether we recorded this map already */ |
| 4933 | for (j = 0; j < env->used_map_cnt; j++) |
| 4934 | if (env->used_maps[j] == map) { |
| 4935 | fdput(f); |
| 4936 | goto next_insn; |
| 4937 | } |
| 4938 | |
| 4939 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 4940 | fdput(f); |
| 4941 | return -E2BIG; |
| 4942 | } |
| 4943 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4944 | /* hold the map. If the program is rejected by verifier, |
| 4945 | * the map will be released by release_maps() or it |
| 4946 | * will be used by the valid program until it's unloaded |
| 4947 | * and all maps are released in free_bpf_prog_info() |
| 4948 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 4949 | map = bpf_map_inc(map, false); |
| 4950 | if (IS_ERR(map)) { |
| 4951 | fdput(f); |
| 4952 | return PTR_ERR(map); |
| 4953 | } |
| 4954 | env->used_maps[env->used_map_cnt++] = map; |
| 4955 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4956 | fdput(f); |
| 4957 | next_insn: |
| 4958 | insn++; |
| 4959 | i++; |
| 4960 | } |
| 4961 | } |
| 4962 | |
| 4963 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 4964 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 4965 | * These pointers will be used later by verifier to validate map access. |
| 4966 | */ |
| 4967 | return 0; |
| 4968 | } |
| 4969 | |
| 4970 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4971 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4972 | { |
| 4973 | int i; |
| 4974 | |
| 4975 | for (i = 0; i < env->used_map_cnt; i++) |
| 4976 | bpf_map_put(env->used_maps[i]); |
| 4977 | } |
| 4978 | |
| 4979 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4980 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4981 | { |
| 4982 | struct bpf_insn *insn = env->prog->insnsi; |
| 4983 | int insn_cnt = env->prog->len; |
| 4984 | int i; |
| 4985 | |
| 4986 | for (i = 0; i < insn_cnt; i++, insn++) |
| 4987 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 4988 | insn->src_reg = 0; |
| 4989 | } |
| 4990 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4991 | /* single env->prog->insni[off] instruction was replaced with the range |
| 4992 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 4993 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 4994 | */ |
| 4995 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 4996 | u32 off, u32 cnt) |
| 4997 | { |
| 4998 | 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] | 4999 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5000 | |
| 5001 | if (cnt == 1) |
| 5002 | return 0; |
| 5003 | new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len); |
| 5004 | if (!new_data) |
| 5005 | return -ENOMEM; |
| 5006 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 5007 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 5008 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5009 | for (i = off; i < off + cnt - 1; i++) |
| 5010 | new_data[i].seen = true; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5011 | env->insn_aux_data = new_data; |
| 5012 | vfree(old_data); |
| 5013 | return 0; |
| 5014 | } |
| 5015 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5016 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 5017 | { |
| 5018 | int i; |
| 5019 | |
| 5020 | if (len == 1) |
| 5021 | return; |
| 5022 | for (i = 0; i < env->subprog_cnt; i++) { |
| 5023 | if (env->subprog_starts[i] < off) |
| 5024 | continue; |
| 5025 | env->subprog_starts[i] += len - 1; |
| 5026 | } |
| 5027 | } |
| 5028 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5029 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 5030 | const struct bpf_insn *patch, u32 len) |
| 5031 | { |
| 5032 | struct bpf_prog *new_prog; |
| 5033 | |
| 5034 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 5035 | if (!new_prog) |
| 5036 | return NULL; |
| 5037 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 5038 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5039 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5040 | return new_prog; |
| 5041 | } |
| 5042 | |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5043 | /* The verifier does more data flow analysis than llvm and will not explore |
| 5044 | * branches that are dead at run time. Malicious programs can have dead code |
| 5045 | * too. Therefore replace all dead at-run-time code with nops. |
| 5046 | */ |
| 5047 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 5048 | { |
| 5049 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 5050 | struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0); |
| 5051 | struct bpf_insn *insn = env->prog->insnsi; |
| 5052 | const int insn_cnt = env->prog->len; |
| 5053 | int i; |
| 5054 | |
| 5055 | for (i = 0; i < insn_cnt; i++) { |
| 5056 | if (aux_data[i].seen) |
| 5057 | continue; |
| 5058 | memcpy(insn + i, &nop, sizeof(nop)); |
| 5059 | } |
| 5060 | } |
| 5061 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5062 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 5063 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 5064 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5065 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5066 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5067 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5068 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5069 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5070 | struct bpf_insn insn_buf[16], *insn; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5071 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5072 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5073 | bool is_narrower_load; |
| 5074 | u32 target_size; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5075 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5076 | if (ops->gen_prologue) { |
| 5077 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 5078 | env->prog); |
| 5079 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5080 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5081 | return -EINVAL; |
| 5082 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5083 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5084 | if (!new_prog) |
| 5085 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5086 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5087 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5088 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | if (!ops->convert_ctx_access) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5093 | return 0; |
| 5094 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5095 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 5096 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5097 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 5098 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 5099 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 5100 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 5101 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5102 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 5103 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 5104 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 5105 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 5106 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 5107 | type = BPF_WRITE; |
| 5108 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5109 | continue; |
| 5110 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5111 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5112 | continue; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5113 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5114 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5115 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5116 | |
| 5117 | /* If the read access is a narrower load of the field, |
| 5118 | * convert to a 4/8-byte load, to minimum program type specific |
| 5119 | * convert_ctx_access changes. If conversion is successful, |
| 5120 | * we will apply proper mask to the result. |
| 5121 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5122 | is_narrower_load = size < ctx_field_size; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5123 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5124 | u32 off = insn->off; |
| 5125 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5126 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5127 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5128 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5129 | return -EINVAL; |
| 5130 | } |
| 5131 | |
| 5132 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5133 | if (ctx_field_size == 4) |
| 5134 | size_code = BPF_W; |
| 5135 | else if (ctx_field_size == 8) |
| 5136 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5137 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5138 | insn->off = off & ~(ctx_field_size - 1); |
| 5139 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 5140 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5141 | |
| 5142 | target_size = 0; |
| 5143 | cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog, |
| 5144 | &target_size); |
| 5145 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 5146 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5147 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5148 | return -EINVAL; |
| 5149 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5150 | |
| 5151 | if (is_narrower_load && size < target_size) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5152 | if (ctx_field_size <= 4) |
| 5153 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5154 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5155 | else |
| 5156 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5157 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5158 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5159 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5160 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5161 | if (!new_prog) |
| 5162 | return -ENOMEM; |
| 5163 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5164 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5165 | |
| 5166 | /* keep walking new program and skip insns we just inserted */ |
| 5167 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5168 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5169 | } |
| 5170 | |
| 5171 | return 0; |
| 5172 | } |
| 5173 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5174 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 5175 | { |
| 5176 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 5177 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5178 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5179 | void *old_bpf_func; |
| 5180 | int err = -ENOMEM; |
| 5181 | |
| 5182 | if (env->subprog_cnt == 0) |
| 5183 | return 0; |
| 5184 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5185 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5186 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5187 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5188 | continue; |
| 5189 | subprog = find_subprog(env, i + insn->imm + 1); |
| 5190 | if (subprog < 0) { |
| 5191 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 5192 | i + insn->imm + 1); |
| 5193 | return -EFAULT; |
| 5194 | } |
| 5195 | /* temporarily remember subprog id inside insn instead of |
| 5196 | * aux_data, since next loop will split up all insns into funcs |
| 5197 | */ |
| 5198 | insn->off = subprog + 1; |
| 5199 | /* remember original imm in case JIT fails and fallback |
| 5200 | * to interpreter will be needed |
| 5201 | */ |
| 5202 | env->insn_aux_data[i].call_imm = insn->imm; |
| 5203 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 5204 | insn->imm = 1; |
| 5205 | } |
| 5206 | |
| 5207 | func = kzalloc(sizeof(prog) * (env->subprog_cnt + 1), GFP_KERNEL); |
| 5208 | if (!func) |
| 5209 | return -ENOMEM; |
| 5210 | |
| 5211 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5212 | subprog_start = subprog_end; |
| 5213 | if (env->subprog_cnt == i) |
| 5214 | subprog_end = prog->len; |
| 5215 | else |
| 5216 | subprog_end = env->subprog_starts[i]; |
| 5217 | |
| 5218 | len = subprog_end - subprog_start; |
| 5219 | func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); |
| 5220 | if (!func[i]) |
| 5221 | goto out_free; |
| 5222 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 5223 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5224 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5225 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5226 | if (bpf_prog_calc_tag(func[i])) |
| 5227 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5228 | func[i]->is_func = 1; |
| 5229 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 5230 | * Long term would need debug info to populate names |
| 5231 | */ |
| 5232 | func[i]->aux->name[0] = 'F'; |
| 5233 | func[i]->aux->stack_depth = env->subprog_stack_depth[i]; |
| 5234 | func[i]->jit_requested = 1; |
| 5235 | func[i] = bpf_int_jit_compile(func[i]); |
| 5236 | if (!func[i]->jited) { |
| 5237 | err = -ENOTSUPP; |
| 5238 | goto out_free; |
| 5239 | } |
| 5240 | cond_resched(); |
| 5241 | } |
| 5242 | /* at this point all bpf functions were successfully JITed |
| 5243 | * now populate all bpf_calls with correct addresses and |
| 5244 | * run last pass of JIT |
| 5245 | */ |
| 5246 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5247 | insn = func[i]->insnsi; |
| 5248 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 5249 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5250 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5251 | continue; |
| 5252 | subprog = insn->off; |
| 5253 | insn->off = 0; |
| 5254 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 5255 | func[subprog]->bpf_func - |
| 5256 | __bpf_call_base; |
| 5257 | } |
| 5258 | } |
| 5259 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5260 | old_bpf_func = func[i]->bpf_func; |
| 5261 | tmp = bpf_int_jit_compile(func[i]); |
| 5262 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 5263 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
| 5264 | err = -EFAULT; |
| 5265 | goto out_free; |
| 5266 | } |
| 5267 | cond_resched(); |
| 5268 | } |
| 5269 | |
| 5270 | /* finally lock prog and jit images for all functions and |
| 5271 | * populate kallsysm |
| 5272 | */ |
| 5273 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5274 | bpf_prog_lock_ro(func[i]); |
| 5275 | bpf_prog_kallsyms_add(func[i]); |
| 5276 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5277 | |
| 5278 | /* Last step: make now unused interpreter insns from main |
| 5279 | * prog consistent for later dump requests, so they can |
| 5280 | * later look the same as if they were interpreted only. |
| 5281 | */ |
| 5282 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 5283 | unsigned long addr; |
| 5284 | |
| 5285 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5286 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5287 | continue; |
| 5288 | insn->off = env->insn_aux_data[i].call_imm; |
| 5289 | subprog = find_subprog(env, i + insn->off + 1); |
| 5290 | addr = (unsigned long)func[subprog + 1]->bpf_func; |
| 5291 | addr &= PAGE_MASK; |
| 5292 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 5293 | addr - __bpf_call_base; |
| 5294 | } |
| 5295 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5296 | prog->jited = 1; |
| 5297 | prog->bpf_func = func[0]->bpf_func; |
| 5298 | prog->aux->func = func; |
| 5299 | prog->aux->func_cnt = env->subprog_cnt + 1; |
| 5300 | return 0; |
| 5301 | out_free: |
| 5302 | for (i = 0; i <= env->subprog_cnt; i++) |
| 5303 | if (func[i]) |
| 5304 | bpf_jit_free(func[i]); |
| 5305 | kfree(func); |
| 5306 | /* cleanup main prog to be interpreted */ |
| 5307 | prog->jit_requested = 0; |
| 5308 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 5309 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5310 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5311 | continue; |
| 5312 | insn->off = 0; |
| 5313 | insn->imm = env->insn_aux_data[i].call_imm; |
| 5314 | } |
| 5315 | return err; |
| 5316 | } |
| 5317 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5318 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 5319 | { |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5320 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5321 | struct bpf_prog *prog = env->prog; |
| 5322 | struct bpf_insn *insn = prog->insnsi; |
| 5323 | int i, depth; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5324 | #endif |
| 5325 | int err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5326 | |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5327 | err = 0; |
| 5328 | if (env->prog->jit_requested) { |
| 5329 | err = jit_subprogs(env); |
| 5330 | if (err == 0) |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5331 | return 0; |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5332 | } |
| 5333 | #ifndef CONFIG_BPF_JIT_ALWAYS_ON |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5334 | for (i = 0; i < prog->len; i++, insn++) { |
| 5335 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5336 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5337 | continue; |
| 5338 | depth = get_callee_stack_depth(env, insn, i); |
| 5339 | if (depth < 0) |
| 5340 | return depth; |
| 5341 | bpf_patch_call_args(insn, depth); |
| 5342 | } |
David S. Miller | 19d28fb | 2018-01-11 21:27:54 -0500 | [diff] [blame] | 5343 | err = 0; |
| 5344 | #endif |
| 5345 | return err; |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5346 | } |
| 5347 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5348 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5349 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5350 | * |
| 5351 | * this function is called after eBPF program passed verification |
| 5352 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5353 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5354 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5355 | struct bpf_prog *prog = env->prog; |
| 5356 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5357 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5358 | const int insn_cnt = prog->len; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5359 | struct bpf_insn insn_buf[16]; |
| 5360 | struct bpf_prog *new_prog; |
| 5361 | struct bpf_map *map_ptr; |
| 5362 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5363 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5364 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 5365 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 5366 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5367 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 5368 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5369 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5370 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 5371 | prog->dst_needed = 1; |
| 5372 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 5373 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 5374 | if (insn->imm == BPF_FUNC_override_return) |
| 5375 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5376 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 5377 | /* If we tail call into other programs, we |
| 5378 | * cannot make any assumptions since they can |
| 5379 | * be replaced dynamically during runtime in |
| 5380 | * the program array. |
| 5381 | */ |
| 5382 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 5383 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 5384 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5385 | /* mark bpf_tail_call as different opcode to avoid |
| 5386 | * conditional branch in the interpeter for every normal |
| 5387 | * call and to prevent accidental JITing by JIT compiler |
| 5388 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5389 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5390 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 5391 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 5392 | |
| 5393 | /* instead of changing every JIT dealing with tail_call |
| 5394 | * emit two extra insns: |
| 5395 | * if (index >= max_entries) goto out; |
| 5396 | * index &= array->index_mask; |
| 5397 | * to avoid out-of-bounds cpu speculation |
| 5398 | */ |
| 5399 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
| 5400 | if (map_ptr == BPF_MAP_PTR_POISON) { |
Colin Ian King | 4095034 | 2018-01-10 09:20:54 +0000 | [diff] [blame] | 5401 | verbose(env, "tail_call abusing map_ptr\n"); |
Alexei Starovoitov | b215739 | 2018-01-07 17:33:02 -0800 | [diff] [blame] | 5402 | return -EINVAL; |
| 5403 | } |
| 5404 | if (!map_ptr->unpriv_array) |
| 5405 | continue; |
| 5406 | insn_buf[0] = BPF_JMP_IMM(BPF_JGE, BPF_REG_3, |
| 5407 | map_ptr->max_entries, 2); |
| 5408 | insn_buf[1] = BPF_ALU32_IMM(BPF_AND, BPF_REG_3, |
| 5409 | container_of(map_ptr, |
| 5410 | struct bpf_array, |
| 5411 | map)->index_mask); |
| 5412 | insn_buf[2] = *insn; |
| 5413 | cnt = 3; |
| 5414 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
| 5415 | if (!new_prog) |
| 5416 | return -ENOMEM; |
| 5417 | |
| 5418 | delta += cnt - 1; |
| 5419 | env->prog = prog = new_prog; |
| 5420 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5421 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5422 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5423 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 5424 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
| 5425 | * handlers are currently limited to 64 bit only. |
| 5426 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 5427 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 5428 | insn->imm == BPF_FUNC_map_lookup_elem) { |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5429 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 5430 | if (map_ptr == BPF_MAP_PTR_POISON || |
| 5431 | !map_ptr->ops->map_gen_lookup) |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5432 | goto patch_call_imm; |
| 5433 | |
| 5434 | cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf); |
| 5435 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5436 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5437 | return -EINVAL; |
| 5438 | } |
| 5439 | |
| 5440 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 5441 | cnt); |
| 5442 | if (!new_prog) |
| 5443 | return -ENOMEM; |
| 5444 | |
| 5445 | delta += cnt - 1; |
| 5446 | |
| 5447 | /* keep walking new program and skip insns we just inserted */ |
| 5448 | env->prog = prog = new_prog; |
| 5449 | insn = new_prog->insnsi + i + delta; |
| 5450 | continue; |
| 5451 | } |
| 5452 | |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 5453 | if (insn->imm == BPF_FUNC_redirect_map) { |
Daniel Borkmann | 7c30013 | 2017-09-20 00:44:21 +0200 | [diff] [blame] | 5454 | /* Note, we cannot use prog directly as imm as subsequent |
| 5455 | * rewrites would still change the prog pointer. The only |
| 5456 | * stable address we can use is aux, which also works with |
| 5457 | * prog clones during blinding. |
| 5458 | */ |
| 5459 | u64 addr = (unsigned long)prog->aux; |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 5460 | struct bpf_insn r4_ld[] = { |
| 5461 | BPF_LD_IMM64(BPF_REG_4, addr), |
| 5462 | *insn, |
| 5463 | }; |
| 5464 | cnt = ARRAY_SIZE(r4_ld); |
| 5465 | |
| 5466 | new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt); |
| 5467 | if (!new_prog) |
| 5468 | return -ENOMEM; |
| 5469 | |
| 5470 | delta += cnt - 1; |
| 5471 | env->prog = prog = new_prog; |
| 5472 | insn = new_prog->insnsi + i + delta; |
| 5473 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5474 | patch_call_imm: |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5475 | fn = env->ops->get_func_proto(insn->imm); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5476 | /* all functions that have prototype and verifier allowed |
| 5477 | * programs to call them, must be real in-kernel functions |
| 5478 | */ |
| 5479 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5480 | verbose(env, |
| 5481 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5482 | func_id_name(insn->imm), insn->imm); |
| 5483 | return -EFAULT; |
| 5484 | } |
| 5485 | insn->imm = fn->func - __bpf_call_base; |
| 5486 | } |
| 5487 | |
| 5488 | return 0; |
| 5489 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5490 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5491 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5492 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5493 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5494 | int i; |
| 5495 | |
| 5496 | if (!env->explored_states) |
| 5497 | return; |
| 5498 | |
| 5499 | for (i = 0; i < env->prog->len; i++) { |
| 5500 | sl = env->explored_states[i]; |
| 5501 | |
| 5502 | if (sl) |
| 5503 | while (sl != STATE_LIST_MARK) { |
| 5504 | sln = sl->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5505 | free_verifier_state(&sl->state, false); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5506 | kfree(sl); |
| 5507 | sl = sln; |
| 5508 | } |
| 5509 | } |
| 5510 | |
| 5511 | kfree(env->explored_states); |
| 5512 | } |
| 5513 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5514 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5515 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5516 | struct bpf_verifier_env *env; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5517 | struct bpf_verifer_log *log; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5518 | int ret = -EINVAL; |
| 5519 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 5520 | /* no program is valid */ |
| 5521 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 5522 | return -EINVAL; |
| 5523 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5524 | /* '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] | 5525 | * allocate/free it every time bpf_check() is called |
| 5526 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5527 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5528 | if (!env) |
| 5529 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5530 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5531 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5532 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 5533 | (*prog)->len); |
| 5534 | ret = -ENOMEM; |
| 5535 | if (!env->insn_aux_data) |
| 5536 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5537 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5538 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5539 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5540 | /* grab the mutex to protect few globals used by verifier */ |
| 5541 | mutex_lock(&bpf_verifier_lock); |
| 5542 | |
| 5543 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 5544 | /* user requested verbose verifier output |
| 5545 | * and supplied buffer to store the verification trace |
| 5546 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 5547 | log->level = attr->log_level; |
| 5548 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 5549 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5550 | |
| 5551 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 5552 | /* log attributes have to be sane */ |
| 5553 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || |
| 5554 | !log->level || !log->ubuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5555 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5556 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 5557 | |
| 5558 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 5559 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 5560 | env->strict_alignment = true; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5561 | |
Jakub Kicinski | cae1927 | 2017-12-27 18:39:05 -0800 | [diff] [blame] | 5562 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 5563 | ret = bpf_prog_offload_verifier_prep(env); |
| 5564 | if (ret) |
| 5565 | goto err_unlock; |
| 5566 | } |
| 5567 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5568 | ret = replace_map_fd_with_map_ptr(env); |
| 5569 | if (ret < 0) |
| 5570 | goto skip_full_check; |
| 5571 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5572 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5573 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5574 | GFP_USER); |
| 5575 | ret = -ENOMEM; |
| 5576 | if (!env->explored_states) |
| 5577 | goto skip_full_check; |
| 5578 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5579 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 5580 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5581 | ret = check_cfg(env); |
| 5582 | if (ret < 0) |
| 5583 | goto skip_full_check; |
| 5584 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5585 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 5586 | if (env->cur_state) { |
| 5587 | free_verifier_state(env->cur_state, true); |
| 5588 | env->cur_state = NULL; |
| 5589 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5590 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5591 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5592 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5593 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5594 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5595 | if (ret == 0) |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5596 | sanitize_dead_code(env); |
| 5597 | |
| 5598 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame] | 5599 | ret = check_max_stack_depth(env); |
| 5600 | |
| 5601 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5602 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 5603 | ret = convert_ctx_accesses(env); |
| 5604 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5605 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5606 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5607 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5608 | if (ret == 0) |
| 5609 | ret = fixup_call_args(env); |
| 5610 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5611 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5612 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5613 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5614 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5615 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5616 | } |
| 5617 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5618 | if (ret == 0 && env->used_map_cnt) { |
| 5619 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5620 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 5621 | sizeof(env->used_maps[0]), |
| 5622 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5623 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5624 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5625 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5626 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5627 | } |
| 5628 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5629 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5630 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5631 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5632 | |
| 5633 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 5634 | * bpf_ld_imm64 instructions |
| 5635 | */ |
| 5636 | convert_pseudo_ld_imm64(env); |
| 5637 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5638 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5639 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5640 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5641 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 5642 | * them now. Otherwise free_bpf_prog_info() will release them. |
| 5643 | */ |
| 5644 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5645 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5646 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5647 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5648 | vfree(env->insn_aux_data); |
| 5649 | err_free_env: |
| 5650 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5651 | return ret; |
| 5652 | } |