master
c 48 lines 1.1 KB
Raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include "gcs.h"
4
5 /*
6 * A single garbage store to the gcs stack.
7 * The asm inside must be unique, so disallow inlining.
8 */
9 void __attribute__((noinline))
10 test_gcsstr(void)
11 {
12 register uint64_t *ptr __asm__("x0") = gcspr();
13 /* GCSSTR x1, x0 */
14 __asm__("inst_gcsstr: .inst 0xd91f1c01" : : "r"(--ptr));
15 }
16
17 static void test_sigsegv(int sig, siginfo_t *info, void *vuc)
18 {
19 ucontext_t *uc = vuc;
20 uint64_t inst_gcsstr;
21
22 __asm__("adr %0, inst_gcsstr" : "=r"(inst_gcsstr));
23 assert(uc->uc_mcontext.pc == inst_gcsstr);
24 assert(info->si_code == SEGV_CPERR);
25 /* TODO: Dig for ESR and verify syndrome. */
26 exit(0);
27 }
28
29 int main()
30 {
31 struct sigaction sa = {
32 .sa_sigaction = test_sigsegv,
33 .sa_flags = SA_SIGINFO,
34 };
35
36 /* Enable GCSSTR and test the store succeeds. */
37 enable_gcs(PR_SHADOW_STACK_WRITE);
38 test_gcsstr();
39
40 /* Disable GCSSTR and test the resulting sigsegv. */
41 enable_gcs(0);
42 if (sigaction(SIGSEGV, &sa, NULL) < 0) {
43 perror("sigaction");
44 exit(1);
45 }
46 test_gcsstr();
47 abort();
48 }