1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #define _GNU_SOURCE
4 
5 #include <stdio.h>
6 #include <sys/time.h>
7 #include <time.h>
8 #include <stdlib.h>
9 #include <sys/syscall.h>
10 #include <unistd.h>
11 #include <dlfcn.h>
12 #include <string.h>
13 #include <inttypes.h>
14 #include <signal.h>
15 #include <sys/ucontext.h>
16 #include <errno.h>
17 #include <err.h>
18 #include <sched.h>
19 #include <stdbool.h>
20 #include <setjmp.h>
21 
22 #ifdef __x86_64__
23 # define VSYS(x) (x)
24 #else
25 # define VSYS(x) 0
26 #endif
27 
28 #ifndef SYS_getcpu
29 # ifdef __x86_64__
30 #  define SYS_getcpu 309
31 # else
32 #  define SYS_getcpu 318
33 # endif
34 #endif
35 
36 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
37 		       int flags)
38 {
39 	struct sigaction sa;
40 	memset(&sa, 0, sizeof(sa));
41 	sa.sa_sigaction = handler;
42 	sa.sa_flags = SA_SIGINFO | flags;
43 	sigemptyset(&sa.sa_mask);
44 	if (sigaction(sig, &sa, 0))
45 		err(1, "sigaction");
46 }
47 
48 /* vsyscalls and vDSO */
49 bool should_read_vsyscall = false;
50 
51 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
52 gtod_t vgtod = (gtod_t)VSYS(0xffffffffff600000);
53 gtod_t vdso_gtod;
54 
55 typedef int (*vgettime_t)(clockid_t, struct timespec *);
56 vgettime_t vdso_gettime;
57 
58 typedef long (*time_func_t)(time_t *t);
59 time_func_t vtime = (time_func_t)VSYS(0xffffffffff600400);
60 time_func_t vdso_time;
61 
62 typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
63 getcpu_t vgetcpu = (getcpu_t)VSYS(0xffffffffff600800);
64 getcpu_t vdso_getcpu;
65 
66 static void init_vdso(void)
67 {
68 	void *vdso = dlopen("linux-vdso.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
69 	if (!vdso)
70 		vdso = dlopen("linux-gate.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
71 	if (!vdso) {
72 		printf("[WARN]\tfailed to find vDSO\n");
73 		return;
74 	}
75 
76 	vdso_gtod = (gtod_t)dlsym(vdso, "__vdso_gettimeofday");
77 	if (!vdso_gtod)
78 		printf("[WARN]\tfailed to find gettimeofday in vDSO\n");
79 
80 	vdso_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime");
81 	if (!vdso_gettime)
82 		printf("[WARN]\tfailed to find clock_gettime in vDSO\n");
83 
84 	vdso_time = (time_func_t)dlsym(vdso, "__vdso_time");
85 	if (!vdso_time)
86 		printf("[WARN]\tfailed to find time in vDSO\n");
87 
88 	vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
89 	if (!vdso_getcpu) {
90 		/* getcpu() was never wired up in the 32-bit vDSO. */
91 		printf("[%s]\tfailed to find getcpu in vDSO\n",
92 		       sizeof(long) == 8 ? "WARN" : "NOTE");
93 	}
94 }
95 
96 static int init_vsys(void)
97 {
98 #ifdef __x86_64__
99 	int nerrs = 0;
100 	FILE *maps;
101 	char line[128];
102 	bool found = false;
103 
104 	maps = fopen("/proc/self/maps", "r");
105 	if (!maps) {
106 		printf("[WARN]\tCould not open /proc/self/maps -- assuming vsyscall is r-x\n");
107 		should_read_vsyscall = true;
108 		return 0;
109 	}
110 
111 	while (fgets(line, sizeof(line), maps)) {
112 		char r, x;
113 		void *start, *end;
114 		char name[128];
115 		if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
116 			   &start, &end, &r, &x, name) != 5)
117 			continue;
118 
119 		if (strcmp(name, "[vsyscall]"))
120 			continue;
121 
122 		printf("\tvsyscall map: %s", line);
123 
124 		if (start != (void *)0xffffffffff600000 ||
125 		    end != (void *)0xffffffffff601000) {
126 			printf("[FAIL]\taddress range is nonsense\n");
127 			nerrs++;
128 		}
129 
130 		printf("\tvsyscall permissions are %c-%c\n", r, x);
131 		should_read_vsyscall = (r == 'r');
132 		if (x != 'x') {
133 			vgtod = NULL;
134 			vtime = NULL;
135 			vgetcpu = NULL;
136 		}
137 
138 		found = true;
139 		break;
140 	}
141 
142 	fclose(maps);
143 
144 	if (!found) {
145 		printf("\tno vsyscall map in /proc/self/maps\n");
146 		should_read_vsyscall = false;
147 		vgtod = NULL;
148 		vtime = NULL;
149 		vgetcpu = NULL;
150 	}
151 
152 	return nerrs;
153 #else
154 	return 0;
155 #endif
156 }
157 
158 /* syscalls */
159 static inline long sys_gtod(struct timeval *tv, struct timezone *tz)
160 {
161 	return syscall(SYS_gettimeofday, tv, tz);
162 }
163 
164 static inline int sys_clock_gettime(clockid_t id, struct timespec *ts)
165 {
166 	return syscall(SYS_clock_gettime, id, ts);
167 }
168 
169 static inline long sys_time(time_t *t)
170 {
171 	return syscall(SYS_time, t);
172 }
173 
174 static inline long sys_getcpu(unsigned * cpu, unsigned * node,
175 			      void* cache)
176 {
177 	return syscall(SYS_getcpu, cpu, node, cache);
178 }
179 
180 static jmp_buf jmpbuf;
181 
182 static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
183 {
184 	siglongjmp(jmpbuf, 1);
185 }
186 
187 static double tv_diff(const struct timeval *a, const struct timeval *b)
188 {
189 	return (double)(a->tv_sec - b->tv_sec) +
190 		(double)((int)a->tv_usec - (int)b->tv_usec) * 1e-6;
191 }
192 
193 static int check_gtod(const struct timeval *tv_sys1,
194 		      const struct timeval *tv_sys2,
195 		      const struct timezone *tz_sys,
196 		      const char *which,
197 		      const struct timeval *tv_other,
198 		      const struct timezone *tz_other)
199 {
200 	int nerrs = 0;
201 	double d1, d2;
202 
203 	if (tz_other && (tz_sys->tz_minuteswest != tz_other->tz_minuteswest || tz_sys->tz_dsttime != tz_other->tz_dsttime)) {
204 		printf("[FAIL] %s tz mismatch\n", which);
205 		nerrs++;
206 	}
207 
208 	d1 = tv_diff(tv_other, tv_sys1);
209 	d2 = tv_diff(tv_sys2, tv_other);
210 	printf("\t%s time offsets: %lf %lf\n", which, d1, d2);
211 
212 	if (d1 < 0 || d2 < 0) {
213 		printf("[FAIL]\t%s time was inconsistent with the syscall\n", which);
214 		nerrs++;
215 	} else {
216 		printf("[OK]\t%s gettimeofday()'s timeval was okay\n", which);
217 	}
218 
219 	return nerrs;
220 }
221 
222 static int test_gtod(void)
223 {
224 	struct timeval tv_sys1, tv_sys2, tv_vdso, tv_vsys;
225 	struct timezone tz_sys, tz_vdso, tz_vsys;
226 	long ret_vdso = -1;
227 	long ret_vsys = -1;
228 	int nerrs = 0;
229 
230 	printf("[RUN]\ttest gettimeofday()\n");
231 
232 	if (sys_gtod(&tv_sys1, &tz_sys) != 0)
233 		err(1, "syscall gettimeofday");
234 	if (vdso_gtod)
235 		ret_vdso = vdso_gtod(&tv_vdso, &tz_vdso);
236 	if (vgtod)
237 		ret_vsys = vgtod(&tv_vsys, &tz_vsys);
238 	if (sys_gtod(&tv_sys2, &tz_sys) != 0)
239 		err(1, "syscall gettimeofday");
240 
241 	if (vdso_gtod) {
242 		if (ret_vdso == 0) {
243 			nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vDSO", &tv_vdso, &tz_vdso);
244 		} else {
245 			printf("[FAIL]\tvDSO gettimeofday() failed: %ld\n", ret_vdso);
246 			nerrs++;
247 		}
248 	}
249 
250 	if (vgtod) {
251 		if (ret_vsys == 0) {
252 			nerrs += check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vsyscall", &tv_vsys, &tz_vsys);
253 		} else {
254 			printf("[FAIL]\tvsys gettimeofday() failed: %ld\n", ret_vsys);
255 			nerrs++;
256 		}
257 	}
258 
259 	return nerrs;
260 }
261 
262 static int test_time(void) {
263 	int nerrs = 0;
264 
265 	printf("[RUN]\ttest time()\n");
266 	long t_sys1, t_sys2, t_vdso = 0, t_vsys = 0;
267 	long t2_sys1 = -1, t2_sys2 = -1, t2_vdso = -1, t2_vsys = -1;
268 	t_sys1 = sys_time(&t2_sys1);
269 	if (vdso_time)
270 		t_vdso = vdso_time(&t2_vdso);
271 	if (vtime)
272 		t_vsys = vtime(&t2_vsys);
273 	t_sys2 = sys_time(&t2_sys2);
274 	if (t_sys1 < 0 || t_sys1 != t2_sys1 || t_sys2 < 0 || t_sys2 != t2_sys2) {
275 		printf("[FAIL]\tsyscall failed (ret1:%ld output1:%ld ret2:%ld output2:%ld)\n", t_sys1, t2_sys1, t_sys2, t2_sys2);
276 		nerrs++;
277 		return nerrs;
278 	}
279 
280 	if (vdso_time) {
281 		if (t_vdso < 0 || t_vdso != t2_vdso) {
282 			printf("[FAIL]\tvDSO failed (ret:%ld output:%ld)\n", t_vdso, t2_vdso);
283 			nerrs++;
284 		} else if (t_vdso < t_sys1 || t_vdso > t_sys2) {
285 			printf("[FAIL]\tvDSO returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vdso, t_sys2);
286 			nerrs++;
287 		} else {
288 			printf("[OK]\tvDSO time() is okay\n");
289 		}
290 	}
291 
292 	if (vtime) {
293 		if (t_vsys < 0 || t_vsys != t2_vsys) {
294 			printf("[FAIL]\tvsyscall failed (ret:%ld output:%ld)\n", t_vsys, t2_vsys);
295 			nerrs++;
296 		} else if (t_vsys < t_sys1 || t_vsys > t_sys2) {
297 			printf("[FAIL]\tvsyscall returned the wrong time (%ld %ld %ld)\n", t_sys1, t_vsys, t_sys2);
298 			nerrs++;
299 		} else {
300 			printf("[OK]\tvsyscall time() is okay\n");
301 		}
302 	}
303 
304 	return nerrs;
305 }
306 
307 static int test_getcpu(int cpu)
308 {
309 	int nerrs = 0;
310 	long ret_sys, ret_vdso = -1, ret_vsys = -1;
311 
312 	printf("[RUN]\tgetcpu() on CPU %d\n", cpu);
313 
314 	cpu_set_t cpuset;
315 	CPU_ZERO(&cpuset);
316 	CPU_SET(cpu, &cpuset);
317 	if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
318 		printf("[SKIP]\tfailed to force CPU %d\n", cpu);
319 		return nerrs;
320 	}
321 
322 	unsigned cpu_sys, cpu_vdso, cpu_vsys, node_sys, node_vdso, node_vsys;
323 	unsigned node = 0;
324 	bool have_node = false;
325 	ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
326 	if (vdso_getcpu)
327 		ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
328 	if (vgetcpu)
329 		ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
330 
331 	if (ret_sys == 0) {
332 		if (cpu_sys != cpu) {
333 			printf("[FAIL]\tsyscall reported CPU %hu but should be %d\n", cpu_sys, cpu);
334 			nerrs++;
335 		}
336 
337 		have_node = true;
338 		node = node_sys;
339 	}
340 
341 	if (vdso_getcpu) {
342 		if (ret_vdso) {
343 			printf("[FAIL]\tvDSO getcpu() failed\n");
344 			nerrs++;
345 		} else {
346 			if (!have_node) {
347 				have_node = true;
348 				node = node_vdso;
349 			}
350 
351 			if (cpu_vdso != cpu) {
352 				printf("[FAIL]\tvDSO reported CPU %hu but should be %d\n", cpu_vdso, cpu);
353 				nerrs++;
354 			} else {
355 				printf("[OK]\tvDSO reported correct CPU\n");
356 			}
357 
358 			if (node_vdso != node) {
359 				printf("[FAIL]\tvDSO reported node %hu but should be %hu\n", node_vdso, node);
360 				nerrs++;
361 			} else {
362 				printf("[OK]\tvDSO reported correct node\n");
363 			}
364 		}
365 	}
366 
367 	if (vgetcpu) {
368 		if (ret_vsys) {
369 			printf("[FAIL]\tvsyscall getcpu() failed\n");
370 			nerrs++;
371 		} else {
372 			if (!have_node) {
373 				have_node = true;
374 				node = node_vsys;
375 			}
376 
377 			if (cpu_vsys != cpu) {
378 				printf("[FAIL]\tvsyscall reported CPU %hu but should be %d\n", cpu_vsys, cpu);
379 				nerrs++;
380 			} else {
381 				printf("[OK]\tvsyscall reported correct CPU\n");
382 			}
383 
384 			if (node_vsys != node) {
385 				printf("[FAIL]\tvsyscall reported node %hu but should be %hu\n", node_vsys, node);
386 				nerrs++;
387 			} else {
388 				printf("[OK]\tvsyscall reported correct node\n");
389 			}
390 		}
391 	}
392 
393 	return nerrs;
394 }
395 
396 static int test_vsys_r(void)
397 {
398 #ifdef __x86_64__
399 	printf("[RUN]\tChecking read access to the vsyscall page\n");
400 	bool can_read;
401 	if (sigsetjmp(jmpbuf, 1) == 0) {
402 		*(volatile int *)0xffffffffff600000;
403 		can_read = true;
404 	} else {
405 		can_read = false;
406 	}
407 
408 	if (can_read && !should_read_vsyscall) {
409 		printf("[FAIL]\tWe have read access, but we shouldn't\n");
410 		return 1;
411 	} else if (!can_read && should_read_vsyscall) {
412 		printf("[FAIL]\tWe don't have read access, but we should\n");
413 		return 1;
414 	} else {
415 		printf("[OK]\tgot expected result\n");
416 	}
417 #endif
418 
419 	return 0;
420 }
421 
422 
423 #ifdef __x86_64__
424 #define X86_EFLAGS_TF (1UL << 8)
425 static volatile sig_atomic_t num_vsyscall_traps;
426 
427 static unsigned long get_eflags(void)
428 {
429 	unsigned long eflags;
430 	asm volatile ("pushfq\n\tpopq %0" : "=rm" (eflags));
431 	return eflags;
432 }
433 
434 static void set_eflags(unsigned long eflags)
435 {
436 	asm volatile ("pushq %0\n\tpopfq" : : "rm" (eflags) : "flags");
437 }
438 
439 static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
440 {
441 	ucontext_t *ctx = (ucontext_t *)ctx_void;
442 	unsigned long ip = ctx->uc_mcontext.gregs[REG_RIP];
443 
444 	if (((ip ^ 0xffffffffff600000UL) & ~0xfffUL) == 0)
445 		num_vsyscall_traps++;
446 }
447 
448 static int test_native_vsyscall(void)
449 {
450 	time_t tmp;
451 	bool is_native;
452 
453 	if (!vtime)
454 		return 0;
455 
456 	printf("[RUN]\tchecking for native vsyscall\n");
457 	sethandler(SIGTRAP, sigtrap, 0);
458 	set_eflags(get_eflags() | X86_EFLAGS_TF);
459 	vtime(&tmp);
460 	set_eflags(get_eflags() & ~X86_EFLAGS_TF);
461 
462 	/*
463 	 * If vsyscalls are emulated, we expect a single trap in the
464 	 * vsyscall page -- the call instruction will trap with RIP
465 	 * pointing to the entry point before emulation takes over.
466 	 * In native mode, we expect two traps, since whatever code
467 	 * the vsyscall page contains will be more than just a ret
468 	 * instruction.
469 	 */
470 	is_native = (num_vsyscall_traps > 1);
471 
472 	printf("\tvsyscalls are %s (%d instructions in vsyscall page)\n",
473 	       (is_native ? "native" : "emulated"),
474 	       (int)num_vsyscall_traps);
475 
476 	return 0;
477 }
478 #endif
479 
480 int main(int argc, char **argv)
481 {
482 	int nerrs = 0;
483 
484 	init_vdso();
485 	nerrs += init_vsys();
486 
487 	nerrs += test_gtod();
488 	nerrs += test_time();
489 	nerrs += test_getcpu(0);
490 	nerrs += test_getcpu(1);
491 
492 	sethandler(SIGSEGV, sigsegv, 0);
493 	nerrs += test_vsys_r();
494 
495 #ifdef __x86_64__
496 	nerrs += test_native_vsyscall();
497 #endif
498 
499 	return nerrs ? 1 : 0;
500 }
501