Thank you for your contribution! Sashiko AI review found 2 potential issue(s) 
to consider:
- [Medium] selftests/bpf: raise(SIGSTOP) in trigger_stop() overwrites recorded 
stack
- [Medium] selftests/bpf: broken s390x unwind and missing IP check in 
subtest_raw_tp()
--

commit f32a6f825aa7edd03a124636be8381204465d887
Author: Feng Yang <[email protected]>

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

This commit extends the build_id selftest with sleepable and non-sleepable
raw tracepoints on sys_enter to test faulting in build IDs when the build
ID page of uprobe_multi has been evicted.

> diff --git a/tools/testing/selftests/bpf/prog_tests/build_id.c 
> b/tools/testing/selftests/bpf/prog_tests/build_id.c
> index aec9c8d6bc96b..885dc3da5900b 100644
> --- a/tools/testing/selftests/bpf/prog_tests/build_id.c
> +++ b/tools/testing/selftests/bpf/prog_tests/build_id.c

[ ... ]

> @@ -104,6 +106,138 @@ 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 void subtest_raw_tp(bool sleepable)
> +{

[ ... ]

> +     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_FALSE(stack_has_build_id(stack, frame_cnt, build_id, 
> build_id_sz),
> +                          "nofault_build_id");

[Severity: Medium]
In trigger_stop(), uprobe_multi invokes the syscall via libc's syscall()
wrapper:

    subtest_raw_tp()
      -> run_uprobe_multi_stopped()
        -> trigger_stop()
          -> syscall(SYS_getpid)

When rawtp_nofault or rawtp_sleepable fires on sys_enter and calls
bpf_get_stack() with BPF_F_USER_STACK | BPF_F_USER_BUILD_ID, stack[0] is
inside libc.so rather than uprobe_multi, so reaching uprobe_multi requires
perf_callchain_user() to unwind user stack frames past libc.so.

On s390x, arch_stack_walk_user_common() in arch/s390/kernel/stacktrace.c
unwinds user stacks via sf->back_chain. Because uprobe_multi is built with
-fno-omit-frame-pointer but without -mbackchain in
tools/testing/selftests/bpf/Makefile, sf->back_chain is 0 and unwinding
stops after stack[0] in libc.so. Will raw-tp-sleepable-paged-out fail on
s390x because stack_has_build_id() never sees a frame from uprobe_multi?

Also, the commit message states that for the non-sleepable control:

    the same frame must be reported as IP, which also proves that the page
    was really evicted.

However, the non-sleepable branch in subtest_raw_tp() only asserts that
stack_has_build_id() is false without checking that any frame has status
BPF_STACK_BUILD_ID_IP. If unwinding stops at stack[0] in libc.so (whose
build ID is resident and valid), won't the non-sleepable subtest pass even
though uprobe_multi was never reached and no frame was reported with
BPF_STACK_BUILD_ID_IP?

[ ... ]

> diff --git a/tools/testing/selftests/bpf/progs/test_build_id.c 
> b/tools/testing/selftests/bpf/progs/test_build_id.c
> index 32ce59f9aa27b..a6d6712888dc9 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,41 @@ 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;
> +
> +     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;
> +
> +     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 0af330b6c3646..ef44d86962c13 100644
> --- a/tools/testing/selftests/bpf/uprobe_multi.c
> +++ b/tools/testing/selftests/bpf/uprobe_multi.c

[ ... ]

> @@ -123,12 +125,39 @@ int __attribute__((weak)) trigger_uprobe(bool 
> build_id_resident)

[ ... ]

> +int __attribute__((weak)) trigger_stop(void)
> +{
> +     int err = prep_build_id_page(false /* page-out build ID */);
> +
> +     if (err)
> +             return err;
> +
> +     /*
> +      * Make one syscall that the BPF program under test filters on, then
> +      * stop this process so that the recorded stack cannot be overwritten
> +      * by another syscall while the caller inspects it.
> +      */
> +     (void)syscall(SYS_getpid);
> +     raise(SIGSTOP);

[Severity: Medium]
Does raise(SIGSTOP) here end up issuing a second SYS_getpid syscall that
overwrites stack_rawtp_nofault and stack_rawtp_sleepable?

In glibc, raise(SIGSTOP) calls __pthread_kill_implementation(), which calls
__getpid() and executes SYS_getpid before invoking tgkill(). Because
rawtp_nofault and rawtp_sleepable in
tools/testing/selftests/bpf/progs/test_build_id.c only filter on
target_tgid and target_syscall without disarming after the first hit, the
SYS_getpid inside raise(SIGSTOP) will trigger the BPF program a second
time.

In raw-tp-sleepable-paged-out, the first syscall(SYS_getpid) has already
faulted in uprobe_multi's build ID page, so the second call records the
stack when the page is already resident. Furthermore, when libc.so is built
with -fomit-frame-pointer, unwinding out of __pthread_kill_implementation()
during the second SYS_getpid can fail to reach uprobe_multi, overwriting
the valid trace captured during syscall(SYS_getpid) and causing the
sleepable_build_id assertion in subtest_raw_tp() to fail.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=2

Reply via email to