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 | 58e2af8 | 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> |
| 22 | |
| 23 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 24 | * instruction by instruction and updates register/stack state. |
| 25 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 26 | * |
| 27 | * The first pass is depth-first-search to check that the program is a DAG. |
| 28 | * It rejects the following programs: |
| 29 | * - larger than BPF_MAXINSNS insns |
| 30 | * - if loop is present (detected via back-edge) |
| 31 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 32 | * - out of bounds or malformed jumps |
| 33 | * The second pass is all possible path descent from the 1st insn. |
| 34 | * Since it's analyzing all pathes through the program, the length of the |
| 35 | * analysis is limited to 32k insn, which may be hit even if total number of |
| 36 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 37 | * Number of 'branches to be analyzed' is limited to 1k |
| 38 | * |
| 39 | * On entry to each instruction, each register has a type, and the instruction |
| 40 | * changes the types of the registers depending on instruction semantics. |
| 41 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 42 | * copied to R1. |
| 43 | * |
| 44 | * All registers are 64-bit. |
| 45 | * R0 - return register |
| 46 | * R1-R5 argument passing registers |
| 47 | * R6-R9 callee saved registers |
| 48 | * R10 - frame pointer read-only |
| 49 | * |
| 50 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 51 | * and has type PTR_TO_CTX. |
| 52 | * |
| 53 | * Verifier tracks arithmetic operations on pointers in case: |
| 54 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 55 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 56 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 57 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 58 | * that it wants to construct a pointer to some element within stack. |
| 59 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 60 | * (and -20 constant is saved for further stack bounds checking). |
| 61 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 62 | * |
| 63 | * Most of the time the registers have UNKNOWN_VALUE type, which |
| 64 | * means the register has some value, but it's not a valid pointer. |
| 65 | * (like pointer plus pointer becomes UNKNOWN_VALUE type) |
| 66 | * |
| 67 | * When verifier sees load or store instructions the type of base register |
| 68 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer |
| 69 | * types recognized by check_mem_access() function. |
| 70 | * |
| 71 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 72 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 73 | * |
| 74 | * registers used to pass values to function calls are checked against |
| 75 | * function argument constraints. |
| 76 | * |
| 77 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 78 | * It means that the register type passed to this function must be |
| 79 | * PTR_TO_STACK and it will be used inside the function as |
| 80 | * 'pointer to map element key' |
| 81 | * |
| 82 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 83 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 84 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 85 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 86 | * |
| 87 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 88 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 89 | * 2nd argument should be a pointer to stack, which will be used inside |
| 90 | * the helper function as a pointer to map element key. |
| 91 | * |
| 92 | * On the kernel side the helper function looks like: |
| 93 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 94 | * { |
| 95 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 96 | * void *key = (void *) (unsigned long) r2; |
| 97 | * void *value; |
| 98 | * |
| 99 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 100 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 101 | * the stack of eBPF program. |
| 102 | * } |
| 103 | * |
| 104 | * Corresponding eBPF program may look like: |
| 105 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 106 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 107 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 108 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 109 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 110 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 111 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 112 | * |
| 113 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 114 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 115 | * and were initialized prior to this call. |
| 116 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 117 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 118 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 119 | * returns ether pointer to map value or NULL. |
| 120 | * |
| 121 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 122 | * insn, the register holding that pointer in the true branch changes state to |
| 123 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 124 | * branch. See check_cond_jmp_op(). |
| 125 | * |
| 126 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 127 | * are set to NOT_INIT to indicate that they are no longer readable. |
| 128 | */ |
| 129 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 130 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 131 | struct bpf_verifier_stack_elem { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 132 | /* verifer state is 'st' |
| 133 | * before processing instruction 'insn_idx' |
| 134 | * and after processing instruction 'prev_insn_idx' |
| 135 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 136 | struct bpf_verifier_state st; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 137 | int insn_idx; |
| 138 | int prev_insn_idx; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 139 | struct bpf_verifier_stack_elem *next; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 142 | #define BPF_COMPLEXITY_LIMIT_INSNS 65536 |
| 143 | #define BPF_COMPLEXITY_LIMIT_STACK 1024 |
| 144 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 145 | struct bpf_call_arg_meta { |
| 146 | struct bpf_map *map_ptr; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 147 | bool raw_mode; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 148 | bool pkt_access; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 149 | int regno; |
| 150 | int access_size; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 151 | }; |
| 152 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 153 | /* verbose verifier prints what it's seeing |
| 154 | * bpf_check() is called under lock, so no race to access these global vars |
| 155 | */ |
| 156 | static u32 log_level, log_size, log_len; |
| 157 | static char *log_buf; |
| 158 | |
| 159 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 160 | |
| 161 | /* log_level controls verbosity level of eBPF verifier. |
| 162 | * verbose() is used to dump the verification trace to the log, so the user |
| 163 | * can figure out what's wrong with the program |
| 164 | */ |
Daniel Borkmann | 1d056d9 | 2015-11-03 11:39:20 +0100 | [diff] [blame] | 165 | static __printf(1, 2) void verbose(const char *fmt, ...) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 166 | { |
| 167 | va_list args; |
| 168 | |
| 169 | if (log_level == 0 || log_len >= log_size - 1) |
| 170 | return; |
| 171 | |
| 172 | va_start(args, fmt); |
| 173 | log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args); |
| 174 | va_end(args); |
| 175 | } |
| 176 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 177 | /* string representation of 'enum bpf_reg_type' */ |
| 178 | static const char * const reg_type_str[] = { |
| 179 | [NOT_INIT] = "?", |
| 180 | [UNKNOWN_VALUE] = "inv", |
| 181 | [PTR_TO_CTX] = "ctx", |
| 182 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 183 | [PTR_TO_MAP_VALUE] = "map_value", |
| 184 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 185 | [PTR_TO_MAP_VALUE_ADJ] = "map_value_adj", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 186 | [FRAME_PTR] = "fp", |
| 187 | [PTR_TO_STACK] = "fp", |
| 188 | [CONST_IMM] = "imm", |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 189 | [PTR_TO_PACKET] = "pkt", |
| 190 | [PTR_TO_PACKET_END] = "pkt_end", |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 193 | static void print_verifier_state(struct bpf_verifier_state *state) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 194 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 195 | struct bpf_reg_state *reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 196 | enum bpf_reg_type t; |
| 197 | int i; |
| 198 | |
| 199 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 200 | reg = &state->regs[i]; |
| 201 | t = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 202 | if (t == NOT_INIT) |
| 203 | continue; |
| 204 | verbose(" R%d=%s", i, reg_type_str[t]); |
| 205 | if (t == CONST_IMM || t == PTR_TO_STACK) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 206 | verbose("%lld", reg->imm); |
| 207 | else if (t == PTR_TO_PACKET) |
| 208 | verbose("(id=%d,off=%d,r=%d)", |
| 209 | reg->id, reg->off, reg->range); |
| 210 | else if (t == UNKNOWN_VALUE && reg->imm) |
| 211 | verbose("%lld", reg->imm); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 212 | else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE || |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 213 | t == PTR_TO_MAP_VALUE_OR_NULL || |
| 214 | t == PTR_TO_MAP_VALUE_ADJ) |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 215 | verbose("(ks=%d,vs=%d,id=%u)", |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 216 | reg->map_ptr->key_size, |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 217 | reg->map_ptr->value_size, |
| 218 | reg->id); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 219 | if (reg->min_value != BPF_REGISTER_MIN_RANGE) |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 220 | verbose(",min_value=%lld", |
| 221 | (long long)reg->min_value); |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 222 | if (reg->max_value != BPF_REGISTER_MAX_RANGE) |
| 223 | verbose(",max_value=%llu", |
| 224 | (unsigned long long)reg->max_value); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 225 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 226 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 227 | if (state->stack_slot_type[i] == STACK_SPILL) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 228 | verbose(" fp%d=%s", -MAX_BPF_STACK + i, |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 229 | reg_type_str[state->spilled_regs[i / BPF_REG_SIZE].type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 230 | } |
| 231 | verbose("\n"); |
| 232 | } |
| 233 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 234 | static const char *const bpf_class_string[] = { |
| 235 | [BPF_LD] = "ld", |
| 236 | [BPF_LDX] = "ldx", |
| 237 | [BPF_ST] = "st", |
| 238 | [BPF_STX] = "stx", |
| 239 | [BPF_ALU] = "alu", |
| 240 | [BPF_JMP] = "jmp", |
| 241 | [BPF_RET] = "BUG", |
| 242 | [BPF_ALU64] = "alu64", |
| 243 | }; |
| 244 | |
Alexei Starovoitov | 687f071 | 2015-09-08 13:40:01 -0700 | [diff] [blame] | 245 | static const char *const bpf_alu_string[16] = { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 246 | [BPF_ADD >> 4] = "+=", |
| 247 | [BPF_SUB >> 4] = "-=", |
| 248 | [BPF_MUL >> 4] = "*=", |
| 249 | [BPF_DIV >> 4] = "/=", |
| 250 | [BPF_OR >> 4] = "|=", |
| 251 | [BPF_AND >> 4] = "&=", |
| 252 | [BPF_LSH >> 4] = "<<=", |
| 253 | [BPF_RSH >> 4] = ">>=", |
| 254 | [BPF_NEG >> 4] = "neg", |
| 255 | [BPF_MOD >> 4] = "%=", |
| 256 | [BPF_XOR >> 4] = "^=", |
| 257 | [BPF_MOV >> 4] = "=", |
| 258 | [BPF_ARSH >> 4] = "s>>=", |
| 259 | [BPF_END >> 4] = "endian", |
| 260 | }; |
| 261 | |
| 262 | static const char *const bpf_ldst_string[] = { |
| 263 | [BPF_W >> 3] = "u32", |
| 264 | [BPF_H >> 3] = "u16", |
| 265 | [BPF_B >> 3] = "u8", |
| 266 | [BPF_DW >> 3] = "u64", |
| 267 | }; |
| 268 | |
Alexei Starovoitov | 687f071 | 2015-09-08 13:40:01 -0700 | [diff] [blame] | 269 | static const char *const bpf_jmp_string[16] = { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 270 | [BPF_JA >> 4] = "jmp", |
| 271 | [BPF_JEQ >> 4] = "==", |
| 272 | [BPF_JGT >> 4] = ">", |
| 273 | [BPF_JGE >> 4] = ">=", |
| 274 | [BPF_JSET >> 4] = "&", |
| 275 | [BPF_JNE >> 4] = "!=", |
| 276 | [BPF_JSGT >> 4] = "s>", |
| 277 | [BPF_JSGE >> 4] = "s>=", |
| 278 | [BPF_CALL >> 4] = "call", |
| 279 | [BPF_EXIT >> 4] = "exit", |
| 280 | }; |
| 281 | |
Daniel Borkmann | ced0a31 | 2017-05-08 00:04:09 +0200 | [diff] [blame] | 282 | static void print_bpf_insn(const struct bpf_verifier_env *env, |
| 283 | const struct bpf_insn *insn) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 284 | { |
| 285 | u8 class = BPF_CLASS(insn->code); |
| 286 | |
| 287 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 288 | if (BPF_SRC(insn->code) == BPF_X) |
| 289 | verbose("(%02x) %sr%d %s %sr%d\n", |
| 290 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 291 | insn->dst_reg, |
| 292 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 293 | class == BPF_ALU ? "(u32) " : "", |
| 294 | insn->src_reg); |
| 295 | else |
| 296 | verbose("(%02x) %sr%d %s %s%d\n", |
| 297 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 298 | insn->dst_reg, |
| 299 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 300 | class == BPF_ALU ? "(u32) " : "", |
| 301 | insn->imm); |
| 302 | } else if (class == BPF_STX) { |
| 303 | if (BPF_MODE(insn->code) == BPF_MEM) |
| 304 | verbose("(%02x) *(%s *)(r%d %+d) = r%d\n", |
| 305 | insn->code, |
| 306 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 307 | insn->dst_reg, |
| 308 | insn->off, insn->src_reg); |
| 309 | else if (BPF_MODE(insn->code) == BPF_XADD) |
| 310 | verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n", |
| 311 | insn->code, |
| 312 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 313 | insn->dst_reg, insn->off, |
| 314 | insn->src_reg); |
| 315 | else |
| 316 | verbose("BUG_%02x\n", insn->code); |
| 317 | } else if (class == BPF_ST) { |
| 318 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 319 | verbose("BUG_st_%02x\n", insn->code); |
| 320 | return; |
| 321 | } |
| 322 | verbose("(%02x) *(%s *)(r%d %+d) = %d\n", |
| 323 | insn->code, |
| 324 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 325 | insn->dst_reg, |
| 326 | insn->off, insn->imm); |
| 327 | } else if (class == BPF_LDX) { |
| 328 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 329 | verbose("BUG_ldx_%02x\n", insn->code); |
| 330 | return; |
| 331 | } |
| 332 | verbose("(%02x) r%d = *(%s *)(r%d %+d)\n", |
| 333 | insn->code, insn->dst_reg, |
| 334 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 335 | insn->src_reg, insn->off); |
| 336 | } else if (class == BPF_LD) { |
| 337 | if (BPF_MODE(insn->code) == BPF_ABS) { |
| 338 | verbose("(%02x) r0 = *(%s *)skb[%d]\n", |
| 339 | insn->code, |
| 340 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 341 | insn->imm); |
| 342 | } else if (BPF_MODE(insn->code) == BPF_IND) { |
| 343 | verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n", |
| 344 | insn->code, |
| 345 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 346 | insn->src_reg, insn->imm); |
Daniel Borkmann | ced0a31 | 2017-05-08 00:04:09 +0200 | [diff] [blame] | 347 | } else if (BPF_MODE(insn->code) == BPF_IMM && |
| 348 | BPF_SIZE(insn->code) == BPF_DW) { |
| 349 | /* At this point, we already made sure that the second |
| 350 | * part of the ldimm64 insn is accessible. |
| 351 | */ |
| 352 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 353 | bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD; |
| 354 | |
| 355 | if (map_ptr && !env->allow_ptr_leaks) |
| 356 | imm = 0; |
| 357 | |
| 358 | verbose("(%02x) r%d = 0x%llx\n", insn->code, |
| 359 | insn->dst_reg, (unsigned long long)imm); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 360 | } else { |
| 361 | verbose("BUG_ld_%02x\n", insn->code); |
| 362 | return; |
| 363 | } |
| 364 | } else if (class == BPF_JMP) { |
| 365 | u8 opcode = BPF_OP(insn->code); |
| 366 | |
| 367 | if (opcode == BPF_CALL) { |
| 368 | verbose("(%02x) call %d\n", insn->code, insn->imm); |
| 369 | } else if (insn->code == (BPF_JMP | BPF_JA)) { |
| 370 | verbose("(%02x) goto pc%+d\n", |
| 371 | insn->code, insn->off); |
| 372 | } else if (insn->code == (BPF_JMP | BPF_EXIT)) { |
| 373 | verbose("(%02x) exit\n", insn->code); |
| 374 | } else if (BPF_SRC(insn->code) == BPF_X) { |
| 375 | verbose("(%02x) if r%d %s r%d goto pc%+d\n", |
| 376 | insn->code, insn->dst_reg, |
| 377 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 378 | insn->src_reg, insn->off); |
| 379 | } else { |
| 380 | verbose("(%02x) if r%d %s 0x%x goto pc%+d\n", |
| 381 | insn->code, insn->dst_reg, |
| 382 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 383 | insn->imm, insn->off); |
| 384 | } |
| 385 | } else { |
| 386 | verbose("(%02x) %s\n", insn->code, bpf_class_string[class]); |
| 387 | } |
| 388 | } |
| 389 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 390 | static int pop_stack(struct bpf_verifier_env *env, int *prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 391 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 392 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 393 | int insn_idx; |
| 394 | |
| 395 | if (env->head == NULL) |
| 396 | return -1; |
| 397 | |
| 398 | memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state)); |
| 399 | insn_idx = env->head->insn_idx; |
| 400 | if (prev_insn_idx) |
| 401 | *prev_insn_idx = env->head->prev_insn_idx; |
| 402 | elem = env->head->next; |
| 403 | kfree(env->head); |
| 404 | env->head = elem; |
| 405 | env->stack_size--; |
| 406 | return insn_idx; |
| 407 | } |
| 408 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 409 | static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, |
| 410 | int insn_idx, int prev_insn_idx) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 411 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 412 | struct bpf_verifier_stack_elem *elem; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 413 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 414 | elem = kmalloc(sizeof(struct bpf_verifier_stack_elem), GFP_KERNEL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 415 | if (!elem) |
| 416 | goto err; |
| 417 | |
| 418 | memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state)); |
| 419 | elem->insn_idx = insn_idx; |
| 420 | elem->prev_insn_idx = prev_insn_idx; |
| 421 | elem->next = env->head; |
| 422 | env->head = elem; |
| 423 | env->stack_size++; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 424 | if (env->stack_size > BPF_COMPLEXITY_LIMIT_STACK) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 425 | verbose("BPF program is too complex\n"); |
| 426 | goto err; |
| 427 | } |
| 428 | return &elem->st; |
| 429 | err: |
| 430 | /* pop all elements and return */ |
| 431 | while (pop_stack(env, NULL) >= 0); |
| 432 | return NULL; |
| 433 | } |
| 434 | |
| 435 | #define CALLER_SAVED_REGS 6 |
| 436 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 437 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 438 | }; |
| 439 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 440 | static void init_reg_state(struct bpf_reg_state *regs) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 441 | { |
| 442 | int i; |
| 443 | |
| 444 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 445 | regs[i].type = NOT_INIT; |
| 446 | regs[i].imm = 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 447 | regs[i].min_value = BPF_REGISTER_MIN_RANGE; |
| 448 | regs[i].max_value = BPF_REGISTER_MAX_RANGE; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* frame pointer */ |
| 452 | regs[BPF_REG_FP].type = FRAME_PTR; |
| 453 | |
| 454 | /* 1st arg to a function */ |
| 455 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 456 | } |
| 457 | |
Daniel Borkmann | 0e0f1d6 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 458 | static void __mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 459 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 460 | regs[regno].type = UNKNOWN_VALUE; |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 461 | regs[regno].id = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 462 | regs[regno].imm = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Daniel Borkmann | 0e0f1d6 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 465 | static void mark_reg_unknown_value(struct bpf_reg_state *regs, u32 regno) |
| 466 | { |
| 467 | BUG_ON(regno >= MAX_BPF_REG); |
| 468 | __mark_reg_unknown_value(regs, regno); |
| 469 | } |
| 470 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 471 | static void reset_reg_range_values(struct bpf_reg_state *regs, u32 regno) |
| 472 | { |
| 473 | regs[regno].min_value = BPF_REGISTER_MIN_RANGE; |
| 474 | regs[regno].max_value = BPF_REGISTER_MAX_RANGE; |
| 475 | } |
| 476 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 477 | enum reg_arg_type { |
| 478 | SRC_OP, /* register is used as source operand */ |
| 479 | DST_OP, /* register is used as destination operand */ |
| 480 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 481 | }; |
| 482 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 483 | static int check_reg_arg(struct bpf_reg_state *regs, u32 regno, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 484 | enum reg_arg_type t) |
| 485 | { |
| 486 | if (regno >= MAX_BPF_REG) { |
| 487 | verbose("R%d is invalid\n", regno); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
| 491 | if (t == SRC_OP) { |
| 492 | /* check whether register used as source operand can be read */ |
| 493 | if (regs[regno].type == NOT_INIT) { |
| 494 | verbose("R%d !read_ok\n", regno); |
| 495 | return -EACCES; |
| 496 | } |
| 497 | } else { |
| 498 | /* check whether register used as dest operand can be written to */ |
| 499 | if (regno == BPF_REG_FP) { |
| 500 | verbose("frame pointer is read only\n"); |
| 501 | return -EACCES; |
| 502 | } |
| 503 | if (t == DST_OP) |
| 504 | mark_reg_unknown_value(regs, regno); |
| 505 | } |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int bpf_size_to_bytes(int bpf_size) |
| 510 | { |
| 511 | if (bpf_size == BPF_W) |
| 512 | return 4; |
| 513 | else if (bpf_size == BPF_H) |
| 514 | return 2; |
| 515 | else if (bpf_size == BPF_B) |
| 516 | return 1; |
| 517 | else if (bpf_size == BPF_DW) |
| 518 | return 8; |
| 519 | else |
| 520 | return -EINVAL; |
| 521 | } |
| 522 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 523 | static bool is_spillable_regtype(enum bpf_reg_type type) |
| 524 | { |
| 525 | switch (type) { |
| 526 | case PTR_TO_MAP_VALUE: |
| 527 | case PTR_TO_MAP_VALUE_OR_NULL: |
| 528 | case PTR_TO_STACK: |
| 529 | case PTR_TO_CTX: |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 530 | case PTR_TO_PACKET: |
| 531 | case PTR_TO_PACKET_END: |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 532 | case FRAME_PTR: |
| 533 | case CONST_PTR_TO_MAP: |
| 534 | return true; |
| 535 | default: |
| 536 | return false; |
| 537 | } |
| 538 | } |
| 539 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 540 | /* check_stack_read/write functions track spill/fill of registers, |
| 541 | * stack boundary and alignment are checked in check_mem_access() |
| 542 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 543 | static int check_stack_write(struct bpf_verifier_state *state, int off, |
| 544 | int size, int value_regno) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 545 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 546 | int i; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 547 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 548 | * so it's aligned access and [off, off + size) are within stack limits |
| 549 | */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 550 | |
| 551 | if (value_regno >= 0 && |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 552 | is_spillable_regtype(state->regs[value_regno].type)) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 553 | |
| 554 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 555 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 556 | verbose("invalid size of register spill\n"); |
| 557 | return -EACCES; |
| 558 | } |
| 559 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 560 | /* save register state */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 561 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
| 562 | state->regs[value_regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 563 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 564 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 565 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL; |
| 566 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 567 | /* regular write of data into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 568 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 569 | (struct bpf_reg_state) {}; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 570 | |
| 571 | for (i = 0; i < size; i++) |
| 572 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 573 | } |
| 574 | return 0; |
| 575 | } |
| 576 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 577 | static int check_stack_read(struct bpf_verifier_state *state, int off, int size, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 578 | int value_regno) |
| 579 | { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 580 | u8 *slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 581 | int i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 582 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 583 | slot_type = &state->stack_slot_type[MAX_BPF_STACK + off]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 584 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 585 | if (slot_type[0] == STACK_SPILL) { |
| 586 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 587 | verbose("invalid size of register spill\n"); |
| 588 | return -EACCES; |
| 589 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 590 | for (i = 1; i < BPF_REG_SIZE; i++) { |
| 591 | if (slot_type[i] != STACK_SPILL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 592 | verbose("corrupted spill memory\n"); |
| 593 | return -EACCES; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if (value_regno >= 0) |
| 598 | /* restore register state from stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 599 | state->regs[value_regno] = |
| 600 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 601 | return 0; |
| 602 | } else { |
| 603 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 604 | if (slot_type[i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 605 | verbose("invalid read from stack off %d+%d size %d\n", |
| 606 | off, i, size); |
| 607 | return -EACCES; |
| 608 | } |
| 609 | } |
| 610 | if (value_regno >= 0) |
| 611 | /* have read misc data from the stack */ |
| 612 | mark_reg_unknown_value(state->regs, value_regno); |
| 613 | return 0; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 618 | static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 619 | int size) |
| 620 | { |
| 621 | struct bpf_map *map = env->cur_state.regs[regno].map_ptr; |
| 622 | |
| 623 | if (off < 0 || off + size > map->value_size) { |
| 624 | verbose("invalid access to map value, value_size=%d off=%d size=%d\n", |
| 625 | map->value_size, off, size); |
| 626 | return -EACCES; |
| 627 | } |
| 628 | return 0; |
| 629 | } |
| 630 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 631 | #define MAX_PACKET_OFF 0xffff |
| 632 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 633 | static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 634 | const struct bpf_call_arg_meta *meta) |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 635 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 636 | switch (env->prog->type) { |
| 637 | case BPF_PROG_TYPE_SCHED_CLS: |
| 638 | case BPF_PROG_TYPE_SCHED_ACT: |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 639 | case BPF_PROG_TYPE_XDP: |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 640 | if (meta) |
| 641 | return meta->pkt_access; |
| 642 | |
| 643 | env->seen_direct_write = true; |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 644 | return true; |
| 645 | default: |
| 646 | return false; |
| 647 | } |
| 648 | } |
| 649 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 650 | static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off, |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 651 | int size) |
| 652 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 653 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 654 | struct bpf_reg_state *reg = ®s[regno]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 655 | |
Alexei Starovoitov | d91b28e | 2016-05-19 18:17:13 -0700 | [diff] [blame] | 656 | off += reg->off; |
Daniel Borkmann | b399cf6 | 2016-09-20 00:26:12 +0200 | [diff] [blame] | 657 | if (off < 0 || size <= 0 || off + size > reg->range) { |
Alexei Starovoitov | d91b28e | 2016-05-19 18:17:13 -0700 | [diff] [blame] | 658 | verbose("invalid access to packet, off=%d size=%d, R%d(id=%d,off=%d,r=%d)\n", |
| 659 | off, size, regno, reg->id, reg->off, reg->range); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 660 | return -EACCES; |
| 661 | } |
| 662 | return 0; |
| 663 | } |
| 664 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 665 | /* check access to 'struct bpf_context' fields */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 666 | static int check_ctx_access(struct bpf_verifier_env *env, int off, int size, |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 667 | enum bpf_access_type t, enum bpf_reg_type *reg_type) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 668 | { |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 669 | /* for analyzer ctx accesses are already validated and converted */ |
| 670 | if (env->analyzer_ops) |
| 671 | return 0; |
| 672 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 673 | if (env->prog->aux->ops->is_valid_access && |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 674 | env->prog->aux->ops->is_valid_access(off, size, t, reg_type)) { |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 675 | /* remember the offset of last byte accessed in ctx */ |
| 676 | if (env->prog->aux->max_ctx_offset < off + size) |
| 677 | env->prog->aux->max_ctx_offset = off + size; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 678 | return 0; |
Alexei Starovoitov | 32bbe00 | 2016-04-06 18:43:28 -0700 | [diff] [blame] | 679 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 680 | |
| 681 | verbose("invalid bpf_context access off=%d size=%d\n", off, size); |
| 682 | return -EACCES; |
| 683 | } |
| 684 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 685 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 686 | { |
| 687 | if (env->allow_ptr_leaks) |
| 688 | return false; |
| 689 | |
| 690 | switch (env->cur_state.regs[regno].type) { |
| 691 | case UNKNOWN_VALUE: |
| 692 | case CONST_IMM: |
| 693 | return false; |
| 694 | default: |
| 695 | return true; |
| 696 | } |
| 697 | } |
| 698 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 699 | static int check_ptr_alignment(struct bpf_verifier_env *env, |
| 700 | struct bpf_reg_state *reg, int off, int size) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 701 | { |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 702 | if (reg->type != PTR_TO_PACKET && reg->type != PTR_TO_MAP_VALUE_ADJ) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 703 | if (off % size != 0) { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 704 | verbose("misaligned access off %d size %d\n", |
| 705 | off, size); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 706 | return -EACCES; |
| 707 | } else { |
| 708 | return 0; |
| 709 | } |
| 710 | } |
| 711 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 712 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) |
| 713 | /* misaligned access to packet is ok on x86,arm,arm64 */ |
| 714 | return 0; |
| 715 | |
| 716 | if (reg->id && size != 1) { |
| 717 | verbose("Unknown packet alignment. Only byte-sized access allowed\n"); |
| 718 | return -EACCES; |
| 719 | } |
| 720 | |
| 721 | /* skb->data is NET_IP_ALIGN-ed */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 722 | if (reg->type == PTR_TO_PACKET && |
| 723 | (NET_IP_ALIGN + reg->off + off) % size != 0) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 724 | verbose("misaligned packet access off %d+%d+%d size %d\n", |
| 725 | NET_IP_ALIGN, reg->off, off, size); |
| 726 | return -EACCES; |
| 727 | } |
| 728 | return 0; |
| 729 | } |
| 730 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 731 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 732 | * if t==write, value_regno is a register which value is stored into memory |
| 733 | * if t==read, value_regno is a register which will receive the value from memory |
| 734 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 735 | * if t==read && value_regno==-1, don't care what we read from memory |
| 736 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 737 | static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 738 | int bpf_size, enum bpf_access_type t, |
| 739 | int value_regno) |
| 740 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 741 | struct bpf_verifier_state *state = &env->cur_state; |
| 742 | struct bpf_reg_state *reg = &state->regs[regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 743 | int size, err = 0; |
| 744 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 745 | if (reg->type == PTR_TO_STACK) |
| 746 | off += reg->imm; |
Alex Gartrell | 24b4d2a | 2015-07-23 14:24:40 -0700 | [diff] [blame] | 747 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 748 | size = bpf_size_to_bytes(bpf_size); |
| 749 | if (size < 0) |
| 750 | return size; |
| 751 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 752 | err = check_ptr_alignment(env, reg, off, size); |
| 753 | if (err) |
| 754 | return err; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 755 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 756 | if (reg->type == PTR_TO_MAP_VALUE || |
| 757 | reg->type == PTR_TO_MAP_VALUE_ADJ) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 758 | if (t == BPF_WRITE && value_regno >= 0 && |
| 759 | is_pointer_value(env, value_regno)) { |
| 760 | verbose("R%d leaks addr into map\n", value_regno); |
| 761 | return -EACCES; |
| 762 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 763 | |
| 764 | /* If we adjusted the register to this map value at all then we |
| 765 | * need to change off and size to min_value and max_value |
| 766 | * respectively to make sure our theoretical access will be |
| 767 | * safe. |
| 768 | */ |
| 769 | if (reg->type == PTR_TO_MAP_VALUE_ADJ) { |
| 770 | if (log_level) |
| 771 | print_verifier_state(state); |
| 772 | env->varlen_map_value_access = true; |
| 773 | /* The minimum value is only important with signed |
| 774 | * comparisons where we can't assume the floor of a |
| 775 | * value is 0. If we are using signed variables for our |
| 776 | * index'es we need to make sure that whatever we use |
| 777 | * will have a set floor within our range. |
| 778 | */ |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 779 | if (reg->min_value < 0) { |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 780 | verbose("R%d min value is negative, either use unsigned index or do a if (index >=0) check.\n", |
| 781 | regno); |
| 782 | return -EACCES; |
| 783 | } |
| 784 | err = check_map_access(env, regno, reg->min_value + off, |
| 785 | size); |
| 786 | if (err) { |
| 787 | verbose("R%d min value is outside of the array range\n", |
| 788 | regno); |
| 789 | return err; |
| 790 | } |
| 791 | |
| 792 | /* If we haven't set a max value then we need to bail |
| 793 | * since we can't be sure we won't do bad things. |
| 794 | */ |
| 795 | if (reg->max_value == BPF_REGISTER_MAX_RANGE) { |
| 796 | verbose("R%d unbounded memory access, make sure to bounds check any array access into a map\n", |
| 797 | regno); |
| 798 | return -EACCES; |
| 799 | } |
| 800 | off += reg->max_value; |
| 801 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 802 | err = check_map_access(env, regno, off, size); |
| 803 | if (!err && t == BPF_READ && value_regno >= 0) |
| 804 | mark_reg_unknown_value(state->regs, value_regno); |
| 805 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 806 | } else if (reg->type == PTR_TO_CTX) { |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 807 | enum bpf_reg_type reg_type = UNKNOWN_VALUE; |
| 808 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 809 | if (t == BPF_WRITE && value_regno >= 0 && |
| 810 | is_pointer_value(env, value_regno)) { |
| 811 | verbose("R%d leaks addr into ctx\n", value_regno); |
| 812 | return -EACCES; |
| 813 | } |
Alexei Starovoitov | 19de99f | 2016-06-15 18:25:38 -0700 | [diff] [blame] | 814 | err = check_ctx_access(env, off, size, t, ®_type); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 815 | if (!err && t == BPF_READ && value_regno >= 0) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 816 | mark_reg_unknown_value(state->regs, value_regno); |
Mickaël Salaün | 1955351 | 2016-09-24 20:01:50 +0200 | [diff] [blame] | 817 | /* note that reg.[id|off|range] == 0 */ |
| 818 | state->regs[value_regno].type = reg_type; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 819 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 820 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 821 | } else if (reg->type == FRAME_PTR || reg->type == PTR_TO_STACK) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 822 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 823 | verbose("invalid stack off=%d size=%d\n", off, size); |
| 824 | return -EACCES; |
| 825 | } |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 826 | if (t == BPF_WRITE) { |
| 827 | if (!env->allow_ptr_leaks && |
| 828 | state->stack_slot_type[MAX_BPF_STACK + off] == STACK_SPILL && |
| 829 | size != BPF_REG_SIZE) { |
| 830 | verbose("attempt to corrupt spilled pointer on stack\n"); |
| 831 | return -EACCES; |
| 832 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 833 | err = check_stack_write(state, off, size, value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 834 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 835 | err = check_stack_read(state, off, size, value_regno); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 836 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 837 | } else if (state->regs[regno].type == PTR_TO_PACKET) { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 838 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL)) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 839 | verbose("cannot write into packet\n"); |
| 840 | return -EACCES; |
| 841 | } |
Brenden Blanco | 4acf6c0 | 2016-07-19 12:16:56 -0700 | [diff] [blame] | 842 | if (t == BPF_WRITE && value_regno >= 0 && |
| 843 | is_pointer_value(env, value_regno)) { |
| 844 | verbose("R%d leaks addr into packet\n", value_regno); |
| 845 | return -EACCES; |
| 846 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 847 | err = check_packet_access(env, regno, off, size); |
| 848 | if (!err && t == BPF_READ && value_regno >= 0) |
| 849 | mark_reg_unknown_value(state->regs, value_regno); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 850 | } else { |
| 851 | verbose("R%d invalid mem access '%s'\n", |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 852 | regno, reg_type_str[reg->type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 853 | return -EACCES; |
| 854 | } |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 855 | |
| 856 | if (!err && size <= 2 && value_regno >= 0 && env->allow_ptr_leaks && |
| 857 | state->regs[value_regno].type == UNKNOWN_VALUE) { |
| 858 | /* 1 or 2 byte load zero-extends, determine the number of |
| 859 | * zero upper bits. Not doing it fo 4 byte load, since |
| 860 | * such values cannot be added to ptr_to_packet anyway. |
| 861 | */ |
| 862 | state->regs[value_regno].imm = 64 - size * 8; |
| 863 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 864 | return err; |
| 865 | } |
| 866 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 867 | static int check_xadd(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 868 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 869 | struct bpf_reg_state *regs = env->cur_state.regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 870 | int err; |
| 871 | |
| 872 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 873 | insn->imm != 0) { |
| 874 | verbose("BPF_XADD uses reserved fields\n"); |
| 875 | return -EINVAL; |
| 876 | } |
| 877 | |
| 878 | /* check src1 operand */ |
| 879 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 880 | if (err) |
| 881 | return err; |
| 882 | |
| 883 | /* check src2 operand */ |
| 884 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 885 | if (err) |
| 886 | return err; |
| 887 | |
| 888 | /* check whether atomic_add can read the memory */ |
| 889 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 890 | BPF_SIZE(insn->code), BPF_READ, -1); |
| 891 | if (err) |
| 892 | return err; |
| 893 | |
| 894 | /* check whether atomic_add can write into the same memory */ |
| 895 | return check_mem_access(env, insn->dst_reg, insn->off, |
| 896 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
| 897 | } |
| 898 | |
| 899 | /* when register 'regno' is passed into function that will read 'access_size' |
| 900 | * bytes from that pointer, make sure that it's within stack boundary |
| 901 | * and all elements of stack are initialized |
| 902 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 903 | static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 904 | int access_size, bool zero_size_allowed, |
| 905 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 906 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 907 | struct bpf_verifier_state *state = &env->cur_state; |
| 908 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 909 | int off, i; |
| 910 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 911 | if (regs[regno].type != PTR_TO_STACK) { |
| 912 | if (zero_size_allowed && access_size == 0 && |
| 913 | regs[regno].type == CONST_IMM && |
| 914 | regs[regno].imm == 0) |
| 915 | return 0; |
| 916 | |
| 917 | verbose("R%d type=%s expected=%s\n", regno, |
| 918 | reg_type_str[regs[regno].type], |
| 919 | reg_type_str[PTR_TO_STACK]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 920 | return -EACCES; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 921 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 922 | |
| 923 | off = regs[regno].imm; |
| 924 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 925 | access_size <= 0) { |
| 926 | verbose("invalid stack type R%d off=%d access_size=%d\n", |
| 927 | regno, off, access_size); |
| 928 | return -EACCES; |
| 929 | } |
| 930 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 931 | if (meta && meta->raw_mode) { |
| 932 | meta->access_size = access_size; |
| 933 | meta->regno = regno; |
| 934 | return 0; |
| 935 | } |
| 936 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 937 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 938 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 939 | verbose("invalid indirect read from stack off %d+%d size %d\n", |
| 940 | off, i, access_size); |
| 941 | return -EACCES; |
| 942 | } |
| 943 | } |
| 944 | return 0; |
| 945 | } |
| 946 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 947 | static int check_func_arg(struct bpf_verifier_env *env, u32 regno, |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 948 | enum bpf_arg_type arg_type, |
| 949 | struct bpf_call_arg_meta *meta) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 950 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 951 | struct bpf_reg_state *regs = env->cur_state.regs, *reg = ®s[regno]; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 952 | enum bpf_reg_type expected_type, type = reg->type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 953 | int err = 0; |
| 954 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 955 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 956 | return 0; |
| 957 | |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 958 | if (type == NOT_INIT) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 959 | verbose("R%d !read_ok\n", regno); |
| 960 | return -EACCES; |
| 961 | } |
| 962 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 963 | if (arg_type == ARG_ANYTHING) { |
| 964 | if (is_pointer_value(env, regno)) { |
| 965 | verbose("R%d leaks addr into helper function\n", regno); |
| 966 | return -EACCES; |
| 967 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 968 | return 0; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 969 | } |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 970 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 971 | if (type == PTR_TO_PACKET && !may_access_direct_pkt_data(env, meta)) { |
| 972 | verbose("helper access to the packet is not allowed\n"); |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 973 | return -EACCES; |
| 974 | } |
| 975 | |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 976 | if (arg_type == ARG_PTR_TO_MAP_KEY || |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 977 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 978 | expected_type = PTR_TO_STACK; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 979 | if (type != PTR_TO_PACKET && type != expected_type) |
| 980 | goto err_type; |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 981 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
| 982 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 983 | expected_type = CONST_IMM; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 984 | if (type != expected_type) |
| 985 | goto err_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 986 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 987 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 988 | if (type != expected_type) |
| 989 | goto err_type; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 990 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 991 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 992 | if (type != expected_type) |
| 993 | goto err_type; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 994 | } else if (arg_type == ARG_PTR_TO_STACK || |
| 995 | arg_type == ARG_PTR_TO_RAW_STACK) { |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 996 | expected_type = PTR_TO_STACK; |
| 997 | /* One exception here. In case function allows for NULL to be |
| 998 | * passed in as argument, it's a CONST_IMM type. Final test |
| 999 | * happens during stack boundary checking. |
| 1000 | */ |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1001 | if (type == CONST_IMM && reg->imm == 0) |
| 1002 | /* final test in check_stack_boundary() */; |
| 1003 | else if (type != PTR_TO_PACKET && type != expected_type) |
| 1004 | goto err_type; |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1005 | meta->raw_mode = arg_type == ARG_PTR_TO_RAW_STACK; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1006 | } else { |
| 1007 | verbose("unsupported arg_type %d\n", arg_type); |
| 1008 | return -EFAULT; |
| 1009 | } |
| 1010 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1011 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 1012 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1013 | meta->map_ptr = reg->map_ptr; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1014 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 1015 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 1016 | * check that [key, key + map->key_size) are within |
| 1017 | * stack limits and initialized |
| 1018 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1019 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1020 | /* in function declaration map_ptr must come before |
| 1021 | * map_key, so that it's verified and known before |
| 1022 | * we have to check map_key here. Otherwise it means |
| 1023 | * that kernel subsystem misconfigured verifier |
| 1024 | */ |
| 1025 | verbose("invalid map_ptr to access map->key\n"); |
| 1026 | return -EACCES; |
| 1027 | } |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1028 | if (type == PTR_TO_PACKET) |
| 1029 | err = check_packet_access(env, regno, 0, |
| 1030 | meta->map_ptr->key_size); |
| 1031 | else |
| 1032 | err = check_stack_boundary(env, regno, |
| 1033 | meta->map_ptr->key_size, |
| 1034 | false, NULL); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1035 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 1036 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 1037 | * check [value, value + map->value_size) validity |
| 1038 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1039 | if (!meta->map_ptr) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1040 | /* kernel subsystem misconfigured verifier */ |
| 1041 | verbose("invalid map_ptr to access map->value\n"); |
| 1042 | return -EACCES; |
| 1043 | } |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1044 | if (type == PTR_TO_PACKET) |
| 1045 | err = check_packet_access(env, regno, 0, |
| 1046 | meta->map_ptr->value_size); |
| 1047 | else |
| 1048 | err = check_stack_boundary(env, regno, |
| 1049 | meta->map_ptr->value_size, |
| 1050 | false, NULL); |
Daniel Borkmann | 8e2fe1d9 | 2016-02-19 23:05:22 +0100 | [diff] [blame] | 1051 | } else if (arg_type == ARG_CONST_STACK_SIZE || |
| 1052 | arg_type == ARG_CONST_STACK_SIZE_OR_ZERO) { |
| 1053 | bool zero_size_allowed = (arg_type == ARG_CONST_STACK_SIZE_OR_ZERO); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1054 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1055 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
| 1056 | * from stack pointer 'buf'. Check it |
| 1057 | * note: regno == len, regno - 1 == buf |
| 1058 | */ |
| 1059 | if (regno == 0) { |
| 1060 | /* kernel subsystem misconfigured verifier */ |
| 1061 | verbose("ARG_CONST_STACK_SIZE cannot be first argument\n"); |
| 1062 | return -EACCES; |
| 1063 | } |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1064 | if (regs[regno - 1].type == PTR_TO_PACKET) |
| 1065 | err = check_packet_access(env, regno - 1, 0, reg->imm); |
| 1066 | else |
| 1067 | err = check_stack_boundary(env, regno - 1, reg->imm, |
| 1068 | zero_size_allowed, meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | return err; |
Alexei Starovoitov | 6841de8 | 2016-08-11 18:17:16 -0700 | [diff] [blame] | 1072 | err_type: |
| 1073 | verbose("R%d type=%s expected=%s\n", regno, |
| 1074 | reg_type_str[type], reg_type_str[expected_type]); |
| 1075 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1078 | static int check_map_func_compatibility(struct bpf_map *map, int func_id) |
| 1079 | { |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1080 | if (!map) |
| 1081 | return 0; |
| 1082 | |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1083 | /* We need a two way check, first is from map perspective ... */ |
| 1084 | switch (map->map_type) { |
| 1085 | case BPF_MAP_TYPE_PROG_ARRAY: |
| 1086 | if (func_id != BPF_FUNC_tail_call) |
| 1087 | goto error; |
| 1088 | break; |
| 1089 | case BPF_MAP_TYPE_PERF_EVENT_ARRAY: |
| 1090 | if (func_id != BPF_FUNC_perf_event_read && |
| 1091 | func_id != BPF_FUNC_perf_event_output) |
| 1092 | goto error; |
| 1093 | break; |
| 1094 | case BPF_MAP_TYPE_STACK_TRACE: |
| 1095 | if (func_id != BPF_FUNC_get_stackid) |
| 1096 | goto error; |
| 1097 | break; |
Martin KaFai Lau | 4ed8ec5 | 2016-06-30 10:28:43 -0700 | [diff] [blame] | 1098 | case BPF_MAP_TYPE_CGROUP_ARRAY: |
David S. Miller | 60747ef | 2016-08-18 01:17:32 -0400 | [diff] [blame] | 1099 | if (func_id != BPF_FUNC_skb_under_cgroup && |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 1100 | func_id != BPF_FUNC_current_task_under_cgroup) |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1101 | goto error; |
| 1102 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1103 | default: |
| 1104 | break; |
| 1105 | } |
| 1106 | |
| 1107 | /* ... and second from the function itself. */ |
| 1108 | switch (func_id) { |
| 1109 | case BPF_FUNC_tail_call: |
| 1110 | if (map->map_type != BPF_MAP_TYPE_PROG_ARRAY) |
| 1111 | goto error; |
| 1112 | break; |
| 1113 | case BPF_FUNC_perf_event_read: |
| 1114 | case BPF_FUNC_perf_event_output: |
| 1115 | if (map->map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY) |
| 1116 | goto error; |
| 1117 | break; |
| 1118 | case BPF_FUNC_get_stackid: |
| 1119 | if (map->map_type != BPF_MAP_TYPE_STACK_TRACE) |
| 1120 | goto error; |
| 1121 | break; |
Sargun Dhillon | 60d20f9 | 2016-08-12 08:56:52 -0700 | [diff] [blame] | 1122 | case BPF_FUNC_current_task_under_cgroup: |
Daniel Borkmann | 747ea55 | 2016-08-12 22:17:17 +0200 | [diff] [blame] | 1123 | case BPF_FUNC_skb_under_cgroup: |
Martin KaFai Lau | 4a482f3 | 2016-06-30 10:28:44 -0700 | [diff] [blame] | 1124 | if (map->map_type != BPF_MAP_TYPE_CGROUP_ARRAY) |
| 1125 | goto error; |
| 1126 | break; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1127 | default: |
| 1128 | break; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | return 0; |
Alexei Starovoitov | 6aff67c | 2016-04-27 18:56:21 -0700 | [diff] [blame] | 1132 | error: |
| 1133 | verbose("cannot pass map_type %d into func %d\n", |
| 1134 | map->map_type, func_id); |
| 1135 | return -EINVAL; |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1138 | static int check_raw_mode(const struct bpf_func_proto *fn) |
| 1139 | { |
| 1140 | int count = 0; |
| 1141 | |
| 1142 | if (fn->arg1_type == ARG_PTR_TO_RAW_STACK) |
| 1143 | count++; |
| 1144 | if (fn->arg2_type == ARG_PTR_TO_RAW_STACK) |
| 1145 | count++; |
| 1146 | if (fn->arg3_type == ARG_PTR_TO_RAW_STACK) |
| 1147 | count++; |
| 1148 | if (fn->arg4_type == ARG_PTR_TO_RAW_STACK) |
| 1149 | count++; |
| 1150 | if (fn->arg5_type == ARG_PTR_TO_RAW_STACK) |
| 1151 | count++; |
| 1152 | |
| 1153 | return count > 1 ? -EINVAL : 0; |
| 1154 | } |
| 1155 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1156 | static void clear_all_pkt_pointers(struct bpf_verifier_env *env) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1157 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1158 | struct bpf_verifier_state *state = &env->cur_state; |
| 1159 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1160 | int i; |
| 1161 | |
| 1162 | for (i = 0; i < MAX_BPF_REG; i++) |
| 1163 | if (regs[i].type == PTR_TO_PACKET || |
| 1164 | regs[i].type == PTR_TO_PACKET_END) |
| 1165 | mark_reg_unknown_value(regs, i); |
| 1166 | |
| 1167 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 1168 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 1169 | continue; |
| 1170 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; |
| 1171 | if (reg->type != PTR_TO_PACKET && |
| 1172 | reg->type != PTR_TO_PACKET_END) |
| 1173 | continue; |
| 1174 | reg->type = UNKNOWN_VALUE; |
| 1175 | reg->imm = 0; |
| 1176 | } |
| 1177 | } |
| 1178 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1179 | static int check_call(struct bpf_verifier_env *env, int func_id) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1180 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1181 | struct bpf_verifier_state *state = &env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1182 | const struct bpf_func_proto *fn = NULL; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1183 | struct bpf_reg_state *regs = state->regs; |
| 1184 | struct bpf_reg_state *reg; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1185 | struct bpf_call_arg_meta meta; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1186 | bool changes_data; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1187 | int i, err; |
| 1188 | |
| 1189 | /* find function prototype */ |
| 1190 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
| 1191 | verbose("invalid func %d\n", func_id); |
| 1192 | return -EINVAL; |
| 1193 | } |
| 1194 | |
| 1195 | if (env->prog->aux->ops->get_func_proto) |
| 1196 | fn = env->prog->aux->ops->get_func_proto(func_id); |
| 1197 | |
| 1198 | if (!fn) { |
| 1199 | verbose("unknown func %d\n", func_id); |
| 1200 | return -EINVAL; |
| 1201 | } |
| 1202 | |
| 1203 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 1204 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1205 | verbose("cannot call GPL only function from proprietary program\n"); |
| 1206 | return -EINVAL; |
| 1207 | } |
| 1208 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1209 | changes_data = bpf_helper_changes_skb_data(fn->func); |
| 1210 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1211 | memset(&meta, 0, sizeof(meta)); |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 1212 | meta.pkt_access = fn->pkt_access; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1213 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1214 | /* We only support one arg being in raw mode at the moment, which |
| 1215 | * is sufficient for the helper functions we have right now. |
| 1216 | */ |
| 1217 | err = check_raw_mode(fn); |
| 1218 | if (err) { |
| 1219 | verbose("kernel subsystem misconfigured func %d\n", func_id); |
| 1220 | return err; |
| 1221 | } |
| 1222 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1223 | /* check args */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1224 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1225 | if (err) |
| 1226 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1227 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1228 | if (err) |
| 1229 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1230 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1231 | if (err) |
| 1232 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1233 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1234 | if (err) |
| 1235 | return err; |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1236 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &meta); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1237 | if (err) |
| 1238 | return err; |
| 1239 | |
Daniel Borkmann | 435faee1 | 2016-04-13 00:10:51 +0200 | [diff] [blame] | 1240 | /* Mark slots with STACK_MISC in case of raw mode, stack offset |
| 1241 | * is inferred from register state. |
| 1242 | */ |
| 1243 | for (i = 0; i < meta.access_size; i++) { |
| 1244 | err = check_mem_access(env, meta.regno, i, BPF_B, BPF_WRITE, -1); |
| 1245 | if (err) |
| 1246 | return err; |
| 1247 | } |
| 1248 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1249 | /* reset caller saved regs */ |
| 1250 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 1251 | reg = regs + caller_saved[i]; |
| 1252 | reg->type = NOT_INIT; |
| 1253 | reg->imm = 0; |
| 1254 | } |
| 1255 | |
| 1256 | /* update return register */ |
| 1257 | if (fn->ret_type == RET_INTEGER) { |
| 1258 | regs[BPF_REG_0].type = UNKNOWN_VALUE; |
| 1259 | } else if (fn->ret_type == RET_VOID) { |
| 1260 | regs[BPF_REG_0].type = NOT_INIT; |
| 1261 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { |
| 1262 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1263 | regs[BPF_REG_0].max_value = regs[BPF_REG_0].min_value = 0; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1264 | /* remember map_ptr, so that check_map_access() |
| 1265 | * can check 'value_size' boundary of memory access |
| 1266 | * to map element returned from bpf_map_lookup_elem() |
| 1267 | */ |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1268 | if (meta.map_ptr == NULL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1269 | verbose("kernel subsystem misconfigured verifier\n"); |
| 1270 | return -EINVAL; |
| 1271 | } |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1272 | regs[BPF_REG_0].map_ptr = meta.map_ptr; |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1273 | regs[BPF_REG_0].id = ++env->id_gen; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1274 | } else { |
| 1275 | verbose("unknown return type %d of func %d\n", |
| 1276 | fn->ret_type, func_id); |
| 1277 | return -EINVAL; |
| 1278 | } |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1279 | |
Daniel Borkmann | 33ff982 | 2016-04-13 00:10:50 +0200 | [diff] [blame] | 1280 | err = check_map_func_compatibility(meta.map_ptr, func_id); |
Kaixu Xia | 35578d7 | 2015-08-06 07:02:35 +0000 | [diff] [blame] | 1281 | if (err) |
| 1282 | return err; |
Alexei Starovoitov | 04fd61a | 2015-05-19 16:59:03 -0700 | [diff] [blame] | 1283 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1284 | if (changes_data) |
| 1285 | clear_all_pkt_pointers(env); |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1289 | static int check_packet_ptr_add(struct bpf_verifier_env *env, |
| 1290 | struct bpf_insn *insn) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1291 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1292 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 1293 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; |
| 1294 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
| 1295 | struct bpf_reg_state tmp_reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1296 | s32 imm; |
| 1297 | |
| 1298 | if (BPF_SRC(insn->code) == BPF_K) { |
| 1299 | /* pkt_ptr += imm */ |
| 1300 | imm = insn->imm; |
| 1301 | |
| 1302 | add_imm: |
| 1303 | if (imm <= 0) { |
| 1304 | verbose("addition of negative constant to packet pointer is not allowed\n"); |
| 1305 | return -EACCES; |
| 1306 | } |
| 1307 | if (imm >= MAX_PACKET_OFF || |
| 1308 | imm + dst_reg->off >= MAX_PACKET_OFF) { |
| 1309 | verbose("constant %d is too large to add to packet pointer\n", |
| 1310 | imm); |
| 1311 | return -EACCES; |
| 1312 | } |
| 1313 | /* a constant was added to pkt_ptr. |
| 1314 | * Remember it while keeping the same 'id' |
| 1315 | */ |
| 1316 | dst_reg->off += imm; |
| 1317 | } else { |
Alexei Starovoitov | 1b9b69e | 2016-05-19 18:17:14 -0700 | [diff] [blame] | 1318 | if (src_reg->type == PTR_TO_PACKET) { |
| 1319 | /* R6=pkt(id=0,off=0,r=62) R7=imm22; r7 += r6 */ |
| 1320 | tmp_reg = *dst_reg; /* save r7 state */ |
| 1321 | *dst_reg = *src_reg; /* copy pkt_ptr state r6 into r7 */ |
| 1322 | src_reg = &tmp_reg; /* pretend it's src_reg state */ |
| 1323 | /* if the checks below reject it, the copy won't matter, |
| 1324 | * since we're rejecting the whole program. If all ok, |
| 1325 | * then imm22 state will be added to r7 |
| 1326 | * and r7 will be pkt(id=0,off=22,r=62) while |
| 1327 | * r6 will stay as pkt(id=0,off=0,r=62) |
| 1328 | */ |
| 1329 | } |
| 1330 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1331 | if (src_reg->type == CONST_IMM) { |
| 1332 | /* pkt_ptr += reg where reg is known constant */ |
| 1333 | imm = src_reg->imm; |
| 1334 | goto add_imm; |
| 1335 | } |
| 1336 | /* disallow pkt_ptr += reg |
| 1337 | * if reg is not uknown_value with guaranteed zero upper bits |
| 1338 | * otherwise pkt_ptr may overflow and addition will become |
| 1339 | * subtraction which is not allowed |
| 1340 | */ |
| 1341 | if (src_reg->type != UNKNOWN_VALUE) { |
| 1342 | verbose("cannot add '%s' to ptr_to_packet\n", |
| 1343 | reg_type_str[src_reg->type]); |
| 1344 | return -EACCES; |
| 1345 | } |
| 1346 | if (src_reg->imm < 48) { |
| 1347 | verbose("cannot add integer value with %lld upper zero bits to ptr_to_packet\n", |
| 1348 | src_reg->imm); |
| 1349 | return -EACCES; |
| 1350 | } |
| 1351 | /* dst_reg stays as pkt_ptr type and since some positive |
| 1352 | * integer value was added to the pointer, increment its 'id' |
| 1353 | */ |
Jakub Kicinski | 1f415a7 | 2016-08-02 16:12:14 +0100 | [diff] [blame] | 1354 | dst_reg->id = ++env->id_gen; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1355 | |
| 1356 | /* something was added to pkt_ptr, set range and off to zero */ |
| 1357 | dst_reg->off = 0; |
| 1358 | dst_reg->range = 0; |
| 1359 | } |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1363 | static int evaluate_reg_alu(struct bpf_verifier_env *env, struct bpf_insn *insn) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1364 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1365 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 1366 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1367 | u8 opcode = BPF_OP(insn->code); |
| 1368 | s64 imm_log2; |
| 1369 | |
| 1370 | /* for type == UNKNOWN_VALUE: |
| 1371 | * imm > 0 -> number of zero upper bits |
| 1372 | * imm == 0 -> don't track which is the same as all bits can be non-zero |
| 1373 | */ |
| 1374 | |
| 1375 | if (BPF_SRC(insn->code) == BPF_X) { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1376 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1377 | |
| 1378 | if (src_reg->type == UNKNOWN_VALUE && src_reg->imm > 0 && |
| 1379 | dst_reg->imm && opcode == BPF_ADD) { |
| 1380 | /* dreg += sreg |
| 1381 | * where both have zero upper bits. Adding them |
| 1382 | * can only result making one more bit non-zero |
| 1383 | * in the larger value. |
| 1384 | * Ex. 0xffff (imm=48) + 1 (imm=63) = 0x10000 (imm=47) |
| 1385 | * 0xffff (imm=48) + 0xffff = 0x1fffe (imm=47) |
| 1386 | */ |
| 1387 | dst_reg->imm = min(dst_reg->imm, src_reg->imm); |
| 1388 | dst_reg->imm--; |
| 1389 | return 0; |
| 1390 | } |
| 1391 | if (src_reg->type == CONST_IMM && src_reg->imm > 0 && |
| 1392 | dst_reg->imm && opcode == BPF_ADD) { |
| 1393 | /* dreg += sreg |
| 1394 | * where dreg has zero upper bits and sreg is const. |
| 1395 | * Adding them can only result making one more bit |
| 1396 | * non-zero in the larger value. |
| 1397 | */ |
| 1398 | imm_log2 = __ilog2_u64((long long)src_reg->imm); |
| 1399 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); |
| 1400 | dst_reg->imm--; |
| 1401 | return 0; |
| 1402 | } |
| 1403 | /* all other cases non supported yet, just mark dst_reg */ |
| 1404 | dst_reg->imm = 0; |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | /* sign extend 32-bit imm into 64-bit to make sure that |
| 1409 | * negative values occupy bit 63. Note ilog2() would have |
| 1410 | * been incorrect, since sizeof(insn->imm) == 4 |
| 1411 | */ |
| 1412 | imm_log2 = __ilog2_u64((long long)insn->imm); |
| 1413 | |
| 1414 | if (dst_reg->imm && opcode == BPF_LSH) { |
| 1415 | /* reg <<= imm |
| 1416 | * if reg was a result of 2 byte load, then its imm == 48 |
| 1417 | * which means that upper 48 bits are zero and shifting this reg |
| 1418 | * left by 4 would mean that upper 44 bits are still zero |
| 1419 | */ |
| 1420 | dst_reg->imm -= insn->imm; |
| 1421 | } else if (dst_reg->imm && opcode == BPF_MUL) { |
| 1422 | /* reg *= imm |
| 1423 | * if multiplying by 14 subtract 4 |
| 1424 | * This is conservative calculation of upper zero bits. |
| 1425 | * It's not trying to special case insn->imm == 1 or 0 cases |
| 1426 | */ |
| 1427 | dst_reg->imm -= imm_log2 + 1; |
| 1428 | } else if (opcode == BPF_AND) { |
| 1429 | /* reg &= imm */ |
| 1430 | dst_reg->imm = 63 - imm_log2; |
| 1431 | } else if (dst_reg->imm && opcode == BPF_ADD) { |
| 1432 | /* reg += imm */ |
| 1433 | dst_reg->imm = min(dst_reg->imm, 63 - imm_log2); |
| 1434 | dst_reg->imm--; |
| 1435 | } else if (opcode == BPF_RSH) { |
| 1436 | /* reg >>= imm |
| 1437 | * which means that after right shift, upper bits will be zero |
| 1438 | * note that verifier already checked that |
| 1439 | * 0 <= imm < 64 for shift insn |
| 1440 | */ |
| 1441 | dst_reg->imm += insn->imm; |
| 1442 | if (unlikely(dst_reg->imm > 64)) |
| 1443 | /* some dumb code did: |
| 1444 | * r2 = *(u32 *)mem; |
| 1445 | * r2 >>= 32; |
| 1446 | * and all bits are zero now */ |
| 1447 | dst_reg->imm = 64; |
| 1448 | } else { |
| 1449 | /* all other alu ops, means that we don't know what will |
| 1450 | * happen to the value, mark it with unknown number of zero bits |
| 1451 | */ |
| 1452 | dst_reg->imm = 0; |
| 1453 | } |
| 1454 | |
| 1455 | if (dst_reg->imm < 0) { |
| 1456 | /* all 64 bits of the register can contain non-zero bits |
| 1457 | * and such value cannot be added to ptr_to_packet, since it |
| 1458 | * may overflow, mark it as unknown to avoid further eval |
| 1459 | */ |
| 1460 | dst_reg->imm = 0; |
| 1461 | } |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1465 | static int evaluate_reg_imm_alu(struct bpf_verifier_env *env, |
| 1466 | struct bpf_insn *insn) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1467 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1468 | struct bpf_reg_state *regs = env->cur_state.regs; |
| 1469 | struct bpf_reg_state *dst_reg = ®s[insn->dst_reg]; |
| 1470 | struct bpf_reg_state *src_reg = ®s[insn->src_reg]; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1471 | u8 opcode = BPF_OP(insn->code); |
| 1472 | |
| 1473 | /* dst_reg->type == CONST_IMM here, simulate execution of 'add' insn. |
| 1474 | * Don't care about overflow or negative values, just add them |
| 1475 | */ |
| 1476 | if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_K) |
| 1477 | dst_reg->imm += insn->imm; |
| 1478 | else if (opcode == BPF_ADD && BPF_SRC(insn->code) == BPF_X && |
| 1479 | src_reg->type == CONST_IMM) |
| 1480 | dst_reg->imm += src_reg->imm; |
| 1481 | else |
| 1482 | mark_reg_unknown_value(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1483 | return 0; |
| 1484 | } |
| 1485 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1486 | static void check_reg_overflow(struct bpf_reg_state *reg) |
| 1487 | { |
| 1488 | if (reg->max_value > BPF_REGISTER_MAX_RANGE) |
| 1489 | reg->max_value = BPF_REGISTER_MAX_RANGE; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1490 | if (reg->min_value < BPF_REGISTER_MIN_RANGE || |
| 1491 | reg->min_value > BPF_REGISTER_MAX_RANGE) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1492 | reg->min_value = BPF_REGISTER_MIN_RANGE; |
| 1493 | } |
| 1494 | |
| 1495 | static void adjust_reg_min_max_vals(struct bpf_verifier_env *env, |
| 1496 | struct bpf_insn *insn) |
| 1497 | { |
| 1498 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1499 | s64 min_val = BPF_REGISTER_MIN_RANGE; |
| 1500 | u64 max_val = BPF_REGISTER_MAX_RANGE; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1501 | bool min_set = false, max_set = false; |
| 1502 | u8 opcode = BPF_OP(insn->code); |
| 1503 | |
| 1504 | dst_reg = ®s[insn->dst_reg]; |
| 1505 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1506 | check_reg_overflow(®s[insn->src_reg]); |
| 1507 | min_val = regs[insn->src_reg].min_value; |
| 1508 | max_val = regs[insn->src_reg].max_value; |
| 1509 | |
| 1510 | /* If the source register is a random pointer then the |
| 1511 | * min_value/max_value values represent the range of the known |
| 1512 | * accesses into that value, not the actual min/max value of the |
| 1513 | * register itself. In this case we have to reset the reg range |
| 1514 | * values so we know it is not safe to look at. |
| 1515 | */ |
| 1516 | if (regs[insn->src_reg].type != CONST_IMM && |
| 1517 | regs[insn->src_reg].type != UNKNOWN_VALUE) { |
| 1518 | min_val = BPF_REGISTER_MIN_RANGE; |
| 1519 | max_val = BPF_REGISTER_MAX_RANGE; |
| 1520 | } |
| 1521 | } else if (insn->imm < BPF_REGISTER_MAX_RANGE && |
| 1522 | (s64)insn->imm > BPF_REGISTER_MIN_RANGE) { |
| 1523 | min_val = max_val = insn->imm; |
| 1524 | min_set = max_set = true; |
| 1525 | } |
| 1526 | |
| 1527 | /* We don't know anything about what was done to this register, mark it |
| 1528 | * as unknown. |
| 1529 | */ |
| 1530 | if (min_val == BPF_REGISTER_MIN_RANGE && |
| 1531 | max_val == BPF_REGISTER_MAX_RANGE) { |
| 1532 | reset_reg_range_values(regs, insn->dst_reg); |
| 1533 | return; |
| 1534 | } |
| 1535 | |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1536 | /* If one of our values was at the end of our ranges then we can't just |
| 1537 | * do our normal operations to the register, we need to set the values |
| 1538 | * to the min/max since they are undefined. |
| 1539 | */ |
| 1540 | if (min_val == BPF_REGISTER_MIN_RANGE) |
| 1541 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
| 1542 | if (max_val == BPF_REGISTER_MAX_RANGE) |
| 1543 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; |
| 1544 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1545 | switch (opcode) { |
| 1546 | case BPF_ADD: |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1547 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
| 1548 | dst_reg->min_value += min_val; |
| 1549 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
| 1550 | dst_reg->max_value += max_val; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1551 | break; |
| 1552 | case BPF_SUB: |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1553 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
| 1554 | dst_reg->min_value -= min_val; |
| 1555 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
| 1556 | dst_reg->max_value -= max_val; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1557 | break; |
| 1558 | case BPF_MUL: |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1559 | if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
| 1560 | dst_reg->min_value *= min_val; |
| 1561 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
| 1562 | dst_reg->max_value *= max_val; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1563 | break; |
| 1564 | case BPF_AND: |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1565 | /* Disallow AND'ing of negative numbers, ain't nobody got time |
| 1566 | * for that. Otherwise the minimum is 0 and the max is the max |
| 1567 | * value we could AND against. |
| 1568 | */ |
| 1569 | if (min_val < 0) |
| 1570 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
| 1571 | else |
| 1572 | dst_reg->min_value = 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1573 | dst_reg->max_value = max_val; |
| 1574 | break; |
| 1575 | case BPF_LSH: |
| 1576 | /* Gotta have special overflow logic here, if we're shifting |
| 1577 | * more than MAX_RANGE then just assume we have an invalid |
| 1578 | * range. |
| 1579 | */ |
| 1580 | if (min_val > ilog2(BPF_REGISTER_MAX_RANGE)) |
| 1581 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1582 | else if (dst_reg->min_value != BPF_REGISTER_MIN_RANGE) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1583 | dst_reg->min_value <<= min_val; |
| 1584 | |
| 1585 | if (max_val > ilog2(BPF_REGISTER_MAX_RANGE)) |
| 1586 | dst_reg->max_value = BPF_REGISTER_MAX_RANGE; |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1587 | else if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1588 | dst_reg->max_value <<= max_val; |
| 1589 | break; |
| 1590 | case BPF_RSH: |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1591 | /* RSH by a negative number is undefined, and the BPF_RSH is an |
| 1592 | * unsigned shift, so make the appropriate casts. |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1593 | */ |
Josef Bacik | f23cc64 | 2016-11-14 15:45:36 -0500 | [diff] [blame] | 1594 | if (min_val < 0 || dst_reg->min_value < 0) |
| 1595 | dst_reg->min_value = BPF_REGISTER_MIN_RANGE; |
| 1596 | else |
| 1597 | dst_reg->min_value = |
| 1598 | (u64)(dst_reg->min_value) >> min_val; |
| 1599 | if (dst_reg->max_value != BPF_REGISTER_MAX_RANGE) |
| 1600 | dst_reg->max_value >>= max_val; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1601 | break; |
| 1602 | default: |
| 1603 | reset_reg_range_values(regs, insn->dst_reg); |
| 1604 | break; |
| 1605 | } |
| 1606 | |
| 1607 | check_reg_overflow(dst_reg); |
| 1608 | } |
| 1609 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1610 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1611 | 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] | 1612 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1613 | struct bpf_reg_state *regs = env->cur_state.regs, *dst_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1614 | u8 opcode = BPF_OP(insn->code); |
| 1615 | int err; |
| 1616 | |
| 1617 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 1618 | if (opcode == BPF_NEG) { |
| 1619 | if (BPF_SRC(insn->code) != 0 || |
| 1620 | insn->src_reg != BPF_REG_0 || |
| 1621 | insn->off != 0 || insn->imm != 0) { |
| 1622 | verbose("BPF_NEG uses reserved fields\n"); |
| 1623 | return -EINVAL; |
| 1624 | } |
| 1625 | } else { |
| 1626 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
| 1627 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) { |
| 1628 | verbose("BPF_END uses reserved fields\n"); |
| 1629 | return -EINVAL; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | /* check src operand */ |
| 1634 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1635 | if (err) |
| 1636 | return err; |
| 1637 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1638 | if (is_pointer_value(env, insn->dst_reg)) { |
| 1639 | verbose("R%d pointer arithmetic prohibited\n", |
| 1640 | insn->dst_reg); |
| 1641 | return -EACCES; |
| 1642 | } |
| 1643 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1644 | /* check dest operand */ |
| 1645 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 1646 | if (err) |
| 1647 | return err; |
| 1648 | |
| 1649 | } else if (opcode == BPF_MOV) { |
| 1650 | |
| 1651 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1652 | if (insn->imm != 0 || insn->off != 0) { |
| 1653 | verbose("BPF_MOV uses reserved fields\n"); |
| 1654 | return -EINVAL; |
| 1655 | } |
| 1656 | |
| 1657 | /* check src operand */ |
| 1658 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1659 | if (err) |
| 1660 | return err; |
| 1661 | } else { |
| 1662 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 1663 | verbose("BPF_MOV uses reserved fields\n"); |
| 1664 | return -EINVAL; |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | /* check dest operand */ |
| 1669 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 1670 | if (err) |
| 1671 | return err; |
| 1672 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1673 | /* we are setting our register to something new, we need to |
| 1674 | * reset its range values. |
| 1675 | */ |
| 1676 | reset_reg_range_values(regs, insn->dst_reg); |
| 1677 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1678 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1679 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 1680 | /* case: R1 = R2 |
| 1681 | * copy register state to dest reg |
| 1682 | */ |
| 1683 | regs[insn->dst_reg] = regs[insn->src_reg]; |
| 1684 | } else { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1685 | if (is_pointer_value(env, insn->src_reg)) { |
| 1686 | verbose("R%d partial copy of pointer\n", |
| 1687 | insn->src_reg); |
| 1688 | return -EACCES; |
| 1689 | } |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1690 | mark_reg_unknown_value(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1691 | } |
| 1692 | } else { |
| 1693 | /* case: R = imm |
| 1694 | * remember the value we stored into this reg |
| 1695 | */ |
| 1696 | regs[insn->dst_reg].type = CONST_IMM; |
| 1697 | regs[insn->dst_reg].imm = insn->imm; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1698 | regs[insn->dst_reg].max_value = insn->imm; |
| 1699 | regs[insn->dst_reg].min_value = insn->imm; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | } else if (opcode > BPF_END) { |
| 1703 | verbose("invalid BPF_ALU opcode %x\n", opcode); |
| 1704 | return -EINVAL; |
| 1705 | |
| 1706 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 1707 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1708 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1709 | if (insn->imm != 0 || insn->off != 0) { |
| 1710 | verbose("BPF_ALU uses reserved fields\n"); |
| 1711 | return -EINVAL; |
| 1712 | } |
| 1713 | /* check src1 operand */ |
| 1714 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1715 | if (err) |
| 1716 | return err; |
| 1717 | } else { |
| 1718 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 1719 | verbose("BPF_ALU uses reserved fields\n"); |
| 1720 | return -EINVAL; |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | /* check src2 operand */ |
| 1725 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1726 | if (err) |
| 1727 | return err; |
| 1728 | |
| 1729 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 1730 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
| 1731 | verbose("div by zero\n"); |
| 1732 | return -EINVAL; |
| 1733 | } |
| 1734 | |
Rabin Vincent | 229394e | 2016-01-12 20:17:08 +0100 | [diff] [blame] | 1735 | if ((opcode == BPF_LSH || opcode == BPF_RSH || |
| 1736 | opcode == BPF_ARSH) && BPF_SRC(insn->code) == BPF_K) { |
| 1737 | int size = BPF_CLASS(insn->code) == BPF_ALU64 ? 64 : 32; |
| 1738 | |
| 1739 | if (insn->imm < 0 || insn->imm >= size) { |
| 1740 | verbose("invalid shift %d\n", insn->imm); |
| 1741 | return -EINVAL; |
| 1742 | } |
| 1743 | } |
| 1744 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1745 | /* check dest operand */ |
| 1746 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); |
| 1747 | if (err) |
| 1748 | return err; |
| 1749 | |
| 1750 | dst_reg = ®s[insn->dst_reg]; |
| 1751 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1752 | /* first we want to adjust our ranges. */ |
| 1753 | adjust_reg_min_max_vals(env, insn); |
| 1754 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1755 | /* pattern match 'bpf_add Rx, imm' instruction */ |
| 1756 | if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 1757 | dst_reg->type == FRAME_PTR && BPF_SRC(insn->code) == BPF_K) { |
| 1758 | dst_reg->type = PTR_TO_STACK; |
| 1759 | dst_reg->imm = insn->imm; |
| 1760 | return 0; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1761 | } else if (opcode == BPF_ADD && |
| 1762 | BPF_CLASS(insn->code) == BPF_ALU64 && |
Yonghong Song | 7bca0a9 | 2017-04-29 22:52:42 -0700 | [diff] [blame] | 1763 | dst_reg->type == PTR_TO_STACK && |
| 1764 | ((BPF_SRC(insn->code) == BPF_X && |
| 1765 | regs[insn->src_reg].type == CONST_IMM) || |
| 1766 | BPF_SRC(insn->code) == BPF_K)) { |
| 1767 | if (BPF_SRC(insn->code) == BPF_X) |
| 1768 | dst_reg->imm += regs[insn->src_reg].imm; |
| 1769 | else |
| 1770 | dst_reg->imm += insn->imm; |
| 1771 | return 0; |
| 1772 | } else if (opcode == BPF_ADD && |
| 1773 | BPF_CLASS(insn->code) == BPF_ALU64 && |
Alexei Starovoitov | 1b9b69e | 2016-05-19 18:17:14 -0700 | [diff] [blame] | 1774 | (dst_reg->type == PTR_TO_PACKET || |
| 1775 | (BPF_SRC(insn->code) == BPF_X && |
| 1776 | regs[insn->src_reg].type == PTR_TO_PACKET))) { |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1777 | /* ptr_to_packet += K|X */ |
| 1778 | return check_packet_ptr_add(env, insn); |
| 1779 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && |
| 1780 | dst_reg->type == UNKNOWN_VALUE && |
| 1781 | env->allow_ptr_leaks) { |
| 1782 | /* unknown += K|X */ |
| 1783 | return evaluate_reg_alu(env, insn); |
| 1784 | } else if (BPF_CLASS(insn->code) == BPF_ALU64 && |
| 1785 | dst_reg->type == CONST_IMM && |
| 1786 | env->allow_ptr_leaks) { |
| 1787 | /* reg_imm += K|X */ |
| 1788 | return evaluate_reg_imm_alu(env, insn); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 1789 | } else if (is_pointer_value(env, insn->dst_reg)) { |
| 1790 | verbose("R%d pointer arithmetic prohibited\n", |
| 1791 | insn->dst_reg); |
| 1792 | return -EACCES; |
| 1793 | } else if (BPF_SRC(insn->code) == BPF_X && |
| 1794 | is_pointer_value(env, insn->src_reg)) { |
| 1795 | verbose("R%d pointer arithmetic prohibited\n", |
| 1796 | insn->src_reg); |
| 1797 | return -EACCES; |
| 1798 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1799 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1800 | /* If we did pointer math on a map value then just set it to our |
| 1801 | * PTR_TO_MAP_VALUE_ADJ type so we can deal with any stores or |
| 1802 | * loads to this register appropriately, otherwise just mark the |
| 1803 | * register as unknown. |
| 1804 | */ |
| 1805 | if (env->allow_ptr_leaks && |
| 1806 | (dst_reg->type == PTR_TO_MAP_VALUE || |
| 1807 | dst_reg->type == PTR_TO_MAP_VALUE_ADJ)) |
| 1808 | dst_reg->type = PTR_TO_MAP_VALUE_ADJ; |
| 1809 | else |
| 1810 | mark_reg_unknown_value(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | return 0; |
| 1814 | } |
| 1815 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1816 | static void find_good_pkt_pointers(struct bpf_verifier_state *state, |
| 1817 | struct bpf_reg_state *dst_reg) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1818 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 1819 | struct bpf_reg_state *regs = state->regs, *reg; |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1820 | int i; |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 1821 | |
| 1822 | /* LLVM can generate two kind of checks: |
| 1823 | * |
| 1824 | * Type 1: |
| 1825 | * |
| 1826 | * r2 = r3; |
| 1827 | * r2 += 8; |
| 1828 | * if (r2 > pkt_end) goto <handle exception> |
| 1829 | * <access okay> |
| 1830 | * |
| 1831 | * Where: |
| 1832 | * r2 == dst_reg, pkt_end == src_reg |
| 1833 | * r2=pkt(id=n,off=8,r=0) |
| 1834 | * r3=pkt(id=n,off=0,r=0) |
| 1835 | * |
| 1836 | * Type 2: |
| 1837 | * |
| 1838 | * r2 = r3; |
| 1839 | * r2 += 8; |
| 1840 | * if (pkt_end >= r2) goto <access okay> |
| 1841 | * <handle exception> |
| 1842 | * |
| 1843 | * Where: |
| 1844 | * pkt_end == dst_reg, r2 == src_reg |
| 1845 | * r2=pkt(id=n,off=8,r=0) |
| 1846 | * r3=pkt(id=n,off=0,r=0) |
| 1847 | * |
| 1848 | * Find register r3 and mark its range as r3=pkt(id=n,off=0,r=8) |
| 1849 | * so that range of bytes [r3, r3 + 8) is safe to access. |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1850 | */ |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 1851 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1852 | for (i = 0; i < MAX_BPF_REG; i++) |
| 1853 | if (regs[i].type == PTR_TO_PACKET && regs[i].id == dst_reg->id) |
Alexei Starovoitov | 0ea3c23 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 1854 | /* keep the maximum range already checked */ |
| 1855 | regs[i].range = max(regs[i].range, dst_reg->off); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1856 | |
| 1857 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 1858 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 1859 | continue; |
| 1860 | reg = &state->spilled_regs[i / BPF_REG_SIZE]; |
| 1861 | if (reg->type == PTR_TO_PACKET && reg->id == dst_reg->id) |
Alexei Starovoitov | 0ea3c23 | 2017-03-24 15:57:33 -0700 | [diff] [blame] | 1862 | reg->range = max(reg->range, dst_reg->off); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 1863 | } |
| 1864 | } |
| 1865 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 1866 | /* Adjusts the register min/max values in the case that the dst_reg is the |
| 1867 | * variable register that we are working on, and src_reg is a constant or we're |
| 1868 | * simply doing a BPF_K check. |
| 1869 | */ |
| 1870 | static void reg_set_min_max(struct bpf_reg_state *true_reg, |
| 1871 | struct bpf_reg_state *false_reg, u64 val, |
| 1872 | u8 opcode) |
| 1873 | { |
| 1874 | switch (opcode) { |
| 1875 | case BPF_JEQ: |
| 1876 | /* If this is false then we know nothing Jon Snow, but if it is |
| 1877 | * true then we know for sure. |
| 1878 | */ |
| 1879 | true_reg->max_value = true_reg->min_value = val; |
| 1880 | break; |
| 1881 | case BPF_JNE: |
| 1882 | /* If this is true we know nothing Jon Snow, but if it is false |
| 1883 | * we know the value for sure; |
| 1884 | */ |
| 1885 | false_reg->max_value = false_reg->min_value = val; |
| 1886 | break; |
| 1887 | case BPF_JGT: |
| 1888 | /* Unsigned comparison, the minimum value is 0. */ |
| 1889 | false_reg->min_value = 0; |
| 1890 | case BPF_JSGT: |
| 1891 | /* If this is false then we know the maximum val is val, |
| 1892 | * otherwise we know the min val is val+1. |
| 1893 | */ |
| 1894 | false_reg->max_value = val; |
| 1895 | true_reg->min_value = val + 1; |
| 1896 | break; |
| 1897 | case BPF_JGE: |
| 1898 | /* Unsigned comparison, the minimum value is 0. */ |
| 1899 | false_reg->min_value = 0; |
| 1900 | case BPF_JSGE: |
| 1901 | /* If this is false then we know the maximum value is val - 1, |
| 1902 | * otherwise we know the mimimum value is val. |
| 1903 | */ |
| 1904 | false_reg->max_value = val - 1; |
| 1905 | true_reg->min_value = val; |
| 1906 | break; |
| 1907 | default: |
| 1908 | break; |
| 1909 | } |
| 1910 | |
| 1911 | check_reg_overflow(false_reg); |
| 1912 | check_reg_overflow(true_reg); |
| 1913 | } |
| 1914 | |
| 1915 | /* Same as above, but for the case that dst_reg is a CONST_IMM reg and src_reg |
| 1916 | * is the variable reg. |
| 1917 | */ |
| 1918 | static void reg_set_min_max_inv(struct bpf_reg_state *true_reg, |
| 1919 | struct bpf_reg_state *false_reg, u64 val, |
| 1920 | u8 opcode) |
| 1921 | { |
| 1922 | switch (opcode) { |
| 1923 | case BPF_JEQ: |
| 1924 | /* If this is false then we know nothing Jon Snow, but if it is |
| 1925 | * true then we know for sure. |
| 1926 | */ |
| 1927 | true_reg->max_value = true_reg->min_value = val; |
| 1928 | break; |
| 1929 | case BPF_JNE: |
| 1930 | /* If this is true we know nothing Jon Snow, but if it is false |
| 1931 | * we know the value for sure; |
| 1932 | */ |
| 1933 | false_reg->max_value = false_reg->min_value = val; |
| 1934 | break; |
| 1935 | case BPF_JGT: |
| 1936 | /* Unsigned comparison, the minimum value is 0. */ |
| 1937 | true_reg->min_value = 0; |
| 1938 | case BPF_JSGT: |
| 1939 | /* |
| 1940 | * If this is false, then the val is <= the register, if it is |
| 1941 | * true the register <= to the val. |
| 1942 | */ |
| 1943 | false_reg->min_value = val; |
| 1944 | true_reg->max_value = val - 1; |
| 1945 | break; |
| 1946 | case BPF_JGE: |
| 1947 | /* Unsigned comparison, the minimum value is 0. */ |
| 1948 | true_reg->min_value = 0; |
| 1949 | case BPF_JSGE: |
| 1950 | /* If this is false then constant < register, if it is true then |
| 1951 | * the register < constant. |
| 1952 | */ |
| 1953 | false_reg->min_value = val + 1; |
| 1954 | true_reg->max_value = val; |
| 1955 | break; |
| 1956 | default: |
| 1957 | break; |
| 1958 | } |
| 1959 | |
| 1960 | check_reg_overflow(false_reg); |
| 1961 | check_reg_overflow(true_reg); |
| 1962 | } |
| 1963 | |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1964 | static void mark_map_reg(struct bpf_reg_state *regs, u32 regno, u32 id, |
| 1965 | enum bpf_reg_type type) |
| 1966 | { |
| 1967 | struct bpf_reg_state *reg = ®s[regno]; |
| 1968 | |
| 1969 | if (reg->type == PTR_TO_MAP_VALUE_OR_NULL && reg->id == id) { |
| 1970 | reg->type = type; |
Daniel Borkmann | 1889d6d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 1971 | /* We don't need id from this point onwards anymore, thus we |
| 1972 | * should better reset it, so that state pruning has chances |
| 1973 | * to take effect. |
| 1974 | */ |
| 1975 | reg->id = 0; |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1976 | if (type == UNKNOWN_VALUE) |
Daniel Borkmann | 0e0f1d6 | 2016-12-18 01:52:59 +0100 | [diff] [blame] | 1977 | __mark_reg_unknown_value(regs, regno); |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | /* The logic is similar to find_good_pkt_pointers(), both could eventually |
| 1982 | * be folded together at some point. |
| 1983 | */ |
| 1984 | static void mark_map_regs(struct bpf_verifier_state *state, u32 regno, |
| 1985 | enum bpf_reg_type type) |
| 1986 | { |
| 1987 | struct bpf_reg_state *regs = state->regs; |
Daniel Borkmann | 1889d6d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 1988 | u32 id = regs[regno].id; |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1989 | int i; |
| 1990 | |
| 1991 | for (i = 0; i < MAX_BPF_REG; i++) |
Daniel Borkmann | 1889d6d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 1992 | mark_map_reg(regs, i, id, type); |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1993 | |
| 1994 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 1995 | if (state->stack_slot_type[i] != STACK_SPILL) |
| 1996 | continue; |
Daniel Borkmann | 1889d6d | 2016-12-15 01:30:06 +0100 | [diff] [blame] | 1997 | mark_map_reg(state->spilled_regs, i / BPF_REG_SIZE, id, type); |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 1998 | } |
| 1999 | } |
| 2000 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2001 | static int check_cond_jmp_op(struct bpf_verifier_env *env, |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2002 | struct bpf_insn *insn, int *insn_idx) |
| 2003 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2004 | struct bpf_verifier_state *other_branch, *this_branch = &env->cur_state; |
| 2005 | struct bpf_reg_state *regs = this_branch->regs, *dst_reg; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2006 | u8 opcode = BPF_OP(insn->code); |
| 2007 | int err; |
| 2008 | |
| 2009 | if (opcode > BPF_EXIT) { |
| 2010 | verbose("invalid BPF_JMP opcode %x\n", opcode); |
| 2011 | return -EINVAL; |
| 2012 | } |
| 2013 | |
| 2014 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2015 | if (insn->imm != 0) { |
| 2016 | verbose("BPF_JMP uses reserved fields\n"); |
| 2017 | return -EINVAL; |
| 2018 | } |
| 2019 | |
| 2020 | /* check src1 operand */ |
| 2021 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2022 | if (err) |
| 2023 | return err; |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2024 | |
| 2025 | if (is_pointer_value(env, insn->src_reg)) { |
| 2026 | verbose("R%d pointer comparison prohibited\n", |
| 2027 | insn->src_reg); |
| 2028 | return -EACCES; |
| 2029 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2030 | } else { |
| 2031 | if (insn->src_reg != BPF_REG_0) { |
| 2032 | verbose("BPF_JMP uses reserved fields\n"); |
| 2033 | return -EINVAL; |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | /* check src2 operand */ |
| 2038 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 2039 | if (err) |
| 2040 | return err; |
| 2041 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2042 | dst_reg = ®s[insn->dst_reg]; |
| 2043 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2044 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 2045 | if (BPF_SRC(insn->code) == BPF_K && |
| 2046 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2047 | dst_reg->type == CONST_IMM && dst_reg->imm == insn->imm) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2048 | if (opcode == BPF_JEQ) { |
| 2049 | /* if (imm == imm) goto pc+off; |
| 2050 | * only follow the goto, ignore fall-through |
| 2051 | */ |
| 2052 | *insn_idx += insn->off; |
| 2053 | return 0; |
| 2054 | } else { |
| 2055 | /* if (imm != imm) goto pc+off; |
| 2056 | * only follow fall-through branch, since |
| 2057 | * that's where the program will go |
| 2058 | */ |
| 2059 | return 0; |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 2064 | if (!other_branch) |
| 2065 | return -EFAULT; |
| 2066 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2067 | /* detect if we are comparing against a constant value so we can adjust |
| 2068 | * our min/max values for our dst register. |
| 2069 | */ |
| 2070 | if (BPF_SRC(insn->code) == BPF_X) { |
| 2071 | if (regs[insn->src_reg].type == CONST_IMM) |
| 2072 | reg_set_min_max(&other_branch->regs[insn->dst_reg], |
| 2073 | dst_reg, regs[insn->src_reg].imm, |
| 2074 | opcode); |
| 2075 | else if (dst_reg->type == CONST_IMM) |
| 2076 | reg_set_min_max_inv(&other_branch->regs[insn->src_reg], |
| 2077 | ®s[insn->src_reg], dst_reg->imm, |
| 2078 | opcode); |
| 2079 | } else { |
| 2080 | reg_set_min_max(&other_branch->regs[insn->dst_reg], |
| 2081 | dst_reg, insn->imm, opcode); |
| 2082 | } |
| 2083 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2084 | /* 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] | 2085 | if (BPF_SRC(insn->code) == BPF_K && |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2086 | insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 2087 | dst_reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
Thomas Graf | 1411707 | 2016-10-18 19:51:19 +0200 | [diff] [blame] | 2088 | /* Mark all identical map registers in each branch as either |
| 2089 | * safe or unknown depending R == 0 or R != 0 conditional. |
| 2090 | */ |
| 2091 | mark_map_regs(this_branch, insn->dst_reg, |
| 2092 | opcode == BPF_JEQ ? PTR_TO_MAP_VALUE : UNKNOWN_VALUE); |
| 2093 | mark_map_regs(other_branch, insn->dst_reg, |
| 2094 | opcode == BPF_JEQ ? UNKNOWN_VALUE : PTR_TO_MAP_VALUE); |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2095 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGT && |
| 2096 | dst_reg->type == PTR_TO_PACKET && |
| 2097 | regs[insn->src_reg].type == PTR_TO_PACKET_END) { |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2098 | find_good_pkt_pointers(this_branch, dst_reg); |
| 2099 | } else if (BPF_SRC(insn->code) == BPF_X && opcode == BPF_JGE && |
| 2100 | dst_reg->type == PTR_TO_PACKET_END && |
| 2101 | regs[insn->src_reg].type == PTR_TO_PACKET) { |
| 2102 | find_good_pkt_pointers(other_branch, ®s[insn->src_reg]); |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2103 | } else if (is_pointer_value(env, insn->dst_reg)) { |
| 2104 | verbose("R%d pointer comparison prohibited\n", insn->dst_reg); |
| 2105 | return -EACCES; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2106 | } |
| 2107 | if (log_level) |
Daniel Borkmann | 2d2be8c | 2016-09-08 01:03:42 +0200 | [diff] [blame] | 2108 | print_verifier_state(this_branch); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2109 | return 0; |
| 2110 | } |
| 2111 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2112 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 2113 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 2114 | { |
| 2115 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 2116 | |
| 2117 | return (struct bpf_map *) (unsigned long) imm64; |
| 2118 | } |
| 2119 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2120 | /* verify BPF_LD_IMM64 instruction */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2121 | 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] | 2122 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2123 | struct bpf_reg_state *regs = env->cur_state.regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2124 | int err; |
| 2125 | |
| 2126 | if (BPF_SIZE(insn->code) != BPF_DW) { |
| 2127 | verbose("invalid BPF_LD_IMM insn\n"); |
| 2128 | return -EINVAL; |
| 2129 | } |
| 2130 | if (insn->off != 0) { |
| 2131 | verbose("BPF_LD_IMM64 uses reserved fields\n"); |
| 2132 | return -EINVAL; |
| 2133 | } |
| 2134 | |
| 2135 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 2136 | if (err) |
| 2137 | return err; |
| 2138 | |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 2139 | if (insn->src_reg == 0) { |
| 2140 | /* generic move 64-bit immediate into a register, |
| 2141 | * only analyzer needs to collect the ld_imm value. |
| 2142 | */ |
| 2143 | u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm; |
| 2144 | |
| 2145 | if (!env->analyzer_ops) |
| 2146 | return 0; |
| 2147 | |
| 2148 | regs[insn->dst_reg].type = CONST_IMM; |
| 2149 | regs[insn->dst_reg].imm = imm; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2150 | return 0; |
Jakub Kicinski | 6b17387 | 2016-09-21 11:43:59 +0100 | [diff] [blame] | 2151 | } |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2152 | |
| 2153 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 2154 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 2155 | |
| 2156 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 2157 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 2158 | return 0; |
| 2159 | } |
| 2160 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2161 | static bool may_access_skb(enum bpf_prog_type type) |
| 2162 | { |
| 2163 | switch (type) { |
| 2164 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 2165 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8 | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 2166 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 2167 | return true; |
| 2168 | default: |
| 2169 | return false; |
| 2170 | } |
| 2171 | } |
| 2172 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2173 | /* verify safety of LD_ABS|LD_IND instructions: |
| 2174 | * - they can only appear in the programs where ctx == skb |
| 2175 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 2176 | * preserve R6-R9, and store return value into R0 |
| 2177 | * |
| 2178 | * Implicit input: |
| 2179 | * ctx == skb == R6 == CTX |
| 2180 | * |
| 2181 | * Explicit input: |
| 2182 | * SRC == any register |
| 2183 | * IMM == 32-bit immediate |
| 2184 | * |
| 2185 | * Output: |
| 2186 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 2187 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2188 | 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] | 2189 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2190 | struct bpf_reg_state *regs = env->cur_state.regs; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2191 | u8 mode = BPF_MODE(insn->code); |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2192 | struct bpf_reg_state *reg; |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2193 | int i, err; |
| 2194 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 2195 | if (!may_access_skb(env->prog->type)) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2196 | verbose("BPF_LD_[ABS|IND] instructions not allowed for this program type\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2197 | return -EINVAL; |
| 2198 | } |
| 2199 | |
| 2200 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
Alexei Starovoitov | d82bccc | 2016-04-12 10:26:19 -0700 | [diff] [blame] | 2201 | BPF_SIZE(insn->code) == BPF_DW || |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2202 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2203 | verbose("BPF_LD_[ABS|IND] uses reserved fields\n"); |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2204 | return -EINVAL; |
| 2205 | } |
| 2206 | |
| 2207 | /* check whether implicit source operand (register R6) is readable */ |
| 2208 | err = check_reg_arg(regs, BPF_REG_6, SRC_OP); |
| 2209 | if (err) |
| 2210 | return err; |
| 2211 | |
| 2212 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
| 2213 | verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
| 2214 | return -EINVAL; |
| 2215 | } |
| 2216 | |
| 2217 | if (mode == BPF_IND) { |
| 2218 | /* check explicit source operand */ |
| 2219 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2220 | if (err) |
| 2221 | return err; |
| 2222 | } |
| 2223 | |
| 2224 | /* reset caller saved regs to unreadable */ |
| 2225 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 2226 | reg = regs + caller_saved[i]; |
| 2227 | reg->type = NOT_INIT; |
| 2228 | reg->imm = 0; |
| 2229 | } |
| 2230 | |
| 2231 | /* mark destination R0 register as readable, since it contains |
| 2232 | * the value fetched from the packet |
| 2233 | */ |
| 2234 | regs[BPF_REG_0].type = UNKNOWN_VALUE; |
| 2235 | return 0; |
| 2236 | } |
| 2237 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2238 | /* non-recursive DFS pseudo code |
| 2239 | * 1 procedure DFS-iterative(G,v): |
| 2240 | * 2 label v as discovered |
| 2241 | * 3 let S be a stack |
| 2242 | * 4 S.push(v) |
| 2243 | * 5 while S is not empty |
| 2244 | * 6 t <- S.pop() |
| 2245 | * 7 if t is what we're looking for: |
| 2246 | * 8 return t |
| 2247 | * 9 for all edges e in G.adjacentEdges(t) do |
| 2248 | * 10 if edge e is already labelled |
| 2249 | * 11 continue with the next edge |
| 2250 | * 12 w <- G.adjacentVertex(t,e) |
| 2251 | * 13 if vertex w is not discovered and not explored |
| 2252 | * 14 label e as tree-edge |
| 2253 | * 15 label w as discovered |
| 2254 | * 16 S.push(w) |
| 2255 | * 17 continue at 5 |
| 2256 | * 18 else if vertex w is discovered |
| 2257 | * 19 label e as back-edge |
| 2258 | * 20 else |
| 2259 | * 21 // vertex w is explored |
| 2260 | * 22 label e as forward- or cross-edge |
| 2261 | * 23 label t as explored |
| 2262 | * 24 S.pop() |
| 2263 | * |
| 2264 | * convention: |
| 2265 | * 0x10 - discovered |
| 2266 | * 0x11 - discovered and fall-through edge labelled |
| 2267 | * 0x12 - discovered and fall-through and branch edges labelled |
| 2268 | * 0x20 - explored |
| 2269 | */ |
| 2270 | |
| 2271 | enum { |
| 2272 | DISCOVERED = 0x10, |
| 2273 | EXPLORED = 0x20, |
| 2274 | FALLTHROUGH = 1, |
| 2275 | BRANCH = 2, |
| 2276 | }; |
| 2277 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2278 | #define STATE_LIST_MARK ((struct bpf_verifier_state_list *) -1L) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2279 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2280 | static int *insn_stack; /* stack of insns to process */ |
| 2281 | static int cur_stack; /* current stack index */ |
| 2282 | static int *insn_state; |
| 2283 | |
| 2284 | /* t, w, e - match pseudo-code above: |
| 2285 | * t - index of current instruction |
| 2286 | * w - next instruction |
| 2287 | * e - edge |
| 2288 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2289 | 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] | 2290 | { |
| 2291 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 2292 | return 0; |
| 2293 | |
| 2294 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 2295 | return 0; |
| 2296 | |
| 2297 | if (w < 0 || w >= env->prog->len) { |
| 2298 | verbose("jump out of range from insn %d to %d\n", t, w); |
| 2299 | return -EINVAL; |
| 2300 | } |
| 2301 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2302 | if (e == BRANCH) |
| 2303 | /* mark branch target for state pruning */ |
| 2304 | env->explored_states[w] = STATE_LIST_MARK; |
| 2305 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2306 | if (insn_state[w] == 0) { |
| 2307 | /* tree-edge */ |
| 2308 | insn_state[t] = DISCOVERED | e; |
| 2309 | insn_state[w] = DISCOVERED; |
| 2310 | if (cur_stack >= env->prog->len) |
| 2311 | return -E2BIG; |
| 2312 | insn_stack[cur_stack++] = w; |
| 2313 | return 1; |
| 2314 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
| 2315 | verbose("back-edge from insn %d to %d\n", t, w); |
| 2316 | return -EINVAL; |
| 2317 | } else if (insn_state[w] == EXPLORED) { |
| 2318 | /* forward- or cross-edge */ |
| 2319 | insn_state[t] = DISCOVERED | e; |
| 2320 | } else { |
| 2321 | verbose("insn state internal bug\n"); |
| 2322 | return -EFAULT; |
| 2323 | } |
| 2324 | return 0; |
| 2325 | } |
| 2326 | |
| 2327 | /* non-recursive depth-first-search to detect loops in BPF program |
| 2328 | * loop == back-edge in directed graph |
| 2329 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2330 | static int check_cfg(struct bpf_verifier_env *env) |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2331 | { |
| 2332 | struct bpf_insn *insns = env->prog->insnsi; |
| 2333 | int insn_cnt = env->prog->len; |
| 2334 | int ret = 0; |
| 2335 | int i, t; |
| 2336 | |
| 2337 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 2338 | if (!insn_state) |
| 2339 | return -ENOMEM; |
| 2340 | |
| 2341 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 2342 | if (!insn_stack) { |
| 2343 | kfree(insn_state); |
| 2344 | return -ENOMEM; |
| 2345 | } |
| 2346 | |
| 2347 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 2348 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 2349 | cur_stack = 1; |
| 2350 | |
| 2351 | peek_stack: |
| 2352 | if (cur_stack == 0) |
| 2353 | goto check_state; |
| 2354 | t = insn_stack[cur_stack - 1]; |
| 2355 | |
| 2356 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 2357 | u8 opcode = BPF_OP(insns[t].code); |
| 2358 | |
| 2359 | if (opcode == BPF_EXIT) { |
| 2360 | goto mark_explored; |
| 2361 | } else if (opcode == BPF_CALL) { |
| 2362 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 2363 | if (ret == 1) |
| 2364 | goto peek_stack; |
| 2365 | else if (ret < 0) |
| 2366 | goto err_free; |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 2367 | if (t + 1 < insn_cnt) |
| 2368 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2369 | } else if (opcode == BPF_JA) { |
| 2370 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 2371 | ret = -EINVAL; |
| 2372 | goto err_free; |
| 2373 | } |
| 2374 | /* unconditional jump with single edge */ |
| 2375 | ret = push_insn(t, t + insns[t].off + 1, |
| 2376 | FALLTHROUGH, env); |
| 2377 | if (ret == 1) |
| 2378 | goto peek_stack; |
| 2379 | else if (ret < 0) |
| 2380 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2381 | /* tell verifier to check for equivalent states |
| 2382 | * after every call and jump |
| 2383 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame] | 2384 | if (t + 1 < insn_cnt) |
| 2385 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2386 | } else { |
| 2387 | /* conditional jump with two edges */ |
| 2388 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 2389 | if (ret == 1) |
| 2390 | goto peek_stack; |
| 2391 | else if (ret < 0) |
| 2392 | goto err_free; |
| 2393 | |
| 2394 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 2395 | if (ret == 1) |
| 2396 | goto peek_stack; |
| 2397 | else if (ret < 0) |
| 2398 | goto err_free; |
| 2399 | } |
| 2400 | } else { |
| 2401 | /* all other non-branch instructions with single |
| 2402 | * fall-through edge |
| 2403 | */ |
| 2404 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 2405 | if (ret == 1) |
| 2406 | goto peek_stack; |
| 2407 | else if (ret < 0) |
| 2408 | goto err_free; |
| 2409 | } |
| 2410 | |
| 2411 | mark_explored: |
| 2412 | insn_state[t] = EXPLORED; |
| 2413 | if (cur_stack-- <= 0) { |
| 2414 | verbose("pop stack internal bug\n"); |
| 2415 | ret = -EFAULT; |
| 2416 | goto err_free; |
| 2417 | } |
| 2418 | goto peek_stack; |
| 2419 | |
| 2420 | check_state: |
| 2421 | for (i = 0; i < insn_cnt; i++) { |
| 2422 | if (insn_state[i] != EXPLORED) { |
| 2423 | verbose("unreachable insn %d\n", i); |
| 2424 | ret = -EINVAL; |
| 2425 | goto err_free; |
| 2426 | } |
| 2427 | } |
| 2428 | ret = 0; /* cfg looks good */ |
| 2429 | |
| 2430 | err_free: |
| 2431 | kfree(insn_state); |
| 2432 | kfree(insn_stack); |
| 2433 | return ret; |
| 2434 | } |
| 2435 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2436 | /* the following conditions reduce the number of explored insns |
| 2437 | * from ~140k to ~80k for ultra large programs that use a lot of ptr_to_packet |
| 2438 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2439 | static bool compare_ptrs_to_packet(struct bpf_reg_state *old, |
| 2440 | struct bpf_reg_state *cur) |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2441 | { |
| 2442 | if (old->id != cur->id) |
| 2443 | return false; |
| 2444 | |
| 2445 | /* old ptr_to_packet is more conservative, since it allows smaller |
| 2446 | * range. Ex: |
| 2447 | * old(off=0,r=10) is equal to cur(off=0,r=20), because |
| 2448 | * old(off=0,r=10) means that with range=10 the verifier proceeded |
| 2449 | * further and found no issues with the program. Now we're in the same |
| 2450 | * spot with cur(off=0,r=20), so we're safe too, since anything further |
| 2451 | * will only be looking at most 10 bytes after this pointer. |
| 2452 | */ |
| 2453 | if (old->off == cur->off && old->range < cur->range) |
| 2454 | return true; |
| 2455 | |
| 2456 | /* old(off=20,r=10) is equal to cur(off=22,re=22 or 5 or 0) |
| 2457 | * since both cannot be used for packet access and safe(old) |
| 2458 | * pointer has smaller off that could be used for further |
| 2459 | * 'if (ptr > data_end)' check |
| 2460 | * Ex: |
| 2461 | * old(off=20,r=10) and cur(off=22,r=22) and cur(off=22,r=0) mean |
| 2462 | * that we cannot access the packet. |
| 2463 | * The safe range is: |
| 2464 | * [ptr, ptr + range - off) |
| 2465 | * so whenever off >=range, it means no safe bytes from this pointer. |
| 2466 | * When comparing old->off <= cur->off, it means that older code |
| 2467 | * went with smaller offset and that offset was later |
| 2468 | * used to figure out the safe range after 'if (ptr > data_end)' check |
| 2469 | * Say, 'old' state was explored like: |
| 2470 | * ... R3(off=0, r=0) |
| 2471 | * R4 = R3 + 20 |
| 2472 | * ... now R4(off=20,r=0) <-- here |
| 2473 | * if (R4 > data_end) |
| 2474 | * ... R4(off=20,r=20), R3(off=0,r=20) and R3 can be used to access. |
| 2475 | * ... the code further went all the way to bpf_exit. |
| 2476 | * Now the 'cur' state at the mark 'here' has R4(off=30,r=0). |
| 2477 | * old_R4(off=20,r=0) equal to cur_R4(off=30,r=0), since if the verifier |
| 2478 | * goes further, such cur_R4 will give larger safe packet range after |
| 2479 | * 'if (R4 > data_end)' and all further insn were already good with r=20, |
| 2480 | * so they will be good with r=30 and we can prune the search. |
| 2481 | */ |
| 2482 | if (old->off <= cur->off && |
| 2483 | old->off >= old->range && cur->off >= cur->range) |
| 2484 | return true; |
| 2485 | |
| 2486 | return false; |
| 2487 | } |
| 2488 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2489 | /* compare two verifier states |
| 2490 | * |
| 2491 | * all states stored in state_list are known to be valid, since |
| 2492 | * verifier reached 'bpf_exit' instruction through them |
| 2493 | * |
| 2494 | * this function is called when verifier exploring different branches of |
| 2495 | * execution popped from the state stack. If it sees an old state that has |
| 2496 | * more strict register state and more strict stack state then this execution |
| 2497 | * branch doesn't need to be explored further, since verifier already |
| 2498 | * concluded that more strict state leads to valid finish. |
| 2499 | * |
| 2500 | * Therefore two states are equivalent if register state is more conservative |
| 2501 | * and explored stack state is more conservative than the current one. |
| 2502 | * Example: |
| 2503 | * explored current |
| 2504 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 2505 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 2506 | * |
| 2507 | * In other words if current stack state (one being explored) has more |
| 2508 | * valid slots than old one that already passed validation, it means |
| 2509 | * the verifier can stop exploring and conclude that current state is valid too |
| 2510 | * |
| 2511 | * Similarly with registers. If explored state has register type as invalid |
| 2512 | * whereas register type in current state is meaningful, it means that |
| 2513 | * the current state will reach 'bpf_exit' instruction safely |
| 2514 | */ |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2515 | static bool states_equal(struct bpf_verifier_env *env, |
| 2516 | struct bpf_verifier_state *old, |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2517 | struct bpf_verifier_state *cur) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2518 | { |
Josef Bacik | e2d2afe | 2016-11-29 12:27:09 -0500 | [diff] [blame] | 2519 | bool varlen_map_access = env->varlen_map_value_access; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2520 | struct bpf_reg_state *rold, *rcur; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2521 | int i; |
| 2522 | |
| 2523 | for (i = 0; i < MAX_BPF_REG; i++) { |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2524 | rold = &old->regs[i]; |
| 2525 | rcur = &cur->regs[i]; |
| 2526 | |
| 2527 | if (memcmp(rold, rcur, sizeof(*rold)) == 0) |
| 2528 | continue; |
| 2529 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2530 | /* If the ranges were not the same, but everything else was and |
| 2531 | * we didn't do a variable access into a map then we are a-ok. |
| 2532 | */ |
Josef Bacik | e2d2afe | 2016-11-29 12:27:09 -0500 | [diff] [blame] | 2533 | if (!varlen_map_access && |
Alexei Starovoitov | b7f5aa1 | 2016-12-07 10:57:59 -0800 | [diff] [blame] | 2534 | memcmp(rold, rcur, offsetofend(struct bpf_reg_state, id)) == 0) |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2535 | continue; |
| 2536 | |
Josef Bacik | e2d2afe | 2016-11-29 12:27:09 -0500 | [diff] [blame] | 2537 | /* If we didn't map access then again we don't care about the |
| 2538 | * mismatched range values and it's ok if our old type was |
| 2539 | * UNKNOWN and we didn't go to a NOT_INIT'ed reg. |
| 2540 | */ |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2541 | if (rold->type == NOT_INIT || |
Josef Bacik | e2d2afe | 2016-11-29 12:27:09 -0500 | [diff] [blame] | 2542 | (!varlen_map_access && rold->type == UNKNOWN_VALUE && |
| 2543 | rcur->type != NOT_INIT)) |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2544 | continue; |
| 2545 | |
Alexei Starovoitov | 969bf05 | 2016-05-05 19:49:10 -0700 | [diff] [blame] | 2546 | if (rold->type == PTR_TO_PACKET && rcur->type == PTR_TO_PACKET && |
| 2547 | compare_ptrs_to_packet(rold, rcur)) |
| 2548 | continue; |
| 2549 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2550 | return false; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | for (i = 0; i < MAX_BPF_STACK; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2554 | if (old->stack_slot_type[i] == STACK_INVALID) |
| 2555 | continue; |
| 2556 | if (old->stack_slot_type[i] != cur->stack_slot_type[i]) |
| 2557 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 2558 | * this stack slot, but current has has STACK_MISC -> |
| 2559 | * this verifier states are not equivalent, |
| 2560 | * return false to continue verification of this path |
| 2561 | */ |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2562 | return false; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2563 | if (i % BPF_REG_SIZE) |
| 2564 | continue; |
| 2565 | if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE], |
| 2566 | &cur->spilled_regs[i / BPF_REG_SIZE], |
| 2567 | sizeof(old->spilled_regs[0]))) |
| 2568 | /* when explored and current stack slot types are |
| 2569 | * the same, check that stored pointers types |
| 2570 | * are the same as well. |
| 2571 | * Ex: explored safe path could have stored |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2572 | * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -8} |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2573 | * but current path has stored: |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2574 | * (bpf_reg_state) {.type = PTR_TO_STACK, .imm = -16} |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 2575 | * such verifier states are not equivalent. |
| 2576 | * return false to continue verification of this path |
| 2577 | */ |
| 2578 | return false; |
| 2579 | else |
| 2580 | continue; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2581 | } |
| 2582 | return true; |
| 2583 | } |
| 2584 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2585 | 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] | 2586 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2587 | struct bpf_verifier_state_list *new_sl; |
| 2588 | struct bpf_verifier_state_list *sl; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2589 | |
| 2590 | sl = env->explored_states[insn_idx]; |
| 2591 | if (!sl) |
| 2592 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 2593 | * be doing state search here |
| 2594 | */ |
| 2595 | return 0; |
| 2596 | |
| 2597 | while (sl != STATE_LIST_MARK) { |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2598 | if (states_equal(env, &sl->state, &env->cur_state)) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2599 | /* reached equivalent register/stack state, |
| 2600 | * prune the search |
| 2601 | */ |
| 2602 | return 1; |
| 2603 | sl = sl->next; |
| 2604 | } |
| 2605 | |
| 2606 | /* there were no equivalent states, remember current one. |
| 2607 | * technically the current state is not proven to be safe yet, |
| 2608 | * but it will either reach bpf_exit (which means it's safe) or |
| 2609 | * it will be rejected. Since there are no loops, we won't be |
| 2610 | * seeing this 'insn_idx' instruction again on the way to bpf_exit |
| 2611 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2612 | new_sl = kmalloc(sizeof(struct bpf_verifier_state_list), GFP_USER); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2613 | if (!new_sl) |
| 2614 | return -ENOMEM; |
| 2615 | |
| 2616 | /* add new state to the head of linked list */ |
| 2617 | memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state)); |
| 2618 | new_sl->next = env->explored_states[insn_idx]; |
| 2619 | env->explored_states[insn_idx] = new_sl; |
| 2620 | return 0; |
| 2621 | } |
| 2622 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 2623 | static int ext_analyzer_insn_hook(struct bpf_verifier_env *env, |
| 2624 | int insn_idx, int prev_insn_idx) |
| 2625 | { |
| 2626 | if (!env->analyzer_ops || !env->analyzer_ops->insn_hook) |
| 2627 | return 0; |
| 2628 | |
| 2629 | return env->analyzer_ops->insn_hook(env, insn_idx, prev_insn_idx); |
| 2630 | } |
| 2631 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2632 | static int do_check(struct bpf_verifier_env *env) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2633 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2634 | struct bpf_verifier_state *state = &env->cur_state; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2635 | struct bpf_insn *insns = env->prog->insnsi; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2636 | struct bpf_reg_state *regs = state->regs; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2637 | int insn_cnt = env->prog->len; |
| 2638 | int insn_idx, prev_insn_idx = 0; |
| 2639 | int insn_processed = 0; |
| 2640 | bool do_print_state = false; |
| 2641 | |
| 2642 | init_reg_state(regs); |
| 2643 | insn_idx = 0; |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2644 | env->varlen_map_value_access = false; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2645 | for (;;) { |
| 2646 | struct bpf_insn *insn; |
| 2647 | u8 class; |
| 2648 | int err; |
| 2649 | |
| 2650 | if (insn_idx >= insn_cnt) { |
| 2651 | verbose("invalid insn idx %d insn_cnt %d\n", |
| 2652 | insn_idx, insn_cnt); |
| 2653 | return -EFAULT; |
| 2654 | } |
| 2655 | |
| 2656 | insn = &insns[insn_idx]; |
| 2657 | class = BPF_CLASS(insn->code); |
| 2658 | |
Daniel Borkmann | 0701615 | 2016-04-05 22:33:17 +0200 | [diff] [blame] | 2659 | if (++insn_processed > BPF_COMPLEXITY_LIMIT_INSNS) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2660 | verbose("BPF program is too large. Proccessed %d insn\n", |
| 2661 | insn_processed); |
| 2662 | return -E2BIG; |
| 2663 | } |
| 2664 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2665 | err = is_state_visited(env, insn_idx); |
| 2666 | if (err < 0) |
| 2667 | return err; |
| 2668 | if (err == 1) { |
| 2669 | /* found equivalent state, can prune the search */ |
| 2670 | if (log_level) { |
| 2671 | if (do_print_state) |
| 2672 | verbose("\nfrom %d to %d: safe\n", |
| 2673 | prev_insn_idx, insn_idx); |
| 2674 | else |
| 2675 | verbose("%d: safe\n", insn_idx); |
| 2676 | } |
| 2677 | goto process_bpf_exit; |
| 2678 | } |
| 2679 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2680 | if (log_level && do_print_state) { |
| 2681 | verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx); |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2682 | print_verifier_state(&env->cur_state); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2683 | do_print_state = false; |
| 2684 | } |
| 2685 | |
| 2686 | if (log_level) { |
| 2687 | verbose("%d: ", insn_idx); |
Daniel Borkmann | ced0a31 | 2017-05-08 00:04:09 +0200 | [diff] [blame] | 2688 | print_bpf_insn(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2689 | } |
| 2690 | |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 2691 | err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx); |
| 2692 | if (err) |
| 2693 | return err; |
| 2694 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2695 | if (class == BPF_ALU || class == BPF_ALU64) { |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2696 | err = check_alu_op(env, insn); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2697 | if (err) |
| 2698 | return err; |
| 2699 | |
| 2700 | } else if (class == BPF_LDX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2701 | enum bpf_reg_type *prev_src_type, src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2702 | |
| 2703 | /* check for reserved fields is already done */ |
| 2704 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2705 | /* check src operand */ |
| 2706 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2707 | if (err) |
| 2708 | return err; |
| 2709 | |
| 2710 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); |
| 2711 | if (err) |
| 2712 | return err; |
| 2713 | |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 2714 | src_reg_type = regs[insn->src_reg].type; |
| 2715 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2716 | /* check that memory (src_reg + off) is readable, |
| 2717 | * the state of dst_reg will be updated by this func |
| 2718 | */ |
| 2719 | err = check_mem_access(env, insn->src_reg, insn->off, |
| 2720 | BPF_SIZE(insn->code), BPF_READ, |
| 2721 | insn->dst_reg); |
| 2722 | if (err) |
| 2723 | return err; |
| 2724 | |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2725 | reset_reg_range_values(regs, insn->dst_reg); |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 2726 | if (BPF_SIZE(insn->code) != BPF_W && |
| 2727 | BPF_SIZE(insn->code) != BPF_DW) { |
Alexei Starovoitov | 725f9dc | 2015-04-15 16:19:33 -0700 | [diff] [blame] | 2728 | insn_idx++; |
| 2729 | continue; |
| 2730 | } |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2731 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2732 | prev_src_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 2733 | |
| 2734 | if (*prev_src_type == NOT_INIT) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2735 | /* saw a valid insn |
| 2736 | * dst_reg = *(u32 *)(src_reg + off) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2737 | * save type to validate intersecting paths |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2738 | */ |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2739 | *prev_src_type = src_reg_type; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2740 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2741 | } else if (src_reg_type != *prev_src_type && |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2742 | (src_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2743 | *prev_src_type == PTR_TO_CTX)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2744 | /* ABuser program is trying to use the same insn |
| 2745 | * dst_reg = *(u32*) (src_reg + off) |
| 2746 | * with different pointer types: |
| 2747 | * src_reg == ctx in one branch and |
| 2748 | * src_reg == stack|map in some other branch. |
| 2749 | * Reject it. |
| 2750 | */ |
| 2751 | verbose("same insn cannot be used with different pointers\n"); |
| 2752 | return -EINVAL; |
| 2753 | } |
| 2754 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2755 | } else if (class == BPF_STX) { |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2756 | enum bpf_reg_type *prev_dst_type, dst_reg_type; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2757 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2758 | if (BPF_MODE(insn->code) == BPF_XADD) { |
| 2759 | err = check_xadd(env, insn); |
| 2760 | if (err) |
| 2761 | return err; |
| 2762 | insn_idx++; |
| 2763 | continue; |
| 2764 | } |
| 2765 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2766 | /* check src1 operand */ |
| 2767 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 2768 | if (err) |
| 2769 | return err; |
| 2770 | /* check src2 operand */ |
| 2771 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 2772 | if (err) |
| 2773 | return err; |
| 2774 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2775 | dst_reg_type = regs[insn->dst_reg].type; |
| 2776 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2777 | /* check that memory (dst_reg + off) is writeable */ |
| 2778 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 2779 | BPF_SIZE(insn->code), BPF_WRITE, |
| 2780 | insn->src_reg); |
| 2781 | if (err) |
| 2782 | return err; |
| 2783 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2784 | prev_dst_type = &env->insn_aux_data[insn_idx].ptr_type; |
| 2785 | |
| 2786 | if (*prev_dst_type == NOT_INIT) { |
| 2787 | *prev_dst_type = dst_reg_type; |
| 2788 | } else if (dst_reg_type != *prev_dst_type && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2789 | (dst_reg_type == PTR_TO_CTX || |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 2790 | *prev_dst_type == PTR_TO_CTX)) { |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2791 | verbose("same insn cannot be used with different pointers\n"); |
| 2792 | return -EINVAL; |
| 2793 | } |
| 2794 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2795 | } else if (class == BPF_ST) { |
| 2796 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 2797 | insn->src_reg != BPF_REG_0) { |
| 2798 | verbose("BPF_ST uses reserved fields\n"); |
| 2799 | return -EINVAL; |
| 2800 | } |
| 2801 | /* check src operand */ |
| 2802 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 2803 | if (err) |
| 2804 | return err; |
| 2805 | |
| 2806 | /* check that memory (dst_reg + off) is writeable */ |
| 2807 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 2808 | BPF_SIZE(insn->code), BPF_WRITE, |
| 2809 | -1); |
| 2810 | if (err) |
| 2811 | return err; |
| 2812 | |
| 2813 | } else if (class == BPF_JMP) { |
| 2814 | u8 opcode = BPF_OP(insn->code); |
| 2815 | |
| 2816 | if (opcode == BPF_CALL) { |
| 2817 | if (BPF_SRC(insn->code) != BPF_K || |
| 2818 | insn->off != 0 || |
| 2819 | insn->src_reg != BPF_REG_0 || |
| 2820 | insn->dst_reg != BPF_REG_0) { |
| 2821 | verbose("BPF_CALL uses reserved fields\n"); |
| 2822 | return -EINVAL; |
| 2823 | } |
| 2824 | |
| 2825 | err = check_call(env, insn->imm); |
| 2826 | if (err) |
| 2827 | return err; |
| 2828 | |
| 2829 | } else if (opcode == BPF_JA) { |
| 2830 | if (BPF_SRC(insn->code) != BPF_K || |
| 2831 | insn->imm != 0 || |
| 2832 | insn->src_reg != BPF_REG_0 || |
| 2833 | insn->dst_reg != BPF_REG_0) { |
| 2834 | verbose("BPF_JA uses reserved fields\n"); |
| 2835 | return -EINVAL; |
| 2836 | } |
| 2837 | |
| 2838 | insn_idx += insn->off + 1; |
| 2839 | continue; |
| 2840 | |
| 2841 | } else if (opcode == BPF_EXIT) { |
| 2842 | if (BPF_SRC(insn->code) != BPF_K || |
| 2843 | insn->imm != 0 || |
| 2844 | insn->src_reg != BPF_REG_0 || |
| 2845 | insn->dst_reg != BPF_REG_0) { |
| 2846 | verbose("BPF_EXIT uses reserved fields\n"); |
| 2847 | return -EINVAL; |
| 2848 | } |
| 2849 | |
| 2850 | /* eBPF calling convetion is such that R0 is used |
| 2851 | * to return the value from eBPF program. |
| 2852 | * Make sure that it's readable at this time |
| 2853 | * of bpf_exit, which means that program wrote |
| 2854 | * something into it earlier |
| 2855 | */ |
| 2856 | err = check_reg_arg(regs, BPF_REG_0, SRC_OP); |
| 2857 | if (err) |
| 2858 | return err; |
| 2859 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 2860 | if (is_pointer_value(env, BPF_REG_0)) { |
| 2861 | verbose("R0 leaks addr as return value\n"); |
| 2862 | return -EACCES; |
| 2863 | } |
| 2864 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2865 | process_bpf_exit: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2866 | insn_idx = pop_stack(env, &prev_insn_idx); |
| 2867 | if (insn_idx < 0) { |
| 2868 | break; |
| 2869 | } else { |
| 2870 | do_print_state = true; |
| 2871 | continue; |
| 2872 | } |
| 2873 | } else { |
| 2874 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 2875 | if (err) |
| 2876 | return err; |
| 2877 | } |
| 2878 | } else if (class == BPF_LD) { |
| 2879 | u8 mode = BPF_MODE(insn->code); |
| 2880 | |
| 2881 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 2882 | err = check_ld_abs(env, insn); |
| 2883 | if (err) |
| 2884 | return err; |
| 2885 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2886 | } else if (mode == BPF_IMM) { |
| 2887 | err = check_ld_imm(env, insn); |
| 2888 | if (err) |
| 2889 | return err; |
| 2890 | |
| 2891 | insn_idx++; |
| 2892 | } else { |
| 2893 | verbose("invalid BPF_LD mode\n"); |
| 2894 | return -EINVAL; |
| 2895 | } |
Josef Bacik | 4846113 | 2016-09-28 10:54:32 -0400 | [diff] [blame] | 2896 | reset_reg_range_values(regs, insn->dst_reg); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2897 | } else { |
| 2898 | verbose("unknown insn class %d\n", class); |
| 2899 | return -EINVAL; |
| 2900 | } |
| 2901 | |
| 2902 | insn_idx++; |
| 2903 | } |
| 2904 | |
Alexei Starovoitov | 1a0dc1a | 2016-05-05 19:49:09 -0700 | [diff] [blame] | 2905 | verbose("processed %d insns\n", insn_processed); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2906 | return 0; |
| 2907 | } |
| 2908 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 2909 | static int check_map_prog_compatibility(struct bpf_map *map, |
| 2910 | struct bpf_prog *prog) |
| 2911 | |
| 2912 | { |
| 2913 | if (prog->type == BPF_PROG_TYPE_PERF_EVENT && |
| 2914 | (map->map_type == BPF_MAP_TYPE_HASH || |
| 2915 | map->map_type == BPF_MAP_TYPE_PERCPU_HASH) && |
| 2916 | (map->map_flags & BPF_F_NO_PREALLOC)) { |
| 2917 | verbose("perf_event programs can only use preallocated hash map\n"); |
| 2918 | return -EINVAL; |
| 2919 | } |
| 2920 | return 0; |
| 2921 | } |
| 2922 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2923 | /* look for pseudo eBPF instructions that access map FDs and |
| 2924 | * replace them with actual map pointers |
| 2925 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 2926 | 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] | 2927 | { |
| 2928 | struct bpf_insn *insn = env->prog->insnsi; |
| 2929 | int insn_cnt = env->prog->len; |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 2930 | int i, j, err; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2931 | |
| 2932 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2933 | if (BPF_CLASS(insn->code) == BPF_LDX && |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2934 | (BPF_MODE(insn->code) != BPF_MEM || insn->imm != 0)) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2935 | verbose("BPF_LDX uses reserved fields\n"); |
| 2936 | return -EINVAL; |
| 2937 | } |
| 2938 | |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 2939 | if (BPF_CLASS(insn->code) == BPF_STX && |
| 2940 | ((BPF_MODE(insn->code) != BPF_MEM && |
| 2941 | BPF_MODE(insn->code) != BPF_XADD) || insn->imm != 0)) { |
| 2942 | verbose("BPF_STX uses reserved fields\n"); |
| 2943 | return -EINVAL; |
| 2944 | } |
| 2945 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2946 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 2947 | struct bpf_map *map; |
| 2948 | struct fd f; |
| 2949 | |
| 2950 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 2951 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 2952 | insn[1].off != 0) { |
| 2953 | verbose("invalid bpf_ld_imm64 insn\n"); |
| 2954 | return -EINVAL; |
| 2955 | } |
| 2956 | |
| 2957 | if (insn->src_reg == 0) |
| 2958 | /* valid generic load 64-bit imm */ |
| 2959 | goto next_insn; |
| 2960 | |
| 2961 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
| 2962 | verbose("unrecognized bpf_ld_imm64 insn\n"); |
| 2963 | return -EINVAL; |
| 2964 | } |
| 2965 | |
| 2966 | f = fdget(insn->imm); |
Daniel Borkmann | c210129 | 2015-10-29 14:58:07 +0100 | [diff] [blame] | 2967 | map = __bpf_map_get(f); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2968 | if (IS_ERR(map)) { |
| 2969 | verbose("fd %d is not pointing to valid bpf_map\n", |
| 2970 | insn->imm); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2971 | return PTR_ERR(map); |
| 2972 | } |
| 2973 | |
Alexei Starovoitov | fdc15d3 | 2016-09-01 18:37:23 -0700 | [diff] [blame] | 2974 | err = check_map_prog_compatibility(map, env->prog); |
| 2975 | if (err) { |
| 2976 | fdput(f); |
| 2977 | return err; |
| 2978 | } |
| 2979 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2980 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 2981 | insn[0].imm = (u32) (unsigned long) map; |
| 2982 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 2983 | |
| 2984 | /* check whether we recorded this map already */ |
| 2985 | for (j = 0; j < env->used_map_cnt; j++) |
| 2986 | if (env->used_maps[j] == map) { |
| 2987 | fdput(f); |
| 2988 | goto next_insn; |
| 2989 | } |
| 2990 | |
| 2991 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 2992 | fdput(f); |
| 2993 | return -E2BIG; |
| 2994 | } |
| 2995 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2996 | /* hold the map. If the program is rejected by verifier, |
| 2997 | * the map will be released by release_maps() or it |
| 2998 | * will be used by the valid program until it's unloaded |
| 2999 | * and all maps are released in free_bpf_prog_info() |
| 3000 | */ |
Alexei Starovoitov | 92117d8 | 2016-04-27 18:56:20 -0700 | [diff] [blame] | 3001 | map = bpf_map_inc(map, false); |
| 3002 | if (IS_ERR(map)) { |
| 3003 | fdput(f); |
| 3004 | return PTR_ERR(map); |
| 3005 | } |
| 3006 | env->used_maps[env->used_map_cnt++] = map; |
| 3007 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3008 | fdput(f); |
| 3009 | next_insn: |
| 3010 | insn++; |
| 3011 | i++; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 3016 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 3017 | * These pointers will be used later by verifier to validate map access. |
| 3018 | */ |
| 3019 | return 0; |
| 3020 | } |
| 3021 | |
| 3022 | /* drop refcnt of maps used by the rejected program */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3023 | static void release_maps(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3024 | { |
| 3025 | int i; |
| 3026 | |
| 3027 | for (i = 0; i < env->used_map_cnt; i++) |
| 3028 | bpf_map_put(env->used_maps[i]); |
| 3029 | } |
| 3030 | |
| 3031 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3032 | static void convert_pseudo_ld_imm64(struct bpf_verifier_env *env) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3033 | { |
| 3034 | struct bpf_insn *insn = env->prog->insnsi; |
| 3035 | int insn_cnt = env->prog->len; |
| 3036 | int i; |
| 3037 | |
| 3038 | for (i = 0; i < insn_cnt; i++, insn++) |
| 3039 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 3040 | insn->src_reg = 0; |
| 3041 | } |
| 3042 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3043 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 3044 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 3045 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3046 | static int convert_ctx_accesses(struct bpf_verifier_env *env) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3047 | { |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3048 | const struct bpf_verifier_ops *ops = env->prog->aux->ops; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3049 | const int insn_cnt = env->prog->len; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3050 | struct bpf_insn insn_buf[16], *insn; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3051 | struct bpf_prog *new_prog; |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3052 | enum bpf_access_type type; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3053 | int i, cnt, delta = 0; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3054 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3055 | if (ops->gen_prologue) { |
| 3056 | cnt = ops->gen_prologue(insn_buf, env->seen_direct_write, |
| 3057 | env->prog); |
| 3058 | if (cnt >= ARRAY_SIZE(insn_buf)) { |
| 3059 | verbose("bpf verifier is misconfigured\n"); |
| 3060 | return -EINVAL; |
| 3061 | } else if (cnt) { |
| 3062 | new_prog = bpf_patch_insn_single(env->prog, 0, |
| 3063 | insn_buf, cnt); |
| 3064 | if (!new_prog) |
| 3065 | return -ENOMEM; |
| 3066 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3067 | delta += cnt - 1; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | if (!ops->convert_ctx_access) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3072 | return 0; |
| 3073 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3074 | insn = env->prog->insnsi + delta; |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3075 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3076 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 3077 | if (insn->code == (BPF_LDX | BPF_MEM | BPF_W) || |
| 3078 | insn->code == (BPF_LDX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3079 | type = BPF_READ; |
Alexei Starovoitov | ea2e7ce | 2016-09-01 18:37:21 -0700 | [diff] [blame] | 3080 | else if (insn->code == (BPF_STX | BPF_MEM | BPF_W) || |
| 3081 | insn->code == (BPF_STX | BPF_MEM | BPF_DW)) |
Alexei Starovoitov | d691f9e | 2015-06-04 10:11:54 -0700 | [diff] [blame] | 3082 | type = BPF_WRITE; |
| 3083 | else |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3084 | continue; |
| 3085 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3086 | if (env->insn_aux_data[i].ptr_type != PTR_TO_CTX) |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3087 | continue; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3088 | |
Daniel Borkmann | 36bbef5 | 2016-09-20 00:26:13 +0200 | [diff] [blame] | 3089 | cnt = ops->convert_ctx_access(type, insn->dst_reg, insn->src_reg, |
| 3090 | insn->off, insn_buf, env->prog); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3091 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 3092 | verbose("bpf verifier is misconfigured\n"); |
| 3093 | return -EINVAL; |
| 3094 | } |
| 3095 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3096 | new_prog = bpf_patch_insn_single(env->prog, i + delta, insn_buf, |
| 3097 | cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3098 | if (!new_prog) |
| 3099 | return -ENOMEM; |
| 3100 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3101 | delta += cnt - 1; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3102 | |
| 3103 | /* keep walking new program and skip insns we just inserted */ |
| 3104 | env->prog = new_prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3105 | insn = new_prog->insnsi + i + delta; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | return 0; |
| 3109 | } |
| 3110 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3111 | static void free_states(struct bpf_verifier_env *env) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3112 | { |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3113 | struct bpf_verifier_state_list *sl, *sln; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3114 | int i; |
| 3115 | |
| 3116 | if (!env->explored_states) |
| 3117 | return; |
| 3118 | |
| 3119 | for (i = 0; i < env->prog->len; i++) { |
| 3120 | sl = env->explored_states[i]; |
| 3121 | |
| 3122 | if (sl) |
| 3123 | while (sl != STATE_LIST_MARK) { |
| 3124 | sln = sl->next; |
| 3125 | kfree(sl); |
| 3126 | sl = sln; |
| 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | kfree(env->explored_states); |
| 3131 | } |
| 3132 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3133 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 3134 | { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3135 | char __user *log_ubuf = NULL; |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3136 | struct bpf_verifier_env *env; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 3137 | int ret = -EINVAL; |
| 3138 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3139 | if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3140 | return -E2BIG; |
| 3141 | |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3142 | /* '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] | 3143 | * allocate/free it every time bpf_check() is called |
| 3144 | */ |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3145 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3146 | if (!env) |
| 3147 | return -ENOMEM; |
| 3148 | |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3149 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 3150 | (*prog)->len); |
| 3151 | ret = -ENOMEM; |
| 3152 | if (!env->insn_aux_data) |
| 3153 | goto err_free_env; |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3154 | env->prog = *prog; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3155 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3156 | /* grab the mutex to protect few globals used by verifier */ |
| 3157 | mutex_lock(&bpf_verifier_lock); |
| 3158 | |
| 3159 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 3160 | /* user requested verbose verifier output |
| 3161 | * and supplied buffer to store the verification trace |
| 3162 | */ |
| 3163 | log_level = attr->log_level; |
| 3164 | log_ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 3165 | log_size = attr->log_size; |
| 3166 | log_len = 0; |
| 3167 | |
| 3168 | ret = -EINVAL; |
| 3169 | /* log_* values have to be sane */ |
| 3170 | if (log_size < 128 || log_size > UINT_MAX >> 8 || |
| 3171 | log_level == 0 || log_ubuf == NULL) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3172 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3173 | |
| 3174 | ret = -ENOMEM; |
| 3175 | log_buf = vmalloc(log_size); |
| 3176 | if (!log_buf) |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3177 | goto err_unlock; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3178 | } else { |
| 3179 | log_level = 0; |
| 3180 | } |
| 3181 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3182 | ret = replace_map_fd_with_map_ptr(env); |
| 3183 | if (ret < 0) |
| 3184 | goto skip_full_check; |
| 3185 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3186 | env->explored_states = kcalloc(env->prog->len, |
Jakub Kicinski | 58e2af8 | 2016-09-21 11:43:57 +0100 | [diff] [blame] | 3187 | sizeof(struct bpf_verifier_state_list *), |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3188 | GFP_USER); |
| 3189 | ret = -ENOMEM; |
| 3190 | if (!env->explored_states) |
| 3191 | goto skip_full_check; |
| 3192 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 3193 | ret = check_cfg(env); |
| 3194 | if (ret < 0) |
| 3195 | goto skip_full_check; |
| 3196 | |
Alexei Starovoitov | 1be7f75 | 2015-10-07 22:23:21 -0700 | [diff] [blame] | 3197 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 3198 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3199 | ret = do_check(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3200 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3201 | skip_full_check: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 3202 | while (pop_stack(env, NULL) >= 0); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 3203 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3204 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3205 | if (ret == 0) |
| 3206 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 3207 | ret = convert_ctx_accesses(env); |
| 3208 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3209 | if (log_level && log_len >= log_size - 1) { |
| 3210 | BUG_ON(log_len >= log_size); |
| 3211 | /* verifier log exceeded user supplied buffer */ |
| 3212 | ret = -ENOSPC; |
| 3213 | /* fall through to return what was recorded */ |
| 3214 | } |
| 3215 | |
| 3216 | /* copy verifier log back to user space including trailing zero */ |
| 3217 | if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) { |
| 3218 | ret = -EFAULT; |
| 3219 | goto free_log_buf; |
| 3220 | } |
| 3221 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3222 | if (ret == 0 && env->used_map_cnt) { |
| 3223 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3224 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 3225 | sizeof(env->used_maps[0]), |
| 3226 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3227 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3228 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3229 | ret = -ENOMEM; |
| 3230 | goto free_log_buf; |
| 3231 | } |
| 3232 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3233 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3234 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3235 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3236 | |
| 3237 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 3238 | * bpf_ld_imm64 instructions |
| 3239 | */ |
| 3240 | convert_pseudo_ld_imm64(env); |
| 3241 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3242 | |
| 3243 | free_log_buf: |
| 3244 | if (log_level) |
| 3245 | vfree(log_buf); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3246 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 3247 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 3248 | * them now. Otherwise free_bpf_prog_info() will release them. |
| 3249 | */ |
| 3250 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 3251 | *prog = env->prog; |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3252 | err_unlock: |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 3253 | mutex_unlock(&bpf_verifier_lock); |
Jakub Kicinski | 3df126f | 2016-09-21 11:43:56 +0100 | [diff] [blame] | 3254 | vfree(env->insn_aux_data); |
| 3255 | err_free_env: |
| 3256 | kfree(env); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 3257 | return ret; |
| 3258 | } |
Jakub Kicinski | 13a27df | 2016-09-21 11:43:58 +0100 | [diff] [blame] | 3259 | |
| 3260 | int bpf_analyzer(struct bpf_prog *prog, const struct bpf_ext_analyzer_ops *ops, |
| 3261 | void *priv) |
| 3262 | { |
| 3263 | struct bpf_verifier_env *env; |
| 3264 | int ret; |
| 3265 | |
| 3266 | env = kzalloc(sizeof(struct bpf_verifier_env), GFP_KERNEL); |
| 3267 | if (!env) |
| 3268 | return -ENOMEM; |
| 3269 | |
| 3270 | env->insn_aux_data = vzalloc(sizeof(struct bpf_insn_aux_data) * |
| 3271 | prog->len); |
| 3272 | ret = -ENOMEM; |
| 3273 | if (!env->insn_aux_data) |
| 3274 | goto err_free_env; |
| 3275 | env->prog = prog; |
| 3276 | env->analyzer_ops = ops; |
| 3277 | env->analyzer_priv = priv; |
| 3278 | |
| 3279 | /* grab the mutex to protect few globals used by verifier */ |
| 3280 | mutex_lock(&bpf_verifier_lock); |
| 3281 | |
| 3282 | log_level = 0; |
| 3283 | |
| 3284 | env->explored_states = kcalloc(env->prog->len, |
| 3285 | sizeof(struct bpf_verifier_state_list *), |
| 3286 | GFP_KERNEL); |
| 3287 | ret = -ENOMEM; |
| 3288 | if (!env->explored_states) |
| 3289 | goto skip_full_check; |
| 3290 | |
| 3291 | ret = check_cfg(env); |
| 3292 | if (ret < 0) |
| 3293 | goto skip_full_check; |
| 3294 | |
| 3295 | env->allow_ptr_leaks = capable(CAP_SYS_ADMIN); |
| 3296 | |
| 3297 | ret = do_check(env); |
| 3298 | |
| 3299 | skip_full_check: |
| 3300 | while (pop_stack(env, NULL) >= 0); |
| 3301 | free_states(env); |
| 3302 | |
| 3303 | mutex_unlock(&bpf_verifier_lock); |
| 3304 | vfree(env->insn_aux_data); |
| 3305 | err_free_env: |
| 3306 | kfree(env); |
| 3307 | return ret; |
| 3308 | } |
| 3309 | EXPORT_SYMBOL_GPL(bpf_analyzer); |