Skip to content

Commit 7edc3fc

Browse files
Alexei Starovoitovborkmann
authored andcommitted
selftest/bpf: Add a test that reads various addresses.
Add a function to bpf_testmod that returns invalid kernel and user addresses. Then attach an fexit program to that function that tries to read memory through these addresses. This logic checks that bpf_probe_read_kernel and BPF_PROBE_MEM logic is sane. Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 588a25e commit 7edc3fc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ noinline int bpf_testmod_loop_test(int n)
3333
return sum;
3434
}
3535

36+
__weak noinline struct file *bpf_testmod_return_ptr(int arg)
37+
{
38+
static struct file f = {};
39+
40+
switch (arg) {
41+
case 1: return (void *)EINVAL; /* user addr */
42+
case 2: return (void *)0xcafe4a11; /* user addr */
43+
case 3: return (void *)-EINVAL; /* canonical, but invalid */
44+
case 4: return (void *)(1ull << 60); /* non-canonical and invalid */
45+
case 5: return (void *)~(1ull << 30); /* trigger extable */
46+
case 6: return &f; /* valid addr */
47+
case 7: return (void *)((long)&f | 1); /* kernel tricks */
48+
default: return NULL;
49+
}
50+
}
51+
3652
noinline ssize_t
3753
bpf_testmod_test_read(struct file *file, struct kobject *kobj,
3854
struct bin_attribute *bin_attr,
@@ -43,6 +59,10 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj,
4359
.off = off,
4460
.len = len,
4561
};
62+
int i = 1;
63+
64+
while (bpf_testmod_return_ptr(i))
65+
i++;
4666

4767
/* This is always true. Use the check to make sure the compiler
4868
* doesn't remove bpf_testmod_loop_test.

tools/testing/selftests/bpf/progs/test_module_attach.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ int BPF_PROG(handle_fexit,
8787
return 0;
8888
}
8989

90+
SEC("fexit/bpf_testmod_return_ptr")
91+
int BPF_PROG(handle_fexit_ret, int arg, struct file *ret)
92+
{
93+
long buf = 0;
94+
95+
bpf_probe_read_kernel(&buf, 8, ret);
96+
bpf_probe_read_kernel(&buf, 8, (char *)ret + 256);
97+
*(volatile long long *)ret;
98+
*(volatile int *)&ret->f_mode;
99+
return 0;
100+
}
101+
90102
__u32 fmod_ret_read_sz = 0;
91103

92104
SEC("fmod_ret/bpf_testmod_test_read")

0 commit comments

Comments
 (0)