Skip to content

Commit 0f4702e

Browse files
yonghong-songNobody
authored and
Nobody
committed
selftests/bpf: add a selftest with __user tag
Added a selftest where the argument is a pointer with __user tag. Directly accessing its field without helper will result verification failure. $ ./test_progs -v -n 21/3 ... Successfully loaded bpf_testmod.ko. test_btf_type_tag_user:PASS:btf_type_tag_user 0 nsec libbpf: load bpf program failed: Permission denied libbpf: -- BEGIN DUMP LOG --- libbpf: R1 type=ctx expected=fp ; int BPF_PROG(sub, struct bpf_testmod_btf_type_tag *arg) 0: (79) r1 = *(u64 *)(r1 +0) func 'bpf_testmod_test_btf_type_tag_user' arg0 accesses user memory invalid bpf_context access off=0 size=8 processed 1 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 ... test_btf_type_tag_user:PASS:btf_type_tag_user 0 nsec #21/3 btf_tag/btf_type_tag_user:OK #21 btf_tag:OK Summary: 1/1 PASSED, 0 SKIPPED, 0 FAILED Successfully unloaded bpf_testmod.ko. Signed-off-by: Yonghong Song <[email protected]>
1 parent 7ea203f commit 0f4702e

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ bpf_testmod_test_mod_kfunc(int i)
2121
*(int *)this_cpu_ptr(&bpf_testmod_ksym_percpu) = i;
2222
}
2323

24+
struct bpf_testmod_btf_type_tag {
25+
int a;
26+
};
27+
28+
noinline int
29+
bpf_testmod_test_btf_type_tag_user(struct bpf_testmod_btf_type_tag __user *arg) {
30+
return arg->a;
31+
}
32+
2433
noinline int bpf_testmod_loop_test(int n)
2534
{
2635
int i, sum = 0;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct btf_type_tag_test {
88
int **p;
99
};
1010
#include "btf_type_tag.skel.h"
11+
#include "btf_type_tag_user.skel.h"
1112

1213
static void test_btf_decl_tag(void)
1314
{
@@ -41,10 +42,32 @@ static void test_btf_type_tag(void)
4142
btf_type_tag__destroy(skel);
4243
}
4344

45+
static void test_btf_type_tag_user(void)
46+
{
47+
struct btf_type_tag_user *skel;
48+
int err;
49+
50+
skel = btf_type_tag_user__open();
51+
if (!ASSERT_OK_PTR(skel, "btf_type_tag_user"))
52+
return;
53+
54+
if (skel->rodata->skip_tests) {
55+
printf("%s:SKIP: btf_type_tag attribute not supported", __func__);
56+
test__skip();
57+
} else {
58+
err = btf_type_tag_user__load(skel);
59+
ASSERT_ERR(err, "btf_type_tag_user");
60+
}
61+
62+
btf_type_tag_user__destroy(skel);
63+
}
64+
4465
void test_btf_tag(void)
4566
{
4667
if (test__start_subtest("btf_decl_tag"))
4768
test_btf_decl_tag();
4869
if (test__start_subtest("btf_type_tag"))
4970
test_btf_type_tag();
71+
if (test__start_subtest("btf_type_tag_user"))
72+
test_btf_type_tag_user();
5073
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2021 Facebook */
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
7+
#if __has_attribute(btf_type_tag)
8+
volatile const bool skip_tests = false;
9+
#else
10+
volatile const bool skip_tests = true;
11+
#endif
12+
13+
struct bpf_testmod_btf_type_tag {
14+
int a;
15+
};
16+
17+
int g;
18+
19+
SEC("fentry/bpf_testmod_test_btf_type_tag_user")
20+
int BPF_PROG(sub, struct bpf_testmod_btf_type_tag *arg)
21+
{
22+
g = arg->a;
23+
return 0;
24+
}

0 commit comments

Comments
 (0)