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