]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: Add test for bpf_override_return helper
authorJiri Olsa <jolsa@kernel.org>
Mon, 12 Jan 2026 12:11:57 +0000 (13:11 +0100)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 16 Jan 2026 00:15:26 +0000 (16:15 -0800)
We do not actually test the bpf_override_return helper functionality
itself at the moment, only the bpf program being able to attach it.

Adding test that override prctl syscall return value on top of
kprobe and kprobe.multi.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/bpf/20260112121157.854473-2-jolsa@kernel.org
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
tools/testing/selftests/bpf/progs/kprobe_multi_override.c
tools/testing/selftests/bpf/trace_helpers.h

index 6cfaa978bc9af26e88e89d9f8ce3e96c55245da0..9caef222e52885baf319ec033676bd07fb266896 100644 (file)
@@ -1,4 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <errno.h>
+#include <sys/prctl.h>
 #include <test_progs.h>
 #include "kprobe_multi.skel.h"
 #include "trace_helpers.h"
@@ -540,6 +542,46 @@ cleanup:
        kprobe_multi_override__destroy(skel);
 }
 
+static void test_override(void)
+{
+       struct kprobe_multi_override *skel = NULL;
+       int err;
+
+       skel = kprobe_multi_override__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load"))
+               goto cleanup;
+
+       skel->bss->pid = getpid();
+
+       /* no override */
+       err = prctl(0xffff, 0);
+       ASSERT_EQ(err, -1, "err");
+
+       /* kprobe.multi override */
+       skel->links.test_override = bpf_program__attach_kprobe_multi_opts(skel->progs.test_override,
+                                               SYS_PREFIX "sys_prctl", NULL);
+       if (!ASSERT_OK_PTR(skel->links.test_override, "bpf_program__attach_kprobe_multi_opts"))
+               goto cleanup;
+
+       err = prctl(0xffff, 0);
+       ASSERT_EQ(err, 123, "err");
+
+       bpf_link__destroy(skel->links.test_override);
+       skel->links.test_override = NULL;
+
+       /* kprobe override */
+       skel->links.test_kprobe_override = bpf_program__attach_kprobe(skel->progs.test_kprobe_override,
+                                                       false, SYS_PREFIX "sys_prctl");
+       if (!ASSERT_OK_PTR(skel->links.test_kprobe_override, "bpf_program__attach_kprobe"))
+               goto cleanup;
+
+       err = prctl(0xffff, 0);
+       ASSERT_EQ(err, 123, "err");
+
+cleanup:
+       kprobe_multi_override__destroy(skel);
+}
+
 #ifdef __x86_64__
 static void test_attach_write_ctx(void)
 {
@@ -597,6 +639,8 @@ void test_kprobe_multi_test(void)
                test_attach_api_fails();
        if (test__start_subtest("attach_override"))
                test_attach_override();
+       if (test__start_subtest("override"))
+               test_override();
        if (test__start_subtest("session"))
                test_session_skel_api();
        if (test__start_subtest("session_cookie"))
index 28f8487c9059ed502acc8df267d60bf96f64a849..14f39fa6d51586e1a428b908ba100fa47550dbd0 100644 (file)
@@ -5,9 +5,24 @@
 
 char _license[] SEC("license") = "GPL";
 
+int pid = 0;
+
 SEC("kprobe.multi")
 int test_override(struct pt_regs *ctx)
 {
+       if (bpf_get_current_pid_tgid() >> 32 != pid)
+               return 0;
+
+       bpf_override_return(ctx, 123);
+       return 0;
+}
+
+SEC("kprobe")
+int test_kprobe_override(struct pt_regs *ctx)
+{
+       if (bpf_get_current_pid_tgid() >> 32 != pid)
+               return 0;
+
        bpf_override_return(ctx, 123);
        return 0;
 }
index 9437bdd4afa5058b0521b9243ea7e84fa9831ba0..a5576b2dfc267ebcb0eeafad0235875d9c65c9b7 100644 (file)
@@ -4,6 +4,18 @@
 
 #include <bpf/libbpf.h>
 
+#ifdef __x86_64__
+#define SYS_PREFIX "__x64_"
+#elif defined(__s390x__)
+#define SYS_PREFIX "__s390x_"
+#elif defined(__aarch64__)
+#define SYS_PREFIX "__arm64_"
+#elif defined(__riscv)
+#define SYS_PREFIX "__riscv_"
+#else
+#define SYS_PREFIX ""
+#endif
+
 #define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
 #define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)