aboutsummaryrefslogtreecommitdiff
path: root/tools/test/stress2/misc/pkru.sh
blob: 047b7f8f8745f1af7a0b6fa71649fed62ba30047 (plain) (blame)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#/bin/sh
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2019 Konstantin Belousov <kib@FreeBSD.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# Test scenario for Intel userspace protection keys feature on Skylake Xeons

grep -qw PKU /var/run/dmesg.boot || exit 0
cd /tmp
cat > /tmp/pkru_exec.c <<EOF
/* $Id: pkru_exec.c,v 1.4 2019/01/12 04:55:57 kostik Exp kostik $ */
/*
 * cc -Wall -Wextra -g -O -o pkru_fork64 pkru_fork.c
 * Run it with LD_BIND_NOW=1.
 */

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <machine/sysarch.h>
#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef TEST_COMPILE
int x86_pkru_get_perm(unsigned int keyidx, int *access, int *modify);
int x86_pkru_set_perm(unsigned int keyidx, int access, int modify);
int x86_pkru_protect_range(void *addr, unsigned long len, unsigned int keyidx,
    int flag);
int x86_pkru_unprotect_range(void *addr, unsigned long len);
uint32_t rdpkru(void);
void wrpkru(uint32_t);
#define	AMD64_PKRU_PERSIST	0x0001
#endif

extern char **environ;

#define	OPKEY	1

int
main(void)
{
	char *args[3] = {
	    "/bin/date",
	    NULL
	};
	struct rlimit rl;

	if (getrlimit(RLIMIT_STACK, &rl) != 0)
		err(1, "getrlimit RLIMIT_STACK");
	if (x86_pkru_protect_range(0, 0x800000000000 - rl.rlim_max, OPKEY,
	    AMD64_PKRU_PERSIST) != 0)
		err(1, "x86_pkru_protect_range");
	if (x86_pkru_set_perm(1, 1, 0) != 0)
		err(1, "x86_pkru_set_perm");
	execve("/bin/date", args, environ);
}
EOF
cc -Wall -Wextra -g -O -o pkru_exec64 pkru_exec.c || exit 1
cc -Wall -Wextra -g -O -o pkru_exec32 pkru_exec.c -m32 || exit 1
rm pkru_exec.c
echo "Expect: Segmentation fault (core dumped)"
LD_BIND_NOW=1 ./pkru_exec64
LD_BIND_NOW=1 ./pkru_exec32
rm -f pkru_exec64 pkru_exec32 pkru_exec64.core pkru_exec32.core

cat > /tmp/pkru_fork.c <<EOF
/* $Id: pkru_fork.c,v 1.2 2019/01/12 03:39:42 kostik Exp kostik $ */
/* cc -Wall -Wextra -g -O -o pkru_fork64 pkru_fork.c */

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <machine/cpufunc.h>
#include <machine/sysarch.h>
#include <x86/fpu.h>
#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef TEST_COMPILE
int x86_pkru_get_perm(unsigned int keyidx, int *access, int *modify);
int x86_pkru_set_perm(unsigned int keyidx, int access, int modify);
int x86_pkru_protect_range(void *addr, unsigned long len, unsigned int keyidx,
    int flag);
int x86_pkru_unprotect_range(void *addr, unsigned long len);
uint32_t rdpkru(void);
void wrpkru(uint32_t);
#endif

static volatile char *mapping;

#define	OPKEY	1

int
main(void)
{
	int error;
	pid_t child;

	mapping = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
	    MAP_PRIVATE | MAP_ANON, -1, 0);
	if (mapping == MAP_FAILED)
		err(1, "mmap");
	error = x86_pkru_protect_range((void *)mapping, getpagesize(),
	    OPKEY, 0);
	if (error != 0)
		err(1, "x86_pkru_protect_range");
	error = x86_pkru_set_perm(OPKEY, 0, 0);
	if (error != 0)
		err(1, "x86_pkru_set_perm");
	child = fork();
	if (child == -1)
		err(1, "fork");
	if (child == 0) {
		*mapping = 0;
		printf("Still alive, pkru did not worked after fork");
	}
	waitpid(child, NULL, 0);
}
EOF
cc -Wall -Wextra -g -O -o pkru_fork64 pkru_fork.c || exit 1
cc -Wall -Wextra -g -O -o pkru_fork32 -m32 pkru_fork.c || exit 1
rm pkru_fork.c
./pkru_fork64
./pkru_fork32
rm -f pkru_fork64 pkru_fork64.core pkru_fork32 pkru_fork32.core

cat > /tmp/pkru_perm.c <<EOF
/* $Id: pkru_perm.c,v 1.6 2019/01/12 04:43:20 kostik Exp kostik $ */
/* cc -Wall -Wextra -g -O -o pkru_fork64 pkru_fork.c */

#include <sys/types.h>
#include <sys/mman.h>
#include <machine/cpufunc.h>
#include <machine/sysarch.h>
#include <x86/fpu.h>
#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef TEST_COMPILE
int x86_pkru_get_perm(unsigned int keyidx, int *access, int *modify);
int x86_pkru_set_perm(unsigned int keyidx, int access, int modify);
int x86_pkru_protect_range(void *addr, unsigned long len, unsigned int keyidx,
    int flag);
int x86_pkru_unprotect_range(void *addr, unsigned long len);
uint32_t rdpkru(void);
void wrpkru(uint32_t);
#define	AMD64_PKRU_PERSIST	0x0001
#endif

static void
sighandler(int signo __unused, siginfo_t *si __unused, void *uc1 __unused)
{

	exit(0);
}

static volatile char *mapping;

#define	OPKEY	1

int
main(void)
{
	struct sigaction sa;
	char *mapping1;
	int error;

	error = x86_pkru_set_perm(OPKEY, 0, 0);
	if (error != 0)
		err(1, "x86_pkru_set_perm");
	mapping = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE,
	    MAP_PRIVATE | MAP_ANON, -1, 0);
	if (mapping == MAP_FAILED)
		err(1, "mmap");
	error = x86_pkru_protect_range((void *)mapping, getpagesize(),
	    OPKEY, 0);
	if (error != 0)
		err(1, "x86_pkru_protect_range");
	error = munmap((void *)mapping, getpagesize());
	if (error != 0)
		err(1, "munmap");
	mapping1 = mmap((void *)mapping, getpagesize(), PROT_READ | PROT_WRITE,
	    MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_EXCL, -1, 0);
	if (mapping1 == MAP_FAILED)
		err(1, "mmap 2");
	*mapping = 0;
	error = x86_pkru_protect_range((void *)mapping, getpagesize(),
	    OPKEY, AMD64_PKRU_PERSIST);
	mapping1 = mmap((void *)mapping, getpagesize(), PROT_READ | PROT_WRITE,
	    MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);
	if (mapping1 == MAP_FAILED)
		err(1, "mmap 3");
	memset(&sa, 0, sizeof(sa));
	sa.sa_sigaction = sighandler;
	sa.sa_flags = SA_SIGINFO;
	if (sigaction(SIGSEGV, &sa, NULL) == -1)
		err(1, "sigaction");
	*mapping = 0;
	printf("Still alive, pkru persist did not worked");
	exit(1);
}
EOF
cc -Wall -Wextra -g -O -o pkru_perm64 pkru_perm.c || exit 1
cc -Wall -Wextra -g -O -o pkru_perm32 -m32 pkru_perm.c || exit 1
rm pkru_perm.c
./pkru_perm64
./pkru_perm32
rm -f pkru_perm64 pkru_perm32

cat > /tmp/pkru.c <<EOF
/* $Id: pkru.c,v 1.27 2019/01/10 12:06:31 kostik Exp $ */
/* cc -Wall -Wextra -g -O -o pkru64 pkru.c -lpthread */

#include <sys/types.h>
#include <sys/mman.h>
#include <machine/cpufunc.h>
#include <machine/sysarch.h>
#include <x86/fpu.h>
#include <err.h>
#include <pthread.h>
#include <pthread_np.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#ifdef TEST_COMPILE
int x86_pkru_get_perm(unsigned int keyidx, int *access, int *modify);
int x86_pkru_set_perm(unsigned int keyidx, int access, int modify);
int x86_pkru_protect_range(void *addr, unsigned long len, unsigned int keyidx,
    int flag);
int x86_pkru_unprotect_range(void *addr, unsigned long len);
uint32_t rdpkru(void);
void wrpkru(uint32_t);
#endif

static char *mut_region;
static size_t mut_region_len;
static unsigned *mut_region_keys;
static pthread_t bga_thr;
static int signal_seen;
static siginfo_t si_seen;
static ucontext_t *uc_seen;
static u_int rpku_offset;

static void
handler(int i __unused) {
	_exit(0);
}

static void
report_sig(int signo, siginfo_t *si, ucontext_t *uc)
{

	printf("signal %d %s", signo, strsignal(signo));
	printf(" si_code %d si_status %d si_addr %p", si->si_code,
	    si->si_status, si->si_addr);
	printf(" mc_err %#jx", (uintmax_t)uc->uc_mcontext.mc_err);
	if (uc->uc_mcontext.mc_xfpustate != 0 &&
	    (unsigned long)uc->uc_mcontext.mc_xfpustate_len >=
	    rpku_offset) {
		printf(" pkru 0x%08x", *(uint32_t *)(
		    uc->uc_mcontext.mc_xfpustate + rpku_offset));
	}
	printf("\n");
}

static void
sighandler(int signo, siginfo_t *si, void *u)
{
	ucontext_t *uc;
	pthread_t thr;
	size_t len;
	uint32_t *pkrup;

	uc = u;
	thr = pthread_self();
	if (thr == bga_thr) {
		printf("Fault from background access thread\n");
		report_sig(signo, si, uc);
		exit(1);
	}
	signal_seen = signo;
	si_seen = *si;

	len = sizeof(ucontext_t);
	if (uc->uc_mcontext.mc_xfpustate != 0)
		len += uc->uc_mcontext.mc_xfpustate_len;
	uc_seen = malloc(len);
	if (uc_seen == NULL)
		err(1, "malloc(%d)", (int)len);
	memcpy(uc_seen, uc, sizeof(*uc));
#if 0
printf("signal %d xpfustate %p len %ld rpkuo %u\n", signo, (void *)uc->uc_mcontext.mc_xfpustate, uc->uc_mcontext.mc_xfpustate_len, rpku_offset);
#endif
	if (uc->uc_mcontext.mc_xfpustate != 0) {
		uc_seen->uc_mcontext.mc_xfpustate = (uintptr_t)uc_seen +
		    sizeof(*uc);
		memcpy((void *)uc_seen->uc_mcontext.mc_xfpustate,
		    (void *)uc->uc_mcontext.mc_xfpustate,
		    uc->uc_mcontext.mc_xfpustate_len);

		if ((unsigned long)uc->uc_mcontext.mc_xfpustate_len >=
		    rpku_offset + sizeof(uint32_t)) {
			pkrup = (uint32_t *)(rpku_offset +
			    (char *)uc->uc_mcontext.mc_xfpustate);
#if 0
printf("signal %d *pkru %08x\n", signo, *pkrup);
#endif
			*pkrup = 0;
		}
	}
}

static void *
bg_access_thread_fn(void *arg __unused)
{
	char *c, x;

	pthread_set_name_np(pthread_self(), "bgaccess");
	for (x = 0, c = mut_region;;) {
		*c = x;
		if (++c >= mut_region + mut_region_len) {
			c = mut_region;
			x++;
		}
	}
	return (NULL);
}

static void
clear_signal_report(void)
{

	signal_seen = 0;
	free(uc_seen);
	uc_seen = NULL;
}

static void
check_signal(unsigned key, int check_access, int check_modify)
{

	if (signal_seen == 0) {
		printf("Did not get signal, key %d check_access %d "
		    "check_modify %d\n", key, check_access, check_modify);
		printf("pkru 0x%08x\n", rdpkru());
		exit(1);
	}
}

static void
check_no_signal(void)
{

	if (signal_seen != 0) {
		printf("pkru 0x%08x\n", rdpkru());
		printf("Got signal\n");
		report_sig(signal_seen, &si_seen, uc_seen);
		exit(1);
	}
}

static void
check(char *p, unsigned key, int check_access, int check_modify)
{
	int access, error, modify, orig_access, orig_modify;

	error = x86_pkru_get_perm(key, &orig_access, &orig_modify);
	if (error != 0)
		err(1, "x86_pkru_get_perm");
	access = check_access ? 0 : 1;
	modify = check_modify ? 0 : 1;
	error = x86_pkru_set_perm(key, access, modify);
	if (error != 0)
		err(1, "x86_pkru_set_perm access");
	clear_signal_report();
	if (check_modify)
		*(volatile char *)p = 1;
	else if (check_access)
		*(volatile char *)p;
	if (key == mut_region_keys[(p - mut_region) / getpagesize()])
		check_signal(key, check_access, check_modify);
	else
		check_no_signal();
	error = x86_pkru_set_perm(key, orig_access, orig_modify);
	if (error != 0)
		err(1, "x86_pkru_set_perm access restore");
	clear_signal_report();
	if (check_modify)
		*(volatile char *)p = 1;
	else if (check_access)
		*(volatile char *)p;
	check_no_signal();
}

static void
mutate_perms(void)
{
	unsigned key;
	char *p;

	for (p = mut_region;;) {
		for (key = 1; key < 0x10; key++) {
			check(p, key, 1, 0);
			check(p, key, 0, 1);
			check(p, key, 1, 1);
		}
		p += getpagesize();
		if (p >= mut_region + mut_region_len)
			p = mut_region;
	}
}

int
main(void)
{
	struct sigaction sa;
	char *p;
	unsigned i;
	u_int regs[4];
	int error;

	cpuid_count(0xd, 0x9, regs);
	rpku_offset = regs[1];
	if (rpku_offset != 0)
#if defined(__i386__)
		rpku_offset -= sizeof(union savefpu);
#else
		rpku_offset -= sizeof(struct savefpu);
#endif

	memset(&sa, 0, sizeof(sa));
	sa.sa_sigaction = sighandler;
	sa.sa_flags = SA_SIGINFO;
	if (sigaction(SIGSEGV, &sa, NULL) == -1)
		err(1, "sigaction SIGSEGV");
	if (sigaction(SIGBUS, &sa, NULL) == -1)
		err(1, "sigaction SIGBUS");

	mut_region_len = getpagesize() * 100;
	mut_region_keys = calloc(mut_region_len, sizeof(unsigned));
	if (mut_region_keys == NULL)
		err(1, "calloc keys");
	mut_region = mmap(NULL, mut_region_len, PROT_READ | PROT_WRITE,
	    MAP_ANON | MAP_SHARED, -1, 0);
	if (mut_region == MAP_FAILED)
		err(1, "mmap");
	for (i = 1, p = mut_region; p < mut_region + mut_region_len;
	     p += getpagesize()) {
		error = x86_pkru_protect_range(p, getpagesize(), i, 0);
		if (error != 0)
			err(1, "x86_pkru_protect_range key %d", i);
		mut_region_keys[(p - mut_region) / getpagesize()] = i;
		if (++i > 0xf)
			i = 1;
	}

	signal(SIGALRM, handler);
	alarm(5);
	error = pthread_create(&bga_thr, NULL, bg_access_thread_fn, NULL);
	if (error != 0)
		errc(1, error, "pthread create background access thread");

	mutate_perms();
}
EOF
cc -Wall -Wextra -g -O -o pkru64 pkru.c -lpthread || exit 1
cc -Wall -Wextra -g -O -o pkru32 -m32 pkru.c -lpthread || exit 1
rm pkru.c
./pkru64
./pkru32
rm -f pkru64 pkru32

exit