test_vsyscall.c 11 KB

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