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