bpf: Fix abs(INT_MIN) undefined behavior in interpreter sdiv/smod#11321
Closed
kernel-patches-daemon-bpf[bot] wants to merge 2 commits intobpf-next_basefrom
Closed
bpf: Fix abs(INT_MIN) undefined behavior in interpreter sdiv/smod#11321kernel-patches-daemon-bpf[bot] wants to merge 2 commits intobpf-next_basefrom
kernel-patches-daemon-bpf[bot] wants to merge 2 commits intobpf-next_basefrom
Conversation
Author
|
Upstream branch: 6dd780f |
59120bd to
94aca0b
Compare
Author
|
Upstream branch: 099bded |
87a24bf to
593a559
Compare
94aca0b to
980a66f
Compare
added 2 commits
March 9, 2026 09:45
The BPF interpreter's signed 32-bit division and modulo handlers use the kernel abs() macro on s32 operands. The abs() macro documentation (include/linux/math.h) explicitly states the result is undefined when the input is the type minimum. When DST contains S32_MIN (0x80000000), abs((s32)DST) triggers undefined behavior and returns S32_MIN unchanged on arm64/x86. This value is then sign-extended to u64 as 0xFFFFFFFF80000000, causing do_div() to compute the wrong result. The verifier's abstract interpretation (scalar32_min_max_sdiv) computes the mathematically correct result for range tracking, creating a verifier/interpreter mismatch that can be exploited for out-of-bounds map value access. Introduce __safe_abs32() which handles S32_MIN correctly and replace all 8 abs((s32)...) call sites in the interpreter's sdiv32/smod32 handlers. Fixes: ec0e2da ("bpf: Support new signed div/mod instructions.") Signed-off-by: Jenny Guanni Qu <qguanni@gmail.com>
Add tests to verify that signed 32-bit division and modulo operations produce correct results when the dividend is INT_MIN (0x80000000). These test the fix in the previous commit which replaced abs() with a safe helper to avoid undefined behavior for S32_MIN. Test cases: - SDIV32 INT_MIN / 2 = -1073741824 (imm and reg divisor) - SMOD32 INT_MIN % 2 = 0 (positive and negative divisor) Signed-off-by: Jenny Guanni Qu <qguanni@gmail.com>
Author
|
Upstream branch: bd2e02e |
593a559 to
8da9a98
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull request for series with
subject: bpf: Fix abs(INT_MIN) undefined behavior in interpreter sdiv/smod
version: 2
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=1062802