Skip to content

Commit af8908d

Browse files
ahjf-07Kernel Patches Daemon
authored andcommitted
selftests/bpf: filter by pid to avoid cross-test interference
The test installs a kprobe on __sys_connect and checks that bpf_probe_write_user() can modify the syscall argument. However, any concurrent thread in any other test that calls connect() will also trigger the kprobe and have its sockaddr silently overwritten, causing flaky failures in unrelated tests. Constrain the hook to the current test process by filtering on a PID stored as a global variable in .bss. Initialize the .bss value from user space before bpf_object__load() using bpf_map__set_initial_value(), and validate the bss map value size to catch layout mismatches. No new map is introduced and the test keeps the existing non-skeleton flow. Signed-off-by: Sun Jian <sun.jian.kdev@gmail.com> Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
1 parent b72a510 commit af8908d

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ void serial_test_probe_user(void)
2020
struct bpf_program *kprobe_progs[prog_count];
2121
struct bpf_object *obj;
2222
static const int zero = 0;
23+
struct test_pro_bss {
24+
struct sockaddr_in old;
25+
__u32 test_pid;
26+
};
27+
struct test_pro_bss results = {};
2328
size_t i;
2429

2530
obj = bpf_object__open_file(obj_file, &opts);
@@ -34,6 +39,23 @@ void serial_test_probe_user(void)
3439
goto cleanup;
3540
}
3641

42+
{
43+
struct bpf_map *bss_map;
44+
struct test_pro_bss bss_init = {};
45+
46+
bss_init.test_pid = getpid();
47+
bss_map = bpf_object__find_map_by_name(obj, "test_pro.bss");
48+
if (!ASSERT_OK_PTR(bss_map, "find_bss_map"))
49+
goto cleanup;
50+
if (!ASSERT_EQ(bpf_map__value_size(bss_map), sizeof(bss_init),
51+
"bss_size"))
52+
goto cleanup;
53+
err = bpf_map__set_initial_value(bss_map, &bss_init,
54+
sizeof(bss_init));
55+
if (!ASSERT_OK(err, "set_bss_init"))
56+
goto cleanup;
57+
}
58+
3759
err = bpf_object__load(obj);
3860
if (CHECK(err, "obj_load", "err %d\n", err))
3961
goto cleanup;
@@ -62,11 +84,13 @@ void serial_test_probe_user(void)
6284
connect(sock_fd, &curr, sizeof(curr));
6385
close(sock_fd);
6486

65-
err = bpf_map_lookup_elem(results_map_fd, &zero, &tmp);
87+
err = bpf_map_lookup_elem(results_map_fd, &zero, &results);
6688
if (CHECK(err, "get_kprobe_res",
6789
"failed to get kprobe res: %d\n", err))
6890
goto cleanup;
6991

92+
memcpy(&tmp, &results.old, sizeof(tmp));
93+
7094
in = (struct sockaddr_in *)&tmp;
7195
if (CHECK(memcmp(&tmp, &orig, sizeof(orig)), "check_kprobe_res",
7296
"wrong kprobe res from probe read: %s:%u\n",

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55
#include <bpf/bpf_core_read.h>
66
#include "bpf_misc.h"
77

8-
static struct sockaddr_in old;
8+
struct test_pro_bss {
9+
struct sockaddr_in old;
10+
__u32 test_pid;
11+
};
12+
13+
struct test_pro_bss bss;
914

1015
static int handle_sys_connect_common(struct sockaddr_in *uservaddr)
1116
{
1217
struct sockaddr_in new;
18+
__u32 cur = bpf_get_current_pid_tgid() >> 32;
19+
20+
if (bss.test_pid && cur != bss.test_pid)
21+
return 0;
1322

14-
bpf_probe_read_user(&old, sizeof(old), uservaddr);
23+
bpf_probe_read_user(&bss.old, sizeof(bss.old), uservaddr);
1524
__builtin_memset(&new, 0xab, sizeof(new));
1625
bpf_probe_write_user(uservaddr, &new, sizeof(new));
1726

0 commit comments

Comments
 (0)