From: Alexei Starovoitov After a prog compares 'pkt + 54' with pkt_end, the verifier knows that 54 bytes are in the packet: the range of pkt (r=54 in the log). The range stays when the prog adds a constant. But the verifier clears it when the prog adds a scalar that is not a constant, although it has an upper bound: if (pkt + 54 > pkt_end || off > 38) return 0; *(u64 *)(pkt + off) rejected: pkt has r=54, the sum has r=0 Keep the range when the scalar cannot be negative. The verifier accepts a load if umax + insn->off + size <= range, where umax is the largest offset of the pointer. Thus it accepts range - umax bytes after the pointer. A scalar in [0, N] adds N to umax, and it moves the pointer by N at most: pkt pkt + off pkt + 38 pkt + 54 | | | | the sum its umax its range |<-- 16 bytes ->| accepted |<------- 54 - off bytes -------->| in the packet Clear the range, as before, when umax of the sum is not below it. Such a range permits only a load with a negative insn->off, which stays rejected. It would make states different that regsafe() can prune today. reg_bounds_sync() of the sum stays: the alignment check and the check of the smallest offset use what var_off and the step tell about the sum. But it can make umax smaller. Then make the range smaller by the same number, with bpf_trim_pkt_range() of commit 88e2c896d859 ("bpf: Keep the valid bytes of a packet pointer in scalar evolution"). This is sufficient because the sum gets a new id, as before: no other pointer shares its range. '(pkt + x) - y' can keep a range now, and BPF_SUB does not sync a result that keeps a range. A prog that needs what the sync learns from var_off about this difference (smallest offset, alignment) is rejected now. To sync and trim the results of BPF_SUB like a sum would also change 'pkt - y', which is not synced today: valid progs that load today are rejected then. Sums of paths that checked different lengths now differ in the range, and regsafe() prunes fewer of them. veristat shows the same verdicts and insns for Cilium, Katran, sched-ext/scx and the selftests. These progs check only the bytes in front of a variable offset: no sum keeps a range. Signed-off-by: Alexei Starovoitov --- kernel/bpf/verifier.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 62f26149c3ae..fa53ee735762 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -16358,6 +16358,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn const char *reason; int ret, bounds_ret; bool to_scalar; + s64 smax; dst_reg = ®s[dst]; /* @@ -16513,12 +16514,18 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn if (!known) dst_reg->id = ++env->id_gen; /* - * Clear range for unknown addends since we can't know - * where the pkt pointer ended up. Also clear AT_PKT_END / + * The range is what umax of the pointer may get to, see + * check_packet_access(). umax of the addend was added to it + * and the pointer moved by that much at most, so the range + * holds. Clear it if the addend may be negative: umax says + * nothing then. Clear it if umax got to it: it permits no + * byte at or after the pointer, and it would only keep equal + * states apart in regsafe(). Also clear AT_PKT_END / * BEYOND_PKT_END from prior comparison as any pointer * arithmetic invalidates them. */ - if (!known || dst_reg->range < 0) + if (dst_reg->range < 0 || + (!known && (smin_val < 0 || reg_umax(dst_reg) >= dst_reg->range))) memset(&dst_reg->raw, 0, sizeof(dst_reg->raw)); } break; @@ -16611,11 +16618,18 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, struct bpf_insn * A packet pointer that keeps its id or range is checked against a * range set from the checked pointer's umax, so var_off must not tighten * its umax. r32 must still match var_off for reg_bounds_sanity_check(). + * A sum with a new id is different: progs rely on what var_off and the + * step tell about it (alignment, umin). No other pointer has its id, so + * it is enough that its range loses what its umax loses. */ - if (reg_is_pkt_pointer(dst_reg) && (known || dst_reg->range > 0)) + if (reg_is_pkt_pointer(dst_reg) && + (known || (opcode == BPF_SUB && dst_reg->range > 0))) { __update_reg32_bounds(dst_reg); - else + } else { + smax = reg_smax(dst_reg); reg_bounds_sync(dst_reg); + bpf_trim_pkt_range(dst_reg, smax); + } bounds_ret = sanitize_check_bounds(env, insn, dst_reg); if (bounds_ret == -EACCES) return bounds_ret; -- 2.55.0