| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "tst-gcs-helper.h" |
| 21 |
|
| 22 |
#include <support/xunistd.h> |
| 23 |
#include <support/xsignal.h> |
| 24 |
#include <sys/ptrace.h> |
| 25 |
#include <sys/prctl.h> |
| 26 |
#include <sys/wait.h> |
| 27 |
#include <sys/uio.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <string.h> |
| 30 |
#include <errno.h> |
| 31 |
|
| 32 |
|
| 33 |
struct user_gcs |
| 34 |
{ |
| 35 |
uint64_t enabled; |
| 36 |
uint64_t locked; |
| 37 |
uint64_t gcspr_el0; |
| 38 |
}; |
| 39 |
|
| 40 |
static int |
| 41 |
target (void) |
| 42 |
{ |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
xraise (SIGUSR1); |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
fork_target (char *args[], uint64_t aarch64_gcs) |
| 52 |
{ |
| 53 |
|
| 54 |
|
| 55 |
bool lock_gcs = aarch64_gcs != 0 && aarch64_gcs != 2; |
| 56 |
uint64_t expected_locked = lock_gcs ? 0xfffffffful : 0ul; |
| 57 |
pid_t pid = xfork (); |
| 58 |
if (pid == 0) |
| 59 |
{ |
| 60 |
char tunables[90]; |
| 61 |
snprintf (tunables, sizeof (tunables), "GLIBC_TUNABLES=" |
| 62 |
"glibc.cpu.aarch64_gcs=0x%016lx", aarch64_gcs); |
| 63 |
char *envp[] = { tunables, NULL }; |
| 64 |
|
| 65 |
|
| 66 |
int res = ptrace (PTRACE_TRACEME, 0, NULL, NULL); |
| 67 |
if (res != 0) |
| 68 |
FAIL_EXIT1 ("ptrace: %m"); |
| 69 |
execve (args[0], args, envp); |
| 70 |
FAIL_EXIT1 ("execve: %m"); |
| 71 |
} |
| 72 |
bool checked = false; |
| 73 |
while (true) |
| 74 |
{ |
| 75 |
int status; |
| 76 |
xwaitpid (pid, &status, 0); |
| 77 |
if (WIFSTOPPED (status)) |
| 78 |
{ |
| 79 |
|
| 80 |
int sig = WSTOPSIG (status); |
| 81 |
if (sig == SIGUSR1) |
| 82 |
{ |
| 83 |
struct user_gcs ugcs = {}; |
| 84 |
struct iovec io; |
| 85 |
io.iov_base = &ugcs; |
| 86 |
io.iov_len = sizeof (struct user_gcs); |
| 87 |
if (ptrace (PTRACE_GETREGSET, pid, NT_ARM_GCS, &io)) |
| 88 |
FAIL_EXIT1 ("ptrace (PTRACE_GETREGSET): %m"); |
| 89 |
printf ("expected vs locked: %016lx %016lx\n", |
| 90 |
expected_locked, ugcs.locked); |
| 91 |
if (lock_gcs) |
| 92 |
TEST_VERIFY_EXIT (ugcs.enabled); |
| 93 |
TEST_VERIFY_EXIT (ugcs.locked == expected_locked); |
| 94 |
if (aarch64_gcs != 0) |
| 95 |
TEST_VERIFY_EXIT ((void *) ugcs.gcspr_el0 != NULL); |
| 96 |
checked = true; |
| 97 |
} |
| 98 |
} |
| 99 |
else if (WIFSIGNALED (status)) |
| 100 |
{ |
| 101 |
|
| 102 |
break; |
| 103 |
} |
| 104 |
else if (WIFEXITED (status)) |
| 105 |
{ |
| 106 |
|
| 107 |
break; |
| 108 |
} |
| 109 |
ptrace (PTRACE_CONT, pid, 0, 0); |
| 110 |
} |
| 111 |
|
| 112 |
TEST_VERIFY_EXIT (checked); |
| 113 |
} |
| 114 |
|
| 115 |
int main(int argc, char *argv[]) |
| 116 |
{ |
| 117 |
|
| 118 |
if (!(getauxval (AT_HWCAP) & HWCAP_GCS)) |
| 119 |
FAIL_UNSUPPORTED ("kernel or CPU does not support GCS"); |
| 120 |
|
| 121 |
|
| 122 |
TEST_VERIFY (__check_gcs_status ()); |
| 123 |
|
| 124 |
|
| 125 |
if (strcmp (argv[argc - 1], "target") == 0) |
| 126 |
return target (); |
| 127 |
|
| 128 |
|
| 129 |
if (argc < 3) |
| 130 |
FAIL_EXIT1 ("wrong number of arguments: %d", argc); |
| 131 |
|
| 132 |
char *child_args[] = { NULL, NULL, NULL, NULL, NULL , NULL }; |
| 133 |
|
| 134 |
|
| 135 |
if (strcmp (argv[0], argv[2]) == 0) |
| 136 |
{ |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
child_args[0] = argv[0]; |
| 141 |
|
| 142 |
child_args[1] = (char *)"target"; |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
|
| 147 |
|
| 148 |
TEST_VERIFY_EXIT (argc > 5); |
| 149 |
TEST_COMPARE_STRING (argv[3], "--library-path"); |
| 150 |
|
| 151 |
child_args[0] = argv[2]; |
| 152 |
|
| 153 |
child_args[1] = argv[3]; |
| 154 |
|
| 155 |
child_args[2] = argv[4]; |
| 156 |
|
| 157 |
child_args[3] = argv[5]; |
| 158 |
|
| 159 |
child_args[4] = (char *)"target"; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
for (uint64_t aarch64_gcs = 0; aarch64_gcs < 4; aarch64_gcs++) |
| 164 |
fork_target (child_args, aarch64_gcs); |
| 165 |
return 0; |
| 166 |
} |