Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH bpf-next v4 1/2] bpf: Add sleepable version of bpf_get_stack in raw_tp_prog_func_proto and tp_prog_func_proto
@ 2026-11-05  9:32 Feng Yang
  2026-11-05  9:32 ` [PATCH bpf-next v4 2/2] selftests/bpf: Test faulting build IDs from a sleepable raw tracepoint Feng Yang
  0 siblings, 1 reply; 3+ messages in thread
From: Feng Yang @ 2026-11-05  9:32 UTC (permalink / raw)
  To: song, jolsa, ihor.solodrai, kpsingh, matt, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, yonghong.song, emil, rostedt,
	mhiramat, mathieu.desnoyers
  Cc: bpf, linux-trace-kernel, linux-kselftest

From: Feng Yang <yangfeng@kylinos.cn>

bpf_get_stack() has two implementations.  bpf_get_stack() passes
may_fault == false and resolves build IDs with build_id_parse_nofault(),
so an IP whose backing file page is not resident falls back to
BPF_STACK_BUILD_ID_IP.  bpf_get_stack_sleepable() passes may_fault == true
and uses build_id_parse_file(), which faults the page in and returns a real
build ID.

Only kprobe/uprobe programs are routed to the sleepable implementation
today, so the other program types that can be sleepable keep losing
build IDs for non-resident pages:

 - tracing_prog_func_proto() serves BPF_TRACE_ITER, fentry/fexit/fmod_ret
   and BPF_TRACE_RAW_TP programs, and falls back to
   raw_tp_prog_func_proto() for BPF_FUNC_get_stack, which is also the
   get_func_proto() of BPF_PROG_TYPE_RAW_TRACEPOINT and is reached by
   BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL through the default cases
   of bpf_lsm_func_proto() and syscall_prog_func_proto().

 - tp_prog_func_proto() serves BPF_PROG_TYPE_TRACEPOINT programs.

Route them to bpf_get_stack_sleepable() as well and mark the new
prototypes might_sleep, as
commit 9d02927fdf4e ("bpf: Mark faultable stack helpers as sleepable")
did for the faultable stack helper prototypes.

The verifier then rejects bpf_get_stack() from these program types
marked sleepable whenever in_sleepable_context() is false:
inside bpf_rcu_read_lock(), bpf_preempt_disable(), and so on. This also
covers calls that do not pass BPF_F_USER_BUILD_ID and would not sleep;
such programs load today. This is consistent with the existing constraints.

Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
---
v4: Update commit messages. Test modification: limit capture to one hit, add s390 flags.

v3: Add selftest. Update commit messages.
https://lore.kernel.org/all/20261104083833.971911-1-yangfeng59949@163.com/

v2: Move regs onto the stack.
https://lore.kernel.org/all/20261102073714.1103088-1-yangfeng59949@163.com/

v1: https://lore.kernel.org/all/20261028093548.790977-1-yangfeng59949@163.com/
---
 kernel/trace/bpf_trace.c | 54 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 2aec56d7f3d3..1229ee06dd71 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -84,6 +84,7 @@ static struct bpf_raw_event_map *bpf_get_raw_tracepoint_module(const char *name)
 
 u64 bpf_get_stackid(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
 u64 bpf_get_stack(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
+u64 bpf_get_stack_sleepable(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
 
 static int bpf_btf_printf_prepare(struct btf_ptr *ptr, u32 btf_ptr_size,
 				  u64 flags, const struct btf **btf,
@@ -1477,6 +1478,26 @@ static const struct bpf_func_proto bpf_get_stack_proto_tp = {
 	.arg4_type	= ARG_ANYTHING,
 };
 
+BPF_CALL_4(bpf_get_stack_tp_sleepable, void *, tp_buff, void *, buf, u32, size,
+	   u64, flags)
+{
+	struct pt_regs *regs = *(struct pt_regs **)tp_buff;
+
+	return bpf_get_stack_sleepable((unsigned long) regs, (unsigned long) buf,
+				       (unsigned long) size, flags, 0);
+}
+
+static const struct bpf_func_proto bpf_get_stack_proto_tp_sleepable = {
+	.func		= bpf_get_stack_tp_sleepable,
+	.gpl_only	= true,
+	.might_sleep	= true,
+	.ret_type	= RET_INTEGER,
+	.arg1_type	= ARG_PTR_TO_CTX,
+	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
+	.arg3_type	= ARG_MEM_SIZE_OR_ZERO,
+	.arg4_type	= ARG_ANYTHING,
+};
+
 static const struct bpf_func_proto *
 tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 {
@@ -1486,7 +1507,8 @@ tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 	case BPF_FUNC_get_stackid:
 		return &bpf_get_stackid_proto_tp;
 	case BPF_FUNC_get_stack:
-		return &bpf_get_stack_proto_tp;
+		return prog->sleepable ? &bpf_get_stack_proto_tp_sleepable
+				       : &bpf_get_stack_proto_tp;
 	case BPF_FUNC_get_attach_cookie:
 		return &bpf_get_attach_cookie_proto_trace;
 	default:
@@ -1716,6 +1738,33 @@ static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = {
 	.arg4_type	= ARG_ANYTHING,
 };
 
+BPF_CALL_4(bpf_get_stack_raw_tp_sleepable, struct bpf_raw_tracepoint_args *, args,
+	   void *, buf, u32, size, u64, flags)
+{
+	/*
+	 * Don't take a per-CPU bpf_raw_tp_regs slot here: those slots are a
+	 * LIFO nest counter and are vulnerable to slot overwrite and
+	 * corruption across sleep.  Sleepable programs never run in
+	 * IRQ/NMI context, so a local pt_regs is sufficient.
+	 */
+	struct pt_regs regs = {};
+
+	perf_fetch_caller_regs(&regs);
+	return bpf_get_stack_sleepable((unsigned long) &regs, (unsigned long) buf,
+				       (unsigned long) size, flags, 0);
+}
+
+static const struct bpf_func_proto bpf_get_stack_proto_raw_tp_sleepable = {
+	.func		= bpf_get_stack_raw_tp_sleepable,
+	.gpl_only	= true,
+	.might_sleep	= true,
+	.ret_type	= RET_INTEGER,
+	.arg1_type	= ARG_PTR_TO_CTX,
+	.arg2_type	= ARG_PTR_TO_UNINIT_MEM,
+	.arg3_type	= ARG_MEM_SIZE_OR_ZERO,
+	.arg4_type	= ARG_ANYTHING,
+};
+
 static const struct bpf_func_proto *
 raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 {
@@ -1725,7 +1774,8 @@ raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 	case BPF_FUNC_get_stackid:
 		return &bpf_get_stackid_proto_raw_tp;
 	case BPF_FUNC_get_stack:
-		return &bpf_get_stack_proto_raw_tp;
+		return prog->sleepable ? &bpf_get_stack_proto_raw_tp_sleepable
+				       : &bpf_get_stack_proto_raw_tp;
 	case BPF_FUNC_get_attach_cookie:
 		return &bpf_get_attach_cookie_proto_tracing;
 	default:
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH bpf-next v4 2/2] selftests/bpf: Test faulting build IDs from a sleepable raw tracepoint
  2026-11-05  9:32 [PATCH bpf-next v4 1/2] bpf: Add sleepable version of bpf_get_stack in raw_tp_prog_func_proto and tp_prog_func_proto Feng Yang
@ 2026-11-05  9:32 ` Feng Yang
  2026-11-05  9:41   ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Feng Yang @ 2026-11-05  9:32 UTC (permalink / raw)
  To: song, jolsa, ihor.solodrai, kpsingh, matt, ast, daniel, andrii,
	eddyz87, memxor, martin.lau, yonghong.song, emil, rostedt,
	mhiramat, mathieu.desnoyers
  Cc: bpf, linux-trace-kernel, linux-kselftest

From: Feng Yang <yangfeng@kylinos.cn>

Extend the build_id test with a sleepable raw tracepoint on sys_enter.
A helper process pages out the build ID page and issues a getpid()
syscall; the BPF program filters on the target tgid and syscall id,
records only the first hit, and captures the user stack with
BPF_F_USER_BUILD_ID.  The test asserts that one of the frames carries
uprobe_multi's build ID.

Frame 0 is libc's syscall wrapper, so reaching uprobe_multi needs user
stack unwinding; build uprobe_multi with -mbackchain on s390, where the
unwinder follows backchains.  The non-sleepable control requires at least
one BPF_STACK_BUILD_ID_IP frame, so it cannot pass just because the walk
never reached uprobe_multi.

Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
---
 tools/testing/selftests/bpf/Makefile          |   4 +-
 .../selftests/bpf/prog_tests/build_id.c       | 134 ++++++++++++++++++
 .../selftests/bpf/progs/test_build_id.c       |  46 ++++++
 tools/testing/selftests/bpf/uprobe_multi.c    |  37 ++++-
 4 files changed, 216 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 77682bf28c6e..367166383246 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -650,7 +650,9 @@ $(OUTPUT)/veristat: $(OUTPUT)/veristat.o
 	$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
 
 # Linking uprobe_multi can fail due to relocation overflows on mips.
-$(OUTPUT)/uprobe_multi: CFLAGS += $(if $(filter mips, $(ARCH)),-mxgot)
+# s390's user unwinder follows backchains, keep them for the build_id test.
+$(OUTPUT)/uprobe_multi: CFLAGS += $(if $(filter mips, $(ARCH)),-mxgot) \
+				$(if $(filter s390 s390x, $(ARCH)),-mbackchain)
 $(OUTPUT)/uprobe_multi: uprobe_multi.c uprobe_multi.ld
 	$(call msg,BINARY,,$@)
 	$(Q)$(CC) $(CFLAGS) -Wl,-T,uprobe_multi.ld -O0 $(LDFLAGS) 	\
diff --git a/tools/testing/selftests/bpf/prog_tests/build_id.c b/tools/testing/selftests/bpf/prog_tests/build_id.c
index aec9c8d6bc96..98e3d0998179 100644
--- a/tools/testing/selftests/bpf/prog_tests/build_id.c
+++ b/tools/testing/selftests/bpf/prog_tests/build_id.c
@@ -1,5 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
+#include <signal.h>
+#include <sys/syscall.h>
 #include <test_progs.h>
 
 #include "test_build_id.skel.h"
@@ -104,6 +106,134 @@ static void subtest_sleepable(void)
 	test_build_id__destroy(skel);
 }
 
+static bool stack_has_build_id(const struct bpf_stack_build_id *stack, int frame_cnt,
+			       const char *id, int id_sz)
+{
+	int i;
+
+	for (i = 0; i < frame_cnt; i++)
+		if (stack[i].status == BPF_STACK_BUILD_ID_VALID &&
+		    !memcmp(stack[i].build_id, id, id_sz))
+			return true;
+
+	return false;
+}
+
+static bool stack_has_ip(const struct bpf_stack_build_id *stack, int frame_cnt)
+{
+	int i;
+
+	for (i = 0; i < frame_cnt; i++)
+		if (stack[i].status == BPF_STACK_BUILD_ID_IP)
+			return true;
+
+	return false;
+}
+
+/*
+ * Fork uprobe_multi, let it page out the build ID page and issue the
+ * filtered syscall.  target_tgid/target_syscall are set before the child is
+ * released, so only its getpid() is recorded, and the BPF program records
+ * only the first hit.
+ */
+static int run_uprobe_multi_syscall(struct test_build_id *skel)
+{
+	char c = 'x';
+	int pfd[2], status, err;
+	pid_t pid;
+
+	if (pipe(pfd))
+		return -errno;
+
+	pid = fork();
+	if (pid < 0) {
+		err = -errno;
+		close(pfd[0]);
+		close(pfd[1]);
+		return err;
+	}
+	if (pid == 0) {
+		close(pfd[1]);
+		if (read(pfd[0], &c, 1) != 1)
+			_exit(127);
+		close(pfd[0]);
+		execl("./uprobe_multi", "uprobe_multi", "paged-out-syscall", NULL);
+		_exit(127);
+	}
+
+	close(pfd[0]);
+	skel->bss->target_tgid = pid;
+	skel->bss->target_syscall = SYS_getpid;
+	if (write(pfd[1], &c, 1) != 1) {
+		err = -errno;
+		close(pfd[1]);
+		kill(pid, SIGKILL);
+		waitpid(pid, NULL, 0);
+		return err;
+	}
+	close(pfd[1]);
+
+	if (waitpid(pid, &status, 0) < 0)
+		return -errno;
+	if (WIFEXITED(status))
+		return WEXITSTATUS(status);
+	return -1;
+}
+
+static void subtest_raw_tp(bool sleepable)
+{
+	struct test_build_id *skel;
+	struct bpf_stack_build_id *stack;
+	int frame_cnt, err, res;
+
+	skel = test_build_id__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open"))
+		return;
+
+	if (sleepable) {
+		skel->links.rawtp_sleepable = bpf_program__attach(skel->progs.rawtp_sleepable);
+		if (!ASSERT_OK_PTR(skel->links.rawtp_sleepable, "attach_rawtp_sleepable"))
+			goto cleanup;
+	} else {
+		skel->links.rawtp_nofault = bpf_program__attach(skel->progs.rawtp_nofault);
+		if (!ASSERT_OK_PTR(skel->links.rawtp_nofault, "attach_rawtp_nofault"))
+			goto cleanup;
+	}
+
+	err = run_uprobe_multi_syscall(skel);
+	if (err == 2) {
+		/* could not evict the build ID page, nothing to test */
+		test__skip();
+		goto cleanup;
+	}
+	if (!ASSERT_OK(err, "trigger_uprobe_multi"))
+		goto cleanup;
+
+	if (sleepable) {
+		res = skel->bss->res_rawtp_sleepable;
+		stack = skel->bss->stack_rawtp_sleepable;
+	} else {
+		res = skel->bss->res_rawtp_nofault;
+		stack = skel->bss->stack_rawtp_nofault;
+	}
+	frame_cnt = res / sizeof(struct bpf_stack_build_id);
+	if (env.verbosity >= VERBOSE_NORMAL)
+		print_stack(stack, frame_cnt);
+
+	ASSERT_GT(frame_cnt, 0, "frames");
+	if (sleepable) {
+		ASSERT_TRUE(stack_has_build_id(stack, frame_cnt, build_id, build_id_sz),
+			    "sleepable_build_id");
+	} else {
+		ASSERT_TRUE(stack_has_ip(stack, frame_cnt), "nofault_has_ip");
+		ASSERT_FALSE(stack_has_build_id(stack, frame_cnt, build_id, build_id_sz),
+			     "nofault_has_build_id");
+	}
+
+cleanup:
+	test_build_id__destroy(skel);
+}
+
 void serial_test_build_id(void)
 {
 	build_id_sz = read_build_id("uprobe_multi", build_id, sizeof(build_id));
@@ -115,4 +245,8 @@ void serial_test_build_id(void)
 		subtest_nofault(true /* resident */);
 	if (test__start_subtest("sleepable"))
 		subtest_sleepable();
+	if (test__start_subtest("raw-tp-nofault-paged-out"))
+		subtest_raw_tp(false /* non-sleepable */);
+	if (test__start_subtest("raw-tp-sleepable-paged-out"))
+		subtest_raw_tp(true /* sleepable */);
 }
diff --git a/tools/testing/selftests/bpf/progs/test_build_id.c b/tools/testing/selftests/bpf/progs/test_build_id.c
index 32ce59f9aa27..7c44195de875 100644
--- a/tools/testing/selftests/bpf/progs/test_build_id.c
+++ b/tools/testing/selftests/bpf/progs/test_build_id.c
@@ -3,6 +3,7 @@
 
 #include "vmlinux.h"
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
 
 struct bpf_stack_build_id stack_sleepable[128];
 int res_sleepable;
@@ -28,4 +29,49 @@ int uprobe_sleepable(struct pt_regs *ctx)
 	return 0;
 }
 
+struct bpf_stack_build_id stack_rawtp_sleepable[128];
+int res_rawtp_sleepable;
+
+struct bpf_stack_build_id stack_rawtp_nofault[128];
+int res_rawtp_nofault;
+
+int target_tgid;
+int target_syscall;
+
+SEC("raw_tp/sys_enter")
+int BPF_PROG(rawtp_nofault, struct pt_regs *regs, long id)
+{
+	if ((u32)(bpf_get_current_pid_tgid() >> 32) != (u32)target_tgid ||
+	    id != target_syscall)
+		return 0;
+
+	/* Record only the first hit, a later getpid() must not overwrite it. */
+	if (res_rawtp_nofault)
+		return 0;
+
+	res_rawtp_nofault = bpf_get_stack(ctx, stack_rawtp_nofault,
+					  sizeof(stack_rawtp_nofault),
+					  BPF_F_USER_STACK | BPF_F_USER_BUILD_ID);
+
+	return 0;
+}
+
+SEC("raw_tp.s/sys_enter")
+int BPF_PROG(rawtp_sleepable, struct pt_regs *regs, long id)
+{
+	if ((u32)(bpf_get_current_pid_tgid() >> 32) != (u32)target_tgid ||
+	    id != target_syscall)
+		return 0;
+
+	/* Record only the first hit, a later getpid() must not overwrite it. */
+	if (res_rawtp_sleepable)
+		return 0;
+
+	res_rawtp_sleepable = bpf_get_stack(ctx, stack_rawtp_sleepable,
+					    sizeof(stack_rawtp_sleepable),
+					    BPF_F_USER_STACK | BPF_F_USER_BUILD_ID);
+
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/uprobe_multi.c b/tools/testing/selftests/bpf/uprobe_multi.c
index 0af330b6c364..6e37ba757157 100644
--- a/tools/testing/selftests/bpf/uprobe_multi.c
+++ b/tools/testing/selftests/bpf/uprobe_multi.c
@@ -5,6 +5,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <sys/mman.h>
+#include <sys/syscall.h>
 #include <unistd.h>
 #include <sdt.h>
 
@@ -95,12 +96,12 @@ static int usdt(void)
 extern char build_id_start[];
 extern char build_id_end[];
 
-int __attribute__((weak)) trigger_uprobe(bool build_id_resident)
+/* Returns 2 if the build ID page could not be evicted. */
+static int prep_build_id_page(bool build_id_resident)
 {
 	int page_sz = sysconf(_SC_PAGESIZE);
-	void *addr;
-
 	unsigned char vec[1];
+	void *addr;
 	int poll = 0;
 
 	/* page-align build ID start */
@@ -123,12 +124,38 @@ int __attribute__((weak)) trigger_uprobe(bool build_id_resident)
 			poll++;
 			usleep(100);
 		} while (poll < 500);
+		if (vec[0] & 1)
+			return 2;
 	}
+	return 0;
+}
+
+int __attribute__((weak)) trigger_uprobe(bool build_id_resident)
+{
+	(void)prep_build_id_page(build_id_resident);
+
 	(void)uprobe();
 
 	return 0;
 }
 
+int __attribute__((weak)) trigger_syscall(void)
+{
+	int err = prep_build_id_page(false /* page-out build ID */);
+
+	if (err)
+		return err;
+
+	/*
+	 * The BPF program under test filters on this syscall and records only
+	 * its first hit, so later getpid() calls (e.g. from exit paths) cannot
+	 * overwrite the captured stack.
+	 */
+	(void)syscall(SYS_getpid);
+
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	if (argc != 2)
@@ -142,10 +169,12 @@ int main(int argc, char **argv)
 		return trigger_uprobe(false /* page-out build ID */);
 	if (!strcmp("uprobe-paged-in", argv[1]))
 		return trigger_uprobe(true /* page-in build ID */);
+	if (!strcmp("paged-out-syscall", argv[1]))
+		return trigger_syscall();
 
 error:
 	fprintf(stderr,
-		"usage: %s <bench|usdt|uprobe-paged-out|uprobe-paged-in>\n",
+		"usage: %s <bench|usdt|uprobe-paged-{in,out}|paged-out-syscall>\n",
 		argv[0]);
 	return -1;
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next v4 2/2] selftests/bpf: Test faulting build IDs from a sleepable raw tracepoint
  2026-11-05  9:32 ` [PATCH bpf-next v4 2/2] selftests/bpf: Test faulting build IDs from a sleepable raw tracepoint Feng Yang
@ 2026-11-05  9:41   ` sashiko-bot
  0 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-05  9:41 UTC (permalink / raw)
  To: Feng Yang; +Cc: bpf, linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] selftests/bpf: race setting target_tgid before target_syscall
- [Medium] selftests/bpf: unchecked signed-unsigned division in subtest_raw_tp()
--

commit 2b4f6f5d972293b188ccb421c050655343b0d0ee
Author: Feng Yang <yangfeng@kylinos.cn>

selftests/bpf: Test faulting build IDs from a sleepable raw tracepoint

This commit extends the build_id selftest with sleepable and non-sleepable
raw tracepoint subtests on sys_enter to verify user stack build ID extraction
when the build ID page is paged out.

> diff --git a/tools/testing/selftests/bpf/prog_tests/build_id.c b/tools/testing/selftests/bpf/prog_tests/build_id.c
> index aec9c8d6bc96b..98e3d09981793 100644
> --- a/tools/testing/selftests/bpf/prog_tests/build_id.c
> +++ b/tools/testing/selftests/bpf/prog_tests/build_id.c
[ ... ]
> +static int run_uprobe_multi_syscall(struct test_build_id *skel)
> +{
> +	char c = 'x';
> +	int pfd[2], status, err;
> +	pid_t pid;
> +
> +	if (pipe(pfd))
> +		return -errno;
> +
> +	pid = fork();
> +	if (pid < 0) {
> +		err = -errno;
> +		close(pfd[0]);
> +		close(pfd[1]);
> +		return err;
> +	}
> +	if (pid == 0) {
> +		close(pfd[1]);
> +		if (read(pfd[0], &c, 1) != 1)
> +			_exit(127);
> +		close(pfd[0]);
> +		execl("./uprobe_multi", "uprobe_multi", "paged-out-syscall", NULL);
> +		_exit(127);
> +	}
> +
> +	close(pfd[0]);
> +	skel->bss->target_tgid = pid;
> +	skel->bss->target_syscall = SYS_getpid;

[Severity: Medium]
Can setting target_tgid before target_syscall in run_uprobe_multi_syscall()
race with the child's read(pfd[0], &c, 1) on architectures where SYS_read is 0
(such as x86_64)?

In subtest_raw_tp(), skel->bss is zero-initialized (so target_syscall is 0)
and rawtp_sleepable or rawtp_nofault is already attached before calling
run_uprobe_multi_syscall():

subtest_raw_tp()
  bpf_program__attach()
  run_uprobe_multi_syscall()
    fork()

If the child enters sys_enter for read(pfd[0], &c, 1) after the parent stores
skel->bss->target_tgid = pid but before skel->bss->target_syscall = SYS_getpid
becomes visible, rawtp_nofault or rawtp_sleepable in
tools/testing/selftests/bpf/progs/test_build_id.c sees:

	if ((u32)(bpf_get_current_pid_tgid() >> 32) != (u32)target_tgid ||
	    id != target_syscall)
		return 0;

	/* Record only the first hit, a later getpid() must not overwrite it. */
	if (res_rawtp_sleepable)
		return 0;

Because id == 0 matches target_syscall == 0, the program records the stack of
the forked test_progs process before execl() and ignores the subsequent
SYS_getpid in uprobe_multi, causing the uprobe_multi build ID assertion to
fail. Should target_syscall be initialized before attaching or before setting
target_tgid?

> +	if (write(pfd[1], &c, 1) != 1) {
[ ... ]
> +static void subtest_raw_tp(bool sleepable)
> +{
[ ... ]
> +	if (sleepable) {
> +		res = skel->bss->res_rawtp_sleepable;
> +		stack = skel->bss->stack_rawtp_sleepable;
> +	} else {
> +		res = skel->bss->res_rawtp_nofault;
> +		stack = skel->bss->stack_rawtp_nofault;
> +	}
> +	frame_cnt = res / sizeof(struct bpf_stack_build_id);
> +	if (env.verbosity >= VERBOSE_NORMAL)
> +		print_stack(stack, frame_cnt);
> +
> +	ASSERT_GT(frame_cnt, 0, "frames");

[Severity: Medium]
If bpf_get_stack() returns a negative errno in res_rawtp_sleepable or
res_rawtp_nofault, does dividing the signed int res by the unsigned size_t
sizeof(struct bpf_stack_build_id) convert res to a large unsigned value before
checking if it succeeded?

On 32-bit architectures, where int and size_t are both 32-bit, a negative res
such as -EFAULT (-14) becomes 0xfffffff2U / 32 = 134217727 in frame_cnt, which
passes ASSERT_GT(frame_cnt, 0, "frames") and overflows the 128-element
stack_rawtp_sleepable or stack_rawtp_nofault array in print_stack(),
stack_has_build_id(), and stack_has_ip(). Even on 64-bit targets, a failed
ASSERT_GT(frame_cnt, 0, "frames") falls through to the subsequent stack checks
instead of jumping to cleanup.

Should subtest_raw_tp() check res before computing frame_cnt, matching
subtest_sleepable() in the same file:

	if (!ASSERT_GT(skel->bss->res_sleepable, 0, "res"))
		goto cleanup;

	stack = skel->bss->stack_sleepable;
	frame_cnt = skel->bss->res_sleepable / sizeof(struct bpf_stack_build_id);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261105093212.826479-1-yangfeng59949@163.com?part=2

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-05  9:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-05  9:32 [PATCH bpf-next v4 1/2] bpf: Add sleepable version of bpf_get_stack in raw_tp_prog_func_proto and tp_prog_func_proto Feng Yang
2026-11-05  9:32 ` [PATCH bpf-next v4 2/2] selftests/bpf: Test faulting build IDs from a sleepable raw tracepoint Feng Yang
2026-11-05  9:41   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox