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