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 | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 23 | |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 24 | #include "disasm.h" |
| 25 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 26 | static const struct bpf_verifier_ops * const bpf_verifier_ops[] = { |
| 27 | #define BPF_PROG_TYPE(_id, _name) \ |
| 28 | [_id] = & _name ## _verifier_ops, |
| 29 | #define BPF_MAP_TYPE(_id, _ops) |
| 30 | #include <linux/bpf_types.h> |
| 31 | #undef BPF_PROG_TYPE |
| 32 | #undef BPF_MAP_TYPE |
| 33 | }; |
| 34 | |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 35 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 36 | * instruction by instruction and updates register/stack state. |
| 37 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 38 | * |
| 39 | * The first pass is depth-first-search to check that the program is a DAG. |
| 40 | * It rejects the following programs: |
| 41 | * - larger than BPF_MAXINSNS insns |
| 42 | * - if loop is present (detected via back-edge) |
| 43 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 44 | * - out of bounds or malformed jumps |
| 45 | * The second pass is all possible path descent from the 1st insn. |
| 46 | * 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] | 47 | * 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] | 48 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 49 | * Number of 'branches to be analyzed' is limited to 1k |
| 50 | * |
| 51 | * On entry to each instruction, each register has a type, and the instruction |
| 52 | * changes the types of the registers depending on instruction semantics. |
| 53 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 54 | * copied to R1. |
| 55 | * |
| 56 | * All registers are 64-bit. |
| 57 | * R0 - return register |
| 58 | * R1-R5 argument passing registers |
| 59 | * R6-R9 callee saved registers |
| 60 | * R10 - frame pointer read-only |
| 61 | * |
| 62 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 63 | * and has type PTR_TO_CTX. |
| 64 | * |
| 65 | * Verifier tracks arithmetic operations on pointers in case: |
| 66 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 67 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 68 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 69 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 70 | * that it wants to construct a pointer to some element within stack. |
| 71 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 72 | * (and -20 constant is saved for further stack bounds checking). |
| 73 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 74 | * |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 75 | * Most of the time the registers have SCALAR_VALUE type, which |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 76 | * 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] | 77 | * (like pointer plus pointer becomes SCALAR_VALUE type) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 78 | * |
| 79 | * When verifier sees load or store instructions the type of base register |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 80 | * 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] | 81 | * types recognized by check_mem_access() function. |
| 82 | * |
| 83 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 84 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 85 | * |
| 86 | * registers used to pass values to function calls are checked against |
| 87 | * function argument constraints. |
| 88 | * |
| 89 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 90 | * It means that the register type passed to this function must be |
| 91 | * PTR_TO_STACK and it will be used inside the function as |
| 92 | * 'pointer to map element key' |
| 93 | * |
| 94 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 95 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 96 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 97 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 98 | * |
| 99 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 100 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 101 | * 2nd argument should be a pointer to stack, which will be used inside |
| 102 | * the helper function as a pointer to map element key. |
| 103 | * |
| 104 | * On the kernel side the helper function looks like: |
| 105 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 106 | * { |
| 107 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 108 | * void *key = (void *) (unsigned long) r2; |
| 109 | * void *value; |
| 110 | * |
| 111 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 112 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 113 | * the stack of eBPF program. |
| 114 | * } |
| 115 | * |
| 116 | * Corresponding eBPF program may look like: |
| 117 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 118 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 119 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 120 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 121 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 122 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 123 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 124 | * |
| 125 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 126 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 127 | * and were initialized prior to this call. |
| 128 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 129 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 130 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 131 | * returns ether pointer to map value or NULL. |
| 132 | * |
| 133 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 134 | * insn, the register holding that pointer in the true branch changes state to |
| 135 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 136 | * branch. See check_cond_jmp_op(). |
| 137 | * |
| 138 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 139 | * are set to NOT_INIT to indicate that they are no longer readable. |
| 140 | */ |
| 141 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 142 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 143 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 144 | /* verifer state is 'st' |
| 145 | * before processing instruction 'insn_idx' |
| 146 | * and after processing instruction 'prev_insn_idx' |
| 147 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 148 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 149 | int insn_idx; |
| 150 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 151 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Edward Cree | 8e17c1b | 2017-08-07 15:30:30 +0100 | [diff] [blame] | 154 | #define BPF_COMPLEXITY_LIMIT_INSNS 131072 |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 155 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
| 156 | |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 157 | #define BPF_MAP_PTR_POISON ((void *)0xeB9F + POISON_POINTER_DELTA) |
| 158 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 159 | struct bpf_call_arg_meta { |
| 160 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 161 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 162 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 163 | int regno; |
| 164 | int access_size; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 165 | }; |
| 166 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 167 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 168 | |
| 169 | /* log_level controls verbosity level of eBPF verifier. |
| 170 | * verbose() is used to dump the verification trace to the log, so the user |
| 171 | * can figure out what's wrong with the program |
| 172 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 173 | static __printf(2, 3) void verbose(struct bpf_verifier_env *env, |
| 174 | const char *fmt, ...) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 175 | { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 176 | struct bpf_verifer_log *log = &env->log; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 177 | unsigned int n; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 178 | va_list args; |
| 179 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 180 | if (!log->level || !log->ubuf || bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 181 | return; |
| 182 | |
| 183 | va_start(args, fmt); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 184 | n = vscnprintf(log->kbuf, BPF_VERIFIER_TMP_LOG_SIZE, fmt, args); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 185 | va_end(args); |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 186 | |
| 187 | WARN_ONCE(n >= BPF_VERIFIER_TMP_LOG_SIZE - 1, |
| 188 | "verifier log line truncated - local buffer too short\n"); |
| 189 | |
| 190 | n = min(log->len_total - log->len_used - 1, n); |
| 191 | log->kbuf[n] = '\0'; |
| 192 | |
| 193 | if (!copy_to_user(log->ubuf + log->len_used, log->kbuf, n + 1)) |
| 194 | log->len_used += n; |
| 195 | else |
| 196 | log->ubuf = NULL; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 199 | static bool type_is_pkt_pointer(enum bpf_reg_type type) |
| 200 | { |
| 201 | return type == PTR_TO_PACKET || |
| 202 | type == PTR_TO_PACKET_META; |
| 203 | } |
| 204 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 205 | /* string representation of 'enum bpf_reg_type' */ |
| 206 | static const char * const reg_type_str[] = { |
| 207 | [NOT_INIT] = "?", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 208 | [SCALAR_VALUE] = "inv", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 209 | [PTR_TO_CTX] = "ctx", |
| 210 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 211 | [PTR_TO_MAP_VALUE] = "map_value", |
| 212 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 213 | [PTR_TO_STACK] = "fp", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 214 | [PTR_TO_PACKET] = "pkt", |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 215 | [PTR_TO_PACKET_META] = "pkt_meta", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 216 | [PTR_TO_PACKET_END] = "pkt_end", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 219 | static void print_liveness(struct bpf_verifier_env *env, |
| 220 | enum bpf_reg_liveness live) |
| 221 | { |
| 222 | if (live & (REG_LIVE_READ | REG_LIVE_WRITTEN)) |
| 223 | verbose(env, "_"); |
| 224 | if (live & REG_LIVE_READ) |
| 225 | verbose(env, "r"); |
| 226 | if (live & REG_LIVE_WRITTEN) |
| 227 | verbose(env, "w"); |
| 228 | } |
| 229 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 230 | static void print_verifier_state(struct bpf_verifier_env *env, |
| 231 | struct bpf_verifier_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 232 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 233 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 234 | enum bpf_reg_type t; |
| 235 | int i; |
| 236 | |
| 237 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 238 | reg = &state->regs[i]; |
| 239 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 240 | if (t == NOT_INIT) |
| 241 | continue; |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 242 | verbose(env, " R%d", i); |
| 243 | print_liveness(env, reg->live); |
| 244 | verbose(env, "=%s", reg_type_str[t]); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 245 | if ((t == SCALAR_VALUE || t == PTR_TO_STACK) && |
| 246 | tnum_is_const(reg->var_off)) { |
| 247 | /* reg->off should be 0 for SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 248 | verbose(env, "%lld", reg->var_off.value + reg->off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 249 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 250 | verbose(env, "(id=%d", reg->id); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 251 | if (t != SCALAR_VALUE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 252 | verbose(env, ",off=%d", reg->off); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 253 | if (type_is_pkt_pointer(t)) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 254 | verbose(env, ",r=%d", reg->range); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 255 | else if (t == CONST_PTR_TO_MAP || |
| 256 | t == PTR_TO_MAP_VALUE || |
| 257 | t == PTR_TO_MAP_VALUE_OR_NULL) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 258 | verbose(env, ",ks=%d,vs=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 259 | reg->map_ptr->key_size, |
| 260 | reg->map_ptr->value_size); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 261 | if (tnum_is_const(reg->var_off)) { |
| 262 | /* Typically an immediate SCALAR_VALUE, but |
| 263 | * could be a pointer whose offset is too big |
| 264 | * for reg->off |
| 265 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 266 | verbose(env, ",imm=%llx", reg->var_off.value); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 267 | } else { |
| 268 | if (reg->smin_value != reg->umin_value && |
| 269 | reg->smin_value != S64_MIN) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 270 | verbose(env, ",smin_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 271 | (long long)reg->smin_value); |
| 272 | if (reg->smax_value != reg->umax_value && |
| 273 | reg->smax_value != S64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 274 | verbose(env, ",smax_value=%lld", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 275 | (long long)reg->smax_value); |
| 276 | if (reg->umin_value != 0) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 277 | verbose(env, ",umin_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 278 | (unsigned long long)reg->umin_value); |
| 279 | if (reg->umax_value != U64_MAX) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 280 | verbose(env, ",umax_value=%llu", |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 281 | (unsigned long long)reg->umax_value); |
| 282 | if (!tnum_is_unknown(reg->var_off)) { |
| 283 | char tn_buf[48]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 284 | |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 285 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 286 | verbose(env, ",var_off=%s", tn_buf); |
Edward Cree | 7d1238f | 2017-08-07 15:26:56 +0100 | [diff] [blame] | 287 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 288 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 289 | verbose(env, ")"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 290 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 291 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 292 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 293 | if (state->stack[i].slot_type[0] == STACK_SPILL) { |
| 294 | verbose(env, " fp%d", |
| 295 | (-i - 1) * BPF_REG_SIZE); |
| 296 | print_liveness(env, state->stack[i].spilled_ptr.live); |
| 297 | verbose(env, "=%s", |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 298 | reg_type_str[state->stack[i].spilled_ptr.type]); |
Alexei Starovoitov | 4e92024 | 2017-11-30 21:31:36 -0800 | [diff] [blame] | 299 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 300 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 301 | verbose(env, "\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 304 | static int copy_stack_state(struct bpf_verifier_state *dst, |
| 305 | const struct bpf_verifier_state *src) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 306 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 307 | if (!src->stack) |
| 308 | return 0; |
| 309 | if (WARN_ON_ONCE(dst->allocated_stack < src->allocated_stack)) { |
| 310 | /* internal bug, make state invalid to reject the program */ |
| 311 | memset(dst, 0, sizeof(*dst)); |
| 312 | return -EFAULT; |
| 313 | } |
| 314 | memcpy(dst->stack, src->stack, |
| 315 | sizeof(*src->stack) * (src->allocated_stack / BPF_REG_SIZE)); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | /* do_check() starts with zero-sized stack in struct bpf_verifier_state to |
| 320 | * make it consume minimal amount of memory. check_stack_write() access from |
| 321 | * the program calls into realloc_verifier_state() to grow the stack size. |
| 322 | * Note there is a non-zero 'parent' pointer inside bpf_verifier_state |
| 323 | * which this function copies over. It points to previous bpf_verifier_state |
| 324 | * which is never reallocated |
| 325 | */ |
| 326 | static int realloc_verifier_state(struct bpf_verifier_state *state, int size, |
| 327 | bool copy_old) |
| 328 | { |
| 329 | u32 old_size = state->allocated_stack; |
| 330 | struct bpf_stack_state *new_stack; |
| 331 | int slot = size / BPF_REG_SIZE; |
| 332 | |
| 333 | if (size <= old_size || !size) { |
| 334 | if (copy_old) |
| 335 | return 0; |
| 336 | state->allocated_stack = slot * BPF_REG_SIZE; |
| 337 | if (!size && old_size) { |
| 338 | kfree(state->stack); |
| 339 | state->stack = NULL; |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | new_stack = kmalloc_array(slot, sizeof(struct bpf_stack_state), |
| 344 | GFP_KERNEL); |
| 345 | if (!new_stack) |
| 346 | return -ENOMEM; |
| 347 | if (copy_old) { |
| 348 | if (state->stack) |
| 349 | memcpy(new_stack, state->stack, |
| 350 | sizeof(*new_stack) * (old_size / BPF_REG_SIZE)); |
| 351 | memset(new_stack + old_size / BPF_REG_SIZE, 0, |
| 352 | sizeof(*new_stack) * (size - old_size) / BPF_REG_SIZE); |
| 353 | } |
| 354 | state->allocated_stack = slot * BPF_REG_SIZE; |
| 355 | kfree(state->stack); |
| 356 | state->stack = new_stack; |
| 357 | return 0; |
| 358 | } |
| 359 | |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 360 | static void free_verifier_state(struct bpf_verifier_state *state, |
| 361 | bool free_self) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 362 | { |
| 363 | kfree(state->stack); |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 364 | if (free_self) |
| 365 | kfree(state); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* copy verifier state from src to dst growing dst stack space |
| 369 | * when necessary to accommodate larger src stack |
| 370 | */ |
| 371 | static int copy_verifier_state(struct bpf_verifier_state *dst, |
| 372 | const struct bpf_verifier_state *src) |
| 373 | { |
| 374 | int err; |
| 375 | |
| 376 | err = realloc_verifier_state(dst, src->allocated_stack, false); |
| 377 | if (err) |
| 378 | return err; |
| 379 | memcpy(dst, src, offsetof(struct bpf_verifier_state, allocated_stack)); |
| 380 | return copy_stack_state(dst, src); |
| 381 | } |
| 382 | |
| 383 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx, |
| 384 | int *insn_idx) |
| 385 | { |
| 386 | struct bpf_verifier_state *cur = env->cur_state; |
| 387 | struct bpf_verifier_stack_elem *elem, *head = env->head; |
| 388 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 389 | |
| 390 | if (env->head == NULL) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 391 | return -ENOENT; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 392 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 393 | if (cur) { |
| 394 | err = copy_verifier_state(cur, &head->st); |
| 395 | if (err) |
| 396 | return err; |
| 397 | } |
| 398 | if (insn_idx) |
| 399 | *insn_idx = head->insn_idx; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 400 | if (prev_insn_idx) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 401 | *prev_insn_idx = head->prev_insn_idx; |
| 402 | elem = head->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 403 | free_verifier_state(&head->st, false); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 404 | kfree(head); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 405 | env->head = elem; |
| 406 | env->stack_size--; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 407 | return 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 410 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 411 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 412 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 413 | struct bpf_verifier_state *cur = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 414 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 415 | int err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 416 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 417 | elem = kzalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 418 | if (!elem) |
| 419 | goto err; |
| 420 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 421 | elem->insn_idx = insn_idx; |
| 422 | elem->prev_insn_idx = prev_insn_idx; |
| 423 | elem->next = env->head; |
| 424 | env->head = elem; |
| 425 | env->stack_size++; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 426 | err = copy_verifier_state(&elem->st, cur); |
| 427 | if (err) |
| 428 | goto err; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 429 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 430 | verbose(env, "BPF program is too complex\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 431 | goto err; |
| 432 | } |
| 433 | return &elem->st; |
| 434 | err: |
| 435 | /* pop all elements and return */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 436 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | #define CALLER_SAVED_REGS 6 |
| 441 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 442 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 443 | }; |
| 444 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 445 | static void __mark_reg_not_init(struct bpf_reg_state *reg); |
| 446 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 447 | /* Mark the unknown part of a register (variable offset or scalar value) as |
| 448 | * known to have the value @imm. |
| 449 | */ |
| 450 | static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm) |
| 451 | { |
| 452 | reg->id = 0; |
| 453 | reg->var_off = tnum_const(imm); |
| 454 | reg->smin_value = (s64)imm; |
| 455 | reg->smax_value = (s64)imm; |
| 456 | reg->umin_value = imm; |
| 457 | reg->umax_value = imm; |
| 458 | } |
| 459 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 460 | /* Mark the 'variable offset' part of a register as zero. This should be |
| 461 | * used only on registers holding a pointer type. |
| 462 | */ |
| 463 | static void __mark_reg_known_zero(struct bpf_reg_state *reg) |
| 464 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 465 | __mark_reg_known(reg, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 466 | } |
| 467 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 468 | static void mark_reg_known_zero(struct bpf_verifier_env *env, |
| 469 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 470 | { |
| 471 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 472 | verbose(env, "mark_reg_known_zero(regs, %u)\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 473 | /* Something bad happened, let's kill all regs */ |
| 474 | for (regno = 0; regno < MAX_BPF_REG; regno++) |
| 475 | __mark_reg_not_init(regs + regno); |
| 476 | return; |
| 477 | } |
| 478 | __mark_reg_known_zero(regs + regno); |
| 479 | } |
| 480 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 481 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 482 | { |
| 483 | return type_is_pkt_pointer(reg->type); |
| 484 | } |
| 485 | |
| 486 | static bool reg_is_pkt_pointer_any(const struct bpf_reg_state *reg) |
| 487 | { |
| 488 | return reg_is_pkt_pointer(reg) || |
| 489 | reg->type == PTR_TO_PACKET_END; |
| 490 | } |
| 491 | |
| 492 | /* Unmodified PTR_TO_PACKET[_META,_END] register from ctx access. */ |
| 493 | static bool reg_is_init_pkt_pointer(const struct bpf_reg_state *reg, |
| 494 | enum bpf_reg_type which) |
| 495 | { |
| 496 | /* The register can already have a range from prior markings. |
| 497 | * This is fine as long as it hasn't been advanced from its |
| 498 | * origin. |
| 499 | */ |
| 500 | return reg->type == which && |
| 501 | reg->id == 0 && |
| 502 | reg->off == 0 && |
| 503 | tnum_equals_const(reg->var_off, 0); |
| 504 | } |
| 505 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 506 | /* Attempts to improve min/max values based on var_off information */ |
| 507 | static void __update_reg_bounds(struct bpf_reg_state *reg) |
| 508 | { |
| 509 | /* min signed is max(sign bit) | min(other bits) */ |
| 510 | reg->smin_value = max_t(s64, reg->smin_value, |
| 511 | reg->var_off.value | (reg->var_off.mask & S64_MIN)); |
| 512 | /* max signed is min(sign bit) | max(other bits) */ |
| 513 | reg->smax_value = min_t(s64, reg->smax_value, |
| 514 | reg->var_off.value | (reg->var_off.mask & S64_MAX)); |
| 515 | reg->umin_value = max(reg->umin_value, reg->var_off.value); |
| 516 | reg->umax_value = min(reg->umax_value, |
| 517 | reg->var_off.value | reg->var_off.mask); |
| 518 | } |
| 519 | |
| 520 | /* Uses signed min/max values to inform unsigned, and vice-versa */ |
| 521 | static void __reg_deduce_bounds(struct bpf_reg_state *reg) |
| 522 | { |
| 523 | /* Learn sign from signed bounds. |
| 524 | * If we cannot cross the sign boundary, then signed and unsigned bounds |
| 525 | * are the same, so combine. This works even in the negative case, e.g. |
| 526 | * -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff. |
| 527 | */ |
| 528 | if (reg->smin_value >= 0 || reg->smax_value < 0) { |
| 529 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 530 | reg->umin_value); |
| 531 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 532 | reg->umax_value); |
| 533 | return; |
| 534 | } |
| 535 | /* Learn sign from unsigned bounds. Signed bounds cross the sign |
| 536 | * boundary, so we must be careful. |
| 537 | */ |
| 538 | if ((s64)reg->umax_value >= 0) { |
| 539 | /* Positive. We can't learn anything from the smin, but smax |
| 540 | * is positive, hence safe. |
| 541 | */ |
| 542 | reg->smin_value = reg->umin_value; |
| 543 | reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value, |
| 544 | reg->umax_value); |
| 545 | } else if ((s64)reg->umin_value < 0) { |
| 546 | /* Negative. We can't learn anything from the smax, but smin |
| 547 | * is negative, hence safe. |
| 548 | */ |
| 549 | reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value, |
| 550 | reg->umin_value); |
| 551 | reg->smax_value = reg->umax_value; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /* Attempts to improve var_off based on unsigned min/max information */ |
| 556 | static void __reg_bound_offset(struct bpf_reg_state *reg) |
| 557 | { |
| 558 | reg->var_off = tnum_intersect(reg->var_off, |
| 559 | tnum_range(reg->umin_value, |
| 560 | reg->umax_value)); |
| 561 | } |
| 562 | |
| 563 | /* Reset the min/max bounds of a register */ |
| 564 | static void __mark_reg_unbounded(struct bpf_reg_state *reg) |
| 565 | { |
| 566 | reg->smin_value = S64_MIN; |
| 567 | reg->smax_value = S64_MAX; |
| 568 | reg->umin_value = 0; |
| 569 | reg->umax_value = U64_MAX; |
| 570 | } |
| 571 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 572 | /* Mark a register as having a completely unknown (scalar) value. */ |
| 573 | static void __mark_reg_unknown(struct bpf_reg_state *reg) |
| 574 | { |
| 575 | reg->type = SCALAR_VALUE; |
| 576 | reg->id = 0; |
| 577 | reg->off = 0; |
| 578 | reg->var_off = tnum_unknown; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 579 | __mark_reg_unbounded(reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 582 | static void mark_reg_unknown(struct bpf_verifier_env *env, |
| 583 | struct bpf_reg_state *regs, u32 regno) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 584 | { |
| 585 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 586 | verbose(env, "mark_reg_unknown(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 587 | /* Something bad happened, let's kill all regs except FP */ |
| 588 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 589 | __mark_reg_not_init(regs + regno); |
| 590 | return; |
| 591 | } |
| 592 | __mark_reg_unknown(regs + regno); |
| 593 | } |
| 594 | |
| 595 | static void __mark_reg_not_init(struct bpf_reg_state *reg) |
| 596 | { |
| 597 | __mark_reg_unknown(reg); |
| 598 | reg->type = NOT_INIT; |
| 599 | } |
| 600 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 601 | static void mark_reg_not_init(struct bpf_verifier_env *env, |
| 602 | struct bpf_reg_state *regs, u32 regno) |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 603 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 604 | if (WARN_ON(regno >= MAX_BPF_REG)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 605 | verbose(env, "mark_reg_not_init(regs, %u)\n", regno); |
Alexei Starovoitov | 19ceb41 | 2017-11-30 21:31:37 -0800 | [diff] [blame] | 606 | /* Something bad happened, let's kill all regs except FP */ |
| 607 | for (regno = 0; regno < BPF_REG_FP; regno++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 608 | __mark_reg_not_init(regs + regno); |
| 609 | return; |
| 610 | } |
| 611 | __mark_reg_not_init(regs + regno); |
Daniel Borkmann | a9789ef | 2017-05-25 01:05:06 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 614 | static void init_reg_state(struct bpf_verifier_env *env, |
| 615 | struct bpf_reg_state *regs) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 616 | { |
| 617 | int i; |
| 618 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 619 | for (i = 0; i < MAX_BPF_REG; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 620 | mark_reg_not_init(env, regs, i); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 621 | regs[i].live = REG_LIVE_NONE; |
| 622 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 623 | |
| 624 | /* frame pointer */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 625 | regs[BPF_REG_FP].type = PTR_TO_STACK; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 626 | mark_reg_known_zero(env, regs, BPF_REG_FP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 627 | |
| 628 | /* 1st arg to a function */ |
| 629 | regs[BPF_REG_1].type = PTR_TO_CTX; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 630 | mark_reg_known_zero(env, regs, BPF_REG_1); |
Daniel Borkmann | 6760bf2 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 631 | } |
| 632 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 633 | enum reg_arg_type { |
| 634 | SRC_OP, /* register is used as source operand */ |
| 635 | DST_OP, /* register is used as destination operand */ |
| 636 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 637 | }; |
| 638 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 639 | static void mark_reg_read(const struct bpf_verifier_state *state, u32 regno) |
| 640 | { |
| 641 | struct bpf_verifier_state *parent = state->parent; |
| 642 | |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 643 | if (regno == BPF_REG_FP) |
| 644 | /* We don't need to worry about FP liveness because it's read-only */ |
| 645 | return; |
| 646 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 647 | while (parent) { |
| 648 | /* if read wasn't screened by an earlier write ... */ |
| 649 | if (state->regs[regno].live & REG_LIVE_WRITTEN) |
| 650 | break; |
| 651 | /* ... then we depend on parent's value */ |
| 652 | parent->regs[regno].live |= REG_LIVE_READ; |
| 653 | state = parent; |
| 654 | parent = state->parent; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 659 | enum reg_arg_type t) |
| 660 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 661 | struct bpf_reg_state *regs = env->cur_state->regs; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 662 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 663 | if (regno >= MAX_BPF_REG) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 664 | verbose(env, "R%d is invalid\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 665 | return -EINVAL; |
| 666 | } |
| 667 | |
| 668 | if (t == SRC_OP) { |
| 669 | /* check whether register used as source operand can be read */ |
| 670 | if (regs[regno].type == NOT_INIT) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 671 | verbose(env, "R%d !read_ok\n", regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 672 | return -EACCES; |
| 673 | } |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 674 | mark_reg_read(env->cur_state, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 675 | } else { |
| 676 | /* check whether register used as dest operand can be written to */ |
| 677 | if (regno == BPF_REG_FP) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 678 | verbose(env, "frame pointer is read only\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 679 | return -EACCES; |
| 680 | } |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 681 | regs[regno].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 682 | if (t == DST_OP) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 683 | mark_reg_unknown(env, regs, regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 684 | } |
| 685 | return 0; |
| 686 | } |
| 687 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 688 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 689 | { |
| 690 | switch (type) { |
| 691 | case PTR_TO_MAP_VALUE: |
| 692 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 693 | case PTR_TO_STACK: |
| 694 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 695 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 696 | case PTR_TO_PACKET_META: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 697 | case PTR_TO_PACKET_END: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 698 | case CONST_PTR_TO_MAP: |
| 699 | return true; |
| 700 | default: |
| 701 | return false; |
| 702 | } |
| 703 | } |
| 704 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 705 | /* check_stack_read/write functions track spill/fill of registers, |
| 706 | * stack boundary and alignment are checked in check_mem_access() |
| 707 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 708 | static int check_stack_write(struct bpf_verifier_env *env, |
| 709 | struct bpf_verifier_state *state, int off, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 710 | int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 711 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 712 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
| 713 | |
| 714 | err = realloc_verifier_state(state, round_up(slot + 1, BPF_REG_SIZE), |
| 715 | true); |
| 716 | if (err) |
| 717 | return err; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 718 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 719 | * so it's aligned access and [off, off + size) are within stack limits |
| 720 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 721 | if (!env->allow_ptr_leaks && |
| 722 | state->stack[spi].slot_type[0] == STACK_SPILL && |
| 723 | size != BPF_REG_SIZE) { |
| 724 | verbose(env, "attempt to corrupt spilled pointer on stack\n"); |
| 725 | return -EACCES; |
| 726 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 727 | |
| 728 | if (value_regno >= 0 && |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 729 | is_spillable_regtype(state->regs[value_regno].type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 730 | |
| 731 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 732 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 733 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 734 | return -EACCES; |
| 735 | } |
| 736 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 737 | /* save register state */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 738 | state->stack[spi].spilled_ptr = state->regs[value_regno]; |
| 739 | state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 740 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 741 | for (i = 0; i < BPF_REG_SIZE; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 742 | state->stack[spi].slot_type[i] = STACK_SPILL; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 743 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 744 | /* regular write of data into stack */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 745 | state->stack[spi].spilled_ptr = (struct bpf_reg_state) {}; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 746 | |
| 747 | for (i = 0; i < size; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 748 | state->stack[spi].slot_type[(slot - i) % BPF_REG_SIZE] = |
| 749 | STACK_MISC; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 750 | } |
| 751 | return 0; |
| 752 | } |
| 753 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 754 | static void mark_stack_slot_read(const struct bpf_verifier_state *state, int slot) |
| 755 | { |
| 756 | struct bpf_verifier_state *parent = state->parent; |
| 757 | |
| 758 | while (parent) { |
| 759 | /* if read wasn't screened by an earlier write ... */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 760 | if (state->stack[slot].spilled_ptr.live & REG_LIVE_WRITTEN) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 761 | break; |
| 762 | /* ... then we depend on parent's value */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 763 | parent->stack[slot].spilled_ptr.live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 764 | state = parent; |
| 765 | parent = state->parent; |
| 766 | } |
| 767 | } |
| 768 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 769 | static int check_stack_read(struct bpf_verifier_env *env, |
| 770 | struct bpf_verifier_state *state, int off, int size, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 771 | int value_regno) |
| 772 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 773 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE; |
| 774 | u8 *stype; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 775 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 776 | if (state->allocated_stack <= slot) { |
| 777 | verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 778 | off, size); |
| 779 | return -EACCES; |
| 780 | } |
| 781 | stype = state->stack[spi].slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 782 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 783 | if (stype[0] == STACK_SPILL) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 784 | if (size != BPF_REG_SIZE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 785 | verbose(env, "invalid size of register spill\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 786 | return -EACCES; |
| 787 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 788 | for (i = 1; i < BPF_REG_SIZE; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 789 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_SPILL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 790 | verbose(env, "corrupted spill memory\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 791 | return -EACCES; |
| 792 | } |
| 793 | } |
| 794 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 795 | if (value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 796 | /* restore register state from stack */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 797 | state->regs[value_regno] = state->stack[spi].spilled_ptr; |
Alexei Starovoitov | 2f18f62 | 2017-11-30 21:31:38 -0800 | [diff] [blame] | 798 | /* mark reg as written since spilled pointer state likely |
| 799 | * has its liveness marks cleared by is_state_visited() |
| 800 | * which resets stack/reg liveness for state transitions |
| 801 | */ |
| 802 | state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 803 | mark_stack_slot_read(state, spi); |
| 804 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 805 | return 0; |
| 806 | } else { |
| 807 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 808 | if (stype[(slot - i) % BPF_REG_SIZE] != STACK_MISC) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 809 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 810 | off, i, size); |
| 811 | return -EACCES; |
| 812 | } |
| 813 | } |
| 814 | if (value_regno >= 0) |
| 815 | /* have read misc data from the stack */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 816 | mark_reg_unknown(env, state->regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 817 | return 0; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 822 | 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] | 823 | int size, bool zero_size_allowed) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 824 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 825 | struct bpf_reg_state *regs = cur_regs(env); |
| 826 | struct bpf_map *map = regs[regno].map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 827 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 828 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 829 | off + size > map->value_size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 830 | 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] | 831 | map->value_size, off, size); |
| 832 | return -EACCES; |
| 833 | } |
| 834 | return 0; |
| 835 | } |
| 836 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 837 | /* check read/write into a map element with possible variable offset */ |
| 838 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 839 | int off, int size, bool zero_size_allowed) |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 840 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 841 | struct bpf_verifier_state *state = env->cur_state; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 842 | struct bpf_reg_state *reg = &state->regs[regno]; |
| 843 | int err; |
| 844 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 845 | /* We may have adjusted the register to this map value, so we |
| 846 | * need to try adding each of min_value and max_value to off |
| 847 | * to make sure our theoretical access will be safe. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 848 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 849 | if (env->log.level) |
| 850 | print_verifier_state(env, state); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 851 | /* The minimum value is only important with signed |
| 852 | * comparisons where we can't assume the floor of a |
| 853 | * value is 0. If we are using signed variables for our |
| 854 | * index'es we need to make sure that whatever we use |
| 855 | * will have a set floor within our range. |
| 856 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 857 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 858 | 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] | 859 | regno); |
| 860 | return -EACCES; |
| 861 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 862 | err = __check_map_access(env, regno, reg->smin_value + off, size, |
| 863 | zero_size_allowed); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 864 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 865 | verbose(env, "R%d min value is outside of the array range\n", |
| 866 | regno); |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 867 | return err; |
| 868 | } |
| 869 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 870 | /* If we haven't set a max value then we need to bail since we can't be |
| 871 | * sure we won't do bad things. |
| 872 | * If reg->umax_value + off could overflow, treat that as unbounded too. |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 873 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 874 | if (reg->umax_value >= BPF_MAX_VAR_OFF) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 875 | 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] | 876 | regno); |
| 877 | return -EACCES; |
| 878 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 879 | err = __check_map_access(env, regno, reg->umax_value + off, size, |
| 880 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 881 | if (err) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 882 | verbose(env, "R%d max value is outside of the array range\n", |
| 883 | regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 884 | return err; |
Gianluca Borello | dbcfe5f | 2017-01-09 10:19:46 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 887 | #define MAX_PACKET_OFF 0xffff |
| 888 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 889 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 890 | const struct bpf_call_arg_meta *meta, |
| 891 | enum bpf_access_type t) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 892 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 893 | switch (env->prog->type) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 894 | case BPF_PROG_TYPE_LWT_IN: |
| 895 | case BPF_PROG_TYPE_LWT_OUT: |
| 896 | /* dst_input() and dst_output() can't write for now */ |
| 897 | if (t == BPF_WRITE) |
| 898 | return false; |
Alexander Alemayhu | 7e57fbb | 2017-02-14 00:02:35 +0100 | [diff] [blame] | 899 | /* fallthrough */ |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 900 | case BPF_PROG_TYPE_SCHED_CLS: |
| 901 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 902 | case BPF_PROG_TYPE_XDP: |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 903 | case BPF_PROG_TYPE_LWT_XMIT: |
John Fastabend | 8a31db5 | 2017-08-15 22:33:09 -0700 | [diff] [blame] | 904 | case BPF_PROG_TYPE_SK_SKB: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 905 | if (meta) |
| 906 | return meta->pkt_access; |
| 907 | |
| 908 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 909 | return true; |
| 910 | default: |
| 911 | return false; |
| 912 | } |
| 913 | } |
| 914 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 915 | static int __check_packet_access(struct bpf_verifier_env *env, u32 regno, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 916 | int off, int size, bool zero_size_allowed) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 917 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 918 | struct bpf_reg_state *regs = cur_regs(env); |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 919 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 920 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 921 | if (off < 0 || size < 0 || (size == 0 && !zero_size_allowed) || |
| 922 | (u64)off + size > reg->range) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 923 | 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] | 924 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 925 | return -EACCES; |
| 926 | } |
| 927 | return 0; |
| 928 | } |
| 929 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 930 | 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] | 931 | int size, bool zero_size_allowed) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 932 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 933 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 934 | struct bpf_reg_state *reg = ®s[regno]; |
| 935 | int err; |
| 936 | |
| 937 | /* We may have added a variable offset to the packet pointer; but any |
| 938 | * reg->range we have comes after that. We are only checking the fixed |
| 939 | * offset. |
| 940 | */ |
| 941 | |
| 942 | /* We don't allow negative numbers, because we aren't tracking enough |
| 943 | * detail to prove they're safe. |
| 944 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 945 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 946 | 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] | 947 | regno); |
| 948 | return -EACCES; |
| 949 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 950 | err = __check_packet_access(env, regno, off, size, zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 951 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 952 | verbose(env, "R%d offset is outside of the packet\n", regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 953 | return err; |
| 954 | } |
| 955 | return err; |
| 956 | } |
| 957 | |
| 958 | /* check access to 'struct bpf_context' fields. Supports fixed offsets only */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 959 | 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] | 960 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 961 | { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 962 | struct bpf_insn_access_aux info = { |
| 963 | .reg_type = *reg_type, |
| 964 | }; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 965 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 966 | if (env->ops->is_valid_access && |
| 967 | env->ops->is_valid_access(off, size, t, &info)) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 968 | /* A non zero info.ctx_field_size indicates that this field is a |
| 969 | * candidate for later verifier transformation to load the whole |
| 970 | * field and then apply a mask when accessed with a narrower |
| 971 | * access than actual ctx access size. A zero info.ctx_field_size |
| 972 | * will only allow for whole field access and rejects any other |
| 973 | * type of narrower access. |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 974 | */ |
Yonghong Song | 2399463 | 2017-06-22 15:07:39 -0700 | [diff] [blame] | 975 | *reg_type = info.reg_type; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 976 | |
Jakub Kicinski | 4f9218a | 2017-10-16 16:40:55 -0700 | [diff] [blame] | 977 | 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] | 978 | /* remember the offset of last byte accessed in ctx */ |
| 979 | if (env->prog->aux->max_ctx_offset < off + size) |
| 980 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 981 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 982 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 983 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 984 | 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] | 985 | return -EACCES; |
| 986 | } |
| 987 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 988 | static bool __is_pointer_value(bool allow_ptr_leaks, |
| 989 | const struct bpf_reg_state *reg) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 990 | { |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 991 | if (allow_ptr_leaks) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 992 | return false; |
| 993 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 994 | return reg->type != SCALAR_VALUE; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 997 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 998 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 999 | return __is_pointer_value(env->allow_ptr_leaks, cur_regs(env) + regno); |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1002 | static int check_pkt_ptr_alignment(struct bpf_verifier_env *env, |
| 1003 | const struct bpf_reg_state *reg, |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1004 | int off, int size, bool strict) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1005 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1006 | struct tnum reg_off; |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1007 | int ip_align; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1008 | |
| 1009 | /* Byte size accesses are always allowed. */ |
| 1010 | if (!strict || size == 1) |
| 1011 | return 0; |
| 1012 | |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1013 | /* For platforms that do not have a Kconfig enabling |
| 1014 | * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS the value of |
| 1015 | * NET_IP_ALIGN is universally set to '2'. And on platforms |
| 1016 | * that do set CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS, we get |
| 1017 | * to this code only in strict mode where we want to emulate |
| 1018 | * the NET_IP_ALIGN==2 checking. Therefore use an |
| 1019 | * unconditional IP align value of '2'. |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1020 | */ |
David S. Miller | e4eda88 | 2017-05-22 12:27:07 -0400 | [diff] [blame] | 1021 | ip_align = 2; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1022 | |
| 1023 | reg_off = tnum_add(reg->var_off, tnum_const(ip_align + reg->off + off)); |
| 1024 | if (!tnum_is_aligned(reg_off, size)) { |
| 1025 | char tn_buf[48]; |
| 1026 | |
| 1027 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1028 | verbose(env, |
| 1029 | "misaligned packet access off %d+%s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1030 | ip_align, tn_buf, reg->off, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1031 | return -EACCES; |
| 1032 | } |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1033 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1034 | return 0; |
| 1035 | } |
| 1036 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1037 | static int check_generic_ptr_alignment(struct bpf_verifier_env *env, |
| 1038 | const struct bpf_reg_state *reg, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1039 | const char *pointer_desc, |
| 1040 | int off, int size, bool strict) |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1041 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1042 | struct tnum reg_off; |
| 1043 | |
| 1044 | /* Byte size accesses are always allowed. */ |
| 1045 | if (!strict || size == 1) |
| 1046 | return 0; |
| 1047 | |
| 1048 | reg_off = tnum_add(reg->var_off, tnum_const(reg->off + off)); |
| 1049 | if (!tnum_is_aligned(reg_off, size)) { |
| 1050 | char tn_buf[48]; |
| 1051 | |
| 1052 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1053 | verbose(env, "misaligned %saccess off %s+%d+%d size %d\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1054 | pointer_desc, tn_buf, reg->off, off, size); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1055 | return -EACCES; |
| 1056 | } |
| 1057 | |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1061 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
| 1062 | const struct bpf_reg_state *reg, |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1063 | int off, int size) |
| 1064 | { |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1065 | bool strict = env->strict_alignment; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1066 | const char *pointer_desc = ""; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1067 | |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1068 | switch (reg->type) { |
| 1069 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1070 | case PTR_TO_PACKET_META: |
| 1071 | /* Special case, because of NET_IP_ALIGN. Given metadata sits |
| 1072 | * right in front, treat it the very same way. |
| 1073 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1074 | return check_pkt_ptr_alignment(env, reg, off, size, strict); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1075 | case PTR_TO_MAP_VALUE: |
| 1076 | pointer_desc = "value "; |
| 1077 | break; |
| 1078 | case PTR_TO_CTX: |
| 1079 | pointer_desc = "context "; |
| 1080 | break; |
| 1081 | case PTR_TO_STACK: |
| 1082 | pointer_desc = "stack "; |
| 1083 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1084 | default: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1085 | break; |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1086 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1087 | return check_generic_ptr_alignment(env, reg, pointer_desc, off, size, |
| 1088 | strict); |
Daniel Borkmann | 79adffc | 2017-03-31 02:24:03 +0200 | [diff] [blame] | 1089 | } |
| 1090 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1091 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 1092 | * if t==write, value_regno is a register which value is stored into memory |
| 1093 | * if t==read, value_regno is a register which will receive the value from memory |
| 1094 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 1095 | * if t==read && value_regno==-1, don't care what we read from memory |
| 1096 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1097 | 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] | 1098 | int bpf_size, enum bpf_access_type t, |
| 1099 | int value_regno) |
| 1100 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1101 | struct bpf_verifier_state *state = env->cur_state; |
| 1102 | struct bpf_reg_state *regs = cur_regs(env); |
| 1103 | struct bpf_reg_state *reg = regs + regno; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1104 | int size, err = 0; |
| 1105 | |
| 1106 | size = bpf_size_to_bytes(bpf_size); |
| 1107 | if (size < 0) |
| 1108 | return size; |
| 1109 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1110 | /* alignment checks will add in reg->off themselves */ |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 1111 | err = check_ptr_alignment(env, reg, off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1112 | if (err) |
| 1113 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1114 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1115 | /* for access checks, reg->off is just part of off */ |
| 1116 | off += reg->off; |
| 1117 | |
| 1118 | if (reg->type == PTR_TO_MAP_VALUE) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1119 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1120 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1121 | verbose(env, "R%d leaks addr into map\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1122 | return -EACCES; |
| 1123 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1124 | |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1125 | err = check_map_access(env, regno, off, size, false); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1126 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1127 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1128 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1129 | } else if (reg->type == PTR_TO_CTX) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1130 | enum bpf_reg_type reg_type = SCALAR_VALUE; |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 1131 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1132 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1133 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1134 | verbose(env, "R%d leaks addr into ctx\n", value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1135 | return -EACCES; |
| 1136 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1137 | /* ctx accesses must be at a fixed offset, so that we can |
| 1138 | * determine what type of data were returned. |
| 1139 | */ |
Jakub Kicinski | 28e33f9 | 2017-10-16 11:16:55 -0700 | [diff] [blame] | 1140 | if (reg->off) { |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 1141 | verbose(env, |
| 1142 | "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] | 1143 | regno, reg->off, off - reg->off); |
| 1144 | return -EACCES; |
| 1145 | } |
| 1146 | if (!tnum_is_const(reg->var_off) || reg->var_off.value) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1147 | char tn_buf[48]; |
| 1148 | |
| 1149 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1150 | verbose(env, |
| 1151 | "variable ctx access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1152 | tn_buf, off, size); |
| 1153 | return -EACCES; |
| 1154 | } |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1155 | err = check_ctx_access(env, insn_idx, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1156 | if (!err && t == BPF_READ && value_regno >= 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1157 | /* ctx access returns either a scalar, or a |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1158 | * PTR_TO_PACKET[_META,_END]. In the latter |
| 1159 | * case, we know the offset is zero. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1160 | */ |
| 1161 | if (reg_type == SCALAR_VALUE) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1162 | mark_reg_unknown(env, regs, value_regno); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1163 | else |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1164 | mark_reg_known_zero(env, regs, |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1165 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1166 | regs[value_regno].id = 0; |
| 1167 | regs[value_regno].off = 0; |
| 1168 | regs[value_regno].range = 0; |
| 1169 | regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1170 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1171 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1172 | } else if (reg->type == PTR_TO_STACK) { |
| 1173 | /* stack accesses must be at a fixed offset, so that we can |
| 1174 | * determine what type of data were returned. |
| 1175 | * See check_stack_read(). |
| 1176 | */ |
| 1177 | if (!tnum_is_const(reg->var_off)) { |
| 1178 | char tn_buf[48]; |
| 1179 | |
| 1180 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1181 | verbose(env, "variable stack access var_off=%s off=%d size=%d", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1182 | tn_buf, off, size); |
| 1183 | return -EACCES; |
| 1184 | } |
| 1185 | off += reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1186 | if (off >= 0 || off < -MAX_BPF_STACK) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1187 | verbose(env, "invalid stack off=%d size=%d\n", off, |
| 1188 | size); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1189 | return -EACCES; |
| 1190 | } |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1191 | |
| 1192 | if (env->prog->aux->stack_depth < -off) |
| 1193 | env->prog->aux->stack_depth = -off; |
| 1194 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1195 | if (t == BPF_WRITE) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1196 | err = check_stack_write(env, state, off, size, |
| 1197 | value_regno); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1198 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1199 | err = check_stack_read(env, state, off, size, |
| 1200 | value_regno); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1201 | } else if (reg_is_pkt_pointer(reg)) { |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1202 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1203 | verbose(env, "cannot write into packet\n"); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1204 | return -EACCES; |
| 1205 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1206 | if (t == BPF_WRITE && value_regno >= 0 && |
| 1207 | is_pointer_value(env, value_regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1208 | verbose(env, "R%d leaks addr into packet\n", |
| 1209 | value_regno); |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 1210 | return -EACCES; |
| 1211 | } |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1212 | err = check_packet_access(env, regno, off, size, false); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1213 | if (!err && t == BPF_READ && value_regno >= 0) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1214 | mark_reg_unknown(env, regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1215 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1216 | verbose(env, "R%d invalid mem access '%s'\n", regno, |
| 1217 | reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1218 | return -EACCES; |
| 1219 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1220 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1221 | if (!err && size < BPF_REG_SIZE && value_regno >= 0 && t == BPF_READ && |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1222 | regs[value_regno].type == SCALAR_VALUE) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1223 | /* b/h/w load zero-extends, mark upper bits as known 0 */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1224 | regs[value_regno].var_off = |
| 1225 | tnum_cast(regs[value_regno].var_off, size); |
| 1226 | __update_reg_bounds(®s[value_regno]); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1227 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1228 | return err; |
| 1229 | } |
| 1230 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1231 | 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] | 1232 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1233 | int err; |
| 1234 | |
| 1235 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 1236 | insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1237 | verbose(env, "BPF_XADD uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1238 | return -EINVAL; |
| 1239 | } |
| 1240 | |
| 1241 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1242 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1243 | if (err) |
| 1244 | return err; |
| 1245 | |
| 1246 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1247 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1248 | if (err) |
| 1249 | return err; |
| 1250 | |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1251 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1252 | verbose(env, "R%d leaks addr into mem\n", insn->src_reg); |
Daniel Borkmann | 6bdf6ab | 2017-06-29 03:04:59 +0200 | [diff] [blame] | 1253 | return -EACCES; |
| 1254 | } |
| 1255 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1256 | /* check whether atomic_add can read the memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1257 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1258 | BPF_SIZE(insn->code), BPF_READ, -1); |
| 1259 | if (err) |
| 1260 | return err; |
| 1261 | |
| 1262 | /* check whether atomic_add can write into the same memory */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1263 | return check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1264 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
| 1265 | } |
| 1266 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1267 | /* Does this register contain a constant zero? */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1268 | static bool register_is_null(struct bpf_reg_state *reg) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1269 | { |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1270 | return reg->type == SCALAR_VALUE && tnum_equals_const(reg->var_off, 0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1271 | } |
| 1272 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1273 | /* when register 'regno' is passed into function that will read 'access_size' |
| 1274 | * bytes from that pointer, make sure that it's within stack boundary |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1275 | * and all elements of stack are initialized. |
| 1276 | * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 1277 | * 'off' argument, so it has to add in reg->off itself. |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1278 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1279 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1280 | int access_size, bool zero_size_allowed, |
| 1281 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1282 | { |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1283 | struct bpf_reg_state *reg = cur_regs(env) + regno; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1284 | struct bpf_verifier_state *state = env->cur_state; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1285 | int off, i, slot, spi; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1286 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1287 | if (reg->type != PTR_TO_STACK) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1288 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1289 | if (zero_size_allowed && access_size == 0 && |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1290 | register_is_null(reg)) |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1291 | return 0; |
| 1292 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1293 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1294 | reg_type_str[reg->type], |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1295 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1296 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1297 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1298 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1299 | /* Only allow fixed-offset stack reads */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1300 | if (!tnum_is_const(reg->var_off)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1301 | char tn_buf[48]; |
| 1302 | |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1303 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1304 | verbose(env, "invalid variable stack read R%d var_off=%s\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1305 | regno, tn_buf); |
| 1306 | } |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1307 | off = reg->off + reg->var_off.value; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1308 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1309 | access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1310 | 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] | 1311 | regno, off, access_size); |
| 1312 | return -EACCES; |
| 1313 | } |
| 1314 | |
Alexei Starovoitov | 8726679 | 2017-05-30 13:31:29 -0700 | [diff] [blame] | 1315 | if (env->prog->aux->stack_depth < -off) |
| 1316 | env->prog->aux->stack_depth = -off; |
| 1317 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1318 | if (meta && meta->raw_mode) { |
| 1319 | meta->access_size = access_size; |
| 1320 | meta->regno = regno; |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1324 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1325 | slot = -(off + i) - 1; |
| 1326 | spi = slot / BPF_REG_SIZE; |
| 1327 | if (state->allocated_stack <= slot || |
| 1328 | state->stack[spi].slot_type[slot % BPF_REG_SIZE] != |
| 1329 | STACK_MISC) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1330 | verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1331 | off, i, access_size); |
| 1332 | return -EACCES; |
| 1333 | } |
| 1334 | } |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1338 | static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, |
| 1339 | int access_size, bool zero_size_allowed, |
| 1340 | struct bpf_call_arg_meta *meta) |
| 1341 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1342 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1343 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1344 | switch (reg->type) { |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1345 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1346 | case PTR_TO_PACKET_META: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1347 | return check_packet_access(env, regno, reg->off, access_size, |
| 1348 | zero_size_allowed); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1349 | case PTR_TO_MAP_VALUE: |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1350 | return check_map_access(env, regno, reg->off, access_size, |
| 1351 | zero_size_allowed); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1352 | default: /* scalar_value|ptr_to_stack or invalid ptr */ |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1353 | return check_stack_boundary(env, regno, access_size, |
| 1354 | zero_size_allowed, meta); |
| 1355 | } |
| 1356 | } |
| 1357 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1358 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1359 | enum bpf_arg_type arg_type, |
| 1360 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1361 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1362 | struct bpf_reg_state *regs = cur_regs(env), *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1363 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1364 | int err = 0; |
| 1365 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1366 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1367 | return 0; |
| 1368 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1369 | err = check_reg_arg(env, regno, SRC_OP); |
| 1370 | if (err) |
| 1371 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1372 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1373 | if (arg_type == ARG_ANYTHING) { |
| 1374 | if (is_pointer_value(env, regno)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1375 | verbose(env, "R%d leaks addr into helper function\n", |
| 1376 | regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1377 | return -EACCES; |
| 1378 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1379 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1380 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 1381 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1382 | if (type_is_pkt_pointer(type) && |
Thomas Graf | 3a0af8f | 2016-11-30 17:10:10 +0100 | [diff] [blame] | 1383 | !may_access_direct_pkt_data(env, meta, BPF_READ)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1384 | verbose(env, "helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1385 | return -EACCES; |
| 1386 | } |
| 1387 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1388 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1389 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1390 | expected_type = PTR_TO_STACK; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1391 | if (!type_is_pkt_pointer(type) && |
| 1392 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1393 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1394 | } else if (arg_type == ARG_CONST_SIZE || |
| 1395 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1396 | expected_type = SCALAR_VALUE; |
| 1397 | if (type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1398 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1399 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 1400 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1401 | if (type != expected_type) |
| 1402 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 1403 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 1404 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1405 | if (type != expected_type) |
| 1406 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1407 | } else if (arg_type == ARG_PTR_TO_MEM || |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 1408 | arg_type == ARG_PTR_TO_MEM_OR_NULL || |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1409 | arg_type == ARG_PTR_TO_UNINIT_MEM) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1410 | expected_type = PTR_TO_STACK; |
| 1411 | /* One exception here. In case function allows for NULL to be |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1412 | * passed in as argument, it's a SCALAR_VALUE type. Final test |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1413 | * happens during stack boundary checking. |
| 1414 | */ |
Alexei Starovoitov | 914cb78 | 2017-11-30 21:31:40 -0800 | [diff] [blame] | 1415 | if (register_is_null(reg) && |
Gianluca Borello | db1ac49 | 2017-11-22 18:32:53 +0000 | [diff] [blame] | 1416 | arg_type == ARG_PTR_TO_MEM_OR_NULL) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1417 | /* final test in check_stack_boundary() */; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1418 | else if (!type_is_pkt_pointer(type) && |
| 1419 | type != PTR_TO_MAP_VALUE && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1420 | type != expected_type) |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1421 | goto err_type; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1422 | meta->raw_mode = arg_type == ARG_PTR_TO_UNINIT_MEM; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1423 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1424 | verbose(env, "unsupported arg_type %d\n", arg_type); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1425 | return -EFAULT; |
| 1426 | } |
| 1427 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1428 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 1429 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1430 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1431 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 1432 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 1433 | * check that [key, key + map->key_size) are within |
| 1434 | * stack limits and initialized |
| 1435 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1436 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1437 | /* in function declaration map_ptr must come before |
| 1438 | * map_key, so that it's verified and known before |
| 1439 | * we have to check map_key here. Otherwise it means |
| 1440 | * that kernel subsystem misconfigured verifier |
| 1441 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1442 | verbose(env, "invalid map_ptr to access map->key\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1443 | return -EACCES; |
| 1444 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1445 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1446 | err = check_packet_access(env, regno, reg->off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1447 | meta->map_ptr->key_size, |
| 1448 | false); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1449 | else |
| 1450 | err = check_stack_boundary(env, regno, |
| 1451 | meta->map_ptr->key_size, |
| 1452 | false, NULL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1453 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1454 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 1455 | * check [value, value + map->value_size) validity |
| 1456 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1457 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1458 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1459 | verbose(env, "invalid map_ptr to access map->value\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1460 | return -EACCES; |
| 1461 | } |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1462 | if (type_is_pkt_pointer(type)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1463 | err = check_packet_access(env, regno, reg->off, |
Yonghong Song | 9fd29c0 | 2017-11-12 14:49:09 -0800 | [diff] [blame] | 1464 | meta->map_ptr->value_size, |
| 1465 | false); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1466 | else |
| 1467 | err = check_stack_boundary(env, regno, |
| 1468 | meta->map_ptr->value_size, |
| 1469 | false, NULL); |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1470 | } else if (arg_type == ARG_CONST_SIZE || |
| 1471 | arg_type == ARG_CONST_SIZE_OR_ZERO) { |
| 1472 | bool zero_size_allowed = (arg_type == ARG_CONST_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1473 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1474 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
| 1475 | * from stack pointer 'buf'. Check it |
| 1476 | * note: regno == len, regno - 1 == buf |
| 1477 | */ |
| 1478 | if (regno == 0) { |
| 1479 | /* kernel subsystem misconfigured verifier */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1480 | verbose(env, |
| 1481 | "ARG_CONST_SIZE cannot be first argument\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1482 | return -EACCES; |
| 1483 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1484 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1485 | /* The register is SCALAR_VALUE; the access check |
| 1486 | * happens using its boundaries. |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1487 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1488 | |
| 1489 | if (!tnum_is_const(reg->var_off)) |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1490 | /* For unprivileged variable accesses, disable raw |
| 1491 | * mode so that the program is required to |
| 1492 | * initialize all the memory that the helper could |
| 1493 | * just partially fill up. |
| 1494 | */ |
| 1495 | meta = NULL; |
| 1496 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1497 | if (reg->smin_value < 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1498 | 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] | 1499 | regno); |
| 1500 | return -EACCES; |
| 1501 | } |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1502 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1503 | if (reg->umin_value == 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1504 | err = check_helper_mem_access(env, regno - 1, 0, |
| 1505 | zero_size_allowed, |
| 1506 | meta); |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1507 | if (err) |
| 1508 | return err; |
Gianluca Borello | 06c1c04 | 2017-01-09 10:19:49 -0800 | [diff] [blame] | 1509 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1510 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1511 | if (reg->umax_value >= BPF_MAX_VAR_SIZ) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1512 | 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] | 1513 | regno); |
| 1514 | return -EACCES; |
| 1515 | } |
| 1516 | err = check_helper_mem_access(env, regno - 1, |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1517 | reg->umax_value, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1518 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1522 | err_type: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1523 | verbose(env, "R%d type=%s expected=%s\n", regno, |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1524 | reg_type_str[type], reg_type_str[expected_type]); |
| 1525 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1528 | static int check_map_func_compatibility(struct bpf_verifier_env *env, |
| 1529 | struct bpf_map *map, int func_id) |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1530 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1531 | if (!map) |
| 1532 | return 0; |
| 1533 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1534 | /* We need a two way check, first is from map perspective ... */ |
| 1535 | switch (map->map_type) { |
| 1536 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 1537 | if (func_id != BPF_FUNC_tail_call) |
| 1538 | goto error; |
| 1539 | break; |
| 1540 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 1541 | if (func_id != BPF_FUNC_perf_event_read && |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 1542 | func_id != BPF_FUNC_perf_event_output && |
| 1543 | func_id != BPF_FUNC_perf_event_read_value) |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1544 | goto error; |
| 1545 | break; |
| 1546 | case BPF_MAP_TYPE_STACK_TRACE: |
| 1547 | if (func_id != BPF_FUNC_get_stackid) |
| 1548 | goto error; |
| 1549 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 1550 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 1551 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 1552 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1553 | goto error; |
| 1554 | break; |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 1555 | /* devmap returns a pointer to a live net_device ifindex that we cannot |
| 1556 | * allow to be modified from bpf side. So do not allow lookup elements |
| 1557 | * for now. |
| 1558 | */ |
| 1559 | case BPF_MAP_TYPE_DEVMAP: |
John Fastabend | 2ddf71e | 2017-07-17 09:30:02 -0700 | [diff] [blame] | 1560 | if (func_id != BPF_FUNC_redirect_map) |
John Fastabend | 546ac1f | 2017-07-17 09:28:56 -0700 | [diff] [blame] | 1561 | goto error; |
| 1562 | break; |
Jesper Dangaard Brouer | 6710e11 | 2017-10-16 12:19:28 +0200 | [diff] [blame] | 1563 | /* Restrict bpf side of cpumap, open when use-cases appear */ |
| 1564 | case BPF_MAP_TYPE_CPUMAP: |
| 1565 | if (func_id != BPF_FUNC_redirect_map) |
| 1566 | goto error; |
| 1567 | break; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 1568 | case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 1569 | case BPF_MAP_TYPE_HASH_OF_MAPS: |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 1570 | if (func_id != BPF_FUNC_map_lookup_elem) |
| 1571 | goto error; |
Martin KaFai Lau | 16a4362 | 2017-08-17 18:14:43 -0700 | [diff] [blame] | 1572 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1573 | case BPF_MAP_TYPE_SOCKMAP: |
| 1574 | if (func_id != BPF_FUNC_sk_redirect_map && |
| 1575 | func_id != BPF_FUNC_sock_map_update && |
| 1576 | func_id != BPF_FUNC_map_delete_elem) |
| 1577 | goto error; |
| 1578 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1579 | default: |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | /* ... and second from the function itself. */ |
| 1584 | switch (func_id) { |
| 1585 | case BPF_FUNC_tail_call: |
| 1586 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 1587 | goto error; |
| 1588 | break; |
| 1589 | case BPF_FUNC_perf_event_read: |
| 1590 | case BPF_FUNC_perf_event_output: |
Yonghong Song | 908432c | 2017-10-05 09:19:20 -0700 | [diff] [blame] | 1591 | case BPF_FUNC_perf_event_read_value: |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1592 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 1593 | goto error; |
| 1594 | break; |
| 1595 | case BPF_FUNC_get_stackid: |
| 1596 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 1597 | goto error; |
| 1598 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 1599 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 1600 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1601 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 1602 | goto error; |
| 1603 | break; |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 1604 | case BPF_FUNC_redirect_map: |
Jesper Dangaard Brouer | 9c270af | 2017-10-16 12:19:34 +0200 | [diff] [blame] | 1605 | if (map->map_type != BPF_MAP_TYPE_DEVMAP && |
| 1606 | map->map_type != BPF_MAP_TYPE_CPUMAP) |
John Fastabend | 97f91a7 | 2017-07-17 09:29:18 -0700 | [diff] [blame] | 1607 | goto error; |
| 1608 | break; |
John Fastabend | 174a79f | 2017-08-15 22:32:47 -0700 | [diff] [blame] | 1609 | case BPF_FUNC_sk_redirect_map: |
| 1610 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 1611 | goto error; |
| 1612 | break; |
| 1613 | case BPF_FUNC_sock_map_update: |
| 1614 | if (map->map_type != BPF_MAP_TYPE_SOCKMAP) |
| 1615 | goto error; |
| 1616 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1617 | default: |
| 1618 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1622 | error: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1623 | verbose(env, "cannot pass map_type %d into func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 1624 | map->map_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1625 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1628 | static int check_raw_mode(const struct bpf_func_proto *fn) |
| 1629 | { |
| 1630 | int count = 0; |
| 1631 | |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1632 | if (fn->arg1_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1633 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1634 | if (fn->arg2_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1635 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1636 | if (fn->arg3_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1637 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1638 | if (fn->arg4_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1639 | count++; |
Alexei Starovoitov | 39f19ebb | 2017-01-09 10:19:50 -0800 | [diff] [blame] | 1640 | if (fn->arg5_type == ARG_PTR_TO_UNINIT_MEM) |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1641 | count++; |
| 1642 | |
| 1643 | return count > 1 ? -EINVAL : 0; |
| 1644 | } |
| 1645 | |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1646 | /* Packet data might have moved, any old PTR_TO_PACKET[_META,_END] |
| 1647 | * are now invalid, so turn them into unknown SCALAR_VALUE. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1648 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1649 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1650 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1651 | struct bpf_verifier_state *state = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1652 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1653 | int i; |
| 1654 | |
| 1655 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1656 | if (reg_is_pkt_pointer_any(®s[i])) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1657 | mark_reg_unknown(env, regs, i); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1658 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1659 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 1660 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1661 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1662 | reg = &state->stack[i].spilled_ptr; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1663 | if (reg_is_pkt_pointer_any(reg)) |
| 1664 | __mark_reg_unknown(reg); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 1668 | static int check_call(struct bpf_verifier_env *env, int func_id, int insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1669 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1670 | const struct bpf_func_proto *fn = NULL; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1671 | struct bpf_reg_state *regs; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1672 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1673 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1674 | int i, err; |
| 1675 | |
| 1676 | /* find function prototype */ |
| 1677 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1678 | verbose(env, "invalid func %s#%d\n", func_id_name(func_id), |
| 1679 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1680 | return -EINVAL; |
| 1681 | } |
| 1682 | |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 1683 | if (env->ops->get_func_proto) |
| 1684 | fn = env->ops->get_func_proto(func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1685 | |
| 1686 | if (!fn) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1687 | verbose(env, "unknown func %s#%d\n", func_id_name(func_id), |
| 1688 | func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1689 | return -EINVAL; |
| 1690 | } |
| 1691 | |
| 1692 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 1693 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1694 | verbose(env, "cannot call GPL only function from proprietary program\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1695 | return -EINVAL; |
| 1696 | } |
| 1697 | |
Martin KaFai Lau | 17bedab | 2016-12-07 15:53:11 -0800 | [diff] [blame] | 1698 | changes_data = bpf_helper_changes_pkt_data(fn->func); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1699 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1700 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1701 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1702 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1703 | /* We only support one arg being in raw mode at the moment, which |
| 1704 | * is sufficient for the helper functions we have right now. |
| 1705 | */ |
| 1706 | err = check_raw_mode(fn); |
| 1707 | if (err) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1708 | verbose(env, "kernel subsystem misconfigured func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 1709 | func_id_name(func_id), func_id); |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1710 | return err; |
| 1711 | } |
| 1712 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1713 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1714 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1715 | if (err) |
| 1716 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1717 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1718 | if (err) |
| 1719 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1720 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1721 | if (err) |
| 1722 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1723 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1724 | if (err) |
| 1725 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1726 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1727 | if (err) |
| 1728 | return err; |
| 1729 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1730 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 1731 | * is inferred from register state. |
| 1732 | */ |
| 1733 | for (i = 0; i < meta.access_size; i++) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 1734 | 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] | 1735 | if (err) |
| 1736 | return err; |
| 1737 | } |
| 1738 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1739 | regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1740 | /* reset caller saved regs */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1741 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1742 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1743 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 1744 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1745 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 1746 | /* update return register (already marked as written above) */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1747 | if (fn->ret_type == RET_INTEGER) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1748 | /* sets type to SCALAR_VALUE */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1749 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1750 | } else if (fn->ret_type == RET_VOID) { |
| 1751 | regs[BPF_REG_0].type = NOT_INIT; |
| 1752 | } 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] | 1753 | struct bpf_insn_aux_data *insn_aux; |
| 1754 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1755 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1756 | /* There is no offset yet applied, variable or fixed */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1757 | mark_reg_known_zero(env, regs, BPF_REG_0); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1758 | regs[BPF_REG_0].off = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1759 | /* remember map_ptr, so that check_map_access() |
| 1760 | * can check 'value_size' boundary of memory access |
| 1761 | * to map element returned from bpf_map_lookup_elem() |
| 1762 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1763 | if (meta.map_ptr == NULL) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1764 | verbose(env, |
| 1765 | "kernel subsystem misconfigured verifier\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1766 | return -EINVAL; |
| 1767 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1768 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1769 | regs[BPF_REG_0].id = ++env->id_gen; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 1770 | insn_aux = &env->insn_aux_data[insn_idx]; |
| 1771 | if (!insn_aux->map_ptr) |
| 1772 | insn_aux->map_ptr = meta.map_ptr; |
| 1773 | else if (insn_aux->map_ptr != meta.map_ptr) |
| 1774 | insn_aux->map_ptr = BPF_MAP_PTR_POISON; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1775 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1776 | verbose(env, "unknown return type %d of func %s#%d\n", |
Thomas Graf | ebb676d | 2016-10-27 11:23:51 +0200 | [diff] [blame] | 1777 | fn->ret_type, func_id_name(func_id), func_id); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1778 | return -EINVAL; |
| 1779 | } |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1780 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1781 | err = check_map_func_compatibility(env, meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1782 | if (err) |
| 1783 | return err; |
Alexei Starovoitov | 04fd61ab | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1784 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1785 | if (changes_data) |
| 1786 | clear_all_pkt_pointers(env); |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1790 | static void coerce_reg_to_32(struct bpf_reg_state *reg) |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1791 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1792 | /* clear high 32 bits */ |
| 1793 | reg->var_off = tnum_cast(reg->var_off, 4); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1794 | /* Update bounds */ |
| 1795 | __update_reg_bounds(reg); |
| 1796 | } |
| 1797 | |
| 1798 | static bool signed_add_overflows(s64 a, s64 b) |
| 1799 | { |
| 1800 | /* Do the add in u64, where overflow is well-defined */ |
| 1801 | s64 res = (s64)((u64)a + (u64)b); |
| 1802 | |
| 1803 | if (b < 0) |
| 1804 | return res > a; |
| 1805 | return res < a; |
| 1806 | } |
| 1807 | |
| 1808 | static bool signed_sub_overflows(s64 a, s64 b) |
| 1809 | { |
| 1810 | /* Do the sub in u64, where overflow is well-defined */ |
| 1811 | s64 res = (s64)((u64)a - (u64)b); |
| 1812 | |
| 1813 | if (b < 0) |
| 1814 | return res < a; |
| 1815 | return res > a; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1818 | /* 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] | 1819 | * Caller should also handle BPF_MOV case separately. |
| 1820 | * If we return -EACCES, caller may want to try again treating pointer as a |
| 1821 | * scalar. So we only emit a diagnostic if !env->allow_ptr_leaks. |
| 1822 | */ |
| 1823 | static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, |
| 1824 | struct bpf_insn *insn, |
| 1825 | const struct bpf_reg_state *ptr_reg, |
| 1826 | const struct bpf_reg_state *off_reg) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1827 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1828 | struct bpf_reg_state *regs = cur_regs(env), *dst_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1829 | bool known = tnum_is_const(off_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1830 | s64 smin_val = off_reg->smin_value, smax_val = off_reg->smax_value, |
| 1831 | smin_ptr = ptr_reg->smin_value, smax_ptr = ptr_reg->smax_value; |
| 1832 | u64 umin_val = off_reg->umin_value, umax_val = off_reg->umax_value, |
| 1833 | umin_ptr = ptr_reg->umin_value, umax_ptr = ptr_reg->umax_value; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1834 | u8 opcode = BPF_OP(insn->code); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1835 | u32 dst = insn->dst_reg; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1836 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1837 | dst_reg = ®s[dst]; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1838 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1839 | if (WARN_ON_ONCE(known && (smin_val != smax_val))) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1840 | print_verifier_state(env, env->cur_state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1841 | verbose(env, |
| 1842 | "verifier internal error: known but bad sbounds\n"); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1843 | return -EINVAL; |
| 1844 | } |
| 1845 | if (WARN_ON_ONCE(known && (umin_val != umax_val))) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 1846 | print_verifier_state(env, env->cur_state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1847 | verbose(env, |
| 1848 | "verifier internal error: known but bad ubounds\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1849 | return -EINVAL; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1850 | } |
| 1851 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1852 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 1853 | /* 32-bit ALU ops on pointers produce (meaningless) scalars */ |
| 1854 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1855 | verbose(env, |
| 1856 | "R%d 32-bit pointer arithmetic prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1857 | dst); |
| 1858 | return -EACCES; |
| 1859 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 1860 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1861 | if (ptr_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 1862 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1863 | 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] | 1864 | dst); |
| 1865 | return -EACCES; |
| 1866 | } |
| 1867 | if (ptr_reg->type == CONST_PTR_TO_MAP) { |
| 1868 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1869 | 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] | 1870 | dst); |
| 1871 | return -EACCES; |
| 1872 | } |
| 1873 | if (ptr_reg->type == PTR_TO_PACKET_END) { |
| 1874 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1875 | 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] | 1876 | dst); |
| 1877 | return -EACCES; |
| 1878 | } |
| 1879 | |
| 1880 | /* In case of 'scalar += pointer', dst_reg inherits pointer type and id. |
| 1881 | * 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] | 1882 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1883 | dst_reg->type = ptr_reg->type; |
| 1884 | dst_reg->id = ptr_reg->id; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1885 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1886 | switch (opcode) { |
| 1887 | case BPF_ADD: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1888 | /* We can take a fixed offset as long as it doesn't overflow |
| 1889 | * the s32 'off' field |
| 1890 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1891 | if (known && (ptr_reg->off + smin_val == |
| 1892 | (s64)(s32)(ptr_reg->off + smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1893 | /* pointer += K. Accumulate it into fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1894 | dst_reg->smin_value = smin_ptr; |
| 1895 | dst_reg->smax_value = smax_ptr; |
| 1896 | dst_reg->umin_value = umin_ptr; |
| 1897 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1898 | dst_reg->var_off = ptr_reg->var_off; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1899 | dst_reg->off = ptr_reg->off + smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1900 | dst_reg->range = ptr_reg->range; |
| 1901 | break; |
| 1902 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1903 | /* A new variable offset is created. Note that off_reg->off |
| 1904 | * == 0, since it's a scalar. |
| 1905 | * dst_reg gets the pointer type and since some positive |
| 1906 | * integer value was added to the pointer, give it a new 'id' |
| 1907 | * if it's a PTR_TO_PACKET. |
| 1908 | * this creates a new 'base' pointer, off_reg (variable) gets |
| 1909 | * added into the variable offset, and we copy the fixed offset |
| 1910 | * from ptr_reg. |
| 1911 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1912 | if (signed_add_overflows(smin_ptr, smin_val) || |
| 1913 | signed_add_overflows(smax_ptr, smax_val)) { |
| 1914 | dst_reg->smin_value = S64_MIN; |
| 1915 | dst_reg->smax_value = S64_MAX; |
| 1916 | } else { |
| 1917 | dst_reg->smin_value = smin_ptr + smin_val; |
| 1918 | dst_reg->smax_value = smax_ptr + smax_val; |
| 1919 | } |
| 1920 | if (umin_ptr + umin_val < umin_ptr || |
| 1921 | umax_ptr + umax_val < umax_ptr) { |
| 1922 | dst_reg->umin_value = 0; |
| 1923 | dst_reg->umax_value = U64_MAX; |
| 1924 | } else { |
| 1925 | dst_reg->umin_value = umin_ptr + umin_val; |
| 1926 | dst_reg->umax_value = umax_ptr + umax_val; |
| 1927 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1928 | dst_reg->var_off = tnum_add(ptr_reg->var_off, off_reg->var_off); |
| 1929 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1930 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1931 | dst_reg->id = ++env->id_gen; |
| 1932 | /* something was added to pkt_ptr, set range to zero */ |
| 1933 | dst_reg->range = 0; |
| 1934 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1935 | break; |
| 1936 | case BPF_SUB: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1937 | if (dst_reg == off_reg) { |
| 1938 | /* scalar -= pointer. Creates an unknown scalar */ |
| 1939 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1940 | verbose(env, "R%d tried to subtract pointer from scalar\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1941 | dst); |
| 1942 | return -EACCES; |
| 1943 | } |
| 1944 | /* We don't allow subtraction from FP, because (according to |
| 1945 | * test_verifier.c test "invalid fp arithmetic", JITs might not |
| 1946 | * be able to deal with it. |
Edward Cree | 9305706 | 2017-07-21 14:37:34 +0100 | [diff] [blame] | 1947 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1948 | if (ptr_reg->type == PTR_TO_STACK) { |
| 1949 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 1950 | verbose(env, "R%d subtraction from stack pointer prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1951 | dst); |
| 1952 | return -EACCES; |
| 1953 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1954 | if (known && (ptr_reg->off - smin_val == |
| 1955 | (s64)(s32)(ptr_reg->off - smin_val))) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1956 | /* pointer -= K. Subtract it from fixed offset */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1957 | dst_reg->smin_value = smin_ptr; |
| 1958 | dst_reg->smax_value = smax_ptr; |
| 1959 | dst_reg->umin_value = umin_ptr; |
| 1960 | dst_reg->umax_value = umax_ptr; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1961 | dst_reg->var_off = ptr_reg->var_off; |
| 1962 | dst_reg->id = ptr_reg->id; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1963 | dst_reg->off = ptr_reg->off - smin_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1964 | dst_reg->range = ptr_reg->range; |
| 1965 | break; |
| 1966 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1967 | /* A new variable offset is created. If the subtrahend is known |
| 1968 | * nonnegative, then any reg->range we had before is still good. |
| 1969 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1970 | if (signed_sub_overflows(smin_ptr, smax_val) || |
| 1971 | signed_sub_overflows(smax_ptr, smin_val)) { |
| 1972 | /* Overflow possible, we know nothing */ |
| 1973 | dst_reg->smin_value = S64_MIN; |
| 1974 | dst_reg->smax_value = S64_MAX; |
| 1975 | } else { |
| 1976 | dst_reg->smin_value = smin_ptr - smax_val; |
| 1977 | dst_reg->smax_value = smax_ptr - smin_val; |
| 1978 | } |
| 1979 | if (umin_ptr < umax_val) { |
| 1980 | /* Overflow possible, we know nothing */ |
| 1981 | dst_reg->umin_value = 0; |
| 1982 | dst_reg->umax_value = U64_MAX; |
| 1983 | } else { |
| 1984 | /* Cannot overflow (as long as bounds are consistent) */ |
| 1985 | dst_reg->umin_value = umin_ptr - umax_val; |
| 1986 | dst_reg->umax_value = umax_ptr - umin_val; |
| 1987 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1988 | dst_reg->var_off = tnum_sub(ptr_reg->var_off, off_reg->var_off); |
| 1989 | dst_reg->off = ptr_reg->off; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 1990 | if (reg_is_pkt_pointer(ptr_reg)) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1991 | dst_reg->id = ++env->id_gen; |
| 1992 | /* something was added to pkt_ptr, set range to zero */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 1993 | if (smin_val < 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 1994 | dst_reg->range = 0; |
| 1995 | } |
| 1996 | break; |
| 1997 | case BPF_AND: |
| 1998 | case BPF_OR: |
| 1999 | case BPF_XOR: |
| 2000 | /* bitwise ops on pointers are troublesome, prohibit for now. |
| 2001 | * (However, in principle we could allow some cases, e.g. |
| 2002 | * ptr &= ~3 which would reduce min_value by 3.) |
| 2003 | */ |
| 2004 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2005 | verbose(env, "R%d bitwise operator %s on pointer prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2006 | dst, bpf_alu_string[opcode >> 4]); |
| 2007 | return -EACCES; |
| 2008 | default: |
| 2009 | /* other operators (e.g. MUL,LSH) produce non-pointer results */ |
| 2010 | if (!env->allow_ptr_leaks) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2011 | verbose(env, "R%d pointer arithmetic with %s operator prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2012 | dst, bpf_alu_string[opcode >> 4]); |
| 2013 | return -EACCES; |
| 2014 | } |
| 2015 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2016 | __update_reg_bounds(dst_reg); |
| 2017 | __reg_deduce_bounds(dst_reg); |
| 2018 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2019 | return 0; |
| 2020 | } |
| 2021 | |
| 2022 | static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env, |
| 2023 | struct bpf_insn *insn, |
| 2024 | struct bpf_reg_state *dst_reg, |
| 2025 | struct bpf_reg_state src_reg) |
| 2026 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2027 | struct bpf_reg_state *regs = cur_regs(env); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2028 | u8 opcode = BPF_OP(insn->code); |
| 2029 | bool src_known, dst_known; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2030 | s64 smin_val, smax_val; |
| 2031 | u64 umin_val, umax_val; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2032 | |
| 2033 | if (BPF_CLASS(insn->code) != BPF_ALU64) { |
| 2034 | /* 32-bit ALU ops are (32,32)->64 */ |
| 2035 | coerce_reg_to_32(dst_reg); |
| 2036 | coerce_reg_to_32(&src_reg); |
| 2037 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2038 | smin_val = src_reg.smin_value; |
| 2039 | smax_val = src_reg.smax_value; |
| 2040 | umin_val = src_reg.umin_value; |
| 2041 | umax_val = src_reg.umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2042 | src_known = tnum_is_const(src_reg.var_off); |
| 2043 | dst_known = tnum_is_const(dst_reg->var_off); |
| 2044 | |
| 2045 | switch (opcode) { |
| 2046 | case BPF_ADD: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2047 | if (signed_add_overflows(dst_reg->smin_value, smin_val) || |
| 2048 | signed_add_overflows(dst_reg->smax_value, smax_val)) { |
| 2049 | dst_reg->smin_value = S64_MIN; |
| 2050 | dst_reg->smax_value = S64_MAX; |
| 2051 | } else { |
| 2052 | dst_reg->smin_value += smin_val; |
| 2053 | dst_reg->smax_value += smax_val; |
| 2054 | } |
| 2055 | if (dst_reg->umin_value + umin_val < umin_val || |
| 2056 | dst_reg->umax_value + umax_val < umax_val) { |
| 2057 | dst_reg->umin_value = 0; |
| 2058 | dst_reg->umax_value = U64_MAX; |
| 2059 | } else { |
| 2060 | dst_reg->umin_value += umin_val; |
| 2061 | dst_reg->umax_value += umax_val; |
| 2062 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2063 | dst_reg->var_off = tnum_add(dst_reg->var_off, src_reg.var_off); |
| 2064 | break; |
| 2065 | case BPF_SUB: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2066 | if (signed_sub_overflows(dst_reg->smin_value, smax_val) || |
| 2067 | signed_sub_overflows(dst_reg->smax_value, smin_val)) { |
| 2068 | /* Overflow possible, we know nothing */ |
| 2069 | dst_reg->smin_value = S64_MIN; |
| 2070 | dst_reg->smax_value = S64_MAX; |
| 2071 | } else { |
| 2072 | dst_reg->smin_value -= smax_val; |
| 2073 | dst_reg->smax_value -= smin_val; |
| 2074 | } |
| 2075 | if (dst_reg->umin_value < umax_val) { |
| 2076 | /* Overflow possible, we know nothing */ |
| 2077 | dst_reg->umin_value = 0; |
| 2078 | dst_reg->umax_value = U64_MAX; |
| 2079 | } else { |
| 2080 | /* Cannot overflow (as long as bounds are consistent) */ |
| 2081 | dst_reg->umin_value -= umax_val; |
| 2082 | dst_reg->umax_value -= umin_val; |
| 2083 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2084 | 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] | 2085 | break; |
| 2086 | case BPF_MUL: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2087 | dst_reg->var_off = tnum_mul(dst_reg->var_off, src_reg.var_off); |
| 2088 | if (smin_val < 0 || dst_reg->smin_value < 0) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2089 | /* Ain't nobody got time to multiply that sign */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2090 | __mark_reg_unbounded(dst_reg); |
| 2091 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2092 | break; |
| 2093 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2094 | /* Both values are positive, so we can work with unsigned and |
| 2095 | * copy the result to signed (unless it exceeds S64_MAX). |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2096 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2097 | if (umax_val > U32_MAX || dst_reg->umax_value > U32_MAX) { |
| 2098 | /* Potential overflow, we know nothing */ |
| 2099 | __mark_reg_unbounded(dst_reg); |
| 2100 | /* (except what we can learn from the var_off) */ |
| 2101 | __update_reg_bounds(dst_reg); |
| 2102 | break; |
| 2103 | } |
| 2104 | dst_reg->umin_value *= umin_val; |
| 2105 | dst_reg->umax_value *= umax_val; |
| 2106 | if (dst_reg->umax_value > S64_MAX) { |
| 2107 | /* Overflow possible, we know nothing */ |
| 2108 | dst_reg->smin_value = S64_MIN; |
| 2109 | dst_reg->smax_value = S64_MAX; |
| 2110 | } else { |
| 2111 | dst_reg->smin_value = dst_reg->umin_value; |
| 2112 | dst_reg->smax_value = dst_reg->umax_value; |
| 2113 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2114 | break; |
| 2115 | case BPF_AND: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2116 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2117 | __mark_reg_known(dst_reg, dst_reg->var_off.value & |
| 2118 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2119 | break; |
| 2120 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2121 | /* We get our minimum from the var_off, since that's inherently |
| 2122 | * bitwise. Our maximum is the minimum of the operands' maxima. |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 2123 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2124 | 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] | 2125 | dst_reg->umin_value = dst_reg->var_off.value; |
| 2126 | dst_reg->umax_value = min(dst_reg->umax_value, umax_val); |
| 2127 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2128 | /* Lose signed bounds when ANDing negative numbers, |
| 2129 | * ain't nobody got time for that. |
| 2130 | */ |
| 2131 | dst_reg->smin_value = S64_MIN; |
| 2132 | dst_reg->smax_value = S64_MAX; |
| 2133 | } else { |
| 2134 | /* ANDing two positives gives a positive, so safe to |
| 2135 | * cast result into s64. |
| 2136 | */ |
| 2137 | dst_reg->smin_value = dst_reg->umin_value; |
| 2138 | dst_reg->smax_value = dst_reg->umax_value; |
| 2139 | } |
| 2140 | /* We may learn something more from the var_off */ |
| 2141 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2142 | break; |
| 2143 | case BPF_OR: |
| 2144 | if (src_known && dst_known) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2145 | __mark_reg_known(dst_reg, dst_reg->var_off.value | |
| 2146 | src_reg.var_off.value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2147 | break; |
| 2148 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2149 | /* We get our maximum from the var_off, and our minimum is the |
| 2150 | * maximum of the operands' minima |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2151 | */ |
| 2152 | 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] | 2153 | dst_reg->umin_value = max(dst_reg->umin_value, umin_val); |
| 2154 | dst_reg->umax_value = dst_reg->var_off.value | |
| 2155 | dst_reg->var_off.mask; |
| 2156 | if (dst_reg->smin_value < 0 || smin_val < 0) { |
| 2157 | /* Lose signed bounds when ORing negative numbers, |
| 2158 | * ain't nobody got time for that. |
| 2159 | */ |
| 2160 | dst_reg->smin_value = S64_MIN; |
| 2161 | dst_reg->smax_value = S64_MAX; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2162 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2163 | /* ORing two positives gives a positive, so safe to |
| 2164 | * cast result into s64. |
| 2165 | */ |
| 2166 | dst_reg->smin_value = dst_reg->umin_value; |
| 2167 | dst_reg->smax_value = dst_reg->umax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2168 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2169 | /* We may learn something more from the var_off */ |
| 2170 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2171 | break; |
| 2172 | case BPF_LSH: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2173 | if (umax_val > 63) { |
| 2174 | /* Shifts greater than 63 are undefined. This includes |
| 2175 | * shifts by a negative number. |
| 2176 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2177 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2178 | break; |
| 2179 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2180 | /* We lose all sign bit information (except what we can pick |
| 2181 | * up from var_off) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2182 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2183 | dst_reg->smin_value = S64_MIN; |
| 2184 | dst_reg->smax_value = S64_MAX; |
| 2185 | /* If we might shift our top bit out, then we know nothing */ |
| 2186 | if (dst_reg->umax_value > 1ULL << (63 - umax_val)) { |
| 2187 | dst_reg->umin_value = 0; |
| 2188 | dst_reg->umax_value = U64_MAX; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2189 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2190 | dst_reg->umin_value <<= umin_val; |
| 2191 | dst_reg->umax_value <<= umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2192 | } |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2193 | if (src_known) |
| 2194 | dst_reg->var_off = tnum_lshift(dst_reg->var_off, umin_val); |
| 2195 | else |
| 2196 | dst_reg->var_off = tnum_lshift(tnum_unknown, umin_val); |
| 2197 | /* We may learn something more from the var_off */ |
| 2198 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2199 | break; |
| 2200 | case BPF_RSH: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2201 | if (umax_val > 63) { |
| 2202 | /* Shifts greater than 63 are undefined. This includes |
| 2203 | * shifts by a negative number. |
| 2204 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2205 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2206 | break; |
| 2207 | } |
| 2208 | /* BPF_RSH is an unsigned shift, so make the appropriate casts */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2209 | if (dst_reg->smin_value < 0) { |
| 2210 | if (umin_val) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2211 | /* Sign bit will be cleared */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2212 | dst_reg->smin_value = 0; |
| 2213 | } else { |
| 2214 | /* Lost sign bit information */ |
| 2215 | dst_reg->smin_value = S64_MIN; |
| 2216 | dst_reg->smax_value = S64_MAX; |
| 2217 | } |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2218 | } else { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2219 | dst_reg->smin_value = |
| 2220 | (u64)(dst_reg->smin_value) >> umax_val; |
David S. Miller | d117441 | 2017-05-10 11:22:52 -0700 | [diff] [blame] | 2221 | } |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2222 | if (src_known) |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2223 | dst_reg->var_off = tnum_rshift(dst_reg->var_off, |
| 2224 | umin_val); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2225 | else |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2226 | dst_reg->var_off = tnum_rshift(tnum_unknown, umin_val); |
| 2227 | dst_reg->umin_value >>= umax_val; |
| 2228 | dst_reg->umax_value >>= umin_val; |
| 2229 | /* We may learn something more from the var_off */ |
| 2230 | __update_reg_bounds(dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2231 | break; |
| 2232 | default: |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2233 | mark_reg_unknown(env, regs, insn->dst_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2234 | break; |
| 2235 | } |
| 2236 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2237 | __reg_deduce_bounds(dst_reg); |
| 2238 | __reg_bound_offset(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2239 | return 0; |
| 2240 | } |
| 2241 | |
| 2242 | /* Handles ALU ops other than BPF_END, BPF_NEG and BPF_MOV: computes new min/max |
| 2243 | * and var_off. |
| 2244 | */ |
| 2245 | static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 2246 | struct bpf_insn *insn) |
| 2247 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2248 | struct bpf_reg_state *regs = cur_regs(env), *dst_reg, *src_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2249 | struct bpf_reg_state *ptr_reg = NULL, off_reg = {0}; |
| 2250 | u8 opcode = BPF_OP(insn->code); |
| 2251 | int rc; |
| 2252 | |
| 2253 | dst_reg = ®s[insn->dst_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2254 | src_reg = NULL; |
| 2255 | if (dst_reg->type != SCALAR_VALUE) |
| 2256 | ptr_reg = dst_reg; |
| 2257 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2258 | src_reg = ®s[insn->src_reg]; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2259 | if (src_reg->type != SCALAR_VALUE) { |
| 2260 | if (dst_reg->type != SCALAR_VALUE) { |
| 2261 | /* Combining two pointers by any ALU op yields |
| 2262 | * an arbitrary scalar. |
| 2263 | */ |
| 2264 | if (!env->allow_ptr_leaks) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2265 | verbose(env, "R%d pointer %s pointer prohibited\n", |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2266 | insn->dst_reg, |
| 2267 | bpf_alu_string[opcode >> 4]); |
| 2268 | return -EACCES; |
| 2269 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2270 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2271 | return 0; |
| 2272 | } else { |
| 2273 | /* scalar += pointer |
| 2274 | * This is legal, but we have to reverse our |
| 2275 | * src/dest handling in computing the range |
| 2276 | */ |
| 2277 | rc = adjust_ptr_min_max_vals(env, insn, |
| 2278 | src_reg, dst_reg); |
| 2279 | if (rc == -EACCES && env->allow_ptr_leaks) { |
| 2280 | /* scalar += unknown scalar */ |
| 2281 | __mark_reg_unknown(&off_reg); |
| 2282 | return adjust_scalar_min_max_vals( |
| 2283 | env, insn, |
| 2284 | dst_reg, off_reg); |
| 2285 | } |
| 2286 | return rc; |
| 2287 | } |
| 2288 | } else if (ptr_reg) { |
| 2289 | /* pointer += scalar */ |
| 2290 | rc = adjust_ptr_min_max_vals(env, insn, |
| 2291 | dst_reg, src_reg); |
| 2292 | if (rc == -EACCES && env->allow_ptr_leaks) { |
| 2293 | /* unknown scalar += scalar */ |
| 2294 | __mark_reg_unknown(dst_reg); |
| 2295 | return adjust_scalar_min_max_vals( |
| 2296 | env, insn, dst_reg, *src_reg); |
| 2297 | } |
| 2298 | return rc; |
| 2299 | } |
| 2300 | } else { |
| 2301 | /* Pretend the src is a reg with a known value, since we only |
| 2302 | * need to be able to read from this state. |
| 2303 | */ |
| 2304 | off_reg.type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2305 | __mark_reg_known(&off_reg, insn->imm); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2306 | src_reg = &off_reg; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2307 | if (ptr_reg) { /* pointer += K */ |
| 2308 | rc = adjust_ptr_min_max_vals(env, insn, |
| 2309 | ptr_reg, src_reg); |
| 2310 | if (rc == -EACCES && env->allow_ptr_leaks) { |
| 2311 | /* unknown scalar += K */ |
| 2312 | __mark_reg_unknown(dst_reg); |
| 2313 | return adjust_scalar_min_max_vals( |
| 2314 | env, insn, dst_reg, off_reg); |
| 2315 | } |
| 2316 | return rc; |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | /* Got here implies adding two SCALAR_VALUEs */ |
| 2321 | if (WARN_ON_ONCE(ptr_reg)) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2322 | print_verifier_state(env, env->cur_state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2323 | verbose(env, "verifier internal error: unexpected ptr_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2324 | return -EINVAL; |
| 2325 | } |
| 2326 | if (WARN_ON(!src_reg)) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2327 | print_verifier_state(env, env->cur_state); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2328 | verbose(env, "verifier internal error: no src_reg\n"); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2329 | return -EINVAL; |
| 2330 | } |
| 2331 | return adjust_scalar_min_max_vals(env, insn, dst_reg, *src_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2332 | } |
| 2333 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2334 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2335 | 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] | 2336 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2337 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2338 | u8 opcode = BPF_OP(insn->code); |
| 2339 | int err; |
| 2340 | |
| 2341 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 2342 | if (opcode == BPF_NEG) { |
| 2343 | if (BPF_SRC(insn->code) != 0 || |
| 2344 | insn->src_reg != BPF_REG_0 || |
| 2345 | insn->off != 0 || insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2346 | verbose(env, "BPF_NEG uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2347 | return -EINVAL; |
| 2348 | } |
| 2349 | } else { |
| 2350 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
Edward Cree | e67b8a6 | 2017-09-15 14:37:38 +0100 | [diff] [blame] | 2351 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64) || |
| 2352 | BPF_CLASS(insn->code) == BPF_ALU64) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2353 | verbose(env, "BPF_END uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2354 | return -EINVAL; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2359 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2360 | if (err) |
| 2361 | return err; |
| 2362 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2363 | if (is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2364 | verbose(env, "R%d pointer arithmetic prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2365 | insn->dst_reg); |
| 2366 | return -EACCES; |
| 2367 | } |
| 2368 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2369 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2370 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2371 | if (err) |
| 2372 | return err; |
| 2373 | |
| 2374 | } else if (opcode == BPF_MOV) { |
| 2375 | |
| 2376 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2377 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2378 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2379 | return -EINVAL; |
| 2380 | } |
| 2381 | |
| 2382 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2383 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2384 | if (err) |
| 2385 | return err; |
| 2386 | } else { |
| 2387 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2388 | verbose(env, "BPF_MOV uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2389 | return -EINVAL; |
| 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2394 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2395 | if (err) |
| 2396 | return err; |
| 2397 | |
| 2398 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2399 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 2400 | /* case: R1 = R2 |
| 2401 | * copy register state to dest reg |
| 2402 | */ |
| 2403 | regs[insn->dst_reg] = regs[insn->src_reg]; |
Alexei Starovoitov | 8fe2d6c | 2017-10-05 16:20:56 -0700 | [diff] [blame] | 2404 | regs[insn->dst_reg].live |= REG_LIVE_WRITTEN; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2405 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2406 | /* R1 = (u32) R2 */ |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2407 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2408 | verbose(env, |
| 2409 | "R%d partial copy of pointer\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2410 | insn->src_reg); |
| 2411 | return -EACCES; |
| 2412 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2413 | mark_reg_unknown(env, regs, insn->dst_reg); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2414 | /* high 32 bits are known zero. */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2415 | regs[insn->dst_reg].var_off = tnum_cast( |
| 2416 | regs[insn->dst_reg].var_off, 4); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2417 | __update_reg_bounds(®s[insn->dst_reg]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2418 | } |
| 2419 | } else { |
| 2420 | /* case: R = imm |
| 2421 | * remember the value we stored into this reg |
| 2422 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2423 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2424 | __mark_reg_known(regs + insn->dst_reg, insn->imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | } else if (opcode > BPF_END) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2428 | verbose(env, "invalid BPF_ALU opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2429 | return -EINVAL; |
| 2430 | |
| 2431 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 2432 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2433 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2434 | if (insn->imm != 0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2435 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2436 | return -EINVAL; |
| 2437 | } |
| 2438 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2439 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2440 | if (err) |
| 2441 | return err; |
| 2442 | } else { |
| 2443 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2444 | verbose(env, "BPF_ALU uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2445 | return -EINVAL; |
| 2446 | } |
| 2447 | } |
| 2448 | |
| 2449 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2450 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2451 | if (err) |
| 2452 | return err; |
| 2453 | |
| 2454 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 2455 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2456 | verbose(env, "div by zero\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2457 | return -EINVAL; |
| 2458 | } |
| 2459 | |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 2460 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 2461 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 2462 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 2463 | |
| 2464 | if (insn->imm < 0 || insn->imm >= size) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2465 | verbose(env, "invalid shift %d\n", insn->imm); |
Rabin Vincent | 229394e8 | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 2466 | return -EINVAL; |
| 2467 | } |
| 2468 | } |
| 2469 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2470 | /* check dest operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2471 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2472 | if (err) |
| 2473 | return err; |
| 2474 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2475 | return adjust_reg_min_max_vals(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | return 0; |
| 2479 | } |
| 2480 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2481 | static void find_good_pkt_pointers(struct bpf_verifier_state *state, |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2482 | struct bpf_reg_state *dst_reg, |
David S. Miller | f8ddadc | 2017-10-22 13:36:53 +0100 | [diff] [blame] | 2483 | enum bpf_reg_type type, |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 2484 | bool range_right_open) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2485 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2486 | struct bpf_reg_state *regs = state->regs, *reg; |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 2487 | u16 new_range; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2488 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2489 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 2490 | if (dst_reg->off < 0 || |
| 2491 | (dst_reg->off == 0 && range_right_open)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2492 | /* This doesn't give us any range */ |
| 2493 | return; |
| 2494 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2495 | if (dst_reg->umax_value > MAX_PACKET_OFF || |
| 2496 | dst_reg->umax_value + dst_reg->off > MAX_PACKET_OFF) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2497 | /* Risk of overflow. For instance, ptr + (1<<63) may be less |
| 2498 | * than pkt_end, but that's because it's also less than pkt. |
| 2499 | */ |
| 2500 | return; |
| 2501 | |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 2502 | new_range = dst_reg->off; |
| 2503 | if (range_right_open) |
| 2504 | new_range--; |
| 2505 | |
| 2506 | /* Examples for register markings: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2507 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 2508 | * pkt_data in dst register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2509 | * |
| 2510 | * r2 = r3; |
| 2511 | * r2 += 8; |
| 2512 | * if (r2 > pkt_end) goto <handle exception> |
| 2513 | * <access okay> |
| 2514 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2515 | * r2 = r3; |
| 2516 | * r2 += 8; |
| 2517 | * if (r2 < pkt_end) goto <access okay> |
| 2518 | * <handle exception> |
| 2519 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2520 | * Where: |
| 2521 | * r2 == dst_reg, pkt_end == src_reg |
| 2522 | * r2=pkt(id=n,off=8,r=0) |
| 2523 | * r3=pkt(id=n,off=0,r=0) |
| 2524 | * |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 2525 | * pkt_data in src register: |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2526 | * |
| 2527 | * r2 = r3; |
| 2528 | * r2 += 8; |
| 2529 | * if (pkt_end >= r2) goto <access okay> |
| 2530 | * <handle exception> |
| 2531 | * |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2532 | * r2 = r3; |
| 2533 | * r2 += 8; |
| 2534 | * if (pkt_end <= r2) goto <handle exception> |
| 2535 | * <access okay> |
| 2536 | * |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2537 | * Where: |
| 2538 | * pkt_end == dst_reg, r2 == src_reg |
| 2539 | * r2=pkt(id=n,off=8,r=0) |
| 2540 | * r3=pkt(id=n,off=0,r=0) |
| 2541 | * |
| 2542 | * 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] | 2543 | * or r3=pkt(id=n,off=0,r=8-1), so that range of bytes [r3, r3 + 8) |
| 2544 | * and [r3, r3 + 8-1) respectively is safe to access depending on |
| 2545 | * the check. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2546 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2547 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2548 | /* If our ids match, then we must have the same max_value. And we |
| 2549 | * don't care about the other reg's fixed offset, since if it's too big |
| 2550 | * the range won't allow anything. |
| 2551 | * dst_reg->off is known < MAX_PACKET_OFF, therefore it fits in a u16. |
| 2552 | */ |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2553 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2554 | if (regs[i].type == type && regs[i].id == dst_reg->id) |
Alexei Starovoitov | b197768 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 2555 | /* keep the maximum range already checked */ |
Daniel Borkmann | fb2a311 | 2017-10-21 02:34:21 +0200 | [diff] [blame] | 2556 | regs[i].range = max(regs[i].range, new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2557 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2558 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 2559 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2560 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2561 | reg = &state->stack[i].spilled_ptr; |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 2562 | if (reg->type == type && reg->id == dst_reg->id) |
Daniel Borkmann | b06723d | 2017-11-01 23:58:09 +0100 | [diff] [blame] | 2563 | reg->range = max(reg->range, new_range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2564 | } |
| 2565 | } |
| 2566 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2567 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 2568 | * variable register that we are working on, and src_reg is a constant or we're |
| 2569 | * simply doing a BPF_K check. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2570 | * In JEQ/JNE cases we also adjust the var_off values. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2571 | */ |
| 2572 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 2573 | struct bpf_reg_state *false_reg, u64 val, |
| 2574 | u8 opcode) |
| 2575 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2576 | /* If the dst_reg is a pointer, we can't learn anything about its |
| 2577 | * variable offset from the compare (unless src_reg were a pointer into |
| 2578 | * the same object, but we don't bother with that. |
| 2579 | * Since false_reg and true_reg have the same type by construction, we |
| 2580 | * only need to check one of them for pointerness. |
| 2581 | */ |
| 2582 | if (__is_pointer_value(false, false_reg)) |
| 2583 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2584 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2585 | switch (opcode) { |
| 2586 | case BPF_JEQ: |
| 2587 | /* If this is false then we know nothing Jon Snow, but if it is |
| 2588 | * true then we know for sure. |
| 2589 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2590 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2591 | break; |
| 2592 | case BPF_JNE: |
| 2593 | /* If this is true we know nothing Jon Snow, but if it is false |
| 2594 | * we know the value for sure; |
| 2595 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2596 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2597 | break; |
| 2598 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2599 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 2600 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 2601 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2602 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2603 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 2604 | 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] | 2605 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2606 | case BPF_JLT: |
| 2607 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 2608 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 2609 | break; |
| 2610 | case BPF_JSLT: |
| 2611 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
| 2612 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 2613 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2614 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2615 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 2616 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 2617 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2618 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2619 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 2620 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2621 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2622 | case BPF_JLE: |
| 2623 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 2624 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 2625 | break; |
| 2626 | case BPF_JSLE: |
| 2627 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val + 1); |
| 2628 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 2629 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2630 | default: |
| 2631 | break; |
| 2632 | } |
| 2633 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2634 | __reg_deduce_bounds(false_reg); |
| 2635 | __reg_deduce_bounds(true_reg); |
| 2636 | /* We might have learned some bits from the bounds. */ |
| 2637 | __reg_bound_offset(false_reg); |
| 2638 | __reg_bound_offset(true_reg); |
| 2639 | /* Intersecting with the old var_off might have improved our bounds |
| 2640 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 2641 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 2642 | */ |
| 2643 | __update_reg_bounds(false_reg); |
| 2644 | __update_reg_bounds(true_reg); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2645 | } |
| 2646 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2647 | /* Same as above, but for the case that dst_reg holds a constant and src_reg is |
| 2648 | * the variable reg. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2649 | */ |
| 2650 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 2651 | struct bpf_reg_state *false_reg, u64 val, |
| 2652 | u8 opcode) |
| 2653 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2654 | if (__is_pointer_value(false, false_reg)) |
| 2655 | return; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2656 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2657 | switch (opcode) { |
| 2658 | case BPF_JEQ: |
| 2659 | /* If this is false then we know nothing Jon Snow, but if it is |
| 2660 | * true then we know for sure. |
| 2661 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2662 | __mark_reg_known(true_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2663 | break; |
| 2664 | case BPF_JNE: |
| 2665 | /* If this is true we know nothing Jon Snow, but if it is false |
| 2666 | * we know the value for sure; |
| 2667 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2668 | __mark_reg_known(false_reg, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2669 | break; |
| 2670 | case BPF_JGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2671 | true_reg->umax_value = min(true_reg->umax_value, val - 1); |
| 2672 | false_reg->umin_value = max(false_reg->umin_value, val); |
| 2673 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2674 | case BPF_JSGT: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2675 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val - 1); |
| 2676 | false_reg->smin_value = max_t(s64, false_reg->smin_value, val); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2677 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2678 | case BPF_JLT: |
| 2679 | true_reg->umin_value = max(true_reg->umin_value, val + 1); |
| 2680 | false_reg->umax_value = min(false_reg->umax_value, val); |
| 2681 | break; |
| 2682 | case BPF_JSLT: |
| 2683 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val + 1); |
| 2684 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val); |
| 2685 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2686 | case BPF_JGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2687 | true_reg->umax_value = min(true_reg->umax_value, val); |
| 2688 | false_reg->umin_value = max(false_reg->umin_value, val + 1); |
| 2689 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2690 | case BPF_JSGE: |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2691 | true_reg->smax_value = min_t(s64, true_reg->smax_value, val); |
| 2692 | 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] | 2693 | break; |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2694 | case BPF_JLE: |
| 2695 | true_reg->umin_value = max(true_reg->umin_value, val); |
| 2696 | false_reg->umax_value = min(false_reg->umax_value, val - 1); |
| 2697 | break; |
| 2698 | case BPF_JSLE: |
| 2699 | true_reg->smin_value = max_t(s64, true_reg->smin_value, val); |
| 2700 | false_reg->smax_value = min_t(s64, false_reg->smax_value, val - 1); |
| 2701 | break; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2702 | default: |
| 2703 | break; |
| 2704 | } |
| 2705 | |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2706 | __reg_deduce_bounds(false_reg); |
| 2707 | __reg_deduce_bounds(true_reg); |
| 2708 | /* We might have learned some bits from the bounds. */ |
| 2709 | __reg_bound_offset(false_reg); |
| 2710 | __reg_bound_offset(true_reg); |
| 2711 | /* Intersecting with the old var_off might have improved our bounds |
| 2712 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 2713 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 2714 | */ |
| 2715 | __update_reg_bounds(false_reg); |
| 2716 | __update_reg_bounds(true_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2717 | } |
| 2718 | |
| 2719 | /* Regs are known to be equal, so intersect their min/max/var_off */ |
| 2720 | static void __reg_combine_min_max(struct bpf_reg_state *src_reg, |
| 2721 | struct bpf_reg_state *dst_reg) |
| 2722 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2723 | src_reg->umin_value = dst_reg->umin_value = max(src_reg->umin_value, |
| 2724 | dst_reg->umin_value); |
| 2725 | src_reg->umax_value = dst_reg->umax_value = min(src_reg->umax_value, |
| 2726 | dst_reg->umax_value); |
| 2727 | src_reg->smin_value = dst_reg->smin_value = max(src_reg->smin_value, |
| 2728 | dst_reg->smin_value); |
| 2729 | src_reg->smax_value = dst_reg->smax_value = min(src_reg->smax_value, |
| 2730 | dst_reg->smax_value); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2731 | src_reg->var_off = dst_reg->var_off = tnum_intersect(src_reg->var_off, |
| 2732 | dst_reg->var_off); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2733 | /* We might have learned new bounds from the var_off. */ |
| 2734 | __update_reg_bounds(src_reg); |
| 2735 | __update_reg_bounds(dst_reg); |
| 2736 | /* We might have learned something about the sign bit. */ |
| 2737 | __reg_deduce_bounds(src_reg); |
| 2738 | __reg_deduce_bounds(dst_reg); |
| 2739 | /* We might have learned some bits from the bounds. */ |
| 2740 | __reg_bound_offset(src_reg); |
| 2741 | __reg_bound_offset(dst_reg); |
| 2742 | /* Intersecting with the old var_off might have improved our bounds |
| 2743 | * slightly. e.g. if umax was 0x7f...f and var_off was (0; 0xf...fc), |
| 2744 | * then new var_off is (0; 0x7f...fc) which improves our umax. |
| 2745 | */ |
| 2746 | __update_reg_bounds(src_reg); |
| 2747 | __update_reg_bounds(dst_reg); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | static void reg_combine_min_max(struct bpf_reg_state *true_src, |
| 2751 | struct bpf_reg_state *true_dst, |
| 2752 | struct bpf_reg_state *false_src, |
| 2753 | struct bpf_reg_state *false_dst, |
| 2754 | u8 opcode) |
| 2755 | { |
| 2756 | switch (opcode) { |
| 2757 | case BPF_JEQ: |
| 2758 | __reg_combine_min_max(true_src, true_dst); |
| 2759 | break; |
| 2760 | case BPF_JNE: |
| 2761 | __reg_combine_min_max(false_src, false_dst); |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2762 | break; |
Daniel Borkmann | 4cabc5b | 2017-07-21 00:00:21 +0200 | [diff] [blame] | 2763 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2764 | } |
| 2765 | |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2766 | 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] | 2767 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2768 | { |
| 2769 | struct bpf_reg_state *reg = ®s[regno]; |
| 2770 | |
| 2771 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2772 | /* Old offset (both fixed and variable parts) should |
| 2773 | * have been known-zero, because we don't allow pointer |
| 2774 | * arithmetic on pointers that might be NULL. |
| 2775 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2776 | if (WARN_ON_ONCE(reg->smin_value || reg->smax_value || |
| 2777 | !tnum_equals_const(reg->var_off, 0) || |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2778 | reg->off)) { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 2779 | __mark_reg_known_zero(reg); |
| 2780 | reg->off = 0; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2781 | } |
| 2782 | if (is_null) { |
| 2783 | reg->type = SCALAR_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2784 | } else if (reg->map_ptr->inner_map_meta) { |
| 2785 | reg->type = CONST_PTR_TO_MAP; |
| 2786 | reg->map_ptr = reg->map_ptr->inner_map_meta; |
| 2787 | } else { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2788 | reg->type = PTR_TO_MAP_VALUE; |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 2789 | } |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 2790 | /* We don't need id from this point onwards anymore, thus we |
| 2791 | * should better reset it, so that state pruning has chances |
| 2792 | * to take effect. |
| 2793 | */ |
| 2794 | reg->id = 0; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 2799 | * be folded together at some point. |
| 2800 | */ |
| 2801 | static void mark_map_regs(struct bpf_verifier_state *state, u32 regno, |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2802 | bool is_null) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2803 | { |
| 2804 | struct bpf_reg_state *regs = state->regs; |
Daniel Borkmann | a08dd0d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 2805 | u32 id = regs[regno].id; |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2806 | int i; |
| 2807 | |
| 2808 | for (i = 0; i < MAX_BPF_REG; i++) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2809 | mark_map_reg(regs, i, id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2810 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2811 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) { |
| 2812 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2813 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2814 | mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null); |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 2818 | static bool try_match_pkt_pointers(const struct bpf_insn *insn, |
| 2819 | struct bpf_reg_state *dst_reg, |
| 2820 | struct bpf_reg_state *src_reg, |
| 2821 | struct bpf_verifier_state *this_branch, |
| 2822 | struct bpf_verifier_state *other_branch) |
| 2823 | { |
| 2824 | if (BPF_SRC(insn->code) != BPF_X) |
| 2825 | return false; |
| 2826 | |
| 2827 | switch (BPF_OP(insn->code)) { |
| 2828 | case BPF_JGT: |
| 2829 | if ((dst_reg->type == PTR_TO_PACKET && |
| 2830 | src_reg->type == PTR_TO_PACKET_END) || |
| 2831 | (dst_reg->type == PTR_TO_PACKET_META && |
| 2832 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 2833 | /* pkt_data' > pkt_end, pkt_meta' > pkt_data */ |
| 2834 | find_good_pkt_pointers(this_branch, dst_reg, |
| 2835 | dst_reg->type, false); |
| 2836 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 2837 | src_reg->type == PTR_TO_PACKET) || |
| 2838 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 2839 | src_reg->type == PTR_TO_PACKET_META)) { |
| 2840 | /* pkt_end > pkt_data', pkt_data > pkt_meta' */ |
| 2841 | find_good_pkt_pointers(other_branch, src_reg, |
| 2842 | src_reg->type, true); |
| 2843 | } else { |
| 2844 | return false; |
| 2845 | } |
| 2846 | break; |
| 2847 | case BPF_JLT: |
| 2848 | if ((dst_reg->type == PTR_TO_PACKET && |
| 2849 | src_reg->type == PTR_TO_PACKET_END) || |
| 2850 | (dst_reg->type == PTR_TO_PACKET_META && |
| 2851 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 2852 | /* pkt_data' < pkt_end, pkt_meta' < pkt_data */ |
| 2853 | find_good_pkt_pointers(other_branch, dst_reg, |
| 2854 | dst_reg->type, true); |
| 2855 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 2856 | src_reg->type == PTR_TO_PACKET) || |
| 2857 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 2858 | src_reg->type == PTR_TO_PACKET_META)) { |
| 2859 | /* pkt_end < pkt_data', pkt_data > pkt_meta' */ |
| 2860 | find_good_pkt_pointers(this_branch, src_reg, |
| 2861 | src_reg->type, false); |
| 2862 | } else { |
| 2863 | return false; |
| 2864 | } |
| 2865 | break; |
| 2866 | case BPF_JGE: |
| 2867 | if ((dst_reg->type == PTR_TO_PACKET && |
| 2868 | src_reg->type == PTR_TO_PACKET_END) || |
| 2869 | (dst_reg->type == PTR_TO_PACKET_META && |
| 2870 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 2871 | /* pkt_data' >= pkt_end, pkt_meta' >= pkt_data */ |
| 2872 | find_good_pkt_pointers(this_branch, dst_reg, |
| 2873 | dst_reg->type, true); |
| 2874 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 2875 | src_reg->type == PTR_TO_PACKET) || |
| 2876 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 2877 | src_reg->type == PTR_TO_PACKET_META)) { |
| 2878 | /* pkt_end >= pkt_data', pkt_data >= pkt_meta' */ |
| 2879 | find_good_pkt_pointers(other_branch, src_reg, |
| 2880 | src_reg->type, false); |
| 2881 | } else { |
| 2882 | return false; |
| 2883 | } |
| 2884 | break; |
| 2885 | case BPF_JLE: |
| 2886 | if ((dst_reg->type == PTR_TO_PACKET && |
| 2887 | src_reg->type == PTR_TO_PACKET_END) || |
| 2888 | (dst_reg->type == PTR_TO_PACKET_META && |
| 2889 | reg_is_init_pkt_pointer(src_reg, PTR_TO_PACKET))) { |
| 2890 | /* pkt_data' <= pkt_end, pkt_meta' <= pkt_data */ |
| 2891 | find_good_pkt_pointers(other_branch, dst_reg, |
| 2892 | dst_reg->type, false); |
| 2893 | } else if ((dst_reg->type == PTR_TO_PACKET_END && |
| 2894 | src_reg->type == PTR_TO_PACKET) || |
| 2895 | (reg_is_init_pkt_pointer(dst_reg, PTR_TO_PACKET) && |
| 2896 | src_reg->type == PTR_TO_PACKET_META)) { |
| 2897 | /* pkt_end <= pkt_data', pkt_data <= pkt_meta' */ |
| 2898 | find_good_pkt_pointers(this_branch, src_reg, |
| 2899 | src_reg->type, true); |
| 2900 | } else { |
| 2901 | return false; |
| 2902 | } |
| 2903 | break; |
| 2904 | default: |
| 2905 | return false; |
| 2906 | } |
| 2907 | |
| 2908 | return true; |
| 2909 | } |
| 2910 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2911 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2912 | struct bpf_insn *insn, int *insn_idx) |
| 2913 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 2914 | struct bpf_verifier_state *other_branch, *this_branch = env->cur_state; |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2915 | struct bpf_reg_state *regs = this_branch->regs, *dst_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2916 | u8 opcode = BPF_OP(insn->code); |
| 2917 | int err; |
| 2918 | |
Daniel Borkmann | b4e432f | 2017-08-10 01:40:02 +0200 | [diff] [blame] | 2919 | if (opcode > BPF_JSLE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2920 | verbose(env, "invalid BPF_JMP opcode %x\n", opcode); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2921 | return -EINVAL; |
| 2922 | } |
| 2923 | |
| 2924 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2925 | if (insn->imm != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2926 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2927 | return -EINVAL; |
| 2928 | } |
| 2929 | |
| 2930 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2931 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2932 | if (err) |
| 2933 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2934 | |
| 2935 | if (is_pointer_value(env, insn->src_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2936 | verbose(env, "R%d pointer comparison prohibited\n", |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2937 | insn->src_reg); |
| 2938 | return -EACCES; |
| 2939 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2940 | } else { |
| 2941 | if (insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 2942 | verbose(env, "BPF_JMP uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2943 | return -EINVAL; |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 2948 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2949 | if (err) |
| 2950 | return err; |
| 2951 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2952 | dst_reg = ®s[insn->dst_reg]; |
| 2953 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2954 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 2955 | if (BPF_SRC(insn->code) == BPF_K && |
| 2956 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2957 | dst_reg->type == SCALAR_VALUE && |
Alexei Starovoitov | 3bf1592 | 2017-11-30 21:31:39 -0800 | [diff] [blame] | 2958 | tnum_is_const(dst_reg->var_off)) { |
| 2959 | if ((opcode == BPF_JEQ && dst_reg->var_off.value == insn->imm) || |
| 2960 | (opcode == BPF_JNE && dst_reg->var_off.value != insn->imm)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2961 | /* if (imm == imm) goto pc+off; |
| 2962 | * only follow the goto, ignore fall-through |
| 2963 | */ |
| 2964 | *insn_idx += insn->off; |
| 2965 | return 0; |
| 2966 | } else { |
| 2967 | /* if (imm != imm) goto pc+off; |
| 2968 | * only follow fall-through branch, since |
| 2969 | * that's where the program will go |
| 2970 | */ |
| 2971 | return 0; |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 2976 | if (!other_branch) |
| 2977 | return -EFAULT; |
| 2978 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2979 | /* detect if we are comparing against a constant value so we can adjust |
| 2980 | * our min/max values for our dst register. |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2981 | * this is only legit if both are scalars (or pointers to the same |
| 2982 | * object, I suppose, but we don't support that right now), because |
| 2983 | * otherwise the different base pointers mean the offsets aren't |
| 2984 | * comparable. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2985 | */ |
| 2986 | if (BPF_SRC(insn->code) == BPF_X) { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 2987 | if (dst_reg->type == SCALAR_VALUE && |
| 2988 | regs[insn->src_reg].type == SCALAR_VALUE) { |
| 2989 | if (tnum_is_const(regs[insn->src_reg].var_off)) |
| 2990 | reg_set_min_max(&other_branch->regs[insn->dst_reg], |
| 2991 | dst_reg, regs[insn->src_reg].var_off.value, |
| 2992 | opcode); |
| 2993 | else if (tnum_is_const(dst_reg->var_off)) |
| 2994 | reg_set_min_max_inv(&other_branch->regs[insn->src_reg], |
| 2995 | ®s[insn->src_reg], |
| 2996 | dst_reg->var_off.value, opcode); |
| 2997 | else if (opcode == BPF_JEQ || opcode == BPF_JNE) |
| 2998 | /* Comparing for equality, we can combine knowledge */ |
| 2999 | reg_combine_min_max(&other_branch->regs[insn->src_reg], |
| 3000 | &other_branch->regs[insn->dst_reg], |
| 3001 | ®s[insn->src_reg], |
| 3002 | ®s[insn->dst_reg], opcode); |
| 3003 | } |
| 3004 | } else if (dst_reg->type == SCALAR_VALUE) { |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3005 | reg_set_min_max(&other_branch->regs[insn->dst_reg], |
| 3006 | dst_reg, insn->imm, opcode); |
| 3007 | } |
| 3008 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3009 | /* 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] | 3010 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3011 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 3012 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Thomas Graf | 57a09bf | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 3013 | /* Mark all identical map registers in each branch as either |
| 3014 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 3015 | */ |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3016 | mark_map_regs(this_branch, insn->dst_reg, opcode == BPF_JNE); |
| 3017 | mark_map_regs(other_branch, insn->dst_reg, opcode == BPF_JEQ); |
Daniel Borkmann | 5beca08 | 2017-11-01 23:58:10 +0100 | [diff] [blame] | 3018 | } else if (!try_match_pkt_pointers(insn, dst_reg, ®s[insn->src_reg], |
| 3019 | this_branch, other_branch) && |
| 3020 | is_pointer_value(env, insn->dst_reg)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3021 | verbose(env, "R%d pointer comparison prohibited\n", |
| 3022 | insn->dst_reg); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3023 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3024 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3025 | if (env->log.level) |
| 3026 | print_verifier_state(env, this_branch); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3027 | return 0; |
| 3028 | } |
| 3029 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3030 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 3031 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 3032 | { |
| 3033 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 3034 | |
| 3035 | return (struct bpf_map *) (unsigned long) imm64; |
| 3036 | } |
| 3037 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3038 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3039 | 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] | 3040 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3041 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3042 | int err; |
| 3043 | |
| 3044 | if (BPF_SIZE(insn->code) != BPF_DW) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3045 | verbose(env, "invalid BPF_LD_IMM insn\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3046 | return -EINVAL; |
| 3047 | } |
| 3048 | if (insn->off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3049 | verbose(env, "BPF_LD_IMM64 uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3050 | return -EINVAL; |
| 3051 | } |
| 3052 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3053 | err = check_reg_arg(env, insn->dst_reg, DST_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3054 | if (err) |
| 3055 | return err; |
| 3056 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3057 | if (insn->src_reg == 0) { |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3058 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 3059 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3060 | regs[insn->dst_reg].type = SCALAR_VALUE; |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3061 | __mark_reg_known(®s[insn->dst_reg], imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3062 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 3063 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3064 | |
| 3065 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 3066 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 3067 | |
| 3068 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 3069 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 3070 | return 0; |
| 3071 | } |
| 3072 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3073 | static bool may_access_skb(enum bpf_prog_type type) |
| 3074 | { |
| 3075 | switch (type) { |
| 3076 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 3077 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 3078 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 3079 | return true; |
| 3080 | default: |
| 3081 | return false; |
| 3082 | } |
| 3083 | } |
| 3084 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3085 | /* verify safety of LD_ABS|LD_IND instructions: |
| 3086 | * - they can only appear in the programs where ctx == skb |
| 3087 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 3088 | * preserve R6-R9, and store return value into R0 |
| 3089 | * |
| 3090 | * Implicit input: |
| 3091 | * ctx == skb == R6 == CTX |
| 3092 | * |
| 3093 | * Explicit input: |
| 3094 | * SRC == any register |
| 3095 | * IMM == 32-bit immediate |
| 3096 | * |
| 3097 | * Output: |
| 3098 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 3099 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3100 | 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] | 3101 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3102 | struct bpf_reg_state *regs = cur_regs(env); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3103 | u8 mode = BPF_MODE(insn->code); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3104 | int i, err; |
| 3105 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 3106 | if (!may_access_skb(env->prog->type)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3107 | 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] | 3108 | return -EINVAL; |
| 3109 | } |
| 3110 | |
| 3111 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 3112 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3113 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3114 | verbose(env, "BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3115 | return -EINVAL; |
| 3116 | } |
| 3117 | |
| 3118 | /* check whether implicit source operand (register R6) is readable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3119 | err = check_reg_arg(env, BPF_REG_6, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3120 | if (err) |
| 3121 | return err; |
| 3122 | |
| 3123 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3124 | verbose(env, |
| 3125 | "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] | 3126 | return -EINVAL; |
| 3127 | } |
| 3128 | |
| 3129 | if (mode == BPF_IND) { |
| 3130 | /* check explicit source operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3131 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3132 | if (err) |
| 3133 | return err; |
| 3134 | } |
| 3135 | |
| 3136 | /* reset caller saved regs to unreadable */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3137 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3138 | mark_reg_not_init(env, regs, caller_saved[i]); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3139 | check_reg_arg(env, caller_saved[i], DST_OP_NO_MARK); |
| 3140 | } |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3141 | |
| 3142 | /* mark destination R0 register as readable, since it contains |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3143 | * the value fetched from the packet. |
| 3144 | * Already marked as written above. |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3145 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3146 | mark_reg_unknown(env, regs, BPF_REG_0); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 3147 | return 0; |
| 3148 | } |
| 3149 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3150 | static int check_return_code(struct bpf_verifier_env *env) |
| 3151 | { |
| 3152 | struct bpf_reg_state *reg; |
| 3153 | struct tnum range = tnum_range(0, 1); |
| 3154 | |
| 3155 | switch (env->prog->type) { |
| 3156 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 3157 | case BPF_PROG_TYPE_CGROUP_SOCK: |
| 3158 | case BPF_PROG_TYPE_SOCK_OPS: |
Roman Gushchin | ebc614f | 2017-11-05 08:15:32 -0500 | [diff] [blame] | 3159 | case BPF_PROG_TYPE_CGROUP_DEVICE: |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3160 | break; |
| 3161 | default: |
| 3162 | return 0; |
| 3163 | } |
| 3164 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3165 | reg = cur_regs(env) + BPF_REG_0; |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3166 | if (reg->type != SCALAR_VALUE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3167 | 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] | 3168 | reg_type_str[reg->type]); |
| 3169 | return -EINVAL; |
| 3170 | } |
| 3171 | |
| 3172 | if (!tnum_in(range, reg->var_off)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3173 | verbose(env, "At program exit the register R0 "); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3174 | if (!tnum_is_unknown(reg->var_off)) { |
| 3175 | char tn_buf[48]; |
| 3176 | |
| 3177 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3178 | verbose(env, "has value %s", tn_buf); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3179 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3180 | verbose(env, "has unknown scalar value"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3181 | } |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3182 | verbose(env, " should have been 0 or 1\n"); |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 3183 | return -EINVAL; |
| 3184 | } |
| 3185 | return 0; |
| 3186 | } |
| 3187 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3188 | /* non-recursive DFS pseudo code |
| 3189 | * 1 procedure DFS-iterative(G,v): |
| 3190 | * 2 label v as discovered |
| 3191 | * 3 let S be a stack |
| 3192 | * 4 S.push(v) |
| 3193 | * 5 while S is not empty |
| 3194 | * 6 t <- S.pop() |
| 3195 | * 7 if t is what we're looking for: |
| 3196 | * 8 return t |
| 3197 | * 9 for all edges e in G.adjacentEdges(t) do |
| 3198 | * 10 if edge e is already labelled |
| 3199 | * 11 continue with the next edge |
| 3200 | * 12 w <- G.adjacentVertex(t,e) |
| 3201 | * 13 if vertex w is not discovered and not explored |
| 3202 | * 14 label e as tree-edge |
| 3203 | * 15 label w as discovered |
| 3204 | * 16 S.push(w) |
| 3205 | * 17 continue at 5 |
| 3206 | * 18 else if vertex w is discovered |
| 3207 | * 19 label e as back-edge |
| 3208 | * 20 else |
| 3209 | * 21 // vertex w is explored |
| 3210 | * 22 label e as forward- or cross-edge |
| 3211 | * 23 label t as explored |
| 3212 | * 24 S.pop() |
| 3213 | * |
| 3214 | * convention: |
| 3215 | * 0x10 - discovered |
| 3216 | * 0x11 - discovered and fall-through edge labelled |
| 3217 | * 0x12 - discovered and fall-through and branch edges labelled |
| 3218 | * 0x20 - explored |
| 3219 | */ |
| 3220 | |
| 3221 | enum { |
| 3222 | DISCOVERED = 0x10, |
| 3223 | EXPLORED = 0x20, |
| 3224 | FALLTHROUGH = 1, |
| 3225 | BRANCH = 2, |
| 3226 | }; |
| 3227 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3228 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3229 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3230 | static int *insn_stack; /* stack of insns to process */ |
| 3231 | static int cur_stack; /* current stack index */ |
| 3232 | static int *insn_state; |
| 3233 | |
| 3234 | /* t, w, e - match pseudo-code above: |
| 3235 | * t - index of current instruction |
| 3236 | * w - next instruction |
| 3237 | * e - edge |
| 3238 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3239 | 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] | 3240 | { |
| 3241 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 3242 | return 0; |
| 3243 | |
| 3244 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 3245 | return 0; |
| 3246 | |
| 3247 | if (w < 0 || w >= env->prog->len) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3248 | 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] | 3249 | return -EINVAL; |
| 3250 | } |
| 3251 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3252 | if (e == BRANCH) |
| 3253 | /* mark branch target for state pruning */ |
| 3254 | env->explored_states[w] = STATE_LIST_MARK; |
| 3255 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3256 | if (insn_state[w] == 0) { |
| 3257 | /* tree-edge */ |
| 3258 | insn_state[t] = DISCOVERED | e; |
| 3259 | insn_state[w] = DISCOVERED; |
| 3260 | if (cur_stack >= env->prog->len) |
| 3261 | return -E2BIG; |
| 3262 | insn_stack[cur_stack++] = w; |
| 3263 | return 1; |
| 3264 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3265 | verbose(env, "back-edge from insn %d to %d\n", t, w); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3266 | return -EINVAL; |
| 3267 | } else if (insn_state[w] == EXPLORED) { |
| 3268 | /* forward- or cross-edge */ |
| 3269 | insn_state[t] = DISCOVERED | e; |
| 3270 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3271 | verbose(env, "insn state internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3272 | return -EFAULT; |
| 3273 | } |
| 3274 | return 0; |
| 3275 | } |
| 3276 | |
| 3277 | /* non-recursive depth-first-search to detect loops in BPF program |
| 3278 | * loop == back-edge in directed graph |
| 3279 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3280 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3281 | { |
| 3282 | struct bpf_insn *insns = env->prog->insnsi; |
| 3283 | int insn_cnt = env->prog->len; |
| 3284 | int ret = 0; |
| 3285 | int i, t; |
| 3286 | |
| 3287 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 3288 | if (!insn_state) |
| 3289 | return -ENOMEM; |
| 3290 | |
| 3291 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 3292 | if (!insn_stack) { |
| 3293 | kfree(insn_state); |
| 3294 | return -ENOMEM; |
| 3295 | } |
| 3296 | |
| 3297 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 3298 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 3299 | cur_stack = 1; |
| 3300 | |
| 3301 | peek_stack: |
| 3302 | if (cur_stack == 0) |
| 3303 | goto check_state; |
| 3304 | t = insn_stack[cur_stack - 1]; |
| 3305 | |
| 3306 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 3307 | u8 opcode = BPF_OP(insns[t].code); |
| 3308 | |
| 3309 | if (opcode == BPF_EXIT) { |
| 3310 | goto mark_explored; |
| 3311 | } else if (opcode == BPF_CALL) { |
| 3312 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 3313 | if (ret == 1) |
| 3314 | goto peek_stack; |
| 3315 | else if (ret < 0) |
| 3316 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 3317 | if (t + 1 < insn_cnt) |
| 3318 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3319 | } else if (opcode == BPF_JA) { |
| 3320 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 3321 | ret = -EINVAL; |
| 3322 | goto err_free; |
| 3323 | } |
| 3324 | /* unconditional jump with single edge */ |
| 3325 | ret = push_insn(t, t + insns[t].off + 1, |
| 3326 | FALLTHROUGH, env); |
| 3327 | if (ret == 1) |
| 3328 | goto peek_stack; |
| 3329 | else if (ret < 0) |
| 3330 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3331 | /* tell verifier to check for equivalent states |
| 3332 | * after every call and jump |
| 3333 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 3334 | if (t + 1 < insn_cnt) |
| 3335 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3336 | } else { |
| 3337 | /* conditional jump with two edges */ |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 3338 | env->explored_states[t] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3339 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 3340 | if (ret == 1) |
| 3341 | goto peek_stack; |
| 3342 | else if (ret < 0) |
| 3343 | goto err_free; |
| 3344 | |
| 3345 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 3346 | if (ret == 1) |
| 3347 | goto peek_stack; |
| 3348 | else if (ret < 0) |
| 3349 | goto err_free; |
| 3350 | } |
| 3351 | } else { |
| 3352 | /* all other non-branch instructions with single |
| 3353 | * fall-through edge |
| 3354 | */ |
| 3355 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 3356 | if (ret == 1) |
| 3357 | goto peek_stack; |
| 3358 | else if (ret < 0) |
| 3359 | goto err_free; |
| 3360 | } |
| 3361 | |
| 3362 | mark_explored: |
| 3363 | insn_state[t] = EXPLORED; |
| 3364 | if (cur_stack-- <= 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3365 | verbose(env, "pop stack internal bug\n"); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3366 | ret = -EFAULT; |
| 3367 | goto err_free; |
| 3368 | } |
| 3369 | goto peek_stack; |
| 3370 | |
| 3371 | check_state: |
| 3372 | for (i = 0; i < insn_cnt; i++) { |
| 3373 | if (insn_state[i] != EXPLORED) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3374 | verbose(env, "unreachable insn %d\n", i); |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3375 | ret = -EINVAL; |
| 3376 | goto err_free; |
| 3377 | } |
| 3378 | } |
| 3379 | ret = 0; /* cfg looks good */ |
| 3380 | |
| 3381 | err_free: |
| 3382 | kfree(insn_state); |
| 3383 | kfree(insn_stack); |
| 3384 | return ret; |
| 3385 | } |
| 3386 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3387 | /* check %cur's range satisfies %old's */ |
| 3388 | static bool range_within(struct bpf_reg_state *old, |
| 3389 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3390 | { |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3391 | return old->umin_value <= cur->umin_value && |
| 3392 | old->umax_value >= cur->umax_value && |
| 3393 | old->smin_value <= cur->smin_value && |
| 3394 | old->smax_value >= cur->smax_value; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | /* Maximum number of register states that can exist at once */ |
| 3398 | #define ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE) |
| 3399 | struct idpair { |
| 3400 | u32 old; |
| 3401 | u32 cur; |
| 3402 | }; |
| 3403 | |
| 3404 | /* If in the old state two registers had the same id, then they need to have |
| 3405 | * the same id in the new state as well. But that id could be different from |
| 3406 | * the old state, so we need to track the mapping from old to new ids. |
| 3407 | * Once we have seen that, say, a reg with old id 5 had new id 9, any subsequent |
| 3408 | * regs with old id 5 must also have new id 9 for the new state to be safe. But |
| 3409 | * regs with a different old id could still have new id 9, we don't care about |
| 3410 | * that. |
| 3411 | * So we look through our idmap to see if this old id has been seen before. If |
| 3412 | * so, we require the new id to match; otherwise, we add the id pair to the map. |
| 3413 | */ |
| 3414 | static bool check_ids(u32 old_id, u32 cur_id, struct idpair *idmap) |
| 3415 | { |
| 3416 | unsigned int i; |
| 3417 | |
| 3418 | for (i = 0; i < ID_MAP_SIZE; i++) { |
| 3419 | if (!idmap[i].old) { |
| 3420 | /* Reached an empty slot; haven't seen this id before */ |
| 3421 | idmap[i].old = old_id; |
| 3422 | idmap[i].cur = cur_id; |
| 3423 | return true; |
| 3424 | } |
| 3425 | if (idmap[i].old == old_id) |
| 3426 | return idmap[i].cur == cur_id; |
| 3427 | } |
| 3428 | /* We ran out of idmap slots, which should be impossible */ |
| 3429 | WARN_ON_ONCE(1); |
| 3430 | return false; |
| 3431 | } |
| 3432 | |
| 3433 | /* Returns true if (rold safe implies rcur safe) */ |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 3434 | static bool regsafe(struct bpf_reg_state *rold, struct bpf_reg_state *rcur, |
| 3435 | struct idpair *idmap) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3436 | { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3437 | if (!(rold->live & REG_LIVE_READ)) |
| 3438 | /* explored state didn't use this */ |
| 3439 | return true; |
| 3440 | |
| 3441 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, live)) == 0) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3442 | return true; |
| 3443 | |
| 3444 | if (rold->type == NOT_INIT) |
| 3445 | /* explored state can't have used this */ |
| 3446 | return true; |
| 3447 | if (rcur->type == NOT_INIT) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3448 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3449 | switch (rold->type) { |
| 3450 | case SCALAR_VALUE: |
| 3451 | if (rcur->type == SCALAR_VALUE) { |
| 3452 | /* new val must satisfy old val knowledge */ |
| 3453 | return range_within(rold, rcur) && |
| 3454 | tnum_in(rold->var_off, rcur->var_off); |
| 3455 | } else { |
| 3456 | /* if we knew anything about the old value, we're not |
| 3457 | * equal, because we can't know anything about the |
| 3458 | * scalar value of the pointer in the new value. |
| 3459 | */ |
Edward Cree | b03c9f9 | 2017-08-07 15:26:36 +0100 | [diff] [blame] | 3460 | return rold->umin_value == 0 && |
| 3461 | rold->umax_value == U64_MAX && |
| 3462 | rold->smin_value == S64_MIN && |
| 3463 | rold->smax_value == S64_MAX && |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3464 | tnum_is_unknown(rold->var_off); |
| 3465 | } |
| 3466 | case PTR_TO_MAP_VALUE: |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 3467 | /* If the new min/max/var_off satisfy the old ones and |
| 3468 | * everything else matches, we are OK. |
| 3469 | * We don't care about the 'id' value, because nothing |
| 3470 | * uses it for PTR_TO_MAP_VALUE (only for ..._OR_NULL) |
| 3471 | */ |
| 3472 | return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 && |
| 3473 | range_within(rold, rcur) && |
| 3474 | tnum_in(rold->var_off, rcur->var_off); |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3475 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 3476 | /* a PTR_TO_MAP_VALUE could be safe to use as a |
| 3477 | * PTR_TO_MAP_VALUE_OR_NULL into the same map. |
| 3478 | * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL- |
| 3479 | * checked, doing so could have affected others with the same |
| 3480 | * id, and we can't check for that because we lost the id when |
| 3481 | * we converted to a PTR_TO_MAP_VALUE. |
| 3482 | */ |
| 3483 | if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL) |
| 3484 | return false; |
| 3485 | if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id))) |
| 3486 | return false; |
| 3487 | /* Check our ids match any regs they're supposed to */ |
| 3488 | return check_ids(rold->id, rcur->id, idmap); |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3489 | case PTR_TO_PACKET_META: |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3490 | case PTR_TO_PACKET: |
Daniel Borkmann | de8f3a8 | 2017-09-25 02:25:51 +0200 | [diff] [blame] | 3491 | if (rcur->type != rold->type) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3492 | return false; |
| 3493 | /* We must have at least as much range as the old ptr |
| 3494 | * did, so that any accesses which were safe before are |
| 3495 | * still safe. This is true even if old range < old off, |
| 3496 | * since someone could have accessed through (ptr - k), or |
| 3497 | * even done ptr -= k in a register, to get a safe access. |
| 3498 | */ |
| 3499 | if (rold->range > rcur->range) |
| 3500 | return false; |
| 3501 | /* If the offsets don't match, we can't trust our alignment; |
| 3502 | * nor can we be sure that we won't fall out of range. |
| 3503 | */ |
| 3504 | if (rold->off != rcur->off) |
| 3505 | return false; |
| 3506 | /* id relations must be preserved */ |
| 3507 | if (rold->id && !check_ids(rold->id, rcur->id, idmap)) |
| 3508 | return false; |
| 3509 | /* new val must satisfy old val knowledge */ |
| 3510 | return range_within(rold, rcur) && |
| 3511 | tnum_in(rold->var_off, rcur->var_off); |
| 3512 | case PTR_TO_CTX: |
| 3513 | case CONST_PTR_TO_MAP: |
| 3514 | case PTR_TO_STACK: |
| 3515 | case PTR_TO_PACKET_END: |
| 3516 | /* Only valid matches are exact, which memcmp() above |
| 3517 | * would have accepted |
| 3518 | */ |
| 3519 | default: |
| 3520 | /* Don't know what's going on, just say it's not safe */ |
| 3521 | return false; |
| 3522 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3523 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3524 | /* Shouldn't get here; if we do, say it's not safe */ |
| 3525 | WARN_ON_ONCE(1); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 3526 | return false; |
| 3527 | } |
| 3528 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3529 | static bool stacksafe(struct bpf_verifier_state *old, |
| 3530 | struct bpf_verifier_state *cur, |
| 3531 | struct idpair *idmap) |
| 3532 | { |
| 3533 | int i, spi; |
| 3534 | |
| 3535 | /* if explored stack has more populated slots than current stack |
| 3536 | * such stacks are not equivalent |
| 3537 | */ |
| 3538 | if (old->allocated_stack > cur->allocated_stack) |
| 3539 | return false; |
| 3540 | |
| 3541 | /* walk slots of the explored stack and ignore any additional |
| 3542 | * slots in the current stack, since explored(safe) state |
| 3543 | * didn't use them |
| 3544 | */ |
| 3545 | for (i = 0; i < old->allocated_stack; i++) { |
| 3546 | spi = i / BPF_REG_SIZE; |
| 3547 | |
| 3548 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID) |
| 3549 | continue; |
| 3550 | if (old->stack[spi].slot_type[i % BPF_REG_SIZE] != |
| 3551 | cur->stack[spi].slot_type[i % BPF_REG_SIZE]) |
| 3552 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 3553 | * this stack slot, but current has has STACK_MISC -> |
| 3554 | * this verifier states are not equivalent, |
| 3555 | * return false to continue verification of this path |
| 3556 | */ |
| 3557 | return false; |
| 3558 | if (i % BPF_REG_SIZE) |
| 3559 | continue; |
| 3560 | if (old->stack[spi].slot_type[0] != STACK_SPILL) |
| 3561 | continue; |
| 3562 | if (!regsafe(&old->stack[spi].spilled_ptr, |
| 3563 | &cur->stack[spi].spilled_ptr, |
| 3564 | idmap)) |
| 3565 | /* when explored and current stack slot are both storing |
| 3566 | * spilled registers, check that stored pointers types |
| 3567 | * are the same as well. |
| 3568 | * Ex: explored safe path could have stored |
| 3569 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -8} |
| 3570 | * but current path has stored: |
| 3571 | * (bpf_reg_state) {.type = PTR_TO_STACK, .off = -16} |
| 3572 | * such verifier states are not equivalent. |
| 3573 | * return false to continue verification of this path |
| 3574 | */ |
| 3575 | return false; |
| 3576 | } |
| 3577 | return true; |
| 3578 | } |
| 3579 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3580 | /* compare two verifier states |
| 3581 | * |
| 3582 | * all states stored in state_list are known to be valid, since |
| 3583 | * verifier reached 'bpf_exit' instruction through them |
| 3584 | * |
| 3585 | * this function is called when verifier exploring different branches of |
| 3586 | * execution popped from the state stack. If it sees an old state that has |
| 3587 | * more strict register state and more strict stack state then this execution |
| 3588 | * branch doesn't need to be explored further, since verifier already |
| 3589 | * concluded that more strict state leads to valid finish. |
| 3590 | * |
| 3591 | * Therefore two states are equivalent if register state is more conservative |
| 3592 | * and explored stack state is more conservative than the current one. |
| 3593 | * Example: |
| 3594 | * explored current |
| 3595 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 3596 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 3597 | * |
| 3598 | * In other words if current stack state (one being explored) has more |
| 3599 | * valid slots than old one that already passed validation, it means |
| 3600 | * the verifier can stop exploring and conclude that current state is valid too |
| 3601 | * |
| 3602 | * Similarly with registers. If explored state has register type as invalid |
| 3603 | * whereas register type in current state is meaningful, it means that |
| 3604 | * the current state will reach 'bpf_exit' instruction safely |
| 3605 | */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 3606 | static bool states_equal(struct bpf_verifier_env *env, |
| 3607 | struct bpf_verifier_state *old, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3608 | struct bpf_verifier_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3609 | { |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3610 | struct idpair *idmap; |
| 3611 | bool ret = false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3612 | int i; |
| 3613 | |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3614 | idmap = kcalloc(ID_MAP_SIZE, sizeof(struct idpair), GFP_KERNEL); |
| 3615 | /* If we failed to allocate the idmap, just say it's not safe */ |
| 3616 | if (!idmap) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 3617 | return false; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3618 | |
| 3619 | for (i = 0; i < MAX_BPF_REG; i++) { |
Edward Cree | 1b688a1 | 2017-08-23 15:10:50 +0100 | [diff] [blame] | 3620 | if (!regsafe(&old->regs[i], &cur->regs[i], idmap)) |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3621 | goto out_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3622 | } |
| 3623 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3624 | if (!stacksafe(old, cur, idmap)) |
| 3625 | goto out_free; |
Edward Cree | f1174f7 | 2017-08-07 15:26:19 +0100 | [diff] [blame] | 3626 | ret = true; |
| 3627 | out_free: |
| 3628 | kfree(idmap); |
| 3629 | return ret; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3630 | } |
| 3631 | |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 3632 | /* A write screens off any subsequent reads; but write marks come from the |
| 3633 | * straight-line code between a state and its parent. When we arrive at a |
| 3634 | * jump target (in the first iteration of the propagate_liveness() loop), |
| 3635 | * we didn't arrive by the straight-line code, so read marks in state must |
| 3636 | * propagate to parent regardless of state's write marks. |
| 3637 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3638 | static bool do_propagate_liveness(const struct bpf_verifier_state *state, |
| 3639 | struct bpf_verifier_state *parent) |
| 3640 | { |
Edward Cree | 63f45f8 | 2017-08-23 15:10:03 +0100 | [diff] [blame] | 3641 | bool writes = parent == state->parent; /* Observe write marks */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3642 | bool touched = false; /* any changes made? */ |
| 3643 | int i; |
| 3644 | |
| 3645 | if (!parent) |
| 3646 | return touched; |
| 3647 | /* Propagate read liveness of registers... */ |
| 3648 | BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG); |
| 3649 | /* We don't need to worry about FP liveness because it's read-only */ |
| 3650 | for (i = 0; i < BPF_REG_FP; i++) { |
| 3651 | if (parent->regs[i].live & REG_LIVE_READ) |
| 3652 | continue; |
Edward Cree | 63f45f8 | 2017-08-23 15:10:03 +0100 | [diff] [blame] | 3653 | if (writes && (state->regs[i].live & REG_LIVE_WRITTEN)) |
| 3654 | continue; |
| 3655 | if (state->regs[i].live & REG_LIVE_READ) { |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3656 | parent->regs[i].live |= REG_LIVE_READ; |
| 3657 | touched = true; |
| 3658 | } |
| 3659 | } |
| 3660 | /* ... and stack slots */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3661 | for (i = 0; i < state->allocated_stack / BPF_REG_SIZE && |
| 3662 | i < parent->allocated_stack / BPF_REG_SIZE; i++) { |
| 3663 | if (parent->stack[i].slot_type[0] != STACK_SPILL) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3664 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3665 | if (state->stack[i].slot_type[0] != STACK_SPILL) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3666 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3667 | if (parent->stack[i].spilled_ptr.live & REG_LIVE_READ) |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3668 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3669 | if (writes && |
| 3670 | (state->stack[i].spilled_ptr.live & REG_LIVE_WRITTEN)) |
Edward Cree | 63f45f8 | 2017-08-23 15:10:03 +0100 | [diff] [blame] | 3671 | continue; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3672 | if (state->stack[i].spilled_ptr.live & REG_LIVE_READ) { |
| 3673 | parent->stack[i].spilled_ptr.live |= REG_LIVE_READ; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3674 | touched = true; |
| 3675 | } |
| 3676 | } |
| 3677 | return touched; |
| 3678 | } |
| 3679 | |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 3680 | /* "parent" is "a state from which we reach the current state", but initially |
| 3681 | * it is not the state->parent (i.e. "the state whose straight-line code leads |
| 3682 | * to the current state"), instead it is the state that happened to arrive at |
| 3683 | * a (prunable) equivalent of the current state. See comment above |
| 3684 | * do_propagate_liveness() for consequences of this. |
| 3685 | * This function is just a more efficient way of calling mark_reg_read() or |
| 3686 | * mark_stack_slot_read() on each reg in "parent" that is read in "state", |
| 3687 | * though it requires that parent != state->parent in the call arguments. |
| 3688 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3689 | static void propagate_liveness(const struct bpf_verifier_state *state, |
| 3690 | struct bpf_verifier_state *parent) |
| 3691 | { |
| 3692 | while (do_propagate_liveness(state, parent)) { |
| 3693 | /* Something changed, so we need to feed those changes onward */ |
| 3694 | state = parent; |
| 3695 | parent = state->parent; |
| 3696 | } |
| 3697 | } |
| 3698 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3699 | 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] | 3700 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3701 | struct bpf_verifier_state_list *new_sl; |
| 3702 | struct bpf_verifier_state_list *sl; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3703 | struct bpf_verifier_state *cur = env->cur_state; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 3704 | int i, err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3705 | |
| 3706 | sl = env->explored_states[insn_idx]; |
| 3707 | if (!sl) |
| 3708 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 3709 | * be doing state search here |
| 3710 | */ |
| 3711 | return 0; |
| 3712 | |
| 3713 | while (sl != STATE_LIST_MARK) { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3714 | if (states_equal(env, &sl->state, cur)) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3715 | /* reached equivalent register/stack state, |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3716 | * prune the search. |
| 3717 | * Registers read by the continuation are read by us. |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 3718 | * If we have any write marks in env->cur_state, they |
| 3719 | * will prevent corresponding reads in the continuation |
| 3720 | * from reaching our parent (an explored_state). Our |
| 3721 | * own state will get the read marks recorded, but |
| 3722 | * they'll be immediately forgotten as we're pruning |
| 3723 | * this state and will pop a new one. |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3724 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3725 | propagate_liveness(&sl->state, cur); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3726 | return 1; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3727 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3728 | sl = sl->next; |
| 3729 | } |
| 3730 | |
| 3731 | /* there were no equivalent states, remember current one. |
| 3732 | * technically the current state is not proven to be safe yet, |
| 3733 | * but it will either reach bpf_exit (which means it's safe) or |
| 3734 | * it will be rejected. Since there are no loops, we won't be |
| 3735 | * seeing this 'insn_idx' instruction again on the way to bpf_exit |
| 3736 | */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3737 | new_sl = kzalloc(sizeof(struct bpf_verifier_state_list), GFP_KERNEL); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3738 | if (!new_sl) |
| 3739 | return -ENOMEM; |
| 3740 | |
| 3741 | /* add new state to the head of linked list */ |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 3742 | err = copy_verifier_state(&new_sl->state, cur); |
| 3743 | if (err) { |
| 3744 | free_verifier_state(&new_sl->state, false); |
| 3745 | kfree(new_sl); |
| 3746 | return err; |
| 3747 | } |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3748 | new_sl->next = env->explored_states[insn_idx]; |
| 3749 | env->explored_states[insn_idx] = new_sl; |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3750 | /* connect new state to parentage chain */ |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3751 | cur->parent = &new_sl->state; |
Edward Cree | 8e9cd9c | 2017-08-23 15:11:21 +0100 | [diff] [blame] | 3752 | /* clear write marks in current state: the writes we did are not writes |
| 3753 | * our child did, so they don't screen off its reads from us. |
| 3754 | * (There are no read marks in current state, because reads always mark |
| 3755 | * their parent and current state never has children yet. Only |
| 3756 | * explored_states can get read marks.) |
| 3757 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3758 | for (i = 0; i < BPF_REG_FP; i++) |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3759 | cur->regs[i].live = REG_LIVE_NONE; |
| 3760 | for (i = 0; i < cur->allocated_stack / BPF_REG_SIZE; i++) |
| 3761 | if (cur->stack[i].slot_type[0] == STACK_SPILL) |
| 3762 | cur->stack[i].spilled_ptr.live = REG_LIVE_NONE; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3763 | return 0; |
| 3764 | } |
| 3765 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 3766 | static int ext_analyzer_insn_hook(struct bpf_verifier_env *env, |
| 3767 | int insn_idx, int prev_insn_idx) |
| 3768 | { |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 3769 | if (env->dev_ops && env->dev_ops->insn_hook) |
| 3770 | return env->dev_ops->insn_hook(env, insn_idx, prev_insn_idx); |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 3771 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 3772 | return 0; |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 3773 | } |
| 3774 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3775 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3776 | { |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3777 | struct bpf_verifier_state *state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3778 | struct bpf_insn *insns = env->prog->insnsi; |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3779 | struct bpf_reg_state *regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3780 | int insn_cnt = env->prog->len; |
| 3781 | int insn_idx, prev_insn_idx = 0; |
| 3782 | int insn_processed = 0; |
| 3783 | bool do_print_state = false; |
| 3784 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3785 | state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL); |
| 3786 | if (!state) |
| 3787 | return -ENOMEM; |
| 3788 | env->cur_state = state; |
| 3789 | init_reg_state(env, state->regs); |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3790 | state->parent = NULL; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3791 | insn_idx = 0; |
| 3792 | for (;;) { |
| 3793 | struct bpf_insn *insn; |
| 3794 | u8 class; |
| 3795 | int err; |
| 3796 | |
| 3797 | if (insn_idx >= insn_cnt) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3798 | verbose(env, "invalid insn idx %d insn_cnt %d\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3799 | insn_idx, insn_cnt); |
| 3800 | return -EFAULT; |
| 3801 | } |
| 3802 | |
| 3803 | insn = &insns[insn_idx]; |
| 3804 | class = BPF_CLASS(insn->code); |
| 3805 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 3806 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3807 | verbose(env, |
| 3808 | "BPF program is too large. Processed %d insn\n", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3809 | insn_processed); |
| 3810 | return -E2BIG; |
| 3811 | } |
| 3812 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3813 | err = is_state_visited(env, insn_idx); |
| 3814 | if (err < 0) |
| 3815 | return err; |
| 3816 | if (err == 1) { |
| 3817 | /* found equivalent state, can prune the search */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3818 | if (env->log.level) { |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3819 | if (do_print_state) |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3820 | verbose(env, "\nfrom %d to %d: safe\n", |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3821 | prev_insn_idx, insn_idx); |
| 3822 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3823 | verbose(env, "%d: safe\n", insn_idx); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3824 | } |
| 3825 | goto process_bpf_exit; |
| 3826 | } |
| 3827 | |
Daniel Borkmann | 3c2ce60 | 2017-05-18 03:00:06 +0200 | [diff] [blame] | 3828 | if (need_resched()) |
| 3829 | cond_resched(); |
| 3830 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3831 | if (env->log.level > 1 || (env->log.level && do_print_state)) { |
| 3832 | if (env->log.level > 1) |
| 3833 | verbose(env, "%d:", insn_idx); |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 3834 | else |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3835 | verbose(env, "\nfrom %d to %d:", |
David S. Miller | c5fc969 | 2017-05-10 11:25:17 -0700 | [diff] [blame] | 3836 | prev_insn_idx, insn_idx); |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3837 | print_verifier_state(env, state); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3838 | do_print_state = false; |
| 3839 | } |
| 3840 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3841 | if (env->log.level) { |
| 3842 | verbose(env, "%d: ", insn_idx); |
Jakub Kicinski | f4ac7e0 | 2017-10-09 10:30:12 -0700 | [diff] [blame] | 3843 | print_bpf_insn(verbose, env, insn, |
| 3844 | env->allow_ptr_leaks); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3845 | } |
| 3846 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 3847 | err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx); |
| 3848 | if (err) |
| 3849 | return err; |
| 3850 | |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 3851 | regs = cur_regs(env); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 3852 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3853 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3854 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3855 | if (err) |
| 3856 | return err; |
| 3857 | |
| 3858 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3859 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3860 | |
| 3861 | /* check for reserved fields is already done */ |
| 3862 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3863 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3864 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3865 | if (err) |
| 3866 | return err; |
| 3867 | |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3868 | err = check_reg_arg(env, insn->dst_reg, DST_OP_NO_MARK); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3869 | if (err) |
| 3870 | return err; |
| 3871 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 3872 | src_reg_type = regs[insn->src_reg].type; |
| 3873 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3874 | /* check that memory (src_reg + off) is readable, |
| 3875 | * the state of dst_reg will be updated by this func |
| 3876 | */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3877 | err = check_mem_access(env, insn_idx, insn->src_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3878 | BPF_SIZE(insn->code), BPF_READ, |
| 3879 | insn->dst_reg); |
| 3880 | if (err) |
| 3881 | return err; |
| 3882 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3883 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 3884 | |
| 3885 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3886 | /* saw a valid insn |
| 3887 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3888 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3889 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3890 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3891 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3892 | } else if (src_reg_type != *prev_src_type && |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3893 | (src_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3894 | *prev_src_type == PTR_TO_CTX)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3895 | /* ABuser program is trying to use the same insn |
| 3896 | * dst_reg = *(u32*) (src_reg + off) |
| 3897 | * with different pointer types: |
| 3898 | * src_reg == ctx in one branch and |
| 3899 | * src_reg == stack|map in some other branch. |
| 3900 | * Reject it. |
| 3901 | */ |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3902 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3903 | return -EINVAL; |
| 3904 | } |
| 3905 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3906 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3907 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3908 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3909 | if (BPF_MODE(insn->code) == BPF_XADD) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3910 | err = check_xadd(env, insn_idx, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3911 | if (err) |
| 3912 | return err; |
| 3913 | insn_idx++; |
| 3914 | continue; |
| 3915 | } |
| 3916 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3917 | /* check src1 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3918 | err = check_reg_arg(env, insn->src_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3919 | if (err) |
| 3920 | return err; |
| 3921 | /* check src2 operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3922 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3923 | if (err) |
| 3924 | return err; |
| 3925 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3926 | dst_reg_type = regs[insn->dst_reg].type; |
| 3927 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3928 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3929 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3930 | BPF_SIZE(insn->code), BPF_WRITE, |
| 3931 | insn->src_reg); |
| 3932 | if (err) |
| 3933 | return err; |
| 3934 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3935 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 3936 | |
| 3937 | if (*prev_dst_type == NOT_INIT) { |
| 3938 | *prev_dst_type = dst_reg_type; |
| 3939 | } else if (dst_reg_type != *prev_dst_type && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3940 | (dst_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3941 | *prev_dst_type == PTR_TO_CTX)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3942 | verbose(env, "same insn cannot be used with different pointers\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3943 | return -EINVAL; |
| 3944 | } |
| 3945 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3946 | } else if (class == BPF_ST) { |
| 3947 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 3948 | insn->src_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3949 | verbose(env, "BPF_ST uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3950 | return -EINVAL; |
| 3951 | } |
| 3952 | /* check src operand */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 3953 | err = check_reg_arg(env, insn->dst_reg, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3954 | if (err) |
| 3955 | return err; |
| 3956 | |
| 3957 | /* check that memory (dst_reg + off) is writeable */ |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 3958 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3959 | BPF_SIZE(insn->code), BPF_WRITE, |
| 3960 | -1); |
| 3961 | if (err) |
| 3962 | return err; |
| 3963 | |
| 3964 | } else if (class == BPF_JMP) { |
| 3965 | u8 opcode = BPF_OP(insn->code); |
| 3966 | |
| 3967 | if (opcode == BPF_CALL) { |
| 3968 | if (BPF_SRC(insn->code) != BPF_K || |
| 3969 | insn->off != 0 || |
| 3970 | insn->src_reg != BPF_REG_0 || |
| 3971 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3972 | verbose(env, "BPF_CALL uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3973 | return -EINVAL; |
| 3974 | } |
| 3975 | |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 3976 | err = check_call(env, insn->imm, insn_idx); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3977 | if (err) |
| 3978 | return err; |
| 3979 | |
| 3980 | } else if (opcode == BPF_JA) { |
| 3981 | if (BPF_SRC(insn->code) != BPF_K || |
| 3982 | insn->imm != 0 || |
| 3983 | insn->src_reg != BPF_REG_0 || |
| 3984 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3985 | verbose(env, "BPF_JA uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3986 | return -EINVAL; |
| 3987 | } |
| 3988 | |
| 3989 | insn_idx += insn->off + 1; |
| 3990 | continue; |
| 3991 | |
| 3992 | } else if (opcode == BPF_EXIT) { |
| 3993 | if (BPF_SRC(insn->code) != BPF_K || |
| 3994 | insn->imm != 0 || |
| 3995 | insn->src_reg != BPF_REG_0 || |
| 3996 | insn->dst_reg != BPF_REG_0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 3997 | verbose(env, "BPF_EXIT uses reserved fields\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3998 | return -EINVAL; |
| 3999 | } |
| 4000 | |
| 4001 | /* eBPF calling convetion is such that R0 is used |
| 4002 | * to return the value from eBPF program. |
| 4003 | * Make sure that it's readable at this time |
| 4004 | * of bpf_exit, which means that program wrote |
| 4005 | * something into it earlier |
| 4006 | */ |
Edward Cree | dc503a8 | 2017-08-15 20:34:35 +0100 | [diff] [blame] | 4007 | err = check_reg_arg(env, BPF_REG_0, SRC_OP); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4008 | if (err) |
| 4009 | return err; |
| 4010 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4011 | if (is_pointer_value(env, BPF_REG_0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4012 | verbose(env, "R0 leaks addr as return value\n"); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4013 | return -EACCES; |
| 4014 | } |
| 4015 | |
Alexei Starovoitov | 390ee7e | 2017-10-02 22:50:23 -0700 | [diff] [blame] | 4016 | err = check_return_code(env); |
| 4017 | if (err) |
| 4018 | return err; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4019 | process_bpf_exit: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4020 | err = pop_stack(env, &prev_insn_idx, &insn_idx); |
| 4021 | if (err < 0) { |
| 4022 | if (err != -ENOENT) |
| 4023 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4024 | break; |
| 4025 | } else { |
| 4026 | do_print_state = true; |
| 4027 | continue; |
| 4028 | } |
| 4029 | } else { |
| 4030 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 4031 | if (err) |
| 4032 | return err; |
| 4033 | } |
| 4034 | } else if (class == BPF_LD) { |
| 4035 | u8 mode = BPF_MODE(insn->code); |
| 4036 | |
| 4037 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 4038 | err = check_ld_abs(env, insn); |
| 4039 | if (err) |
| 4040 | return err; |
| 4041 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4042 | } else if (mode == BPF_IMM) { |
| 4043 | err = check_ld_imm(env, insn); |
| 4044 | if (err) |
| 4045 | return err; |
| 4046 | |
| 4047 | insn_idx++; |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4048 | env->insn_aux_data[insn_idx].seen = true; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4049 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4050 | verbose(env, "invalid BPF_LD mode\n"); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4051 | return -EINVAL; |
| 4052 | } |
| 4053 | } else { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4054 | verbose(env, "unknown insn class %d\n", class); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4055 | return -EINVAL; |
| 4056 | } |
| 4057 | |
| 4058 | insn_idx++; |
| 4059 | } |
| 4060 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4061 | verbose(env, "processed %d insns, stack depth %d\n", insn_processed, |
| 4062 | env->prog->aux->stack_depth); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4063 | return 0; |
| 4064 | } |
| 4065 | |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4066 | static int check_map_prealloc(struct bpf_map *map) |
| 4067 | { |
| 4068 | return (map->map_type != BPF_MAP_TYPE_HASH && |
Martin KaFai Lau | bcc6b1b | 2017-03-22 10:00:34 -0700 | [diff] [blame] | 4069 | map->map_type != BPF_MAP_TYPE_PERCPU_HASH && |
| 4070 | map->map_type != BPF_MAP_TYPE_HASH_OF_MAPS) || |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4071 | !(map->map_flags & BPF_F_NO_PREALLOC); |
| 4072 | } |
| 4073 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4074 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 4075 | struct bpf_map *map, |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4076 | struct bpf_prog *prog) |
| 4077 | |
| 4078 | { |
Martin KaFai Lau | 56f668d | 2017-03-22 10:00:33 -0700 | [diff] [blame] | 4079 | /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use |
| 4080 | * preallocated hash maps, since doing memory allocation |
| 4081 | * in overflow_handler can crash depending on where nmi got |
| 4082 | * triggered. |
| 4083 | */ |
| 4084 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { |
| 4085 | if (!check_map_prealloc(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4086 | 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] | 4087 | return -EINVAL; |
| 4088 | } |
| 4089 | if (map->inner_map_meta && |
| 4090 | !check_map_prealloc(map->inner_map_meta)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4091 | 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] | 4092 | return -EINVAL; |
| 4093 | } |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4094 | } |
| 4095 | return 0; |
| 4096 | } |
| 4097 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4098 | /* look for pseudo eBPF instructions that access map FDs and |
| 4099 | * replace them with actual map pointers |
| 4100 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4101 | 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] | 4102 | { |
| 4103 | struct bpf_insn *insn = env->prog->insnsi; |
| 4104 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4105 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4106 | |
Daniel Borkmann | f1f7714 | 2017-01-13 23:38:15 +0100 | [diff] [blame] | 4107 | err = bpf_prog_calc_tag(env->prog); |
Daniel Borkmann | aafe6ae | 2016-12-18 01:52:57 +0100 | [diff] [blame] | 4108 | if (err) |
| 4109 | return err; |
| 4110 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4111 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4112 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4113 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4114 | verbose(env, "BPF_LDX uses reserved fields\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4115 | return -EINVAL; |
| 4116 | } |
| 4117 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4118 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 4119 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 4120 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4121 | verbose(env, "BPF_STX uses reserved fields\n"); |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4122 | return -EINVAL; |
| 4123 | } |
| 4124 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4125 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 4126 | struct bpf_map *map; |
| 4127 | struct fd f; |
| 4128 | |
| 4129 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 4130 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 4131 | insn[1].off != 0) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4132 | verbose(env, "invalid bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4133 | return -EINVAL; |
| 4134 | } |
| 4135 | |
| 4136 | if (insn->src_reg == 0) |
| 4137 | /* valid generic load 64-bit imm */ |
| 4138 | goto next_insn; |
| 4139 | |
| 4140 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4141 | verbose(env, |
| 4142 | "unrecognized bpf_ld_imm64 insn\n"); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4143 | return -EINVAL; |
| 4144 | } |
| 4145 | |
| 4146 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 4147 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4148 | if (IS_ERR(map)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4149 | verbose(env, "fd %d is not pointing to valid bpf_map\n", |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4150 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4151 | return PTR_ERR(map); |
| 4152 | } |
| 4153 | |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4154 | err = check_map_prog_compatibility(env, map, env->prog); |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 4155 | if (err) { |
| 4156 | fdput(f); |
| 4157 | return err; |
| 4158 | } |
| 4159 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4160 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 4161 | insn[0].imm = (u32) (unsigned long) map; |
| 4162 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 4163 | |
| 4164 | /* check whether we recorded this map already */ |
| 4165 | for (j = 0; j < env->used_map_cnt; j++) |
| 4166 | if (env->used_maps[j] == map) { |
| 4167 | fdput(f); |
| 4168 | goto next_insn; |
| 4169 | } |
| 4170 | |
| 4171 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 4172 | fdput(f); |
| 4173 | return -E2BIG; |
| 4174 | } |
| 4175 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4176 | /* hold the map. If the program is rejected by verifier, |
| 4177 | * the map will be released by release_maps() or it |
| 4178 | * will be used by the valid program until it's unloaded |
| 4179 | * and all maps are released in free_bpf_prog_info() |
| 4180 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 4181 | map = bpf_map_inc(map, false); |
| 4182 | if (IS_ERR(map)) { |
| 4183 | fdput(f); |
| 4184 | return PTR_ERR(map); |
| 4185 | } |
| 4186 | env->used_maps[env->used_map_cnt++] = map; |
| 4187 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4188 | fdput(f); |
| 4189 | next_insn: |
| 4190 | insn++; |
| 4191 | i++; |
| 4192 | } |
| 4193 | } |
| 4194 | |
| 4195 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 4196 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 4197 | * These pointers will be used later by verifier to validate map access. |
| 4198 | */ |
| 4199 | return 0; |
| 4200 | } |
| 4201 | |
| 4202 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4203 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4204 | { |
| 4205 | int i; |
| 4206 | |
| 4207 | for (i = 0; i < env->used_map_cnt; i++) |
| 4208 | bpf_map_put(env->used_maps[i]); |
| 4209 | } |
| 4210 | |
| 4211 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4212 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4213 | { |
| 4214 | struct bpf_insn *insn = env->prog->insnsi; |
| 4215 | int insn_cnt = env->prog->len; |
| 4216 | int i; |
| 4217 | |
| 4218 | for (i = 0; i < insn_cnt; i++, insn++) |
| 4219 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 4220 | insn->src_reg = 0; |
| 4221 | } |
| 4222 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4223 | /* single env->prog->insni[off] instruction was replaced with the range |
| 4224 | * insni[off, off + cnt). Adjust corresponding insn_aux_data by copying |
| 4225 | * [0, off) and [off, end) to new locations, so the patched range stays zero |
| 4226 | */ |
| 4227 | static int adjust_insn_aux_data(struct bpf_verifier_env *env, u32 prog_len, |
| 4228 | u32 off, u32 cnt) |
| 4229 | { |
| 4230 | 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] | 4231 | int i; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4232 | |
| 4233 | if (cnt == 1) |
| 4234 | return 0; |
| 4235 | new_data = vzalloc(sizeof(struct bpf_insn_aux_data) * prog_len); |
| 4236 | if (!new_data) |
| 4237 | return -ENOMEM; |
| 4238 | memcpy(new_data, old_data, sizeof(struct bpf_insn_aux_data) * off); |
| 4239 | memcpy(new_data + off + cnt - 1, old_data + off, |
| 4240 | sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1)); |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4241 | for (i = off; i < off + cnt - 1; i++) |
| 4242 | new_data[i].seen = true; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4243 | env->insn_aux_data = new_data; |
| 4244 | vfree(old_data); |
| 4245 | return 0; |
| 4246 | } |
| 4247 | |
| 4248 | static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off, |
| 4249 | const struct bpf_insn *patch, u32 len) |
| 4250 | { |
| 4251 | struct bpf_prog *new_prog; |
| 4252 | |
| 4253 | new_prog = bpf_patch_insn_single(env->prog, off, patch, len); |
| 4254 | if (!new_prog) |
| 4255 | return NULL; |
| 4256 | if (adjust_insn_aux_data(env, new_prog->len, off, len)) |
| 4257 | return NULL; |
| 4258 | return new_prog; |
| 4259 | } |
| 4260 | |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4261 | /* The verifier does more data flow analysis than llvm and will not explore |
| 4262 | * branches that are dead at run time. Malicious programs can have dead code |
| 4263 | * too. Therefore replace all dead at-run-time code with nops. |
| 4264 | */ |
| 4265 | static void sanitize_dead_code(struct bpf_verifier_env *env) |
| 4266 | { |
| 4267 | struct bpf_insn_aux_data *aux_data = env->insn_aux_data; |
| 4268 | struct bpf_insn nop = BPF_MOV64_REG(BPF_REG_0, BPF_REG_0); |
| 4269 | struct bpf_insn *insn = env->prog->insnsi; |
| 4270 | const int insn_cnt = env->prog->len; |
| 4271 | int i; |
| 4272 | |
| 4273 | for (i = 0; i < insn_cnt; i++) { |
| 4274 | if (aux_data[i].seen) |
| 4275 | continue; |
| 4276 | memcpy(insn + i, &nop, sizeof(nop)); |
| 4277 | } |
| 4278 | } |
| 4279 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4280 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 4281 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 4282 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4283 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4284 | { |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 4285 | const struct bpf_verifier_ops *ops = env->ops; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4286 | int i, cnt, size, ctx_field_size, delta = 0; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4287 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4288 | struct bpf_insn insn_buf[16], *insn; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4289 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4290 | enum bpf_access_type type; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4291 | bool is_narrower_load; |
| 4292 | u32 target_size; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4293 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4294 | if (ops->gen_prologue) { |
| 4295 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 4296 | env->prog); |
| 4297 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4298 | verbose(env, "bpf verifier is misconfigured\n"); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4299 | return -EINVAL; |
| 4300 | } else if (cnt) { |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4301 | new_prog = bpf_patch_insn_data(env, 0, insn_buf, cnt); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4302 | if (!new_prog) |
| 4303 | return -ENOMEM; |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4304 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4305 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4306 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4307 | } |
| 4308 | } |
| 4309 | |
| 4310 | if (!ops->convert_ctx_access) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4311 | return 0; |
| 4312 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4313 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 4314 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4315 | for (i = 0; i < insn_cnt; i++, insn++) { |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 4316 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_B) || |
| 4317 | insn->code == (BPF_LDX | BPF_MEM | BPF_H) || |
| 4318 | insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 4319 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4320 | type = BPF_READ; |
Daniel Borkmann | 62c7989 | 2017-01-12 11:51:33 +0100 | [diff] [blame] | 4321 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_B) || |
| 4322 | insn->code == (BPF_STX | BPF_MEM | BPF_H) || |
| 4323 | insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 4324 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 4325 | type = BPF_WRITE; |
| 4326 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4327 | continue; |
| 4328 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4329 | if (env->insn_aux_data[i + delta].ptr_type != PTR_TO_CTX) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4330 | continue; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4331 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4332 | ctx_field_size = env->insn_aux_data[i + delta].ctx_field_size; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4333 | size = BPF_LDST_BYTES(insn); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4334 | |
| 4335 | /* If the read access is a narrower load of the field, |
| 4336 | * convert to a 4/8-byte load, to minimum program type specific |
| 4337 | * convert_ctx_access changes. If conversion is successful, |
| 4338 | * we will apply proper mask to the result. |
| 4339 | */ |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4340 | is_narrower_load = size < ctx_field_size; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4341 | if (is_narrower_load) { |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4342 | u32 off = insn->off; |
| 4343 | u8 size_code; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4344 | |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4345 | if (type == BPF_WRITE) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4346 | verbose(env, "bpf verifier narrow ctx access misconfigured\n"); |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4347 | return -EINVAL; |
| 4348 | } |
| 4349 | |
| 4350 | size_code = BPF_H; |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4351 | if (ctx_field_size == 4) |
| 4352 | size_code = BPF_W; |
| 4353 | else if (ctx_field_size == 8) |
| 4354 | size_code = BPF_DW; |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4355 | |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4356 | insn->off = off & ~(ctx_field_size - 1); |
| 4357 | insn->code = BPF_LDX | BPF_MEM | size_code; |
| 4358 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4359 | |
| 4360 | target_size = 0; |
| 4361 | cnt = ops->convert_ctx_access(type, insn, insn_buf, env->prog, |
| 4362 | &target_size); |
| 4363 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf) || |
| 4364 | (ctx_field_size && !target_size)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4365 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4366 | return -EINVAL; |
| 4367 | } |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4368 | |
| 4369 | if (is_narrower_load && size < target_size) { |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4370 | if (ctx_field_size <= 4) |
| 4371 | insn_buf[cnt++] = BPF_ALU32_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4372 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4373 | else |
| 4374 | insn_buf[cnt++] = BPF_ALU64_IMM(BPF_AND, insn->dst_reg, |
Daniel Borkmann | f96da09 | 2017-07-02 02:13:27 +0200 | [diff] [blame] | 4375 | (1 << size * 8) - 1); |
Yonghong Song | 31fd858 | 2017-06-13 15:52:13 -0700 | [diff] [blame] | 4376 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4377 | |
Alexei Starovoitov | 8041902 | 2017-03-15 18:26:41 -0700 | [diff] [blame] | 4378 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4379 | if (!new_prog) |
| 4380 | return -ENOMEM; |
| 4381 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4382 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4383 | |
| 4384 | /* keep walking new program and skip insns we just inserted */ |
| 4385 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4386 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4387 | } |
| 4388 | |
| 4389 | return 0; |
| 4390 | } |
| 4391 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4392 | /* fixup insn->imm field of bpf_call instructions |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4393 | * and inline eligible helpers as explicit sequence of BPF instructions |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4394 | * |
| 4395 | * this function is called after eBPF program passed verification |
| 4396 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4397 | static int fixup_bpf_calls(struct bpf_verifier_env *env) |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4398 | { |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4399 | struct bpf_prog *prog = env->prog; |
| 4400 | struct bpf_insn *insn = prog->insnsi; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4401 | const struct bpf_func_proto *fn; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4402 | const int insn_cnt = prog->len; |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4403 | struct bpf_insn insn_buf[16]; |
| 4404 | struct bpf_prog *new_prog; |
| 4405 | struct bpf_map *map_ptr; |
| 4406 | int i, cnt, delta = 0; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4407 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4408 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 4409 | if (insn->code != (BPF_JMP | BPF_CALL)) |
| 4410 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4411 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4412 | if (insn->imm == BPF_FUNC_get_route_realm) |
| 4413 | prog->dst_needed = 1; |
| 4414 | if (insn->imm == BPF_FUNC_get_prandom_u32) |
| 4415 | bpf_user_rnd_init_once(); |
Josef Bacik | 9802d86 | 2017-12-11 11:36:48 -0500 | [diff] [blame^] | 4416 | if (insn->imm == BPF_FUNC_override_return) |
| 4417 | prog->kprobe_override = 1; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4418 | if (insn->imm == BPF_FUNC_tail_call) { |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 4419 | /* If we tail call into other programs, we |
| 4420 | * cannot make any assumptions since they can |
| 4421 | * be replaced dynamically during runtime in |
| 4422 | * the program array. |
| 4423 | */ |
| 4424 | prog->cb_access = 1; |
Alexei Starovoitov | 80a58d0 | 2017-05-30 13:31:30 -0700 | [diff] [blame] | 4425 | env->prog->aux->stack_depth = MAX_BPF_STACK; |
David S. Miller | 7b9f6da | 2017-04-20 10:35:33 -0400 | [diff] [blame] | 4426 | |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4427 | /* mark bpf_tail_call as different opcode to avoid |
| 4428 | * conditional branch in the interpeter for every normal |
| 4429 | * call and to prevent accidental JITing by JIT compiler |
| 4430 | * that doesn't support bpf_tail_call yet |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4431 | */ |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4432 | insn->imm = 0; |
Alexei Starovoitov | 71189fa | 2017-05-30 13:31:27 -0700 | [diff] [blame] | 4433 | insn->code = BPF_JMP | BPF_TAIL_CALL; |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4434 | continue; |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4435 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4436 | |
Daniel Borkmann | 89c6307 | 2017-08-19 03:12:45 +0200 | [diff] [blame] | 4437 | /* BPF_EMIT_CALL() assumptions in some of the map_gen_lookup |
| 4438 | * handlers are currently limited to 64 bit only. |
| 4439 | */ |
| 4440 | if (ebpf_jit_enabled() && BITS_PER_LONG == 64 && |
| 4441 | insn->imm == BPF_FUNC_map_lookup_elem) { |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4442 | map_ptr = env->insn_aux_data[i + delta].map_ptr; |
Martin KaFai Lau | fad73a1 | 2017-03-22 10:00:32 -0700 | [diff] [blame] | 4443 | if (map_ptr == BPF_MAP_PTR_POISON || |
| 4444 | !map_ptr->ops->map_gen_lookup) |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4445 | goto patch_call_imm; |
| 4446 | |
| 4447 | cnt = map_ptr->ops->map_gen_lookup(map_ptr, insn_buf); |
| 4448 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4449 | verbose(env, "bpf verifier is misconfigured\n"); |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4450 | return -EINVAL; |
| 4451 | } |
| 4452 | |
| 4453 | new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, |
| 4454 | cnt); |
| 4455 | if (!new_prog) |
| 4456 | return -ENOMEM; |
| 4457 | |
| 4458 | delta += cnt - 1; |
| 4459 | |
| 4460 | /* keep walking new program and skip insns we just inserted */ |
| 4461 | env->prog = prog = new_prog; |
| 4462 | insn = new_prog->insnsi + i + delta; |
| 4463 | continue; |
| 4464 | } |
| 4465 | |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 4466 | if (insn->imm == BPF_FUNC_redirect_map) { |
Daniel Borkmann | 7c30013 | 2017-09-20 00:44:21 +0200 | [diff] [blame] | 4467 | /* Note, we cannot use prog directly as imm as subsequent |
| 4468 | * rewrites would still change the prog pointer. The only |
| 4469 | * stable address we can use is aux, which also works with |
| 4470 | * prog clones during blinding. |
| 4471 | */ |
| 4472 | u64 addr = (unsigned long)prog->aux; |
Daniel Borkmann | 109980b | 2017-09-08 00:14:51 +0200 | [diff] [blame] | 4473 | struct bpf_insn r4_ld[] = { |
| 4474 | BPF_LD_IMM64(BPF_REG_4, addr), |
| 4475 | *insn, |
| 4476 | }; |
| 4477 | cnt = ARRAY_SIZE(r4_ld); |
| 4478 | |
| 4479 | new_prog = bpf_patch_insn_data(env, i + delta, r4_ld, cnt); |
| 4480 | if (!new_prog) |
| 4481 | return -ENOMEM; |
| 4482 | |
| 4483 | delta += cnt - 1; |
| 4484 | env->prog = prog = new_prog; |
| 4485 | insn = new_prog->insnsi + i + delta; |
| 4486 | } |
Alexei Starovoitov | 81ed18a | 2017-03-15 18:26:42 -0700 | [diff] [blame] | 4487 | patch_call_imm: |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 4488 | fn = env->ops->get_func_proto(insn->imm); |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4489 | /* all functions that have prototype and verifier allowed |
| 4490 | * programs to call them, must be real in-kernel functions |
| 4491 | */ |
| 4492 | if (!fn->func) { |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4493 | verbose(env, |
| 4494 | "kernel subsystem misconfigured func %s#%d\n", |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4495 | func_id_name(insn->imm), insn->imm); |
| 4496 | return -EFAULT; |
| 4497 | } |
| 4498 | insn->imm = fn->func - __bpf_call_base; |
| 4499 | } |
| 4500 | |
| 4501 | return 0; |
| 4502 | } |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4503 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4504 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4505 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4506 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4507 | int i; |
| 4508 | |
| 4509 | if (!env->explored_states) |
| 4510 | return; |
| 4511 | |
| 4512 | for (i = 0; i < env->prog->len; i++) { |
| 4513 | sl = env->explored_states[i]; |
| 4514 | |
| 4515 | if (sl) |
| 4516 | while (sl != STATE_LIST_MARK) { |
| 4517 | sln = sl->next; |
Alexei Starovoitov | 1969db4 | 2017-11-01 00:08:04 -0700 | [diff] [blame] | 4518 | free_verifier_state(&sl->state, false); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4519 | kfree(sl); |
| 4520 | sl = sln; |
| 4521 | } |
| 4522 | } |
| 4523 | |
| 4524 | kfree(env->explored_states); |
| 4525 | } |
| 4526 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4527 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 4528 | { |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4529 | struct bpf_verifier_env *env; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4530 | struct bpf_verifer_log *log; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 4531 | int ret = -EINVAL; |
| 4532 | |
Arnd Bergmann | eba0c92 | 2017-11-02 12:05:52 +0100 | [diff] [blame] | 4533 | /* no program is valid */ |
| 4534 | if (ARRAY_SIZE(bpf_verifier_ops) == 0) |
| 4535 | return -EINVAL; |
| 4536 | |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4537 | /* '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] | 4538 | * allocate/free it every time bpf_check() is called |
| 4539 | */ |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4540 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4541 | if (!env) |
| 4542 | return -ENOMEM; |
Jakub Kicinski | 61bd521 | 2017-10-09 10:30:11 -0700 | [diff] [blame] | 4543 | log = &env->log; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4544 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4545 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 4546 | (*prog)->len); |
| 4547 | ret = -ENOMEM; |
| 4548 | if (!env->insn_aux_data) |
| 4549 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4550 | env->prog = *prog; |
Jakub Kicinski | 00176a3 | 2017-10-16 16:40:54 -0700 | [diff] [blame] | 4551 | env->ops = bpf_verifier_ops[env->prog->type]; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4552 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4553 | /* grab the mutex to protect few globals used by verifier */ |
| 4554 | mutex_lock(&bpf_verifier_lock); |
| 4555 | |
| 4556 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 4557 | /* user requested verbose verifier output |
| 4558 | * and supplied buffer to store the verification trace |
| 4559 | */ |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 4560 | log->level = attr->log_level; |
| 4561 | log->ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 4562 | log->len_total = attr->log_size; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4563 | |
| 4564 | ret = -EINVAL; |
Jakub Kicinski | e7bf824 | 2017-10-09 10:30:10 -0700 | [diff] [blame] | 4565 | /* log attributes have to be sane */ |
| 4566 | if (log->len_total < 128 || log->len_total > UINT_MAX >> 8 || |
| 4567 | !log->level || !log->ubuf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4568 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4569 | } |
Daniel Borkmann | 1ad2f58 | 2017-05-25 01:05:05 +0200 | [diff] [blame] | 4570 | |
| 4571 | env->strict_alignment = !!(attr->prog_flags & BPF_F_STRICT_ALIGNMENT); |
| 4572 | if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
David S. Miller | e07b98d | 2017-05-10 11:38:07 -0700 | [diff] [blame] | 4573 | env->strict_alignment = true; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4574 | |
Jakub Kicinski | ab3f006 | 2017-11-03 13:56:17 -0700 | [diff] [blame] | 4575 | if (env->prog->aux->offload) { |
| 4576 | ret = bpf_prog_offload_verifier_prep(env); |
| 4577 | if (ret) |
| 4578 | goto err_unlock; |
| 4579 | } |
| 4580 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4581 | ret = replace_map_fd_with_map_ptr(env); |
| 4582 | if (ret < 0) |
| 4583 | goto skip_full_check; |
| 4584 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4585 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8b | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 4586 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4587 | GFP_USER); |
| 4588 | ret = -ENOMEM; |
| 4589 | if (!env->explored_states) |
| 4590 | goto skip_full_check; |
| 4591 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 4592 | ret = check_cfg(env); |
| 4593 | if (ret < 0) |
| 4594 | goto skip_full_check; |
| 4595 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 4596 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 4597 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 4598 | ret = do_check(env); |
Craig Gallek | 8c01c4f | 2017-11-02 11:18:01 -0400 | [diff] [blame] | 4599 | if (env->cur_state) { |
| 4600 | free_verifier_state(env->cur_state, true); |
| 4601 | env->cur_state = NULL; |
| 4602 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4603 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4604 | skip_full_check: |
Alexei Starovoitov | 638f5b9 | 2017-10-31 18:16:05 -0700 | [diff] [blame] | 4605 | while (!pop_stack(env, NULL, NULL)); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 4606 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4607 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4608 | if (ret == 0) |
Alexei Starovoitov | c131187 | 2017-11-22 16:42:05 -0800 | [diff] [blame] | 4609 | sanitize_dead_code(env); |
| 4610 | |
| 4611 | if (ret == 0) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4612 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 4613 | ret = convert_ctx_accesses(env); |
| 4614 | |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4615 | if (ret == 0) |
Alexei Starovoitov | 79741b3 | 2017-03-15 18:26:40 -0700 | [diff] [blame] | 4616 | ret = fixup_bpf_calls(env); |
Alexei Starovoitov | e245c5c6 | 2017-03-15 18:26:39 -0700 | [diff] [blame] | 4617 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 4618 | if (log->level && bpf_verifier_log_full(log)) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4619 | ret = -ENOSPC; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 4620 | if (log->level && !log->ubuf) { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4621 | ret = -EFAULT; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 4622 | goto err_release_maps; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4623 | } |
| 4624 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4625 | if (ret == 0 && env->used_map_cnt) { |
| 4626 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4627 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 4628 | sizeof(env->used_maps[0]), |
| 4629 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4630 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4631 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4632 | ret = -ENOMEM; |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 4633 | goto err_release_maps; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4634 | } |
| 4635 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4636 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4637 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4638 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4639 | |
| 4640 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 4641 | * bpf_ld_imm64 instructions |
| 4642 | */ |
| 4643 | convert_pseudo_ld_imm64(env); |
| 4644 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4645 | |
Jakub Kicinski | a2a7d57 | 2017-10-09 10:30:15 -0700 | [diff] [blame] | 4646 | err_release_maps: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4647 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 4648 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 4649 | * them now. Otherwise free_bpf_prog_info() will release them. |
| 4650 | */ |
| 4651 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 4652 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4653 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 4654 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 4655 | vfree(env->insn_aux_data); |
| 4656 | err_free_env: |
| 4657 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 4658 | return ret; |
| 4659 | } |