Debian Codegraph @

sysdeps/unix/sysv/linux/aarch64/tst-gcs-lock-ptrace.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /* AArch64 test for GCS for creating child process using fork
2 with ptrace to check locked GCS operations.
3 Copyright (C) 2026 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
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 /* Uapi struct for PTRACE_GETREGSET with NT_ARM_GCS. */
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 /* This signal is raised after the process has started
44 and has been initialised so we can ptrace it at this
45 point and obtain GCS locked features. */
46 xraise (SIGUSR1);
47 return 0;
48 }
49
50 static void
51 fork_target (char *args[], uint64_t aarch64_gcs)
52 {
53 /* Currently kernel returns only lower 32 bits of locked
54 features so we only compare them. */
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 /* We need to ptrace child process to use PTRACE_GETREGSET
65 with NT_ARM_GCS after it has started. */
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 /* Child stopped by signal. */
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 /* Child terminated by signal. */
102 break;
103 }
104 else if (WIFEXITED (status))
105 {
106 /* Child terminated by normally. */
107 break;
108 }
109 ptrace (PTRACE_CONT, pid, 0, 0);
110 }
111 /* If child process hasn't run correctly, this will remain false. */
112 TEST_VERIFY_EXIT (checked);
113 }
114
115 int main(int argc, char *argv[])
116 {
117 /* Check if GCS could possible by enabled. */
118 if (!(getauxval (AT_HWCAP) & HWCAP_GCS))
119 FAIL_UNSUPPORTED ("kernel or CPU does not support GCS");
120
121 /* GCS should be enabled for this test. */
122 TEST_VERIFY (__check_gcs_status ());
123
124 /* If last argument is 'target', we just run target code. */
125 if (strcmp (argv[argc - 1], "target") == 0)
126 return target ();
127
128 /* In parent, we should at least have 3 arguments. */
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 /* Check command line arguments to construct child command. */
135 if (strcmp (argv[0], argv[2]) == 0)
136 {
137 /* Command looks like
138 /path/to/test -- /path/to/test */
139 /* /path/to/test */
140 child_args[0] = argv[0];
141 /* Extra argument for the child process. */
142 child_args[1] = (char *)"target";
143 }
144 else
145 {
146 /* Command looks like
147 /path/to/test -- /path/to/ld.so ... */
148 TEST_VERIFY_EXIT (argc > 5);
149 TEST_COMPARE_STRING (argv[3], "--library-path");
150 /* /path/to/ld-linux-aarch64.so.1 */
151 child_args[0] = argv[2];
152 /* --library-path */
153 child_args[1] = argv[3];
154 /* Library path... */
155 child_args[2] = argv[4];
156 /* /path/to/test */
157 child_args[3] = argv[5];
158 /* Extra argument for the child process. */
159 child_args[4] = (char *)"target";
160 }
161
162 /* Check all 4 values for the aarch64_gcs tunable. */
163 for (uint64_t aarch64_gcs = 0; aarch64_gcs < 4; aarch64_gcs++)
164 fork_target (child_args, aarch64_gcs);
165 return 0;
166 }