Skip to content

Commit 00bf8d0

Browse files
pchaignoAlexei Starovoitov
authored andcommitted
bpf: Improve bounds when s64 crosses sign boundary
__reg64_deduce_bounds currently improves the s64 range using the u64 range and vice versa, but only if it doesn't cross the sign boundary. This patch improves __reg64_deduce_bounds to cover the case where the s64 range crosses the sign boundary but overlaps with the u64 range on only one end. In that case, we can improve both ranges. Consider the following example, with the s64 range crossing the sign boundary: 0 U64_MAX | [xxxxxxxxxxxxxx u64 range xxxxxxxxxxxxxx] | |----------------------------|----------------------------| |xxxxx s64 range xxxxxxxxx] [xxxxxxx| 0 S64_MAX S64_MIN -1 The u64 range overlaps only with positive portion of the s64 range. We can thus derive the following new s64 and u64 ranges. 0 U64_MAX | [xxxxxx u64 range xxxxx] | |----------------------------|----------------------------| | [xxxxxx s64 range xxxxx] | 0 S64_MAX S64_MIN -1 The same logic can probably apply to the s32/u32 ranges, but this patch doesn't implement that change. In addition to the selftests, the __reg64_deduce_bounds change was also tested with Agni, the formal verification tool for the range analysis [1]. Link: https://github.com/bpfverif/agni [1] Acked-by: Eduard Zingerman <[email protected]> Acked-by: Shung-Hsi Yu <[email protected]> Signed-off-by: Paul Chaignon <[email protected]> Link: https://lore.kernel.org/r/933bd9ce1f36ded5559f92fdc09e5dbc823fa245.1753695655.git.paul.chaignon@gmail.com Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 5345e64 commit 00bf8d0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

kernel/bpf/verifier.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,58 @@ static void __reg64_deduce_bounds(struct bpf_reg_state *reg)
25232523
if ((u64)reg->smin_value <= (u64)reg->smax_value) {
25242524
reg->umin_value = max_t(u64, reg->smin_value, reg->umin_value);
25252525
reg->umax_value = min_t(u64, reg->smax_value, reg->umax_value);
2526+
} else {
2527+
/* If the s64 range crosses the sign boundary, then it's split
2528+
* between the beginning and end of the U64 domain. In that
2529+
* case, we can derive new bounds if the u64 range overlaps
2530+
* with only one end of the s64 range.
2531+
*
2532+
* In the following example, the u64 range overlaps only with
2533+
* positive portion of the s64 range.
2534+
*
2535+
* 0 U64_MAX
2536+
* | [xxxxxxxxxxxxxx u64 range xxxxxxxxxxxxxx] |
2537+
* |----------------------------|----------------------------|
2538+
* |xxxxx s64 range xxxxxxxxx] [xxxxxxx|
2539+
* 0 S64_MAX S64_MIN -1
2540+
*
2541+
* We can thus derive the following new s64 and u64 ranges.
2542+
*
2543+
* 0 U64_MAX
2544+
* | [xxxxxx u64 range xxxxx] |
2545+
* |----------------------------|----------------------------|
2546+
* | [xxxxxx s64 range xxxxx] |
2547+
* 0 S64_MAX S64_MIN -1
2548+
*
2549+
* If they overlap in two places, we can't derive anything
2550+
* because reg_state can't represent two ranges per numeric
2551+
* domain.
2552+
*
2553+
* 0 U64_MAX
2554+
* | [xxxxxxxxxxxxxxxxx u64 range xxxxxxxxxxxxxxxxx] |
2555+
* |----------------------------|----------------------------|
2556+
* |xxxxx s64 range xxxxxxxxx] [xxxxxxxxxx|
2557+
* 0 S64_MAX S64_MIN -1
2558+
*
2559+
* The first condition below corresponds to the first diagram
2560+
* above.
2561+
*/
2562+
if (reg->umax_value < (u64)reg->smin_value) {
2563+
reg->smin_value = (s64)reg->umin_value;
2564+
reg->umax_value = min_t(u64, reg->umax_value, reg->smax_value);
2565+
} else if ((u64)reg->smax_value < reg->umin_value) {
2566+
/* This second condition considers the case where the u64 range
2567+
* overlaps with the negative portion of the s64 range:
2568+
*
2569+
* 0 U64_MAX
2570+
* | [xxxxxxxxxxxxxx u64 range xxxxxxxxxxxxxx] |
2571+
* |----------------------------|----------------------------|
2572+
* |xxxxxxxxx] [xxxxxxxxxxxx s64 range |
2573+
* 0 S64_MAX S64_MIN -1
2574+
*/
2575+
reg->smax_value = (s64)reg->umax_value;
2576+
reg->umin_value = max_t(u64, reg->umin_value, reg->smin_value);
2577+
}
25262578
}
25272579
}
25282580

0 commit comments

Comments
 (0)