1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2020 ARM Limited
#define _GNU_SOURCE
#include <sys/auxv.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <setjmp.h>
#include <sched.h>
#include "../../kselftest_harness.h"
#include "helper.h"
#define PAC_COLLISION_ATTEMPTS 1000
/*
* The kernel sets TBID by default. So bits 55 and above should remain
* untouched no matter what.
* The VA space size is 48 bits. Bigger is opt-in.
*/
#define PAC_MASK (~0xff80ffffffffffff)
#define ARBITRARY_VALUE (0x1234)
#define ASSERT_PAUTH_ENABLED() \
do { \
unsigned long hwcaps = getauxval(AT_HWCAP); \
/* data key instructions are not in NOP space. This prevents a SIGILL */ \
if (!(hwcaps & HWCAP_PACA)) \
SKIP(return, "PAUTH not enabled"); \
} while (0)
#define ASSERT_GENERIC_PAUTH_ENABLED() \
do { \
unsigned long hwcaps = getauxval(AT_HWCAP); \
/* generic key instructions are not in NOP space. This prevents a SIGILL */ \
if (!(hwcaps & HWCAP_PACG)) \
SKIP(return, "Generic PAUTH not enabled"); \
} while (0)
void sign_specific(struct signatures *sign, size_t val)
{
sign->keyia = keyia_sign(val);
sign->keyib = keyib_sign(val);
sign->keyda = keyda_sign(val);
sign->keydb = keydb_sign(val);
}
void sign_all(struct signatures *sign, size_t val)
{
sign->keyia = keyia_sign(val);
sign->keyib = keyib_sign(val);
sign->keyda = keyda_sign(val);
sign->keydb = keydb_sign(val);
sign->keyg = keyg_sign(val);
}
int n_same(struct signatures *old, struct signatures *new, int nkeys)
{
int res = 0;
res += old->keyia == new->keyia;
res += old->keyib == new->keyib;
res += old->keyda == new->keyda;
res += old->keydb == new->keydb;
if (nkeys == NKEYS)
res += old->keyg == new->keyg;
return res;
}
int n_same_single_set(struct signatures *sign, int nkeys)
{
size_t vals[nkeys];
int same = 0;
vals[0] = sign->keyia & PAC_MASK;
vals[1] = sign->keyib & PAC_MASK;
vals[2] = sign->keyda & PAC_MASK;
vals[3] = sign->keydb & PAC_MASK;
if (nkeys >= 4)
vals[4] = sign->keyg & PAC_MASK;
for (int i = 0; i < nkeys - 1; i++) {
for (int j = i + 1; j < nkeys; j++) {
if (vals[i] == vals[j])
same += 1;
}
}
return same;
}
int exec_sign_all(struct signatures *signed_vals, size_t val)
{
int new_stdin[2];
int new_stdout[2];
int status;
int i;
ssize_t ret;
pid_t pid;
cpu_set_t mask;
ret = pipe(new_stdin);
if (ret == -1) {
perror("pipe returned error");
return -1;
}
ret = pipe(new_stdout);
if (ret == -1) {
perror("pipe returned error");
return -1;
}
/*
* pin this process and all its children to a single CPU, so it can also
* guarantee a context switch with its child
*/
sched_getaffinity(0, sizeof(mask), &mask);
for (i = 0; i < sizeof(cpu_set_t); i++)
if (CPU_ISSET(i, &mask))
break;
CPU_ZERO(&mask);
CPU_SET(i, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
pid = fork();
// child
if (pid == 0) {
dup2(new_stdin[0], STDIN_FILENO);
if (ret == -1) {
perror("dup2 returned error");
exit(1);
}
dup2(new_stdout[1], STDOUT_FILENO);
if (ret == -1) {
perror("dup2 returned error");
exit(1);
}
close(new_stdin[0]);
close(new_stdin[1]);
close(new_stdout[0]);
close(new_stdout[1]);
ret = execl("exec_target", "exec_target", (char *)NULL);
if (ret == -1) {
perror("exec returned error");
exit(1);
}
}
close(new_stdin[0]);
close(new_stdout[1]);
ret = write(new_stdin[1], &val, sizeof(size_t));
if (ret == -1) {
perror("write returned error");
return -1;
}
/*
* wait for the worker to finish, so that read() reads all data
* will also context switch with worker so that this function can be used
* for context switch tests
*/
waitpid(pid, &status, 0);
if (WIFEXITED(status) == 0) {
fprintf(stderr, "worker exited unexpectedly\n");
return -1;
}
if (WEXITSTATUS(status) != 0) {
fprintf(stderr, "worker exited with error\n");
return -1;
}
ret = read(new_stdout[0], signed_vals, sizeof(struct signatures));
if (ret == -1) {
perror("read returned error");
return -1;
}
close(new_stdin[1]);
close(new_stdout[0]);
return 0;
}
sigjmp_buf jmpbuf;
void pac_signal_handler(int signum, siginfo_t *si, void *uc)
{
if (signum == SIGSEGV || signum == SIGILL)
siglongjmp(jmpbuf, 1);
}
/* check that a corrupted PAC results in SIGSEGV or SIGILL */
TEST(corrupt_pac)
{
struct sigaction sa;
ASSERT_PAUTH_ENABLED();
if (sigsetjmp(jmpbuf, 1) == 0) {
sa.sa_sigaction = pac_signal_handler;
sa.sa_flags = SA_SIGINFO | SA_RESETHAND;
sigemptyset(&sa.sa_mask);
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
pac_corruptor();
ASSERT_TRUE(0) TH_LOG("SIGSEGV/SIGILL signal did not occur");
}
}
/*
* There are no separate pac* and aut* controls so checking only the pac*
* instructions is sufficient
*/
TEST(pac_instructions_not_nop)
{
size_t keyia = 0;
size_t keyib = 0;
size_t keyda = 0;
size_t keydb = 0;
ASSERT_PAUTH_ENABLED();
for (int i = 0; i < PAC_COLLISION_ATTEMPTS; i++) {
keyia |= keyia_sign(i) & PAC_MASK;
keyib |= keyib_sign(i) & PAC_MASK;
keyda |= keyda_sign(i) & PAC_MASK;
keydb |= keydb_sign(i) & PAC_MASK;
}
ASSERT_NE(0, keyia) TH_LOG("keyia instructions did nothing");
ASSERT_NE(0, keyib) TH_LOG("keyib instructions did nothing");
ASSERT_NE(0, keyda) TH_LOG("keyda instructions did nothing");
ASSERT_NE(0, keydb) TH_LOG("keydb instructions did nothing");
}
TEST(pac_instructions_not_nop_generic)
{
size_t keyg = 0;
ASSERT_GENERIC_PAUTH_ENABLED();
for (int i = 0; i < PAC_COLLISION_ATTEMPTS; i++)
keyg |= keyg_sign(i) & PAC_MASK;
ASSERT_NE(0, keyg) TH_LOG("keyg instructions did nothing");
}
TEST(single_thread_different_keys)
{
int same = 10;
int nkeys = NKEYS;
int tmp;
struct signatures signed_vals;
unsigned long hwcaps = getauxval(AT_HWCAP);
/* generic and data key instructions are not in NOP space. This prevents a SIGILL */
ASSERT_PAUTH_ENABLED();
if (!(hwcaps & HWCAP_PACG)) {
TH_LOG("WARNING: Generic PAUTH not enabled. Skipping generic key checks");
nkeys = NKEYS - 1;
}
/*
* In Linux the PAC field can be up to 7 bits wide. Even if keys are
* different, there is about 5% chance for PACs to collide with
* different addresses. This chance rapidly increases with fewer bits
* allocated for the PAC (e.g. wider address). A comparison of the keys
* directly will be more reliable.
* All signed values need to be different at least once out of n
* attempts to be certain that the keys are different
*/
for (int i = 0; i < PAC_COLLISION_ATTEMPTS; i++) {
if (nkeys == NKEYS)
sign_all(&signed_vals, i);
else
sign_specific(&signed_vals, i);
tmp = n_same_single_set(&signed_vals, nkeys);
if (tmp < same)
same = tmp;
}
ASSERT_EQ(0, same) TH_LOG("%d keys clashed every time", same);
}
/*
* fork() does not change keys. Only exec() does so call a worker program.
* Its only job is to sign a value and report back the resutls
*/
TEST(exec_changed_keys)
{
struct signatures new_keys;
struct signatures old_keys;
int ret;
int same = 10;
int nkeys = NKEYS;
unsigned long hwcaps = getauxval(AT_HWCAP);
/* generic and data key instructions are not in NOP space. This prevents a SIGILL */
ASSERT_PAUTH_ENABLED();
if (!(hwcaps & HWCAP_PACG)) {
TH_LOG("WARNING: Generic PAUTH not enabled. Skipping generic key checks");
nkeys = NKEYS - 1;
}
for (int i = 0; i < PAC_COLLISION_ATTEMPTS; i++) {
ret = exec_sign_all(&new_keys, i);
ASSERT_EQ(0, ret) TH_LOG("failed to run worker");
if (nkeys == NKEYS)
sign_all(&old_keys, i);
else
sign_specific(&old_keys, i);
ret = n_same(&old_keys, &new_keys, nkeys);
if (ret < same)
same = ret;
}
ASSERT_EQ(0, same) TH_LOG("exec() did not change %d keys", same);
}
TEST(context_switch_keep_keys)
{
int ret;
struct signatures trash;
struct signatures before;
struct signatures after;
ASSERT_PAUTH_ENABLED();
sign_specific(&before, ARBITRARY_VALUE);
/* will context switch with a process with different keys at least once */
ret = exec_sign_all(&trash, ARBITRARY_VALUE);
ASSERT_EQ(0, ret) TH_LOG("failed to run worker");
sign_specific(&after, ARBITRARY_VALUE);
ASSERT_EQ(before.keyia, after.keyia) TH_LOG("keyia changed after context switching");
ASSERT_EQ(before.keyib, after.keyib) TH_LOG("keyib changed after context switching");
ASSERT_EQ(before.keyda, after.keyda) TH_LOG("keyda changed after context switching");
ASSERT_EQ(before.keydb, after.keydb) TH_LOG("keydb changed after context switching");
}
TEST(context_switch_keep_keys_generic)
{
int ret;
struct signatures trash;
size_t before;
size_t after;
ASSERT_GENERIC_PAUTH_ENABLED();
before = keyg_sign(ARBITRARY_VALUE);
/* will context switch with a process with different keys at least once */
ret = exec_sign_all(&trash, ARBITRARY_VALUE);
ASSERT_EQ(0, ret) TH_LOG("failed to run worker");
after = keyg_sign(ARBITRARY_VALUE);
ASSERT_EQ(before, after) TH_LOG("keyg changed after context switching");
}
TEST_HARNESS_MAIN
|