From: Alexei Starovoitov A valid program can be rejected. No unsafe program is accepted, because the path-sensitive run accepts nothing. After a pass with an error, the run looks for one path to the error. It skips trips of a loop: it adds step * trips to each xreg that moves. It adds in 64 bits, but a 32-bit insn wraps: r7 = 0; for (i = 0; i < 100; i++) { x = r7; if (!flag) w7 += 0x4000000; } if (!flag) buf[x - 0x8c000000]; If flag is 0, x is 0x8c000000 after the loop. The skip gives 0x18c000000. No path has this value, but the access to buf fails with it. The error counts as real, so the budget of the next passes is cut. If the program needs more than that budget, it is rejected. If the skip moves a number past a multiple of 2^31, a 32-bit insn can wrap. If such an insn writes an xreg that the skip moves, and the upper half is live, mark the skip as a guess. A guess does not cut the budget. Fixes: a46de5d64e18 ("bpf: Reproduce an error on one path, with no joins") Signed-off-by: Alexei Starovoitov --- include/linux/bpf_verifier.h | 2 +- kernel/bpf/equation_join.c | 45 ++++++++++++++++++++++++++++-------- kernel/bpf/verifier.c | 5 ++-- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 88b3a5226c..942629cc49 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -2593,7 +2593,7 @@ void bpf_collect_iv_thresholds(struct bpf_verifier_env *env); int bpf_guess_first_trips(struct bpf_verifier_env *env, struct bpf_verifier_state *st, struct bpf_loop *loop); bool bpf_steps_are_constant(struct bpf_verifier_env *env, const struct loop_stack_entry *entry, - const unsigned long *iv_xregs, bool infinite); + const unsigned long *iv_xregs, bool infinite, u64 factor); void bpf_fast_forward_xregs(struct bpf_verifier_env *env, const struct bpf_verifier_state *old, struct bpf_verifier_state *cur, u64 factor, unsigned long *iv_xregs); diff --git a/kernel/bpf/equation_join.c b/kernel/bpf/equation_join.c index e601e3dda7..c7273d8800 100644 --- a/kernel/bpf/equation_join.c +++ b/kernel/bpf/equation_join.c @@ -826,24 +826,27 @@ static u32 slot_of_insn(struct bpf_verifier_env *env, const struct bpf_insn *ins return 0; } -/* If the operand has a constant step, the result has one too. @prev: in the same block. */ +/* + * If the operand has a constant step, the result has one too. @prev: in the same block. + * @wraps: the low 32 bits of the result can wrap. + */ static bool keeps_constant_step(const struct bpf_insn *insn, const struct bpf_insn *prev, - bool infinite) + bool wraps) { u8 class = BPF_CLASS(insn->code), op = BPF_OP(insn->code); if (bpf_is_ldimm64(insn)) return true; - if (class != BPF_ALU64 && (class != BPF_ALU || infinite)) + if (class != BPF_ALU64 && (class != BPF_ALU || wraps)) return false; if (op == BPF_MOV) - return !insn->off || (!infinite && insn->off == 32); + return !insn->off || (!wraps && insn->off == 32); if (BPF_SRC(insn->code) == BPF_X) return false; if (op == BPF_ADD || op == BPF_SUB) return true; /* x <<= 32; x >>= 32; */ - return !infinite && insn->imm == 32 && + return !wraps && insn->imm == 32 && (op == BPF_LSH || ((op == BPF_RSH || op == BPF_ARSH) && prev && prev->imm == 32 && prev->code == (BPF_ALU64 | BPF_LSH | BPF_K) && prev->dst_reg == insn->dst_reg)); @@ -862,6 +865,26 @@ static u64 step_of_xreg(struct bpf_verifier_env *env, const struct loop_stack_en bpf_find_var_bounds(env, &entry->header_state->st, xreg, &is_ptr)->r64.base; } +/* An xreg of @xregs passes a multiple of 2^31 if it moves @factor times as far */ +static bool skip_wraps(struct bpf_verifier_env *env, const struct loop_stack_entry *entry, + const unsigned long *xregs, u64 factor) +{ + u64 off, end; + struct cnum64 x; + bool is_ptr; + u32 n; + + for_each_set_bit(n, xregs, MAX_BPF_XREG) { + x = bpf_find_var_bounds(env, env->cur_state, + bpf_xreg(env->cur_state->curframe, n), &is_ptr)->r64; + off = step_of_xreg(env, entry, xregs, n) * factor; + end = x.base + x.size; + if (!is_ptr && ((x.base ^ (x.base + off)) | (end ^ (end + off))) >> 31) + return true; + } + return false; +} + /* * Two trips gave the steps. Does each trip add the same constant to the operands of the jump on * the iv, and to @iv_xregs? The code shows it, or a pass saw it at the header. @@ -869,9 +892,10 @@ static u64 step_of_xreg(struct bpf_verifier_env *env, const struct loop_stack_en * sum += i; x = n / i; no * a = b; b = 0; no, if a moved and b did not * @infinite: above 2^32 trips. Only the jump counts, and 32-bit insns wrap. + * @factor: of the skip. If a 32-bit insn wraps in it, the upper half of its result has no step. */ bool bpf_steps_are_constant(struct bpf_verifier_env *env, const struct loop_stack_entry *entry, - const unsigned long *iv_xregs, bool infinite) + const unsigned long *iv_xregs, bool infinite, u64 factor) { const struct bpf_insn_aux_data *aux = env->insn_aux_data; const struct bpf_insn *insn, *prev = NULL; @@ -881,11 +905,12 @@ bool bpf_steps_are_constant(struct bpf_verifier_env *env, const struct loop_stac u16 new_step = 0; /* the last write in this basic block has a constant step */ u16 last_round, uses_no_step; u64 steps[MAX_BPF_EXT_REG]; - bool has_step; + bool has_step, wraps, insn_wraps; u32 slot; int n; bitmap_andnot(not_linear, iv_xregs, loop->linear_xregs, MAX_BPF_XREG); + wraps = skip_wraps(env, entry, not_linear, factor); do { last_round = no_step; bpf_add_cost(env, loop->nr_insns, BPF_COST_SCAN); @@ -902,11 +927,13 @@ bool bpf_steps_are_constant(struct bpf_verifier_env *env, const struct loop_stac if (n == entry->iv_jump_idx && (uses_no_step & ~aux[n].const_reg_mask)) return false; slot = slot_of_insn(env, insn); - has_step = !slot ? keeps_constant_step(insn, prev, infinite) : + insn_wraps = infinite || (wraps && (BPF_CLASS(insn->code) != BPF_ALU || + aux[n].zext_dst)); + has_step = !slot ? keeps_constant_step(insn, prev, insn_wraps) : test_bit(bpf_xreg_to_spi(slot), aux[loop->header].live_stack_before) && !test_bit(slot, not_linear) && - (!infinite || BPF_SIZE(insn->code) == BPF_DW); + (!insn_wraps || BPF_SIZE(insn->code) == BPF_DW); /* a copy that the run made has the step of its source */ if (has_step && aux[n].seen && (aux[n].def_regs & ~aux[n].use_regs)) steps[insn->dst_reg] = diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 0b8063dbb3..5bd6fa081f 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -21055,6 +21055,7 @@ static int fast_forward_trips(struct bpf_verifier_env *env, struct loop_stack_en const struct loop_stack_entry *saved = bpf_top_loop(old->frame[old->curframe]); u32 period = entry->trip - saved->trip; bool infinite = entry->trips_left > BPF_MAX_TRIPS, guess; + u64 factor = div_u64(entry->trips_left - 1, period); DECLARE_BITMAP(iv_xregs, MAX_BPF_XREG) = {}; int err; @@ -21067,14 +21068,14 @@ static int fast_forward_trips(struct bpf_verifier_env *env, struct loop_stack_en if (err <= 0) return err; guess = (!infinite && saved->exit_threshold != entry->exit_threshold) || - !bpf_steps_are_constant(env, entry, iv_xregs, infinite); + !bpf_steps_are_constant(env, entry, iv_xregs, infinite, factor); if (infinite) { entry->loop->infinite = true; entry->loop->infinite_is_guess = guess || env->run_has_guess; return 0; } env->run_has_guess |= guess; - bpf_fast_forward_xregs(env, old, cur, div_u64(entry->trips_left - 1, period), iv_xregs); + bpf_fast_forward_xregs(env, old, cur, factor, iv_xregs); entry->iv_jump_idx = 0; entry->trips_left = 1; return 1; -- 2.55.0