Skip to content

Commit 93d2c6b

Browse files
nandedamanaKernel Patches Daemon
authored andcommitted
bpf: add selftest to check the verifier's abstract multiplication
This commit adds selftest to test the abstract multiplication technique(s) used by the verifier, following the recent improvement in tnum multiplication (tnum_mul). One of the newly added programs, verifier_mul/mul_precise, results in a false positive with the old tnum_mul, while the program passes with the latest one. Signed-off-by: Nandakumar Edamana <[email protected]>
1 parent d2acc6b commit 93d2c6b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

tools/testing/selftests/bpf/prog_tests/verifier.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "verifier_meta_access.skel.h"
6060
#include "verifier_movsx.skel.h"
6161
#include "verifier_mtu.skel.h"
62+
#include "verifier_mul.skel.h"
6263
#include "verifier_netfilter_ctx.skel.h"
6364
#include "verifier_netfilter_retcode.skel.h"
6465
#include "verifier_bpf_fastcall.skel.h"
@@ -194,6 +195,7 @@ void test_verifier_may_goto_1(void) { RUN(verifier_may_goto_1); }
194195
void test_verifier_may_goto_2(void) { RUN(verifier_may_goto_2); }
195196
void test_verifier_meta_access(void) { RUN(verifier_meta_access); }
196197
void test_verifier_movsx(void) { RUN(verifier_movsx); }
198+
void test_verifier_mul(void) { RUN(verifier_mul); }
197199
void test_verifier_netfilter_ctx(void) { RUN(verifier_netfilter_ctx); }
198200
void test_verifier_netfilter_retcode(void) { RUN(verifier_netfilter_retcode); }
199201
void test_verifier_bpf_fastcall(void) { RUN(verifier_bpf_fastcall); }
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Nandakumar Edamana */
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
#include "bpf_misc.h"
7+
8+
/* The programs here are meant to test the abstract multiplication
9+
* technique(s) used by the verifier. Using assembly to prevent
10+
* compiler optimizations.
11+
*/
12+
13+
SEC("fentry/bpf_fentry_test1")
14+
void BPF_PROG(mul_0, int x)
15+
{
16+
asm volatile ("\
17+
call %[bpf_get_prandom_u32];\
18+
r0 *= 0;\
19+
if r0 != 0 goto l0_%=;\
20+
r0 = 0;\
21+
goto l1_%=;\
22+
l0_%=:\
23+
r0 = 1;\
24+
l1_%=:\
25+
" :
26+
: __imm(bpf_get_prandom_u32)
27+
: __clobber_all);
28+
}
29+
30+
SEC("fentry/bpf_fentry_test1")
31+
__failure __msg("At program exit the register R0 has smin=1 smax=1 should have been in [0, 0]")
32+
void BPF_PROG(mul_uncertain, int x)
33+
{
34+
asm volatile ("\
35+
call %[bpf_get_prandom_u32];\
36+
r0 *= 0x3;\
37+
if r0 != 0 goto l0_%=;\
38+
r0 = 0;\
39+
goto l1_%=;\
40+
l0_%=:\
41+
r0 = 1;\
42+
l1_%=:\
43+
" :
44+
: __imm(bpf_get_prandom_u32)
45+
: __clobber_all);
46+
}
47+
48+
SEC("fentry/bpf_fentry_test1")
49+
void BPF_PROG(mul_precise, int x)
50+
{
51+
/* First, force the verifier to be uncertain about the value:
52+
* unsigned int a = (bpf_get_prandom_u32() & 0x2) | 0x1;
53+
*
54+
* Assuming the verifier is using tnum, a must be tnum{.v=0x1, .m=0x2}.
55+
* Then a * 0x3 would be m0m1 (m for uncertain). Added imprecision
56+
* would cause the following to fail, because the required return value
57+
* is 0:
58+
* return (a * 0x3) & 0x4);
59+
*/
60+
asm volatile ("\
61+
call %[bpf_get_prandom_u32];\
62+
r0 &= 0x2;\
63+
r0 |= 0x1;\
64+
r0 *= 0x3;\
65+
r0 &= 0x4;\
66+
if r0 != 0 goto l0_%=;\
67+
r0 = 0;\
68+
goto l1_%=;\
69+
l0_%=:\
70+
r0 = 1;\
71+
l1_%=:\
72+
" :
73+
: __imm(bpf_get_prandom_u32)
74+
: __clobber_all);
75+
}

0 commit comments

Comments
 (0)