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. |
| 172 | * verbose() is used to dump the verification trace to the log, so the user |
| 173 | * can figure out what's wrong with the program |
| 174 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 175 | static __printf(2, 3) void verbose(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 | } |
| 200 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 201 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 202 | { |
| 203 | return type == PTR_TO_PACKET || |
| 204 | type == PTR_TO_PACKET_META; |
| 205 | } |
| 206 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 207 | /* string representation of 'enum bpf_reg_type' */ |
| 208 | static const char * const reg_type_str[] = { |
| 209 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 210 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 211 | [PTR_TO_CTX] = "ctx", |
| 212 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 213 | [PTR_TO_MAP_VALUE] = "map_value", |
| 214 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 215 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 216 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 217 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 218 | [PTR_TO_PACKET_END] = "pkt_end", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 219 | }; |
| 220 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 221 | static void print_liveness(struct bpf_verifier_env *env, |
| 222 | enum bpf_reg_liveness live) |
| 223 | { |
| 224 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN)) |
| 225 | verbose(env, "_"); |
| 226 | if (live & REG_LIVE_READ) |
| 227 | verbose(env, "r"); |
| 228 | if (live & REG_LIVE_WRITTEN) |
| 229 | verbose(env, "w"); |
| 230 | } |
| 231 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 232 | static struct bpf_func_state *func(struct bpf_verifier_env *env, |
| 233 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 234 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 235 | struct bpf_verifier_state *cur = env->cur_state; |
| 236 | |
| 237 | return cur->frame[reg->frameno]; |
| 238 | } |
| 239 | |
| 240 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 241 | const struct bpf_func_state *state) |
| 242 | { |
| 243 | const struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 244 | enum bpf_reg_type t; |
| 245 | int i; |
| 246 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 247 | if (state->frameno) |
| 248 | verbose(env, " frame%d:", state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 249 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 250 | reg = &state->regs[i]; |
| 251 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 252 | if (t == NOT_INIT) |
| 253 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 254 | verbose(env, " R%d", i); |
| 255 | print_liveness(env, reg->live); |
| 256 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 257 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 258 | tnum_is_const(reg->var_off)) { |
| 259 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 260 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 261 | if (t == PTR_TO_STACK) |
| 262 | verbose(env, ",call_%d", func(env, reg)->callsite); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 263 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 264 | verbose(env, "(id=%d", reg->id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 265 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 266 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 267 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 268 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 269 | else if (t == CONST_PTR_TO_MAP || |
| 270 | t == PTR_TO_MAP_VALUE || |
| 271 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 272 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 273 | reg->map_ptr->key_size, |
| 274 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 275 | if (tnum_is_const(reg->var_off)) { |
| 276 | /* Typically an immediate SCALAR_VALUE, but |
| 277 | * could be a pointer whose offset is too big |
| 278 | * for reg->off |
| 279 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 280 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 281 | } else { |
| 282 | if (reg->smin_value != reg->umin_value && |
| 283 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 284 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 285 | (long long)reg->smin_value); |
| 286 | if (reg->smax_value != reg->umax_value && |
| 287 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 288 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 289 | (long long)reg->smax_value); |
| 290 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 291 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 292 | (unsigned long long)reg->umin_value); |
| 293 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 294 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 295 | (unsigned long long)reg->umax_value); |
| 296 | if (!tnum_is_unknown(reg->var_off)) { |
| 297 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 298 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 299 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 300 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 301 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 302 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 303 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 304 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 305 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 306 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 307 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 308 | verbose(env, " fp%d", |
| 309 | (-i - 1) * BPF_REG_SIZE); |
| 310 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 311 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 312 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 313 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 314 | if (state->stack[i].slot_type[0] == STACK_ZERO) |
| 315 | verbose(env, " fp%d=0", (-i - 1) * BPF_REG_SIZE); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 316 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 317 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 320 | static int copy_stack_state(struct bpf_func_state *dst, |
| 321 | const struct bpf_func_state *src) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 322 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 323 | if (!src->stack) |
| 324 | return 0; |
| 325 | if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) { |
| 326 | /* internal bug, make state invalid to reject the program */ |
| 327 | memset(dst, 0, sizeof(*dst)); |
| 328 | return -EFAULT; |
| 329 | } |
| 330 | memcpy(dst->stack, src->stack, |
| 331 | sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE)); |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 336 | * make it consume minimal amount of memory. check_stack_write() access from |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 337 | * the program calls into realloc_func_state() to grow the stack size. |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 338 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 339 | * which this function copies over. It points to previous bpf_verifier_state |
| 340 | * which is never reallocated |
| 341 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 342 | static int realloc_func_state(struct bpf_func_state *state, int size, |
| 343 | bool copy_old) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 344 | { |
| 345 | u32 old_size = state->allocated_stack; |
| 346 | struct bpf_stack_state *new_stack; |
| 347 | int slot = size / BPF_REG_SIZE; |
| 348 | |
| 349 | if (size <= old_size || !size) { |
| 350 | if (copy_old) |
| 351 | return 0; |
| 352 | state->allocated_stack = slot * BPF_REG_SIZE; |
| 353 | if (!size && old_size) { |
| 354 | kfree(state->stack); |
| 355 | state->stack = NULL; |
| 356 | } |
| 357 | return 0; |
| 358 | } |
| 359 | new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state), |
| 360 | GFP_KERNEL); |
| 361 | if (!new_stack) |
| 362 | return -ENOMEM; |
| 363 | if (copy_old) { |
| 364 | if (state->stack) |
| 365 | memcpy(new_stack, state->stack, |
| 366 | sizeof(*new_stack) * (old_size / BPF_REG_SIZE)); |
| 367 | memset(new_stack + old_size / BPF_REG_SIZE, 0, |
| 368 | sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE); |
| 369 | } |
| 370 | state->allocated_stack = slot * BPF_REG_SIZE; |
| 371 | kfree(state->stack); |
| 372 | state->stack = new_stack; |
| 373 | return 0; |
| 374 | } |
| 375 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 376 | static void free_func_state(struct bpf_func_state *state) |
| 377 | { |
| 378 | kfree(state->stack); |
| 379 | kfree(state); |
| 380 | } |
| 381 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 382 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 383 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 384 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 385 | int i; |
| 386 | |
| 387 | for (i = 0; i <= state->curframe; i++) { |
| 388 | free_func_state(state->frame[i]); |
| 389 | state->frame[i] = NULL; |
| 390 | } |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 391 | if (free_self) |
| 392 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* copy verifier state from src to dst growing dst stack space |
| 396 | * when necessary to accommodate larger src stack |
| 397 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 398 | static int copy_func_state(struct bpf_func_state *dst, |
| 399 | const struct bpf_func_state *src) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 400 | { |
| 401 | int err; |
| 402 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 403 | err = realloc_func_state(dst, src->allocated_stack, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 404 | if (err) |
| 405 | return err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 406 | memcpy(dst, src, offsetof(struct bpf_func_state, allocated_stack)); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 407 | return copy_stack_state(dst, src); |
| 408 | } |
| 409 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 410 | static int copy_verifier_state(struct bpf_verifier_state *dst_state, |
| 411 | const struct bpf_verifier_state *src) |
| 412 | { |
| 413 | struct bpf_func_state *dst; |
| 414 | int i, err; |
| 415 | |
| 416 | /* if dst has more stack frames then src frame, free them */ |
| 417 | for (i = src->curframe + 1; i <= dst_state->curframe; i++) { |
| 418 | free_func_state(dst_state->frame[i]); |
| 419 | dst_state->frame[i] = NULL; |
| 420 | } |
| 421 | dst_state->curframe = src->curframe; |
| 422 | dst_state->parent = src->parent; |
| 423 | for (i = 0; i <= src->curframe; i++) { |
| 424 | dst = dst_state->frame[i]; |
| 425 | if (!dst) { |
| 426 | dst = kzalloc(sizeof(*dst), GFP_KERNEL); |
| 427 | if (!dst) |
| 428 | return -ENOMEM; |
| 429 | dst_state->frame[i] = dst; |
| 430 | } |
| 431 | err = copy_func_state(dst, src->frame[i]); |
| 432 | if (err) |
| 433 | return err; |
| 434 | } |
| 435 | return 0; |
| 436 | } |
| 437 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 438 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 439 | int *insn_idx) |
| 440 | { |
| 441 | struct bpf_verifier_state *cur = env->cur_state; |
| 442 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 443 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 444 | |
| 445 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 446 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 447 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 448 | if (cur) { |
| 449 | err = copy_verifier_state(cur, &head->st); |
| 450 | if (err) |
| 451 | return err; |
| 452 | } |
| 453 | if (insn_idx) |
| 454 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 455 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 456 | *prev_insn_idx = head->prev_insn_idx; |
| 457 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 458 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 459 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 460 | env->head = elem; |
| 461 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 462 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 465 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 466 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 467 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 468 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 469 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 470 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 471 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 472 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 473 | if (!elem) |
| 474 | goto err; |
| 475 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 476 | elem->insn_idx = insn_idx; |
| 477 | elem->prev_insn_idx = prev_insn_idx; |
| 478 | elem->next = env->head; |
| 479 | env->head = elem; |
| 480 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 481 | err = copy_verifier_state(&elem->st, cur); |
| 482 | if (err) |
| 483 | goto err; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 484 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 485 | verbose(env, "BPF program is too complex\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 486 | goto err; |
| 487 | } |
| 488 | return &elem->st; |
| 489 | err: |
| 490 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 491 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 492 | return NULL; |
| 493 | } |
| 494 | |
| 495 | #define CALLER_SAVED_REGS 6 |
| 496 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 497 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 498 | }; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 499 | #define CALLEE_SAVED_REGS 5 |
| 500 | static const int callee_saved[CALLEE_SAVED_REGS] = { |
| 501 | BPF_REG_6, BPF_REG_7, BPF_REG_8, BPF_REG_9 |
| 502 | }; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 503 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 504 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 505 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 506 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 507 | * known to have the value @imm. |
| 508 | */ |
| 509 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 510 | { |
| 511 | reg->id = 0; |
| 512 | reg->var_off = tnum_const(imm); |
| 513 | reg->smin_value = (s64)imm; |
| 514 | reg->smax_value = (s64)imm; |
| 515 | reg->umin_value = imm; |
| 516 | reg->umax_value = imm; |
| 517 | } |
| 518 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 519 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 520 | * used only on registers holding a pointer type. |
| 521 | */ |
| 522 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 523 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 524 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 527 | static void __mark_reg_const_zero(struct bpf_reg_state *reg) |
| 528 | { |
| 529 | __mark_reg_known(reg, 0); |
| 530 | reg->off = 0; |
| 531 | reg->type = SCALAR_VALUE; |
| 532 | } |
| 533 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 534 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 535 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 536 | { |
| 537 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 538 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 539 | /* Something bad happened, let's kill all regs */ |
| 540 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 541 | __mark_reg_not_init(regs + regno); |
| 542 | return; |
| 543 | } |
| 544 | __mark_reg_known_zero(regs + regno); |
| 545 | } |
| 546 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 547 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 548 | { |
| 549 | return type_is_pkt_pointer(reg->type); |
| 550 | } |
| 551 | |
| 552 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 553 | { |
| 554 | return reg_is_pkt_pointer(reg) || |
| 555 | reg->type == PTR_TO_PACKET_END; |
| 556 | } |
| 557 | |
| 558 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 559 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 560 | enum bpf_reg_type which) |
| 561 | { |
| 562 | /* The register can already have a range from prior markings. |
| 563 | * This is fine as long as it hasn't been advanced from its |
| 564 | * origin. |
| 565 | */ |
| 566 | return reg->type == which && |
| 567 | reg->id == 0 && |
| 568 | reg->off == 0 && |
| 569 | tnum_equals_const(reg->var_off, 0); |
| 570 | } |
| 571 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 572 | /* Attempts to improve min/max values based on var_off information */ |
| 573 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 574 | { |
| 575 | /* min signed is max(sign bit) | min(other bits) */ |
| 576 | reg->smin_value = max_t(s64, reg->smin_value, |
| 577 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 578 | /* max signed is min(sign bit) | max(other bits) */ |
| 579 | reg->smax_value = min_t(s64, reg->smax_value, |
| 580 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 581 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 582 | reg->umax_value = min(reg->umax_value, |
| 583 | reg->var_off.value | reg->var_off.mask); |
| 584 | } |
| 585 | |
| 586 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 587 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 588 | { |
| 589 | /* Learn sign from signed bounds. |
| 590 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 591 | * are the same, so combine. This works even in the negative case, e.g. |
| 592 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 593 | */ |
| 594 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 595 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 596 | reg->umin_value); |
| 597 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 598 | reg->umax_value); |
| 599 | return; |
| 600 | } |
| 601 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 602 | * boundary, so we must be careful. |
| 603 | */ |
| 604 | if ((s64)reg->umax_value >= 0) { |
| 605 | /* Positive. We can't learn anything from the smin, but smax |
| 606 | * is positive, hence safe. |
| 607 | */ |
| 608 | reg->smin_value = reg->umin_value; |
| 609 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 610 | reg->umax_value); |
| 611 | } else if ((s64)reg->umin_value < 0) { |
| 612 | /* Negative. We can't learn anything from the smax, but smin |
| 613 | * is negative, hence safe. |
| 614 | */ |
| 615 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 616 | reg->umin_value); |
| 617 | reg->smax_value = reg->umax_value; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 622 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 623 | { |
| 624 | reg->var_off = tnum_intersect(reg->var_off, |
| 625 | tnum_range(reg->umin_value, |
| 626 | reg->umax_value)); |
| 627 | } |
| 628 | |
| 629 | /* Reset the min/max bounds of a register */ |
| 630 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 631 | { |
| 632 | reg->smin_value = S64_MIN; |
| 633 | reg->smax_value = S64_MAX; |
| 634 | reg->umin_value = 0; |
| 635 | reg->umax_value = U64_MAX; |
| 636 | } |
| 637 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 638 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 639 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 640 | { |
| 641 | reg->type = SCALAR_VALUE; |
| 642 | reg->id = 0; |
| 643 | reg->off = 0; |
| 644 | reg->var_off = tnum_unknown; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 645 | reg->frameno = 0; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 646 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 649 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 650 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 651 | { |
| 652 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 653 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 654 | /* Something bad happened, let's kill all regs except FP */ |
| 655 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 656 | __mark_reg_not_init(regs + regno); |
| 657 | return; |
| 658 | } |
| 659 | __mark_reg_unknown(regs + regno); |
| 660 | } |
| 661 | |
| 662 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 663 | { |
| 664 | __mark_reg_unknown(reg); |
| 665 | reg->type = NOT_INIT; |
| 666 | } |
| 667 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 668 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 669 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 670 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 671 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 672 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 673 | /* Something bad happened, let's kill all regs except FP */ |
| 674 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 675 | __mark_reg_not_init(regs + regno); |
| 676 | return; |
| 677 | } |
| 678 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 681 | static void init_reg_state(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 682 | struct bpf_func_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 683 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 684 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 685 | int i; |
| 686 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 687 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 688 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 689 | regs[i].live = REG_LIVE_NONE; |
| 690 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 691 | |
| 692 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 693 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 694 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 695 | regs[BPF_REG_FP].frameno = state->frameno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 696 | |
| 697 | /* 1st arg to a function */ |
| 698 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 699 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 702 | #define BPF_MAIN_FUNC (-1) |
| 703 | static void init_func_state(struct bpf_verifier_env *env, |
| 704 | struct bpf_func_state *state, |
| 705 | int callsite, int frameno, int subprogno) |
| 706 | { |
| 707 | state->callsite = callsite; |
| 708 | state->frameno = frameno; |
| 709 | state->subprogno = subprogno; |
| 710 | init_reg_state(env, state); |
| 711 | } |
| 712 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 713 | enum reg_arg_type { |
| 714 | SRC_OP, /* register is used as source operand */ |
| 715 | DST_OP, /* register is used as destination operand */ |
| 716 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 717 | }; |
| 718 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 719 | static int cmp_subprogs(const void *a, const void *b) |
| 720 | { |
| 721 | return *(int *)a - *(int *)b; |
| 722 | } |
| 723 | |
| 724 | static int find_subprog(struct bpf_verifier_env *env, int off) |
| 725 | { |
| 726 | u32 *p; |
| 727 | |
| 728 | p = bsearch(&off, env->subprog_starts, env->subprog_cnt, |
| 729 | sizeof(env->subprog_starts[0]), cmp_subprogs); |
| 730 | if (!p) |
| 731 | return -ENOENT; |
| 732 | return p - env->subprog_starts; |
| 733 | |
| 734 | } |
| 735 | |
| 736 | static int add_subprog(struct bpf_verifier_env *env, int off) |
| 737 | { |
| 738 | int insn_cnt = env->prog->len; |
| 739 | int ret; |
| 740 | |
| 741 | if (off >= insn_cnt || off < 0) { |
| 742 | verbose(env, "call to invalid destination\n"); |
| 743 | return -EINVAL; |
| 744 | } |
| 745 | ret = find_subprog(env, off); |
| 746 | if (ret >= 0) |
| 747 | return 0; |
| 748 | if (env->subprog_cnt >= BPF_MAX_SUBPROGS) { |
| 749 | verbose(env, "too many subprograms\n"); |
| 750 | return -E2BIG; |
| 751 | } |
| 752 | env->subprog_starts[env->subprog_cnt++] = off; |
| 753 | sort(env->subprog_starts, env->subprog_cnt, |
| 754 | sizeof(env->subprog_starts[0]), cmp_subprogs, NULL); |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | static int check_subprogs(struct bpf_verifier_env *env) |
| 759 | { |
| 760 | int i, ret, subprog_start, subprog_end, off, cur_subprog = 0; |
| 761 | struct bpf_insn *insn = env->prog->insnsi; |
| 762 | int insn_cnt = env->prog->len; |
| 763 | |
| 764 | /* determine subprog starts. The end is one before the next starts */ |
| 765 | for (i = 0; i < insn_cnt; i++) { |
| 766 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 767 | continue; |
| 768 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 769 | continue; |
| 770 | if (!env->allow_ptr_leaks) { |
| 771 | verbose(env, "function calls to other bpf functions are allowed for root only\n"); |
| 772 | return -EPERM; |
| 773 | } |
| 774 | if (bpf_prog_is_dev_bound(env->prog->aux)) { |
Colin Ian King | e90004d5 | 2017-12-18 14:03:12 +0000 | [diff] [blame] | 775 | verbose(env, "function calls in offloaded programs are not supported yet\n"); |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 776 | return -EINVAL; |
| 777 | } |
| 778 | ret = add_subprog(env, i + insn[i].imm + 1); |
| 779 | if (ret < 0) |
| 780 | return ret; |
| 781 | } |
| 782 | |
| 783 | if (env->log.level > 1) |
| 784 | for (i = 0; i < env->subprog_cnt; i++) |
| 785 | verbose(env, "func#%d @%d\n", i, env->subprog_starts[i]); |
| 786 | |
| 787 | /* now check that all jumps are within the same subprog */ |
| 788 | subprog_start = 0; |
| 789 | if (env->subprog_cnt == cur_subprog) |
| 790 | subprog_end = insn_cnt; |
| 791 | else |
| 792 | subprog_end = env->subprog_starts[cur_subprog++]; |
| 793 | for (i = 0; i < insn_cnt; i++) { |
| 794 | u8 code = insn[i].code; |
| 795 | |
| 796 | if (BPF_CLASS(code) != BPF_JMP) |
| 797 | goto next; |
| 798 | if (BPF_OP(code) == BPF_EXIT || BPF_OP(code) == BPF_CALL) |
| 799 | goto next; |
| 800 | off = i + insn[i].off + 1; |
| 801 | if (off < subprog_start || off >= subprog_end) { |
| 802 | verbose(env, "jump out of range from insn %d to %d\n", i, off); |
| 803 | return -EINVAL; |
| 804 | } |
| 805 | next: |
| 806 | if (i == subprog_end - 1) { |
| 807 | /* to avoid fall-through from one subprog into another |
| 808 | * the last insn of the subprog should be either exit |
| 809 | * or unconditional jump back |
| 810 | */ |
| 811 | if (code != (BPF_JMP | BPF_EXIT) && |
| 812 | code != (BPF_JMP | BPF_JA)) { |
| 813 | verbose(env, "last insn is not an exit or jmp\n"); |
| 814 | return -EINVAL; |
| 815 | } |
| 816 | subprog_start = subprog_end; |
| 817 | if (env->subprog_cnt == cur_subprog) |
| 818 | subprog_end = insn_cnt; |
| 819 | else |
| 820 | subprog_end = env->subprog_starts[cur_subprog++]; |
| 821 | } |
| 822 | } |
| 823 | return 0; |
| 824 | } |
| 825 | |
Colin Ian King | fa2d41a | 2017-12-18 17:47:07 +0000 | [diff] [blame] | 826 | static |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 827 | struct bpf_verifier_state *skip_callee(struct bpf_verifier_env *env, |
| 828 | const struct bpf_verifier_state *state, |
| 829 | struct bpf_verifier_state *parent, |
| 830 | u32 regno) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 831 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 832 | struct bpf_verifier_state *tmp = NULL; |
| 833 | |
| 834 | /* 'parent' could be a state of caller and |
| 835 | * 'state' could be a state of callee. In such case |
| 836 | * parent->curframe < state->curframe |
| 837 | * and it's ok for r1 - r5 registers |
| 838 | * |
| 839 | * 'parent' could be a callee's state after it bpf_exit-ed. |
| 840 | * In such case parent->curframe > state->curframe |
| 841 | * and it's ok for r0 only |
| 842 | */ |
| 843 | if (parent->curframe == state->curframe || |
| 844 | (parent->curframe < state->curframe && |
| 845 | regno >= BPF_REG_1 && regno <= BPF_REG_5) || |
| 846 | (parent->curframe > state->curframe && |
| 847 | regno == BPF_REG_0)) |
| 848 | return parent; |
| 849 | |
| 850 | if (parent->curframe > state->curframe && |
| 851 | regno >= BPF_REG_6) { |
| 852 | /* for callee saved regs we have to skip the whole chain |
| 853 | * of states that belong to callee and mark as LIVE_READ |
| 854 | * the registers before the call |
| 855 | */ |
| 856 | tmp = parent; |
| 857 | while (tmp && tmp->curframe != state->curframe) { |
| 858 | tmp = tmp->parent; |
| 859 | } |
| 860 | if (!tmp) |
| 861 | goto bug; |
| 862 | parent = tmp; |
| 863 | } else { |
| 864 | goto bug; |
| 865 | } |
| 866 | return parent; |
| 867 | bug: |
| 868 | verbose(env, "verifier bug regno %d tmp %p\n", regno, tmp); |
| 869 | verbose(env, "regno %d parent frame %d current frame %d\n", |
| 870 | regno, parent->curframe, state->curframe); |
Colin Ian King | fa2d41a | 2017-12-18 17:47:07 +0000 | [diff] [blame] | 871 | return NULL; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static int mark_reg_read(struct bpf_verifier_env *env, |
| 875 | const struct bpf_verifier_state *state, |
| 876 | struct bpf_verifier_state *parent, |
| 877 | u32 regno) |
| 878 | { |
| 879 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 880 | |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 881 | if (regno == BPF_REG_FP) |
| 882 | /* 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] | 883 | return 0; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 884 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 885 | while (parent) { |
| 886 | /* if read wasn't screened by an earlier write ... */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 887 | if (writes && state->frame[state->curframe]->regs[regno].live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 888 | break; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 889 | parent = skip_callee(env, state, parent, regno); |
| 890 | if (!parent) |
| 891 | return -EFAULT; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 892 | /* ... then we depend on parent's value */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 893 | parent->frame[parent->curframe]->regs[regno].live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 894 | state = parent; |
| 895 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 896 | writes = true; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 897 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 898 | return 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 902 | enum reg_arg_type t) |
| 903 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 904 | struct bpf_verifier_state *vstate = env->cur_state; |
| 905 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 906 | struct bpf_reg_state *regs = state->regs; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 907 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 908 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 909 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 910 | return -EINVAL; |
| 911 | } |
| 912 | |
| 913 | if (t == SRC_OP) { |
| 914 | /* check whether register used as source operand can be read */ |
| 915 | if (regs[regno].type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 916 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 917 | return -EACCES; |
| 918 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 919 | return mark_reg_read(env, vstate, vstate->parent, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 920 | } else { |
| 921 | /* check whether register used as dest operand can be written to */ |
| 922 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 923 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 924 | return -EACCES; |
| 925 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 926 | regs[regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 927 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 928 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 929 | } |
| 930 | return 0; |
| 931 | } |
| 932 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 933 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 934 | { |
| 935 | switch (type) { |
| 936 | case PTR_TO_MAP_VALUE: |
| 937 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 938 | case PTR_TO_STACK: |
| 939 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 940 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 941 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 942 | case PTR_TO_PACKET_END: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 943 | case CONST_PTR_TO_MAP: |
| 944 | return true; |
| 945 | default: |
| 946 | return false; |
| 947 | } |
| 948 | } |
| 949 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 950 | /* Does this register contain a constant zero? */ |
| 951 | static bool register_is_null(struct bpf_reg_state *reg) |
| 952 | { |
| 953 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
| 954 | } |
| 955 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 956 | /* check_stack_read/write functions track spill/fill of registers, |
| 957 | * stack boundary and alignment are checked in check_mem_access() |
| 958 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 959 | static int check_stack_write(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 960 | struct bpf_func_state *state, /* func where register points to */ |
| 961 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 962 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 963 | struct bpf_func_state *cur; /* state of the current function */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 964 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 965 | enum bpf_reg_type type; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 966 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 967 | err = realloc_func_state(state, round_up(slot + 1, BPF_REG_SIZE), |
| 968 | true); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 969 | if (err) |
| 970 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 971 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 972 | * so it's aligned access and [off, off + size) are within stack limits |
| 973 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 974 | if (!env->allow_ptr_leaks && |
| 975 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 976 | size != BPF_REG_SIZE) { |
| 977 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 978 | return -EACCES; |
| 979 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 980 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 981 | cur = env->cur_state->frame[env->cur_state->curframe]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 982 | if (value_regno >= 0 && |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 983 | is_spillable_regtype((type = cur->regs[value_regno].type))) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 984 | |
| 985 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 986 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 987 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 988 | return -EACCES; |
| 989 | } |
| 990 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 991 | if (state != cur && type == PTR_TO_STACK) { |
| 992 | verbose(env, "cannot spill pointers to stack into stack frame of the caller\n"); |
| 993 | return -EINVAL; |
| 994 | } |
| 995 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 996 | /* save register state */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 997 | state->stack[spi].spilled_ptr = cur->regs[value_regno]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 998 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 999 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1000 | for (i = 0; i < BPF_REG_SIZE; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1001 | state->stack[spi].slot_type[i] = STACK_SPILL; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1002 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1003 | u8 type = STACK_MISC; |
| 1004 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1005 | /* regular write of data into stack */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1006 | state->stack[spi].spilled_ptr = (struct bpf_reg_state) {}; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1007 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1008 | /* only mark the slot as written if all 8 bytes were written |
| 1009 | * otherwise read propagation may incorrectly stop too soon |
| 1010 | * when stack slots are partially written. |
| 1011 | * This heuristic means that read propagation will be |
| 1012 | * conservative, since it will add reg_live_read marks |
| 1013 | * to stack slots all the way to first state when programs |
| 1014 | * writes+reads less than 8 bytes |
| 1015 | */ |
| 1016 | if (size == BPF_REG_SIZE) |
| 1017 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
| 1018 | |
| 1019 | /* when we zero initialize stack slots mark them as such */ |
| 1020 | if (value_regno >= 0 && |
| 1021 | register_is_null(&cur->regs[value_regno])) |
| 1022 | type = STACK_ZERO; |
| 1023 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1024 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1025 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1026 | type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1027 | } |
| 1028 | return 0; |
| 1029 | } |
| 1030 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1031 | /* registers of every function are unique and mark_reg_read() propagates |
| 1032 | * the liveness in the following cases: |
| 1033 | * - from callee into caller for R1 - R5 that were used as arguments |
| 1034 | * - from caller into callee for R0 that used as result of the call |
| 1035 | * - from caller to the same caller skipping states of the callee for R6 - R9, |
| 1036 | * since R6 - R9 are callee saved by implicit function prologue and |
| 1037 | * caller's R6 != callee's R6, so when we propagate liveness up to |
| 1038 | * parent states we need to skip callee states for R6 - R9. |
| 1039 | * |
| 1040 | * stack slot marking is different, since stacks of caller and callee are |
| 1041 | * accessible in both (since caller can pass a pointer to caller's stack to |
| 1042 | * callee which can pass it to another function), hence mark_stack_slot_read() |
| 1043 | * has to propagate the stack liveness to all parent states at given frame number. |
| 1044 | * Consider code: |
| 1045 | * f1() { |
| 1046 | * ptr = fp - 8; |
| 1047 | * *ptr = ctx; |
| 1048 | * call f2 { |
| 1049 | * .. = *ptr; |
| 1050 | * } |
| 1051 | * .. = *ptr; |
| 1052 | * } |
| 1053 | * First *ptr is reading from f1's stack and mark_stack_slot_read() has |
| 1054 | * to mark liveness at the f1's frame and not f2's frame. |
| 1055 | * Second *ptr is also reading from f1's stack and mark_stack_slot_read() has |
| 1056 | * to propagate liveness to f2 states at f1's frame level and further into |
| 1057 | * f1 states at f1's frame level until write into that stack slot |
| 1058 | */ |
| 1059 | static void mark_stack_slot_read(struct bpf_verifier_env *env, |
| 1060 | const struct bpf_verifier_state *state, |
| 1061 | struct bpf_verifier_state *parent, |
| 1062 | int slot, int frameno) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1063 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1064 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1065 | |
| 1066 | while (parent) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1067 | if (parent->frame[frameno]->allocated_stack <= slot * BPF_REG_SIZE) |
| 1068 | /* since LIVE_WRITTEN mark is only done for full 8-byte |
| 1069 | * write the read marks are conservative and parent |
| 1070 | * state may not even have the stack allocated. In such case |
| 1071 | * end the propagation, since the loop reached beginning |
| 1072 | * of the function |
| 1073 | */ |
| 1074 | break; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1075 | /* if read wasn't screened by an earlier write ... */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1076 | 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] | 1077 | break; |
| 1078 | /* ... then we depend on parent's value */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1079 | parent->frame[frameno]->stack[slot].spilled_ptr.live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1080 | state = parent; |
| 1081 | parent = state->parent; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1082 | writes = true; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1086 | static int check_stack_read(struct bpf_verifier_env *env, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1087 | struct bpf_func_state *reg_state /* func where register points to */, |
| 1088 | int off, int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1089 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1090 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1091 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1092 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
| 1093 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1094 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1095 | if (reg_state->allocated_stack <= slot) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1096 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 1097 | off, size); |
| 1098 | return -EACCES; |
| 1099 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1100 | stype = reg_state->stack[spi].slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1101 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1102 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1103 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1104 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1105 | return -EACCES; |
| 1106 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1107 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1108 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1109 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1110 | return -EACCES; |
| 1111 | } |
| 1112 | } |
| 1113 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1114 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1115 | /* restore register state from stack */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1116 | state->regs[value_regno] = reg_state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 1117 | /* mark reg as written since spilled pointer state likely |
| 1118 | * has its liveness marks cleared by is_state_visited() |
| 1119 | * which resets stack/reg liveness for state transitions |
| 1120 | */ |
| 1121 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1122 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1123 | mark_stack_slot_read(env, vstate, vstate->parent, spi, |
| 1124 | reg_state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1125 | return 0; |
| 1126 | } else { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1127 | int zeros = 0; |
| 1128 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1129 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1130 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 1131 | continue; |
| 1132 | if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 1133 | zeros++; |
| 1134 | continue; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1135 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1136 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 1137 | off, i, size); |
| 1138 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1139 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1140 | mark_stack_slot_read(env, vstate, vstate->parent, spi, |
| 1141 | reg_state->frameno); |
| 1142 | if (value_regno >= 0) { |
| 1143 | if (zeros == size) { |
| 1144 | /* any size read into register is zero extended, |
| 1145 | * so the whole register == const_zero |
| 1146 | */ |
| 1147 | __mark_reg_const_zero(&state->regs[value_regno]); |
| 1148 | } else { |
| 1149 | /* have read misc data from the stack */ |
| 1150 | mark_reg_unknown(env, state->regs, value_regno); |
| 1151 | } |
| 1152 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 1153 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1154 | return 0; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1159 | 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] | 1160 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1161 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1162 | struct bpf_reg_state *regs = cur_regs(env); |
| 1163 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1164 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1165 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1166 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1167 | 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] | 1168 | map->value_size, off, size); |
| 1169 | return -EACCES; |
| 1170 | } |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1174 | /* check read/write into a map element with possible variable offset */ |
| 1175 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1176 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1177 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1178 | struct bpf_verifier_state *vstate = env->cur_state; |
| 1179 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1180 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 1181 | int err; |
| 1182 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1183 | /* We may have adjusted the register to this map value, so we |
| 1184 | * need to try adding each of min_value and max_value to off |
| 1185 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1186 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1187 | if (env->log.level) |
| 1188 | print_verifier_state(env, state); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1189 | /* The minimum value is only important with signed |
| 1190 | * comparisons where we can't assume the floor of a |
| 1191 | * value is 0. If we are using signed variables for our |
| 1192 | * index'es we need to make sure that whatever we use |
| 1193 | * will have a set floor within our range. |
| 1194 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1195 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1196 | 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] | 1197 | regno); |
| 1198 | return -EACCES; |
| 1199 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1200 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 1201 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1202 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1203 | verbose(env, "R%d min value is outside of the array range\n", |
| 1204 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1205 | return err; |
| 1206 | } |
| 1207 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1208 | /* If we haven't set a max value then we need to bail since we can't be |
| 1209 | * sure we won't do bad things. |
| 1210 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1211 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1212 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1213 | 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] | 1214 | regno); |
| 1215 | return -EACCES; |
| 1216 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1217 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 1218 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1219 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1220 | verbose(env, "R%d max value is outside of the array range\n", |
| 1221 | regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1222 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1225 | #define MAX_PACKET_OFF 0xffff |
| 1226 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1227 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1228 | const struct bpf_call_arg_meta *meta, |
| 1229 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1230 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1231 | switch (env->prog->type) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1232 | case BPF_PROG_TYPE_LWT_IN: |
| 1233 | case BPF_PROG_TYPE_LWT_OUT: |
| 1234 | /* dst_input() and dst_output() can't write for now */ |
| 1235 | if (t == BPF_WRITE) |
| 1236 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 1237 | /* fallthrough */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1238 | case BPF_PROG_TYPE_SCHED_CLS: |
| 1239 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1240 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1241 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 1242 | case BPF_PROG_TYPE_SK_SKB: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1243 | if (meta) |
| 1244 | return meta->pkt_access; |
| 1245 | |
| 1246 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1247 | return true; |
| 1248 | default: |
| 1249 | return false; |
| 1250 | } |
| 1251 | } |
| 1252 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1253 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1254 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1255 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1256 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1257 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1258 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1259 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 1260 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1261 | 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] | 1262 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1263 | return -EACCES; |
| 1264 | } |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1268 | 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] | 1269 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1270 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1271 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1272 | struct bpf_reg_state *reg = ®s[regno]; |
| 1273 | int err; |
| 1274 | |
| 1275 | /* We may have added a variable offset to the packet pointer; but any |
| 1276 | * reg->range we have comes after that. We are only checking the fixed |
| 1277 | * offset. |
| 1278 | */ |
| 1279 | |
| 1280 | /* We don't allow negative numbers, because we aren't tracking enough |
| 1281 | * detail to prove they're safe. |
| 1282 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1283 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1284 | 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] | 1285 | regno); |
| 1286 | return -EACCES; |
| 1287 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1288 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1289 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1290 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1291 | return err; |
| 1292 | } |
| 1293 | return err; |
| 1294 | } |
| 1295 | |
| 1296 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1297 | 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] | 1298 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1299 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1300 | struct bpf_insn_access_aux info = { |
| 1301 | .reg_type = *reg_type, |
| 1302 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1303 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1304 | if (env->ops->is_valid_access && |
| 1305 | env->ops->is_valid_access(off, size, t, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 1306 | /* A non zero info.ctx_field_size indicates that this field is a |
| 1307 | * candidate for later verifier transformation to load the whole |
| 1308 | * field and then apply a mask when accessed with a narrower |
| 1309 | * access than actual ctx access size. A zero info.ctx_field_size |
| 1310 | * will only allow for whole field access and rejects any other |
| 1311 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1312 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 1313 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1314 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 1315 | 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] | 1316 | /* remember the offset of last byte accessed in ctx */ |
| 1317 | if (env->prog->aux->max_ctx_offset < off + size) |
| 1318 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1319 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 1320 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1321 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1322 | 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] | 1323 | return -EACCES; |
| 1324 | } |
| 1325 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1326 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 1327 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1328 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1329 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1330 | return false; |
| 1331 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1332 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1335 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 1336 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1337 | return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1340 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1341 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1342 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1343 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1344 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1345 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1346 | |
| 1347 | /* Byte size accesses are always allowed. */ |
| 1348 | if (!strict || size == 1) |
| 1349 | return 0; |
| 1350 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1351 | /* For platforms that do not have a Kconfig enabling |
| 1352 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1353 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1354 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1355 | * to this code only in strict mode where we want to emulate |
| 1356 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1357 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1358 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1359 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1360 | |
| 1361 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1362 | if (!tnum_is_aligned(reg_off, size)) { |
| 1363 | char tn_buf[48]; |
| 1364 | |
| 1365 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1366 | verbose(env, |
| 1367 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1368 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1369 | return -EACCES; |
| 1370 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1371 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1372 | return 0; |
| 1373 | } |
| 1374 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1375 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1376 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1377 | const char *pointer_desc, |
| 1378 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1379 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1380 | struct tnum reg_off; |
| 1381 | |
| 1382 | /* Byte size accesses are always allowed. */ |
| 1383 | if (!strict || size == 1) |
| 1384 | return 0; |
| 1385 | |
| 1386 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1387 | if (!tnum_is_aligned(reg_off, size)) { |
| 1388 | char tn_buf[48]; |
| 1389 | |
| 1390 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1391 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1392 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1393 | return -EACCES; |
| 1394 | } |
| 1395 | |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1399 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
| 1400 | const struct bpf_reg_state *reg, |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1401 | int off, int size) |
| 1402 | { |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1403 | bool strict = env->strict_alignment; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1404 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1405 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1406 | switch (reg->type) { |
| 1407 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1408 | case PTR_TO_PACKET_META: |
| 1409 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1410 | * right in front, treat it the very same way. |
| 1411 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1412 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1413 | case PTR_TO_MAP_VALUE: |
| 1414 | pointer_desc = "value "; |
| 1415 | break; |
| 1416 | case PTR_TO_CTX: |
| 1417 | pointer_desc = "context "; |
| 1418 | break; |
| 1419 | case PTR_TO_STACK: |
| 1420 | pointer_desc = "stack "; |
| 1421 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1422 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1423 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1424 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1425 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 1426 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1427 | } |
| 1428 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1429 | static int update_stack_depth(struct bpf_verifier_env *env, |
| 1430 | const struct bpf_func_state *func, |
| 1431 | int off) |
| 1432 | { |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame^] | 1433 | u16 stack = env->subprog_stack_depth[func->subprogno]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1434 | |
| 1435 | if (stack >= -off) |
| 1436 | return 0; |
| 1437 | |
| 1438 | /* update known max for given subprogram */ |
| 1439 | env->subprog_stack_depth[func->subprogno] = -off; |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame^] | 1440 | return 0; |
| 1441 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1442 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame^] | 1443 | /* starting from main bpf function walk all instructions of the function |
| 1444 | * and recursively walk all callees that given function can call. |
| 1445 | * Ignore jump and exit insns. |
| 1446 | * Since recursion is prevented by check_cfg() this algorithm |
| 1447 | * only needs a local stack of MAX_CALL_FRAMES to remember callsites |
| 1448 | */ |
| 1449 | static int check_max_stack_depth(struct bpf_verifier_env *env) |
| 1450 | { |
| 1451 | int depth = 0, frame = 0, subprog = 0, i = 0, subprog_end; |
| 1452 | struct bpf_insn *insn = env->prog->insnsi; |
| 1453 | int insn_cnt = env->prog->len; |
| 1454 | int ret_insn[MAX_CALL_FRAMES]; |
| 1455 | int ret_prog[MAX_CALL_FRAMES]; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1456 | |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame^] | 1457 | process_func: |
| 1458 | /* round up to 32-bytes, since this is granularity |
| 1459 | * of interpreter stack size |
| 1460 | */ |
| 1461 | depth += round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32); |
| 1462 | if (depth > MAX_BPF_STACK) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1463 | 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^] | 1464 | frame + 1, depth); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1465 | return -EACCES; |
| 1466 | } |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame^] | 1467 | continue_func: |
| 1468 | if (env->subprog_cnt == subprog) |
| 1469 | subprog_end = insn_cnt; |
| 1470 | else |
| 1471 | subprog_end = env->subprog_starts[subprog]; |
| 1472 | for (; i < subprog_end; i++) { |
| 1473 | if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1474 | continue; |
| 1475 | if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1476 | continue; |
| 1477 | /* remember insn and function to return to */ |
| 1478 | ret_insn[frame] = i + 1; |
| 1479 | ret_prog[frame] = subprog; |
| 1480 | |
| 1481 | /* find the callee */ |
| 1482 | i = i + insn[i].imm + 1; |
| 1483 | subprog = find_subprog(env, i); |
| 1484 | if (subprog < 0) { |
| 1485 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1486 | i); |
| 1487 | return -EFAULT; |
| 1488 | } |
| 1489 | subprog++; |
| 1490 | frame++; |
| 1491 | if (frame >= MAX_CALL_FRAMES) { |
| 1492 | WARN_ONCE(1, "verifier bug. Call stack is too deep\n"); |
| 1493 | return -EFAULT; |
| 1494 | } |
| 1495 | goto process_func; |
| 1496 | } |
| 1497 | /* end of for() loop means the last insn of the 'subprog' |
| 1498 | * was reached. Doesn't matter whether it was JA or EXIT |
| 1499 | */ |
| 1500 | if (frame == 0) |
| 1501 | return 0; |
| 1502 | depth -= round_up(max_t(u32, env->subprog_stack_depth[subprog], 1), 32); |
| 1503 | frame--; |
| 1504 | i = ret_insn[frame]; |
| 1505 | subprog = ret_prog[frame]; |
| 1506 | goto continue_func; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1507 | } |
| 1508 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 1509 | static int get_callee_stack_depth(struct bpf_verifier_env *env, |
| 1510 | const struct bpf_insn *insn, int idx) |
| 1511 | { |
| 1512 | int start = idx + insn->imm + 1, subprog; |
| 1513 | |
| 1514 | subprog = find_subprog(env, start); |
| 1515 | if (subprog < 0) { |
| 1516 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 1517 | start); |
| 1518 | return -EFAULT; |
| 1519 | } |
| 1520 | subprog++; |
| 1521 | return env->subprog_stack_depth[subprog]; |
| 1522 | } |
| 1523 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1524 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 1525 | * if t==write, value_regno is a register which value is stored into memory |
| 1526 | * if t==read, value_regno is a register which will receive the value from memory |
| 1527 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 1528 | * if t==read && value_regno==-1, don't care what we read from memory |
| 1529 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1530 | 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] | 1531 | int bpf_size, enum bpf_access_type t, |
| 1532 | int value_regno) |
| 1533 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1534 | struct bpf_reg_state *regs = cur_regs(env); |
| 1535 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1536 | struct bpf_func_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1537 | int size, err = 0; |
| 1538 | |
| 1539 | size = bpf_size_to_bytes(bpf_size); |
| 1540 | if (size < 0) |
| 1541 | return size; |
| 1542 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1543 | /* alignment checks will add in reg->off themselves */ |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1544 | err = check_ptr_alignment(env, reg, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1545 | if (err) |
| 1546 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1547 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1548 | /* for access checks, reg->off is just part of off */ |
| 1549 | off += reg->off; |
| 1550 | |
| 1551 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1552 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1553 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1554 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1555 | return -EACCES; |
| 1556 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1557 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1558 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1559 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1560 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1561 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1562 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1563 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1564 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1565 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1566 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1567 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1568 | return -EACCES; |
| 1569 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1570 | /* ctx accesses must be at a fixed offset, so that we can |
| 1571 | * determine what type of data were returned. |
| 1572 | */ |
Jakub Kicinski | 28e33f9 | 2017-10-16 11:16:55 -0700 | [diff] [blame] | 1573 | if (reg->off) { |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 1574 | verbose(env, |
| 1575 | "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] | 1576 | regno, reg->off, off - reg->off); |
| 1577 | return -EACCES; |
| 1578 | } |
| 1579 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1580 | char tn_buf[48]; |
| 1581 | |
| 1582 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1583 | verbose(env, |
| 1584 | "variable ctx access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1585 | tn_buf, off, size); |
| 1586 | return -EACCES; |
| 1587 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1588 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1589 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1590 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1591 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 1592 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1593 | */ |
| 1594 | if (reg_type == SCALAR_VALUE) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1595 | mark_reg_unknown(env, regs, value_regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1596 | else |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1597 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1598 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1599 | regs[value_regno].id = 0; |
| 1600 | regs[value_regno].off = 0; |
| 1601 | regs[value_regno].range = 0; |
| 1602 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1603 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1604 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1605 | } else if (reg->type == PTR_TO_STACK) { |
| 1606 | /* stack accesses must be at a fixed offset, so that we can |
| 1607 | * determine what type of data were returned. |
| 1608 | * See check_stack_read(). |
| 1609 | */ |
| 1610 | if (!tnum_is_const(reg->var_off)) { |
| 1611 | char tn_buf[48]; |
| 1612 | |
| 1613 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1614 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1615 | tn_buf, off, size); |
| 1616 | return -EACCES; |
| 1617 | } |
| 1618 | off += reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1619 | if (off >= 0 || off < -MAX_BPF_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1620 | verbose(env, "invalid stack off=%d size=%d\n", off, |
| 1621 | size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1622 | return -EACCES; |
| 1623 | } |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1624 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1625 | state = func(env, reg); |
| 1626 | err = update_stack_depth(env, state, off); |
| 1627 | if (err) |
| 1628 | return err; |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1629 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1630 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1631 | err = check_stack_write(env, state, off, size, |
| 1632 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1633 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1634 | err = check_stack_read(env, state, off, size, |
| 1635 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1636 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1637 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1638 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1639 | return -EACCES; |
| 1640 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1641 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1642 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1643 | verbose(env, "R%d leaks addr into packet\n", |
| 1644 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1645 | return -EACCES; |
| 1646 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1647 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1648 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1649 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1650 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1651 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 1652 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1653 | return -EACCES; |
| 1654 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1655 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1656 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1657 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1658 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1659 | regs[value_regno].var_off = |
| 1660 | tnum_cast(regs[value_regno].var_off, size); |
| 1661 | __update_reg_bounds(®s[value_regno]); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1662 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1663 | return err; |
| 1664 | } |
| 1665 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1666 | 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] | 1667 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1668 | int err; |
| 1669 | |
| 1670 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 1671 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1672 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1673 | return -EINVAL; |
| 1674 | } |
| 1675 | |
| 1676 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1677 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1678 | if (err) |
| 1679 | return err; |
| 1680 | |
| 1681 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1682 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1683 | if (err) |
| 1684 | return err; |
| 1685 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1686 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1687 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1688 | return -EACCES; |
| 1689 | } |
| 1690 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1691 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1692 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1693 | BPF_SIZE(insn->code), BPF_READ, -1); |
| 1694 | if (err) |
| 1695 | return err; |
| 1696 | |
| 1697 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1698 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1699 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
| 1700 | } |
| 1701 | |
| 1702 | /* when register 'regno' is passed into function that will read 'access_size' |
| 1703 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1704 | * and all elements of stack are initialized. |
| 1705 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 1706 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1707 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1708 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1709 | int access_size, bool zero_size_allowed, |
| 1710 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1711 | { |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1712 | struct bpf_reg_state *reg = cur_regs(env) + regno; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1713 | struct bpf_func_state *state = func(env, reg); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1714 | int off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1715 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1716 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1717 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1718 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1719 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1720 | return 0; |
| 1721 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1722 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1723 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1724 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1725 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1726 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1727 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1728 | /* Only allow fixed-offset stack reads */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1729 | if (!tnum_is_const(reg->var_off)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1730 | char tn_buf[48]; |
| 1731 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1732 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1733 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1734 | regno, tn_buf); |
| 1735 | } |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1736 | off = reg->off + reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1737 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1738 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1739 | 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] | 1740 | regno, off, access_size); |
| 1741 | return -EACCES; |
| 1742 | } |
| 1743 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1744 | if (meta && meta->raw_mode) { |
| 1745 | meta->access_size = access_size; |
| 1746 | meta->regno = regno; |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1750 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1751 | u8 *stype; |
| 1752 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1753 | slot = -(off + i) - 1; |
| 1754 | spi = slot / BPF_REG_SIZE; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1755 | if (state->allocated_stack <= slot) |
| 1756 | goto err; |
| 1757 | stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 1758 | if (*stype == STACK_MISC) |
| 1759 | goto mark; |
| 1760 | if (*stype == STACK_ZERO) { |
| 1761 | /* helper can write anything into the stack */ |
| 1762 | *stype = STACK_MISC; |
| 1763 | goto mark; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1764 | } |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 1765 | err: |
| 1766 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 1767 | off, i, access_size); |
| 1768 | return -EACCES; |
| 1769 | mark: |
| 1770 | /* reading any byte out of 8-byte 'spill_slot' will cause |
| 1771 | * the whole slot to be marked as 'read' |
| 1772 | */ |
| 1773 | mark_stack_slot_read(env, env->cur_state, env->cur_state->parent, |
| 1774 | spi, state->frameno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1775 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 1776 | return update_stack_depth(env, state, off); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1779 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 1780 | int access_size, bool zero_size_allowed, |
| 1781 | struct bpf_call_arg_meta *meta) |
| 1782 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1783 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1784 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1785 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1786 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1787 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1788 | return check_packet_access(env, regno, reg->off, access_size, |
| 1789 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1790 | case PTR_TO_MAP_VALUE: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1791 | return check_map_access(env, regno, reg->off, access_size, |
| 1792 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1793 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1794 | return check_stack_boundary(env, regno, access_size, |
| 1795 | zero_size_allowed, meta); |
| 1796 | } |
| 1797 | } |
| 1798 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1799 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1800 | enum bpf_arg_type arg_type, |
| 1801 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1802 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1803 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1804 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1805 | int err = 0; |
| 1806 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1807 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1808 | return 0; |
| 1809 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1810 | err = check_reg_arg(env, regno, SRC_OP); |
| 1811 | if (err) |
| 1812 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1813 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1814 | if (arg_type == ARG_ANYTHING) { |
| 1815 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1816 | verbose(env, "R%d leaks addr into helper function\n", |
| 1817 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1818 | return -EACCES; |
| 1819 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1820 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1821 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1822 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1823 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1824 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1825 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1826 | return -EACCES; |
| 1827 | } |
| 1828 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1829 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1830 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1831 | expected_type = PTR_TO_STACK; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1832 | if (!type_is_pkt_pointer(type) && |
| 1833 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1834 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1835 | } else if (arg_type == ARG_CONST_SIZE || |
| 1836 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1837 | expected_type = SCALAR_VALUE; |
| 1838 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1839 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1840 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 1841 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1842 | if (type != expected_type) |
| 1843 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1844 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 1845 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1846 | if (type != expected_type) |
| 1847 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1848 | } else if (arg_type == ARG_PTR_TO_MEM || |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 1849 | arg_type == ARG_PTR_TO_MEM_OR_NULL || |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1850 | arg_type == ARG_PTR_TO_UNINIT_MEM) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1851 | expected_type = PTR_TO_STACK; |
| 1852 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1853 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1854 | * happens during stack boundary checking. |
| 1855 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1856 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 1857 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1858 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1859 | else if (!type_is_pkt_pointer(type) && |
| 1860 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1861 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1862 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1863 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1864 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1865 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1866 | return -EFAULT; |
| 1867 | } |
| 1868 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1869 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 1870 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1871 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1872 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 1873 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 1874 | * check that [key, key + map->key_size) are within |
| 1875 | * stack limits and initialized |
| 1876 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1877 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1878 | /* in function declaration map_ptr must come before |
| 1879 | * map_key, so that it's verified and known before |
| 1880 | * we have to check map_key here. Otherwise it means |
| 1881 | * that kernel subsystem misconfigured verifier |
| 1882 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1883 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1884 | return -EACCES; |
| 1885 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1886 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1887 | err = check_packet_access(env, regno, reg->off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1888 | meta->map_ptr->key_size, |
| 1889 | false); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1890 | else |
| 1891 | err = check_stack_boundary(env, regno, |
| 1892 | meta->map_ptr->key_size, |
| 1893 | false, NULL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1894 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1895 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 1896 | * check [value, value + map->value_size) validity |
| 1897 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1898 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1899 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1900 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1901 | return -EACCES; |
| 1902 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1903 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1904 | err = check_packet_access(env, regno, reg->off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1905 | meta->map_ptr->value_size, |
| 1906 | false); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1907 | else |
| 1908 | err = check_stack_boundary(env, regno, |
| 1909 | meta->map_ptr->value_size, |
| 1910 | false, NULL); |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1911 | } else if (arg_type == ARG_CONST_SIZE || |
| 1912 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
| 1913 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1914 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1915 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
| 1916 | * from stack pointer 'buf'. Check it |
| 1917 | * note: regno == len, regno - 1 == buf |
| 1918 | */ |
| 1919 | if (regno == 0) { |
| 1920 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1921 | verbose(env, |
| 1922 | "ARG_CONST_SIZE cannot be first argument\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1923 | return -EACCES; |
| 1924 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1925 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1926 | /* The register is SCALAR_VALUE; the access check |
| 1927 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1928 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1929 | |
| 1930 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1931 | /* For unprivileged variable accesses, disable raw |
| 1932 | * mode so that the program is required to |
| 1933 | * initialize all the memory that the helper could |
| 1934 | * just partially fill up. |
| 1935 | */ |
| 1936 | meta = NULL; |
| 1937 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1938 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1939 | 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] | 1940 | regno); |
| 1941 | return -EACCES; |
| 1942 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1943 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1944 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1945 | err = check_helper_mem_access(env, regno - 1, 0, |
| 1946 | zero_size_allowed, |
| 1947 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1948 | if (err) |
| 1949 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1950 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1951 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1952 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1953 | 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] | 1954 | regno); |
| 1955 | return -EACCES; |
| 1956 | } |
| 1957 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1958 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1959 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1963 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1964 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1965 | reg_type_str[type], reg_type_str[expected_type]); |
| 1966 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1969 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 1970 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1971 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1972 | if (!map) |
| 1973 | return 0; |
| 1974 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1975 | /* We need a two way check, first is from map perspective ... */ |
| 1976 | switch (map->map_type) { |
| 1977 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 1978 | if (func_id != BPF_FUNC_tail_call) |
| 1979 | goto error; |
| 1980 | break; |
| 1981 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 1982 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 1983 | func_id != BPF_FUNC_perf_event_output && |
| 1984 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1985 | goto error; |
| 1986 | break; |
| 1987 | case BPF_MAP_TYPE_STACK_TRACE: |
| 1988 | if (func_id != BPF_FUNC_get_stackid) |
| 1989 | goto error; |
| 1990 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 1991 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 1992 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 1993 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1994 | goto error; |
| 1995 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 1996 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 1997 | * allow to be modified from bpf side. So do not allow lookup elements |
| 1998 | * for now. |
| 1999 | */ |
| 2000 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 2001 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 2002 | goto error; |
| 2003 | break; |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 2004 | /* Restrict bpf side of cpumap, open when use-cases appear */ |
| 2005 | case BPF_MAP_TYPE_CPUMAP: |
| 2006 | if (func_id != BPF_FUNC_redirect_map) |
| 2007 | goto error; |
| 2008 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2009 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 2010 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2011 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 2012 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 2013 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2014 | case BPF_MAP_TYPE_SOCKMAP: |
| 2015 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 2016 | func_id != BPF_FUNC_sock_map_update && |
| 2017 | func_id != BPF_FUNC_map_delete_elem) |
| 2018 | goto error; |
| 2019 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2020 | default: |
| 2021 | break; |
| 2022 | } |
| 2023 | |
| 2024 | /* ... and second from the function itself. */ |
| 2025 | switch (func_id) { |
| 2026 | case BPF_FUNC_tail_call: |
| 2027 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 2028 | goto error; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2029 | if (env->subprog_cnt) { |
| 2030 | verbose(env, "tail_calls are not allowed in programs with bpf-to-bpf calls\n"); |
| 2031 | return -EINVAL; |
| 2032 | } |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2033 | break; |
| 2034 | case BPF_FUNC_perf_event_read: |
| 2035 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 2036 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2037 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 2038 | goto error; |
| 2039 | break; |
| 2040 | case BPF_FUNC_get_stackid: |
| 2041 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 2042 | goto error; |
| 2043 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 2044 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 2045 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 2046 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 2047 | goto error; |
| 2048 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2049 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 2050 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
| 2051 | map->map_type != BPF_MAP_TYPE_CPUMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 2052 | goto error; |
| 2053 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 2054 | case BPF_FUNC_sk_redirect_map: |
| 2055 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2056 | goto error; |
| 2057 | break; |
| 2058 | case BPF_FUNC_sock_map_update: |
| 2059 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 2060 | goto error; |
| 2061 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2062 | default: |
| 2063 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2067 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2068 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2069 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 2070 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2073 | static int check_raw_mode(const struct bpf_func_proto *fn) |
| 2074 | { |
| 2075 | int count = 0; |
| 2076 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2077 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2078 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2079 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2080 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2081 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2082 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2083 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2084 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 2085 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2086 | count++; |
| 2087 | |
| 2088 | return count > 1 ? -EINVAL : 0; |
| 2089 | } |
| 2090 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2091 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 2092 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2093 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2094 | static void __clear_all_pkt_pointers(struct bpf_verifier_env *env, |
| 2095 | struct bpf_func_state *state) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2096 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2097 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2098 | int i; |
| 2099 | |
| 2100 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2101 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2102 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2103 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2104 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 2105 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2106 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2107 | reg = &state->stack[i].spilled_ptr; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2108 | if (reg_is_pkt_pointer_any(reg)) |
| 2109 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2113 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
| 2114 | { |
| 2115 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2116 | int i; |
| 2117 | |
| 2118 | for (i = 0; i <= vstate->curframe; i++) |
| 2119 | __clear_all_pkt_pointers(env, vstate->frame[i]); |
| 2120 | } |
| 2121 | |
| 2122 | static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, |
| 2123 | int *insn_idx) |
| 2124 | { |
| 2125 | struct bpf_verifier_state *state = env->cur_state; |
| 2126 | struct bpf_func_state *caller, *callee; |
| 2127 | int i, subprog, target_insn; |
| 2128 | |
| 2129 | if (state->curframe >= MAX_CALL_FRAMES) { |
| 2130 | verbose(env, "the call stack of %d frames is too deep\n", |
| 2131 | state->curframe); |
| 2132 | return -E2BIG; |
| 2133 | } |
| 2134 | |
| 2135 | target_insn = *insn_idx + insn->imm; |
| 2136 | subprog = find_subprog(env, target_insn + 1); |
| 2137 | if (subprog < 0) { |
| 2138 | verbose(env, "verifier bug. No program starts at insn %d\n", |
| 2139 | target_insn + 1); |
| 2140 | return -EFAULT; |
| 2141 | } |
| 2142 | |
| 2143 | caller = state->frame[state->curframe]; |
| 2144 | if (state->frame[state->curframe + 1]) { |
| 2145 | verbose(env, "verifier bug. Frame %d already allocated\n", |
| 2146 | state->curframe + 1); |
| 2147 | return -EFAULT; |
| 2148 | } |
| 2149 | |
| 2150 | callee = kzalloc(sizeof(*callee), GFP_KERNEL); |
| 2151 | if (!callee) |
| 2152 | return -ENOMEM; |
| 2153 | state->frame[state->curframe + 1] = callee; |
| 2154 | |
| 2155 | /* callee cannot access r0, r6 - r9 for reading and has to write |
| 2156 | * into its own stack before reading from it. |
| 2157 | * callee can read/write into caller's stack |
| 2158 | */ |
| 2159 | init_func_state(env, callee, |
| 2160 | /* remember the callsite, it will be used by bpf_exit */ |
| 2161 | *insn_idx /* callsite */, |
| 2162 | state->curframe + 1 /* frameno within this callchain */, |
| 2163 | subprog + 1 /* subprog number within this prog */); |
| 2164 | |
| 2165 | /* copy r1 - r5 args that callee can access */ |
| 2166 | for (i = BPF_REG_1; i <= BPF_REG_5; i++) |
| 2167 | callee->regs[i] = caller->regs[i]; |
| 2168 | |
| 2169 | /* after the call regsiters r0 - r5 were scratched */ |
| 2170 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 2171 | mark_reg_not_init(env, caller->regs, caller_saved[i]); |
| 2172 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2173 | } |
| 2174 | |
| 2175 | /* only increment it after check_reg_arg() finished */ |
| 2176 | state->curframe++; |
| 2177 | |
| 2178 | /* and go analyze first insn of the callee */ |
| 2179 | *insn_idx = target_insn; |
| 2180 | |
| 2181 | if (env->log.level) { |
| 2182 | verbose(env, "caller:\n"); |
| 2183 | print_verifier_state(env, caller); |
| 2184 | verbose(env, "callee:\n"); |
| 2185 | print_verifier_state(env, callee); |
| 2186 | } |
| 2187 | return 0; |
| 2188 | } |
| 2189 | |
| 2190 | static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) |
| 2191 | { |
| 2192 | struct bpf_verifier_state *state = env->cur_state; |
| 2193 | struct bpf_func_state *caller, *callee; |
| 2194 | struct bpf_reg_state *r0; |
| 2195 | |
| 2196 | callee = state->frame[state->curframe]; |
| 2197 | r0 = &callee->regs[BPF_REG_0]; |
| 2198 | if (r0->type == PTR_TO_STACK) { |
| 2199 | /* technically it's ok to return caller's stack pointer |
| 2200 | * (or caller's caller's pointer) back to the caller, |
| 2201 | * since these pointers are valid. Only current stack |
| 2202 | * pointer will be invalid as soon as function exits, |
| 2203 | * but let's be conservative |
| 2204 | */ |
| 2205 | verbose(env, "cannot return stack pointer to the caller\n"); |
| 2206 | return -EINVAL; |
| 2207 | } |
| 2208 | |
| 2209 | state->curframe--; |
| 2210 | caller = state->frame[state->curframe]; |
| 2211 | /* return to the caller whatever r0 had in the callee */ |
| 2212 | caller->regs[BPF_REG_0] = *r0; |
| 2213 | |
| 2214 | *insn_idx = callee->callsite + 1; |
| 2215 | if (env->log.level) { |
| 2216 | verbose(env, "returning from callee:\n"); |
| 2217 | print_verifier_state(env, callee); |
| 2218 | verbose(env, "to caller at %d:\n", *insn_idx); |
| 2219 | print_verifier_state(env, caller); |
| 2220 | } |
| 2221 | /* clear everything in the callee */ |
| 2222 | free_func_state(callee); |
| 2223 | state->frame[state->curframe + 1] = NULL; |
| 2224 | return 0; |
| 2225 | } |
| 2226 | |
| 2227 | 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] | 2228 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2229 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2230 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2231 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2232 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2233 | int i, err; |
| 2234 | |
| 2235 | /* find function prototype */ |
| 2236 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2237 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 2238 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2239 | return -EINVAL; |
| 2240 | } |
| 2241 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 2242 | if (env->ops->get_func_proto) |
| 2243 | fn = env->ops->get_func_proto(func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2244 | |
| 2245 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2246 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 2247 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2248 | return -EINVAL; |
| 2249 | } |
| 2250 | |
| 2251 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 2252 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2253 | verbose(env, "cannot call GPL only function from proprietary program\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2254 | return -EINVAL; |
| 2255 | } |
| 2256 | |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 2257 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2258 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2259 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 2260 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2261 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2262 | /* We only support one arg being in raw mode at the moment, which |
| 2263 | * is sufficient for the helper functions we have right now. |
| 2264 | */ |
| 2265 | err = check_raw_mode(fn); |
| 2266 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2267 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2268 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2269 | return err; |
| 2270 | } |
| 2271 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2272 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2273 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2274 | if (err) |
| 2275 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2276 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2277 | if (err) |
| 2278 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2279 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2280 | if (err) |
| 2281 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2282 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2283 | if (err) |
| 2284 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2285 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2286 | if (err) |
| 2287 | return err; |
| 2288 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 2289 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 2290 | * is inferred from register state. |
| 2291 | */ |
| 2292 | for (i = 0; i < meta.access_size; i++) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 2293 | 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] | 2294 | if (err) |
| 2295 | return err; |
| 2296 | } |
| 2297 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2298 | regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2299 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2300 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2301 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2302 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 2303 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2304 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2305 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2306 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2307 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2308 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2309 | } else if (fn->ret_type == RET_VOID) { |
| 2310 | regs[BPF_REG_0].type = NOT_INIT; |
| 2311 | } 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] | 2312 | struct bpf_insn_aux_data *insn_aux; |
| 2313 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2314 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2315 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2316 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2317 | regs[BPF_REG_0].off = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2318 | /* remember map_ptr, so that check_map_access() |
| 2319 | * can check 'value_size' boundary of memory access |
| 2320 | * to map element returned from bpf_map_lookup_elem() |
| 2321 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2322 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2323 | verbose(env, |
| 2324 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2325 | return -EINVAL; |
| 2326 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 2327 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2328 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 2329 | insn_aux = &env->insn_aux_data[insn_idx]; |
| 2330 | if (!insn_aux->map_ptr) |
| 2331 | insn_aux->map_ptr = meta.map_ptr; |
| 2332 | else if (insn_aux->map_ptr != meta.map_ptr) |
| 2333 | insn_aux->map_ptr = BPF_MAP_PTR_POISON; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2334 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2335 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 2336 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2337 | return -EINVAL; |
| 2338 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2339 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2340 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 2341 | if (err) |
| 2342 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 2343 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2344 | if (changes_data) |
| 2345 | clear_all_pkt_pointers(env); |
| 2346 | return 0; |
| 2347 | } |
| 2348 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2349 | static void coerce_reg_to_32(struct bpf_reg_state *reg) |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2350 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2351 | /* clear high 32 bits */ |
| 2352 | reg->var_off = tnum_cast(reg->var_off, 4); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2353 | /* Update bounds */ |
| 2354 | __update_reg_bounds(reg); |
| 2355 | } |
| 2356 | |
| 2357 | static bool signed_add_overflows(s64 a, s64 b) |
| 2358 | { |
| 2359 | /* Do the add in u64, where overflow is well-defined */ |
| 2360 | s64 res = (s64)((u64)a + (u64)b); |
| 2361 | |
| 2362 | if (b < 0) |
| 2363 | return res > a; |
| 2364 | return res < a; |
| 2365 | } |
| 2366 | |
| 2367 | static bool signed_sub_overflows(s64 a, s64 b) |
| 2368 | { |
| 2369 | /* Do the sub in u64, where overflow is well-defined */ |
| 2370 | s64 res = (s64)((u64)a - (u64)b); |
| 2371 | |
| 2372 | if (b < 0) |
| 2373 | return res < a; |
| 2374 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2375 | } |
| 2376 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2377 | /* 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] | 2378 | * Caller should also handle BPF_MOV case separately. |
| 2379 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 2380 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 2381 | */ |
| 2382 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 2383 | struct bpf_insn *insn, |
| 2384 | const struct bpf_reg_state *ptr_reg, |
| 2385 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2386 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2387 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2388 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2389 | struct bpf_reg_state *regs = state->regs, *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2390 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2391 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 2392 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 2393 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 2394 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2395 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2396 | u32 dst = insn->dst_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2397 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2398 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2399 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2400 | if (WARN_ON_ONCE(known && (smin_val != smax_val))) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2401 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2402 | verbose(env, |
| 2403 | "verifier internal error: known but bad sbounds\n"); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2404 | return -EINVAL; |
| 2405 | } |
| 2406 | if (WARN_ON_ONCE(known && (umin_val != umax_val))) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2407 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2408 | verbose(env, |
| 2409 | "verifier internal error: known but bad ubounds\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2410 | return -EINVAL; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2411 | } |
| 2412 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2413 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 2414 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
| 2415 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2416 | verbose(env, |
| 2417 | "R%d 32-bit pointer arithmetic prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2418 | dst); |
| 2419 | return -EACCES; |
| 2420 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2421 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2422 | if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 2423 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2424 | verbose(env, "R%d pointer arithmetic on PTR_TO_MAP_VALUE_OR_NULL prohibited, null-check it first\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2425 | dst); |
| 2426 | return -EACCES; |
| 2427 | } |
| 2428 | if (ptr_reg->type == CONST_PTR_TO_MAP) { |
| 2429 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2430 | verbose(env, "R%d pointer arithmetic on CONST_PTR_TO_MAP prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2431 | dst); |
| 2432 | return -EACCES; |
| 2433 | } |
| 2434 | if (ptr_reg->type == PTR_TO_PACKET_END) { |
| 2435 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2436 | verbose(env, "R%d pointer arithmetic on PTR_TO_PACKET_END prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2437 | dst); |
| 2438 | return -EACCES; |
| 2439 | } |
| 2440 | |
| 2441 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 2442 | * 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] | 2443 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2444 | dst_reg->type = ptr_reg->type; |
| 2445 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2446 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2447 | switch (opcode) { |
| 2448 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2449 | /* We can take a fixed offset as long as it doesn't overflow |
| 2450 | * the s32 'off' field |
| 2451 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2452 | if (known && (ptr_reg->off + smin_val == |
| 2453 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2454 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2455 | dst_reg->smin_value = smin_ptr; |
| 2456 | dst_reg->smax_value = smax_ptr; |
| 2457 | dst_reg->umin_value = umin_ptr; |
| 2458 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2459 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2460 | dst_reg->off = ptr_reg->off + smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2461 | dst_reg->range = ptr_reg->range; |
| 2462 | break; |
| 2463 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2464 | /* A new variable offset is created. Note that off_reg->off |
| 2465 | * == 0, since it's a scalar. |
| 2466 | * dst_reg gets the pointer type and since some positive |
| 2467 | * integer value was added to the pointer, give it a new 'id' |
| 2468 | * if it's a PTR_TO_PACKET. |
| 2469 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 2470 | * added into the variable offset, and we copy the fixed offset |
| 2471 | * from ptr_reg. |
| 2472 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2473 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 2474 | signed_add_overflows(smax_ptr, smax_val)) { |
| 2475 | dst_reg->smin_value = S64_MIN; |
| 2476 | dst_reg->smax_value = S64_MAX; |
| 2477 | } else { |
| 2478 | dst_reg->smin_value = smin_ptr + smin_val; |
| 2479 | dst_reg->smax_value = smax_ptr + smax_val; |
| 2480 | } |
| 2481 | if (umin_ptr + umin_val < umin_ptr || |
| 2482 | umax_ptr + umax_val < umax_ptr) { |
| 2483 | dst_reg->umin_value = 0; |
| 2484 | dst_reg->umax_value = U64_MAX; |
| 2485 | } else { |
| 2486 | dst_reg->umin_value = umin_ptr + umin_val; |
| 2487 | dst_reg->umax_value = umax_ptr + umax_val; |
| 2488 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2489 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 2490 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2491 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2492 | dst_reg->id = ++env->id_gen; |
| 2493 | /* something was added to pkt_ptr, set range to zero */ |
| 2494 | dst_reg->range = 0; |
| 2495 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2496 | break; |
| 2497 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2498 | if (dst_reg == off_reg) { |
| 2499 | /* scalar -= pointer. Creates an unknown scalar */ |
| 2500 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2501 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2502 | dst); |
| 2503 | return -EACCES; |
| 2504 | } |
| 2505 | /* We don't allow subtraction from FP, because (according to |
| 2506 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 2507 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 2508 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2509 | if (ptr_reg->type == PTR_TO_STACK) { |
| 2510 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2511 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2512 | dst); |
| 2513 | return -EACCES; |
| 2514 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2515 | if (known && (ptr_reg->off - smin_val == |
| 2516 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2517 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2518 | dst_reg->smin_value = smin_ptr; |
| 2519 | dst_reg->smax_value = smax_ptr; |
| 2520 | dst_reg->umin_value = umin_ptr; |
| 2521 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2522 | dst_reg->var_off = ptr_reg->var_off; |
| 2523 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2524 | dst_reg->off = ptr_reg->off - smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2525 | dst_reg->range = ptr_reg->range; |
| 2526 | break; |
| 2527 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2528 | /* A new variable offset is created. If the subtrahend is known |
| 2529 | * nonnegative, then any reg->range we had before is still good. |
| 2530 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2531 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 2532 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 2533 | /* Overflow possible, we know nothing */ |
| 2534 | dst_reg->smin_value = S64_MIN; |
| 2535 | dst_reg->smax_value = S64_MAX; |
| 2536 | } else { |
| 2537 | dst_reg->smin_value = smin_ptr - smax_val; |
| 2538 | dst_reg->smax_value = smax_ptr - smin_val; |
| 2539 | } |
| 2540 | if (umin_ptr < umax_val) { |
| 2541 | /* Overflow possible, we know nothing */ |
| 2542 | dst_reg->umin_value = 0; |
| 2543 | dst_reg->umax_value = U64_MAX; |
| 2544 | } else { |
| 2545 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2546 | dst_reg->umin_value = umin_ptr - umax_val; |
| 2547 | dst_reg->umax_value = umax_ptr - umin_val; |
| 2548 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2549 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 2550 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2551 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2552 | dst_reg->id = ++env->id_gen; |
| 2553 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2554 | if (smin_val < 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2555 | dst_reg->range = 0; |
| 2556 | } |
| 2557 | break; |
| 2558 | case BPF_AND: |
| 2559 | case BPF_OR: |
| 2560 | case BPF_XOR: |
| 2561 | /* bitwise ops on pointers are troublesome, prohibit for now. |
| 2562 | * (However, in principle we could allow some cases, e.g. |
| 2563 | * ptr &= ~3 which would reduce min_value by 3.) |
| 2564 | */ |
| 2565 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2566 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2567 | dst, bpf_alu_string[opcode >> 4]); |
| 2568 | return -EACCES; |
| 2569 | default: |
| 2570 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
| 2571 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2572 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2573 | dst, bpf_alu_string[opcode >> 4]); |
| 2574 | return -EACCES; |
| 2575 | } |
| 2576 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2577 | __update_reg_bounds(dst_reg); |
| 2578 | __reg_deduce_bounds(dst_reg); |
| 2579 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2580 | return 0; |
| 2581 | } |
| 2582 | |
| 2583 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 2584 | struct bpf_insn *insn, |
| 2585 | struct bpf_reg_state *dst_reg, |
| 2586 | struct bpf_reg_state src_reg) |
| 2587 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2588 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2589 | u8 opcode = BPF_OP(insn->code); |
| 2590 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2591 | s64 smin_val, smax_val; |
| 2592 | u64 umin_val, umax_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2593 | |
| 2594 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 2595 | /* 32-bit ALU ops are (32,32)->64 */ |
| 2596 | coerce_reg_to_32(dst_reg); |
| 2597 | coerce_reg_to_32(&src_reg); |
| 2598 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2599 | smin_val = src_reg.smin_value; |
| 2600 | smax_val = src_reg.smax_value; |
| 2601 | umin_val = src_reg.umin_value; |
| 2602 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2603 | src_known = tnum_is_const(src_reg.var_off); |
| 2604 | dst_known = tnum_is_const(dst_reg->var_off); |
| 2605 | |
| 2606 | switch (opcode) { |
| 2607 | case BPF_ADD: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2608 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 2609 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 2610 | dst_reg->smin_value = S64_MIN; |
| 2611 | dst_reg->smax_value = S64_MAX; |
| 2612 | } else { |
| 2613 | dst_reg->smin_value += smin_val; |
| 2614 | dst_reg->smax_value += smax_val; |
| 2615 | } |
| 2616 | if (dst_reg->umin_value + umin_val < umin_val || |
| 2617 | dst_reg->umax_value + umax_val < umax_val) { |
| 2618 | dst_reg->umin_value = 0; |
| 2619 | dst_reg->umax_value = U64_MAX; |
| 2620 | } else { |
| 2621 | dst_reg->umin_value += umin_val; |
| 2622 | dst_reg->umax_value += umax_val; |
| 2623 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2624 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 2625 | break; |
| 2626 | case BPF_SUB: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2627 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 2628 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 2629 | /* Overflow possible, we know nothing */ |
| 2630 | dst_reg->smin_value = S64_MIN; |
| 2631 | dst_reg->smax_value = S64_MAX; |
| 2632 | } else { |
| 2633 | dst_reg->smin_value -= smax_val; |
| 2634 | dst_reg->smax_value -= smin_val; |
| 2635 | } |
| 2636 | if (dst_reg->umin_value < umax_val) { |
| 2637 | /* Overflow possible, we know nothing */ |
| 2638 | dst_reg->umin_value = 0; |
| 2639 | dst_reg->umax_value = U64_MAX; |
| 2640 | } else { |
| 2641 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2642 | dst_reg->umin_value -= umax_val; |
| 2643 | dst_reg->umax_value -= umin_val; |
| 2644 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2645 | 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] | 2646 | break; |
| 2647 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2648 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 2649 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2650 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2651 | __mark_reg_unbounded(dst_reg); |
| 2652 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2653 | break; |
| 2654 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2655 | /* Both values are positive, so we can work with unsigned and |
| 2656 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2657 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2658 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 2659 | /* Potential overflow, we know nothing */ |
| 2660 | __mark_reg_unbounded(dst_reg); |
| 2661 | /* (except what we can learn from the var_off) */ |
| 2662 | __update_reg_bounds(dst_reg); |
| 2663 | break; |
| 2664 | } |
| 2665 | dst_reg->umin_value *= umin_val; |
| 2666 | dst_reg->umax_value *= umax_val; |
| 2667 | if (dst_reg->umax_value > S64_MAX) { |
| 2668 | /* Overflow possible, we know nothing */ |
| 2669 | dst_reg->smin_value = S64_MIN; |
| 2670 | dst_reg->smax_value = S64_MAX; |
| 2671 | } else { |
| 2672 | dst_reg->smin_value = dst_reg->umin_value; |
| 2673 | dst_reg->smax_value = dst_reg->umax_value; |
| 2674 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2675 | break; |
| 2676 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2677 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2678 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 2679 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2680 | break; |
| 2681 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2682 | /* We get our minimum from the var_off, since that's inherently |
| 2683 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2684 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2685 | 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] | 2686 | dst_reg->umin_value = dst_reg->var_off.value; |
| 2687 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 2688 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2689 | /* Lose signed bounds when ANDing negative numbers, |
| 2690 | * ain't nobody got time for that. |
| 2691 | */ |
| 2692 | dst_reg->smin_value = S64_MIN; |
| 2693 | dst_reg->smax_value = S64_MAX; |
| 2694 | } else { |
| 2695 | /* ANDing two positives gives a positive, so safe to |
| 2696 | * cast result into s64. |
| 2697 | */ |
| 2698 | dst_reg->smin_value = dst_reg->umin_value; |
| 2699 | dst_reg->smax_value = dst_reg->umax_value; |
| 2700 | } |
| 2701 | /* We may learn something more from the var_off */ |
| 2702 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2703 | break; |
| 2704 | case BPF_OR: |
| 2705 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2706 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 2707 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2708 | break; |
| 2709 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2710 | /* We get our maximum from the var_off, and our minimum is the |
| 2711 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2712 | */ |
| 2713 | 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] | 2714 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 2715 | dst_reg->umax_value = dst_reg->var_off.value | |
| 2716 | dst_reg->var_off.mask; |
| 2717 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2718 | /* Lose signed bounds when ORing negative numbers, |
| 2719 | * ain't nobody got time for that. |
| 2720 | */ |
| 2721 | dst_reg->smin_value = S64_MIN; |
| 2722 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2723 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2724 | /* ORing two positives gives a positive, so safe to |
| 2725 | * cast result into s64. |
| 2726 | */ |
| 2727 | dst_reg->smin_value = dst_reg->umin_value; |
| 2728 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2729 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2730 | /* We may learn something more from the var_off */ |
| 2731 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2732 | break; |
| 2733 | case BPF_LSH: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2734 | if (umax_val > 63) { |
| 2735 | /* Shifts greater than 63 are undefined. This includes |
| 2736 | * shifts by a negative number. |
| 2737 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2738 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2739 | break; |
| 2740 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2741 | /* We lose all sign bit information (except what we can pick |
| 2742 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2743 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2744 | dst_reg->smin_value = S64_MIN; |
| 2745 | dst_reg->smax_value = S64_MAX; |
| 2746 | /* If we might shift our top bit out, then we know nothing */ |
| 2747 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 2748 | dst_reg->umin_value = 0; |
| 2749 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2750 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2751 | dst_reg->umin_value <<= umin_val; |
| 2752 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2753 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2754 | if (src_known) |
| 2755 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 2756 | else |
| 2757 | dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val); |
| 2758 | /* We may learn something more from the var_off */ |
| 2759 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2760 | break; |
| 2761 | case BPF_RSH: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2762 | if (umax_val > 63) { |
| 2763 | /* Shifts greater than 63 are undefined. This includes |
| 2764 | * shifts by a negative number. |
| 2765 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2766 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2767 | break; |
| 2768 | } |
| 2769 | /* BPF_RSH is an unsigned shift, so make the appropriate casts */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2770 | if (dst_reg->smin_value < 0) { |
| 2771 | if (umin_val) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2772 | /* Sign bit will be cleared */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2773 | dst_reg->smin_value = 0; |
| 2774 | } else { |
| 2775 | /* Lost sign bit information */ |
| 2776 | dst_reg->smin_value = S64_MIN; |
| 2777 | dst_reg->smax_value = S64_MAX; |
| 2778 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2779 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2780 | dst_reg->smin_value = |
| 2781 | (u64)(dst_reg->smin_value) >> umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2782 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2783 | if (src_known) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2784 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, |
| 2785 | umin_val); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2786 | else |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2787 | dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val); |
| 2788 | dst_reg->umin_value >>= umax_val; |
| 2789 | dst_reg->umax_value >>= umin_val; |
| 2790 | /* We may learn something more from the var_off */ |
| 2791 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2792 | break; |
| 2793 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2794 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2795 | break; |
| 2796 | } |
| 2797 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2798 | __reg_deduce_bounds(dst_reg); |
| 2799 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2800 | return 0; |
| 2801 | } |
| 2802 | |
| 2803 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 2804 | * and var_off. |
| 2805 | */ |
| 2806 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 2807 | struct bpf_insn *insn) |
| 2808 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2809 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2810 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2811 | struct bpf_reg_state *regs = state->regs, *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2812 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 2813 | u8 opcode = BPF_OP(insn->code); |
| 2814 | int rc; |
| 2815 | |
| 2816 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2817 | src_reg = NULL; |
| 2818 | if (dst_reg->type != SCALAR_VALUE) |
| 2819 | ptr_reg = dst_reg; |
| 2820 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2821 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2822 | if (src_reg->type != SCALAR_VALUE) { |
| 2823 | if (dst_reg->type != SCALAR_VALUE) { |
| 2824 | /* Combining two pointers by any ALU op yields |
| 2825 | * an arbitrary scalar. |
| 2826 | */ |
| 2827 | if (!env->allow_ptr_leaks) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2828 | verbose(env, "R%d pointer %s pointer prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2829 | insn->dst_reg, |
| 2830 | bpf_alu_string[opcode >> 4]); |
| 2831 | return -EACCES; |
| 2832 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2833 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2834 | return 0; |
| 2835 | } else { |
| 2836 | /* scalar += pointer |
| 2837 | * This is legal, but we have to reverse our |
| 2838 | * src/dest handling in computing the range |
| 2839 | */ |
| 2840 | rc = adjust_ptr_min_max_vals(env, insn, |
| 2841 | src_reg, dst_reg); |
| 2842 | if (rc == -EACCES && env->allow_ptr_leaks) { |
| 2843 | /* scalar += unknown scalar */ |
| 2844 | __mark_reg_unknown(&off_reg); |
| 2845 | return adjust_scalar_min_max_vals( |
| 2846 | env, insn, |
| 2847 | dst_reg, off_reg); |
| 2848 | } |
| 2849 | return rc; |
| 2850 | } |
| 2851 | } else if (ptr_reg) { |
| 2852 | /* pointer += scalar */ |
| 2853 | rc = adjust_ptr_min_max_vals(env, insn, |
| 2854 | dst_reg, src_reg); |
| 2855 | if (rc == -EACCES && env->allow_ptr_leaks) { |
| 2856 | /* unknown scalar += scalar */ |
| 2857 | __mark_reg_unknown(dst_reg); |
| 2858 | return adjust_scalar_min_max_vals( |
| 2859 | env, insn, dst_reg, *src_reg); |
| 2860 | } |
| 2861 | return rc; |
| 2862 | } |
| 2863 | } else { |
| 2864 | /* Pretend the src is a reg with a known value, since we only |
| 2865 | * need to be able to read from this state. |
| 2866 | */ |
| 2867 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2868 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2869 | src_reg = &off_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2870 | if (ptr_reg) { /* pointer += K */ |
| 2871 | rc = adjust_ptr_min_max_vals(env, insn, |
| 2872 | ptr_reg, src_reg); |
| 2873 | if (rc == -EACCES && env->allow_ptr_leaks) { |
| 2874 | /* unknown scalar += K */ |
| 2875 | __mark_reg_unknown(dst_reg); |
| 2876 | return adjust_scalar_min_max_vals( |
| 2877 | env, insn, dst_reg, off_reg); |
| 2878 | } |
| 2879 | return rc; |
| 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | /* Got here implies adding two SCALAR_VALUEs */ |
| 2884 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2885 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2886 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2887 | return -EINVAL; |
| 2888 | } |
| 2889 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 2890 | print_verifier_state(env, state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2891 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2892 | return -EINVAL; |
| 2893 | } |
| 2894 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2895 | } |
| 2896 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2897 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2898 | 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] | 2899 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2900 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2901 | u8 opcode = BPF_OP(insn->code); |
| 2902 | int err; |
| 2903 | |
| 2904 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 2905 | if (opcode == BPF_NEG) { |
| 2906 | if (BPF_SRC(insn->code) != 0 || |
| 2907 | insn->src_reg != BPF_REG_0 || |
| 2908 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2909 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2910 | return -EINVAL; |
| 2911 | } |
| 2912 | } else { |
| 2913 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 2914 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 2915 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2916 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2917 | return -EINVAL; |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2922 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2923 | if (err) |
| 2924 | return err; |
| 2925 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2926 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2927 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2928 | insn->dst_reg); |
| 2929 | return -EACCES; |
| 2930 | } |
| 2931 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2932 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2933 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2934 | if (err) |
| 2935 | return err; |
| 2936 | |
| 2937 | } else if (opcode == BPF_MOV) { |
| 2938 | |
| 2939 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2940 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2941 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2942 | return -EINVAL; |
| 2943 | } |
| 2944 | |
| 2945 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2946 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2947 | if (err) |
| 2948 | return err; |
| 2949 | } else { |
| 2950 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2951 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2952 | return -EINVAL; |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2957 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2958 | if (err) |
| 2959 | return err; |
| 2960 | |
| 2961 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2962 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 2963 | /* case: R1 = R2 |
| 2964 | * copy register state to dest reg |
| 2965 | */ |
| 2966 | regs[insn->dst_reg] = regs[insn->src_reg]; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 2967 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2968 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2969 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2970 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2971 | verbose(env, |
| 2972 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2973 | insn->src_reg); |
| 2974 | return -EACCES; |
| 2975 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2976 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2977 | /* high 32 bits are known zero. */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2978 | regs[insn->dst_reg].var_off = tnum_cast( |
| 2979 | regs[insn->dst_reg].var_off, 4); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2980 | __update_reg_bounds(®s[insn->dst_reg]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2981 | } |
| 2982 | } else { |
| 2983 | /* case: R = imm |
| 2984 | * remember the value we stored into this reg |
| 2985 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2986 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2987 | __mark_reg_known(regs + insn->dst_reg, insn->imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2991 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2992 | return -EINVAL; |
| 2993 | |
| 2994 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 2995 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2996 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2997 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2998 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2999 | return -EINVAL; |
| 3000 | } |
| 3001 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3002 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3003 | if (err) |
| 3004 | return err; |
| 3005 | } else { |
| 3006 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3007 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3008 | return -EINVAL; |
| 3009 | } |
| 3010 | } |
| 3011 | |
| 3012 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3013 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3014 | if (err) |
| 3015 | return err; |
| 3016 | |
| 3017 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 3018 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3019 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3020 | return -EINVAL; |
| 3021 | } |
| 3022 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3023 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 3024 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 3025 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 3026 | |
| 3027 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3028 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 3029 | return -EINVAL; |
| 3030 | } |
| 3031 | } |
| 3032 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3033 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3034 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3035 | if (err) |
| 3036 | return err; |
| 3037 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3038 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | return 0; |
| 3042 | } |
| 3043 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3044 | static void find_good_pkt_pointers(struct bpf_verifier_state *vstate, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3045 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 3046 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3047 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3048 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3049 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3050 | struct bpf_reg_state *regs = state->regs, *reg; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3051 | u16 new_range; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3052 | int i, j; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3053 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3054 | if (dst_reg->off < 0 || |
| 3055 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3056 | /* This doesn't give us any range */ |
| 3057 | return; |
| 3058 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3059 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 3060 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3061 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 3062 | * than pkt_end, but that's because it's also less than pkt. |
| 3063 | */ |
| 3064 | return; |
| 3065 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3066 | new_range = dst_reg->off; |
| 3067 | if (range_right_open) |
| 3068 | new_range--; |
| 3069 | |
| 3070 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3071 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3072 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3073 | * |
| 3074 | * r2 = r3; |
| 3075 | * r2 += 8; |
| 3076 | * if (r2 > pkt_end) goto <handle exception> |
| 3077 | * <access okay> |
| 3078 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3079 | * r2 = r3; |
| 3080 | * r2 += 8; |
| 3081 | * if (r2 < pkt_end) goto <access okay> |
| 3082 | * <handle exception> |
| 3083 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3084 | * Where: |
| 3085 | * r2 == dst_reg, pkt_end == src_reg |
| 3086 | * r2=pkt(id=n,off=8,r=0) |
| 3087 | * r3=pkt(id=n,off=0,r=0) |
| 3088 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3089 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3090 | * |
| 3091 | * r2 = r3; |
| 3092 | * r2 += 8; |
| 3093 | * if (pkt_end >= r2) goto <access okay> |
| 3094 | * <handle exception> |
| 3095 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3096 | * r2 = r3; |
| 3097 | * r2 += 8; |
| 3098 | * if (pkt_end <= r2) goto <handle exception> |
| 3099 | * <access okay> |
| 3100 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3101 | * Where: |
| 3102 | * pkt_end == dst_reg, r2 == src_reg |
| 3103 | * r2=pkt(id=n,off=8,r=0) |
| 3104 | * r3=pkt(id=n,off=0,r=0) |
| 3105 | * |
| 3106 | * 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] | 3107 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 3108 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 3109 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3110 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 3111 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3112 | /* If our ids match, then we must have the same max_value. And we |
| 3113 | * don't care about the other reg's fixed offset, since if it's too big |
| 3114 | * the range won't allow anything. |
| 3115 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 3116 | */ |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3117 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3118 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
Alexei Starovoitov | b197768 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 3119 | /* keep the maximum range already checked */ |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 3120 | regs[i].range = max(regs[i].range, new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3121 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3122 | for (j = 0; j <= vstate->curframe; j++) { |
| 3123 | state = vstate->frame[j]; |
| 3124 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 3125 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 3126 | continue; |
| 3127 | reg = &state->stack[i].spilled_ptr; |
| 3128 | if (reg->type == type && reg->id == dst_reg->id) |
| 3129 | reg->range = max(reg->range, new_range); |
| 3130 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3131 | } |
| 3132 | } |
| 3133 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3134 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 3135 | * variable register that we are working on, and src_reg is a constant or we're |
| 3136 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3137 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3138 | */ |
| 3139 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 3140 | struct bpf_reg_state *false_reg, u64 val, |
| 3141 | u8 opcode) |
| 3142 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3143 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 3144 | * variable offset from the compare (unless src_reg were a pointer into |
| 3145 | * the same object, but we don't bother with that. |
| 3146 | * Since false_reg and true_reg have the same type by construction, we |
| 3147 | * only need to check one of them for pointerness. |
| 3148 | */ |
| 3149 | if (__is_pointer_value(false, false_reg)) |
| 3150 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3151 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3152 | switch (opcode) { |
| 3153 | case BPF_JEQ: |
| 3154 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3155 | * true then we know for sure. |
| 3156 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3157 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3158 | break; |
| 3159 | case BPF_JNE: |
| 3160 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3161 | * we know the value for sure; |
| 3162 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3163 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3164 | break; |
| 3165 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3166 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3167 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3168 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3169 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3170 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3171 | 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] | 3172 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3173 | case BPF_JLT: |
| 3174 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3175 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3176 | break; |
| 3177 | case BPF_JSLT: |
| 3178 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
| 3179 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3180 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3181 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3182 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3183 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3184 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3185 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3186 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3187 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3188 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3189 | case BPF_JLE: |
| 3190 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3191 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3192 | break; |
| 3193 | case BPF_JSLE: |
| 3194 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
| 3195 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3196 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3197 | default: |
| 3198 | break; |
| 3199 | } |
| 3200 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3201 | __reg_deduce_bounds(false_reg); |
| 3202 | __reg_deduce_bounds(true_reg); |
| 3203 | /* We might have learned some bits from the bounds. */ |
| 3204 | __reg_bound_offset(false_reg); |
| 3205 | __reg_bound_offset(true_reg); |
| 3206 | /* Intersecting with the old var_off might have improved our bounds |
| 3207 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3208 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3209 | */ |
| 3210 | __update_reg_bounds(false_reg); |
| 3211 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3212 | } |
| 3213 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3214 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 3215 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3216 | */ |
| 3217 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 3218 | struct bpf_reg_state *false_reg, u64 val, |
| 3219 | u8 opcode) |
| 3220 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3221 | if (__is_pointer_value(false, false_reg)) |
| 3222 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 3223 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3224 | switch (opcode) { |
| 3225 | case BPF_JEQ: |
| 3226 | /* If this is false then we know nothing Jon Snow, but if it is |
| 3227 | * true then we know for sure. |
| 3228 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3229 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3230 | break; |
| 3231 | case BPF_JNE: |
| 3232 | /* If this is true we know nothing Jon Snow, but if it is false |
| 3233 | * we know the value for sure; |
| 3234 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3235 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3236 | break; |
| 3237 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3238 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 3239 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 3240 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3241 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3242 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 3243 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3244 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3245 | case BPF_JLT: |
| 3246 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 3247 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 3248 | break; |
| 3249 | case BPF_JSLT: |
| 3250 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
| 3251 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 3252 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3253 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3254 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 3255 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 3256 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3257 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3258 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 3259 | 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] | 3260 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3261 | case BPF_JLE: |
| 3262 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 3263 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 3264 | break; |
| 3265 | case BPF_JSLE: |
| 3266 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
| 3267 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 3268 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3269 | default: |
| 3270 | break; |
| 3271 | } |
| 3272 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3273 | __reg_deduce_bounds(false_reg); |
| 3274 | __reg_deduce_bounds(true_reg); |
| 3275 | /* We might have learned some bits from the bounds. */ |
| 3276 | __reg_bound_offset(false_reg); |
| 3277 | __reg_bound_offset(true_reg); |
| 3278 | /* Intersecting with the old var_off might have improved our bounds |
| 3279 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3280 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3281 | */ |
| 3282 | __update_reg_bounds(false_reg); |
| 3283 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 3287 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 3288 | struct bpf_reg_state *dst_reg) |
| 3289 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3290 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 3291 | dst_reg->umin_value); |
| 3292 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 3293 | dst_reg->umax_value); |
| 3294 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 3295 | dst_reg->smin_value); |
| 3296 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 3297 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3298 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 3299 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3300 | /* We might have learned new bounds from the var_off. */ |
| 3301 | __update_reg_bounds(src_reg); |
| 3302 | __update_reg_bounds(dst_reg); |
| 3303 | /* We might have learned something about the sign bit. */ |
| 3304 | __reg_deduce_bounds(src_reg); |
| 3305 | __reg_deduce_bounds(dst_reg); |
| 3306 | /* We might have learned some bits from the bounds. */ |
| 3307 | __reg_bound_offset(src_reg); |
| 3308 | __reg_bound_offset(dst_reg); |
| 3309 | /* Intersecting with the old var_off might have improved our bounds |
| 3310 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 3311 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 3312 | */ |
| 3313 | __update_reg_bounds(src_reg); |
| 3314 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3315 | } |
| 3316 | |
| 3317 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 3318 | struct bpf_reg_state *true_dst, |
| 3319 | struct bpf_reg_state *false_src, |
| 3320 | struct bpf_reg_state *false_dst, |
| 3321 | u8 opcode) |
| 3322 | { |
| 3323 | switch (opcode) { |
| 3324 | case BPF_JEQ: |
| 3325 | __reg_combine_min_max(true_src, true_dst); |
| 3326 | break; |
| 3327 | case BPF_JNE: |
| 3328 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3329 | break; |
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 | } |
| 3332 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3333 | 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] | 3334 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3335 | { |
| 3336 | struct bpf_reg_state *reg = ®s[regno]; |
| 3337 | |
| 3338 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3339 | /* Old offset (both fixed and variable parts) should |
| 3340 | * have been known-zero, because we don't allow pointer |
| 3341 | * arithmetic on pointers that might be NULL. |
| 3342 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3343 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 3344 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3345 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3346 | __mark_reg_known_zero(reg); |
| 3347 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3348 | } |
| 3349 | if (is_null) { |
| 3350 | reg->type = SCALAR_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3351 | } else if (reg->map_ptr->inner_map_meta) { |
| 3352 | reg->type = CONST_PTR_TO_MAP; |
| 3353 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 3354 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3355 | reg->type = PTR_TO_MAP_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 3356 | } |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 3357 | /* We don't need id from this point onwards anymore, thus we |
| 3358 | * should better reset it, so that state pruning has chances |
| 3359 | * to take effect. |
| 3360 | */ |
| 3361 | reg->id = 0; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | |
| 3365 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 3366 | * be folded together at some point. |
| 3367 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3368 | static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3369 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3370 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3371 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3372 | struct bpf_reg_state *regs = state->regs; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 3373 | u32 id = regs[regno].id; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3374 | int i, j; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3375 | |
| 3376 | for (i = 0; i < MAX_BPF_REG; i++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3377 | mark_map_reg(regs, i, id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3378 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3379 | for (j = 0; j <= vstate->curframe; j++) { |
| 3380 | state = vstate->frame[j]; |
| 3381 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 3382 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
| 3383 | continue; |
| 3384 | mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null); |
| 3385 | } |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3386 | } |
| 3387 | } |
| 3388 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 3389 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 3390 | struct bpf_reg_state *dst_reg, |
| 3391 | struct bpf_reg_state *src_reg, |
| 3392 | struct bpf_verifier_state *this_branch, |
| 3393 | struct bpf_verifier_state *other_branch) |
| 3394 | { |
| 3395 | if (BPF_SRC(insn->code) != BPF_X) |
| 3396 | return false; |
| 3397 | |
| 3398 | switch (BPF_OP(insn->code)) { |
| 3399 | case BPF_JGT: |
| 3400 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3401 | src_reg->type == PTR_TO_PACKET_END) || |
| 3402 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3403 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3404 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 3405 | find_good_pkt_pointers(this_branch, dst_reg, |
| 3406 | dst_reg->type, false); |
| 3407 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3408 | src_reg->type == PTR_TO_PACKET) || |
| 3409 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3410 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3411 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 3412 | find_good_pkt_pointers(other_branch, src_reg, |
| 3413 | src_reg->type, true); |
| 3414 | } else { |
| 3415 | return false; |
| 3416 | } |
| 3417 | break; |
| 3418 | case BPF_JLT: |
| 3419 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3420 | src_reg->type == PTR_TO_PACKET_END) || |
| 3421 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3422 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3423 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 3424 | find_good_pkt_pointers(other_branch, dst_reg, |
| 3425 | dst_reg->type, true); |
| 3426 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3427 | src_reg->type == PTR_TO_PACKET) || |
| 3428 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3429 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3430 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 3431 | find_good_pkt_pointers(this_branch, src_reg, |
| 3432 | src_reg->type, false); |
| 3433 | } else { |
| 3434 | return false; |
| 3435 | } |
| 3436 | break; |
| 3437 | case BPF_JGE: |
| 3438 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3439 | src_reg->type == PTR_TO_PACKET_END) || |
| 3440 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3441 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3442 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 3443 | find_good_pkt_pointers(this_branch, dst_reg, |
| 3444 | dst_reg->type, true); |
| 3445 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3446 | src_reg->type == PTR_TO_PACKET) || |
| 3447 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3448 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3449 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 3450 | find_good_pkt_pointers(other_branch, src_reg, |
| 3451 | src_reg->type, false); |
| 3452 | } else { |
| 3453 | return false; |
| 3454 | } |
| 3455 | break; |
| 3456 | case BPF_JLE: |
| 3457 | if ((dst_reg->type == PTR_TO_PACKET && |
| 3458 | src_reg->type == PTR_TO_PACKET_END) || |
| 3459 | (dst_reg->type == PTR_TO_PACKET_META && |
| 3460 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 3461 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 3462 | find_good_pkt_pointers(other_branch, dst_reg, |
| 3463 | dst_reg->type, false); |
| 3464 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 3465 | src_reg->type == PTR_TO_PACKET) || |
| 3466 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 3467 | src_reg->type == PTR_TO_PACKET_META)) { |
| 3468 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 3469 | find_good_pkt_pointers(this_branch, src_reg, |
| 3470 | src_reg->type, true); |
| 3471 | } else { |
| 3472 | return false; |
| 3473 | } |
| 3474 | break; |
| 3475 | default: |
| 3476 | return false; |
| 3477 | } |
| 3478 | |
| 3479 | return true; |
| 3480 | } |
| 3481 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3482 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3483 | struct bpf_insn *insn, int *insn_idx) |
| 3484 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3485 | struct bpf_verifier_state *this_branch = env->cur_state; |
| 3486 | struct bpf_verifier_state *other_branch; |
| 3487 | struct bpf_reg_state *regs = this_branch->frame[this_branch->curframe]->regs; |
| 3488 | struct bpf_reg_state *dst_reg, *other_branch_regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3489 | u8 opcode = BPF_OP(insn->code); |
| 3490 | int err; |
| 3491 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 3492 | if (opcode > BPF_JSLE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3493 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3494 | return -EINVAL; |
| 3495 | } |
| 3496 | |
| 3497 | if (BPF_SRC(insn->code) == BPF_X) { |
| 3498 | if (insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3499 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3500 | return -EINVAL; |
| 3501 | } |
| 3502 | |
| 3503 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3504 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3505 | if (err) |
| 3506 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3507 | |
| 3508 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3509 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3510 | insn->src_reg); |
| 3511 | return -EACCES; |
| 3512 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3513 | } else { |
| 3514 | if (insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3515 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3516 | return -EINVAL; |
| 3517 | } |
| 3518 | } |
| 3519 | |
| 3520 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3521 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3522 | if (err) |
| 3523 | return err; |
| 3524 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3525 | dst_reg = ®s[insn->dst_reg]; |
| 3526 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3527 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 3528 | if (BPF_SRC(insn->code) == BPF_K && |
| 3529 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3530 | dst_reg->type == SCALAR_VALUE && |
Alexei Starovoitov | 3bf1592 | 2017-11-30 21:31:39 -0800 | [diff] [blame] | 3531 | tnum_is_const(dst_reg->var_off)) { |
| 3532 | if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) || |
| 3533 | (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3534 | /* if (imm == imm) goto pc+off; |
| 3535 | * only follow the goto, ignore fall-through |
| 3536 | */ |
| 3537 | *insn_idx += insn->off; |
| 3538 | return 0; |
| 3539 | } else { |
| 3540 | /* if (imm != imm) goto pc+off; |
| 3541 | * only follow fall-through branch, since |
| 3542 | * that's where the program will go |
| 3543 | */ |
| 3544 | return 0; |
| 3545 | } |
| 3546 | } |
| 3547 | |
| 3548 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 3549 | if (!other_branch) |
| 3550 | return -EFAULT; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3551 | other_branch_regs = other_branch->frame[other_branch->curframe]->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3552 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3553 | /* detect if we are comparing against a constant value so we can adjust |
| 3554 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3555 | * this is only legit if both are scalars (or pointers to the same |
| 3556 | * object, I suppose, but we don't support that right now), because |
| 3557 | * otherwise the different base pointers mean the offsets aren't |
| 3558 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3559 | */ |
| 3560 | if (BPF_SRC(insn->code) == BPF_X) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3561 | if (dst_reg->type == SCALAR_VALUE && |
| 3562 | regs[insn->src_reg].type == SCALAR_VALUE) { |
| 3563 | if (tnum_is_const(regs[insn->src_reg].var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3564 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3565 | dst_reg, regs[insn->src_reg].var_off.value, |
| 3566 | opcode); |
| 3567 | else if (tnum_is_const(dst_reg->var_off)) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3568 | reg_set_min_max_inv(&other_branch_regs[insn->src_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3569 | ®s[insn->src_reg], |
| 3570 | dst_reg->var_off.value, opcode); |
| 3571 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) |
| 3572 | /* Comparing for equality, we can combine knowledge */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3573 | reg_combine_min_max(&other_branch_regs[insn->src_reg], |
| 3574 | &other_branch_regs[insn->dst_reg], |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3575 | ®s[insn->src_reg], |
| 3576 | ®s[insn->dst_reg], opcode); |
| 3577 | } |
| 3578 | } else if (dst_reg->type == SCALAR_VALUE) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3579 | reg_set_min_max(&other_branch_regs[insn->dst_reg], |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3580 | dst_reg, insn->imm, opcode); |
| 3581 | } |
| 3582 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3583 | /* 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] | 3584 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3585 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 3586 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3587 | /* Mark all identical map registers in each branch as either |
| 3588 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 3589 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3590 | mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE); |
| 3591 | mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 3592 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 3593 | this_branch, other_branch) && |
| 3594 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3595 | verbose(env, "R%d pointer comparison prohibited\n", |
| 3596 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3597 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3598 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3599 | if (env->log.level) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3600 | print_verifier_state(env, this_branch->frame[this_branch->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3601 | return 0; |
| 3602 | } |
| 3603 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3604 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 3605 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 3606 | { |
| 3607 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 3608 | |
| 3609 | return (struct bpf_map *) (unsigned long) imm64; |
| 3610 | } |
| 3611 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3612 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3613 | 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] | 3614 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3615 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3616 | int err; |
| 3617 | |
| 3618 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3619 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3620 | return -EINVAL; |
| 3621 | } |
| 3622 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3623 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3624 | return -EINVAL; |
| 3625 | } |
| 3626 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3627 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3628 | if (err) |
| 3629 | return err; |
| 3630 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3631 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3632 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 3633 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3634 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3635 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3636 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3637 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3638 | |
| 3639 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 3640 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 3641 | |
| 3642 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 3643 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 3644 | return 0; |
| 3645 | } |
| 3646 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3647 | static bool may_access_skb(enum bpf_prog_type type) |
| 3648 | { |
| 3649 | switch (type) { |
| 3650 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 3651 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 3652 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3653 | return true; |
| 3654 | default: |
| 3655 | return false; |
| 3656 | } |
| 3657 | } |
| 3658 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3659 | /* verify safety of LD_ABS|LD_IND instructions: |
| 3660 | * - they can only appear in the programs where ctx == skb |
| 3661 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 3662 | * preserve R6-R9, and store return value into R0 |
| 3663 | * |
| 3664 | * Implicit input: |
| 3665 | * ctx == skb == R6 == CTX |
| 3666 | * |
| 3667 | * Explicit input: |
| 3668 | * SRC == any register |
| 3669 | * IMM == 32-bit immediate |
| 3670 | * |
| 3671 | * Output: |
| 3672 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 3673 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3674 | 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] | 3675 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3676 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3677 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3678 | int i, err; |
| 3679 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 3680 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3681 | 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] | 3682 | return -EINVAL; |
| 3683 | } |
| 3684 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 3685 | if (env->subprog_cnt) { |
| 3686 | /* when program has LD_ABS insn JITs and interpreter assume |
| 3687 | * that r1 == ctx == skb which is not the case for callees |
| 3688 | * that can have arbitrary arguments. It's problematic |
| 3689 | * for main prog as well since JITs would need to analyze |
| 3690 | * all functions in order to make proper register save/restore |
| 3691 | * decisions in the main prog. Hence disallow LD_ABS with calls |
| 3692 | */ |
| 3693 | verbose(env, "BPF_LD_[ABS|IND] instructions cannot be mixed with bpf-to-bpf calls\n"); |
| 3694 | return -EINVAL; |
| 3695 | } |
| 3696 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3697 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 3698 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3699 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3700 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3701 | return -EINVAL; |
| 3702 | } |
| 3703 | |
| 3704 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3705 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3706 | if (err) |
| 3707 | return err; |
| 3708 | |
| 3709 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3710 | verbose(env, |
| 3711 | "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] | 3712 | return -EINVAL; |
| 3713 | } |
| 3714 | |
| 3715 | if (mode == BPF_IND) { |
| 3716 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3717 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3718 | if (err) |
| 3719 | return err; |
| 3720 | } |
| 3721 | |
| 3722 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3723 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3724 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3725 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3726 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3727 | |
| 3728 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3729 | * the value fetched from the packet. |
| 3730 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3731 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3732 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3733 | return 0; |
| 3734 | } |
| 3735 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3736 | static int check_return_code(struct bpf_verifier_env *env) |
| 3737 | { |
| 3738 | struct bpf_reg_state *reg; |
| 3739 | struct tnum range = tnum_range(0, 1); |
| 3740 | |
| 3741 | switch (env->prog->type) { |
| 3742 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 3743 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 3744 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 3745 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3746 | break; |
| 3747 | default: |
| 3748 | return 0; |
| 3749 | } |
| 3750 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3751 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3752 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3753 | 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] | 3754 | reg_type_str[reg->type]); |
| 3755 | return -EINVAL; |
| 3756 | } |
| 3757 | |
| 3758 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3759 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3760 | if (!tnum_is_unknown(reg->var_off)) { |
| 3761 | char tn_buf[48]; |
| 3762 | |
| 3763 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3764 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3765 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3766 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3767 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3768 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3769 | return -EINVAL; |
| 3770 | } |
| 3771 | return 0; |
| 3772 | } |
| 3773 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3774 | /* non-recursive DFS pseudo code |
| 3775 | * 1 procedure DFS-iterative(G,v): |
| 3776 | * 2 label v as discovered |
| 3777 | * 3 let S be a stack |
| 3778 | * 4 S.push(v) |
| 3779 | * 5 while S is not empty |
| 3780 | * 6 t <- S.pop() |
| 3781 | * 7 if t is what we're looking for: |
| 3782 | * 8 return t |
| 3783 | * 9 for all edges e in G.adjacentEdges(t) do |
| 3784 | * 10 if edge e is already labelled |
| 3785 | * 11 continue with the next edge |
| 3786 | * 12 w <- G.adjacentVertex(t,e) |
| 3787 | * 13 if vertex w is not discovered and not explored |
| 3788 | * 14 label e as tree-edge |
| 3789 | * 15 label w as discovered |
| 3790 | * 16 S.push(w) |
| 3791 | * 17 continue at 5 |
| 3792 | * 18 else if vertex w is discovered |
| 3793 | * 19 label e as back-edge |
| 3794 | * 20 else |
| 3795 | * 21 // vertex w is explored |
| 3796 | * 22 label e as forward- or cross-edge |
| 3797 | * 23 label t as explored |
| 3798 | * 24 S.pop() |
| 3799 | * |
| 3800 | * convention: |
| 3801 | * 0x10 - discovered |
| 3802 | * 0x11 - discovered and fall-through edge labelled |
| 3803 | * 0x12 - discovered and fall-through and branch edges labelled |
| 3804 | * 0x20 - explored |
| 3805 | */ |
| 3806 | |
| 3807 | enum { |
| 3808 | DISCOVERED = 0x10, |
| 3809 | EXPLORED = 0x20, |
| 3810 | FALLTHROUGH = 1, |
| 3811 | BRANCH = 2, |
| 3812 | }; |
| 3813 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3814 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3815 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3816 | static int *insn_stack; /* stack of insns to process */ |
| 3817 | static int cur_stack; /* current stack index */ |
| 3818 | static int *insn_state; |
| 3819 | |
| 3820 | /* t, w, e - match pseudo-code above: |
| 3821 | * t - index of current instruction |
| 3822 | * w - next instruction |
| 3823 | * e - edge |
| 3824 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3825 | 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] | 3826 | { |
| 3827 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 3828 | return 0; |
| 3829 | |
| 3830 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 3831 | return 0; |
| 3832 | |
| 3833 | if (w < 0 || w >= env->prog->len) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3834 | 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] | 3835 | return -EINVAL; |
| 3836 | } |
| 3837 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3838 | if (e == BRANCH) |
| 3839 | /* mark branch target for state pruning */ |
| 3840 | env->explored_states[w] = STATE_LIST_MARK; |
| 3841 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3842 | if (insn_state[w] == 0) { |
| 3843 | /* tree-edge */ |
| 3844 | insn_state[t] = DISCOVERED | e; |
| 3845 | insn_state[w] = DISCOVERED; |
| 3846 | if (cur_stack >= env->prog->len) |
| 3847 | return -E2BIG; |
| 3848 | insn_stack[cur_stack++] = w; |
| 3849 | return 1; |
| 3850 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3851 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3852 | return -EINVAL; |
| 3853 | } else if (insn_state[w] == EXPLORED) { |
| 3854 | /* forward- or cross-edge */ |
| 3855 | insn_state[t] = DISCOVERED | e; |
| 3856 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3857 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3858 | return -EFAULT; |
| 3859 | } |
| 3860 | return 0; |
| 3861 | } |
| 3862 | |
| 3863 | /* non-recursive depth-first-search to detect loops in BPF program |
| 3864 | * loop == back-edge in directed graph |
| 3865 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3866 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3867 | { |
| 3868 | struct bpf_insn *insns = env->prog->insnsi; |
| 3869 | int insn_cnt = env->prog->len; |
| 3870 | int ret = 0; |
| 3871 | int i, t; |
| 3872 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 3873 | ret = check_subprogs(env); |
| 3874 | if (ret < 0) |
| 3875 | return ret; |
| 3876 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3877 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 3878 | if (!insn_state) |
| 3879 | return -ENOMEM; |
| 3880 | |
| 3881 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 3882 | if (!insn_stack) { |
| 3883 | kfree(insn_state); |
| 3884 | return -ENOMEM; |
| 3885 | } |
| 3886 | |
| 3887 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 3888 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 3889 | cur_stack = 1; |
| 3890 | |
| 3891 | peek_stack: |
| 3892 | if (cur_stack == 0) |
| 3893 | goto check_state; |
| 3894 | t = insn_stack[cur_stack - 1]; |
| 3895 | |
| 3896 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 3897 | u8 opcode = BPF_OP(insns[t].code); |
| 3898 | |
| 3899 | if (opcode == BPF_EXIT) { |
| 3900 | goto mark_explored; |
| 3901 | } else if (opcode == BPF_CALL) { |
| 3902 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 3903 | if (ret == 1) |
| 3904 | goto peek_stack; |
| 3905 | else if (ret < 0) |
| 3906 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 3907 | if (t + 1 < insn_cnt) |
| 3908 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 3909 | if (insns[t].src_reg == BPF_PSEUDO_CALL) { |
| 3910 | env->explored_states[t] = STATE_LIST_MARK; |
| 3911 | ret = push_insn(t, t + insns[t].imm + 1, BRANCH, env); |
| 3912 | if (ret == 1) |
| 3913 | goto peek_stack; |
| 3914 | else if (ret < 0) |
| 3915 | goto err_free; |
| 3916 | } |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3917 | } else if (opcode == BPF_JA) { |
| 3918 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 3919 | ret = -EINVAL; |
| 3920 | goto err_free; |
| 3921 | } |
| 3922 | /* unconditional jump with single edge */ |
| 3923 | ret = push_insn(t, t + insns[t].off + 1, |
| 3924 | FALLTHROUGH, env); |
| 3925 | if (ret == 1) |
| 3926 | goto peek_stack; |
| 3927 | else if (ret < 0) |
| 3928 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3929 | /* tell verifier to check for equivalent states |
| 3930 | * after every call and jump |
| 3931 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 3932 | if (t + 1 < insn_cnt) |
| 3933 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3934 | } else { |
| 3935 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 3936 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3937 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 3938 | if (ret == 1) |
| 3939 | goto peek_stack; |
| 3940 | else if (ret < 0) |
| 3941 | goto err_free; |
| 3942 | |
| 3943 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 3944 | if (ret == 1) |
| 3945 | goto peek_stack; |
| 3946 | else if (ret < 0) |
| 3947 | goto err_free; |
| 3948 | } |
| 3949 | } else { |
| 3950 | /* all other non-branch instructions with single |
| 3951 | * fall-through edge |
| 3952 | */ |
| 3953 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 3954 | if (ret == 1) |
| 3955 | goto peek_stack; |
| 3956 | else if (ret < 0) |
| 3957 | goto err_free; |
| 3958 | } |
| 3959 | |
| 3960 | mark_explored: |
| 3961 | insn_state[t] = EXPLORED; |
| 3962 | if (cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3963 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3964 | ret = -EFAULT; |
| 3965 | goto err_free; |
| 3966 | } |
| 3967 | goto peek_stack; |
| 3968 | |
| 3969 | check_state: |
| 3970 | for (i = 0; i < insn_cnt; i++) { |
| 3971 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3972 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3973 | ret = -EINVAL; |
| 3974 | goto err_free; |
| 3975 | } |
| 3976 | } |
| 3977 | ret = 0; /* cfg looks good */ |
| 3978 | |
| 3979 | err_free: |
| 3980 | kfree(insn_state); |
| 3981 | kfree(insn_stack); |
| 3982 | return ret; |
| 3983 | } |
| 3984 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3985 | /* check %cur's range satisfies %old's */ |
| 3986 | static bool range_within(struct bpf_reg_state *old, |
| 3987 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3988 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3989 | return old->umin_value <= cur->umin_value && |
| 3990 | old->umax_value >= cur->umax_value && |
| 3991 | old->smin_value <= cur->smin_value && |
| 3992 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | /* Maximum number of register states that can exist at once */ |
| 3996 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 3997 | struct idpair { |
| 3998 | u32 old; |
| 3999 | u32 cur; |
| 4000 | }; |
| 4001 | |
| 4002 | /* If in the old state two registers had the same id, then they need to have |
| 4003 | * the same id in the new state as well. But that id could be different from |
| 4004 | * the old state, so we need to track the mapping from old to new ids. |
| 4005 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 4006 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 4007 | * regs with a different old id could still have new id 9, we don't care about |
| 4008 | * that. |
| 4009 | * So we look through our idmap to see if this old id has been seen before. If |
| 4010 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 4011 | */ |
| 4012 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 4013 | { |
| 4014 | unsigned int i; |
| 4015 | |
| 4016 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 4017 | if (!idmap[i].old) { |
| 4018 | /* Reached an empty slot; haven't seen this id before */ |
| 4019 | idmap[i].old = old_id; |
| 4020 | idmap[i].cur = cur_id; |
| 4021 | return true; |
| 4022 | } |
| 4023 | if (idmap[i].old == old_id) |
| 4024 | return idmap[i].cur == cur_id; |
| 4025 | } |
| 4026 | /* We ran out of idmap slots, which should be impossible */ |
| 4027 | WARN_ON_ONCE(1); |
| 4028 | return false; |
| 4029 | } |
| 4030 | |
| 4031 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4032 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 4033 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4034 | { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4035 | bool equal; |
| 4036 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4037 | if (!(rold->live & REG_LIVE_READ)) |
| 4038 | /* explored state didn't use this */ |
| 4039 | return true; |
| 4040 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4041 | equal = memcmp(rold, rcur, offsetof(struct bpf_reg_state, frameno)) == 0; |
| 4042 | |
| 4043 | if (rold->type == PTR_TO_STACK) |
| 4044 | /* two stack pointers are equal only if they're pointing to |
| 4045 | * the same stack frame, since fp-8 in foo != fp-8 in bar |
| 4046 | */ |
| 4047 | return equal && rold->frameno == rcur->frameno; |
| 4048 | |
| 4049 | if (equal) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4050 | return true; |
| 4051 | |
| 4052 | if (rold->type == NOT_INIT) |
| 4053 | /* explored state can't have used this */ |
| 4054 | return true; |
| 4055 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4056 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4057 | switch (rold->type) { |
| 4058 | case SCALAR_VALUE: |
| 4059 | if (rcur->type == SCALAR_VALUE) { |
| 4060 | /* new val must satisfy old val knowledge */ |
| 4061 | return range_within(rold, rcur) && |
| 4062 | tnum_in(rold->var_off, rcur->var_off); |
| 4063 | } else { |
| 4064 | /* if we knew anything about the old value, we're not |
| 4065 | * equal, because we can't know anything about the |
| 4066 | * scalar value of the pointer in the new value. |
| 4067 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 4068 | return rold->umin_value == 0 && |
| 4069 | rold->umax_value == U64_MAX && |
| 4070 | rold->smin_value == S64_MIN && |
| 4071 | rold->smax_value == S64_MAX && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4072 | tnum_is_unknown(rold->var_off); |
| 4073 | } |
| 4074 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4075 | /* If the new min/max/var_off satisfy the old ones and |
| 4076 | * everything else matches, we are OK. |
| 4077 | * We don't care about the 'id' value, because nothing |
| 4078 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) |
| 4079 | */ |
| 4080 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 4081 | range_within(rold, rcur) && |
| 4082 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4083 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 4084 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 4085 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 4086 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 4087 | * checked, doing so could have affected others with the same |
| 4088 | * id, and we can't check for that because we lost the id when |
| 4089 | * we converted to a PTR_TO_MAP_VALUE. |
| 4090 | */ |
| 4091 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 4092 | return false; |
| 4093 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 4094 | return false; |
| 4095 | /* Check our ids match any regs they're supposed to */ |
| 4096 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4097 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4098 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 4099 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4100 | return false; |
| 4101 | /* We must have at least as much range as the old ptr |
| 4102 | * did, so that any accesses which were safe before are |
| 4103 | * still safe. This is true even if old range < old off, |
| 4104 | * since someone could have accessed through (ptr - k), or |
| 4105 | * even done ptr -= k in a register, to get a safe access. |
| 4106 | */ |
| 4107 | if (rold->range > rcur->range) |
| 4108 | return false; |
| 4109 | /* If the offsets don't match, we can't trust our alignment; |
| 4110 | * nor can we be sure that we won't fall out of range. |
| 4111 | */ |
| 4112 | if (rold->off != rcur->off) |
| 4113 | return false; |
| 4114 | /* id relations must be preserved */ |
| 4115 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 4116 | return false; |
| 4117 | /* new val must satisfy old val knowledge */ |
| 4118 | return range_within(rold, rcur) && |
| 4119 | tnum_in(rold->var_off, rcur->var_off); |
| 4120 | case PTR_TO_CTX: |
| 4121 | case CONST_PTR_TO_MAP: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4122 | case PTR_TO_PACKET_END: |
| 4123 | /* Only valid matches are exact, which memcmp() above |
| 4124 | * would have accepted |
| 4125 | */ |
| 4126 | default: |
| 4127 | /* Don't know what's going on, just say it's not safe */ |
| 4128 | return false; |
| 4129 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4130 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4131 | /* Shouldn't get here; if we do, say it's not safe */ |
| 4132 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 4133 | return false; |
| 4134 | } |
| 4135 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4136 | static bool stacksafe(struct bpf_func_state *old, |
| 4137 | struct bpf_func_state *cur, |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4138 | struct idpair *idmap) |
| 4139 | { |
| 4140 | int i, spi; |
| 4141 | |
| 4142 | /* if explored stack has more populated slots than current stack |
| 4143 | * such stacks are not equivalent |
| 4144 | */ |
| 4145 | if (old->allocated_stack > cur->allocated_stack) |
| 4146 | return false; |
| 4147 | |
| 4148 | /* walk slots of the explored stack and ignore any additional |
| 4149 | * slots in the current stack, since explored(safe) state |
| 4150 | * didn't use them |
| 4151 | */ |
| 4152 | for (i = 0; i < old->allocated_stack; i++) { |
| 4153 | spi = i / BPF_REG_SIZE; |
| 4154 | |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4155 | if (!(old->stack[spi].spilled_ptr.live & REG_LIVE_READ)) |
| 4156 | /* explored state didn't use this */ |
Gianluca Borello | fd05e57 | 2017-12-23 10:09:55 +0000 | [diff] [blame] | 4157 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4158 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4159 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 4160 | continue; |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4161 | /* if old state was safe with misc data in the stack |
| 4162 | * it will be safe with zero-initialized stack. |
| 4163 | * The opposite is not true |
| 4164 | */ |
| 4165 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC && |
| 4166 | cur->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_ZERO) |
| 4167 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4168 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 4169 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 4170 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 4171 | * this stack slot, but current has has STACK_MISC -> |
| 4172 | * this verifier states are not equivalent, |
| 4173 | * return false to continue verification of this path |
| 4174 | */ |
| 4175 | return false; |
| 4176 | if (i % BPF_REG_SIZE) |
| 4177 | continue; |
| 4178 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 4179 | continue; |
| 4180 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 4181 | &cur->stack[spi].spilled_ptr, |
| 4182 | idmap)) |
| 4183 | /* when explored and current stack slot are both storing |
| 4184 | * spilled registers, check that stored pointers types |
| 4185 | * are the same as well. |
| 4186 | * Ex: explored safe path could have stored |
| 4187 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 4188 | * but current path has stored: |
| 4189 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 4190 | * such verifier states are not equivalent. |
| 4191 | * return false to continue verification of this path |
| 4192 | */ |
| 4193 | return false; |
| 4194 | } |
| 4195 | return true; |
| 4196 | } |
| 4197 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4198 | /* compare two verifier states |
| 4199 | * |
| 4200 | * all states stored in state_list are known to be valid, since |
| 4201 | * verifier reached 'bpf_exit' instruction through them |
| 4202 | * |
| 4203 | * this function is called when verifier exploring different branches of |
| 4204 | * execution popped from the state stack. If it sees an old state that has |
| 4205 | * more strict register state and more strict stack state then this execution |
| 4206 | * branch doesn't need to be explored further, since verifier already |
| 4207 | * concluded that more strict state leads to valid finish. |
| 4208 | * |
| 4209 | * Therefore two states are equivalent if register state is more conservative |
| 4210 | * and explored stack state is more conservative than the current one. |
| 4211 | * Example: |
| 4212 | * explored current |
| 4213 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 4214 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 4215 | * |
| 4216 | * In other words if current stack state (one being explored) has more |
| 4217 | * valid slots than old one that already passed validation, it means |
| 4218 | * the verifier can stop exploring and conclude that current state is valid too |
| 4219 | * |
| 4220 | * Similarly with registers. If explored state has register type as invalid |
| 4221 | * whereas register type in current state is meaningful, it means that |
| 4222 | * the current state will reach 'bpf_exit' instruction safely |
| 4223 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4224 | static bool func_states_equal(struct bpf_func_state *old, |
| 4225 | struct bpf_func_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4226 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4227 | struct idpair *idmap; |
| 4228 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4229 | int i; |
| 4230 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4231 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 4232 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 4233 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 4234 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4235 | |
| 4236 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 4237 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4238 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4239 | } |
| 4240 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4241 | if (!stacksafe(old, cur, idmap)) |
| 4242 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 4243 | ret = true; |
| 4244 | out_free: |
| 4245 | kfree(idmap); |
| 4246 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4247 | } |
| 4248 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4249 | static bool states_equal(struct bpf_verifier_env *env, |
| 4250 | struct bpf_verifier_state *old, |
| 4251 | struct bpf_verifier_state *cur) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4252 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4253 | int i; |
| 4254 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4255 | if (old->curframe != cur->curframe) |
| 4256 | return false; |
| 4257 | |
| 4258 | /* for states to be equal callsites have to be the same |
| 4259 | * and all frame states need to be equivalent |
| 4260 | */ |
| 4261 | for (i = 0; i <= old->curframe; i++) { |
| 4262 | if (old->frame[i]->callsite != cur->frame[i]->callsite) |
| 4263 | return false; |
| 4264 | if (!func_states_equal(old->frame[i], cur->frame[i])) |
| 4265 | return false; |
| 4266 | } |
| 4267 | return true; |
| 4268 | } |
| 4269 | |
| 4270 | /* A write screens off any subsequent reads; but write marks come from the |
| 4271 | * straight-line code between a state and its parent. When we arrive at an |
| 4272 | * equivalent state (jump target or such) we didn't arrive by the straight-line |
| 4273 | * code, so read marks in the state must propagate to the parent regardless |
| 4274 | * of the state's write marks. That's what 'parent == state->parent' comparison |
| 4275 | * in mark_reg_read() and mark_stack_slot_read() is for. |
| 4276 | */ |
| 4277 | static int propagate_liveness(struct bpf_verifier_env *env, |
| 4278 | const struct bpf_verifier_state *vstate, |
| 4279 | struct bpf_verifier_state *vparent) |
| 4280 | { |
| 4281 | int i, frame, err = 0; |
| 4282 | struct bpf_func_state *state, *parent; |
| 4283 | |
| 4284 | if (vparent->curframe != vstate->curframe) { |
| 4285 | WARN(1, "propagate_live: parent frame %d current frame %d\n", |
| 4286 | vparent->curframe, vstate->curframe); |
| 4287 | return -EFAULT; |
| 4288 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4289 | /* Propagate read liveness of registers... */ |
| 4290 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
| 4291 | /* We don't need to worry about FP liveness because it's read-only */ |
| 4292 | for (i = 0; i < BPF_REG_FP; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4293 | if (vparent->frame[vparent->curframe]->regs[i].live & REG_LIVE_READ) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4294 | continue; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4295 | if (vstate->frame[vstate->curframe]->regs[i].live & REG_LIVE_READ) { |
| 4296 | err = mark_reg_read(env, vstate, vparent, i); |
| 4297 | if (err) |
| 4298 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4299 | } |
| 4300 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4301 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4302 | /* ... and stack slots */ |
| 4303 | for (frame = 0; frame <= vstate->curframe; frame++) { |
| 4304 | state = vstate->frame[frame]; |
| 4305 | parent = vparent->frame[frame]; |
| 4306 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 4307 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4308 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 4309 | continue; |
| 4310 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) |
| 4311 | mark_stack_slot_read(env, vstate, vparent, i, frame); |
| 4312 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4313 | } |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4314 | return err; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4315 | } |
| 4316 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4317 | 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] | 4318 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4319 | struct bpf_verifier_state_list *new_sl; |
| 4320 | struct bpf_verifier_state_list *sl; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4321 | struct bpf_verifier_state *cur = env->cur_state; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4322 | int i, j, err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4323 | |
| 4324 | sl = env->explored_states[insn_idx]; |
| 4325 | if (!sl) |
| 4326 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 4327 | * be doing state search here |
| 4328 | */ |
| 4329 | return 0; |
| 4330 | |
| 4331 | while (sl != STATE_LIST_MARK) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4332 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4333 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4334 | * prune the search. |
| 4335 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 4336 | * If we have any write marks in env->cur_state, they |
| 4337 | * will prevent corresponding reads in the continuation |
| 4338 | * from reaching our parent (an explored_state). Our |
| 4339 | * own state will get the read marks recorded, but |
| 4340 | * they'll be immediately forgotten as we're pruning |
| 4341 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4342 | */ |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4343 | err = propagate_liveness(env, &sl->state, cur); |
| 4344 | if (err) |
| 4345 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4346 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4347 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4348 | sl = sl->next; |
| 4349 | } |
| 4350 | |
| 4351 | /* there were no equivalent states, remember current one. |
| 4352 | * technically the current state is not proven to be safe yet, |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4353 | * but it will either reach outer most bpf_exit (which means it's safe) |
| 4354 | * or it will be rejected. Since there are no loops, we won't be |
| 4355 | * seeing this tuple (frame[0].callsite, frame[1].callsite, .. insn_idx) |
| 4356 | * again on the way to bpf_exit |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4357 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4358 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4359 | if (!new_sl) |
| 4360 | return -ENOMEM; |
| 4361 | |
| 4362 | /* add new state to the head of linked list */ |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 4363 | err = copy_verifier_state(&new_sl->state, cur); |
| 4364 | if (err) { |
| 4365 | free_verifier_state(&new_sl->state, false); |
| 4366 | kfree(new_sl); |
| 4367 | return err; |
| 4368 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4369 | new_sl->next = env->explored_states[insn_idx]; |
| 4370 | env->explored_states[insn_idx] = new_sl; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4371 | /* connect new state to parentage chain */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4372 | cur->parent = &new_sl->state; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 4373 | /* clear write marks in current state: the writes we did are not writes |
| 4374 | * our child did, so they don't screen off its reads from us. |
| 4375 | * (There are no read marks in current state, because reads always mark |
| 4376 | * their parent and current state never has children yet. Only |
| 4377 | * explored_states can get read marks.) |
| 4378 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4379 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4380 | cur->frame[cur->curframe]->regs[i].live = REG_LIVE_NONE; |
| 4381 | |
| 4382 | /* all stack frames are accessible from callee, clear them all */ |
| 4383 | for (j = 0; j <= cur->curframe; j++) { |
| 4384 | struct bpf_func_state *frame = cur->frame[j]; |
| 4385 | |
| 4386 | for (i = 0; i < frame->allocated_stack / BPF_REG_SIZE; i++) |
Alexei Starovoitov | cc2b14d | 2017-12-14 17:55:08 -0800 | [diff] [blame] | 4387 | frame->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4388 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4389 | return 0; |
| 4390 | } |
| 4391 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 4392 | static int ext_analyzer_insn_hook(struct bpf_verifier_env *env, |
| 4393 | int insn_idx, int prev_insn_idx) |
| 4394 | { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 4395 | if (env->dev_ops && env->dev_ops->insn_hook) |
| 4396 | return env->dev_ops->insn_hook(env, insn_idx, prev_insn_idx); |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 4397 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 4398 | return 0; |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 4399 | } |
| 4400 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4401 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4402 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4403 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4404 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4405 | struct bpf_reg_state *regs; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4406 | int insn_cnt = env->prog->len, i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4407 | int insn_idx, prev_insn_idx = 0; |
| 4408 | int insn_processed = 0; |
| 4409 | bool do_print_state = false; |
| 4410 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4411 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 4412 | if (!state) |
| 4413 | return -ENOMEM; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4414 | state->curframe = 0; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4415 | state->parent = NULL; |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4416 | state->frame[0] = kzalloc(sizeof(struct bpf_func_state), GFP_KERNEL); |
| 4417 | if (!state->frame[0]) { |
| 4418 | kfree(state); |
| 4419 | return -ENOMEM; |
| 4420 | } |
| 4421 | env->cur_state = state; |
| 4422 | init_func_state(env, state->frame[0], |
| 4423 | BPF_MAIN_FUNC /* callsite */, |
| 4424 | 0 /* frameno */, |
| 4425 | 0 /* subprogno, zero == main subprog */); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4426 | insn_idx = 0; |
| 4427 | for (;;) { |
| 4428 | struct bpf_insn *insn; |
| 4429 | u8 class; |
| 4430 | int err; |
| 4431 | |
| 4432 | if (insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4433 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4434 | insn_idx, insn_cnt); |
| 4435 | return -EFAULT; |
| 4436 | } |
| 4437 | |
| 4438 | insn = &insns[insn_idx]; |
| 4439 | class = BPF_CLASS(insn->code); |
| 4440 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 4441 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4442 | verbose(env, |
| 4443 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4444 | insn_processed); |
| 4445 | return -E2BIG; |
| 4446 | } |
| 4447 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4448 | err = is_state_visited(env, insn_idx); |
| 4449 | if (err < 0) |
| 4450 | return err; |
| 4451 | if (err == 1) { |
| 4452 | /* found equivalent state, can prune the search */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4453 | if (env->log.level) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4454 | if (do_print_state) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4455 | verbose(env, "\nfrom %d to %d: safe\n", |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4456 | prev_insn_idx, insn_idx); |
| 4457 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4458 | verbose(env, "%d: safe\n", insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4459 | } |
| 4460 | goto process_bpf_exit; |
| 4461 | } |
| 4462 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 4463 | if (need_resched()) |
| 4464 | cond_resched(); |
| 4465 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4466 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
| 4467 | if (env->log.level > 1) |
| 4468 | verbose(env, "%d:", insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 4469 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4470 | verbose(env, "\nfrom %d to %d:", |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 4471 | prev_insn_idx, insn_idx); |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4472 | print_verifier_state(env, state->frame[state->curframe]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4473 | do_print_state = false; |
| 4474 | } |
| 4475 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4476 | if (env->log.level) { |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 4477 | const struct bpf_insn_cbs cbs = { |
| 4478 | .cb_print = verbose, |
| 4479 | }; |
| 4480 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4481 | verbose(env, "%d: ", insn_idx); |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 4482 | print_bpf_insn(&cbs, env, insn, env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4483 | } |
| 4484 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 4485 | err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx); |
| 4486 | if (err) |
| 4487 | return err; |
| 4488 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4489 | regs = cur_regs(env); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4490 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4491 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4492 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4493 | if (err) |
| 4494 | return err; |
| 4495 | |
| 4496 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4497 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4498 | |
| 4499 | /* check for reserved fields is already done */ |
| 4500 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4501 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4502 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4503 | if (err) |
| 4504 | return err; |
| 4505 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4506 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4507 | if (err) |
| 4508 | return err; |
| 4509 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 4510 | src_reg_type = regs[insn->src_reg].type; |
| 4511 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4512 | /* check that memory (src_reg + off) is readable, |
| 4513 | * the state of dst_reg will be updated by this func |
| 4514 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4515 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4516 | BPF_SIZE(insn->code), BPF_READ, |
| 4517 | insn->dst_reg); |
| 4518 | if (err) |
| 4519 | return err; |
| 4520 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4521 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 4522 | |
| 4523 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4524 | /* saw a valid insn |
| 4525 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4526 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4527 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4528 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4529 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4530 | } else if (src_reg_type != *prev_src_type && |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4531 | (src_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4532 | *prev_src_type == PTR_TO_CTX)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4533 | /* ABuser program is trying to use the same insn |
| 4534 | * dst_reg = *(u32*) (src_reg + off) |
| 4535 | * with different pointer types: |
| 4536 | * src_reg == ctx in one branch and |
| 4537 | * src_reg == stack|map in some other branch. |
| 4538 | * Reject it. |
| 4539 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4540 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4541 | return -EINVAL; |
| 4542 | } |
| 4543 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4544 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4545 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4546 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4547 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4548 | err = check_xadd(env, insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4549 | if (err) |
| 4550 | return err; |
| 4551 | insn_idx++; |
| 4552 | continue; |
| 4553 | } |
| 4554 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4555 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4556 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4557 | if (err) |
| 4558 | return err; |
| 4559 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4560 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4561 | if (err) |
| 4562 | return err; |
| 4563 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4564 | dst_reg_type = regs[insn->dst_reg].type; |
| 4565 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4566 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4567 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4568 | BPF_SIZE(insn->code), BPF_WRITE, |
| 4569 | insn->src_reg); |
| 4570 | if (err) |
| 4571 | return err; |
| 4572 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4573 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 4574 | |
| 4575 | if (*prev_dst_type == NOT_INIT) { |
| 4576 | *prev_dst_type = dst_reg_type; |
| 4577 | } else if (dst_reg_type != *prev_dst_type && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4578 | (dst_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4579 | *prev_dst_type == PTR_TO_CTX)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4580 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4581 | return -EINVAL; |
| 4582 | } |
| 4583 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4584 | } else if (class == BPF_ST) { |
| 4585 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 4586 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4587 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4588 | return -EINVAL; |
| 4589 | } |
| 4590 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4591 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4592 | if (err) |
| 4593 | return err; |
| 4594 | |
| 4595 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4596 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4597 | BPF_SIZE(insn->code), BPF_WRITE, |
| 4598 | -1); |
| 4599 | if (err) |
| 4600 | return err; |
| 4601 | |
| 4602 | } else if (class == BPF_JMP) { |
| 4603 | u8 opcode = BPF_OP(insn->code); |
| 4604 | |
| 4605 | if (opcode == BPF_CALL) { |
| 4606 | if (BPF_SRC(insn->code) != BPF_K || |
| 4607 | insn->off != 0 || |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4608 | (insn->src_reg != BPF_REG_0 && |
| 4609 | insn->src_reg != BPF_PSEUDO_CALL) || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4610 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4611 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4612 | return -EINVAL; |
| 4613 | } |
| 4614 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4615 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 4616 | err = check_func_call(env, insn, &insn_idx); |
| 4617 | else |
| 4618 | err = check_helper_call(env, insn->imm, insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4619 | if (err) |
| 4620 | return err; |
| 4621 | |
| 4622 | } else if (opcode == BPF_JA) { |
| 4623 | if (BPF_SRC(insn->code) != BPF_K || |
| 4624 | insn->imm != 0 || |
| 4625 | insn->src_reg != BPF_REG_0 || |
| 4626 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4627 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4628 | return -EINVAL; |
| 4629 | } |
| 4630 | |
| 4631 | insn_idx += insn->off + 1; |
| 4632 | continue; |
| 4633 | |
| 4634 | } else if (opcode == BPF_EXIT) { |
| 4635 | if (BPF_SRC(insn->code) != BPF_K || |
| 4636 | insn->imm != 0 || |
| 4637 | insn->src_reg != BPF_REG_0 || |
| 4638 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4639 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4640 | return -EINVAL; |
| 4641 | } |
| 4642 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4643 | if (state->curframe) { |
| 4644 | /* exit from nested function */ |
| 4645 | prev_insn_idx = insn_idx; |
| 4646 | err = prepare_func_exit(env, &insn_idx); |
| 4647 | if (err) |
| 4648 | return err; |
| 4649 | do_print_state = true; |
| 4650 | continue; |
| 4651 | } |
| 4652 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4653 | /* eBPF calling convetion is such that R0 is used |
| 4654 | * to return the value from eBPF program. |
| 4655 | * Make sure that it's readable at this time |
| 4656 | * of bpf_exit, which means that program wrote |
| 4657 | * something into it earlier |
| 4658 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4659 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4660 | if (err) |
| 4661 | return err; |
| 4662 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4663 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4664 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4665 | return -EACCES; |
| 4666 | } |
| 4667 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4668 | err = check_return_code(env); |
| 4669 | if (err) |
| 4670 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4671 | process_bpf_exit: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4672 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
| 4673 | if (err < 0) { |
| 4674 | if (err != -ENOENT) |
| 4675 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4676 | break; |
| 4677 | } else { |
| 4678 | do_print_state = true; |
| 4679 | continue; |
| 4680 | } |
| 4681 | } else { |
| 4682 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 4683 | if (err) |
| 4684 | return err; |
| 4685 | } |
| 4686 | } else if (class == BPF_LD) { |
| 4687 | u8 mode = BPF_MODE(insn->code); |
| 4688 | |
| 4689 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4690 | err = check_ld_abs(env, insn); |
| 4691 | if (err) |
| 4692 | return err; |
| 4693 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4694 | } else if (mode == BPF_IMM) { |
| 4695 | err = check_ld_imm(env, insn); |
| 4696 | if (err) |
| 4697 | return err; |
| 4698 | |
| 4699 | insn_idx++; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4700 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4701 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4702 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4703 | return -EINVAL; |
| 4704 | } |
| 4705 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4706 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4707 | return -EINVAL; |
| 4708 | } |
| 4709 | |
| 4710 | insn_idx++; |
| 4711 | } |
| 4712 | |
Alexei Starovoitov | f4d7e40 | 2017-12-14 17:55:06 -0800 | [diff] [blame] | 4713 | verbose(env, "processed %d insns, stack depth ", insn_processed); |
| 4714 | for (i = 0; i < env->subprog_cnt + 1; i++) { |
| 4715 | u32 depth = env->subprog_stack_depth[i]; |
| 4716 | |
| 4717 | verbose(env, "%d", depth); |
| 4718 | if (i + 1 < env->subprog_cnt + 1) |
| 4719 | verbose(env, "+"); |
| 4720 | } |
| 4721 | verbose(env, "\n"); |
| 4722 | env->prog->aux->stack_depth = env->subprog_stack_depth[0]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4723 | return 0; |
| 4724 | } |
| 4725 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4726 | static int check_map_prealloc(struct bpf_map *map) |
| 4727 | { |
| 4728 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 4729 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 4730 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4731 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 4732 | } |
| 4733 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4734 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 4735 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4736 | struct bpf_prog *prog) |
| 4737 | |
| 4738 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4739 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 4740 | * preallocated hash maps, since doing memory allocation |
| 4741 | * in overflow_handler can crash depending on where nmi got |
| 4742 | * triggered. |
| 4743 | */ |
| 4744 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 4745 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4746 | 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] | 4747 | return -EINVAL; |
| 4748 | } |
| 4749 | if (map->inner_map_meta && |
| 4750 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4751 | 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] | 4752 | return -EINVAL; |
| 4753 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4754 | } |
| 4755 | return 0; |
| 4756 | } |
| 4757 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4758 | /* look for pseudo eBPF instructions that access map FDs and |
| 4759 | * replace them with actual map pointers |
| 4760 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4761 | 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] | 4762 | { |
| 4763 | struct bpf_insn *insn = env->prog->insnsi; |
| 4764 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4765 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4766 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 4767 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 4768 | if (err) |
| 4769 | return err; |
| 4770 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4771 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4772 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4773 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4774 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4775 | return -EINVAL; |
| 4776 | } |
| 4777 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4778 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 4779 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 4780 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4781 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4782 | return -EINVAL; |
| 4783 | } |
| 4784 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4785 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 4786 | struct bpf_map *map; |
| 4787 | struct fd f; |
| 4788 | |
| 4789 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 4790 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 4791 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4792 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4793 | return -EINVAL; |
| 4794 | } |
| 4795 | |
| 4796 | if (insn->src_reg == 0) |
| 4797 | /* valid generic load 64-bit imm */ |
| 4798 | goto next_insn; |
| 4799 | |
| 4800 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4801 | verbose(env, |
| 4802 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4803 | return -EINVAL; |
| 4804 | } |
| 4805 | |
| 4806 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 4807 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4808 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4809 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4810 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4811 | return PTR_ERR(map); |
| 4812 | } |
| 4813 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4814 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4815 | if (err) { |
| 4816 | fdput(f); |
| 4817 | return err; |
| 4818 | } |
| 4819 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4820 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 4821 | insn[0].imm = (u32) (unsigned long) map; |
| 4822 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 4823 | |
| 4824 | /* check whether we recorded this map already */ |
| 4825 | for (j = 0; j < env->used_map_cnt; j++) |
| 4826 | if (env->used_maps[j] == map) { |
| 4827 | fdput(f); |
| 4828 | goto next_insn; |
| 4829 | } |
| 4830 | |
| 4831 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 4832 | fdput(f); |
| 4833 | return -E2BIG; |
| 4834 | } |
| 4835 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4836 | /* hold the map. If the program is rejected by verifier, |
| 4837 | * the map will be released by release_maps() or it |
| 4838 | * will be used by the valid program until it's unloaded |
| 4839 | * and all maps are released in free_bpf_prog_info() |
| 4840 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 4841 | map = bpf_map_inc(map, false); |
| 4842 | if (IS_ERR(map)) { |
| 4843 | fdput(f); |
| 4844 | return PTR_ERR(map); |
| 4845 | } |
| 4846 | env->used_maps[env->used_map_cnt++] = map; |
| 4847 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4848 | fdput(f); |
| 4849 | next_insn: |
| 4850 | insn++; |
| 4851 | i++; |
| 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 4856 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 4857 | * These pointers will be used later by verifier to validate map access. |
| 4858 | */ |
| 4859 | return 0; |
| 4860 | } |
| 4861 | |
| 4862 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4863 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4864 | { |
| 4865 | int i; |
| 4866 | |
| 4867 | for (i = 0; i < env->used_map_cnt; i++) |
| 4868 | bpf_map_put(env->used_maps[i]); |
| 4869 | } |
| 4870 | |
| 4871 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4872 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4873 | { |
| 4874 | struct bpf_insn *insn = env->prog->insnsi; |
| 4875 | int insn_cnt = env->prog->len; |
| 4876 | int i; |
| 4877 | |
| 4878 | for (i = 0; i < insn_cnt; i++, insn++) |
| 4879 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 4880 | insn->src_reg = 0; |
| 4881 | } |
| 4882 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4883 | /* single env->prog->insni[off] instruction was replaced with the range |
| 4884 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 4885 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 4886 | */ |
| 4887 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 4888 | u32 off, u32 cnt) |
| 4889 | { |
| 4890 | 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] | 4891 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4892 | |
| 4893 | if (cnt == 1) |
| 4894 | return 0; |
| 4895 | new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len); |
| 4896 | if (!new_data) |
| 4897 | return -ENOMEM; |
| 4898 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 4899 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 4900 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4901 | for (i = off; i < off + cnt - 1; i++) |
| 4902 | new_data[i].seen = true; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4903 | env->insn_aux_data = new_data; |
| 4904 | vfree(old_data); |
| 4905 | return 0; |
| 4906 | } |
| 4907 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4908 | static void adjust_subprog_starts(struct bpf_verifier_env *env, u32 off, u32 len) |
| 4909 | { |
| 4910 | int i; |
| 4911 | |
| 4912 | if (len == 1) |
| 4913 | return; |
| 4914 | for (i = 0; i < env->subprog_cnt; i++) { |
| 4915 | if (env->subprog_starts[i] < off) |
| 4916 | continue; |
| 4917 | env->subprog_starts[i] += len - 1; |
| 4918 | } |
| 4919 | } |
| 4920 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4921 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 4922 | const struct bpf_insn *patch, u32 len) |
| 4923 | { |
| 4924 | struct bpf_prog *new_prog; |
| 4925 | |
| 4926 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 4927 | if (!new_prog) |
| 4928 | return NULL; |
| 4929 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 4930 | return NULL; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 4931 | adjust_subprog_starts(env, off, len); |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4932 | return new_prog; |
| 4933 | } |
| 4934 | |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4935 | /* The verifier does more data flow analysis than llvm and will not explore |
| 4936 | * branches that are dead at run time. Malicious programs can have dead code |
| 4937 | * too. Therefore replace all dead at-run-time code with nops. |
| 4938 | */ |
| 4939 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 4940 | { |
| 4941 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 4942 | struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0); |
| 4943 | struct bpf_insn *insn = env->prog->insnsi; |
| 4944 | const int insn_cnt = env->prog->len; |
| 4945 | int i; |
| 4946 | |
| 4947 | for (i = 0; i < insn_cnt; i++) { |
| 4948 | if (aux_data[i].seen) |
| 4949 | continue; |
| 4950 | memcpy(insn + i, &nop, sizeof(nop)); |
| 4951 | } |
| 4952 | } |
| 4953 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4954 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 4955 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 4956 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4957 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4958 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 4959 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4960 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4961 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4962 | struct bpf_insn insn_buf[16], *insn; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4963 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4964 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4965 | bool is_narrower_load; |
| 4966 | u32 target_size; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4967 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4968 | if (ops->gen_prologue) { |
| 4969 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 4970 | env->prog); |
| 4971 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4972 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4973 | return -EINVAL; |
| 4974 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4975 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4976 | if (!new_prog) |
| 4977 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4978 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4979 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4980 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4981 | } |
| 4982 | } |
| 4983 | |
| 4984 | if (!ops->convert_ctx_access) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4985 | return 0; |
| 4986 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4987 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4988 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4989 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 4990 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 4991 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 4992 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 4993 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4994 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 4995 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 4996 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 4997 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 4998 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4999 | type = BPF_WRITE; |
| 5000 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5001 | continue; |
| 5002 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5003 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5004 | continue; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5005 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5006 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5007 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5008 | |
| 5009 | /* If the read access is a narrower load of the field, |
| 5010 | * convert to a 4/8-byte load, to minimum program type specific |
| 5011 | * convert_ctx_access changes. If conversion is successful, |
| 5012 | * we will apply proper mask to the result. |
| 5013 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5014 | is_narrower_load = size < ctx_field_size; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5015 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5016 | u32 off = insn->off; |
| 5017 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5018 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5019 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5020 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5021 | return -EINVAL; |
| 5022 | } |
| 5023 | |
| 5024 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5025 | if (ctx_field_size == 4) |
| 5026 | size_code = BPF_W; |
| 5027 | else if (ctx_field_size == 8) |
| 5028 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5029 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5030 | insn->off = off & ~(ctx_field_size - 1); |
| 5031 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 5032 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5033 | |
| 5034 | target_size = 0; |
| 5035 | cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog, |
| 5036 | &target_size); |
| 5037 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 5038 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5039 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5040 | return -EINVAL; |
| 5041 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5042 | |
| 5043 | if (is_narrower_load && size < target_size) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5044 | if (ctx_field_size <= 4) |
| 5045 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5046 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5047 | else |
| 5048 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 5049 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 5050 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5051 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 5052 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5053 | if (!new_prog) |
| 5054 | return -ENOMEM; |
| 5055 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5056 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5057 | |
| 5058 | /* keep walking new program and skip insns we just inserted */ |
| 5059 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5060 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5061 | } |
| 5062 | |
| 5063 | return 0; |
| 5064 | } |
| 5065 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5066 | static int jit_subprogs(struct bpf_verifier_env *env) |
| 5067 | { |
| 5068 | struct bpf_prog *prog = env->prog, **func, *tmp; |
| 5069 | int i, j, subprog_start, subprog_end = 0, len, subprog; |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5070 | struct bpf_insn *insn; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5071 | void *old_bpf_func; |
| 5072 | int err = -ENOMEM; |
| 5073 | |
| 5074 | if (env->subprog_cnt == 0) |
| 5075 | return 0; |
| 5076 | |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5077 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5078 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5079 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5080 | continue; |
| 5081 | subprog = find_subprog(env, i + insn->imm + 1); |
| 5082 | if (subprog < 0) { |
| 5083 | WARN_ONCE(1, "verifier bug. No program starts at insn %d\n", |
| 5084 | i + insn->imm + 1); |
| 5085 | return -EFAULT; |
| 5086 | } |
| 5087 | /* temporarily remember subprog id inside insn instead of |
| 5088 | * aux_data, since next loop will split up all insns into funcs |
| 5089 | */ |
| 5090 | insn->off = subprog + 1; |
| 5091 | /* remember original imm in case JIT fails and fallback |
| 5092 | * to interpreter will be needed |
| 5093 | */ |
| 5094 | env->insn_aux_data[i].call_imm = insn->imm; |
| 5095 | /* point imm to __bpf_call_base+1 from JITs point of view */ |
| 5096 | insn->imm = 1; |
| 5097 | } |
| 5098 | |
| 5099 | func = kzalloc(sizeof(prog) * (env->subprog_cnt + 1), GFP_KERNEL); |
| 5100 | if (!func) |
| 5101 | return -ENOMEM; |
| 5102 | |
| 5103 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5104 | subprog_start = subprog_end; |
| 5105 | if (env->subprog_cnt == i) |
| 5106 | subprog_end = prog->len; |
| 5107 | else |
| 5108 | subprog_end = env->subprog_starts[i]; |
| 5109 | |
| 5110 | len = subprog_end - subprog_start; |
| 5111 | func[i] = bpf_prog_alloc(bpf_prog_size(len), GFP_USER); |
| 5112 | if (!func[i]) |
| 5113 | goto out_free; |
| 5114 | memcpy(func[i]->insnsi, &prog->insnsi[subprog_start], |
| 5115 | len * sizeof(struct bpf_insn)); |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5116 | func[i]->type = prog->type; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5117 | func[i]->len = len; |
Daniel Borkmann | 4f74d80 | 2017-12-20 13:42:56 +0100 | [diff] [blame] | 5118 | if (bpf_prog_calc_tag(func[i])) |
| 5119 | goto out_free; |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5120 | func[i]->is_func = 1; |
| 5121 | /* Use bpf_prog_F_tag to indicate functions in stack traces. |
| 5122 | * Long term would need debug info to populate names |
| 5123 | */ |
| 5124 | func[i]->aux->name[0] = 'F'; |
| 5125 | func[i]->aux->stack_depth = env->subprog_stack_depth[i]; |
| 5126 | func[i]->jit_requested = 1; |
| 5127 | func[i] = bpf_int_jit_compile(func[i]); |
| 5128 | if (!func[i]->jited) { |
| 5129 | err = -ENOTSUPP; |
| 5130 | goto out_free; |
| 5131 | } |
| 5132 | cond_resched(); |
| 5133 | } |
| 5134 | /* at this point all bpf functions were successfully JITed |
| 5135 | * now populate all bpf_calls with correct addresses and |
| 5136 | * run last pass of JIT |
| 5137 | */ |
| 5138 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5139 | insn = func[i]->insnsi; |
| 5140 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 5141 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5142 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5143 | continue; |
| 5144 | subprog = insn->off; |
| 5145 | insn->off = 0; |
| 5146 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 5147 | func[subprog]->bpf_func - |
| 5148 | __bpf_call_base; |
| 5149 | } |
| 5150 | } |
| 5151 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5152 | old_bpf_func = func[i]->bpf_func; |
| 5153 | tmp = bpf_int_jit_compile(func[i]); |
| 5154 | if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) { |
| 5155 | verbose(env, "JIT doesn't support bpf-to-bpf calls\n"); |
| 5156 | err = -EFAULT; |
| 5157 | goto out_free; |
| 5158 | } |
| 5159 | cond_resched(); |
| 5160 | } |
| 5161 | |
| 5162 | /* finally lock prog and jit images for all functions and |
| 5163 | * populate kallsysm |
| 5164 | */ |
| 5165 | for (i = 0; i <= env->subprog_cnt; i++) { |
| 5166 | bpf_prog_lock_ro(func[i]); |
| 5167 | bpf_prog_kallsyms_add(func[i]); |
| 5168 | } |
Daniel Borkmann | 7105e82 | 2017-12-20 13:42:57 +0100 | [diff] [blame] | 5169 | |
| 5170 | /* Last step: make now unused interpreter insns from main |
| 5171 | * prog consistent for later dump requests, so they can |
| 5172 | * later look the same as if they were interpreted only. |
| 5173 | */ |
| 5174 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 5175 | unsigned long addr; |
| 5176 | |
| 5177 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5178 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5179 | continue; |
| 5180 | insn->off = env->insn_aux_data[i].call_imm; |
| 5181 | subprog = find_subprog(env, i + insn->off + 1); |
| 5182 | addr = (unsigned long)func[subprog + 1]->bpf_func; |
| 5183 | addr &= PAGE_MASK; |
| 5184 | insn->imm = (u64 (*)(u64, u64, u64, u64, u64)) |
| 5185 | addr - __bpf_call_base; |
| 5186 | } |
| 5187 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5188 | prog->jited = 1; |
| 5189 | prog->bpf_func = func[0]->bpf_func; |
| 5190 | prog->aux->func = func; |
| 5191 | prog->aux->func_cnt = env->subprog_cnt + 1; |
| 5192 | return 0; |
| 5193 | out_free: |
| 5194 | for (i = 0; i <= env->subprog_cnt; i++) |
| 5195 | if (func[i]) |
| 5196 | bpf_jit_free(func[i]); |
| 5197 | kfree(func); |
| 5198 | /* cleanup main prog to be interpreted */ |
| 5199 | prog->jit_requested = 0; |
| 5200 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 5201 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5202 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5203 | continue; |
| 5204 | insn->off = 0; |
| 5205 | insn->imm = env->insn_aux_data[i].call_imm; |
| 5206 | } |
| 5207 | return err; |
| 5208 | } |
| 5209 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5210 | static int fixup_call_args(struct bpf_verifier_env *env) |
| 5211 | { |
| 5212 | struct bpf_prog *prog = env->prog; |
| 5213 | struct bpf_insn *insn = prog->insnsi; |
| 5214 | int i, depth; |
| 5215 | |
Alexei Starovoitov | 1c2a088 | 2017-12-14 17:55:15 -0800 | [diff] [blame] | 5216 | if (env->prog->jit_requested) |
| 5217 | if (jit_subprogs(env) == 0) |
| 5218 | return 0; |
| 5219 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5220 | for (i = 0; i < prog->len; i++, insn++) { |
| 5221 | if (insn->code != (BPF_JMP | BPF_CALL) || |
| 5222 | insn->src_reg != BPF_PSEUDO_CALL) |
| 5223 | continue; |
| 5224 | depth = get_callee_stack_depth(env, insn, i); |
| 5225 | if (depth < 0) |
| 5226 | return depth; |
| 5227 | bpf_patch_call_args(insn, depth); |
| 5228 | } |
| 5229 | return 0; |
| 5230 | } |
| 5231 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5232 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5233 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5234 | * |
| 5235 | * this function is called after eBPF program passed verification |
| 5236 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5237 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5238 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5239 | struct bpf_prog *prog = env->prog; |
| 5240 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5241 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5242 | const int insn_cnt = prog->len; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5243 | struct bpf_insn insn_buf[16]; |
| 5244 | struct bpf_prog *new_prog; |
| 5245 | struct bpf_map *map_ptr; |
| 5246 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5247 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5248 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 5249 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 5250 | continue; |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5251 | if (insn->src_reg == BPF_PSEUDO_CALL) |
| 5252 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5253 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5254 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 5255 | prog->dst_needed = 1; |
| 5256 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 5257 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame] | 5258 | if (insn->imm == BPF_FUNC_override_return) |
| 5259 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5260 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 5261 | /* If we tail call into other programs, we |
| 5262 | * cannot make any assumptions since they can |
| 5263 | * be replaced dynamically during runtime in |
| 5264 | * the program array. |
| 5265 | */ |
| 5266 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 5267 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 5268 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5269 | /* mark bpf_tail_call as different opcode to avoid |
| 5270 | * conditional branch in the interpeter for every normal |
| 5271 | * call and to prevent accidental JITing by JIT compiler |
| 5272 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5273 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5274 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 5275 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5276 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5277 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5278 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 5279 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
| 5280 | * handlers are currently limited to 64 bit only. |
| 5281 | */ |
Alexei Starovoitov | 60b58afc | 2017-12-14 17:55:14 -0800 | [diff] [blame] | 5282 | if (prog->jit_requested && BITS_PER_LONG == 64 && |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 5283 | insn->imm == BPF_FUNC_map_lookup_elem) { |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5284 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 5285 | if (map_ptr == BPF_MAP_PTR_POISON || |
| 5286 | !map_ptr->ops->map_gen_lookup) |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5287 | goto patch_call_imm; |
| 5288 | |
| 5289 | cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf); |
| 5290 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5291 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5292 | return -EINVAL; |
| 5293 | } |
| 5294 | |
| 5295 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 5296 | cnt); |
| 5297 | if (!new_prog) |
| 5298 | return -ENOMEM; |
| 5299 | |
| 5300 | delta += cnt - 1; |
| 5301 | |
| 5302 | /* keep walking new program and skip insns we just inserted */ |
| 5303 | env->prog = prog = new_prog; |
| 5304 | insn = new_prog->insnsi + i + delta; |
| 5305 | continue; |
| 5306 | } |
| 5307 | |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 5308 | if (insn->imm == BPF_FUNC_redirect_map) { |
Daniel Borkmann | 7c30013 | 2017-09-20 00:44:21 +0200 | [diff] [blame] | 5309 | /* Note, we cannot use prog directly as imm as subsequent |
| 5310 | * rewrites would still change the prog pointer. The only |
| 5311 | * stable address we can use is aux, which also works with |
| 5312 | * prog clones during blinding. |
| 5313 | */ |
| 5314 | u64 addr = (unsigned long)prog->aux; |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 5315 | struct bpf_insn r4_ld[] = { |
| 5316 | BPF_LD_IMM64(BPF_REG_4, addr), |
| 5317 | *insn, |
| 5318 | }; |
| 5319 | cnt = ARRAY_SIZE(r4_ld); |
| 5320 | |
| 5321 | new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt); |
| 5322 | if (!new_prog) |
| 5323 | return -ENOMEM; |
| 5324 | |
| 5325 | delta += cnt - 1; |
| 5326 | env->prog = prog = new_prog; |
| 5327 | insn = new_prog->insnsi + i + delta; |
| 5328 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 5329 | patch_call_imm: |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5330 | fn = env->ops->get_func_proto(insn->imm); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5331 | /* all functions that have prototype and verifier allowed |
| 5332 | * programs to call them, must be real in-kernel functions |
| 5333 | */ |
| 5334 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5335 | verbose(env, |
| 5336 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5337 | func_id_name(insn->imm), insn->imm); |
| 5338 | return -EFAULT; |
| 5339 | } |
| 5340 | insn->imm = fn->func - __bpf_call_base; |
| 5341 | } |
| 5342 | |
| 5343 | return 0; |
| 5344 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5345 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5346 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5347 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5348 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5349 | int i; |
| 5350 | |
| 5351 | if (!env->explored_states) |
| 5352 | return; |
| 5353 | |
| 5354 | for (i = 0; i < env->prog->len; i++) { |
| 5355 | sl = env->explored_states[i]; |
| 5356 | |
| 5357 | if (sl) |
| 5358 | while (sl != STATE_LIST_MARK) { |
| 5359 | sln = sl->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 5360 | free_verifier_state(&sl->state, false); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5361 | kfree(sl); |
| 5362 | sl = sln; |
| 5363 | } |
| 5364 | } |
| 5365 | |
| 5366 | kfree(env->explored_states); |
| 5367 | } |
| 5368 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5369 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5370 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5371 | struct bpf_verifier_env *env; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5372 | struct bpf_verifer_log *log; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5373 | int ret = -EINVAL; |
| 5374 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 5375 | /* no program is valid */ |
| 5376 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 5377 | return -EINVAL; |
| 5378 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5379 | /* '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] | 5380 | * allocate/free it every time bpf_check() is called |
| 5381 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5382 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5383 | if (!env) |
| 5384 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 5385 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5386 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5387 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 5388 | (*prog)->len); |
| 5389 | ret = -ENOMEM; |
| 5390 | if (!env->insn_aux_data) |
| 5391 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5392 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 5393 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5394 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5395 | /* grab the mutex to protect few globals used by verifier */ |
| 5396 | mutex_lock(&bpf_verifier_lock); |
| 5397 | |
| 5398 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 5399 | /* user requested verbose verifier output |
| 5400 | * and supplied buffer to store the verification trace |
| 5401 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 5402 | log->level = attr->log_level; |
| 5403 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 5404 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5405 | |
| 5406 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 5407 | /* log attributes have to be sane */ |
| 5408 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || |
| 5409 | !log->level || !log->ubuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5410 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5411 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 5412 | |
| 5413 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 5414 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 5415 | env->strict_alignment = true; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5416 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 5417 | if (env->prog->aux->offload) { |
| 5418 | ret = bpf_prog_offload_verifier_prep(env); |
| 5419 | if (ret) |
| 5420 | goto err_unlock; |
| 5421 | } |
| 5422 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5423 | ret = replace_map_fd_with_map_ptr(env); |
| 5424 | if (ret < 0) |
| 5425 | goto skip_full_check; |
| 5426 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5427 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 5428 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5429 | GFP_USER); |
| 5430 | ret = -ENOMEM; |
| 5431 | if (!env->explored_states) |
| 5432 | goto skip_full_check; |
| 5433 | |
Alexei Starovoitov | cc8b0b9 | 2017-12-14 17:55:05 -0800 | [diff] [blame] | 5434 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 5435 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 5436 | ret = check_cfg(env); |
| 5437 | if (ret < 0) |
| 5438 | goto skip_full_check; |
| 5439 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 5440 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 5441 | if (env->cur_state) { |
| 5442 | free_verifier_state(env->cur_state, true); |
| 5443 | env->cur_state = NULL; |
| 5444 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5445 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5446 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 5447 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 5448 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5449 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5450 | if (ret == 0) |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 5451 | sanitize_dead_code(env); |
| 5452 | |
| 5453 | if (ret == 0) |
Alexei Starovoitov | 70a87ff | 2017-12-25 13:15:40 -0800 | [diff] [blame^] | 5454 | ret = check_max_stack_depth(env); |
| 5455 | |
| 5456 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5457 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 5458 | ret = convert_ctx_accesses(env); |
| 5459 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5460 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 5461 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 5462 | |
Alexei Starovoitov | 1ea47e0 | 2017-12-14 17:55:13 -0800 | [diff] [blame] | 5463 | if (ret == 0) |
| 5464 | ret = fixup_call_args(env); |
| 5465 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5466 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5467 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5468 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5469 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5470 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5471 | } |
| 5472 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5473 | if (ret == 0 && env->used_map_cnt) { |
| 5474 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5475 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 5476 | sizeof(env->used_maps[0]), |
| 5477 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5478 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5479 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5480 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5481 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5482 | } |
| 5483 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5484 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5485 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5486 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5487 | |
| 5488 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 5489 | * bpf_ld_imm64 instructions |
| 5490 | */ |
| 5491 | convert_pseudo_ld_imm64(env); |
| 5492 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5493 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 5494 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5495 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 5496 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 5497 | * them now. Otherwise free_bpf_prog_info() will release them. |
| 5498 | */ |
| 5499 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 5500 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5501 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 5502 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 5503 | vfree(env->insn_aux_data); |
| 5504 | err_free_env: |
| 5505 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 5506 | return ret; |
| 5507 | } |