1 /* SPDX-License-Identifier: GPL-2.0 */
9 #include <sys/syscall.h>
15 #include <sys/ucontext.h>
33 # define SYS_getcpu 309
35 # define SYS_getcpu 318
39 /* max length of lines in /proc/self/maps - anything longer is skipped here */
40 #define MAPS_LINE_LEN 128
42 static void sethandler(int sig
, void (*handler
)(int, siginfo_t
*, void *),
46 memset(&sa
, 0, sizeof(sa
));
47 sa
.sa_sigaction
= handler
;
48 sa
.sa_flags
= SA_SIGINFO
| flags
;
49 sigemptyset(&sa
.sa_mask
);
50 if (sigaction(sig
, &sa
, 0))
54 /* vsyscalls and vDSO */
55 bool vsyscall_map_r
= false, vsyscall_map_x
= false;
57 typedef long (*gtod_t
)(struct timeval
*tv
, struct timezone
*tz
);
58 const gtod_t vgtod
= (gtod_t
)VSYS(0xffffffffff600000);
61 typedef int (*vgettime_t
)(clockid_t
, struct timespec
*);
62 vgettime_t vdso_gettime
;
64 typedef long (*time_func_t
)(time_t *t
);
65 const time_func_t vtime
= (time_func_t
)VSYS(0xffffffffff600400);
66 time_func_t vdso_time
;
68 typedef long (*getcpu_t
)(unsigned *, unsigned *, void *);
69 const getcpu_t vgetcpu
= (getcpu_t
)VSYS(0xffffffffff600800);
72 static void init_vdso(void)
74 void *vdso
= dlopen("linux-vdso.so.1", RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
76 vdso
= dlopen("linux-gate.so.1", RTLD_LAZY
| RTLD_LOCAL
| RTLD_NOLOAD
);
78 printf("[WARN]\tfailed to find vDSO\n");
82 vdso_gtod
= (gtod_t
)dlsym(vdso
, "__vdso_gettimeofday");
84 printf("[WARN]\tfailed to find gettimeofday in vDSO\n");
86 vdso_gettime
= (vgettime_t
)dlsym(vdso
, "__vdso_clock_gettime");
88 printf("[WARN]\tfailed to find clock_gettime in vDSO\n");
90 vdso_time
= (time_func_t
)dlsym(vdso
, "__vdso_time");
92 printf("[WARN]\tfailed to find time in vDSO\n");
94 vdso_getcpu
= (getcpu_t
)dlsym(vdso
, "__vdso_getcpu");
96 /* getcpu() was never wired up in the 32-bit vDSO. */
97 printf("[%s]\tfailed to find getcpu in vDSO\n",
98 sizeof(long) == 8 ? "WARN" : "NOTE");
102 static int init_vsys(void)
107 char line
[MAPS_LINE_LEN
];
110 maps
= fopen("/proc/self/maps", "r");
112 printf("[WARN]\tCould not open /proc/self/maps -- assuming vsyscall is r-x\n");
113 vsyscall_map_r
= true;
117 while (fgets(line
, MAPS_LINE_LEN
, maps
)) {
120 char name
[MAPS_LINE_LEN
];
122 /* sscanf() is safe here as strlen(name) >= strlen(line) */
123 if (sscanf(line
, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
124 &start
, &end
, &r
, &x
, name
) != 5)
127 if (strcmp(name
, "[vsyscall]"))
130 printf("\tvsyscall map: %s", line
);
132 if (start
!= (void *)0xffffffffff600000 ||
133 end
!= (void *)0xffffffffff601000) {
134 printf("[FAIL]\taddress range is nonsense\n");
138 printf("\tvsyscall permissions are %c-%c\n", r
, x
);
139 vsyscall_map_r
= (r
== 'r');
140 vsyscall_map_x
= (x
== 'x');
149 printf("\tno vsyscall map in /proc/self/maps\n");
150 vsyscall_map_r
= false;
151 vsyscall_map_x
= false;
161 static inline long sys_gtod(struct timeval
*tv
, struct timezone
*tz
)
163 return syscall(SYS_gettimeofday
, tv
, tz
);
166 static inline int sys_clock_gettime(clockid_t id
, struct timespec
*ts
)
168 return syscall(SYS_clock_gettime
, id
, ts
);
171 static inline long sys_time(time_t *t
)
173 return syscall(SYS_time
, t
);
176 static inline long sys_getcpu(unsigned * cpu
, unsigned * node
,
179 return syscall(SYS_getcpu
, cpu
, node
, cache
);
182 static jmp_buf jmpbuf
;
183 static volatile unsigned long segv_err
;
185 static void sigsegv(int sig
, siginfo_t
*info
, void *ctx_void
)
187 ucontext_t
*ctx
= (ucontext_t
*)ctx_void
;
189 segv_err
= ctx
->uc_mcontext
.gregs
[REG_ERR
];
190 siglongjmp(jmpbuf
, 1);
193 static double tv_diff(const struct timeval
*a
, const struct timeval
*b
)
195 return (double)(a
->tv_sec
- b
->tv_sec
) +
196 (double)((int)a
->tv_usec
- (int)b
->tv_usec
) * 1e-6;
199 static int check_gtod(const struct timeval
*tv_sys1
,
200 const struct timeval
*tv_sys2
,
201 const struct timezone
*tz_sys
,
203 const struct timeval
*tv_other
,
204 const struct timezone
*tz_other
)
209 if (tz_other
&& (tz_sys
->tz_minuteswest
!= tz_other
->tz_minuteswest
|| tz_sys
->tz_dsttime
!= tz_other
->tz_dsttime
)) {
210 printf("[FAIL] %s tz mismatch\n", which
);
214 d1
= tv_diff(tv_other
, tv_sys1
);
215 d2
= tv_diff(tv_sys2
, tv_other
);
216 printf("\t%s time offsets: %lf %lf\n", which
, d1
, d2
);
218 if (d1
< 0 || d2
< 0) {
219 printf("[FAIL]\t%s time was inconsistent with the syscall\n", which
);
222 printf("[OK]\t%s gettimeofday()'s timeval was okay\n", which
);
228 static int test_gtod(void)
230 struct timeval tv_sys1
, tv_sys2
, tv_vdso
, tv_vsys
;
231 struct timezone tz_sys
, tz_vdso
, tz_vsys
;
236 printf("[RUN]\ttest gettimeofday()\n");
238 if (sys_gtod(&tv_sys1
, &tz_sys
) != 0)
239 err(1, "syscall gettimeofday");
241 ret_vdso
= vdso_gtod(&tv_vdso
, &tz_vdso
);
243 ret_vsys
= vgtod(&tv_vsys
, &tz_vsys
);
244 if (sys_gtod(&tv_sys2
, &tz_sys
) != 0)
245 err(1, "syscall gettimeofday");
249 nerrs
+= check_gtod(&tv_sys1
, &tv_sys2
, &tz_sys
, "vDSO", &tv_vdso
, &tz_vdso
);
251 printf("[FAIL]\tvDSO gettimeofday() failed: %ld\n", ret_vdso
);
256 if (vsyscall_map_x
) {
258 nerrs
+= check_gtod(&tv_sys1
, &tv_sys2
, &tz_sys
, "vsyscall", &tv_vsys
, &tz_vsys
);
260 printf("[FAIL]\tvsys gettimeofday() failed: %ld\n", ret_vsys
);
268 static int test_time(void) {
271 printf("[RUN]\ttest time()\n");
272 long t_sys1
, t_sys2
, t_vdso
= 0, t_vsys
= 0;
273 long t2_sys1
= -1, t2_sys2
= -1, t2_vdso
= -1, t2_vsys
= -1;
274 t_sys1
= sys_time(&t2_sys1
);
276 t_vdso
= vdso_time(&t2_vdso
);
278 t_vsys
= vtime(&t2_vsys
);
279 t_sys2
= sys_time(&t2_sys2
);
280 if (t_sys1
< 0 || t_sys1
!= t2_sys1
|| t_sys2
< 0 || t_sys2
!= t2_sys2
) {
281 printf("[FAIL]\tsyscall failed (ret1:%ld output1:%ld ret2:%ld output2:%ld)\n", t_sys1
, t2_sys1
, t_sys2
, t2_sys2
);
287 if (t_vdso
< 0 || t_vdso
!= t2_vdso
) {
288 printf("[FAIL]\tvDSO failed (ret:%ld output:%ld)\n", t_vdso
, t2_vdso
);
290 } else if (t_vdso
< t_sys1
|| t_vdso
> t_sys2
) {
291 printf("[FAIL]\tvDSO returned the wrong time (%ld %ld %ld)\n", t_sys1
, t_vdso
, t_sys2
);
294 printf("[OK]\tvDSO time() is okay\n");
298 if (vsyscall_map_x
) {
299 if (t_vsys
< 0 || t_vsys
!= t2_vsys
) {
300 printf("[FAIL]\tvsyscall failed (ret:%ld output:%ld)\n", t_vsys
, t2_vsys
);
302 } else if (t_vsys
< t_sys1
|| t_vsys
> t_sys2
) {
303 printf("[FAIL]\tvsyscall returned the wrong time (%ld %ld %ld)\n", t_sys1
, t_vsys
, t_sys2
);
306 printf("[OK]\tvsyscall time() is okay\n");
313 static int test_getcpu(int cpu
)
316 long ret_sys
, ret_vdso
= -1, ret_vsys
= -1;
318 printf("[RUN]\tgetcpu() on CPU %d\n", cpu
);
322 CPU_SET(cpu
, &cpuset
);
323 if (sched_setaffinity(0, sizeof(cpuset
), &cpuset
) != 0) {
324 printf("[SKIP]\tfailed to force CPU %d\n", cpu
);
328 unsigned cpu_sys
, cpu_vdso
, cpu_vsys
, node_sys
, node_vdso
, node_vsys
;
330 bool have_node
= false;
331 ret_sys
= sys_getcpu(&cpu_sys
, &node_sys
, 0);
333 ret_vdso
= vdso_getcpu(&cpu_vdso
, &node_vdso
, 0);
335 ret_vsys
= vgetcpu(&cpu_vsys
, &node_vsys
, 0);
338 if (cpu_sys
!= cpu
) {
339 printf("[FAIL]\tsyscall reported CPU %hu but should be %d\n", cpu_sys
, cpu
);
349 printf("[FAIL]\tvDSO getcpu() failed\n");
357 if (cpu_vdso
!= cpu
) {
358 printf("[FAIL]\tvDSO reported CPU %hu but should be %d\n", cpu_vdso
, cpu
);
361 printf("[OK]\tvDSO reported correct CPU\n");
364 if (node_vdso
!= node
) {
365 printf("[FAIL]\tvDSO reported node %hu but should be %hu\n", node_vdso
, node
);
368 printf("[OK]\tvDSO reported correct node\n");
373 if (vsyscall_map_x
) {
375 printf("[FAIL]\tvsyscall getcpu() failed\n");
383 if (cpu_vsys
!= cpu
) {
384 printf("[FAIL]\tvsyscall reported CPU %hu but should be %d\n", cpu_vsys
, cpu
);
387 printf("[OK]\tvsyscall reported correct CPU\n");
390 if (node_vsys
!= node
) {
391 printf("[FAIL]\tvsyscall reported node %hu but should be %hu\n", node_vsys
, node
);
394 printf("[OK]\tvsyscall reported correct node\n");
402 static int test_vsys_r(void)
405 printf("[RUN]\tChecking read access to the vsyscall page\n");
407 if (sigsetjmp(jmpbuf
, 1) == 0) {
408 *(volatile int *)0xffffffffff600000;
414 if (can_read
&& !vsyscall_map_r
) {
415 printf("[FAIL]\tWe have read access, but we shouldn't\n");
417 } else if (!can_read
&& vsyscall_map_r
) {
418 printf("[FAIL]\tWe don't have read access, but we should\n");
420 } else if (can_read
) {
421 printf("[OK]\tWe have read access\n");
423 printf("[OK]\tWe do not have read access: #PF(0x%lx)\n",
431 static int test_vsys_x(void)
434 if (vsyscall_map_x
) {
435 /* We already tested this adequately. */
439 printf("[RUN]\tMake sure that vsyscalls really page fault\n");
442 if (sigsetjmp(jmpbuf
, 1) == 0) {
450 printf("[FAIL]\tExecuting the vsyscall did not page fault\n");
452 } else if (segv_err
& (1 << 4)) { /* INSTR */
453 printf("[OK]\tExecuting the vsyscall page failed: #PF(0x%lx)\n",
456 printf("[FAIL]\tExecution failed with the wrong error: #PF(0x%lx)\n",
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.
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.
476 static int test_process_vm_readv(void)
480 struct iovec local
, remote
;
483 printf("[RUN]\tprocess_vm_readv() from vsyscall page\n");
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);
492 * We expect process_vm_readv() to work if and only if the
493 * vsyscall page is readable.
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;
499 if (vsyscall_map_r
) {
500 if (!memcmp(buf
, (const void *)0xffffffffff600000, 4096)) {
501 printf("[OK]\tIt worked and read correct data\n");
503 printf("[FAIL]\tIt worked but returned incorrect data\n");
507 printf("[FAIL]\tprocess_rm_readv() succeeded, but it should have failed in this configuration\n");
516 static volatile sig_atomic_t num_vsyscall_traps
;
518 static void sigtrap(int sig
, siginfo_t
*info
, void *ctx_void
)
520 ucontext_t
*ctx
= (ucontext_t
*)ctx_void
;
521 unsigned long ip
= ctx
->uc_mcontext
.gregs
[REG_RIP
];
523 if (((ip
^ 0xffffffffff600000UL
) & ~0xfffUL
) == 0)
524 num_vsyscall_traps
++;
527 static int test_emulation(void)
535 printf("[RUN]\tchecking that vsyscalls are emulated\n");
536 sethandler(SIGTRAP
, sigtrap
, 0);
537 set_eflags(get_eflags() | X86_EFLAGS_TF
);
539 set_eflags(get_eflags() & ~X86_EFLAGS_TF
);
542 * If vsyscalls are emulated, we expect a single trap in the
543 * vsyscall page -- the call instruction will trap with RIP
544 * pointing to the entry point before emulation takes over.
545 * In native mode, we expect two traps, since whatever code
546 * the vsyscall page contains will be more than just a ret
549 is_native
= (num_vsyscall_traps
> 1);
551 printf("[%s]\tvsyscalls are %s (%d instructions in vsyscall page)\n",
552 (is_native
? "FAIL" : "OK"),
553 (is_native
? "native" : "emulated"),
554 (int)num_vsyscall_traps
);
560 int main(int argc
, char **argv
)
565 nerrs
+= init_vsys();
567 nerrs
+= test_gtod();
568 nerrs
+= test_time();
569 nerrs
+= test_getcpu(0);
570 nerrs
+= test_getcpu(1);
572 sethandler(SIGSEGV
, sigsegv
, 0);
573 nerrs
+= test_vsys_r();
574 nerrs
+= test_vsys_x();
576 nerrs
+= test_process_vm_readv();
579 nerrs
+= test_emulation();
582 return nerrs
? 1 : 0;