Skip to content

Commit 30addd1

Browse files
olsajirimhiramat
authored andcommitted
selftests/bpf: Add uretprobe shadow stack test
Adding uretprobe shadow stack test that runs all existing uretprobe tests with shadow stack enabled if it's available. Link: https://lore.kernel.org/all/[email protected]/ Acked-by: Andrii Nakryiko <[email protected]> Reviewed-by: Masami Hiramatsu (Google) <[email protected]> Signed-off-by: Jiri Olsa <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
1 parent 9e7f74e commit 30addd1

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

tools/testing/selftests/bpf/prog_tests/uprobe_syscall.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <linux/compiler.h>
1010
#include <linux/stringify.h>
1111
#include <sys/wait.h>
12+
#include <sys/syscall.h>
13+
#include <sys/prctl.h>
14+
#include <asm/prctl.h>
1215
#include "uprobe_syscall.skel.h"
1316
#include "uprobe_syscall_executed.skel.h"
1417

@@ -297,6 +300,56 @@ static void test_uretprobe_syscall_call(void)
297300
close(go[1]);
298301
close(go[0]);
299302
}
303+
304+
/*
305+
* Borrowed from tools/testing/selftests/x86/test_shadow_stack.c.
306+
*
307+
* For use in inline enablement of shadow stack.
308+
*
309+
* The program can't return from the point where shadow stack gets enabled
310+
* because there will be no address on the shadow stack. So it can't use
311+
* syscall() for enablement, since it is a function.
312+
*
313+
* Based on code from nolibc.h. Keep a copy here because this can't pull
314+
* in all of nolibc.h.
315+
*/
316+
#define ARCH_PRCTL(arg1, arg2) \
317+
({ \
318+
long _ret; \
319+
register long _num asm("eax") = __NR_arch_prctl; \
320+
register long _arg1 asm("rdi") = (long)(arg1); \
321+
register long _arg2 asm("rsi") = (long)(arg2); \
322+
\
323+
asm volatile ( \
324+
"syscall\n" \
325+
: "=a"(_ret) \
326+
: "r"(_arg1), "r"(_arg2), \
327+
"0"(_num) \
328+
: "rcx", "r11", "memory", "cc" \
329+
); \
330+
_ret; \
331+
})
332+
333+
#ifndef ARCH_SHSTK_ENABLE
334+
#define ARCH_SHSTK_ENABLE 0x5001
335+
#define ARCH_SHSTK_DISABLE 0x5002
336+
#define ARCH_SHSTK_SHSTK (1ULL << 0)
337+
#endif
338+
339+
static void test_uretprobe_shadow_stack(void)
340+
{
341+
if (ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK)) {
342+
test__skip();
343+
return;
344+
}
345+
346+
/* Run all of the uretprobe tests. */
347+
test_uretprobe_regs_equal();
348+
test_uretprobe_regs_change();
349+
test_uretprobe_syscall_call();
350+
351+
ARCH_PRCTL(ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK);
352+
}
300353
#else
301354
static void test_uretprobe_regs_equal(void)
302355
{
@@ -312,6 +365,11 @@ static void test_uretprobe_syscall_call(void)
312365
{
313366
test__skip();
314367
}
368+
369+
static void test_uretprobe_shadow_stack(void)
370+
{
371+
test__skip();
372+
}
315373
#endif
316374

317375
void test_uprobe_syscall(void)
@@ -322,4 +380,6 @@ void test_uprobe_syscall(void)
322380
test_uretprobe_regs_change();
323381
if (test__start_subtest("uretprobe_syscall_call"))
324382
test_uretprobe_syscall_call();
383+
if (test__start_subtest("uretprobe_shadow_stack"))
384+
test_uretprobe_shadow_stack();
325385
}

0 commit comments

Comments
 (0)