Skip to content

Commit cf5fe3c

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
bpf: make __reg{32,64}_deduce_bounds logic more robust
This change doesn't seem to have any effect on selftests and production BPF object files, but we preemptively try to make it more robust. First, "learn sign from signed bounds" comment is misleading, as we are learning not just sign, but also values. Second, we simplify the check for determining whether entire range is positive or negative similarly to other checks added earlier, using appropriate u32/u64 cast and single comparisons. As explain in comments in __reg64_deduce_bounds(), the checks are equivalent. Last but not least, smin/smax and s32_min/s32_max reassignment based on min/max of both umin/umax and smin/smax (and 32-bit equivalents) is hard to explain and justify. We are updating unsigned bounds from signed bounds, why would we update signed bounds at the same time? This might be correct, but it's far from obvious why and the code or comments don't try to justify this. Given we've added a separate deduction of signed bounds from unsigned bounds earlier, this seems at least redundant, if not just wrong. In short, we remove doubtful pieces, and streamline the rest to follow the logic and approach of the rest of reg_bounds_sync() checks. Acked-by: Shung-Hsi Yu <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 3cf98cf commit cf5fe3c

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

kernel/bpf/verifier.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2399,17 +2399,13 @@ static void __reg32_deduce_bounds(struct bpf_reg_state *reg)
23992399
reg->s32_min_value = max_t(s32, reg->s32_min_value, reg->u32_min_value);
24002400
reg->s32_max_value = min_t(s32, reg->s32_max_value, reg->u32_max_value);
24012401
}
2402-
/* Learn sign from signed bounds.
2403-
* If we cannot cross the sign boundary, then signed and unsigned bounds
2402+
/* If we cannot cross the sign boundary, then signed and unsigned bounds
24042403
* are the same, so combine. This works even in the negative case, e.g.
24052404
* -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
24062405
*/
2407-
if (reg->s32_min_value >= 0 || reg->s32_max_value < 0) {
2408-
reg->s32_min_value = reg->u32_min_value =
2409-
max_t(u32, reg->s32_min_value, reg->u32_min_value);
2410-
reg->s32_max_value = reg->u32_max_value =
2411-
min_t(u32, reg->s32_max_value, reg->u32_max_value);
2412-
return;
2406+
if ((u32)reg->s32_min_value <= (u32)reg->s32_max_value) {
2407+
reg->u32_min_value = max_t(u32, reg->s32_min_value, reg->u32_min_value);
2408+
reg->u32_max_value = min_t(u32, reg->s32_max_value, reg->u32_max_value);
24132409
}
24142410
}
24152411

@@ -2486,17 +2482,13 @@ static void __reg64_deduce_bounds(struct bpf_reg_state *reg)
24862482
reg->smin_value = max_t(s64, reg->smin_value, reg->umin_value);
24872483
reg->smax_value = min_t(s64, reg->smax_value, reg->umax_value);
24882484
}
2489-
/* Learn sign from signed bounds.
2490-
* If we cannot cross the sign boundary, then signed and unsigned bounds
2485+
/* If we cannot cross the sign boundary, then signed and unsigned bounds
24912486
* are the same, so combine. This works even in the negative case, e.g.
24922487
* -3 s<= x s<= -1 implies 0xf...fd u<= x u<= 0xf...ff.
24932488
*/
2494-
if (reg->smin_value >= 0 || reg->smax_value < 0) {
2495-
reg->smin_value = reg->umin_value = max_t(u64, reg->smin_value,
2496-
reg->umin_value);
2497-
reg->smax_value = reg->umax_value = min_t(u64, reg->smax_value,
2498-
reg->umax_value);
2499-
return;
2489+
if ((u64)reg->smin_value <= (u64)reg->smax_value) {
2490+
reg->umin_value = max_t(u64, reg->smin_value, reg->umin_value);
2491+
reg->umax_value = min_t(u64, reg->smax_value, reg->umax_value);
25002492
}
25012493
}
25022494

0 commit comments

Comments
 (0)