Skip to content

Commit

Permalink
selftests/bpf: Add uretprobe syscall call from user space test
Browse files Browse the repository at this point in the history
Adding test to verify that when called from outside of the
trampoline provided by kernel, the uretprobe syscall will cause
calling process to receive SIGILL signal and the attached bpf
program is not executed.

Link: https://lore.kernel.org/all/20240611112158.40795-8-jolsa@kernel.org/

Acked-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
  • Loading branch information
Jiri Olsa authored and Masami Hiramatsu (Google) committed Jun 11, 2024
1 parent f42a58f commit 9e7f74e
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
95 changes: 95 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#include <unistd.h>
#include <asm/ptrace.h>
#include <linux/compiler.h>
#include <linux/stringify.h>
#include <sys/wait.h>
#include "uprobe_syscall.skel.h"
#include "uprobe_syscall_executed.skel.h"

__naked unsigned long uretprobe_regs_trigger(void)
{
Expand Down Expand Up @@ -209,6 +212,91 @@ static void test_uretprobe_regs_change(void)
}
}

#ifndef __NR_uretprobe
#define __NR_uretprobe 463
#endif

__naked unsigned long uretprobe_syscall_call_1(void)
{
/*
* Pretend we are uretprobe trampoline to trigger the return
* probe invocation in order to verify we get SIGILL.
*/
asm volatile (
"pushq %rax\n"
"pushq %rcx\n"
"pushq %r11\n"
"movq $" __stringify(__NR_uretprobe) ", %rax\n"
"syscall\n"
"popq %r11\n"
"popq %rcx\n"
"retq\n"
);
}

__naked unsigned long uretprobe_syscall_call(void)
{
asm volatile (
"call uretprobe_syscall_call_1\n"
"retq\n"
);
}

static void test_uretprobe_syscall_call(void)
{
LIBBPF_OPTS(bpf_uprobe_multi_opts, opts,
.retprobe = true,
);
struct uprobe_syscall_executed *skel;
int pid, status, err, go[2], c;

if (ASSERT_OK(pipe(go), "pipe"))
return;

skel = uprobe_syscall_executed__open_and_load();
if (!ASSERT_OK_PTR(skel, "uprobe_syscall_executed__open_and_load"))
goto cleanup;

pid = fork();
if (!ASSERT_GE(pid, 0, "fork"))
goto cleanup;

/* child */
if (pid == 0) {
close(go[1]);

/* wait for parent's kick */
err = read(go[0], &c, 1);
if (err != 1)
exit(-1);

uretprobe_syscall_call();
_exit(0);
}

skel->links.test = bpf_program__attach_uprobe_multi(skel->progs.test, pid,
"/proc/self/exe",
"uretprobe_syscall_call", &opts);
if (!ASSERT_OK_PTR(skel->links.test, "bpf_program__attach_uprobe_multi"))
goto cleanup;

/* kick the child */
write(go[1], &c, 1);
err = waitpid(pid, &status, 0);
ASSERT_EQ(err, pid, "waitpid");

/* verify the child got killed with SIGILL */
ASSERT_EQ(WIFSIGNALED(status), 1, "WIFSIGNALED");
ASSERT_EQ(WTERMSIG(status), SIGILL, "WTERMSIG");

/* verify the uretprobe program wasn't called */
ASSERT_EQ(skel->bss->executed, 0, "executed");

cleanup:
uprobe_syscall_executed__destroy(skel);
close(go[1]);
close(go[0]);
}
#else
static void test_uretprobe_regs_equal(void)
{
Expand All @@ -219,6 +307,11 @@ static void test_uretprobe_regs_change(void)
{
test__skip();
}

static void test_uretprobe_syscall_call(void)
{
test__skip();
}
#endif

void test_uprobe_syscall(void)
Expand All @@ -227,4 +320,6 @@ void test_uprobe_syscall(void)
test_uretprobe_regs_equal();
if (test__start_subtest("uretprobe_regs_change"))
test_uretprobe_regs_change();
if (test__start_subtest("uretprobe_syscall_call"))
test_uretprobe_syscall_call();
}
17 changes: 17 additions & 0 deletions tools/testing/selftests/bpf/progs/uprobe_syscall_executed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-2.0
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <string.h>

struct pt_regs regs;

char _license[] SEC("license") = "GPL";

int executed = 0;

SEC("uretprobe.multi")
int test(struct pt_regs *regs)
{
executed = 1;
return 0;
}

0 comments on commit 9e7f74e

Please sign in to comment.