| @@ -228,6 +228,12 @@ static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state) · 227 unmodified lines |
| 228 | 228 | (poisoned ? BPF_MAP_KEY_POISON : 0ULL); |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | +static bool bpf_pseudo_call(const struct bpf_insn *insn) |
| 232 | +{ |
| 233 | + return insn->code == (BPF_JMP | BPF_CALL) && |
| 234 | + insn->src_reg == BPF_PSEUDO_CALL; |
| 235 | +} |
| 236 | + |
| 231 | 237 | struct bpf_call_arg_meta { |
| 232 | 238 | struct bpf_map *map_ptr; |
| 233 | 239 | bool raw_mode; |
| @@ -1073,6 +1079,51 @@ static void mark_reg_known_zero(struct bpf_verifier_env *env, · 839 unmodified lines |
| 1073 | 1079 | __mark_reg_known_zero(regs + regno); |
| 1074 | 1080 | } |
| 1075 | 1081 | |
| 1082 | +static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) |
| 1083 | +{ |
| 1084 | + switch (reg->type) { |
| 1085 | + case PTR_TO_MAP_VALUE_OR_NULL: { |
| 1086 | + const struct bpf_map *map = reg->map_ptr; |
| 1087 | + |
| 1088 | + if (map->inner_map_meta) { |
| 1089 | + reg->type = CONST_PTR_TO_MAP; |
| 1090 | + reg->map_ptr = map->inner_map_meta; |
| 1091 | + } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
| 1092 | + reg->type = PTR_TO_XDP_SOCK; |
| 1093 | + } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
| 1094 | + map->map_type == BPF_MAP_TYPE_SOCKHASH) { |
| 1095 | + reg->type = PTR_TO_SOCKET; |
| 1096 | + } else { |
| 1097 | + reg->type = PTR_TO_MAP_VALUE; |
| 1098 | + } |
| 1099 | + break; |
| 1100 | + } |
| 1101 | + case PTR_TO_SOCKET_OR_NULL: |
| 1102 | + reg->type = PTR_TO_SOCKET; |
| 1103 | + break; |
| 1104 | + case PTR_TO_SOCK_COMMON_OR_NULL: |
| 1105 | + reg->type = PTR_TO_SOCK_COMMON; |
| 1106 | + break; |
| 1107 | + case PTR_TO_TCP_SOCK_OR_NULL: |
| 1108 | + reg->type = PTR_TO_TCP_SOCK; |
| 1109 | + break; |
| 1110 | + case PTR_TO_BTF_ID_OR_NULL: |
| 1111 | + reg->type = PTR_TO_BTF_ID; |
| 1112 | + break; |
| 1113 | + case PTR_TO_MEM_OR_NULL: |
| 1114 | + reg->type = PTR_TO_MEM; |
| 1115 | + break; |
| 1116 | + case PTR_TO_RDONLY_BUF_OR_NULL: |
| 1117 | + reg->type = PTR_TO_RDONLY_BUF; |
| 1118 | + break; |
| 1119 | + case PTR_TO_RDWR_BUF_OR_NULL: |
| 1120 | + reg->type = PTR_TO_RDWR_BUF; |
| 1121 | + break; |
| 1122 | + default: |
| 1123 | + WARN_ON("unknown nullable register type"); |
| 1124 | + } |
| 1125 | +} |
| 1126 | + |
| 1076 | 1127 | static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg) |
| 1077 | 1128 | { |
| 1078 | 1129 | return type_is_pkt_pointer(reg->type); |
| @@ -1486,9 +1537,7 @@ static int check_subprogs(struct bpf_verifier_env *env) · 407 unmodified lines |
| 1486 | 1537 | |
| 1487 | 1538 | /* determine subprog starts. The end is one before the next starts */ |
| 1488 | 1539 | for (i = 0; i < insn_cnt; i++) { |
| 1489 | | − if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 1490 | | − continue; |
| 1491 | | − if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 1540 | + if (!bpf_pseudo_call(insn + i)) |
| 1492 | 1541 | continue; |
| 1493 | 1542 | if (!env->bpf_capable) { |
| 1494 | 1543 | verbose(env, |
| @@ -2271,12 +2320,14 @@ static void save_register_state(struct bpf_func_state *state, · 776 unmodified lines |
| 2271 | 2320 | state->stack[spi].slot_type[i] = STACK_SPILL; |
| 2272 | 2321 | } |
| 2273 | 2322 | |
| 2274 | | −/* check_stack_read/write functions track spill/fill of registers, |
| 2323 | +/* check_stack_{read,write}_fixed_off functions track spill/fill of registers, |
| 2275 | 2324 | * stack boundary and alignment are checked in check_mem_access() |
| 2276 | 2325 | */ |
| 2277 | | −static int check_stack_write(struct bpf_verifier_env *env, |
| 2278 | | − struct bpf_func_state *state, /* func where register points to */ |
| 2279 | | − int off, int size, int value_regno, int insn_idx) |
| 2326 | +static int check_stack_write_fixed_off(struct bpf_verifier_env *env, |
| 2327 | + /* stack frame we're writing to */ |
| 2328 | + struct bpf_func_state *state, |
| 2329 | + int off, int size, int value_regno, |
| 2330 | + int insn_idx) |
| 2280 | 2331 | { |
| 2281 | 2332 | struct bpf_func_state *cur; /* state of the current function */ |
| 2282 | 2333 | int i, slot = -off - 1, spi = slot / BPF_REG_SIZE, err; |
| @@ -2402,9 +2453,175 @@ static int check_stack_write(struct bpf_verifier_env *env, · 119 unmodified lines |
| 2402 | 2453 | return 0; |
| 2403 | 2454 | } |
| 2404 | 2455 | |
| 2405 | | −static int check_stack_read(struct bpf_verifier_env *env, |
| 2406 | | − struct bpf_func_state *reg_state /* func where register points to */, |
| 2407 | | − int off, int size, int value_regno) |
| 2456 | +/* Write the stack: 'stack[ptr_regno + off] = value_regno'. 'ptr_regno' is |
| 2457 | + * known to contain a variable offset. |
| 2458 | + * This function checks whether the write is permitted and conservatively |
| 2459 | + * tracks the effects of the write, considering that each stack slot in the |
| 2460 | + * dynamic range is potentially written to. |
| 2461 | + * |
| 2462 | + * 'off' includes 'regno->off'. |
| 2463 | + * 'value_regno' can be -1, meaning that an unknown value is being written to |
| 2464 | + * the stack. |
| 2465 | + * |
| 2466 | + * Spilled pointers in range are not marked as written because we don't know |
| 2467 | + * what's going to be actually written. This means that read propagation for |
| 2468 | + * future reads cannot be terminated by this write. |
| 2469 | + * |
| 2470 | + * For privileged programs, uninitialized stack slots are considered |
| 2471 | + * initialized by this write (even though we don't know exactly what offsets |
| 2472 | + * are going to be written to). The idea is that we don't want the verifier to |
| 2473 | + * reject future reads that access slots written to through variable offsets. |
| 2474 | + */ |
| 2475 | +static int check_stack_write_var_off(struct bpf_verifier_env *env, |
| 2476 | + /* func where register points to */ |
| 2477 | + struct bpf_func_state *state, |
| 2478 | + int ptr_regno, int off, int size, |
| 2479 | + int value_regno, int insn_idx) |
| 2480 | +{ |
| 2481 | + struct bpf_func_state *cur; /* state of the current function */ |
| 2482 | + int min_off, max_off; |
| 2483 | + int i, err; |
| 2484 | + struct bpf_reg_state *ptr_reg = NULL, *value_reg = NULL; |
| 2485 | + bool writing_zero = false; |
| 2486 | + /* set if the fact that we're writing a zero is used to let any |
| 2487 | + * stack slots remain STACK_ZERO |
| 2488 | + */ |
| 2489 | + bool zero_used = false; |
| 2490 | + |
| 2491 | + cur = env->cur_state->frame[env->cur_state->curframe]; |
| 2492 | + ptr_reg = &cur->regs[ptr_regno]; |
| 2493 | + min_off = ptr_reg->smin_value + off; |
| 2494 | + max_off = ptr_reg->smax_value + off + size; |
| 2495 | + if (value_regno >= 0) |
| 2496 | + value_reg = &cur->regs[value_regno]; |
| 2497 | + if (value_reg && register_is_null(value_reg)) |
| 2498 | + writing_zero = true; |
| 2499 | + |
| 2500 | + err = realloc_func_state(state, round_up(-min_off, BPF_REG_SIZE), |
| 2501 | + state->acquired_refs, true); |
| 2502 | + if (err) |
| 2503 | + return err; |
| 2504 | + |
| 2505 | + |
| 2506 | + /* Variable offset writes destroy any spilled pointers in range. */ |
| 2507 | + for (i = min_off; i < max_off; i++) { |
| 2508 | + u8 new_type, *stype; |
| 2509 | + int slot, spi; |
| 2510 | + |
| 2511 | + slot = -i - 1; |
| 2512 | + spi = slot / BPF_REG_SIZE; |
| 2513 | + stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE]; |
| 2514 | + |
| 2515 | + if (!env->allow_ptr_leaks |
| 2516 | + && *stype != NOT_INIT |
| 2517 | + && *stype != SCALAR_VALUE) { |
| 2518 | + /* Reject the write if there's are spilled pointers in |
| 2519 | + * range. If we didn't reject here, the ptr status |
| 2520 | + * would be erased below (even though not all slots are |
| 2521 | + * actually overwritten), possibly opening the door to |
| 2522 | + * leaks. |
| 2523 | + */ |
| 2524 | + verbose(env, "spilled ptr in range of var-offset stack write; insn %d, ptr off: %d", |
| 2525 | + insn_idx, i); |
| 2526 | + return -EINVAL; |
| 2527 | + } |
| 2528 | + |
| 2529 | + /* Erase all spilled pointers. */ |
| 2530 | + state->stack[spi].spilled_ptr.type = NOT_INIT; |
| 2531 | + |
| 2532 | + /* Update the slot type. */ |
| 2533 | + new_type = STACK_MISC; |
| 2534 | + if (writing_zero && *stype == STACK_ZERO) { |
| 2535 | + new_type = STACK_ZERO; |
| 2536 | + zero_used = true; |
| 2537 | + } |
| 2538 | + /* If the slot is STACK_INVALID, we check whether it's OK to |
| 2539 | + * pretend that it will be initialized by this write. The slot |
| 2540 | + * might not actually be written to, and so if we mark it as |
| 2541 | + * initialized future reads might leak uninitialized memory. |
| 2542 | + * For privileged programs, we will accept such reads to slots |
| 2543 | + * that may or may not be written because, if we're reject |
| 2544 | + * them, the error would be too confusing. |
| 2545 | + */ |
| 2546 | + if (*stype == STACK_INVALID && !env->allow_uninit_stack) { |
| 2547 | + verbose(env, "uninit stack in range of var-offset write prohibited for !root; insn %d, off: %d", |
| 2548 | + insn_idx, i); |
| 2549 | + return -EINVAL; |
| 2550 | + } |
| 2551 | + *stype = new_type; |
| 2552 | + } |
| 2553 | + if (zero_used) { |
| 2554 | + /* backtracking doesn't work for STACK_ZERO yet. */ |
| 2555 | + err = mark_chain_precision(env, value_regno); |
| 2556 | + if (err) |
| 2557 | + return err; |
| 2558 | + } |
| 2559 | + return 0; |
| 2560 | +} |
| 2561 | + |
| 2562 | +/* When register 'dst_regno' is assigned some values from stack[min_off, |
| 2563 | + * max_off), we set the register's type according to the types of the |
| 2564 | + * respective stack slots. If all the stack values are known to be zeros, then |
| 2565 | + * so is the destination reg. Otherwise, the register is considered to be |
| 2566 | + * SCALAR. This function does not deal with register filling; the caller must |
| 2567 | + * ensure that all spilled registers in the stack range have been marked as |
| 2568 | + * read. |
| 2569 | + */ |
| 2570 | +static void mark_reg_stack_read(struct bpf_verifier_env *env, |
| 2571 | + /* func where src register points to */ |
| 2572 | + struct bpf_func_state *ptr_state, |
| 2573 | + int min_off, int max_off, int dst_regno) |
| 2574 | +{ |
| 2575 | + struct bpf_verifier_state *vstate = env->cur_state; |
| 2576 | + struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| 2577 | + int i, slot, spi; |
| 2578 | + u8 *stype; |
| 2579 | + int zeros = 0; |
| 2580 | + |
| 2581 | + for (i = min_off; i < max_off; i++) { |
| 2582 | + slot = -i - 1; |
| 2583 | + spi = slot / BPF_REG_SIZE; |
| 2584 | + stype = ptr_state->stack[spi].slot_type; |
| 2585 | + if (stype[slot % BPF_REG_SIZE] != STACK_ZERO) |
| 2586 | + break; |
| 2587 | + zeros++; |
| 2588 | + } |
| 2589 | + if (zeros == max_off - min_off) { |
| 2590 | + /* any access_size read into register is zero extended, |
| 2591 | + * so the whole register == const_zero |
| 2592 | + */ |
| 2593 | + __mark_reg_const_zero(&state->regs[dst_regno]); |
| 2594 | + /* backtracking doesn't support STACK_ZERO yet, |
| 2595 | + * so mark it precise here, so that later |
| 2596 | + * backtracking can stop here. |
| 2597 | + * Backtracking may not need this if this register |
| 2598 | + * doesn't participate in pointer adjustment. |
| 2599 | + * Forward propagation of precise flag is not |
| 2600 | + * necessary either. This mark is only to stop |
| 2601 | + * backtracking. Any register that contributed |
| 2602 | + * to const 0 was marked precise before spill. |
| 2603 | + */ |
| 2604 | + state->regs[dst_regno].precise = true; |
| 2605 | + } else { |
| 2606 | + /* have read misc data from the stack */ |
| 2607 | + mark_reg_unknown(env, state->regs, dst_regno); |
| 2608 | + } |
| 2609 | + state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
| 2610 | +} |
| 2611 | + |
| 2612 | +/* Read the stack at 'off' and put the results into the register indicated by |
| 2613 | + * 'dst_regno'. It handles reg filling if the addressed stack slot is a |
| 2614 | + * spilled reg. |
| 2615 | + * |
| 2616 | + * 'dst_regno' can be -1, meaning that the read value is not going to a |
| 2617 | + * register. |
| 2618 | + * |
| 2619 | + * The access is assumed to be within the current stack bounds. |
| 2620 | + */ |
| 2621 | +static int check_stack_read_fixed_off(struct bpf_verifier_env *env, |
| 2622 | + /* func where src register points to */ |
| 2623 | + struct bpf_func_state *reg_state, |
| 2624 | + int off, int size, int dst_regno) |
| 2408 | 2625 | { |
| 2409 | 2626 | struct bpf_verifier_state *vstate = env->cur_state; |
| 2410 | 2627 | struct bpf_func_state *state = vstate->frame[vstate->curframe]; |
| @@ -2412,11 +2629,6 @@ static int check_stack_read(struct bpf_verifier_env *env, · 1 unmodified line |
| 2412 | 2629 | struct bpf_reg_state *reg; |
| 2413 | 2630 | u8 *stype; |
| 2414 | 2631 | |
| 2415 | | − if (reg_state->allocated_stack <= slot) { |
| 2416 | | − verbose(env, "invalid read from stack off %d+0 size %d\n", |
| 2417 | | − off, size); |
| 2418 | | − return -EACCES; |
| 2419 | | − } |
| 2420 | 2632 | stype = reg_state->stack[spi].slot_type; |
| 2421 | 2633 | reg = ®_state->stack[spi].spilled_ptr; |
| 2422 | 2634 | |
| @@ -2427,9 +2639,9 @@ static int check_stack_read(struct bpf_verifier_env *env, · 4 unmodified lines |
| 2427 | 2639 | verbose(env, "invalid size of register fill\n"); |
| 2428 | 2640 | return -EACCES; |
| 2429 | 2641 | } |
| 2430 | | − if (value_regno >= 0) { |
| 2431 | | − mark_reg_unknown(env, state->regs, value_regno); |
| 2432 | | − state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2642 | + if (dst_regno >= 0) { |
| 2643 | + mark_reg_unknown(env, state->regs, dst_regno); |
| 2644 | + state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
| 2433 | 2645 | } |
| 2434 | 2646 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 2435 | 2647 | return 0; |
| @@ -2441,16 +2653,16 @@ static int check_stack_read(struct bpf_verifier_env *env, · 5 unmodified lines |
| 2441 | 2653 | } |
| 2442 | 2654 | } |
| 2443 | 2655 | |
| 2444 | | − if (value_regno >= 0) { |
| 2656 | + if (dst_regno >= 0) { |
| 2445 | 2657 | /* restore register state from stack */ |
| 2446 | | − state->regs[value_regno] = *reg; |
| 2658 | + state->regs[dst_regno] = *reg; |
| 2447 | 2659 | /* mark reg as written since spilled pointer state likely |
| 2448 | 2660 | * has its liveness marks cleared by is_state_visited() |
| 2449 | 2661 | * which resets stack/reg liveness for state transitions |
| 2450 | 2662 | */ |
| 2451 | | − state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2663 | + state->regs[dst_regno].live |= REG_LIVE_WRITTEN; |
| 2452 | 2664 | } else if (__is_pointer_value(env->allow_ptr_leaks, reg)) { |
| 2453 | | − /* If value_regno==-1, the caller is asking us whether |
| 2665 | + /* If dst_regno==-1, the caller is asking us whether |
| 2454 | 2666 | * it is acceptable to use this value as a SCALAR_VALUE |
| 2455 | 2667 | * (e.g. for XADD). |
| 2456 | 2668 | * We must not allow unprivileged callers to do that |
| @@ -2462,70 +2674,167 @@ static int check_stack_read(struct bpf_verifier_env *env, · 5 unmodified lines |
| 2462 | 2674 | } |
| 2463 | 2675 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 2464 | 2676 | } else { |
| 2465 | | − int zeros = 0; |
| 2677 | + u8 type; |
| 2466 | 2678 | |
| 2467 | 2679 | for (i = 0; i < size; i++) { |
| 2468 | | − if (stype[(slot - i) % BPF_REG_SIZE] == STACK_MISC) |
| 2680 | + type = stype[(slot - i) % BPF_REG_SIZE]; |
| 2681 | + if (type == STACK_MISC) |
| 2469 | 2682 | continue; |
| 2470 | | − if (stype[(slot - i) % BPF_REG_SIZE] == STACK_ZERO) { |
| 2471 | | − zeros++; |
| 2683 | + if (type == STACK_ZERO) |
| 2472 | 2684 | continue; |
| 2473 | | − } |
| 2474 | 2685 | verbose(env, "invalid read from stack off %d+%d size %d\n", |
| 2475 | 2686 | off, i, size); |
| 2476 | 2687 | return -EACCES; |
| 2477 | 2688 | } |
| 2478 | 2689 | mark_reg_read(env, reg, reg->parent, REG_LIVE_READ64); |
| 2479 | | − if (value_regno >= 0) { |
| 2480 | | − if (zeros == size) { |
| 2481 | | − /* any size read into register is zero extended, |
| 2482 | | − * so the whole register == const_zero |
| 2483 | | − */ |
| 2484 | | − __mark_reg_const_zero(&state->regs[value_regno]); |
| 2485 | | − /* backtracking doesn't support STACK_ZERO yet, |
| 2486 | | − * so mark it precise here, so that later |
| 2487 | | − * backtracking can stop here. |
| 2488 | | − * Backtracking may not need this if this register |
| 2489 | | − * doesn't participate in pointer adjustment. |
| 2490 | | − * Forward propagation of precise flag is not |
| 2491 | | − * necessary either. This mark is only to stop |
| 2492 | | − * backtracking. Any register that contributed |
| 2493 | | − * to const 0 was marked precise before spill. |
| 2494 | | − */ |
| 2495 | | − state->regs[value_regno].precise = true; |
| 2496 | | − } else { |
| 2497 | | − /* have read misc data from the stack */ |
| 2498 | | − mark_reg_unknown(env, state->regs, value_regno); |
| 2499 | | − } |
| 2500 | | − state->regs[value_regno].live |= REG_LIVE_WRITTEN; |
| 2501 | | − } |
| 2690 | + if (dst_regno >= 0) |
| 2691 | + mark_reg_stack_read(env, reg_state, off, off + size, dst_regno); |
| 2502 | 2692 | } |
| 2503 | 2693 | return 0; |
| 2504 | 2694 | } |
| 2505 | 2695 | |
| 2506 | | −static int check_stack_access(struct bpf_verifier_env *env, |
| 2507 | | − const struct bpf_reg_state *reg, |
| 2508 | | − int off, int size) |
| 2696 | +enum stack_access_src { |
| 2697 | + ACCESS_DIRECT = 1, /* the access is performed by an instruction */ |
| 2698 | + ACCESS_HELPER = 2, /* the access is performed by a helper */ |
| 2699 | +}; |
| 2700 | + |
| 2701 | +static int check_stack_range_initialized(struct bpf_verifier_env *env, |
| 2702 | + int regno, int off, int access_size, |
| 2703 | + bool zero_size_allowed, |
| 2704 | + enum stack_access_src type, |
| 2705 | + struct bpf_call_arg_meta *meta); |
| 2706 | + |
| 2707 | +static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 2708 | +{ |
| 2709 | + return cur_regs(env) + regno; |
| 2710 | +} |
| 2711 | + |
| 2712 | +/* Read the stack at 'ptr_regno + off' and put the result into the register |
| 2713 | + * 'dst_regno'. |
| 2714 | + * 'off' includes the pointer register's fixed offset(i.e. 'ptr_regno.off'), |
| 2715 | + * but not its variable offset. |
| 2716 | + * 'size' is assumed to be <= reg size and the access is assumed to be aligned. |
| 2717 | + * |
| 2718 | + * As opposed to check_stack_read_fixed_off, this function doesn't deal with |
| 2719 | + * filling registers (i.e. reads of spilled register cannot be detected when |
| 2720 | + * the offset is not fixed). We conservatively mark 'dst_regno' as containing |
| 2721 | + * SCALAR_VALUE. That's why we assert that the 'ptr_regno' has a variable |
| 2722 | + * offset; for a fixed offset check_stack_read_fixed_off should be used |
| 2723 | + * instead. |
| 2724 | + */ |
| 2725 | +static int check_stack_read_var_off(struct bpf_verifier_env *env, |
| 2726 | + int ptr_regno, int off, int size, int dst_regno) |
| 2509 | 2727 | { |
| 2510 | | − /* Stack accesses must be at a fixed offset, so that we |
| 2511 | | − * can determine what type of data were returned. See |
| 2512 | | − * check_stack_read(). |
| 2728 | + /* The state of the source register. */ |
| 2729 | + struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 2730 | + struct bpf_func_state *ptr_state = func(env, reg); |
| 2731 | + int err; |
| 2732 | + int min_off, max_off; |
| 2733 | + |
| 2734 | + /* Note that we pass a NULL meta, so raw access will not be permitted. |
| 2513 | 2735 | */ |
| 2514 | | − if (!tnum_is_const(reg->var_off)) { |
| 2736 | + err = check_stack_range_initialized(env, ptr_regno, off, size, |
| 2737 | + false, ACCESS_DIRECT, NULL); |
| 2738 | + if (err) |
| 2739 | + return err; |
| 2740 | + |
| 2741 | + min_off = reg->smin_value + off; |
| 2742 | + max_off = reg->smax_value + off; |
| 2743 | + mark_reg_stack_read(env, ptr_state, min_off, max_off + size, dst_regno); |
| 2744 | + return 0; |
| 2745 | +} |
| 2746 | + |
| 2747 | +/* check_stack_read dispatches to check_stack_read_fixed_off or |
| 2748 | + * check_stack_read_var_off. |
| 2749 | + * |
| 2750 | + * The caller must ensure that the offset falls within the allocated stack |
| 2751 | + * bounds. |
| 2752 | + * |
| 2753 | + * 'dst_regno' is a register which will receive the value from the stack. It |
| 2754 | + * can be -1, meaning that the read value is not going to a register. |
| 2755 | + */ |
| 2756 | +static int check_stack_read(struct bpf_verifier_env *env, |
| 2757 | + int ptr_regno, int off, int size, |
| 2758 | + int dst_regno) |
| 2759 | +{ |
| 2760 | + struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 2761 | + struct bpf_func_state *state = func(env, reg); |
| 2762 | + int err; |
| 2763 | + /* Some accesses are only permitted with a static offset. */ |
| 2764 | + bool var_off = !tnum_is_const(reg->var_off); |
| 2765 | + |
| 2766 | + /* The offset is required to be static when reads don't go to a |
| 2767 | + * register, in order to not leak pointers (see |
| 2768 | + * check_stack_read_fixed_off). |
| 2769 | + */ |
| 2770 | + if (dst_regno < 0 && var_off) { |
| 2515 | 2771 | char tn_buf[48]; |
| 2516 | 2772 | |
| 2517 | 2773 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2518 | | − verbose(env, "variable stack access var_off=%s off=%d size=%d\n", |
| 2774 | + verbose(env, "variable offset stack pointer cannot be passed into helper function; var_off=%s off=%d size=%d\n", |
| 2519 | 2775 | tn_buf, off, size); |
| 2520 | 2776 | return -EACCES; |
| 2521 | 2777 | } |
| 2778 | + /* Variable offset is prohibited for unprivileged mode for simplicity |
| 2779 | + * since it requires corresponding support in Spectre masking for stack |
| 2780 | + * ALU. See also retrieve_ptr_limit(). |
| 2781 | + */ |
| 2782 | + if (!env->bypass_spec_v1 && var_off) { |
| 2783 | + char tn_buf[48]; |
| 2522 | 2784 | |
| 2523 | | − if (off >= 0 || off < -MAX_BPF_STACK) { |
| 2524 | | − verbose(env, "invalid stack off=%d size=%d\n", off, size); |
| 2785 | + tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 2786 | + verbose(env, "R%d variable offset stack access prohibited for !root, var_off=%s\n", |
| 2787 | + ptr_regno, tn_buf); |
| 2525 | 2788 | return -EACCES; |
| 2526 | 2789 | } |
| 2527 | 2790 | |
| 2528 | | − return 0; |
| 2791 | + if (!var_off) { |
| 2792 | + off += reg->var_off.value; |
| 2793 | + err = check_stack_read_fixed_off(env, state, off, size, |
| 2794 | + dst_regno); |
| 2795 | + } else { |
| 2796 | + /* Variable offset stack reads need more conservative handling |
| 2797 | + * than fixed offset ones. Note that dst_regno >= 0 on this |
| 2798 | + * branch. |
| 2799 | + */ |
| 2800 | + err = check_stack_read_var_off(env, ptr_regno, off, size, |
| 2801 | + dst_regno); |
| 2802 | + } |
| 2803 | + return err; |
| 2804 | +} |
| 2805 | + |
| 2806 | + |
| 2807 | +/* check_stack_write dispatches to check_stack_write_fixed_off or |
| 2808 | + * check_stack_write_var_off. |
| 2809 | + * |
| 2810 | + * 'ptr_regno' is the register used as a pointer into the stack. |
| 2811 | + * 'off' includes 'ptr_regno->off', but not its variable offset (if any). |
| 2812 | + * 'value_regno' is the register whose value we're writing to the stack. It can |
| 2813 | + * be -1, meaning that we're not writing from a register. |
| 2814 | + * |
| 2815 | + * The caller must ensure that the offset falls within the maximum stack size. |
| 2816 | + */ |
| 2817 | +static int check_stack_write(struct bpf_verifier_env *env, |
| 2818 | + int ptr_regno, int off, int size, |
| 2819 | + int value_regno, int insn_idx) |
| 2820 | +{ |
| 2821 | + struct bpf_reg_state *reg = reg_state(env, ptr_regno); |
| 2822 | + struct bpf_func_state *state = func(env, reg); |
| 2823 | + int err; |
| 2824 | + |
| 2825 | + if (tnum_is_const(reg->var_off)) { |
| 2826 | + off += reg->var_off.value; |
| 2827 | + err = check_stack_write_fixed_off(env, state, off, size, |
| 2828 | + value_regno, insn_idx); |
| 2829 | + } else { |
| 2830 | + /* Variable offset stack reads need more conservative handling |
| 2831 | + * than fixed offset ones. |
| 2832 | + */ |
| 2833 | + err = check_stack_write_var_off(env, state, |
| 2834 | + ptr_regno, off, size, |
| 2835 | + value_regno, insn_idx); |
| 2836 | + } |
| 2837 | + return err; |
| 2529 | 2838 | } |
| 2530 | 2839 | |
| 2531 | 2840 | static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, |
| @@ -2858,11 +3167,6 @@ static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, · 326 unmodified lines |
| 2858 | 3167 | return -EACCES; |
| 2859 | 3168 | } |
| 2860 | 3169 | |
| 2861 | | −static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) |
| 2862 | | −{ |
| 2863 | | − return cur_regs(env) + regno; |
| 2864 | | −} |
| 2865 | | − |
| 2866 | 3170 | static bool is_pointer_value(struct bpf_verifier_env *env, int regno) |
| 2867 | 3171 | { |
| 2868 | 3172 | return __is_pointer_value(env->allow_ptr_leaks, reg_state(env, regno)); |
| @@ -2981,8 +3285,8 @@ static int check_ptr_alignment(struct bpf_verifier_env *env, · 112 unmodified lines |
| 2981 | 3285 | break; |
| 2982 | 3286 | case PTR_TO_STACK: |
| 2983 | 3287 | pointer_desc = "stack "; |
| 2984 | | − /* The stack spill tracking logic in check_stack_write() |
| 2985 | | − * and check_stack_read() relies on stack accesses being |
| 3288 | + /* The stack spill tracking logic in check_stack_write_fixed_off() |
| 3289 | + * and check_stack_read_fixed_off() relies on stack accesses being |
| 2986 | 3290 | * aligned. |
| 2987 | 3291 | */ |
| 2988 | 3292 | strict = true; |
| @@ -3074,9 +3378,7 @@ process_func: · 85 unmodified lines |
| 3074 | 3378 | continue_func: |
| 3075 | 3379 | subprog_end = subprog[idx + 1].start; |
| 3076 | 3380 | for (; i < subprog_end; i++) { |
| 3077 | | − if (insn[i].code != (BPF_JMP | BPF_CALL)) |
| 3078 | | − continue; |
| 3079 | | − if (insn[i].src_reg != BPF_PSEUDO_CALL) |
| 3381 | + if (!bpf_pseudo_call(insn + i)) |
| 3080 | 3382 | continue; |
| 3081 | 3383 | /* remember insn and function to return to */ |
| 3082 | 3384 | ret_insn[frame] = i + 1; |
| @@ -3400,6 +3702,91 @@ static int check_ptr_to_map_access(struct bpf_verifier_env *env, · 317 unmodified lines |
| 3400 | 3702 | return 0; |
| 3401 | 3703 | } |
| 3402 | 3704 | |
| 3705 | +/* Check that the stack access at the given offset is within bounds. The |
| 3706 | + * maximum valid offset is -1. |
| 3707 | + * |
| 3708 | + * The minimum valid offset is -MAX_BPF_STACK for writes, and |
| 3709 | + * -state->allocated_stack for reads. |
| 3710 | + */ |
| 3711 | +static int check_stack_slot_within_bounds(int off, |
| 3712 | + struct bpf_func_state *state, |
| 3713 | + enum bpf_access_type t) |
| 3714 | +{ |
| 3715 | + int min_valid_off; |
| 3716 | + |
| 3717 | + if (t == BPF_WRITE) |
| 3718 | + min_valid_off = -MAX_BPF_STACK; |
| 3719 | + else |
| 3720 | + min_valid_off = -state->allocated_stack; |
| 3721 | + |
| 3722 | + if (off < min_valid_off || off > -1) |
| 3723 | + return -EACCES; |
| 3724 | + return 0; |
| 3725 | +} |
| 3726 | + |
| 3727 | +/* Check that the stack access at 'regno + off' falls within the maximum stack |
| 3728 | + * bounds. |
| 3729 | + * |
| 3730 | + * 'off' includes `regno->offset`, but not its dynamic part (if any). |
| 3731 | + */ |
| 3732 | +static int check_stack_access_within_bounds( |
| 3733 | + struct bpf_verifier_env *env, |
| 3734 | + int regno, int off, int access_size, |
| 3735 | + enum stack_access_src src, enum bpf_access_type type) |
| 3736 | +{ |
| 3737 | + struct bpf_reg_state *regs = cur_regs(env); |
| 3738 | + struct bpf_reg_state *reg = regs + regno; |
| 3739 | + struct bpf_func_state *state = func(env, reg); |
| 3740 | + int min_off, max_off; |
| 3741 | + int err; |
| 3742 | + char *err_extra; |
| 3743 | + |
| 3744 | + if (src == ACCESS_HELPER) |
| 3745 | + /* We don't know if helpers are reading or writing (or both). */ |
| 3746 | + err_extra = " indirect access to"; |
| 3747 | + else if (type == BPF_READ) |
| 3748 | + err_extra = " read from"; |
| 3749 | + else |
| 3750 | + err_extra = " write to"; |
| 3751 | + |
| 3752 | + if (tnum_is_const(reg->var_off)) { |
| 3753 | + min_off = reg->var_off.value + off; |
| 3754 | + if (access_size > 0) |
| 3755 | + max_off = min_off + access_size - 1; |
| 3756 | + else |
| 3757 | + max_off = min_off; |
| 3758 | + } else { |
| 3759 | + if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 3760 | + reg->smin_value <= -BPF_MAX_VAR_OFF) { |
| 3761 | + verbose(env, "invalid unbounded variable-offset%s stack R%d\n", |
| 3762 | + err_extra, regno); |
| 3763 | + return -EACCES; |
| 3764 | + } |
| 3765 | + min_off = reg->smin_value + off; |
| 3766 | + if (access_size > 0) |
| 3767 | + max_off = reg->smax_value + off + access_size - 1; |
| 3768 | + else |
| 3769 | + max_off = min_off; |
| 3770 | + } |
| 3771 | + |
| 3772 | + err = check_stack_slot_within_bounds(min_off, state, type); |
| 3773 | + if (!err) |
| 3774 | + err = check_stack_slot_within_bounds(max_off, state, type); |
| 3775 | + |
| 3776 | + if (err) { |
| 3777 | + if (tnum_is_const(reg->var_off)) { |
| 3778 | + verbose(env, "invalid%s stack R%d off=%d size=%d\n", |
| 3779 | + err_extra, regno, off, access_size); |
| 3780 | + } else { |
| 3781 | + char tn_buf[48]; |
| 3782 | + |
| 3783 | + tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3784 | + verbose(env, "invalid variable-offset%s stack R%d var_off=%s size=%d\n", |
| 3785 | + err_extra, regno, tn_buf, access_size); |
| 3786 | + } |
| 3787 | + } |
| 3788 | + return err; |
| 3789 | +} |
| 3403 | 3790 | |
| 3404 | 3791 | /* check whether memory at (regno + off) is accessible for t = (read | write) |
| 3405 | 3792 | * if t==write, value_regno is a register which value is stored into memory |
| @@ -3515,8 +3902,8 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · 109 unmodified lines |
| 3515 | 3902 | } |
| 3516 | 3903 | |
| 3517 | 3904 | } else if (reg->type == PTR_TO_STACK) { |
| 3518 | | − off += reg->var_off.value; |
| 3519 | | − err = check_stack_access(env, reg, off, size); |
| 3905 | + /* Basic bounds checks. */ |
| 3906 | + err = check_stack_access_within_bounds(env, regno, off, size, ACCESS_DIRECT, t); |
| 3520 | 3907 | if (err) |
| 3521 | 3908 | return err; |
| 3522 | 3909 | |
| @@ -3525,12 +3912,12 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · 2 unmodified lines |
| 3525 | 3912 | if (err) |
| 3526 | 3913 | return err; |
| 3527 | 3914 | |
| 3528 | | − if (t == BPF_WRITE) |
| 3529 | | − err = check_stack_write(env, state, off, size, |
| 3530 | | − value_regno, insn_idx); |
| 3531 | | − else |
| 3532 | | − err = check_stack_read(env, state, off, size, |
| 3915 | + if (t == BPF_READ) |
| 3916 | + err = check_stack_read(env, regno, off, size, |
| 3533 | 3917 | value_regno); |
| 3918 | + else |
| 3919 | + err = check_stack_write(env, regno, off, size, |
| 3920 | + value_regno, insn_idx); |
| 3534 | 3921 | } else if (reg_is_pkt_pointer(reg)) { |
| 3535 | 3922 | if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) { |
| 3536 | 3923 | verbose(env, "cannot write into packet\n"); |
| @@ -3665,9 +4052,26 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i · 128 unmodified lines |
| 3665 | 4052 | return -EACCES; |
| 3666 | 4053 | } |
| 3667 | 4054 | |
| 4055 | + if (insn->imm & BPF_FETCH) { |
| 4056 | + if (insn->imm == BPF_CMPXCHG) |
| 4057 | + load_reg = BPF_REG_0; |
| 4058 | + else |
| 4059 | + load_reg = insn->src_reg; |
| 4060 | + |
| 4061 | + /* check and record load of old value */ |
| 4062 | + err = check_reg_arg(env, load_reg, DST_OP); |
| 4063 | + if (err) |
| 4064 | + return err; |
| 4065 | + } else { |
| 4066 | + /* This instruction accesses a memory location but doesn't |
| 4067 | + * actually load it into a register. |
| 4068 | + */ |
| 4069 | + load_reg = -1; |
| 4070 | + } |
| 4071 | + |
| 3668 | 4072 | /* check whether we can read the memory */ |
| 3669 | 4073 | err = check_mem_access(env, insn_idx, insn->dst_reg, insn->off, |
| 3670 | | − BPF_SIZE(insn->code), BPF_READ, -1, true); |
| 4074 | + BPF_SIZE(insn->code), BPF_READ, load_reg, true); |
| 3671 | 4075 | if (err) |
| 3672 | 4076 | return err; |
| 3673 | 4077 | |
| @@ -3677,65 +4081,56 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i · 3 unmodified lines |
| 3677 | 4081 | if (err) |
| 3678 | 4082 | return err; |
| 3679 | 4083 | |
| 3680 | | − if (!(insn->imm & BPF_FETCH)) |
| 3681 | | − return 0; |
| 3682 | | − |
| 3683 | | − if (insn->imm == BPF_CMPXCHG) |
| 3684 | | − load_reg = BPF_REG_0; |
| 3685 | | − else |
| 3686 | | − load_reg = insn->src_reg; |
| 3687 | | − |
| 3688 | | − /* check and record load of old value */ |
| 3689 | | − err = check_reg_arg(env, load_reg, DST_OP); |
| 3690 | | − if (err) |
| 3691 | | − return err; |
| 3692 | | − |
| 3693 | 4084 | return 0; |
| 3694 | 4085 | } |
| 3695 | 4086 | |
| 3696 | | −static int __check_stack_boundary(struct bpf_verifier_env *env, u32 regno, |
| 3697 | | − int off, int access_size, |
| 3698 | | − bool zero_size_allowed) |
| 4087 | +/* When register 'regno' is used to read the stack (either directly or through |
| 4088 | + * a helper function) make sure that it's within stack boundary and, depending |
| 4089 | + * on the access type, that all elements of the stack are initialized. |
| 4090 | + * |
| 4091 | + * 'off' includes 'regno->off', but not its dynamic part (if any). |
| 4092 | + * |
| 4093 | + * All registers that have been spilled on the stack in the slots within the |
| 4094 | + * read offsets are marked as read. |
| 4095 | + */ |
| 4096 | +static int check_stack_range_initialized( |
| 4097 | + struct bpf_verifier_env *env, int regno, int off, |
| 4098 | + int access_size, bool zero_size_allowed, |
| 4099 | + enum stack_access_src type, struct bpf_call_arg_meta *meta) |
| 3699 | 4100 | { |
| 3700 | 4101 | struct bpf_reg_state *reg = reg_state(env, regno); |
| 4102 | + struct bpf_func_state *state = func(env, reg); |
| 4103 | + int err, min_off, max_off, i, j, slot, spi; |
| 4104 | + char *err_extra = type == ACCESS_HELPER ? " indirect" : ""; |
| 4105 | + enum bpf_access_type bounds_check_type; |
| 4106 | + /* Some accesses can write anything into the stack, others are |
| 4107 | + * read-only. |
| 4108 | + */ |
| 4109 | + bool clobber = false; |
| 3701 | 4110 | |
| 3702 | | − if (off >= 0 || off < -MAX_BPF_STACK || off + access_size > 0 || |
| 3703 | | − access_size < 0 || (access_size == 0 && !zero_size_allowed)) { |
| 3704 | | − if (tnum_is_const(reg->var_off)) { |
| 3705 | | − verbose(env, "invalid stack type R%d off=%d access_size=%d\n", |
| 3706 | | − regno, off, access_size); |
| 3707 | | − } else { |
| 3708 | | − char tn_buf[48]; |
| 3709 | | − |
| 3710 | | − tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3711 | | − verbose(env, "invalid stack type R%d var_off=%s access_size=%d\n", |
| 3712 | | − regno, tn_buf, access_size); |
| 3713 | | − } |
| 4111 | + if (access_size == 0 && !zero_size_allowed) { |
| 4112 | + verbose(env, "invalid zero-sized read\n"); |
| 3714 | 4113 | return -EACCES; |
| 3715 | 4114 | } |
| 3716 | | − return 0; |
| 3717 | | −} |
| 3718 | 4115 | |
| 3719 | | −/* when register 'regno' is passed into function that will read 'access_size' |
| 3720 | | − * bytes from that pointer, make sure that it's within stack boundary |
| 3721 | | − * and all elements of stack are initialized. |
| 3722 | | − * Unlike most pointer bounds-checking functions, this one doesn't take an |
| 3723 | | − * 'off' argument, so it has to add in reg->off itself. |
| 3724 | | − */ |
| 3725 | | −static int check_stack_boundary(struct bpf_verifier_env *env, int regno, |
| 3726 | | − int access_size, bool zero_size_allowed, |
| 3727 | | − struct bpf_call_arg_meta *meta) |
| 3728 | | −{ |
| 3729 | | − struct bpf_reg_state *reg = reg_state(env, regno); |
| 3730 | | − struct bpf_func_state *state = func(env, reg); |
| 3731 | | − int err, min_off, max_off, i, j, slot, spi; |
| 4116 | + if (type == ACCESS_HELPER) { |
| 4117 | + /* The bounds checks for writes are more permissive than for |
| 4118 | + * reads. However, if raw_mode is not set, we'll do extra |
| 4119 | + * checks below. |
| 4120 | + */ |
| 4121 | + bounds_check_type = BPF_WRITE; |
| 4122 | + clobber = true; |
| 4123 | + } else { |
| 4124 | + bounds_check_type = BPF_READ; |
| 4125 | + } |
| 4126 | + err = check_stack_access_within_bounds(env, regno, off, access_size, |
| 4127 | + type, bounds_check_type); |
| 4128 | + if (err) |
| 4129 | + return err; |
| 4130 | + |
| 3732 | 4131 | |
| 3733 | 4132 | if (tnum_is_const(reg->var_off)) { |
| 3734 | | − min_off = max_off = reg->var_off.value + reg->off; |
| 3735 | | − err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3736 | | − zero_size_allowed); |
| 3737 | | − if (err) |
| 3738 | | − return err; |
| 4133 | + min_off = max_off = reg->var_off.value + off; |
| 3739 | 4134 | } else { |
| 3740 | 4135 | /* Variable offset is prohibited for unprivileged mode for |
| 3741 | 4136 | * simplicity since it requires corresponding support in |
| @@ -3746,8 +4141,8 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno, · 4 unmodified lines |
| 3746 | 4141 | char tn_buf[48]; |
| 3747 | 4142 | |
| 3748 | 4143 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3749 | | − verbose(env, "R%d indirect variable offset stack access prohibited for !root, var_off=%s\n", |
| 3750 | | − regno, tn_buf); |
| 4144 | + verbose(env, "R%d%s variable offset stack access prohibited for !root, var_off=%s\n", |
| 4145 | + regno, err_extra, tn_buf); |
| 3751 | 4146 | return -EACCES; |
| 3752 | 4147 | } |
| 3753 | 4148 | /* Only initialized buffer on stack is allowed to be accessed |
| @@ -3759,28 +4154,8 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno, · 5 unmodified lines |
| 3759 | 4154 | if (meta && meta->raw_mode) |
| 3760 | 4155 | meta = NULL; |
| 3761 | 4156 | |
| 3762 | | − if (reg->smax_value >= BPF_MAX_VAR_OFF || |
| 3763 | | − reg->smax_value <= -BPF_MAX_VAR_OFF) { |
| 3764 | | − verbose(env, "R%d unbounded indirect variable offset stack access\n", |
| 3765 | | − regno); |
| 3766 | | − return -EACCES; |
| 3767 | | − } |
| 3768 | | − min_off = reg->smin_value + reg->off; |
| 3769 | | − max_off = reg->smax_value + reg->off; |
| 3770 | | − err = __check_stack_boundary(env, regno, min_off, access_size, |
| 3771 | | − zero_size_allowed); |
| 3772 | | − if (err) { |
| 3773 | | − verbose(env, "R%d min value is outside of stack bound\n", |
| 3774 | | − regno); |
| 3775 | | − return err; |
| 3776 | | − } |
| 3777 | | − err = __check_stack_boundary(env, regno, max_off, access_size, |
| 3778 | | − zero_size_allowed); |
| 3779 | | − if (err) { |
| 3780 | | − verbose(env, "R%d max value is outside of stack bound\n", |
| 3781 | | − regno); |
| 3782 | | − return err; |
| 3783 | | − } |
| 4157 | + min_off = reg->smin_value + off; |
| 4158 | + max_off = reg->smax_value + off; |
| 3784 | 4159 | } |
| 3785 | 4160 | |
| 3786 | 4161 | if (meta && meta->raw_mode) { |
| @@ -3800,8 +4175,10 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno, · 13 unmodified lines |
| 3800 | 4175 | if (*stype == STACK_MISC) |
| 3801 | 4176 | goto mark; |
| 3802 | 4177 | if (*stype == STACK_ZERO) { |
| 3803 | | − /* helper can write anything into the stack */ |
| 3804 | | − *stype = STACK_MISC; |
| 4178 | + if (clobber) { |
| 4179 | + /* helper can write anything into the stack */ |
| 4180 | + *stype = STACK_MISC; |
| 4181 | + } |
| 3805 | 4182 | goto mark; |
| 3806 | 4183 | } |
| 3807 | 4184 | |
| @@ -3812,22 +4189,24 @@ static int check_stack_boundary(struct bpf_verifier_env *env, int regno, · 4 unmodified lines |
| 3812 | 4189 | if (state->stack[spi].slot_type[0] == STACK_SPILL && |
| 3813 | 4190 | (state->stack[spi].spilled_ptr.type == SCALAR_VALUE || |
| 3814 | 4191 | env->allow_ptr_leaks)) { |
| 3815 | | − __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
| 3816 | | − for (j = 0; j < BPF_REG_SIZE; j++) |
| 3817 | | − state->stack[spi].slot_type[j] = STACK_MISC; |
| 4192 | + if (clobber) { |
| 4193 | + __mark_reg_unknown(env, &state->stack[spi].spilled_ptr); |
| 4194 | + for (j = 0; j < BPF_REG_SIZE; j++) |
| 4195 | + state->stack[spi].slot_type[j] = STACK_MISC; |
| 4196 | + } |
| 3818 | 4197 | goto mark; |
| 3819 | 4198 | } |
| 3820 | 4199 | |
| 3821 | 4200 | err: |
| 3822 | 4201 | if (tnum_is_const(reg->var_off)) { |
| 3823 | | − verbose(env, "invalid indirect read from stack off %d+%d size %d\n", |
| 3824 | | − min_off, i - min_off, access_size); |
| 4202 | + verbose(env, "invalid%s read from stack R%d off %d+%d size %d\n", |
| 4203 | + err_extra, regno, min_off, i - min_off, access_size); |
| 3825 | 4204 | } else { |
| 3826 | 4205 | char tn_buf[48]; |
| 3827 | 4206 | |
| 3828 | 4207 | tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 3829 | | − verbose(env, "invalid indirect read from stack var_off %s+%d size %d\n", |
| 3830 | | − tn_buf, i - min_off, access_size); |
| 4208 | + verbose(env, "invalid%s read from stack R%d var_off %s+%d size %d\n", |
| 4209 | + err_extra, regno, tn_buf, i - min_off, access_size); |
| 3831 | 4210 | } |
| 3832 | 4211 | return -EACCES; |
| 3833 | 4212 | mark: |
| @@ -3876,8 +4255,10 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, · 42 unmodified lines |
| 3876 | 4255 | "rdwr", |
| 3877 | 4256 | &env->prog->aux->max_rdwr_access); |
| 3878 | 4257 | case PTR_TO_STACK: |
| 3879 | | − return check_stack_boundary(env, regno, access_size, |
| 3880 | | − zero_size_allowed, meta); |
| 4258 | + return check_stack_range_initialized( |
| 4259 | + env, |
| 4260 | + regno, reg->off, access_size, |
| 4261 | + zero_size_allowed, ACCESS_HELPER, meta); |
| 3881 | 4262 | default: /* scalar_value or invalid ptr */ |
| 3882 | 4263 | /* Allow zero-byte read from NULL, regardless of pointer type */ |
| 3883 | 4264 | if (zero_size_allowed && access_size == 0 && |
| @@ -3891,6 +4272,29 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, · 7 unmodified lines |
| 3891 | 4272 | } |
| 3892 | 4273 | } |
| 3893 | 4274 | |
| 4275 | +int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, |
| 4276 | + u32 regno, u32 mem_size) |
| 4277 | +{ |
| 4278 | + if (register_is_null(reg)) |
| 4279 | + return 0; |
| 4280 | + |
| 4281 | + if (reg_type_may_be_null(reg->type)) { |
| 4282 | + /* Assuming that the register contains a value check if the memory |
| 4283 | + * access is safe. Temporarily save and restore the register's state as |
| 4284 | + * the conversion shouldn't be visible to a caller. |
| 4285 | + */ |
| 4286 | + const struct bpf_reg_state saved_reg = *reg; |
| 4287 | + int rv; |
| 4288 | + |
| 4289 | + mark_ptr_not_null_reg(reg); |
| 4290 | + rv = check_helper_mem_access(env, regno, mem_size, true, NULL); |
| 4291 | + *reg = saved_reg; |
| 4292 | + return rv; |
| 4293 | + } |
| 4294 | + |
| 4295 | + return check_helper_mem_access(env, regno, mem_size, true, NULL); |
| 4296 | +} |
| 4297 | + |
| 3894 | 4298 | /* Implementation details: |
| 3895 | 4299 | * bpf_map_lookup returns PTR_TO_MAP_VALUE_OR_NULL |
| 3896 | 4300 | * Two bpf_map_lookups (even with the same key) will have different reg->id. |
| @@ -4875,8 +5279,9 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · 978 unmodified lines |
| 4875 | 5279 | subprog); |
| 4876 | 5280 | clear_caller_saved_regs(env, caller->regs); |
| 4877 | 5281 | |
| 4878 | | − /* All global functions return SCALAR_VALUE */ |
| 5282 | + /* All global functions return a 64-bit SCALAR_VALUE */ |
| 4879 | 5283 | mark_reg_unknown(env, caller->regs, BPF_REG_0); |
| 5284 | + caller->regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG; |
| 4880 | 5285 | |
| 4881 | 5286 | /* continue with next insn after call */ |
| 4882 | 5287 | return 0; |
| @@ -5541,6 +5946,41 @@ do_sim: · 658 unmodified lines |
| 5541 | 5946 | return !ret ? -EFAULT : 0; |
| 5542 | 5947 | } |
| 5543 | 5948 | |
| 5949 | +/* check that stack access falls within stack limits and that 'reg' doesn't |
| 5950 | + * have a variable offset. |
| 5951 | + * |
| 5952 | + * Variable offset is prohibited for unprivileged mode for simplicity since it |
| 5953 | + * requires corresponding support in Spectre masking for stack ALU. See also |
| 5954 | + * retrieve_ptr_limit(). |
| 5955 | + * |
| 5956 | + * |
| 5957 | + * 'off' includes 'reg->off'. |
| 5958 | + */ |
| 5959 | +static int check_stack_access_for_ptr_arithmetic( |
| 5960 | + struct bpf_verifier_env *env, |
| 5961 | + int regno, |
| 5962 | + const struct bpf_reg_state *reg, |
| 5963 | + int off) |
| 5964 | +{ |
| 5965 | + if (!tnum_is_const(reg->var_off)) { |
| 5966 | + char tn_buf[48]; |
| 5967 | + |
| 5968 | + tnum_strn(tn_buf, sizeof(tn_buf), reg->var_off); |
| 5969 | + verbose(env, "R%d variable stack access prohibited for !root, var_off=%s off=%d\n", |
| 5970 | + regno, tn_buf, off); |
| 5971 | + return -EACCES; |
| 5972 | + } |
| 5973 | + |
| 5974 | + if (off >= 0 || off < -MAX_BPF_STACK) { |
| 5975 | + verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 5976 | + "prohibited for !root; off=%d\n", regno, off); |
| 5977 | + return -EACCES; |
| 5978 | + } |
| 5979 | + |
| 5980 | + return 0; |
| 5981 | +} |
| 5982 | + |
| 5983 | + |
| 5544 | 5984 | /* Handles arithmetic on a pointer and a scalar: computes new min/max and var_off. |
| 5545 | 5985 | * Caller should also handle BPF_MOV case separately. |
| 5546 | 5986 | * If we return -EACCES, caller may want to try again treating pointer as a |
| @@ -5784,10 +6224,9 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · 237 unmodified lines |
| 5784 | 6224 | "prohibited for !root\n", dst); |
| 5785 | 6225 | return -EACCES; |
| 5786 | 6226 | } else if (dst_reg->type == PTR_TO_STACK && |
| 5787 | | − check_stack_access(env, dst_reg, dst_reg->off + |
| 5788 | | − dst_reg->var_off.value, 1)) { |
| 5789 | | − verbose(env, "R%d stack pointer arithmetic goes out of range, " |
| 5790 | | − "prohibited for !root\n", dst); |
| 6227 | + check_stack_access_for_ptr_arithmetic( |
| 6228 | + env, dst, dst_reg, dst_reg->off + |
| 6229 | + dst_reg->var_off.value)) { |
| 5791 | 6230 | return -EACCES; |
| 5792 | 6231 | } |
| 5793 | 6232 | } |
| @@ -6266,7 +6705,7 @@ static void scalar32_min_max_rsh(struct bpf_reg_state *dst_reg, · 472 unmodified lines |
| 6266 | 6705 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6267 | 6706 | * about the result |
| 6268 | 6707 | * If the value in dst_reg is known nonnegative, then again the |
| 6269 | | − * unsigned bounts capture the signed bounds. |
| 6708 | + * unsigned bounds capture the signed bounds. |
| 6270 | 6709 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6271 | 6710 | * and rely on inferring new ones from the unsigned bounds and |
| 6272 | 6711 | * var_off of the result. |
| @@ -6297,7 +6736,7 @@ static void scalar_min_max_rsh(struct bpf_reg_state *dst_reg, · 24 unmodified lines |
| 6297 | 6736 | * 3) the signed bounds cross zero, so they tell us nothing |
| 6298 | 6737 | * about the result |
| 6299 | 6738 | * If the value in dst_reg is known nonnegative, then again the |
| 6300 | | − * unsigned bounts capture the signed bounds. |
| 6739 | + * unsigned bounds capture the signed bounds. |
| 6301 | 6740 | * Thus, in all cases it suffices to blow away our signed bounds |
| 6302 | 6741 | * and rely on inferring new ones from the unsigned bounds and |
| 6303 | 6742 | * var_off of the result. |
| @@ -7367,43 +7806,19 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state, · 1063 unmodified lines |
| 7367 | 7806 | } |
| 7368 | 7807 | if (is_null) { |
| 7369 | 7808 | reg->type = SCALAR_VALUE; |
| 7370 | | − } else if (reg->type == PTR_TO_MAP_VALUE_OR_NULL) { |
| 7371 | | − const struct bpf_map *map = reg->map_ptr; |
| 7372 | | − |
| 7373 | | − if (map->inner_map_meta) { |
| 7374 | | − reg->type = CONST_PTR_TO_MAP; |
| 7375 | | − reg->map_ptr = map->inner_map_meta; |
| 7376 | | − } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { |
| 7377 | | − reg->type = PTR_TO_XDP_SOCK; |
| 7378 | | − } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || |
| 7379 | | − map->map_type == BPF_MAP_TYPE_SOCKHASH) { |
| 7380 | | − reg->type = PTR_TO_SOCKET; |
| 7381 | | − } else { |
| 7382 | | − reg->type = PTR_TO_MAP_VALUE; |
| 7383 | | − } |
| 7384 | | − } else if (reg->type == PTR_TO_SOCKET_OR_NULL) { |
| 7385 | | − reg->type = PTR_TO_SOCKET; |
| 7386 | | − } else if (reg->type == PTR_TO_SOCK_COMMON_OR_NULL) { |
| 7387 | | − reg->type = PTR_TO_SOCK_COMMON; |
| 7388 | | − } else if (reg->type == PTR_TO_TCP_SOCK_OR_NULL) { |
| 7389 | | − reg->type = PTR_TO_TCP_SOCK; |
| 7390 | | − } else if (reg->type == PTR_TO_BTF_ID_OR_NULL) { |
| 7391 | | − reg->type = PTR_TO_BTF_ID; |
| 7392 | | − } else if (reg->type == PTR_TO_MEM_OR_NULL) { |
| 7393 | | − reg->type = PTR_TO_MEM; |
| 7394 | | − } else if (reg->type == PTR_TO_RDONLY_BUF_OR_NULL) { |
| 7395 | | − reg->type = PTR_TO_RDONLY_BUF; |
| 7396 | | − } else if (reg->type == PTR_TO_RDWR_BUF_OR_NULL) { |
| 7397 | | − reg->type = PTR_TO_RDWR_BUF; |
| 7398 | | − } |
| 7399 | | − if (is_null) { |
| 7400 | 7809 | /* We don't need id and ref_obj_id from this point |
| 7401 | 7810 | * onwards anymore, thus we should better reset it, |
| 7402 | 7811 | * so that state pruning has chances to take effect. |
| 7403 | 7812 | */ |
| 7404 | 7813 | reg->id = 0; |
| 7405 | 7814 | reg->ref_obj_id = 0; |
| 7406 | | − } else if (!reg_may_point_to_spin_lock(reg)) { |
| 7815 | + |
| 7816 | + return; |
| 7817 | + } |
| 7818 | + |
| 7819 | + mark_ptr_not_null_reg(reg); |
| 7820 | + |
| 7821 | + if (!reg_may_point_to_spin_lock(reg)) { |
| 7407 | 7822 | /* For not-NULL ptr, reg->ref_obj_id will be reset |
| 7408 | 7823 | * in release_reg_references(). |
| 7409 | 7824 | * |
| @@ -7986,6 +8401,9 @@ static int check_return_code(struct bpf_verifier_env *env) · 576 unmodified lines |
| 7986 | 8401 | env->prog->expected_attach_type == BPF_CGROUP_INET4_GETSOCKNAME || |
| 7987 | 8402 | env->prog->expected_attach_type == BPF_CGROUP_INET6_GETSOCKNAME) |
| 7988 | 8403 | range = tnum_range(1, 1); |
| 8404 | + if (env->prog->expected_attach_type == BPF_CGROUP_INET4_BIND || |
| 8405 | + env->prog->expected_attach_type == BPF_CGROUP_INET6_BIND) |
| 8406 | + range = tnum_range(0, 3); |
| 7989 | 8407 | break; |
| 7990 | 8408 | case BPF_PROG_TYPE_CGROUP_SKB: |
| 7991 | 8409 | if (env->prog->expected_attach_type == BPF_CGROUP_INET_EGRESS) { |
| @@ -10015,15 +10433,22 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env, · 2023 unmodified lines |
| 10015 | 10433 | case BPF_MAP_TYPE_HASH: |
| 10016 | 10434 | case BPF_MAP_TYPE_LRU_HASH: |
| 10017 | 10435 | case BPF_MAP_TYPE_ARRAY: |
| 10436 | + case BPF_MAP_TYPE_PERCPU_HASH: |
| 10437 | + case BPF_MAP_TYPE_PERCPU_ARRAY: |
| 10438 | + case BPF_MAP_TYPE_LRU_PERCPU_HASH: |
| 10439 | + case BPF_MAP_TYPE_ARRAY_OF_MAPS: |
| 10440 | + case BPF_MAP_TYPE_HASH_OF_MAPS: |
| 10018 | 10441 | if (!is_preallocated_map(map)) { |
| 10019 | 10442 | verbose(env, |
| 10020 | | − "Sleepable programs can only use preallocated hash maps\n"); |
| 10443 | + "Sleepable programs can only use preallocated maps\n"); |
| 10021 | 10444 | return -EINVAL; |
| 10022 | 10445 | } |
| 10023 | 10446 | break; |
| 10447 | + case BPF_MAP_TYPE_RINGBUF: |
| 10448 | + break; |
| 10024 | 10449 | default: |
| 10025 | 10450 | verbose(env, |
| 10026 | | − "Sleepable programs can only use array and hash maps\n"); |
| 10451 | + "Sleepable programs can only use array, hash, and ringbuf maps\n"); |
| 10027 | 10452 | return -EINVAL; |
| 10028 | 10453 | } |
| 10029 | 10454 | |
| @@ -10581,6 +11006,7 @@ static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, · 551 unmodified lines |
| 10581 | 11006 | for (i = 0; i < len; i++) { |
| 10582 | 11007 | int adj_idx = i + delta; |
| 10583 | 11008 | struct bpf_insn insn; |
| 11009 | + u8 load_reg; |
| 10584 | 11010 | |
| 10585 | 11011 | insn = insns[adj_idx]; |
| 10586 | 11012 | if (!aux[adj_idx].zext_dst) { |
| @@ -10623,9 +11049,27 @@ static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, · 36 unmodified lines |
| 10623 | 11049 | if (!bpf_jit_needs_zext()) |
| 10624 | 11050 | continue; |
| 10625 | 11051 | |
| 11052 | + /* zext_dst means that we want to zero-extend whatever register |
| 11053 | + * the insn defines, which is dst_reg most of the time, with |
| 11054 | + * the notable exception of BPF_STX + BPF_ATOMIC + BPF_FETCH. |
| 11055 | + */ |
| 11056 | + if (BPF_CLASS(insn.code) == BPF_STX && |
| 11057 | + BPF_MODE(insn.code) == BPF_ATOMIC) { |
| 11058 | + /* BPF_STX + BPF_ATOMIC insns without BPF_FETCH do not |
| 11059 | + * define any registers, therefore zext_dst cannot be |
| 11060 | + * set. |
| 11061 | + */ |
| 11062 | + if (WARN_ON(!(insn.imm & BPF_FETCH))) |
| 11063 | + return -EINVAL; |
| 11064 | + load_reg = insn.imm == BPF_CMPXCHG ? BPF_REG_0 |
| 11065 | + : insn.src_reg; |
| 11066 | + } else { |
| 11067 | + load_reg = insn.dst_reg; |
| 11068 | + } |
| 11069 | + |
| 10626 | 11070 | zext_patch[0] = insn; |
| 10627 | | − zext_patch[1].dst_reg = insn.dst_reg; |
| 10628 | | − zext_patch[1].src_reg = insn.dst_reg; |
| 11071 | + zext_patch[1].dst_reg = load_reg; |
| 11072 | + zext_patch[1].src_reg = load_reg; |
| 10629 | 11073 | patch = zext_patch; |
| 10630 | 11074 | patch_len = 2; |
| 10631 | 11075 | apply_patch_buffer: |
| @@ -10841,8 +11285,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) · 209 unmodified lines |
| 10841 | 11285 | return 0; |
| 10842 | 11286 | |
| 10843 | 11287 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 10844 | | − if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10845 | | − insn->src_reg != BPF_PSEUDO_CALL) |
| 11288 | + if (!bpf_pseudo_call(insn)) |
| 10846 | 11289 | continue; |
| 10847 | 11290 | /* Upon error here we cannot fall back to interpreter but |
| 10848 | 11291 | * need a hard reject of the program. Thus -EFAULT is |
| @@ -10883,7 +11326,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) · 34 unmodified lines |
| 10883 | 11326 | /* BPF_PROG_RUN doesn't call subprogs directly, |
| 10884 | 11327 | * hence main prog stats include the runtime of subprogs. |
| 10885 | 11328 | * subprogs don't have IDs and not reachable via prog_get_next_id |
| 10886 | | − * func[i]->aux->stats will never be accessed and stays NULL |
| 11329 | + * func[i]->stats will never be accessed and stays NULL |
| 10887 | 11330 | */ |
| 10888 | 11331 | func[i] = bpf_prog_alloc_no_stats(bpf_prog_size(len), GFP_USER); |
| 10889 | 11332 | if (!func[i]) |
| @@ -10971,8 +11414,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) · 81 unmodified lines |
| 10971 | 11414 | for (i = 0; i < env->subprog_cnt; i++) { |
| 10972 | 11415 | insn = func[i]->insnsi; |
| 10973 | 11416 | for (j = 0; j < func[i]->len; j++, insn++) { |
| 10974 | | − if (insn->code != (BPF_JMP | BPF_CALL) || |
| 10975 | | − insn->src_reg != BPF_PSEUDO_CALL) |
| 11417 | + if (!bpf_pseudo_call(insn)) |
| 10976 | 11418 | continue; |
| 10977 | 11419 | subprog = insn->off; |
| 10978 | 11420 | insn->imm = BPF_CAST_CALL(func[subprog]->bpf_func) - |
| @@ -11017,8 +11459,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) · 38 unmodified lines |
| 11017 | 11459 | * later look the same as if they were interpreted only. |
| 11018 | 11460 | */ |
| 11019 | 11461 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 11020 | | − if (insn->code != (BPF_JMP | BPF_CALL) || |
| 11021 | | − insn->src_reg != BPF_PSEUDO_CALL) |
| 11462 | + if (!bpf_pseudo_call(insn)) |
| 11022 | 11463 | continue; |
| 11023 | 11464 | insn->off = env->insn_aux_data[i].call_imm; |
| 11024 | 11465 | subprog = find_subprog(env, i + insn->off + 1); |
| @@ -11047,8 +11488,7 @@ out_undo_insn: · 22 unmodified lines |
| 11047 | 11488 | /* cleanup main prog to be interpreted */ |
| 11048 | 11489 | prog->jit_requested = 0; |
| 11049 | 11490 | for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) { |
| 11050 | | − if (insn->code != (BPF_JMP | BPF_CALL) || |
| 11051 | | − insn->src_reg != BPF_PSEUDO_CALL) |
| 11491 | + if (!bpf_pseudo_call(insn)) |
| 11052 | 11492 | continue; |
| 11053 | 11493 | insn->off = 0; |
| 11054 | 11494 | insn->imm = env->insn_aux_data[i].call_imm; |
| @@ -11083,8 +11523,7 @@ static int fixup_call_args(struct bpf_verifier_env *env) · 28 unmodified lines |
| 11083 | 11523 | return -EINVAL; |
| 11084 | 11524 | } |
| 11085 | 11525 | for (i = 0; i < prog->len; i++, insn++) { |
| 11086 | | − if (insn->code != (BPF_JMP | BPF_CALL) || |
| 11087 | | − insn->src_reg != BPF_PSEUDO_CALL) |
| 11526 | + if (!bpf_pseudo_call(insn)) |
| 11088 | 11527 | continue; |
| 11089 | 11528 | depth = get_callee_stack_depth(env, insn, i); |
| 11090 | 11529 | if (depth < 0) |
| @@ -11547,6 +11986,13 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog) · 456 unmodified lines |
| 11547 | 11986 | mark_reg_known_zero(env, regs, i); |
| 11548 | 11987 | else if (regs[i].type == SCALAR_VALUE) |
| 11549 | 11988 | mark_reg_unknown(env, regs, i); |
| 11989 | + else if (regs[i].type == PTR_TO_MEM_OR_NULL) { |
| 11990 | + const u32 mem_size = regs[i].mem_size; |
| 11991 | + |
| 11992 | + mark_reg_known_zero(env, regs, i); |
| 11993 | + regs[i].mem_size = mem_size; |
| 11994 | + regs[i].id = ++env->id_gen; |
| 11995 | + } |
| 11550 | 11996 | } |
| 11551 | 11997 | } else { |
| 11552 | 11998 | /* 1st arg to a function */ |
| @@ -12125,6 +12571,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, · 572 unmodified lines |
| 12125 | 12571 | env->strict_alignment = false; |
| 12126 | 12572 | |
| 12127 | 12573 | env->allow_ptr_leaks = bpf_allow_ptr_leaks(); |
| 12574 | + env->allow_uninit_stack = bpf_allow_uninit_stack(); |
| 12128 | 12575 | env->allow_ptr_to_map_access = bpf_allow_ptr_to_map_access(); |
| 12129 | 12576 | env->bypass_spec_v1 = bpf_bypass_spec_v1(); |
| 12130 | 12577 | env->bypass_spec_v4 = bpf_bypass_spec_v4(); |