bpf: Fix abs(INT_MIN) undefined behavior in interpreter sdiv/smod
Subsystems (4)
cover bpf: Fix abs(INT_MIN) undefined behavior in interpreter sdiv/smod
The BPF interpreter's signed 32-bit division and modulo handlers use abs() on s32 operands, which is undefined for S32_MIN. This causes the interpreter to compute wrong results, creating a mismatch with the verifier's range tracking.
For example, INT_MIN / 2 returns 0x40000000 instead of the correct 0xC0000000. The verifier tracks the correct range, so a crafted BPF program can exploit the mismatch for out-of-bounds map value access (confirmed by KASAN).
Patch 1 introduces __safe_abs32() which handles S32_MIN correctly and replaces all 8 abs((s32)...) call sites.
Patch 2 adds selftests covering sdiv32 and smod32 with INT_MIN dividend to prevent regression.
Jenny Guanni Qu (2):
bpf: Fix undefined behavior in interpreter sdiv/smod for INT_MIN selftests/bpf: Add tests for sdiv32/smod32 with INT_MIN dividend
kernel/bpf/core.c | 22 ++++--- .../selftests/bpf/progs/verifier_sdiv.c | 58 +++++++++++++++++++ 2 files changed, 72 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 bpf: Fix undefined behavior in interpreter sdiv/smod for INT_MIN
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.
--- kernel/bpf/core.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
kernel/bpf/core.c +14 -8
@@ -1745,6 +1745,12 @@ bool bpf_opcode_in_insntable(u8 code)* * Return: whatever value is in %BPF_R0 at program exit */ + +/* Safe absolute value for s32 - abs() is undefined for S32_MIN */ +static inline u32 __safe_abs32 (s32 x) +{ +return x >=0 ? (u32 )x : x ==S32_MIN ? (u32 )S32_MIN : (u32)-x; +}static u64 ___bpf_prog_run (u64 *regs,const struct bpf_insn *insn) {#define BPF_INSN_2_LBL (x, y) [BPF_##x | BPF_##y] = &&x##_##y
@@ -1900,8 +1906,8 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)DST =do_div (AX , (u32 )SRC );break ;case 1 : -AX =abs ((s32 )DST ); -AX =do_div (AX ,abs ((s32 )SRC )); +AX =__safe_abs32 ((s32 )DST ); +AX =do_div (AX ,__safe_abs32 ((s32 )SRC ));if ((s32 )DST <0 )DST = (u32)-AX ;else
@@ -1928,8 +1934,8 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)DST =do_div (AX , (u32 )IMM );break ;case 1 : -AX =abs ((s32 )DST ); -AX =do_div (AX ,abs ((s32 )IMM )); +AX =__safe_abs32 ((s32 )DST ); +AX =do_div (AX ,__safe_abs32 ((s32 )IMM ));if ((s32 )DST <0 )DST = (u32)-AX ;else
@@ -1955,8 +1961,8 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)DST = (u32 )AX ;break ;case 1 : -AX =abs ((s32 )DST ); -do_div (AX ,abs ((s32 )SRC )); +AX =__safe_abs32 ((s32 )DST ); +do_div (AX ,__safe_abs32 ((s32 )SRC ));if (((s32 )DST <0 ) == ((s32 )SRC <0 ))DST = (u32 )AX ;else
@@ -1982,8 +1988,8 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn)DST = (u32 )AX ;break ;case 1 : -AX =abs ((s32 )DST ); -do_div (AX ,abs ((s32 )IMM )); +AX =__safe_abs32 ((s32 )DST ); +do_div (AX ,__safe_abs32 ((s32 )IMM ));if (((s32 )DST <0 ) == ((s32 )IMM <0 ))DST = (u32 )AX ;else
Signature · 1 line
2.34.1
PATCH 2/2 selftests/bpf: Add tests for sdiv32/smod32 with INT_MIN dividend
In reply to Jenny Guanni QuAdd 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)
--- .../selftests/bpf/progs/verifier_sdiv.c | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+)
tools/testing/selftests/bpf/progs/verifier_sdiv.c +58 -0
@@ -1209,6 +1209,64 @@ __naked void smod32_ri_divisor_neg_1(void) : __clobber_all); } +SEC("socket") +__description("SDIV32, INT_MIN divided by 2, imm") +__success __success_unpriv __retval(-1073741824) +__naked void sdiv32_int_min_div_2_imm (void ) +{ + asmvolatile (" \ +w0 = %[int_min]; \ +w0 s/= 2; \ +exit; \ +" : + : __imm_const(int_min, INT_MIN) + : __clobber_all); +} + +SEC("socket") +__description("SDIV32, INT_MIN divided by 2, reg") +__success __success_unpriv __retval(-1073741824) +__naked void sdiv32_int_min_div_2_reg (void ) +{ + asmvolatile (" \ +w0 = %[int_min]; \ +w1 = 2; \ +w0 s/= w1; \ +exit; \ +" : + : __imm_const(int_min, INT_MIN) + : __clobber_all); +} + +SEC("socket") +__description("SMOD32, INT_MIN modulo 2, imm") +__success __success_unpriv __retval(0) +__naked void smod32_int_min_mod_2_imm(void) +{ + asmvolatile (" \ +w0 = %[int_min]; \ +w0 s%%= 2; \ +exit; \ +" : + : __imm_const(int_min, INT_MIN) + : __clobber_all); +} + +SEC("socket") +__description("SMOD32, INT_MIN modulo -2, imm") +__success __success_unpriv __retval(0) +__naked void smod32_int_min_mod_neg2_imm(void) +{ + asmvolatile (" \ +w0 = %[int_min]; \ +w0 s%%= -2; \ +exit; \ +" : + : __imm_const(int_min, INT_MIN) + : __clobber_all); +} + +#else SEC ("socket" )
Signature · 1 line
2.34.1
kernel test robot
build botRe: [PATCH v1 1/2] bpf: Fix undefined behavior in interpreter sdiv/smod for INT_MIN
In reply to Jenny Guanni Qukernel test robot noticed the following build warnings:
Hi Jenny,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/net]
[also build test WARNING on bpf-next/master bpf/master linus/master v7.0-rc2 next-20260305] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jenny-Guanni-Qu/bpf-Fix-undefined-behavior-in-interpreter-sdiv-smod-for-INT_MIN/20260306-110344 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git net patch link: https://lore.kernel.org/r/20260306025600.870163-2-qguanni%40gmail.com patch subject: [PATCH v1 1/2] bpf: Fix undefined behavior in interpreter sdiv/smod for INT_MIN config: arc-randconfig-r072-20260306 (https://download.01.org/0day-ci/archive/20260306/[email protected]/config) compiler: arc-linux-gcc (GCC) 8.5.0 smatch: v0.5.0-9004-gb810ac53 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260306/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: kernel/bpf/core.c:1750 function parameter 'x' not described in '__safe_abs32' >> Warning: kernel/bpf/core.c:1750 expecting prototype for ___bpf_prog_run(). Prototype was for __safe_abs32() instead
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki