Skip to content

Commit 6895e1d

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-fix-u32-s32-bounds-when-ranges-cross-min-max-boundary'
Eduard Zingerman says: ==================== bpf: Fix u32/s32 bounds when ranges cross min/max boundary Cover the following cases in range refinement logic for 32-bit ranges: - s32 range crosses U32_MAX/0 boundary, positive part of the s32 range overlaps with u32 range. - s32 range crosses U32_MAX/0 boundary, negative part of the s32 range overlaps with u32 range. These cases are already handled for 64-bit range refinement. Without the fix the test in patch 2 is rejected by the verifier. The test was reduced from sched-ext program. Changelog: - v2 -> v3: - Reverted da653de (Paul) - Removed !BPF_F_TEST_REG_INVARIANTS flag from crossing_32_bit_signed_boundary_2() (Paul) - v1 -> v2: - Extended commit message and comments (Emil) - Targeting 'bpf' tree instead of bpf-next (Alexei) v1: https://lore.kernel.org/bpf/9a23fbacdc6d33ec8fcb3f6988395b5129f75369.camel@gmail.com/T v2: https://lore.kernel.org/bpf/20260305-bpf-32-bit-range-overflow-v2-0-7169206a3041@gmail.com/ --- ==================== Link: https://patch.msgid.link/20260306-bpf-32-bit-range-overflow-v3-0-f7f67e060a6b@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 56145d2 + d87c930 commit 6895e1d

3 files changed

Lines changed: 120 additions & 19 deletions

File tree

kernel/bpf/verifier.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,30 @@ static void __reg32_deduce_bounds(struct bpf_reg_state *reg)
25112511
if ((u32)reg->s32_min_value <= (u32)reg->s32_max_value) {
25122512
reg->u32_min_value = max_t(u32, reg->s32_min_value, reg->u32_min_value);
25132513
reg->u32_max_value = min_t(u32, reg->s32_max_value, reg->u32_max_value);
2514+
} else {
2515+
if (reg->u32_max_value < (u32)reg->s32_min_value) {
2516+
/* See __reg64_deduce_bounds() for detailed explanation.
2517+
* Refine ranges in the following situation:
2518+
*
2519+
* 0 U32_MAX
2520+
* | [xxxxxxxxxxxxxx u32 range xxxxxxxxxxxxxx] |
2521+
* |----------------------------|----------------------------|
2522+
* |xxxxx s32 range xxxxxxxxx] [xxxxxxx|
2523+
* 0 S32_MAX S32_MIN -1
2524+
*/
2525+
reg->s32_min_value = (s32)reg->u32_min_value;
2526+
reg->u32_max_value = min_t(u32, reg->u32_max_value, reg->s32_max_value);
2527+
} else if ((u32)reg->s32_max_value < reg->u32_min_value) {
2528+
/*
2529+
* 0 U32_MAX
2530+
* | [xxxxxxxxxxxxxx u32 range xxxxxxxxxxxxxx] |
2531+
* |----------------------------|----------------------------|
2532+
* |xxxxxxxxx] [xxxxxxxxxxxx s32 range |
2533+
* 0 S32_MAX S32_MIN -1
2534+
*/
2535+
reg->s32_max_value = (s32)reg->u32_max_value;
2536+
reg->u32_min_value = max_t(u32, reg->u32_min_value, reg->s32_min_value);
2537+
}
25142538
}
25152539
}
25162540

tools/testing/selftests/bpf/prog_tests/reg_bounds.c

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,69 @@ static bool is_valid_range(enum num_t t, struct range x)
422422
}
423423
}
424424

425-
static struct range range_improve(enum num_t t, struct range old, struct range new)
425+
static struct range range_intersection(enum num_t t, struct range old, struct range new)
426426
{
427427
return range(t, max_t(t, old.a, new.a), min_t(t, old.b, new.b));
428428
}
429429

430+
/*
431+
* Result is precise when 'x' and 'y' overlap or form a continuous range,
432+
* result is an over-approximation if 'x' and 'y' do not overlap.
433+
*/
434+
static struct range range_union(enum num_t t, struct range x, struct range y)
435+
{
436+
if (!is_valid_range(t, x))
437+
return y;
438+
if (!is_valid_range(t, y))
439+
return x;
440+
return range(t, min_t(t, x.a, y.a), max_t(t, x.b, y.b));
441+
}
442+
443+
/*
444+
* This function attempts to improve x range intersecting it with y.
445+
* range_cast(... to_t ...) looses precision for ranges that pass to_t
446+
* min/max boundaries. To avoid such precision loses this function
447+
* splits both x and y into halves corresponding to non-overflowing
448+
* sub-ranges: [0, smin] and [smax, -1].
449+
* Final result is computed as follows:
450+
*
451+
* ((x ∩ [0, smax]) ∩ (y ∩ [0, smax])) ∪
452+
* ((x ∩ [smin,-1]) ∩ (y ∩ [smin,-1]))
453+
*
454+
* Precision might still be lost if final union is not a continuous range.
455+
*/
456+
static struct range range_refine_in_halves(enum num_t x_t, struct range x,
457+
enum num_t y_t, struct range y)
458+
{
459+
struct range x_pos, x_neg, y_pos, y_neg, r_pos, r_neg;
460+
u64 smax, smin, neg_one;
461+
462+
if (t_is_32(x_t)) {
463+
smax = (u64)(u32)S32_MAX;
464+
smin = (u64)(u32)S32_MIN;
465+
neg_one = (u64)(u32)(s32)(-1);
466+
} else {
467+
smax = (u64)S64_MAX;
468+
smin = (u64)S64_MIN;
469+
neg_one = U64_MAX;
470+
}
471+
x_pos = range_intersection(x_t, x, range(x_t, 0, smax));
472+
x_neg = range_intersection(x_t, x, range(x_t, smin, neg_one));
473+
y_pos = range_intersection(y_t, y, range(x_t, 0, smax));
474+
y_neg = range_intersection(y_t, y, range(y_t, smin, neg_one));
475+
r_pos = range_intersection(x_t, x_pos, range_cast(y_t, x_t, y_pos));
476+
r_neg = range_intersection(x_t, x_neg, range_cast(y_t, x_t, y_neg));
477+
return range_union(x_t, r_pos, r_neg);
478+
479+
}
480+
430481
static struct range range_refine(enum num_t x_t, struct range x, enum num_t y_t, struct range y)
431482
{
432483
struct range y_cast;
433484

485+
if (t_is_32(x_t) == t_is_32(y_t))
486+
x = range_refine_in_halves(x_t, x, y_t, y);
487+
434488
y_cast = range_cast(y_t, x_t, y);
435489

436490
/* If we know that
@@ -444,7 +498,7 @@ static struct range range_refine(enum num_t x_t, struct range x, enum num_t y_t,
444498
*/
445499
if (x_t == S64 && y_t == S32 && y_cast.a <= S32_MAX && y_cast.b <= S32_MAX &&
446500
(s64)x.a >= S32_MIN && (s64)x.b <= S32_MAX)
447-
return range_improve(x_t, x, y_cast);
501+
return range_intersection(x_t, x, y_cast);
448502

449503
/* the case when new range knowledge, *y*, is a 32-bit subregister
450504
* range, while previous range knowledge, *x*, is a full register
@@ -462,25 +516,11 @@ static struct range range_refine(enum num_t x_t, struct range x, enum num_t y_t,
462516
x_swap = range(x_t, swap_low32(x.a, y_cast.a), swap_low32(x.b, y_cast.b));
463517
if (!is_valid_range(x_t, x_swap))
464518
return x;
465-
return range_improve(x_t, x, x_swap);
466-
}
467-
468-
if (!t_is_32(x_t) && !t_is_32(y_t) && x_t != y_t) {
469-
if (x_t == S64 && x.a > x.b) {
470-
if (x.b < y.a && x.a <= y.b)
471-
return range(x_t, x.a, y.b);
472-
if (x.a > y.b && x.b >= y.a)
473-
return range(x_t, y.a, x.b);
474-
} else if (x_t == U64 && y.a > y.b) {
475-
if (y.b < x.a && y.a <= x.b)
476-
return range(x_t, y.a, x.b);
477-
if (y.a > x.b && y.b >= x.a)
478-
return range(x_t, x.a, y.b);
479-
}
519+
return range_intersection(x_t, x, x_swap);
480520
}
481521

482522
/* otherwise, plain range cast and intersection works */
483-
return range_improve(x_t, x, y_cast);
523+
return range_intersection(x_t, x, y_cast);
484524
}
485525

486526
/* =======================

tools/testing/selftests/bpf/progs/verifier_bounds.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ l0_%=: r0 = 0; \
11481148
SEC("xdp")
11491149
__description("bound check with JMP32_JSLT for crossing 32-bit signed boundary")
11501150
__success __retval(0)
1151-
__flag(!BPF_F_TEST_REG_INVARIANTS) /* known invariants violation */
1151+
__flag(BPF_F_TEST_REG_INVARIANTS)
11521152
__naked void crossing_32_bit_signed_boundary_2(void)
11531153
{
11541154
asm volatile (" \
@@ -2000,4 +2000,41 @@ __naked void bounds_refinement_multiple_overlaps(void *ctx)
20002000
: __clobber_all);
20012001
}
20022002

2003+
SEC("socket")
2004+
__success
2005+
__flag(BPF_F_TEST_REG_INVARIANTS)
2006+
__naked void signed_unsigned_intersection32_case1(void *ctx)
2007+
{
2008+
asm volatile(" \
2009+
call %[bpf_get_prandom_u32]; \
2010+
w0 &= 0xffffffff; \
2011+
if w0 < 0x3 goto 1f; /* on fall-through u32 range [3..U32_MAX] */ \
2012+
if w0 s> 0x1 goto 1f; /* on fall-through s32 range [S32_MIN..1] */ \
2013+
if w0 s< 0x0 goto 1f; /* range can be narrowed to [S32_MIN..-1] */ \
2014+
r10 = 0; /* thus predicting the jump. */ \
2015+
1: exit; \
2016+
" :
2017+
: __imm(bpf_get_prandom_u32)
2018+
: __clobber_all);
2019+
}
2020+
2021+
SEC("socket")
2022+
__success
2023+
__flag(BPF_F_TEST_REG_INVARIANTS)
2024+
__naked void signed_unsigned_intersection32_case2(void *ctx)
2025+
{
2026+
asm volatile(" \
2027+
call %[bpf_get_prandom_u32]; \
2028+
w0 &= 0xffffffff; \
2029+
if w0 > 0x80000003 goto 1f; /* on fall-through u32 range [0..S32_MIN+3] */ \
2030+
if w0 s< -3 goto 1f; /* on fall-through s32 range [-3..S32_MAX] */ \
2031+
if w0 s> 5 goto 1f; /* on fall-through s32 range [-3..5] */ \
2032+
if w0 <= 5 goto 1f; /* range can be narrowed to [0..5] */ \
2033+
r10 = 0; /* thus predicting the jump */ \
2034+
1: exit; \
2035+
" :
2036+
: __imm(bpf_get_prandom_u32)
2037+
: __clobber_all);
2038+
}
2039+
20032040
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)