Skip to content

Commit 2660b9d

Browse files
nandedamanaanakryiko
authored andcommitted
bpf: Add selftest to check the verifier's abstract multiplication
Add new 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]> Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Harishankar Vishwanathan <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 1df7dad commit 2660b9d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
/* Intended to test the abstract multiplication technique(s) used by
9+
* the verifier. Using assembly to avoid compiler optimizations.
10+
*/
11+
SEC("fentry/bpf_fentry_test1")
12+
void BPF_PROG(mul_precise, int x)
13+
{
14+
/* First, force the verifier to be uncertain about the value:
15+
* unsigned int a = (bpf_get_prandom_u32() & 0x2) | 0x1;
16+
*
17+
* Assuming the verifier is using tnum, a must be tnum{.v=0x1, .m=0x2}.
18+
* Then a * 0x3 would be m0m1 (m for uncertain). Added imprecision
19+
* would cause the following to fail, because the required return value
20+
* is 0:
21+
* return (a * 0x3) & 0x4);
22+
*/
23+
asm volatile ("\
24+
call %[bpf_get_prandom_u32];\
25+
r0 &= 0x2;\
26+
r0 |= 0x1;\
27+
r0 *= 0x3;\
28+
r0 &= 0x4;\
29+
if r0 != 0 goto l0_%=;\
30+
r0 = 0;\
31+
goto l1_%=;\
32+
l0_%=:\
33+
r0 = 1;\
34+
l1_%=:\
35+
" :
36+
: __imm(bpf_get_prandom_u32)
37+
: __clobber_all);
38+
}

0 commit comments

Comments
 (0)