]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bpf: Fix undefined behavior in interpreter sdiv/smod for INT_MIN
authorJenny Guanni Qu <qguanni@gmail.com>
Wed, 11 Mar 2026 01:11:15 +0000 (01:11 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 21 Mar 2026 20:12:16 +0000 (13:12 -0700)
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 abs_s32() which handles S32_MIN correctly by casting to u32
before negating, avoiding signed overflow entirely. Replace all 8
abs((s32)...) call sites in the interpreter's sdiv32/smod32 handlers.

s32 is the only affected case -- the s64 division/modulo handlers do
not use abs().

Fixes: ec0e2da95f72 ("bpf: Support new signed div/mod instructions.")
Acked-by: Yonghong Song <yonghong.song@linux.dev>
Acked-by: Mykyta Yatsenko <yatsenko@meta.com>
Signed-off-by: Jenny Guanni Qu <qguanni@gmail.com>
Link: https://lore.kernel.org/r/20260311011116.2108005-2-qguanni@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/core.c

index 9e126be33755318bd5d51ad417fd17befa7fdefa..7b675a451ec8ef9f202eac22b3385dac82024a77 100644 (file)
@@ -1757,6 +1757,12 @@ bool bpf_opcode_in_insntable(u8 code)
 }
 
 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
+/* Absolute value of s32 without undefined behavior for S32_MIN */
+static u32 abs_s32(s32 x)
+{
+       return x >= 0 ? (u32)x : -(u32)x;
+}
+
 /**
  *     ___bpf_prog_run - run eBPF program on a given context
  *     @regs: is the array of MAX_BPF_EXT_REG eBPF pseudo-registers
@@ -1921,8 +1927,8 @@ select_insn:
                        DST = do_div(AX, (u32) SRC);
                        break;
                case 1:
-                       AX = abs((s32)DST);
-                       AX = do_div(AX, abs((s32)SRC));
+                       AX = abs_s32((s32)DST);
+                       AX = do_div(AX, abs_s32((s32)SRC));
                        if ((s32)DST < 0)
                                DST = (u32)-AX;
                        else
@@ -1949,8 +1955,8 @@ select_insn:
                        DST = do_div(AX, (u32) IMM);
                        break;
                case 1:
-                       AX = abs((s32)DST);
-                       AX = do_div(AX, abs((s32)IMM));
+                       AX = abs_s32((s32)DST);
+                       AX = do_div(AX, abs_s32((s32)IMM));
                        if ((s32)DST < 0)
                                DST = (u32)-AX;
                        else
@@ -1976,8 +1982,8 @@ select_insn:
                        DST = (u32) AX;
                        break;
                case 1:
-                       AX = abs((s32)DST);
-                       do_div(AX, abs((s32)SRC));
+                       AX = abs_s32((s32)DST);
+                       do_div(AX, abs_s32((s32)SRC));
                        if (((s32)DST < 0) == ((s32)SRC < 0))
                                DST = (u32)AX;
                        else
@@ -2003,8 +2009,8 @@ select_insn:
                        DST = (u32) AX;
                        break;
                case 1:
-                       AX = abs((s32)DST);
-                       do_div(AX, abs((s32)IMM));
+                       AX = abs_s32((s32)DST);
+                       do_div(AX, abs_s32((s32)IMM));
                        if (((s32)DST < 0) == ((s32)IMM < 0))
                                DST = (u32)AX;
                        else