selftests/bpf: Add tests for BPF_DIV analysis
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/2 bpf: Add interval and tnum analysis for signed and unsigned BPF_DIV
From: Yazhou Tang <[email protected]>
This patch introduces interval analysis (range tracking) and tnum analysis (bitwise tracking) for both signed and unsigned division operations in the BPF verifier.
The BPF verifier currently lacks support for value tracking on BPF_DIV instructions, which can lead to false positives during verification of BPF programs that utilize division instructions.
According to the BPF instruction set[1], the instruction's offset field (insn->off) is used to distinguish between signed (off == 1) and unsigned division (off == 0). Moreover, we also follow the BPF division semantics to handle special cases, such as division by zero and signed division overflow.
- UDIV: dst = (src != 0) ? (dst / src) : 0
- SDIV: dst = (src == 0) ? 0 : ((src == -1 && dst == LLONG_MIN) ? LLONG_MIN : (dst / src))
Here is the overview of the changes made in this patch:
1. For interval analysis:
- Added scalar_min_max_udiv and scalar32_min_max_udiv to update umin/umax bounds, which is straightforward.
- Added scalar_min_max_sdiv and scalar32_min_max_sdiv to update smin/smax bounds. It handles non-monotonic intervals by decomposing the divisor range into negative, zero, and positive sub-ranges, and computing the result range for each sub-range separately. Finally, it combines the results to get the final smin/smax bounds.
2. For tnum analysis, we referred to LLVM's KnownBits implementation[2] and the recent research on abstract interpretation of division[3]:
- Added tnum_udiv to compute the tnum for unsigned division. It calculates the maximum possible result based on the maximum values of the dividend tnum and the minimum values of the divisor tnum, then constructs the resulting tnum accordingly. We have prove its soundness using Rocq Prover[4].
- Added tnum_sdiv to compute the tnum for signed division. It splits the operands into positive and negative components, then performs calculation on absolute values using tnum_udiv, finally unions the results to ensure soundness.
3. Also updated existing selftests based on the expected BPF_DIV behavior.
[1] https://www.kernel.org/doc/Documentation/bpf/standardization/instruction-set.rst
[2] https://llvm.org/doxygen/KnownBits_8cpp_source.html
[3] https://dl.acm.org/doi/10.1145/3728905
[4] https://github.com/shenghaoyuan/open-verified-artifacts/tree/main/tnum
---
Hello everyone,
Thanks for reviewing our patch! This patch adds interval and tnum analysis for both signed and unsigned BPF_DIV instructions in the BPF verifier.
We also have implemented interval and tnum analysis for BPF_MOD instruction, which is closely related to division. However, to keep the patch size manageable and facilitate easier review, we have decided to submit the BPF_MOD related changes in a separate patch following this one.
Best,
Yazhou Tang
include/linux/tnum.h | 4 + kernel/bpf/tnum.c | 159 ++++++++++++- kernel/bpf/verifier.c | 225 ++++++++++++++++++ .../bpf/progs/verifier_value_illegal_alu.c | 7 +- 4 files changed, 391 insertions(+), 4 deletions(-)
include/linux/tnum.h +4 -0
@@ -50,6 +50,10 @@ struct tnum tnum_or(struct tnum a, struct tnum b);struct tnum tnum_xor (struct tnum a,struct tnum b);/* Multiply two tnums, return @a * @b */ struct tnum tnum_mul (struct tnum a,struct tnum b); +/* Unsigned division, return @a / @b */ +struct tnum tnum_udiv (struct tnum a,struct tnum b); +/* Signed division, return @a / @b */ +struct tnum tnum_sdiv (struct tnum a,struct tnum b,bool alu32);/* Return true if the known bits of both tnums have the same value */ bool tnum_overlap (struct tnum a,struct tnum b);
kernel/bpf/tnum.c +158 -1
185 lines not on this page · raw message
kernel/bpf/verifier.c +225 -0
249 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c +4 -3
@@ -173,14 +173,15 @@ __naked void flow_keys_illegal_variable_offset_alu(void) asmvolatile (" \ r6 = r1; \ r7 = *(u64*)(r6 + %[flow_keys_off]); \ -r8 = 8; \ -r8 /= 1; \ +call %[bpf_get_prandom_u32]; \ +r8 = r0; \ r8 &= 8; \ r7 += r8; \ r0 = *(u64*)(r7 + 0); \ exit; \ " : - : __imm_const(flow_keys_off, offsetof(struct __sk_buff, flow_keys)) + : __imm_const(flow_keys_off, offsetof(struct __sk_buff, flow_keys)), +__imm (bpf_get_prandom_u32) : __clobber_all); }
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH bpf-next 1/2] bpf: Add interval and tnum analysis for signed and unsigned BPF_DIV
For 32-bit negation, is subtracting from U32_MAX correct? This computes the bitwise NOT (~a) rather than arithmetic negation (-a).
Quoted 2 lines
kernel/bpf/tnum.c +0
-0
[ ... ]
kernel/bpf/tnum.c struct tnum tnum_sub(struct tnum a, struct tnum b) · line 100
Quoted 8 lines
kernel/bpf/tnum.c +8
-0+/* __tnum_neg_width: tnum negation with given bit width. +* @a: the tnum to be negated. +* @width: the bit width to perform negation, 32 or 64. +*/ +static struct tnum __tnum_neg_width (struct tnum a,int width) +{ +if (width ==32 ) +return tnum_sub (TNUM (U32_MAX ,0 ), a);
^^^^^^^
For 32-bit negation, is subtracting from U32_MAX correct? This computes the bitwise NOT (~a) rather than arithmetic negation (-a).
For example, for a = -10 (0xFFFFFFF6 in 32-bit):
U32_MAX - 0xFFFFFFF6 = 9, but |-10| = 10
The 64-bit case correctly uses tnum_sub(TNUM(0, 0), a) for negation. Should the 32-bit case also subtract from zero (with appropriate 32-bit masking)?
This function is called by __tnum_abs() which is used by tnum_sdiv() for 32-bit signed division, so incorrect absolute values would lead to incorrect tnum results.
Quoted 5 lines
kernel/bpf/tnum.c +5
-0+else if (width ==64 ) +return tnum_sub (TNUM (0 ,0 ), a); +else +return tnum_unknown; +}
[ ... ]
Quoted 2 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
kernel/bpf/verifier.c static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, · line 15181
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+static void scalar32_min_max_sdiv (struct bpf_reg_state *dst_reg, +struct bpf_reg_state *src_reg) +{ +u32 *dst_smin = &dst_reg->s32_min_value; +u32 *dst_smax = &dst_reg->s32_max_value; +u32 smin_val = src_reg->s32_min_value; +u32 smax_val = src_reg->s32_max_value;
^^^
Should these be s32 rather than u32? The fields s32_min_value and s32_max_value are declared as s32 in struct bpf_reg_state.
Using u32 causes the signed comparisons below to behave incorrectly:
kernel/bpf/verifier.c
Quoted 1 line
kernel/bpf/verifier.c +1
-0+if (smin_val <=0 && smax_val >=0 ) {
^^^^^^^^^^^^
When s32_min_value contains a negative value like -10 (stored as 0xFFFFFFF6), reading it as u32 gives 4294967286, and the comparison "smin_val <= 0" becomes "4294967286 <= 0" which is always false.
Similarly:
kernel/bpf/verifier.c
Quoted 1 line
kernel/bpf/verifier.c +1
-0+if (smin_val <0 ) {
^^^^^^^^^^^^
This condition can never be true when smin_val is u32.
The 64-bit version scalar_min_max_sdiv() correctly uses s64 types.
[ ... ]
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/20408116978
kernel test robot
build botRe: [PATCH bpf-next 1/2] bpf: Add interval and tnum analysis for signed and unsigned BPF_DIV
In reply to Yazhou Tangkernel test robot noticed the following build warnings:
Hi Yazhou,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Yazhou-Tang/selftests-bpf-Add-tests-for-BPF_DIV-analysis/20251221-174300 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/tencent_7C98FAECA40C98489ACF4515CE346F031509%40qq.com patch subject: [PATCH bpf-next 1/2] bpf: Add interval and tnum analysis for signed and unsigned BPF_DIV config: i386-randconfig-061-20251222 (https://download.01.org/0day-ci/archive/20251223/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251223/[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]/
sparse warnings: (new ones prefixed by >>)
>> kernel/bpf/tnum.c:16:19: sparse: sparse: symbol 'tnum_bottom' was not declared. Should it be static? -- kernel/bpf/verifier.c:23659:38: sparse: sparse: subtraction of functions? Share your drugs >> kernel/bpf/verifier.c:15184:42: sparse: sparse: unsigned value that used to be signed checked against zero?
kernel/bpf/verifier.c:15181:24: sparse: signed value source kernel/bpf/verifier.c:15191:32: sparse: sparse: unsigned value that used to be signed checked against zero? kernel/bpf/verifier.c:15180:24: sparse: signed value source
kernel/bpf/verifier.c: note: in included file (through include/linux/bpf.h, include/linux/bpf-cgroup.h):
include/linux/bpfptr.h:65:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast from non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast to non-scalar include/linux/bpfptr.h:65:40: sparse: sparse: cast from non-scalar
vim +/tnum_bottom +16 kernel/bpf/tnum.c
11
12 #define TNUM(_v, _m) (struct tnum){.value = _v, .mask = _m}
13 /* A completely unknown value */
14 const struct tnum tnum_unknown = { .value = 0, .mask = -1 };
15 /* Tnum bottom */
> 16 const struct tnum tnum_bottom = { .value = -1, .mask = -1 };
17 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH bpf-next 1/2] bpf: Add interval and tnum analysis for signed and unsigned BPF_DIV
In reply to Yazhou Tangkernel test robot noticed the following build errors:
Hi Yazhou,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Yazhou-Tang/selftests-bpf-Add-tests-for-BPF_DIV-analysis/20251221-174300 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/tencent_7C98FAECA40C98489ACF4515CE346F031509%40qq.com patch subject: [PATCH bpf-next 1/2] bpf: Add interval and tnum analysis for signed and unsigned BPF_DIV config: i386-randconfig-053-20251222 (https://download.01.org/0day-ci/archive/20251223/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251223/[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 errors (new ones prefixed by >>):
ld: kernel/bpf/verifier.o: in function `__bpf_sdiv':
>> kernel/bpf/verifier.c:15227:(.text+0xfcb2): undefined reference to `__divdi3' >> ld: kernel/bpf/verifier.c:15227:(.text+0xfd37): undefined reference to `__divdi3' ld: kernel/bpf/verifier.c:15227:(.text+0xfdae): undefined reference to `__divdi3' ld: kernel/bpf/verifier.c:15227:(.text+0xfe0c): undefined reference to `__divdi3' ld: kernel/bpf/verifier.c:15227:(.text+0xff7c): undefined reference to `__divdi3' ld: kernel/bpf/verifier.o: in function `scalar_min_max_udiv': >> kernel/bpf/verifier.c:15138:(.text+0x1b9bb): undefined reference to `__udivdi3' >> ld: kernel/bpf/verifier.c:15139:(.text+0x1b9dc): undefined reference to `__udivdi3' ld: kernel/bpf/tnum.o: in function `tnum_udiv': >> kernel/bpf/tnum.c:227:(.text+0x1b3): undefined reference to `__udivdi3' >> ld: kernel/bpf/tnum.c:219:(.text+0x28d): undefined reference to `__udivdi3' ld: kernel/bpf/tnum.c:227:(.text+0xb05): undefined reference to `__udivdi3' ld: kernel/bpf/tnum.o:kernel/bpf/tnum.c:219: more undefined references to `__udivdi3' follow ld: kernel/bpf/tnum.o: in function `tnum_sdiv': >> kernel/bpf/tnum.c:300:(.text+0x131d): undefined reference to `__divdi3'
vim +15227 kernel/bpf/verifier.c
15118
15119 static void scalar_min_max_udiv(struct bpf_reg_state *dst_reg,
15120 struct bpf_reg_state *src_reg)
15121 {
15122 u64 *dst_umin = &dst_reg->umin_value;
15123 u64 *dst_umax = &dst_reg->umax_value;
15124 u64 umin_val = src_reg->umin_value;
15125 u64 umax_val = src_reg->umax_value;
15126
15127 if (umin_val == 0) {
15128 /* BPF div specification: x / 0 = 0
15129 * 1. If umin_val == umax_val == 0, i.e. divisor is certainly 0,
15130 * then the result must be 0, [a,b] / [0,0] = [0,0].
15131 * 2. If umin_val == 0 && umax_val != 0, then dst_umin = x / 0 = 0,
15132 * dst_umax = dst_umax / 1, remains unchanged, [a,b] / [0,x] = [0,b].
15133 */
15134 *dst_umin = 0;
15135 if (umax_val == 0)
15136 *dst_umax = 0;
15137 } else {
15138 *dst_umin = *dst_umin / umax_val;
15139 *dst_umax = *dst_umax / umin_val;
15140 }
15141
15142 /* Reset signed interval to TOP. */
15143 dst_reg->smin_value = S64_MIN;
15144 dst_reg->smax_value = S64_MAX;
15145 }
15146
15147 static s32 __bpf_sdiv32(s32 a, s32 b)
15148 {
15149 /* BPF div specification: x / 0 = 0 */
15150 if (unlikely(b == 0))
15151 return 0;
15152 /* BPF mod specification: S32_MIN / -1 = S32_MIN */
15153 if (unlikely(a == S32_MIN && b == -1))
15154 return S32_MIN;
15155 return a / b;
15156 }
15157
15158 /* The divisor interval does not cross 0,
15159 * i.e. src_min and src_max have same sign.
15160 */
15161 static void __sdiv32_range(s32 dst_min, s32 dst_max, s32 src_min, s32 src_max,
15162 s32 *res_min, s32 *res_max)
15163 {
15164 s32 tmp_res[4] = {
15165 __bpf_sdiv32(dst_min, src_min),
15166 __bpf_sdiv32(dst_min, src_max),
15167 __bpf_sdiv32(dst_max, src_min),
15168 __bpf_sdiv32(dst_max, src_max)
15169 };
15170
15171 *res_min = min_array(tmp_res, 4);
15172 *res_max = max_array(tmp_res, 4);
15173 }
15174
15175 static void scalar32_min_max_sdiv(struct bpf_reg_state *dst_reg,
15176 struct bpf_reg_state *src_reg)
15177 {
15178 u32 *dst_smin = &dst_reg->s32_min_value;
15179 u32 *dst_smax = &dst_reg->s32_max_value;
15180 u32 smin_val = src_reg->s32_min_value;
15181 u32 smax_val = src_reg->s32_max_value;
15182 s32 res_min, res_max, tmp_min, tmp_max;
15183
15184 if (smin_val <= 0 && smax_val >= 0) {
15185 /* BPF div specification: x / 0 = 0
15186 * Set initial result to 0, as 0 is in divisor interval.
15187 */
15188 res_min = 0;
15189 res_max = 0;
15190 /* negative divisor interval: [a_min,a_max] / [b_min,-1] */
15191 if (smin_val < 0) {
15192 __sdiv32_range(*dst_smin, *dst_smax, smin_val, -1,
15193 &tmp_min, &tmp_max);
15194 __scalar32_min_max_join(&res_min, &res_max, tmp_min, tmp_max);
15195 }
15196 /* positive divisor interval: [a_min,a_max] / [1,b_max] */
15197 if (smax_val > 0) {
15198 __sdiv32_range(*dst_smin, *dst_smax, 1, smax_val,
15199 &tmp_min, &tmp_max);
15200 __scalar32_min_max_join(&res_min, &res_max, tmp_min, tmp_max);
15201 }
15202 } else {
15203 __sdiv32_range(*dst_smin, *dst_smax, smin_val, smax_val,
15204 &res_min, &res_max);
15205 }
15206
15207 /* BPF mod specification: S32_MIN / -1 = S32_MIN */
15208 if (*dst_smin == S32_MIN && smin_val <= -1 && smax_val >= -1)
15209 res_min = S32_MIN;
15210
15211 *dst_smin = res_min;
15212 *dst_smax = res_max;
15213
15214 /* Reset unsigned interval to TOP. */
15215 dst_reg->u32_min_value = 0;
15216 dst_reg->u32_max_value = U32_MAX;
15217 }
15218
15219 static s64 __bpf_sdiv(s64 a, s64 b)
15220 {
15221 /* BPF div specification: x / 0 = 0 */
15222 if (unlikely(b == 0))
15223 return 0;
15224 /* BPF div specification: S64_MIN / -1 = S64_MIN */
15225 if (unlikely(a == S64_MIN && b == -1))
15226 return S64_MIN;
15227 return a / b;
15228 }
15229
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH bpf-next 1/2] bpf: Add interval and tnum analysis for signed and unsigned BPF_DIV
In reply to Yazhou Tangkernel test robot noticed the following build errors:
Hi Yazhou,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/master]
url: https://github.com/intel-lab-lkp/linux/commits/Yazhou-Tang/selftests-bpf-Add-tests-for-BPF_DIV-analysis/20251221-174300 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master patch link: https://lore.kernel.org/r/tencent_7C98FAECA40C98489ACF4515CE346F031509%40qq.com patch subject: [PATCH bpf-next 1/2] bpf: Add interval and tnum analysis for signed and unsigned BPF_DIV config: i386-randconfig-051-20251222 (https://download.01.org/0day-ci/archive/20251224/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251224/[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 errors (new ones prefixed by >>):
>> ld.lld: error: undefined symbol: __divdi3 >>> referenced by tnum.c:300 (kernel/bpf/tnum.c:300) >>> kernel/bpf/tnum.o:(tnum_sdiv) in archive vmlinux.a >>> referenced by verifier.c:15227 (kernel/bpf/verifier.c:15227) >>> kernel/bpf/verifier.o:(scalar_min_max_sdiv) in archive vmlinux.a >>> referenced by verifier.c:15227 (kernel/bpf/verifier.c:15227) >>> kernel/bpf/verifier.o:(scalar_min_max_sdiv) in archive vmlinux.a >>> referenced 6 more times
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH 2/2 selftests/bpf: Add tests for BPF_DIV analysis
In reply to a message not in the archiveFrom: Yazhou Tang <[email protected]>
Now BPF_DIV has value tracking support via interval and tnum analysis. This patch adds selftests to cover various cases of signed and unsigned division operations, including edge cases like division by zero and signed division overflow.
Specifically, these selftests are based on dead code elimination: If the BPF verifier can precisely analyze the result of a division operation, it can prune the path that leads to an error (here we use invalid memory access as the error case), allowing the program to pass verification.
---
Hello everyone,
Thanks for reviewing our patch! This patch adds the necessary selftests for the BPF_DIV range tracking enhancements.
Regarding the test implementation: I noticed multiple patterns for BPF selftests (e.g., out-of-bounds read in verifier_bounds.c, illegal return value in verifier_mul.c and verifier_precision.v). I have opted for the invalid memory access approach with __msg label as it is concise and straightforward.
If the community prefers these tests to be integrated into existing files or follow a different pattern, please let me know and I will gladly refactor them.
Best,
Yazhou Tang
.../selftests/bpf/prog_tests/verifier.c | 2 + .../selftests/bpf/progs/verifier_div_bounds.c | 404 ++++++++++++++++++ 2 files changed, 406 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_div_bounds.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -33,6 +33,7 @@#include "verifier_direct_packet_access.skel.h" #include "verifier_direct_stack_access_wraparound.skel.h" #include "verifier_div0.skel.h" +#include "verifier_div_bounds.skel.h" #include "verifier_div_overflow.skel.h" #include "verifier_global_subprogs.skel.h" #include "verifier_global_ptr_args.skel.h"
@@ -174,6 +175,7 @@ void test_verifier_d_path(void) { RUN(verifier_d_path); }void test_verifier_direct_packet_access (void ) {RUN (verifier_direct_packet_access); }void test_verifier_direct_stack_access_wraparound (void ) {RUN (verifier_direct_stack_access_wraparound); }void test_verifier_div0 (void ) {RUN (verifier_div0); } +void test_verifier_div_bounds (void ) {RUN (verifier_div_bounds); }void test_verifier_div_overflow (void ) {RUN (verifier_div_overflow); }void test_verifier_global_subprogs (void ) {RUN (verifier_global_subprogs); }void test_verifier_global_ptr_args (void ) {RUN (verifier_global_ptr_args); }
tools/testing/selftests/bpf/progs/verifier_div_bounds.c new file +404 -0
405 lines not on this page · raw message
Signature · 1 line
2.52.0