Skip to contentSkip to conversation

selftests/bpf: filter by pid to avoid cross-test interference

PATCH Applied Sun Jian 3 participants 5 messages Mar 6 – Mar 11

Subsystems (3)
lore
Share & tools
lore

Sun Jian

author
#0 lore raw

PATCH v3 1/2 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.

---

v3:
- use ASSERT_*() instead of CHECK() for the new checks
- simplify patch subject as suggested by Andrii

 .../selftests/bpf/prog_tests/probe_user.c     | 26 ++++++++++++++++++-
 .../selftests/bpf/progs/test_probe_user.c     | 13 ++++++++--
 2 files changed, 36 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/probe_user.c +25 -1
index 8721671321de..b3e17a329cb4 100644
@@ -20,6 +20,11 @@ void serial_test_probe_user(void)
 	struct bpf_program *kprobe_progs[prog_count];
 	struct bpf_object *obj;
 	static const int zero = 0;
+	struct test_pro_bss {
+		struct sockaddr_in old;
+		__u32 test_pid;
+	};
+	struct test_pro_bss results = {};
 	size_t i;
 
 	obj = bpf_object__open_file(obj_file, &opts);
@@ -34,6 +39,23 @@ void serial_test_probe_user(void)
 			goto cleanup;
 	}
 
+	{
+		struct bpf_map *bss_map;
+		struct test_pro_bss bss_init = {};
+
+		bss_init.test_pid = getpid();
+		bss_map = bpf_object__find_map_by_name(obj, "test_pro.bss");
+		if (!ASSERT_OK_PTR(bss_map, "find_bss_map"))
+			goto cleanup;
+		if (!ASSERT_EQ(bpf_map__value_size(bss_map), sizeof(bss_init),
+			       "bss_size"))
+			goto cleanup;
+		err = bpf_map__set_initial_value(bss_map, &bss_init,
+						 sizeof(bss_init));
+		if (!ASSERT_OK(err, "set_bss_init"))
+			goto cleanup;
+	}
+
 	err = bpf_object__load(obj);
 	if (CHECK(err, "obj_load", "err %d\n", err))
 		goto cleanup;
@@ -62,11 +84,13 @@ void serial_test_probe_user(void)
 	connect(sock_fd, &curr, sizeof(curr));
 	close(sock_fd);
 
-	err = bpf_map_lookup_elem(results_map_fd, &zero, &tmp);
+	err = bpf_map_lookup_elem(results_map_fd, &zero, &results);
 	if (CHECK(err, "get_kprobe_res",
 		  "failed to get kprobe res: %d\n", err))
 		goto cleanup;
 
+	memcpy(&tmp, &results.old, sizeof(tmp));
+
 	in = (struct sockaddr_in *)&tmp;
 	if (CHECK(memcmp(&tmp, &orig, sizeof(orig)), "check_kprobe_res",
 		  "wrong kprobe res from probe read: %s:%u\n",
tools/testing/selftests/bpf/progs/test_probe_user.c +11 -2
index a8e501af9604..4bc86c7654b1 100644
@@ -5,13 +5,22 @@
 #include <bpf/bpf_core_read.h>
 #include "bpf_misc.h"
 
-static struct sockaddr_in old;
+struct test_pro_bss {
+	struct sockaddr_in old;
+	__u32 test_pid;
+};
+
+struct test_pro_bss bss;
 
 static int handle_sys_connect_common(struct sockaddr_in *uservaddr)
 {
 	struct sockaddr_in new;
+	__u32 cur = bpf_get_current_pid_tgid() >> 32;
+
+	if (bss.test_pid && cur != bss.test_pid)
+		return 0;
 
-	bpf_probe_read_user(&old, sizeof(old), uservaddr);
+	bpf_probe_read_user(&bss.old, sizeof(bss.old), uservaddr);
 	__builtin_memset(&new, 0xab, sizeof(new));
 	bpf_probe_write_user(uservaddr, &new, sizeof(new));
 

base-commit: 7dff99b354601dd01829e1511711846e04340a69

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 2/2 selftests/bpf: drop serial restriction

Patch 1/2 added PID filtering to the probe_user BPF program to avoid cross-test interference from the global connect() hooks.

With the interference removed, drop the serial_ prefix and remove the stale TODO comment so the test can run in parallel.

Tested:
./test_progs -t probe_user -v
./test_progs -j$(nproc) -t probe_user

---
 tools/testing/selftests/bpf/prog_tests/probe_user.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/probe_user.c +1 -2
index b3e17a329cb4..7a9d13aa2c87 100644
@@ -1,8 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <test_progs.h>
 
-/* TODO: corrupts other tests uses connect() */
-void serial_test_probe_user(void)
+void test_probe_user(void)
 {
 	static const char *const prog_names[] = {
 		"handle_sys_connect",
Signature · 1 line
2.43.0
maintainer Acked-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] selftests/bpf: drop serial restriction

Quoted 7 lines · Sun Jian

Patch 1/2 added PID filtering to the probe_user BPF program to avoid cross-test interference from the global connect() hooks.

With the interference removed, drop the serial_ prefix and remove the stale TODO comment so the test can run in parallel.

Quoted 7 lines

Tested:
./test_progs -t probe_user -v
./test_progs -j$(nproc) -t probe_user

---

Quoted 2 lines

[...]

maintainer Acked-by on v3 1/2
#3 lore raw
In reply to Sun Jian
Quoted 12 lines · Sun Jian

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.

Quoted 5 lines

No new map is introduced and the test keeps the existing non-skeleton flow.

---

Quoted 2 lines

[...]

#4 lore raw
In reply to Sun Jian
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 6 lines · On Fri, 6 Mar 2026 16:33:29 +0800 you wrote:

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.

Quoted 6 lines

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.

[...]

Here is the summary with links:
- [v3,1/2] selftests/bpf: filter by pid to avoid cross-test interference

    https://git.kernel.org/bpf/bpf-next/c/70ce840d5f5b
  - [v3,2/2] selftests/bpf: drop serial restriction
    https://git.kernel.org/bpf/bpf-next/c/aa181c7d642c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html