From: Alexei Starovoitov The verifier rejects a valid prog with -EFAULT and a WARN: r6 = 0 r7 = 0 1: if r7 != 9 goto 2f r7 += 1 r6 += 1 if r6 < 10 goto 1b 2: exit verifier bug: scev clamp produced empty range for r7 Scalar evolution describes each variable of a loop separately. It does not know that r6 and r7 are always equal. r6 r7 a run of the prog, at 1: k k the verifier, at 1: 0..9 0..9 after 'if r7 != 9': 0..9 9 r6 < 9 never occurs back at 1: 1..9 10 not in 0..9 When a state comes back to the loop head, bpf_clamp_scev_regs() intersects each variable with its calculated range. For r7 the result is empty, and the verifier reports a bug. A range has three parts: 64 bits, 32 bits and the step. The verifier examines only the 64 bits. When a different part makes the result empty, the prog gets a second message: verifier bug: REG INVARIANTS VIOLATION (bpf_set_reg_range) This is not a bug. No run of the prog gets to this state. Thus each range is correct for r7. Give r7 the range that it has at the loop head, and continue. The verifier continues to compare the other parts of the state with the state at the loop head. Thus it does not hide a real problem on such a path. Each pair of the three parts can have numbers in common when all three have none. Thus new bpf_reg_range_is_empty() examines the three parts together. Fixes: 6ccf49f79327 ("bpf: Use SCEV to widen bounded loops") Signed-off-by: Alexei Starovoitov --- include/linux/bpf_verifier.h | 1 + kernel/bpf/scev.c | 19 ++++++++++++++----- kernel/bpf/verifier.c | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index de301b32015d..7030b997ce2c 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1895,6 +1895,7 @@ bool bpf_same_memory_origin(const struct bpf_reg_state *reg_a, const struct bpf_reg_state *reg_b); int bpf_set_reg_range(struct bpf_verifier_env *env, struct bpf_reg_state *reg, struct cnum64 r64, struct cnum32 r32, u16 base, u16 step); +bool bpf_reg_range_is_empty(struct cnum64 r64, struct cnum32 r32, u16 base, u16 step); bool bpf_reg_union_compatible(const struct bpf_reg_state *a, const struct bpf_reg_state *b); int bpf_reg_union(struct bpf_verifier_env *env, struct bpf_reg_state *acc, const struct bpf_reg_state *src); diff --git a/kernel/bpf/scev.c b/kernel/bpf/scev.c index 4266651e3fe4..6330deb2d3f2 100644 --- a/kernel/bpf/scev.c +++ b/kernel/bpf/scev.c @@ -2869,6 +2869,8 @@ int bpf_clamp_scev_regs(struct bpf_verifier_env *env, struct bpf_func_state *cur struct linear_coefs coefs; struct env *header_env; struct bounds bounds; + struct cnum64 r64; + struct cnum32 r32; u32 r; int err; @@ -2892,11 +2894,18 @@ int bpf_clamp_scev_regs(struct bpf_verifier_env *env, struct bpf_func_state *cur continue; linear_bounds(entry_reg, max_header_count, &coefs, &bounds); - bounds.r64 = cnum64_intersect(reg->r64, bounds.r64); - bounds.r32 = cnum32_intersect(reg->r32, bounds.r32); - if (cnum64_is_empty(bounds.r64)) { - verifier_bug(env, "scev clamp produced empty range for r%d", r); - return -EFAULT; + r64 = cnum64_intersect(reg->r64, bounds.r64); + r32 = cnum32_intersect(reg->r32, bounds.r32); + /* + * The state at the head doesn't know how the variables depend on each other. + * It lets r6 = 0, r7 = 9 into the loop when both count from 0, and r7 = 10 + * comes back. No run gets here like that, so any range is right for r7. + * Take the one it has at the head: the rest of the state is still compared + * with the head, and what was pruned on the way here relies on that. + */ + if (!bpf_reg_range_is_empty(r64, r32, bounds.base, bounds.step)) { + bounds.r64 = r64; + bounds.r32 = r32; } if (log->level & BPF_LOG_LEVEL2) { bpf_log(log, "loop header at %d, clamping ", insn_idx); diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index c93ef399c420..84f722bac539 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -17739,6 +17739,25 @@ int bpf_set_reg_range(struct bpf_verifier_env *env, struct bpf_reg_state *reg, return reg_bounds_sanity_check(env, reg, "bpf_set_reg_range"); } +/* Is there no number that is in @r64, in @r32 and equal to @base + k * @step? */ +bool bpf_reg_range_is_empty(struct cnum64 r64, struct cnum32 r32, u16 base, u16 step) +{ + struct bpf_reg_state reg = { + .type = SCALAR_VALUE, + .r64 = cnum64_intersect_linear(r64, base, step), + .r32 = r32, + .base = base, + .step = step, + .var_off = tnum_unknown, + }; + + if (range_bounds_violation(®)) + return true; + /* Each pair of the three can have numbers in common when all three have none. */ + reg_bounds_sync(®); + return range_bounds_violation(®); +} + /* * Checks if pointer and packet metadata matches between two registers. * For use with bpf_reg_union(). -- 2.55.0