hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/tools/testing/selftests/x86/test_vsyscall.c
....@@ -18,6 +18,9 @@
1818 #include <sched.h>
1919 #include <stdbool.h>
2020 #include <setjmp.h>
21
+#include <sys/uio.h>
22
+
23
+#include "helpers.h"
2124
2225 #ifdef __x86_64__
2326 # define VSYS(x) (x)
....@@ -49,21 +52,21 @@
4952 }
5053
5154 /* vsyscalls and vDSO */
52
-bool should_read_vsyscall = false;
55
+bool vsyscall_map_r = false, vsyscall_map_x = false;
5356
5457 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
55
-gtod_t vgtod = (gtod_t)VSYS(0xffffffffff600000);
58
+const gtod_t vgtod = (gtod_t)VSYS(0xffffffffff600000);
5659 gtod_t vdso_gtod;
5760
5861 typedef int (*vgettime_t)(clockid_t, struct timespec *);
5962 vgettime_t vdso_gettime;
6063
6164 typedef long (*time_func_t)(time_t *t);
62
-time_func_t vtime = (time_func_t)VSYS(0xffffffffff600400);
65
+const time_func_t vtime = (time_func_t)VSYS(0xffffffffff600400);
6366 time_func_t vdso_time;
6467
6568 typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
66
-getcpu_t vgetcpu = (getcpu_t)VSYS(0xffffffffff600800);
69
+const getcpu_t vgetcpu = (getcpu_t)VSYS(0xffffffffff600800);
6770 getcpu_t vdso_getcpu;
6871
6972 static void init_vdso(void)
....@@ -107,7 +110,7 @@
107110 maps = fopen("/proc/self/maps", "r");
108111 if (!maps) {
109112 printf("[WARN]\tCould not open /proc/self/maps -- assuming vsyscall is r-x\n");
110
- should_read_vsyscall = true;
113
+ vsyscall_map_r = true;
111114 return 0;
112115 }
113116
....@@ -133,12 +136,8 @@
133136 }
134137
135138 printf("\tvsyscall permissions are %c-%c\n", r, x);
136
- should_read_vsyscall = (r == 'r');
137
- if (x != 'x') {
138
- vgtod = NULL;
139
- vtime = NULL;
140
- vgetcpu = NULL;
141
- }
139
+ vsyscall_map_r = (r == 'r');
140
+ vsyscall_map_x = (x == 'x');
142141
143142 found = true;
144143 break;
....@@ -148,10 +147,8 @@
148147
149148 if (!found) {
150149 printf("\tno vsyscall map in /proc/self/maps\n");
151
- should_read_vsyscall = false;
152
- vgtod = NULL;
153
- vtime = NULL;
154
- vgetcpu = NULL;
150
+ vsyscall_map_r = false;
151
+ vsyscall_map_x = false;
155152 }
156153
157154 return nerrs;
....@@ -183,9 +180,13 @@
183180 }
184181
185182 static jmp_buf jmpbuf;
183
+static volatile unsigned long segv_err;
186184
187185 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
188186 {
187
+ ucontext_t *ctx = (ucontext_t *)ctx_void;
188
+
189
+ segv_err = ctx->uc_mcontext.gregs[REG_ERR];
189190 siglongjmp(jmpbuf, 1);
190191 }
191192
....@@ -238,7 +239,7 @@
238239 err(1, "syscall gettimeofday");
239240 if (vdso_gtod)
240241 ret_vdso = vdso_gtod(&tv_vdso, &tz_vdso);
241
- if (vgtod)
242
+ if (vsyscall_map_x)
242243 ret_vsys = vgtod(&tv_vsys, &tz_vsys);
243244 if (sys_gtod(&tv_sys2, &tz_sys) != 0)
244245 err(1, "syscall gettimeofday");
....@@ -252,7 +253,7 @@
252253 }
253254 }
254255
255
- if (vgtod) {
256
+ if (vsyscall_map_x) {
256257 if (ret_vsys == 0) {
257258 nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vsyscall", &tv_vsys, &tz_vsys);
258259 } else {
....@@ -273,7 +274,7 @@
273274 t_sys1 = sys_time(&t2_sys1);
274275 if (vdso_time)
275276 t_vdso = vdso_time(&t2_vdso);
276
- if (vtime)
277
+ if (vsyscall_map_x)
277278 t_vsys = vtime(&t2_vsys);
278279 t_sys2 = sys_time(&t2_sys2);
279280 if (t_sys1 < 0 || t_sys1 != t2_sys1 || t_sys2 < 0 || t_sys2 != t2_sys2) {
....@@ -294,7 +295,7 @@
294295 }
295296 }
296297
297
- if (vtime) {
298
+ if (vsyscall_map_x) {
298299 if (t_vsys < 0 || t_vsys != t2_vsys) {
299300 printf("[FAIL]\tvsyscall failed (ret:%ld output:%ld)\n", t_vsys, t2_vsys);
300301 nerrs++;
....@@ -330,7 +331,7 @@
330331 ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
331332 if (vdso_getcpu)
332333 ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
333
- if (vgetcpu)
334
+ if (vsyscall_map_x)
334335 ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
335336
336337 if (ret_sys == 0) {
....@@ -369,7 +370,7 @@
369370 }
370371 }
371372
372
- if (vgetcpu) {
373
+ if (vsyscall_map_x) {
373374 if (ret_vsys) {
374375 printf("[FAIL]\tvsyscall getcpu() failed\n");
375376 nerrs++;
....@@ -410,36 +411,109 @@
410411 can_read = false;
411412 }
412413
413
- if (can_read && !should_read_vsyscall) {
414
+ if (can_read && !vsyscall_map_r) {
414415 printf("[FAIL]\tWe have read access, but we shouldn't\n");
415416 return 1;
416
- } else if (!can_read && should_read_vsyscall) {
417
+ } else if (!can_read && vsyscall_map_r) {
417418 printf("[FAIL]\tWe don't have read access, but we should\n");
418419 return 1;
420
+ } else if (can_read) {
421
+ printf("[OK]\tWe have read access\n");
419422 } else {
420
- printf("[OK]\tgot expected result\n");
423
+ printf("[OK]\tWe do not have read access: #PF(0x%lx)\n",
424
+ segv_err);
421425 }
422426 #endif
423427
424428 return 0;
425429 }
426430
431
+static int test_vsys_x(void)
432
+{
433
+#ifdef __x86_64__
434
+ if (vsyscall_map_x) {
435
+ /* We already tested this adequately. */
436
+ return 0;
437
+ }
438
+
439
+ printf("[RUN]\tMake sure that vsyscalls really page fault\n");
440
+
441
+ bool can_exec;
442
+ if (sigsetjmp(jmpbuf, 1) == 0) {
443
+ vgtod(NULL, NULL);
444
+ can_exec = true;
445
+ } else {
446
+ can_exec = false;
447
+ }
448
+
449
+ if (can_exec) {
450
+ printf("[FAIL]\tExecuting the vsyscall did not page fault\n");
451
+ return 1;
452
+ } else if (segv_err & (1 << 4)) { /* INSTR */
453
+ printf("[OK]\tExecuting the vsyscall page failed: #PF(0x%lx)\n",
454
+ segv_err);
455
+ } else {
456
+ printf("[FAIL]\tExecution failed with the wrong error: #PF(0x%lx)\n",
457
+ segv_err);
458
+ return 1;
459
+ }
460
+#endif
461
+
462
+ return 0;
463
+}
464
+
465
+/*
466
+ * Debuggers expect ptrace() to be able to peek at the vsyscall page.
467
+ * Use process_vm_readv() as a proxy for ptrace() to test this. We
468
+ * want it to work in the vsyscall=emulate case and to fail in the
469
+ * vsyscall=xonly case.
470
+ *
471
+ * It's worth noting that this ABI is a bit nutty. write(2) can't
472
+ * read from the vsyscall page on any kernel version or mode. The
473
+ * fact that ptrace() ever worked was a nice courtesy of old kernels,
474
+ * but the code to support it is fairly gross.
475
+ */
476
+static int test_process_vm_readv(void)
477
+{
478
+#ifdef __x86_64__
479
+ char buf[4096];
480
+ struct iovec local, remote;
481
+ int ret;
482
+
483
+ printf("[RUN]\tprocess_vm_readv() from vsyscall page\n");
484
+
485
+ local.iov_base = buf;
486
+ local.iov_len = 4096;
487
+ remote.iov_base = (void *)0xffffffffff600000;
488
+ remote.iov_len = 4096;
489
+ ret = process_vm_readv(getpid(), &local, 1, &remote, 1, 0);
490
+ if (ret != 4096) {
491
+ /*
492
+ * We expect process_vm_readv() to work if and only if the
493
+ * vsyscall page is readable.
494
+ */
495
+ printf("[%s]\tprocess_vm_readv() failed (ret = %d, errno = %d)\n", vsyscall_map_r ? "FAIL" : "OK", ret, errno);
496
+ return vsyscall_map_r ? 1 : 0;
497
+ }
498
+
499
+ if (vsyscall_map_r) {
500
+ if (!memcmp(buf, remote.iov_base, sizeof(buf))) {
501
+ printf("[OK]\tIt worked and read correct data\n");
502
+ } else {
503
+ printf("[FAIL]\tIt worked but returned incorrect data\n");
504
+ return 1;
505
+ }
506
+ } else {
507
+ printf("[FAIL]\tprocess_rm_readv() succeeded, but it should have failed in this configuration\n");
508
+ return 1;
509
+ }
510
+#endif
511
+
512
+ return 0;
513
+}
427514
428515 #ifdef __x86_64__
429
-#define X86_EFLAGS_TF (1UL << 8)
430516 static volatile sig_atomic_t num_vsyscall_traps;
431
-
432
-static unsigned long get_eflags(void)
433
-{
434
- unsigned long eflags;
435
- asm volatile ("pushfq\n\tpopq %0" : "=rm" (eflags));
436
- return eflags;
437
-}
438
-
439
-static void set_eflags(unsigned long eflags)
440
-{
441
- asm volatile ("pushq %0\n\tpopfq" : : "rm" (eflags) : "flags");
442
-}
443517
444518 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
445519 {
....@@ -455,7 +529,7 @@
455529 time_t tmp;
456530 bool is_native;
457531
458
- if (!vtime)
532
+ if (!vsyscall_map_x)
459533 return 0;
460534
461535 printf("[RUN]\tchecking that vsyscalls are emulated\n");
....@@ -497,6 +571,9 @@
497571
498572 sethandler(SIGSEGV, sigsegv, 0);
499573 nerrs += test_vsys_r();
574
+ nerrs += test_vsys_x();
575
+
576
+ nerrs += test_process_vm_readv();
500577
501578 #ifdef __x86_64__
502579 nerrs += test_emulation();