From: Alexei Starovoitov __update_reg{32,64}_bounds() intersect the range with [tnum min, tnum max], but the ends of the result can still be values that are not members of var_off. For example var_off=(0x0; 0xfc) umax=255 where 252 is the largest possible value. Move both ends of r64 and r32 inwards to the nearest member of var_off. Values dropped from the ends are not members of var_off, so the set of values described by the register doesn't change. If the range has no member of var_off it is left as is. It matters when a loop counter is tracked as a range. In the loop: 13: (07) r6 += 4 14: (55) if r6 != 0x190 goto pc-5 r6 of [0, 399] with var_off=(0x0; 0x1fc) at the loop head becomes [4, 403] after insn 13 and insn 14 cannot cut it. With the ends trimmed the ranges are [0, 396] and [4, 400]. Insn 14 cuts 400 off and r6 that goes back to the loop head is within the range it started with. No veristat changes in selftests. Signed-off-by: Alexei Starovoitov --- kernel/bpf/verifier.c | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index a7c9e2d8965d..cb5d498ada69 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -2094,9 +2094,71 @@ static struct cnum64 cnum64_from_tnum(struct tnum tnum) return cnum64_from_urange(tnum.value, (tnum.value | tnum.mask)); } +/* smallest member of @t that is >= @x, wraps around to the smallest member of @t */ +static u64 tnum_member_ge(struct tnum t, u64 x) +{ + if ((x & ~t.mask) == t.value) + return x; + if (x > (t.value | t.mask)) + return t.value; + return tnum_step(t, x); +} + +/* largest member of @t that is <= @x, wraps around to the largest member of @t */ +static u64 tnum_member_le(struct tnum t, u64 x) +{ + /* members of 'tc' are bitwise complements of members of 't' */ + struct tnum tc = { .value = ~(t.value | t.mask), .mask = t.mask }; + + return ~tnum_member_ge(tc, ~x); +} + +/* + * Move both ends of arc @c inwards to the nearest members of @t. + * Values that are dropped are not members of @t. + */ +static struct cnum64 cnum64_tighten_by_tnum(struct cnum64 c, struct tnum t) +{ + u64 lo, hi, dlo, dhi; + + if (cnum64_is_empty(c) || c.size == U64_MAX || !t.mask) + return c; + lo = tnum_member_ge(t, c.base); + dlo = lo - c.base; + if (dlo > c.size) + return c; + hi = tnum_member_le(t, c.base + c.size); + dhi = c.base + c.size - hi; + if (dhi > c.size - dlo) + return c; + return (struct cnum64){ .base = lo, .size = c.size - dlo - dhi }; +} + +static struct cnum32 cnum32_tighten_by_tnum(struct cnum32 c, struct tnum t) +{ + struct tnum tc; + u32 lo, hi, dlo, dhi; + + t = tnum_subreg(t); + if (cnum32_is_empty(c) || c.size == U32_MAX || !t.mask) + return c; + lo = tnum_member_ge(t, c.base); + dlo = lo - c.base; + if (dlo > c.size) + return c; + /* complement within 32 bits */ + tc = (struct tnum){ .value = (u32)~(t.value | t.mask), .mask = t.mask }; + hi = ~(u32)tnum_member_ge(tc, (u32)~(c.base + c.size)); + dhi = c.base + c.size - hi; + if (dhi > c.size - dlo) + return c; + return (struct cnum32){ .base = lo, .size = c.size - dlo - dhi }; +} + static void __update_reg32_bounds(struct bpf_reg_state *reg) { cnum32_intersect_with(®->r32, cnum32_from_tnum(reg->var_off)); + reg->r32 = cnum32_tighten_by_tnum(reg->r32, reg->var_off); } static void __update_reg64_bounds(struct bpf_reg_state *reg) @@ -2105,6 +2167,7 @@ static void __update_reg64_bounds(struct bpf_reg_state *reg) bool umin_in_tnum; cnum64_intersect_with(®->r64, cnum64_from_tnum(reg->var_off)); + reg->r64 = cnum64_tighten_by_tnum(reg->r64, reg->var_off); /* Check if u64 and tnum overlap in a single value */ tnum_next = tnum_step(reg->var_off, reg_umin(reg)); -- 2.55.0