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