Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or |
| 4 | * modify it under the terms of version 2 of the GNU General Public |
| 5 | * License as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, but |
| 8 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 | * General Public License for more details. |
| 11 | */ |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/bpf.h> |
| 16 | #include <linux/filter.h> |
| 17 | #include <net/netlink.h> |
| 18 | #include <linux/file.h> |
| 19 | #include <linux/vmalloc.h> |
| 20 | |
| 21 | /* bpf_check() is a static code analyzer that walks eBPF program |
| 22 | * instruction by instruction and updates register/stack state. |
| 23 | * All paths of conditional branches are analyzed until 'bpf_exit' insn. |
| 24 | * |
| 25 | * The first pass is depth-first-search to check that the program is a DAG. |
| 26 | * It rejects the following programs: |
| 27 | * - larger than BPF_MAXINSNS insns |
| 28 | * - if loop is present (detected via back-edge) |
| 29 | * - unreachable insns exist (shouldn't be a forest. program = one function) |
| 30 | * - out of bounds or malformed jumps |
| 31 | * The second pass is all possible path descent from the 1st insn. |
| 32 | * Since it's analyzing all pathes through the program, the length of the |
| 33 | * analysis is limited to 32k insn, which may be hit even if total number of |
| 34 | * insn is less then 4K, but there are too many branches that change stack/regs. |
| 35 | * Number of 'branches to be analyzed' is limited to 1k |
| 36 | * |
| 37 | * On entry to each instruction, each register has a type, and the instruction |
| 38 | * changes the types of the registers depending on instruction semantics. |
| 39 | * If instruction is BPF_MOV64_REG(BPF_REG_1, BPF_REG_5), then type of R5 is |
| 40 | * copied to R1. |
| 41 | * |
| 42 | * All registers are 64-bit. |
| 43 | * R0 - return register |
| 44 | * R1-R5 argument passing registers |
| 45 | * R6-R9 callee saved registers |
| 46 | * R10 - frame pointer read-only |
| 47 | * |
| 48 | * At the start of BPF program the register R1 contains a pointer to bpf_context |
| 49 | * and has type PTR_TO_CTX. |
| 50 | * |
| 51 | * Verifier tracks arithmetic operations on pointers in case: |
| 52 | * BPF_MOV64_REG(BPF_REG_1, BPF_REG_10), |
| 53 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_1, -20), |
| 54 | * 1st insn copies R10 (which has FRAME_PTR) type into R1 |
| 55 | * and 2nd arithmetic instruction is pattern matched to recognize |
| 56 | * that it wants to construct a pointer to some element within stack. |
| 57 | * So after 2nd insn, the register R1 has type PTR_TO_STACK |
| 58 | * (and -20 constant is saved for further stack bounds checking). |
| 59 | * Meaning that this reg is a pointer to stack plus known immediate constant. |
| 60 | * |
| 61 | * Most of the time the registers have UNKNOWN_VALUE type, which |
| 62 | * means the register has some value, but it's not a valid pointer. |
| 63 | * (like pointer plus pointer becomes UNKNOWN_VALUE type) |
| 64 | * |
| 65 | * When verifier sees load or store instructions the type of base register |
| 66 | * can be: PTR_TO_MAP_VALUE, PTR_TO_CTX, FRAME_PTR. These are three pointer |
| 67 | * types recognized by check_mem_access() function. |
| 68 | * |
| 69 | * PTR_TO_MAP_VALUE means that this register is pointing to 'map element value' |
| 70 | * and the range of [ptr, ptr + map's value_size) is accessible. |
| 71 | * |
| 72 | * registers used to pass values to function calls are checked against |
| 73 | * function argument constraints. |
| 74 | * |
| 75 | * ARG_PTR_TO_MAP_KEY is one of such argument constraints. |
| 76 | * It means that the register type passed to this function must be |
| 77 | * PTR_TO_STACK and it will be used inside the function as |
| 78 | * 'pointer to map element key' |
| 79 | * |
| 80 | * For example the argument constraints for bpf_map_lookup_elem(): |
| 81 | * .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, |
| 82 | * .arg1_type = ARG_CONST_MAP_PTR, |
| 83 | * .arg2_type = ARG_PTR_TO_MAP_KEY, |
| 84 | * |
| 85 | * ret_type says that this function returns 'pointer to map elem value or null' |
| 86 | * function expects 1st argument to be a const pointer to 'struct bpf_map' and |
| 87 | * 2nd argument should be a pointer to stack, which will be used inside |
| 88 | * the helper function as a pointer to map element key. |
| 89 | * |
| 90 | * On the kernel side the helper function looks like: |
| 91 | * u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) |
| 92 | * { |
| 93 | * struct bpf_map *map = (struct bpf_map *) (unsigned long) r1; |
| 94 | * void *key = (void *) (unsigned long) r2; |
| 95 | * void *value; |
| 96 | * |
| 97 | * here kernel can access 'key' and 'map' pointers safely, knowing that |
| 98 | * [key, key + map->key_size) bytes are valid and were initialized on |
| 99 | * the stack of eBPF program. |
| 100 | * } |
| 101 | * |
| 102 | * Corresponding eBPF program may look like: |
| 103 | * BPF_MOV64_REG(BPF_REG_2, BPF_REG_10), // after this insn R2 type is FRAME_PTR |
| 104 | * BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, -4), // after this insn R2 type is PTR_TO_STACK |
| 105 | * BPF_LD_MAP_FD(BPF_REG_1, map_fd), // after this insn R1 type is CONST_PTR_TO_MAP |
| 106 | * BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_map_lookup_elem), |
| 107 | * here verifier looks at prototype of map_lookup_elem() and sees: |
| 108 | * .arg1_type == ARG_CONST_MAP_PTR and R1->type == CONST_PTR_TO_MAP, which is ok, |
| 109 | * Now verifier knows that this map has key of R1->map_ptr->key_size bytes |
| 110 | * |
| 111 | * Then .arg2_type == ARG_PTR_TO_MAP_KEY and R2->type == PTR_TO_STACK, ok so far, |
| 112 | * Now verifier checks that [R2, R2 + map's key_size) are within stack limits |
| 113 | * and were initialized prior to this call. |
| 114 | * If it's ok, then verifier allows this BPF_CALL insn and looks at |
| 115 | * .ret_type which is RET_PTR_TO_MAP_VALUE_OR_NULL, so it sets |
| 116 | * R0->type = PTR_TO_MAP_VALUE_OR_NULL which means bpf_map_lookup_elem() function |
| 117 | * returns ether pointer to map value or NULL. |
| 118 | * |
| 119 | * When type PTR_TO_MAP_VALUE_OR_NULL passes through 'if (reg != 0) goto +off' |
| 120 | * insn, the register holding that pointer in the true branch changes state to |
| 121 | * PTR_TO_MAP_VALUE and the same register changes state to CONST_IMM in the false |
| 122 | * branch. See check_cond_jmp_op(). |
| 123 | * |
| 124 | * After the call R0 is set to return type of the function and registers R1-R5 |
| 125 | * are set to NOT_INIT to indicate that they are no longer readable. |
| 126 | */ |
| 127 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 128 | /* types of values stored in eBPF registers */ |
| 129 | enum bpf_reg_type { |
| 130 | NOT_INIT = 0, /* nothing was written into register */ |
| 131 | UNKNOWN_VALUE, /* reg doesn't contain a valid pointer */ |
| 132 | PTR_TO_CTX, /* reg points to bpf_context */ |
| 133 | CONST_PTR_TO_MAP, /* reg points to struct bpf_map */ |
| 134 | PTR_TO_MAP_VALUE, /* reg points to map element value */ |
| 135 | PTR_TO_MAP_VALUE_OR_NULL,/* points to map elem value or NULL */ |
| 136 | FRAME_PTR, /* reg == frame_pointer */ |
| 137 | PTR_TO_STACK, /* reg == frame_pointer + imm */ |
| 138 | CONST_IMM, /* constant integer value */ |
| 139 | }; |
| 140 | |
| 141 | struct reg_state { |
| 142 | enum bpf_reg_type type; |
| 143 | union { |
| 144 | /* valid when type == CONST_IMM | PTR_TO_STACK */ |
| 145 | int imm; |
| 146 | |
| 147 | /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE | |
| 148 | * PTR_TO_MAP_VALUE_OR_NULL |
| 149 | */ |
| 150 | struct bpf_map *map_ptr; |
| 151 | }; |
| 152 | }; |
| 153 | |
| 154 | enum bpf_stack_slot_type { |
| 155 | STACK_INVALID, /* nothing was stored in this stack slot */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 156 | STACK_SPILL, /* register spilled into stack */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 157 | STACK_MISC /* BPF program wrote some data into this slot */ |
| 158 | }; |
| 159 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 160 | #define BPF_REG_SIZE 8 /* size of eBPF register in bytes */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 161 | |
| 162 | /* state of the program: |
| 163 | * type of all registers and stack info |
| 164 | */ |
| 165 | struct verifier_state { |
| 166 | struct reg_state regs[MAX_BPF_REG]; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 167 | u8 stack_slot_type[MAX_BPF_STACK]; |
| 168 | struct reg_state spilled_regs[MAX_BPF_STACK / BPF_REG_SIZE]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /* linked list of verifier states used to prune search */ |
| 172 | struct verifier_state_list { |
| 173 | struct verifier_state state; |
| 174 | struct verifier_state_list *next; |
| 175 | }; |
| 176 | |
| 177 | /* verifier_state + insn_idx are pushed to stack when branch is encountered */ |
| 178 | struct verifier_stack_elem { |
| 179 | /* verifer state is 'st' |
| 180 | * before processing instruction 'insn_idx' |
| 181 | * and after processing instruction 'prev_insn_idx' |
| 182 | */ |
| 183 | struct verifier_state st; |
| 184 | int insn_idx; |
| 185 | int prev_insn_idx; |
| 186 | struct verifier_stack_elem *next; |
| 187 | }; |
| 188 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 189 | #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */ |
| 190 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 191 | /* single container for all structs |
| 192 | * one verifier_env per bpf_check() call |
| 193 | */ |
| 194 | struct verifier_env { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 195 | struct bpf_prog *prog; /* eBPF program being verified */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 196 | struct verifier_stack_elem *head; /* stack of verifier states to be processed */ |
| 197 | int stack_size; /* number of states to be processed */ |
| 198 | struct verifier_state cur_state; /* current verifier state */ |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 199 | struct verifier_state_list **explored_states; /* search pruning optimization */ |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 200 | struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */ |
| 201 | u32 used_map_cnt; /* number of used maps */ |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | /* verbose verifier prints what it's seeing |
| 205 | * bpf_check() is called under lock, so no race to access these global vars |
| 206 | */ |
| 207 | static u32 log_level, log_size, log_len; |
| 208 | static char *log_buf; |
| 209 | |
| 210 | static DEFINE_MUTEX(bpf_verifier_lock); |
| 211 | |
| 212 | /* log_level controls verbosity level of eBPF verifier. |
| 213 | * verbose() is used to dump the verification trace to the log, so the user |
| 214 | * can figure out what's wrong with the program |
| 215 | */ |
| 216 | static void verbose(const char *fmt, ...) |
| 217 | { |
| 218 | va_list args; |
| 219 | |
| 220 | if (log_level == 0 || log_len >= log_size - 1) |
| 221 | return; |
| 222 | |
| 223 | va_start(args, fmt); |
| 224 | log_len += vscnprintf(log_buf + log_len, log_size - log_len, fmt, args); |
| 225 | va_end(args); |
| 226 | } |
| 227 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 228 | /* string representation of 'enum bpf_reg_type' */ |
| 229 | static const char * const reg_type_str[] = { |
| 230 | [NOT_INIT] = "?", |
| 231 | [UNKNOWN_VALUE] = "inv", |
| 232 | [PTR_TO_CTX] = "ctx", |
| 233 | [CONST_PTR_TO_MAP] = "map_ptr", |
| 234 | [PTR_TO_MAP_VALUE] = "map_value", |
| 235 | [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null", |
| 236 | [FRAME_PTR] = "fp", |
| 237 | [PTR_TO_STACK] = "fp", |
| 238 | [CONST_IMM] = "imm", |
| 239 | }; |
| 240 | |
| 241 | static void print_verifier_state(struct verifier_env *env) |
| 242 | { |
| 243 | enum bpf_reg_type t; |
| 244 | int i; |
| 245 | |
| 246 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 247 | t = env->cur_state.regs[i].type; |
| 248 | if (t == NOT_INIT) |
| 249 | continue; |
| 250 | verbose(" R%d=%s", i, reg_type_str[t]); |
| 251 | if (t == CONST_IMM || t == PTR_TO_STACK) |
| 252 | verbose("%d", env->cur_state.regs[i].imm); |
| 253 | else if (t == CONST_PTR_TO_MAP || t == PTR_TO_MAP_VALUE || |
| 254 | t == PTR_TO_MAP_VALUE_OR_NULL) |
| 255 | verbose("(ks=%d,vs=%d)", |
| 256 | env->cur_state.regs[i].map_ptr->key_size, |
| 257 | env->cur_state.regs[i].map_ptr->value_size); |
| 258 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 259 | for (i = 0; i < MAX_BPF_STACK; i += BPF_REG_SIZE) { |
| 260 | if (env->cur_state.stack_slot_type[i] == STACK_SPILL) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 261 | verbose(" fp%d=%s", -MAX_BPF_STACK + i, |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 262 | reg_type_str[env->cur_state.spilled_regs[i / BPF_REG_SIZE].type]); |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 263 | } |
| 264 | verbose("\n"); |
| 265 | } |
| 266 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 267 | static const char *const bpf_class_string[] = { |
| 268 | [BPF_LD] = "ld", |
| 269 | [BPF_LDX] = "ldx", |
| 270 | [BPF_ST] = "st", |
| 271 | [BPF_STX] = "stx", |
| 272 | [BPF_ALU] = "alu", |
| 273 | [BPF_JMP] = "jmp", |
| 274 | [BPF_RET] = "BUG", |
| 275 | [BPF_ALU64] = "alu64", |
| 276 | }; |
| 277 | |
| 278 | static const char *const bpf_alu_string[] = { |
| 279 | [BPF_ADD >> 4] = "+=", |
| 280 | [BPF_SUB >> 4] = "-=", |
| 281 | [BPF_MUL >> 4] = "*=", |
| 282 | [BPF_DIV >> 4] = "/=", |
| 283 | [BPF_OR >> 4] = "|=", |
| 284 | [BPF_AND >> 4] = "&=", |
| 285 | [BPF_LSH >> 4] = "<<=", |
| 286 | [BPF_RSH >> 4] = ">>=", |
| 287 | [BPF_NEG >> 4] = "neg", |
| 288 | [BPF_MOD >> 4] = "%=", |
| 289 | [BPF_XOR >> 4] = "^=", |
| 290 | [BPF_MOV >> 4] = "=", |
| 291 | [BPF_ARSH >> 4] = "s>>=", |
| 292 | [BPF_END >> 4] = "endian", |
| 293 | }; |
| 294 | |
| 295 | static const char *const bpf_ldst_string[] = { |
| 296 | [BPF_W >> 3] = "u32", |
| 297 | [BPF_H >> 3] = "u16", |
| 298 | [BPF_B >> 3] = "u8", |
| 299 | [BPF_DW >> 3] = "u64", |
| 300 | }; |
| 301 | |
| 302 | static const char *const bpf_jmp_string[] = { |
| 303 | [BPF_JA >> 4] = "jmp", |
| 304 | [BPF_JEQ >> 4] = "==", |
| 305 | [BPF_JGT >> 4] = ">", |
| 306 | [BPF_JGE >> 4] = ">=", |
| 307 | [BPF_JSET >> 4] = "&", |
| 308 | [BPF_JNE >> 4] = "!=", |
| 309 | [BPF_JSGT >> 4] = "s>", |
| 310 | [BPF_JSGE >> 4] = "s>=", |
| 311 | [BPF_CALL >> 4] = "call", |
| 312 | [BPF_EXIT >> 4] = "exit", |
| 313 | }; |
| 314 | |
| 315 | static void print_bpf_insn(struct bpf_insn *insn) |
| 316 | { |
| 317 | u8 class = BPF_CLASS(insn->code); |
| 318 | |
| 319 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 320 | if (BPF_SRC(insn->code) == BPF_X) |
| 321 | verbose("(%02x) %sr%d %s %sr%d\n", |
| 322 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 323 | insn->dst_reg, |
| 324 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 325 | class == BPF_ALU ? "(u32) " : "", |
| 326 | insn->src_reg); |
| 327 | else |
| 328 | verbose("(%02x) %sr%d %s %s%d\n", |
| 329 | insn->code, class == BPF_ALU ? "(u32) " : "", |
| 330 | insn->dst_reg, |
| 331 | bpf_alu_string[BPF_OP(insn->code) >> 4], |
| 332 | class == BPF_ALU ? "(u32) " : "", |
| 333 | insn->imm); |
| 334 | } else if (class == BPF_STX) { |
| 335 | if (BPF_MODE(insn->code) == BPF_MEM) |
| 336 | verbose("(%02x) *(%s *)(r%d %+d) = r%d\n", |
| 337 | insn->code, |
| 338 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 339 | insn->dst_reg, |
| 340 | insn->off, insn->src_reg); |
| 341 | else if (BPF_MODE(insn->code) == BPF_XADD) |
| 342 | verbose("(%02x) lock *(%s *)(r%d %+d) += r%d\n", |
| 343 | insn->code, |
| 344 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 345 | insn->dst_reg, insn->off, |
| 346 | insn->src_reg); |
| 347 | else |
| 348 | verbose("BUG_%02x\n", insn->code); |
| 349 | } else if (class == BPF_ST) { |
| 350 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 351 | verbose("BUG_st_%02x\n", insn->code); |
| 352 | return; |
| 353 | } |
| 354 | verbose("(%02x) *(%s *)(r%d %+d) = %d\n", |
| 355 | insn->code, |
| 356 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 357 | insn->dst_reg, |
| 358 | insn->off, insn->imm); |
| 359 | } else if (class == BPF_LDX) { |
| 360 | if (BPF_MODE(insn->code) != BPF_MEM) { |
| 361 | verbose("BUG_ldx_%02x\n", insn->code); |
| 362 | return; |
| 363 | } |
| 364 | verbose("(%02x) r%d = *(%s *)(r%d %+d)\n", |
| 365 | insn->code, insn->dst_reg, |
| 366 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 367 | insn->src_reg, insn->off); |
| 368 | } else if (class == BPF_LD) { |
| 369 | if (BPF_MODE(insn->code) == BPF_ABS) { |
| 370 | verbose("(%02x) r0 = *(%s *)skb[%d]\n", |
| 371 | insn->code, |
| 372 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 373 | insn->imm); |
| 374 | } else if (BPF_MODE(insn->code) == BPF_IND) { |
| 375 | verbose("(%02x) r0 = *(%s *)skb[r%d + %d]\n", |
| 376 | insn->code, |
| 377 | bpf_ldst_string[BPF_SIZE(insn->code) >> 3], |
| 378 | insn->src_reg, insn->imm); |
| 379 | } else if (BPF_MODE(insn->code) == BPF_IMM) { |
| 380 | verbose("(%02x) r%d = 0x%x\n", |
| 381 | insn->code, insn->dst_reg, insn->imm); |
| 382 | } else { |
| 383 | verbose("BUG_ld_%02x\n", insn->code); |
| 384 | return; |
| 385 | } |
| 386 | } else if (class == BPF_JMP) { |
| 387 | u8 opcode = BPF_OP(insn->code); |
| 388 | |
| 389 | if (opcode == BPF_CALL) { |
| 390 | verbose("(%02x) call %d\n", insn->code, insn->imm); |
| 391 | } else if (insn->code == (BPF_JMP | BPF_JA)) { |
| 392 | verbose("(%02x) goto pc%+d\n", |
| 393 | insn->code, insn->off); |
| 394 | } else if (insn->code == (BPF_JMP | BPF_EXIT)) { |
| 395 | verbose("(%02x) exit\n", insn->code); |
| 396 | } else if (BPF_SRC(insn->code) == BPF_X) { |
| 397 | verbose("(%02x) if r%d %s r%d goto pc%+d\n", |
| 398 | insn->code, insn->dst_reg, |
| 399 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 400 | insn->src_reg, insn->off); |
| 401 | } else { |
| 402 | verbose("(%02x) if r%d %s 0x%x goto pc%+d\n", |
| 403 | insn->code, insn->dst_reg, |
| 404 | bpf_jmp_string[BPF_OP(insn->code) >> 4], |
| 405 | insn->imm, insn->off); |
| 406 | } |
| 407 | } else { |
| 408 | verbose("(%02x) %s\n", insn->code, bpf_class_string[class]); |
| 409 | } |
| 410 | } |
| 411 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 412 | static int pop_stack(struct verifier_env *env, int *prev_insn_idx) |
| 413 | { |
| 414 | struct verifier_stack_elem *elem; |
| 415 | int insn_idx; |
| 416 | |
| 417 | if (env->head == NULL) |
| 418 | return -1; |
| 419 | |
| 420 | memcpy(&env->cur_state, &env->head->st, sizeof(env->cur_state)); |
| 421 | insn_idx = env->head->insn_idx; |
| 422 | if (prev_insn_idx) |
| 423 | *prev_insn_idx = env->head->prev_insn_idx; |
| 424 | elem = env->head->next; |
| 425 | kfree(env->head); |
| 426 | env->head = elem; |
| 427 | env->stack_size--; |
| 428 | return insn_idx; |
| 429 | } |
| 430 | |
| 431 | static struct verifier_state *push_stack(struct verifier_env *env, int insn_idx, |
| 432 | int prev_insn_idx) |
| 433 | { |
| 434 | struct verifier_stack_elem *elem; |
| 435 | |
| 436 | elem = kmalloc(sizeof(struct verifier_stack_elem), GFP_KERNEL); |
| 437 | if (!elem) |
| 438 | goto err; |
| 439 | |
| 440 | memcpy(&elem->st, &env->cur_state, sizeof(env->cur_state)); |
| 441 | elem->insn_idx = insn_idx; |
| 442 | elem->prev_insn_idx = prev_insn_idx; |
| 443 | elem->next = env->head; |
| 444 | env->head = elem; |
| 445 | env->stack_size++; |
| 446 | if (env->stack_size > 1024) { |
| 447 | verbose("BPF program is too complex\n"); |
| 448 | goto err; |
| 449 | } |
| 450 | return &elem->st; |
| 451 | err: |
| 452 | /* pop all elements and return */ |
| 453 | while (pop_stack(env, NULL) >= 0); |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | #define CALLER_SAVED_REGS 6 |
| 458 | static const int caller_saved[CALLER_SAVED_REGS] = { |
| 459 | BPF_REG_0, BPF_REG_1, BPF_REG_2, BPF_REG_3, BPF_REG_4, BPF_REG_5 |
| 460 | }; |
| 461 | |
| 462 | static void init_reg_state(struct reg_state *regs) |
| 463 | { |
| 464 | int i; |
| 465 | |
| 466 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 467 | regs[i].type = NOT_INIT; |
| 468 | regs[i].imm = 0; |
| 469 | regs[i].map_ptr = NULL; |
| 470 | } |
| 471 | |
| 472 | /* frame pointer */ |
| 473 | regs[BPF_REG_FP].type = FRAME_PTR; |
| 474 | |
| 475 | /* 1st arg to a function */ |
| 476 | regs[BPF_REG_1].type = PTR_TO_CTX; |
| 477 | } |
| 478 | |
| 479 | static void mark_reg_unknown_value(struct reg_state *regs, u32 regno) |
| 480 | { |
| 481 | BUG_ON(regno >= MAX_BPF_REG); |
| 482 | regs[regno].type = UNKNOWN_VALUE; |
| 483 | regs[regno].imm = 0; |
| 484 | regs[regno].map_ptr = NULL; |
| 485 | } |
| 486 | |
| 487 | enum reg_arg_type { |
| 488 | SRC_OP, /* register is used as source operand */ |
| 489 | DST_OP, /* register is used as destination operand */ |
| 490 | DST_OP_NO_MARK /* same as above, check only, don't mark */ |
| 491 | }; |
| 492 | |
| 493 | static int check_reg_arg(struct reg_state *regs, u32 regno, |
| 494 | enum reg_arg_type t) |
| 495 | { |
| 496 | if (regno >= MAX_BPF_REG) { |
| 497 | verbose("R%d is invalid\n", regno); |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | |
| 501 | if (t == SRC_OP) { |
| 502 | /* check whether register used as source operand can be read */ |
| 503 | if (regs[regno].type == NOT_INIT) { |
| 504 | verbose("R%d !read_ok\n", regno); |
| 505 | return -EACCES; |
| 506 | } |
| 507 | } else { |
| 508 | /* check whether register used as dest operand can be written to */ |
| 509 | if (regno == BPF_REG_FP) { |
| 510 | verbose("frame pointer is read only\n"); |
| 511 | return -EACCES; |
| 512 | } |
| 513 | if (t == DST_OP) |
| 514 | mark_reg_unknown_value(regs, regno); |
| 515 | } |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static int bpf_size_to_bytes(int bpf_size) |
| 520 | { |
| 521 | if (bpf_size == BPF_W) |
| 522 | return 4; |
| 523 | else if (bpf_size == BPF_H) |
| 524 | return 2; |
| 525 | else if (bpf_size == BPF_B) |
| 526 | return 1; |
| 527 | else if (bpf_size == BPF_DW) |
| 528 | return 8; |
| 529 | else |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | |
| 533 | /* check_stack_read/write functions track spill/fill of registers, |
| 534 | * stack boundary and alignment are checked in check_mem_access() |
| 535 | */ |
| 536 | static int check_stack_write(struct verifier_state *state, int off, int size, |
| 537 | int value_regno) |
| 538 | { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 539 | int i; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 540 | /* caller checked that off % size == 0 and -MAX_BPF_STACK <= off < 0, |
| 541 | * so it's aligned access and [off, off + size) are within stack limits |
| 542 | */ |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 543 | |
| 544 | if (value_regno >= 0 && |
| 545 | (state->regs[value_regno].type == PTR_TO_MAP_VALUE || |
| 546 | state->regs[value_regno].type == PTR_TO_STACK || |
| 547 | state->regs[value_regno].type == PTR_TO_CTX)) { |
| 548 | |
| 549 | /* register containing pointer is being spilled into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 550 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 551 | verbose("invalid size of register spill\n"); |
| 552 | return -EACCES; |
| 553 | } |
| 554 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 555 | /* save register state */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 556 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
| 557 | state->regs[value_regno]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 558 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 559 | for (i = 0; i < BPF_REG_SIZE; i++) |
| 560 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_SPILL; |
| 561 | } else { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 562 | /* regular write of data into stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 563 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE] = |
| 564 | (struct reg_state) {}; |
| 565 | |
| 566 | for (i = 0; i < size; i++) |
| 567 | state->stack_slot_type[MAX_BPF_STACK + off + i] = STACK_MISC; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 568 | } |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static int check_stack_read(struct verifier_state *state, int off, int size, |
| 573 | int value_regno) |
| 574 | { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 575 | u8 *slot_type; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 576 | int i; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 577 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 578 | slot_type = &state->stack_slot_type[MAX_BPF_STACK + off]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 579 | |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 580 | if (slot_type[0] == STACK_SPILL) { |
| 581 | if (size != BPF_REG_SIZE) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 582 | verbose("invalid size of register spill\n"); |
| 583 | return -EACCES; |
| 584 | } |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 585 | for (i = 1; i < BPF_REG_SIZE; i++) { |
| 586 | if (slot_type[i] != STACK_SPILL) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 587 | verbose("corrupted spill memory\n"); |
| 588 | return -EACCES; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (value_regno >= 0) |
| 593 | /* restore register state from stack */ |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 594 | state->regs[value_regno] = |
| 595 | state->spilled_regs[(MAX_BPF_STACK + off) / BPF_REG_SIZE]; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 596 | return 0; |
| 597 | } else { |
| 598 | for (i = 0; i < size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 599 | if (slot_type[i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 600 | verbose("invalid read from stack off %d+%d size %d\n", |
| 601 | off, i, size); |
| 602 | return -EACCES; |
| 603 | } |
| 604 | } |
| 605 | if (value_regno >= 0) |
| 606 | /* have read misc data from the stack */ |
| 607 | mark_reg_unknown_value(state->regs, value_regno); |
| 608 | return 0; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /* check read/write into map element returned by bpf_map_lookup_elem() */ |
| 613 | static int check_map_access(struct verifier_env *env, u32 regno, int off, |
| 614 | int size) |
| 615 | { |
| 616 | struct bpf_map *map = env->cur_state.regs[regno].map_ptr; |
| 617 | |
| 618 | if (off < 0 || off + size > map->value_size) { |
| 619 | verbose("invalid access to map value, value_size=%d off=%d size=%d\n", |
| 620 | map->value_size, off, size); |
| 621 | return -EACCES; |
| 622 | } |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* check access to 'struct bpf_context' fields */ |
| 627 | static int check_ctx_access(struct verifier_env *env, int off, int size, |
| 628 | enum bpf_access_type t) |
| 629 | { |
| 630 | if (env->prog->aux->ops->is_valid_access && |
| 631 | env->prog->aux->ops->is_valid_access(off, size, t)) |
| 632 | return 0; |
| 633 | |
| 634 | verbose("invalid bpf_context access off=%d size=%d\n", off, size); |
| 635 | return -EACCES; |
| 636 | } |
| 637 | |
| 638 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 639 | * if t==write, value_regno is a register which value is stored into memory |
| 640 | * if t==read, value_regno is a register which will receive the value from memory |
| 641 | * if t==write && value_regno==-1, some unknown value is stored into memory |
| 642 | * if t==read && value_regno==-1, don't care what we read from memory |
| 643 | */ |
| 644 | static int check_mem_access(struct verifier_env *env, u32 regno, int off, |
| 645 | int bpf_size, enum bpf_access_type t, |
| 646 | int value_regno) |
| 647 | { |
| 648 | struct verifier_state *state = &env->cur_state; |
| 649 | int size, err = 0; |
| 650 | |
| 651 | size = bpf_size_to_bytes(bpf_size); |
| 652 | if (size < 0) |
| 653 | return size; |
| 654 | |
| 655 | if (off % size != 0) { |
| 656 | verbose("misaligned access off %d size %d\n", off, size); |
| 657 | return -EACCES; |
| 658 | } |
| 659 | |
| 660 | if (state->regs[regno].type == PTR_TO_MAP_VALUE) { |
| 661 | err = check_map_access(env, regno, off, size); |
| 662 | if (!err && t == BPF_READ && value_regno >= 0) |
| 663 | mark_reg_unknown_value(state->regs, value_regno); |
| 664 | |
| 665 | } else if (state->regs[regno].type == PTR_TO_CTX) { |
| 666 | err = check_ctx_access(env, off, size, t); |
| 667 | if (!err && t == BPF_READ && value_regno >= 0) |
| 668 | mark_reg_unknown_value(state->regs, value_regno); |
| 669 | |
| 670 | } else if (state->regs[regno].type == FRAME_PTR) { |
| 671 | if (off >= 0 || off < -MAX_BPF_STACK) { |
| 672 | verbose("invalid stack off=%d size=%d\n", off, size); |
| 673 | return -EACCES; |
| 674 | } |
| 675 | if (t == BPF_WRITE) |
| 676 | err = check_stack_write(state, off, size, value_regno); |
| 677 | else |
| 678 | err = check_stack_read(state, off, size, value_regno); |
| 679 | } else { |
| 680 | verbose("R%d invalid mem access '%s'\n", |
| 681 | regno, reg_type_str[state->regs[regno].type]); |
| 682 | return -EACCES; |
| 683 | } |
| 684 | return err; |
| 685 | } |
| 686 | |
| 687 | static int check_xadd(struct verifier_env *env, struct bpf_insn *insn) |
| 688 | { |
| 689 | struct reg_state *regs = env->cur_state.regs; |
| 690 | int err; |
| 691 | |
| 692 | if ((BPF_SIZE(insn->code) != BPF_W && BPF_SIZE(insn->code) != BPF_DW) || |
| 693 | insn->imm != 0) { |
| 694 | verbose("BPF_XADD uses reserved fields\n"); |
| 695 | return -EINVAL; |
| 696 | } |
| 697 | |
| 698 | /* check src1 operand */ |
| 699 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 700 | if (err) |
| 701 | return err; |
| 702 | |
| 703 | /* check src2 operand */ |
| 704 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 705 | if (err) |
| 706 | return err; |
| 707 | |
| 708 | /* check whether atomic_add can read the memory */ |
| 709 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 710 | BPF_SIZE(insn->code), BPF_READ, -1); |
| 711 | if (err) |
| 712 | return err; |
| 713 | |
| 714 | /* check whether atomic_add can write into the same memory */ |
| 715 | return check_mem_access(env, insn->dst_reg, insn->off, |
| 716 | BPF_SIZE(insn->code), BPF_WRITE, -1); |
| 717 | } |
| 718 | |
| 719 | /* when register 'regno' is passed into function that will read 'access_size' |
| 720 | * bytes from that pointer, make sure that it's within stack boundary |
| 721 | * and all elements of stack are initialized |
| 722 | */ |
| 723 | static int check_stack_boundary(struct verifier_env *env, |
| 724 | int regno, int access_size) |
| 725 | { |
| 726 | struct verifier_state *state = &env->cur_state; |
| 727 | struct reg_state *regs = state->regs; |
| 728 | int off, i; |
| 729 | |
| 730 | if (regs[regno].type != PTR_TO_STACK) |
| 731 | return -EACCES; |
| 732 | |
| 733 | off = regs[regno].imm; |
| 734 | if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 735 | access_size <= 0) { |
| 736 | verbose("invalid stack type R%d off=%d access_size=%d\n", |
| 737 | regno, off, access_size); |
| 738 | return -EACCES; |
| 739 | } |
| 740 | |
| 741 | for (i = 0; i < access_size; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 742 | if (state->stack_slot_type[MAX_BPF_STACK + off + i] != STACK_MISC) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 743 | verbose("invalid indirect read from stack off %d+%d size %d\n", |
| 744 | off, i, access_size); |
| 745 | return -EACCES; |
| 746 | } |
| 747 | } |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static int check_func_arg(struct verifier_env *env, u32 regno, |
| 752 | enum bpf_arg_type arg_type, struct bpf_map **mapp) |
| 753 | { |
| 754 | struct reg_state *reg = env->cur_state.regs + regno; |
| 755 | enum bpf_reg_type expected_type; |
| 756 | int err = 0; |
| 757 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 758 | if (arg_type == ARG_DONTCARE) |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 759 | return 0; |
| 760 | |
| 761 | if (reg->type == NOT_INIT) { |
| 762 | verbose("R%d !read_ok\n", regno); |
| 763 | return -EACCES; |
| 764 | } |
| 765 | |
Daniel Borkmann | 80f1d68 | 2015-03-12 17:21:42 +0100 | [diff] [blame] | 766 | if (arg_type == ARG_ANYTHING) |
| 767 | return 0; |
| 768 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 769 | if (arg_type == ARG_PTR_TO_STACK || arg_type == ARG_PTR_TO_MAP_KEY || |
| 770 | arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 771 | expected_type = PTR_TO_STACK; |
| 772 | } else if (arg_type == ARG_CONST_STACK_SIZE) { |
| 773 | expected_type = CONST_IMM; |
| 774 | } else if (arg_type == ARG_CONST_MAP_PTR) { |
| 775 | expected_type = CONST_PTR_TO_MAP; |
Alexei Starovoitov | 608cd71 | 2015-03-26 19:53:57 -0700 | [diff] [blame] | 776 | } else if (arg_type == ARG_PTR_TO_CTX) { |
| 777 | expected_type = PTR_TO_CTX; |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 778 | } else { |
| 779 | verbose("unsupported arg_type %d\n", arg_type); |
| 780 | return -EFAULT; |
| 781 | } |
| 782 | |
| 783 | if (reg->type != expected_type) { |
| 784 | verbose("R%d type=%s expected=%s\n", regno, |
| 785 | reg_type_str[reg->type], reg_type_str[expected_type]); |
| 786 | return -EACCES; |
| 787 | } |
| 788 | |
| 789 | if (arg_type == ARG_CONST_MAP_PTR) { |
| 790 | /* bpf_map_xxx(map_ptr) call: remember that map_ptr */ |
| 791 | *mapp = reg->map_ptr; |
| 792 | |
| 793 | } else if (arg_type == ARG_PTR_TO_MAP_KEY) { |
| 794 | /* bpf_map_xxx(..., map_ptr, ..., key) call: |
| 795 | * check that [key, key + map->key_size) are within |
| 796 | * stack limits and initialized |
| 797 | */ |
| 798 | if (!*mapp) { |
| 799 | /* in function declaration map_ptr must come before |
| 800 | * map_key, so that it's verified and known before |
| 801 | * we have to check map_key here. Otherwise it means |
| 802 | * that kernel subsystem misconfigured verifier |
| 803 | */ |
| 804 | verbose("invalid map_ptr to access map->key\n"); |
| 805 | return -EACCES; |
| 806 | } |
| 807 | err = check_stack_boundary(env, regno, (*mapp)->key_size); |
| 808 | |
| 809 | } else if (arg_type == ARG_PTR_TO_MAP_VALUE) { |
| 810 | /* bpf_map_xxx(..., map_ptr, ..., value) call: |
| 811 | * check [value, value + map->value_size) validity |
| 812 | */ |
| 813 | if (!*mapp) { |
| 814 | /* kernel subsystem misconfigured verifier */ |
| 815 | verbose("invalid map_ptr to access map->value\n"); |
| 816 | return -EACCES; |
| 817 | } |
| 818 | err = check_stack_boundary(env, regno, (*mapp)->value_size); |
| 819 | |
| 820 | } else if (arg_type == ARG_CONST_STACK_SIZE) { |
| 821 | /* bpf_xxx(..., buf, len) call will access 'len' bytes |
| 822 | * from stack pointer 'buf'. Check it |
| 823 | * note: regno == len, regno - 1 == buf |
| 824 | */ |
| 825 | if (regno == 0) { |
| 826 | /* kernel subsystem misconfigured verifier */ |
| 827 | verbose("ARG_CONST_STACK_SIZE cannot be first argument\n"); |
| 828 | return -EACCES; |
| 829 | } |
| 830 | err = check_stack_boundary(env, regno - 1, reg->imm); |
| 831 | } |
| 832 | |
| 833 | return err; |
| 834 | } |
| 835 | |
| 836 | static int check_call(struct verifier_env *env, int func_id) |
| 837 | { |
| 838 | struct verifier_state *state = &env->cur_state; |
| 839 | const struct bpf_func_proto *fn = NULL; |
| 840 | struct reg_state *regs = state->regs; |
| 841 | struct bpf_map *map = NULL; |
| 842 | struct reg_state *reg; |
| 843 | int i, err; |
| 844 | |
| 845 | /* find function prototype */ |
| 846 | if (func_id < 0 || func_id >= __BPF_FUNC_MAX_ID) { |
| 847 | verbose("invalid func %d\n", func_id); |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | |
| 851 | if (env->prog->aux->ops->get_func_proto) |
| 852 | fn = env->prog->aux->ops->get_func_proto(func_id); |
| 853 | |
| 854 | if (!fn) { |
| 855 | verbose("unknown func %d\n", func_id); |
| 856 | return -EINVAL; |
| 857 | } |
| 858 | |
| 859 | /* eBPF programs must be GPL compatible to use GPL-ed functions */ |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 860 | if (!env->prog->gpl_compatible && fn->gpl_only) { |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 861 | verbose("cannot call GPL only function from proprietary program\n"); |
| 862 | return -EINVAL; |
| 863 | } |
| 864 | |
| 865 | /* check args */ |
| 866 | err = check_func_arg(env, BPF_REG_1, fn->arg1_type, &map); |
| 867 | if (err) |
| 868 | return err; |
| 869 | err = check_func_arg(env, BPF_REG_2, fn->arg2_type, &map); |
| 870 | if (err) |
| 871 | return err; |
| 872 | err = check_func_arg(env, BPF_REG_3, fn->arg3_type, &map); |
| 873 | if (err) |
| 874 | return err; |
| 875 | err = check_func_arg(env, BPF_REG_4, fn->arg4_type, &map); |
| 876 | if (err) |
| 877 | return err; |
| 878 | err = check_func_arg(env, BPF_REG_5, fn->arg5_type, &map); |
| 879 | if (err) |
| 880 | return err; |
| 881 | |
| 882 | /* reset caller saved regs */ |
| 883 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 884 | reg = regs + caller_saved[i]; |
| 885 | reg->type = NOT_INIT; |
| 886 | reg->imm = 0; |
| 887 | } |
| 888 | |
| 889 | /* update return register */ |
| 890 | if (fn->ret_type == RET_INTEGER) { |
| 891 | regs[BPF_REG_0].type = UNKNOWN_VALUE; |
| 892 | } else if (fn->ret_type == RET_VOID) { |
| 893 | regs[BPF_REG_0].type = NOT_INIT; |
| 894 | } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL) { |
| 895 | regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL; |
| 896 | /* remember map_ptr, so that check_map_access() |
| 897 | * can check 'value_size' boundary of memory access |
| 898 | * to map element returned from bpf_map_lookup_elem() |
| 899 | */ |
| 900 | if (map == NULL) { |
| 901 | verbose("kernel subsystem misconfigured verifier\n"); |
| 902 | return -EINVAL; |
| 903 | } |
| 904 | regs[BPF_REG_0].map_ptr = map; |
| 905 | } else { |
| 906 | verbose("unknown return type %d of func %d\n", |
| 907 | fn->ret_type, func_id); |
| 908 | return -EINVAL; |
| 909 | } |
| 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | /* check validity of 32-bit and 64-bit arithmetic operations */ |
| 914 | static int check_alu_op(struct reg_state *regs, struct bpf_insn *insn) |
| 915 | { |
| 916 | u8 opcode = BPF_OP(insn->code); |
| 917 | int err; |
| 918 | |
| 919 | if (opcode == BPF_END || opcode == BPF_NEG) { |
| 920 | if (opcode == BPF_NEG) { |
| 921 | if (BPF_SRC(insn->code) != 0 || |
| 922 | insn->src_reg != BPF_REG_0 || |
| 923 | insn->off != 0 || insn->imm != 0) { |
| 924 | verbose("BPF_NEG uses reserved fields\n"); |
| 925 | return -EINVAL; |
| 926 | } |
| 927 | } else { |
| 928 | if (insn->src_reg != BPF_REG_0 || insn->off != 0 || |
| 929 | (insn->imm != 16 && insn->imm != 32 && insn->imm != 64)) { |
| 930 | verbose("BPF_END uses reserved fields\n"); |
| 931 | return -EINVAL; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | /* check src operand */ |
| 936 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 937 | if (err) |
| 938 | return err; |
| 939 | |
| 940 | /* check dest operand */ |
| 941 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 942 | if (err) |
| 943 | return err; |
| 944 | |
| 945 | } else if (opcode == BPF_MOV) { |
| 946 | |
| 947 | if (BPF_SRC(insn->code) == BPF_X) { |
| 948 | if (insn->imm != 0 || insn->off != 0) { |
| 949 | verbose("BPF_MOV uses reserved fields\n"); |
| 950 | return -EINVAL; |
| 951 | } |
| 952 | |
| 953 | /* check src operand */ |
| 954 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 955 | if (err) |
| 956 | return err; |
| 957 | } else { |
| 958 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 959 | verbose("BPF_MOV uses reserved fields\n"); |
| 960 | return -EINVAL; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | /* check dest operand */ |
| 965 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 966 | if (err) |
| 967 | return err; |
| 968 | |
| 969 | if (BPF_SRC(insn->code) == BPF_X) { |
| 970 | if (BPF_CLASS(insn->code) == BPF_ALU64) { |
| 971 | /* case: R1 = R2 |
| 972 | * copy register state to dest reg |
| 973 | */ |
| 974 | regs[insn->dst_reg] = regs[insn->src_reg]; |
| 975 | } else { |
| 976 | regs[insn->dst_reg].type = UNKNOWN_VALUE; |
| 977 | regs[insn->dst_reg].map_ptr = NULL; |
| 978 | } |
| 979 | } else { |
| 980 | /* case: R = imm |
| 981 | * remember the value we stored into this reg |
| 982 | */ |
| 983 | regs[insn->dst_reg].type = CONST_IMM; |
| 984 | regs[insn->dst_reg].imm = insn->imm; |
| 985 | } |
| 986 | |
| 987 | } else if (opcode > BPF_END) { |
| 988 | verbose("invalid BPF_ALU opcode %x\n", opcode); |
| 989 | return -EINVAL; |
| 990 | |
| 991 | } else { /* all other ALU ops: and, sub, xor, add, ... */ |
| 992 | |
| 993 | bool stack_relative = false; |
| 994 | |
| 995 | if (BPF_SRC(insn->code) == BPF_X) { |
| 996 | if (insn->imm != 0 || insn->off != 0) { |
| 997 | verbose("BPF_ALU uses reserved fields\n"); |
| 998 | return -EINVAL; |
| 999 | } |
| 1000 | /* check src1 operand */ |
| 1001 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1002 | if (err) |
| 1003 | return err; |
| 1004 | } else { |
| 1005 | if (insn->src_reg != BPF_REG_0 || insn->off != 0) { |
| 1006 | verbose("BPF_ALU uses reserved fields\n"); |
| 1007 | return -EINVAL; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | /* check src2 operand */ |
| 1012 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1013 | if (err) |
| 1014 | return err; |
| 1015 | |
| 1016 | if ((opcode == BPF_MOD || opcode == BPF_DIV) && |
| 1017 | BPF_SRC(insn->code) == BPF_K && insn->imm == 0) { |
| 1018 | verbose("div by zero\n"); |
| 1019 | return -EINVAL; |
| 1020 | } |
| 1021 | |
| 1022 | /* pattern match 'bpf_add Rx, imm' instruction */ |
| 1023 | if (opcode == BPF_ADD && BPF_CLASS(insn->code) == BPF_ALU64 && |
| 1024 | regs[insn->dst_reg].type == FRAME_PTR && |
| 1025 | BPF_SRC(insn->code) == BPF_K) |
| 1026 | stack_relative = true; |
| 1027 | |
| 1028 | /* check dest operand */ |
| 1029 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 1030 | if (err) |
| 1031 | return err; |
| 1032 | |
| 1033 | if (stack_relative) { |
| 1034 | regs[insn->dst_reg].type = PTR_TO_STACK; |
| 1035 | regs[insn->dst_reg].imm = insn->imm; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | static int check_cond_jmp_op(struct verifier_env *env, |
| 1043 | struct bpf_insn *insn, int *insn_idx) |
| 1044 | { |
| 1045 | struct reg_state *regs = env->cur_state.regs; |
| 1046 | struct verifier_state *other_branch; |
| 1047 | u8 opcode = BPF_OP(insn->code); |
| 1048 | int err; |
| 1049 | |
| 1050 | if (opcode > BPF_EXIT) { |
| 1051 | verbose("invalid BPF_JMP opcode %x\n", opcode); |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | |
| 1055 | if (BPF_SRC(insn->code) == BPF_X) { |
| 1056 | if (insn->imm != 0) { |
| 1057 | verbose("BPF_JMP uses reserved fields\n"); |
| 1058 | return -EINVAL; |
| 1059 | } |
| 1060 | |
| 1061 | /* check src1 operand */ |
| 1062 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1063 | if (err) |
| 1064 | return err; |
| 1065 | } else { |
| 1066 | if (insn->src_reg != BPF_REG_0) { |
| 1067 | verbose("BPF_JMP uses reserved fields\n"); |
| 1068 | return -EINVAL; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | /* check src2 operand */ |
| 1073 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1074 | if (err) |
| 1075 | return err; |
| 1076 | |
| 1077 | /* detect if R == 0 where R was initialized to zero earlier */ |
| 1078 | if (BPF_SRC(insn->code) == BPF_K && |
| 1079 | (opcode == BPF_JEQ || opcode == BPF_JNE) && |
| 1080 | regs[insn->dst_reg].type == CONST_IMM && |
| 1081 | regs[insn->dst_reg].imm == insn->imm) { |
| 1082 | if (opcode == BPF_JEQ) { |
| 1083 | /* if (imm == imm) goto pc+off; |
| 1084 | * only follow the goto, ignore fall-through |
| 1085 | */ |
| 1086 | *insn_idx += insn->off; |
| 1087 | return 0; |
| 1088 | } else { |
| 1089 | /* if (imm != imm) goto pc+off; |
| 1090 | * only follow fall-through branch, since |
| 1091 | * that's where the program will go |
| 1092 | */ |
| 1093 | return 0; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | other_branch = push_stack(env, *insn_idx + insn->off + 1, *insn_idx); |
| 1098 | if (!other_branch) |
| 1099 | return -EFAULT; |
| 1100 | |
| 1101 | /* detect if R == 0 where R is returned value from bpf_map_lookup_elem() */ |
| 1102 | if (BPF_SRC(insn->code) == BPF_K && |
| 1103 | insn->imm == 0 && (opcode == BPF_JEQ || |
| 1104 | opcode == BPF_JNE) && |
| 1105 | regs[insn->dst_reg].type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 1106 | if (opcode == BPF_JEQ) { |
| 1107 | /* next fallthrough insn can access memory via |
| 1108 | * this register |
| 1109 | */ |
| 1110 | regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
| 1111 | /* branch targer cannot access it, since reg == 0 */ |
| 1112 | other_branch->regs[insn->dst_reg].type = CONST_IMM; |
| 1113 | other_branch->regs[insn->dst_reg].imm = 0; |
| 1114 | } else { |
| 1115 | other_branch->regs[insn->dst_reg].type = PTR_TO_MAP_VALUE; |
| 1116 | regs[insn->dst_reg].type = CONST_IMM; |
| 1117 | regs[insn->dst_reg].imm = 0; |
| 1118 | } |
| 1119 | } else if (BPF_SRC(insn->code) == BPF_K && |
| 1120 | (opcode == BPF_JEQ || opcode == BPF_JNE)) { |
| 1121 | |
| 1122 | if (opcode == BPF_JEQ) { |
| 1123 | /* detect if (R == imm) goto |
| 1124 | * and in the target state recognize that R = imm |
| 1125 | */ |
| 1126 | other_branch->regs[insn->dst_reg].type = CONST_IMM; |
| 1127 | other_branch->regs[insn->dst_reg].imm = insn->imm; |
| 1128 | } else { |
| 1129 | /* detect if (R != imm) goto |
| 1130 | * and in the fall-through state recognize that R = imm |
| 1131 | */ |
| 1132 | regs[insn->dst_reg].type = CONST_IMM; |
| 1133 | regs[insn->dst_reg].imm = insn->imm; |
| 1134 | } |
| 1135 | } |
| 1136 | if (log_level) |
| 1137 | print_verifier_state(env); |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 1141 | /* return the map pointer stored inside BPF_LD_IMM64 instruction */ |
| 1142 | static struct bpf_map *ld_imm64_to_map_ptr(struct bpf_insn *insn) |
| 1143 | { |
| 1144 | u64 imm64 = ((u64) (u32) insn[0].imm) | ((u64) (u32) insn[1].imm) << 32; |
| 1145 | |
| 1146 | return (struct bpf_map *) (unsigned long) imm64; |
| 1147 | } |
| 1148 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1149 | /* verify BPF_LD_IMM64 instruction */ |
| 1150 | static int check_ld_imm(struct verifier_env *env, struct bpf_insn *insn) |
| 1151 | { |
| 1152 | struct reg_state *regs = env->cur_state.regs; |
| 1153 | int err; |
| 1154 | |
| 1155 | if (BPF_SIZE(insn->code) != BPF_DW) { |
| 1156 | verbose("invalid BPF_LD_IMM insn\n"); |
| 1157 | return -EINVAL; |
| 1158 | } |
| 1159 | if (insn->off != 0) { |
| 1160 | verbose("BPF_LD_IMM64 uses reserved fields\n"); |
| 1161 | return -EINVAL; |
| 1162 | } |
| 1163 | |
| 1164 | err = check_reg_arg(regs, insn->dst_reg, DST_OP); |
| 1165 | if (err) |
| 1166 | return err; |
| 1167 | |
| 1168 | if (insn->src_reg == 0) |
| 1169 | /* generic move 64-bit immediate into a register */ |
| 1170 | return 0; |
| 1171 | |
| 1172 | /* replace_map_fd_with_map_ptr() should have caught bad ld_imm64 */ |
| 1173 | BUG_ON(insn->src_reg != BPF_PSEUDO_MAP_FD); |
| 1174 | |
| 1175 | regs[insn->dst_reg].type = CONST_PTR_TO_MAP; |
| 1176 | regs[insn->dst_reg].map_ptr = ld_imm64_to_map_ptr(insn); |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 1180 | static bool may_access_skb(enum bpf_prog_type type) |
| 1181 | { |
| 1182 | switch (type) { |
| 1183 | case BPF_PROG_TYPE_SOCKET_FILTER: |
| 1184 | case BPF_PROG_TYPE_SCHED_CLS: |
Daniel Borkmann | 94caee8c | 2015-03-20 15:11:11 +0100 | [diff] [blame] | 1185 | case BPF_PROG_TYPE_SCHED_ACT: |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 1186 | return true; |
| 1187 | default: |
| 1188 | return false; |
| 1189 | } |
| 1190 | } |
| 1191 | |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 1192 | /* verify safety of LD_ABS|LD_IND instructions: |
| 1193 | * - they can only appear in the programs where ctx == skb |
| 1194 | * - since they are wrappers of function calls, they scratch R1-R5 registers, |
| 1195 | * preserve R6-R9, and store return value into R0 |
| 1196 | * |
| 1197 | * Implicit input: |
| 1198 | * ctx == skb == R6 == CTX |
| 1199 | * |
| 1200 | * Explicit input: |
| 1201 | * SRC == any register |
| 1202 | * IMM == 32-bit immediate |
| 1203 | * |
| 1204 | * Output: |
| 1205 | * R0 - 8/16/32-bit skb data converted to cpu endianness |
| 1206 | */ |
| 1207 | static int check_ld_abs(struct verifier_env *env, struct bpf_insn *insn) |
| 1208 | { |
| 1209 | struct reg_state *regs = env->cur_state.regs; |
| 1210 | u8 mode = BPF_MODE(insn->code); |
| 1211 | struct reg_state *reg; |
| 1212 | int i, err; |
| 1213 | |
Daniel Borkmann | 24701ec | 2015-03-01 12:31:47 +0100 | [diff] [blame] | 1214 | if (!may_access_skb(env->prog->type)) { |
Daniel Borkmann | 96be432 | 2015-03-01 12:31:46 +0100 | [diff] [blame] | 1215 | 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] | 1216 | return -EINVAL; |
| 1217 | } |
| 1218 | |
| 1219 | if (insn->dst_reg != BPF_REG_0 || insn->off != 0 || |
| 1220 | (mode == BPF_ABS && insn->src_reg != BPF_REG_0)) { |
| 1221 | verbose("BPF_LD_ABS uses reserved fields\n"); |
| 1222 | return -EINVAL; |
| 1223 | } |
| 1224 | |
| 1225 | /* check whether implicit source operand (register R6) is readable */ |
| 1226 | err = check_reg_arg(regs, BPF_REG_6, SRC_OP); |
| 1227 | if (err) |
| 1228 | return err; |
| 1229 | |
| 1230 | if (regs[BPF_REG_6].type != PTR_TO_CTX) { |
| 1231 | verbose("at the time of BPF_LD_ABS|IND R6 != pointer to skb\n"); |
| 1232 | return -EINVAL; |
| 1233 | } |
| 1234 | |
| 1235 | if (mode == BPF_IND) { |
| 1236 | /* check explicit source operand */ |
| 1237 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1238 | if (err) |
| 1239 | return err; |
| 1240 | } |
| 1241 | |
| 1242 | /* reset caller saved regs to unreadable */ |
| 1243 | for (i = 0; i < CALLER_SAVED_REGS; i++) { |
| 1244 | reg = regs + caller_saved[i]; |
| 1245 | reg->type = NOT_INIT; |
| 1246 | reg->imm = 0; |
| 1247 | } |
| 1248 | |
| 1249 | /* mark destination R0 register as readable, since it contains |
| 1250 | * the value fetched from the packet |
| 1251 | */ |
| 1252 | regs[BPF_REG_0].type = UNKNOWN_VALUE; |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 1256 | /* non-recursive DFS pseudo code |
| 1257 | * 1 procedure DFS-iterative(G,v): |
| 1258 | * 2 label v as discovered |
| 1259 | * 3 let S be a stack |
| 1260 | * 4 S.push(v) |
| 1261 | * 5 while S is not empty |
| 1262 | * 6 t <- S.pop() |
| 1263 | * 7 if t is what we're looking for: |
| 1264 | * 8 return t |
| 1265 | * 9 for all edges e in G.adjacentEdges(t) do |
| 1266 | * 10 if edge e is already labelled |
| 1267 | * 11 continue with the next edge |
| 1268 | * 12 w <- G.adjacentVertex(t,e) |
| 1269 | * 13 if vertex w is not discovered and not explored |
| 1270 | * 14 label e as tree-edge |
| 1271 | * 15 label w as discovered |
| 1272 | * 16 S.push(w) |
| 1273 | * 17 continue at 5 |
| 1274 | * 18 else if vertex w is discovered |
| 1275 | * 19 label e as back-edge |
| 1276 | * 20 else |
| 1277 | * 21 // vertex w is explored |
| 1278 | * 22 label e as forward- or cross-edge |
| 1279 | * 23 label t as explored |
| 1280 | * 24 S.pop() |
| 1281 | * |
| 1282 | * convention: |
| 1283 | * 0x10 - discovered |
| 1284 | * 0x11 - discovered and fall-through edge labelled |
| 1285 | * 0x12 - discovered and fall-through and branch edges labelled |
| 1286 | * 0x20 - explored |
| 1287 | */ |
| 1288 | |
| 1289 | enum { |
| 1290 | DISCOVERED = 0x10, |
| 1291 | EXPLORED = 0x20, |
| 1292 | FALLTHROUGH = 1, |
| 1293 | BRANCH = 2, |
| 1294 | }; |
| 1295 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1296 | #define STATE_LIST_MARK ((struct verifier_state_list *) -1L) |
| 1297 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 1298 | static int *insn_stack; /* stack of insns to process */ |
| 1299 | static int cur_stack; /* current stack index */ |
| 1300 | static int *insn_state; |
| 1301 | |
| 1302 | /* t, w, e - match pseudo-code above: |
| 1303 | * t - index of current instruction |
| 1304 | * w - next instruction |
| 1305 | * e - edge |
| 1306 | */ |
| 1307 | static int push_insn(int t, int w, int e, struct verifier_env *env) |
| 1308 | { |
| 1309 | if (e == FALLTHROUGH && insn_state[t] >= (DISCOVERED | FALLTHROUGH)) |
| 1310 | return 0; |
| 1311 | |
| 1312 | if (e == BRANCH && insn_state[t] >= (DISCOVERED | BRANCH)) |
| 1313 | return 0; |
| 1314 | |
| 1315 | if (w < 0 || w >= env->prog->len) { |
| 1316 | verbose("jump out of range from insn %d to %d\n", t, w); |
| 1317 | return -EINVAL; |
| 1318 | } |
| 1319 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1320 | if (e == BRANCH) |
| 1321 | /* mark branch target for state pruning */ |
| 1322 | env->explored_states[w] = STATE_LIST_MARK; |
| 1323 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 1324 | if (insn_state[w] == 0) { |
| 1325 | /* tree-edge */ |
| 1326 | insn_state[t] = DISCOVERED | e; |
| 1327 | insn_state[w] = DISCOVERED; |
| 1328 | if (cur_stack >= env->prog->len) |
| 1329 | return -E2BIG; |
| 1330 | insn_stack[cur_stack++] = w; |
| 1331 | return 1; |
| 1332 | } else if ((insn_state[w] & 0xF0) == DISCOVERED) { |
| 1333 | verbose("back-edge from insn %d to %d\n", t, w); |
| 1334 | return -EINVAL; |
| 1335 | } else if (insn_state[w] == EXPLORED) { |
| 1336 | /* forward- or cross-edge */ |
| 1337 | insn_state[t] = DISCOVERED | e; |
| 1338 | } else { |
| 1339 | verbose("insn state internal bug\n"); |
| 1340 | return -EFAULT; |
| 1341 | } |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
| 1345 | /* non-recursive depth-first-search to detect loops in BPF program |
| 1346 | * loop == back-edge in directed graph |
| 1347 | */ |
| 1348 | static int check_cfg(struct verifier_env *env) |
| 1349 | { |
| 1350 | struct bpf_insn *insns = env->prog->insnsi; |
| 1351 | int insn_cnt = env->prog->len; |
| 1352 | int ret = 0; |
| 1353 | int i, t; |
| 1354 | |
| 1355 | insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 1356 | if (!insn_state) |
| 1357 | return -ENOMEM; |
| 1358 | |
| 1359 | insn_stack = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL); |
| 1360 | if (!insn_stack) { |
| 1361 | kfree(insn_state); |
| 1362 | return -ENOMEM; |
| 1363 | } |
| 1364 | |
| 1365 | insn_state[0] = DISCOVERED; /* mark 1st insn as discovered */ |
| 1366 | insn_stack[0] = 0; /* 0 is the first instruction */ |
| 1367 | cur_stack = 1; |
| 1368 | |
| 1369 | peek_stack: |
| 1370 | if (cur_stack == 0) |
| 1371 | goto check_state; |
| 1372 | t = insn_stack[cur_stack - 1]; |
| 1373 | |
| 1374 | if (BPF_CLASS(insns[t].code) == BPF_JMP) { |
| 1375 | u8 opcode = BPF_OP(insns[t].code); |
| 1376 | |
| 1377 | if (opcode == BPF_EXIT) { |
| 1378 | goto mark_explored; |
| 1379 | } else if (opcode == BPF_CALL) { |
| 1380 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 1381 | if (ret == 1) |
| 1382 | goto peek_stack; |
| 1383 | else if (ret < 0) |
| 1384 | goto err_free; |
| 1385 | } else if (opcode == BPF_JA) { |
| 1386 | if (BPF_SRC(insns[t].code) != BPF_K) { |
| 1387 | ret = -EINVAL; |
| 1388 | goto err_free; |
| 1389 | } |
| 1390 | /* unconditional jump with single edge */ |
| 1391 | ret = push_insn(t, t + insns[t].off + 1, |
| 1392 | FALLTHROUGH, env); |
| 1393 | if (ret == 1) |
| 1394 | goto peek_stack; |
| 1395 | else if (ret < 0) |
| 1396 | goto err_free; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1397 | /* tell verifier to check for equivalent states |
| 1398 | * after every call and jump |
| 1399 | */ |
Alexei Starovoitov | c3de631 | 2015-04-14 15:57:13 -0700 | [diff] [blame^] | 1400 | if (t + 1 < insn_cnt) |
| 1401 | env->explored_states[t + 1] = STATE_LIST_MARK; |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 1402 | } else { |
| 1403 | /* conditional jump with two edges */ |
| 1404 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 1405 | if (ret == 1) |
| 1406 | goto peek_stack; |
| 1407 | else if (ret < 0) |
| 1408 | goto err_free; |
| 1409 | |
| 1410 | ret = push_insn(t, t + insns[t].off + 1, BRANCH, env); |
| 1411 | if (ret == 1) |
| 1412 | goto peek_stack; |
| 1413 | else if (ret < 0) |
| 1414 | goto err_free; |
| 1415 | } |
| 1416 | } else { |
| 1417 | /* all other non-branch instructions with single |
| 1418 | * fall-through edge |
| 1419 | */ |
| 1420 | ret = push_insn(t, t + 1, FALLTHROUGH, env); |
| 1421 | if (ret == 1) |
| 1422 | goto peek_stack; |
| 1423 | else if (ret < 0) |
| 1424 | goto err_free; |
| 1425 | } |
| 1426 | |
| 1427 | mark_explored: |
| 1428 | insn_state[t] = EXPLORED; |
| 1429 | if (cur_stack-- <= 0) { |
| 1430 | verbose("pop stack internal bug\n"); |
| 1431 | ret = -EFAULT; |
| 1432 | goto err_free; |
| 1433 | } |
| 1434 | goto peek_stack; |
| 1435 | |
| 1436 | check_state: |
| 1437 | for (i = 0; i < insn_cnt; i++) { |
| 1438 | if (insn_state[i] != EXPLORED) { |
| 1439 | verbose("unreachable insn %d\n", i); |
| 1440 | ret = -EINVAL; |
| 1441 | goto err_free; |
| 1442 | } |
| 1443 | } |
| 1444 | ret = 0; /* cfg looks good */ |
| 1445 | |
| 1446 | err_free: |
| 1447 | kfree(insn_state); |
| 1448 | kfree(insn_stack); |
| 1449 | return ret; |
| 1450 | } |
| 1451 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1452 | /* compare two verifier states |
| 1453 | * |
| 1454 | * all states stored in state_list are known to be valid, since |
| 1455 | * verifier reached 'bpf_exit' instruction through them |
| 1456 | * |
| 1457 | * this function is called when verifier exploring different branches of |
| 1458 | * execution popped from the state stack. If it sees an old state that has |
| 1459 | * more strict register state and more strict stack state then this execution |
| 1460 | * branch doesn't need to be explored further, since verifier already |
| 1461 | * concluded that more strict state leads to valid finish. |
| 1462 | * |
| 1463 | * Therefore two states are equivalent if register state is more conservative |
| 1464 | * and explored stack state is more conservative than the current one. |
| 1465 | * Example: |
| 1466 | * explored current |
| 1467 | * (slot1=INV slot2=MISC) == (slot1=MISC slot2=MISC) |
| 1468 | * (slot1=MISC slot2=MISC) != (slot1=INV slot2=MISC) |
| 1469 | * |
| 1470 | * In other words if current stack state (one being explored) has more |
| 1471 | * valid slots than old one that already passed validation, it means |
| 1472 | * the verifier can stop exploring and conclude that current state is valid too |
| 1473 | * |
| 1474 | * Similarly with registers. If explored state has register type as invalid |
| 1475 | * whereas register type in current state is meaningful, it means that |
| 1476 | * the current state will reach 'bpf_exit' instruction safely |
| 1477 | */ |
| 1478 | static bool states_equal(struct verifier_state *old, struct verifier_state *cur) |
| 1479 | { |
| 1480 | int i; |
| 1481 | |
| 1482 | for (i = 0; i < MAX_BPF_REG; i++) { |
| 1483 | if (memcmp(&old->regs[i], &cur->regs[i], |
| 1484 | sizeof(old->regs[0])) != 0) { |
| 1485 | if (old->regs[i].type == NOT_INIT || |
Alexei Starovoitov | 32bf08a | 2014-10-20 14:54:57 -0700 | [diff] [blame] | 1486 | (old->regs[i].type == UNKNOWN_VALUE && |
| 1487 | cur->regs[i].type != NOT_INIT)) |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1488 | continue; |
| 1489 | return false; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | for (i = 0; i < MAX_BPF_STACK; i++) { |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1494 | if (old->stack_slot_type[i] == STACK_INVALID) |
| 1495 | continue; |
| 1496 | if (old->stack_slot_type[i] != cur->stack_slot_type[i]) |
| 1497 | /* Ex: old explored (safe) state has STACK_SPILL in |
| 1498 | * this stack slot, but current has has STACK_MISC -> |
| 1499 | * this verifier states are not equivalent, |
| 1500 | * return false to continue verification of this path |
| 1501 | */ |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1502 | return false; |
Alexei Starovoitov | 9c399760 | 2014-10-28 15:11:41 -0700 | [diff] [blame] | 1503 | if (i % BPF_REG_SIZE) |
| 1504 | continue; |
| 1505 | if (memcmp(&old->spilled_regs[i / BPF_REG_SIZE], |
| 1506 | &cur->spilled_regs[i / BPF_REG_SIZE], |
| 1507 | sizeof(old->spilled_regs[0]))) |
| 1508 | /* when explored and current stack slot types are |
| 1509 | * the same, check that stored pointers types |
| 1510 | * are the same as well. |
| 1511 | * Ex: explored safe path could have stored |
| 1512 | * (struct reg_state) {.type = PTR_TO_STACK, .imm = -8} |
| 1513 | * but current path has stored: |
| 1514 | * (struct reg_state) {.type = PTR_TO_STACK, .imm = -16} |
| 1515 | * such verifier states are not equivalent. |
| 1516 | * return false to continue verification of this path |
| 1517 | */ |
| 1518 | return false; |
| 1519 | else |
| 1520 | continue; |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1521 | } |
| 1522 | return true; |
| 1523 | } |
| 1524 | |
| 1525 | static int is_state_visited(struct verifier_env *env, int insn_idx) |
| 1526 | { |
| 1527 | struct verifier_state_list *new_sl; |
| 1528 | struct verifier_state_list *sl; |
| 1529 | |
| 1530 | sl = env->explored_states[insn_idx]; |
| 1531 | if (!sl) |
| 1532 | /* this 'insn_idx' instruction wasn't marked, so we will not |
| 1533 | * be doing state search here |
| 1534 | */ |
| 1535 | return 0; |
| 1536 | |
| 1537 | while (sl != STATE_LIST_MARK) { |
| 1538 | if (states_equal(&sl->state, &env->cur_state)) |
| 1539 | /* reached equivalent register/stack state, |
| 1540 | * prune the search |
| 1541 | */ |
| 1542 | return 1; |
| 1543 | sl = sl->next; |
| 1544 | } |
| 1545 | |
| 1546 | /* there were no equivalent states, remember current one. |
| 1547 | * technically the current state is not proven to be safe yet, |
| 1548 | * but it will either reach bpf_exit (which means it's safe) or |
| 1549 | * it will be rejected. Since there are no loops, we won't be |
| 1550 | * seeing this 'insn_idx' instruction again on the way to bpf_exit |
| 1551 | */ |
| 1552 | new_sl = kmalloc(sizeof(struct verifier_state_list), GFP_USER); |
| 1553 | if (!new_sl) |
| 1554 | return -ENOMEM; |
| 1555 | |
| 1556 | /* add new state to the head of linked list */ |
| 1557 | memcpy(&new_sl->state, &env->cur_state, sizeof(env->cur_state)); |
| 1558 | new_sl->next = env->explored_states[insn_idx]; |
| 1559 | env->explored_states[insn_idx] = new_sl; |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1563 | static int do_check(struct verifier_env *env) |
| 1564 | { |
| 1565 | struct verifier_state *state = &env->cur_state; |
| 1566 | struct bpf_insn *insns = env->prog->insnsi; |
| 1567 | struct reg_state *regs = state->regs; |
| 1568 | int insn_cnt = env->prog->len; |
| 1569 | int insn_idx, prev_insn_idx = 0; |
| 1570 | int insn_processed = 0; |
| 1571 | bool do_print_state = false; |
| 1572 | |
| 1573 | init_reg_state(regs); |
| 1574 | insn_idx = 0; |
| 1575 | for (;;) { |
| 1576 | struct bpf_insn *insn; |
| 1577 | u8 class; |
| 1578 | int err; |
| 1579 | |
| 1580 | if (insn_idx >= insn_cnt) { |
| 1581 | verbose("invalid insn idx %d insn_cnt %d\n", |
| 1582 | insn_idx, insn_cnt); |
| 1583 | return -EFAULT; |
| 1584 | } |
| 1585 | |
| 1586 | insn = &insns[insn_idx]; |
| 1587 | class = BPF_CLASS(insn->code); |
| 1588 | |
| 1589 | if (++insn_processed > 32768) { |
| 1590 | verbose("BPF program is too large. Proccessed %d insn\n", |
| 1591 | insn_processed); |
| 1592 | return -E2BIG; |
| 1593 | } |
| 1594 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1595 | err = is_state_visited(env, insn_idx); |
| 1596 | if (err < 0) |
| 1597 | return err; |
| 1598 | if (err == 1) { |
| 1599 | /* found equivalent state, can prune the search */ |
| 1600 | if (log_level) { |
| 1601 | if (do_print_state) |
| 1602 | verbose("\nfrom %d to %d: safe\n", |
| 1603 | prev_insn_idx, insn_idx); |
| 1604 | else |
| 1605 | verbose("%d: safe\n", insn_idx); |
| 1606 | } |
| 1607 | goto process_bpf_exit; |
| 1608 | } |
| 1609 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1610 | if (log_level && do_print_state) { |
| 1611 | verbose("\nfrom %d to %d:", prev_insn_idx, insn_idx); |
| 1612 | print_verifier_state(env); |
| 1613 | do_print_state = false; |
| 1614 | } |
| 1615 | |
| 1616 | if (log_level) { |
| 1617 | verbose("%d: ", insn_idx); |
| 1618 | print_bpf_insn(insn); |
| 1619 | } |
| 1620 | |
| 1621 | if (class == BPF_ALU || class == BPF_ALU64) { |
| 1622 | err = check_alu_op(regs, insn); |
| 1623 | if (err) |
| 1624 | return err; |
| 1625 | |
| 1626 | } else if (class == BPF_LDX) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1627 | enum bpf_reg_type src_reg_type; |
| 1628 | |
| 1629 | /* check for reserved fields is already done */ |
| 1630 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1631 | /* check src operand */ |
| 1632 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1633 | if (err) |
| 1634 | return err; |
| 1635 | |
| 1636 | err = check_reg_arg(regs, insn->dst_reg, DST_OP_NO_MARK); |
| 1637 | if (err) |
| 1638 | return err; |
| 1639 | |
| 1640 | /* check that memory (src_reg + off) is readable, |
| 1641 | * the state of dst_reg will be updated by this func |
| 1642 | */ |
| 1643 | err = check_mem_access(env, insn->src_reg, insn->off, |
| 1644 | BPF_SIZE(insn->code), BPF_READ, |
| 1645 | insn->dst_reg); |
| 1646 | if (err) |
| 1647 | return err; |
| 1648 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1649 | src_reg_type = regs[insn->src_reg].type; |
| 1650 | |
| 1651 | if (insn->imm == 0 && BPF_SIZE(insn->code) == BPF_W) { |
| 1652 | /* saw a valid insn |
| 1653 | * dst_reg = *(u32 *)(src_reg + off) |
| 1654 | * use reserved 'imm' field to mark this insn |
| 1655 | */ |
| 1656 | insn->imm = src_reg_type; |
| 1657 | |
| 1658 | } else if (src_reg_type != insn->imm && |
| 1659 | (src_reg_type == PTR_TO_CTX || |
| 1660 | insn->imm == PTR_TO_CTX)) { |
| 1661 | /* ABuser program is trying to use the same insn |
| 1662 | * dst_reg = *(u32*) (src_reg + off) |
| 1663 | * with different pointer types: |
| 1664 | * src_reg == ctx in one branch and |
| 1665 | * src_reg == stack|map in some other branch. |
| 1666 | * Reject it. |
| 1667 | */ |
| 1668 | verbose("same insn cannot be used with different pointers\n"); |
| 1669 | return -EINVAL; |
| 1670 | } |
| 1671 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1672 | } else if (class == BPF_STX) { |
| 1673 | if (BPF_MODE(insn->code) == BPF_XADD) { |
| 1674 | err = check_xadd(env, insn); |
| 1675 | if (err) |
| 1676 | return err; |
| 1677 | insn_idx++; |
| 1678 | continue; |
| 1679 | } |
| 1680 | |
| 1681 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 1682 | insn->imm != 0) { |
| 1683 | verbose("BPF_STX uses reserved fields\n"); |
| 1684 | return -EINVAL; |
| 1685 | } |
| 1686 | /* check src1 operand */ |
| 1687 | err = check_reg_arg(regs, insn->src_reg, SRC_OP); |
| 1688 | if (err) |
| 1689 | return err; |
| 1690 | /* check src2 operand */ |
| 1691 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1692 | if (err) |
| 1693 | return err; |
| 1694 | |
| 1695 | /* check that memory (dst_reg + off) is writeable */ |
| 1696 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 1697 | BPF_SIZE(insn->code), BPF_WRITE, |
| 1698 | insn->src_reg); |
| 1699 | if (err) |
| 1700 | return err; |
| 1701 | |
| 1702 | } else if (class == BPF_ST) { |
| 1703 | if (BPF_MODE(insn->code) != BPF_MEM || |
| 1704 | insn->src_reg != BPF_REG_0) { |
| 1705 | verbose("BPF_ST uses reserved fields\n"); |
| 1706 | return -EINVAL; |
| 1707 | } |
| 1708 | /* check src operand */ |
| 1709 | err = check_reg_arg(regs, insn->dst_reg, SRC_OP); |
| 1710 | if (err) |
| 1711 | return err; |
| 1712 | |
| 1713 | /* check that memory (dst_reg + off) is writeable */ |
| 1714 | err = check_mem_access(env, insn->dst_reg, insn->off, |
| 1715 | BPF_SIZE(insn->code), BPF_WRITE, |
| 1716 | -1); |
| 1717 | if (err) |
| 1718 | return err; |
| 1719 | |
| 1720 | } else if (class == BPF_JMP) { |
| 1721 | u8 opcode = BPF_OP(insn->code); |
| 1722 | |
| 1723 | if (opcode == BPF_CALL) { |
| 1724 | if (BPF_SRC(insn->code) != BPF_K || |
| 1725 | insn->off != 0 || |
| 1726 | insn->src_reg != BPF_REG_0 || |
| 1727 | insn->dst_reg != BPF_REG_0) { |
| 1728 | verbose("BPF_CALL uses reserved fields\n"); |
| 1729 | return -EINVAL; |
| 1730 | } |
| 1731 | |
| 1732 | err = check_call(env, insn->imm); |
| 1733 | if (err) |
| 1734 | return err; |
| 1735 | |
| 1736 | } else if (opcode == BPF_JA) { |
| 1737 | if (BPF_SRC(insn->code) != BPF_K || |
| 1738 | insn->imm != 0 || |
| 1739 | insn->src_reg != BPF_REG_0 || |
| 1740 | insn->dst_reg != BPF_REG_0) { |
| 1741 | verbose("BPF_JA uses reserved fields\n"); |
| 1742 | return -EINVAL; |
| 1743 | } |
| 1744 | |
| 1745 | insn_idx += insn->off + 1; |
| 1746 | continue; |
| 1747 | |
| 1748 | } else if (opcode == BPF_EXIT) { |
| 1749 | if (BPF_SRC(insn->code) != BPF_K || |
| 1750 | insn->imm != 0 || |
| 1751 | insn->src_reg != BPF_REG_0 || |
| 1752 | insn->dst_reg != BPF_REG_0) { |
| 1753 | verbose("BPF_EXIT uses reserved fields\n"); |
| 1754 | return -EINVAL; |
| 1755 | } |
| 1756 | |
| 1757 | /* eBPF calling convetion is such that R0 is used |
| 1758 | * to return the value from eBPF program. |
| 1759 | * Make sure that it's readable at this time |
| 1760 | * of bpf_exit, which means that program wrote |
| 1761 | * something into it earlier |
| 1762 | */ |
| 1763 | err = check_reg_arg(regs, BPF_REG_0, SRC_OP); |
| 1764 | if (err) |
| 1765 | return err; |
| 1766 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 1767 | process_bpf_exit: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1768 | insn_idx = pop_stack(env, &prev_insn_idx); |
| 1769 | if (insn_idx < 0) { |
| 1770 | break; |
| 1771 | } else { |
| 1772 | do_print_state = true; |
| 1773 | continue; |
| 1774 | } |
| 1775 | } else { |
| 1776 | err = check_cond_jmp_op(env, insn, &insn_idx); |
| 1777 | if (err) |
| 1778 | return err; |
| 1779 | } |
| 1780 | } else if (class == BPF_LD) { |
| 1781 | u8 mode = BPF_MODE(insn->code); |
| 1782 | |
| 1783 | if (mode == BPF_ABS || mode == BPF_IND) { |
Alexei Starovoitov | ddd872b | 2014-12-01 15:06:34 -0800 | [diff] [blame] | 1784 | err = check_ld_abs(env, insn); |
| 1785 | if (err) |
| 1786 | return err; |
| 1787 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 1788 | } else if (mode == BPF_IMM) { |
| 1789 | err = check_ld_imm(env, insn); |
| 1790 | if (err) |
| 1791 | return err; |
| 1792 | |
| 1793 | insn_idx++; |
| 1794 | } else { |
| 1795 | verbose("invalid BPF_LD mode\n"); |
| 1796 | return -EINVAL; |
| 1797 | } |
| 1798 | } else { |
| 1799 | verbose("unknown insn class %d\n", class); |
| 1800 | return -EINVAL; |
| 1801 | } |
| 1802 | |
| 1803 | insn_idx++; |
| 1804 | } |
| 1805 | |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 1809 | /* look for pseudo eBPF instructions that access map FDs and |
| 1810 | * replace them with actual map pointers |
| 1811 | */ |
| 1812 | static int replace_map_fd_with_map_ptr(struct verifier_env *env) |
| 1813 | { |
| 1814 | struct bpf_insn *insn = env->prog->insnsi; |
| 1815 | int insn_cnt = env->prog->len; |
| 1816 | int i, j; |
| 1817 | |
| 1818 | for (i = 0; i < insn_cnt; i++, insn++) { |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1819 | if (BPF_CLASS(insn->code) == BPF_LDX && |
| 1820 | (BPF_MODE(insn->code) != BPF_MEM || |
| 1821 | insn->imm != 0)) { |
| 1822 | verbose("BPF_LDX uses reserved fields\n"); |
| 1823 | return -EINVAL; |
| 1824 | } |
| 1825 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 1826 | if (insn[0].code == (BPF_LD | BPF_IMM | BPF_DW)) { |
| 1827 | struct bpf_map *map; |
| 1828 | struct fd f; |
| 1829 | |
| 1830 | if (i == insn_cnt - 1 || insn[1].code != 0 || |
| 1831 | insn[1].dst_reg != 0 || insn[1].src_reg != 0 || |
| 1832 | insn[1].off != 0) { |
| 1833 | verbose("invalid bpf_ld_imm64 insn\n"); |
| 1834 | return -EINVAL; |
| 1835 | } |
| 1836 | |
| 1837 | if (insn->src_reg == 0) |
| 1838 | /* valid generic load 64-bit imm */ |
| 1839 | goto next_insn; |
| 1840 | |
| 1841 | if (insn->src_reg != BPF_PSEUDO_MAP_FD) { |
| 1842 | verbose("unrecognized bpf_ld_imm64 insn\n"); |
| 1843 | return -EINVAL; |
| 1844 | } |
| 1845 | |
| 1846 | f = fdget(insn->imm); |
| 1847 | |
| 1848 | map = bpf_map_get(f); |
| 1849 | if (IS_ERR(map)) { |
| 1850 | verbose("fd %d is not pointing to valid bpf_map\n", |
| 1851 | insn->imm); |
| 1852 | fdput(f); |
| 1853 | return PTR_ERR(map); |
| 1854 | } |
| 1855 | |
| 1856 | /* store map pointer inside BPF_LD_IMM64 instruction */ |
| 1857 | insn[0].imm = (u32) (unsigned long) map; |
| 1858 | insn[1].imm = ((u64) (unsigned long) map) >> 32; |
| 1859 | |
| 1860 | /* check whether we recorded this map already */ |
| 1861 | for (j = 0; j < env->used_map_cnt; j++) |
| 1862 | if (env->used_maps[j] == map) { |
| 1863 | fdput(f); |
| 1864 | goto next_insn; |
| 1865 | } |
| 1866 | |
| 1867 | if (env->used_map_cnt >= MAX_USED_MAPS) { |
| 1868 | fdput(f); |
| 1869 | return -E2BIG; |
| 1870 | } |
| 1871 | |
| 1872 | /* remember this map */ |
| 1873 | env->used_maps[env->used_map_cnt++] = map; |
| 1874 | |
| 1875 | /* hold the map. If the program is rejected by verifier, |
| 1876 | * the map will be released by release_maps() or it |
| 1877 | * will be used by the valid program until it's unloaded |
| 1878 | * and all maps are released in free_bpf_prog_info() |
| 1879 | */ |
| 1880 | atomic_inc(&map->refcnt); |
| 1881 | |
| 1882 | fdput(f); |
| 1883 | next_insn: |
| 1884 | insn++; |
| 1885 | i++; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | /* now all pseudo BPF_LD_IMM64 instructions load valid |
| 1890 | * 'struct bpf_map *' into a register instead of user map_fd. |
| 1891 | * These pointers will be used later by verifier to validate map access. |
| 1892 | */ |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | /* drop refcnt of maps used by the rejected program */ |
| 1897 | static void release_maps(struct verifier_env *env) |
| 1898 | { |
| 1899 | int i; |
| 1900 | |
| 1901 | for (i = 0; i < env->used_map_cnt; i++) |
| 1902 | bpf_map_put(env->used_maps[i]); |
| 1903 | } |
| 1904 | |
| 1905 | /* convert pseudo BPF_LD_IMM64 into generic BPF_LD_IMM64 */ |
| 1906 | static void convert_pseudo_ld_imm64(struct verifier_env *env) |
| 1907 | { |
| 1908 | struct bpf_insn *insn = env->prog->insnsi; |
| 1909 | int insn_cnt = env->prog->len; |
| 1910 | int i; |
| 1911 | |
| 1912 | for (i = 0; i < insn_cnt; i++, insn++) |
| 1913 | if (insn->code == (BPF_LD | BPF_IMM | BPF_DW)) |
| 1914 | insn->src_reg = 0; |
| 1915 | } |
| 1916 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 1917 | static void adjust_branches(struct bpf_prog *prog, int pos, int delta) |
| 1918 | { |
| 1919 | struct bpf_insn *insn = prog->insnsi; |
| 1920 | int insn_cnt = prog->len; |
| 1921 | int i; |
| 1922 | |
| 1923 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 1924 | if (BPF_CLASS(insn->code) != BPF_JMP || |
| 1925 | BPF_OP(insn->code) == BPF_CALL || |
| 1926 | BPF_OP(insn->code) == BPF_EXIT) |
| 1927 | continue; |
| 1928 | |
| 1929 | /* adjust offset of jmps if necessary */ |
| 1930 | if (i < pos && i + insn->off + 1 > pos) |
| 1931 | insn->off += delta; |
| 1932 | else if (i > pos && i + insn->off + 1 < pos) |
| 1933 | insn->off -= delta; |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | /* convert load instructions that access fields of 'struct __sk_buff' |
| 1938 | * into sequence of instructions that access fields of 'struct sk_buff' |
| 1939 | */ |
| 1940 | static int convert_ctx_accesses(struct verifier_env *env) |
| 1941 | { |
| 1942 | struct bpf_insn *insn = env->prog->insnsi; |
| 1943 | int insn_cnt = env->prog->len; |
| 1944 | struct bpf_insn insn_buf[16]; |
| 1945 | struct bpf_prog *new_prog; |
| 1946 | u32 cnt; |
| 1947 | int i; |
| 1948 | |
| 1949 | if (!env->prog->aux->ops->convert_ctx_access) |
| 1950 | return 0; |
| 1951 | |
| 1952 | for (i = 0; i < insn_cnt; i++, insn++) { |
| 1953 | if (insn->code != (BPF_LDX | BPF_MEM | BPF_W)) |
| 1954 | continue; |
| 1955 | |
| 1956 | if (insn->imm != PTR_TO_CTX) { |
| 1957 | /* clear internal mark */ |
| 1958 | insn->imm = 0; |
| 1959 | continue; |
| 1960 | } |
| 1961 | |
| 1962 | cnt = env->prog->aux->ops-> |
| 1963 | convert_ctx_access(insn->dst_reg, insn->src_reg, |
| 1964 | insn->off, insn_buf); |
| 1965 | if (cnt == 0 || cnt >= ARRAY_SIZE(insn_buf)) { |
| 1966 | verbose("bpf verifier is misconfigured\n"); |
| 1967 | return -EINVAL; |
| 1968 | } |
| 1969 | |
| 1970 | if (cnt == 1) { |
| 1971 | memcpy(insn, insn_buf, sizeof(*insn)); |
| 1972 | continue; |
| 1973 | } |
| 1974 | |
| 1975 | /* several new insns need to be inserted. Make room for them */ |
| 1976 | insn_cnt += cnt - 1; |
| 1977 | new_prog = bpf_prog_realloc(env->prog, |
| 1978 | bpf_prog_size(insn_cnt), |
| 1979 | GFP_USER); |
| 1980 | if (!new_prog) |
| 1981 | return -ENOMEM; |
| 1982 | |
| 1983 | new_prog->len = insn_cnt; |
| 1984 | |
| 1985 | memmove(new_prog->insnsi + i + cnt, new_prog->insns + i + 1, |
| 1986 | sizeof(*insn) * (insn_cnt - i - cnt)); |
| 1987 | |
| 1988 | /* copy substitute insns in place of load instruction */ |
| 1989 | memcpy(new_prog->insnsi + i, insn_buf, sizeof(*insn) * cnt); |
| 1990 | |
| 1991 | /* adjust branches in the whole program */ |
| 1992 | adjust_branches(new_prog, i, cnt - 1); |
| 1993 | |
| 1994 | /* keep walking new program and skip insns we just inserted */ |
| 1995 | env->prog = new_prog; |
| 1996 | insn = new_prog->insnsi + i + cnt - 1; |
| 1997 | i += cnt - 1; |
| 1998 | } |
| 1999 | |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2003 | static void free_states(struct verifier_env *env) |
| 2004 | { |
| 2005 | struct verifier_state_list *sl, *sln; |
| 2006 | int i; |
| 2007 | |
| 2008 | if (!env->explored_states) |
| 2009 | return; |
| 2010 | |
| 2011 | for (i = 0; i < env->prog->len; i++) { |
| 2012 | sl = env->explored_states[i]; |
| 2013 | |
| 2014 | if (sl) |
| 2015 | while (sl != STATE_LIST_MARK) { |
| 2016 | sln = sl->next; |
| 2017 | kfree(sl); |
| 2018 | sl = sln; |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | kfree(env->explored_states); |
| 2023 | } |
| 2024 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2025 | int bpf_check(struct bpf_prog **prog, union bpf_attr *attr) |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2026 | { |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2027 | char __user *log_ubuf = NULL; |
| 2028 | struct verifier_env *env; |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2029 | int ret = -EINVAL; |
| 2030 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2031 | if ((*prog)->len <= 0 || (*prog)->len > BPF_MAXINSNS) |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2032 | return -E2BIG; |
| 2033 | |
| 2034 | /* 'struct verifier_env' can be global, but since it's not small, |
| 2035 | * allocate/free it every time bpf_check() is called |
| 2036 | */ |
| 2037 | env = kzalloc(sizeof(struct verifier_env), GFP_KERNEL); |
| 2038 | if (!env) |
| 2039 | return -ENOMEM; |
| 2040 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2041 | env->prog = *prog; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2042 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2043 | /* grab the mutex to protect few globals used by verifier */ |
| 2044 | mutex_lock(&bpf_verifier_lock); |
| 2045 | |
| 2046 | if (attr->log_level || attr->log_buf || attr->log_size) { |
| 2047 | /* user requested verbose verifier output |
| 2048 | * and supplied buffer to store the verification trace |
| 2049 | */ |
| 2050 | log_level = attr->log_level; |
| 2051 | log_ubuf = (char __user *) (unsigned long) attr->log_buf; |
| 2052 | log_size = attr->log_size; |
| 2053 | log_len = 0; |
| 2054 | |
| 2055 | ret = -EINVAL; |
| 2056 | /* log_* values have to be sane */ |
| 2057 | if (log_size < 128 || log_size > UINT_MAX >> 8 || |
| 2058 | log_level == 0 || log_ubuf == NULL) |
| 2059 | goto free_env; |
| 2060 | |
| 2061 | ret = -ENOMEM; |
| 2062 | log_buf = vmalloc(log_size); |
| 2063 | if (!log_buf) |
| 2064 | goto free_env; |
| 2065 | } else { |
| 2066 | log_level = 0; |
| 2067 | } |
| 2068 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2069 | ret = replace_map_fd_with_map_ptr(env); |
| 2070 | if (ret < 0) |
| 2071 | goto skip_full_check; |
| 2072 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2073 | env->explored_states = kcalloc(env->prog->len, |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2074 | sizeof(struct verifier_state_list *), |
| 2075 | GFP_USER); |
| 2076 | ret = -ENOMEM; |
| 2077 | if (!env->explored_states) |
| 2078 | goto skip_full_check; |
| 2079 | |
Alexei Starovoitov | 475fb78 | 2014-09-26 00:17:05 -0700 | [diff] [blame] | 2080 | ret = check_cfg(env); |
| 2081 | if (ret < 0) |
| 2082 | goto skip_full_check; |
| 2083 | |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2084 | ret = do_check(env); |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2085 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2086 | skip_full_check: |
Alexei Starovoitov | 17a5267 | 2014-09-26 00:17:06 -0700 | [diff] [blame] | 2087 | while (pop_stack(env, NULL) >= 0); |
Alexei Starovoitov | f1bca82 | 2014-09-29 18:50:01 -0700 | [diff] [blame] | 2088 | free_states(env); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2089 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2090 | if (ret == 0) |
| 2091 | /* program is valid, convert *(u32*)(ctx + off) accesses */ |
| 2092 | ret = convert_ctx_accesses(env); |
| 2093 | |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2094 | if (log_level && log_len >= log_size - 1) { |
| 2095 | BUG_ON(log_len >= log_size); |
| 2096 | /* verifier log exceeded user supplied buffer */ |
| 2097 | ret = -ENOSPC; |
| 2098 | /* fall through to return what was recorded */ |
| 2099 | } |
| 2100 | |
| 2101 | /* copy verifier log back to user space including trailing zero */ |
| 2102 | if (log_level && copy_to_user(log_ubuf, log_buf, log_len + 1) != 0) { |
| 2103 | ret = -EFAULT; |
| 2104 | goto free_log_buf; |
| 2105 | } |
| 2106 | |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2107 | if (ret == 0 && env->used_map_cnt) { |
| 2108 | /* if program passed verifier, update used_maps in bpf_prog_info */ |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2109 | env->prog->aux->used_maps = kmalloc_array(env->used_map_cnt, |
| 2110 | sizeof(env->used_maps[0]), |
| 2111 | GFP_KERNEL); |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2112 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2113 | if (!env->prog->aux->used_maps) { |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2114 | ret = -ENOMEM; |
| 2115 | goto free_log_buf; |
| 2116 | } |
| 2117 | |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2118 | memcpy(env->prog->aux->used_maps, env->used_maps, |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2119 | sizeof(env->used_maps[0]) * env->used_map_cnt); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2120 | env->prog->aux->used_map_cnt = env->used_map_cnt; |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2121 | |
| 2122 | /* program is valid. Convert pseudo bpf_ld_imm64 into generic |
| 2123 | * bpf_ld_imm64 instructions |
| 2124 | */ |
| 2125 | convert_pseudo_ld_imm64(env); |
| 2126 | } |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2127 | |
| 2128 | free_log_buf: |
| 2129 | if (log_level) |
| 2130 | vfree(log_buf); |
| 2131 | free_env: |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2132 | if (!env->prog->aux->used_maps) |
Alexei Starovoitov | 0246e64 | 2014-09-26 00:17:04 -0700 | [diff] [blame] | 2133 | /* if we didn't copy map pointers into bpf_prog_info, release |
| 2134 | * them now. Otherwise free_bpf_prog_info() will release them. |
| 2135 | */ |
| 2136 | release_maps(env); |
Alexei Starovoitov | 9bac3d6 | 2015-03-13 11:57:42 -0700 | [diff] [blame] | 2137 | *prog = env->prog; |
Alexei Starovoitov | cbd3570 | 2014-09-26 00:17:03 -0700 | [diff] [blame] | 2138 | kfree(env); |
| 2139 | mutex_unlock(&bpf_verifier_lock); |
Alexei Starovoitov | 51580e7 | 2014-09-26 00:17:02 -0700 | [diff] [blame] | 2140 | return ret; |
| 2141 | } |