Skip to content

Commit 42155a8

Browse files
brooniectmarinas
authored andcommitted
kselftest/arm64: Always run signals tests with GCS enabled
Since it is not possible to return from the function that enabled GCS without disabling GCS it is very inconvenient to use the signal handling tests to cover GCS when GCS is not enabled by the toolchain and runtime, something that no current distribution does. Since none of the testcases do anything with stacks that would cause problems with GCS we can sidestep this issue by unconditionally enabling GCS on startup and exiting with a call to exit() rather than a return from main(). Reviewed-by: Thiago Jung Bauermann <[email protected]> Signed-off-by: Mark Brown <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Catalin Marinas <[email protected]>
1 parent 956573a commit 42155a8

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

tools/testing/selftests/arm64/signal/test_signals.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
* Each test provides its own tde struct tdescr descriptor to link with
88
* this wrapper. Framework provides common helpers.
99
*/
10+
11+
#include <sys/auxv.h>
12+
#include <sys/prctl.h>
13+
1014
#include <kselftest.h>
1115

1216
#include "test_signals.h"
@@ -16,12 +20,23 @@ struct tdescr *current = &tde;
1620

1721
int main(int argc, char *argv[])
1822
{
23+
/*
24+
* Ensure GCS is at least enabled throughout the tests if
25+
* supported, otherwise the inability to return from the
26+
* function that enabled GCS makes it very inconvenient to set
27+
* up test cases. The prctl() may fail if GCS was locked by
28+
* libc setup code.
29+
*/
30+
if (getauxval(AT_HWCAP) & HWCAP_GCS)
31+
gcs_set_state(PR_SHADOW_STACK_ENABLE);
32+
1933
ksft_print_msg("%s :: %s\n", current->name, current->descr);
2034
if (test_setup(current) && test_init(current)) {
2135
test_run(current);
2236
test_cleanup(current);
2337
}
2438
test_result(current);
2539

26-
return current->result;
40+
/* Do not return in case GCS was enabled */
41+
exit(current->result);
2742
}

tools/testing/selftests/arm64/signal/test_signals_utils.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,35 @@ void test_cleanup(struct tdescr *td);
1818
int test_run(struct tdescr *td);
1919
void test_result(struct tdescr *td);
2020

21+
#ifndef __NR_prctl
22+
#define __NR_prctl 167
23+
#endif
24+
25+
/*
26+
* The prctl takes 1 argument but we need to ensure that the other
27+
* values passed in registers to the syscall are zero since the kernel
28+
* validates them.
29+
*/
30+
#define gcs_set_state(state) \
31+
({ \
32+
register long _num __asm__ ("x8") = __NR_prctl; \
33+
register long _arg1 __asm__ ("x0") = PR_SET_SHADOW_STACK_STATUS; \
34+
register long _arg2 __asm__ ("x1") = (long)(state); \
35+
register long _arg3 __asm__ ("x2") = 0; \
36+
register long _arg4 __asm__ ("x3") = 0; \
37+
register long _arg5 __asm__ ("x4") = 0; \
38+
\
39+
__asm__ volatile ( \
40+
"svc #0\n" \
41+
: "=r"(_arg1) \
42+
: "r"(_arg1), "r"(_arg2), \
43+
"r"(_arg3), "r"(_arg4), \
44+
"r"(_arg5), "r"(_num) \
45+
: "memory", "cc" \
46+
); \
47+
_arg1; \
48+
})
49+
2150
static inline bool feats_ok(struct tdescr *td)
2251
{
2352
if (td->feats_incompatible & td->feats_supported)

0 commit comments

Comments
 (0)