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