test_vsyscall.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. #include <sys/uio.h>
  20. #include "helpers.h"
  21. #include "kselftest.h"
  22. #ifdef __x86_64__
  23. #define TOTAL_TESTS 13
  24. #else
  25. #define TOTAL_TESTS 8
  26. #endif
  27. #ifdef __x86_64__
  28. # define VSYS(x) (x)
  29. #else
  30. # define VSYS(x) 0
  31. #endif
  32. #ifndef SYS_getcpu
  33. # ifdef __x86_64__
  34. # define SYS_getcpu 309
  35. # else
  36. # define SYS_getcpu 318
  37. # endif
  38. #endif
  39. /* max length of lines in /proc/self/maps - anything longer is skipped here */
  40. #define MAPS_LINE_LEN 128
  41. /* vsyscalls and vDSO */
  42. bool vsyscall_map_r = false, vsyscall_map_x = false;
  43. typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
  44. const gtod_t vgtod = (gtod_t)VSYS(0xffffffffff600000);
  45. gtod_t vdso_gtod;
  46. typedef int (*vgettime_t)(clockid_t, struct timespec *);
  47. vgettime_t vdso_gettime;
  48. typedef long (*time_func_t)(time_t *t);
  49. const time_func_t vtime = (time_func_t)VSYS(0xffffffffff600400);
  50. time_func_t vdso_time;
  51. typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
  52. const getcpu_t vgetcpu = (getcpu_t)VSYS(0xffffffffff600800);
  53. getcpu_t vdso_getcpu;
  54. static void init_vdso(void)
  55. {
  56. void *vdso = dlopen("linux-vdso.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
  57. if (!vdso)
  58. vdso = dlopen("linux-gate.so.1", RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
  59. if (!vdso) {
  60. ksft_print_msg("[WARN] failed to find vDSO\n");
  61. return;
  62. }
  63. vdso_gtod = (gtod_t)dlsym(vdso, "__vdso_gettimeofday");
  64. if (!vdso_gtod)
  65. ksft_print_msg("[WARN] failed to find gettimeofday in vDSO\n");
  66. vdso_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime");
  67. if (!vdso_gettime)
  68. ksft_print_msg("[WARN] failed to find clock_gettime in vDSO\n");
  69. vdso_time = (time_func_t)dlsym(vdso, "__vdso_time");
  70. if (!vdso_time)
  71. ksft_print_msg("[WARN] failed to find time in vDSO\n");
  72. vdso_getcpu = (getcpu_t)dlsym(vdso, "__vdso_getcpu");
  73. if (!vdso_getcpu)
  74. ksft_print_msg("[WARN] failed to find getcpu in vDSO\n");
  75. }
  76. /* syscalls */
  77. static inline long sys_gtod(struct timeval *tv, struct timezone *tz)
  78. {
  79. return syscall(SYS_gettimeofday, tv, tz);
  80. }
  81. static inline long sys_time(time_t *t)
  82. {
  83. return syscall(SYS_time, t);
  84. }
  85. static inline long sys_getcpu(unsigned * cpu, unsigned * node,
  86. void* cache)
  87. {
  88. return syscall(SYS_getcpu, cpu, node, cache);
  89. }
  90. static double tv_diff(const struct timeval *a, const struct timeval *b)
  91. {
  92. return (double)(a->tv_sec - b->tv_sec) +
  93. (double)((int)a->tv_usec - (int)b->tv_usec) * 1e-6;
  94. }
  95. static void check_gtod(const struct timeval *tv_sys1,
  96. const struct timeval *tv_sys2,
  97. const struct timezone *tz_sys,
  98. const char *which,
  99. const struct timeval *tv_other,
  100. const struct timezone *tz_other)
  101. {
  102. double d1, d2;
  103. if (tz_other && (tz_sys->tz_minuteswest != tz_other->tz_minuteswest ||
  104. tz_sys->tz_dsttime != tz_other->tz_dsttime))
  105. ksft_print_msg("%s tz mismatch\n", which);
  106. d1 = tv_diff(tv_other, tv_sys1);
  107. d2 = tv_diff(tv_sys2, tv_other);
  108. ksft_print_msg("%s time offsets: %lf %lf\n", which, d1, d2);
  109. ksft_test_result(!(d1 < 0 || d2 < 0), "%s gettimeofday()'s timeval\n", which);
  110. }
  111. static void test_gtod(void)
  112. {
  113. struct timeval tv_sys1, tv_sys2, tv_vdso, tv_vsys;
  114. struct timezone tz_sys, tz_vdso, tz_vsys;
  115. long ret_vdso = -1;
  116. long ret_vsys = -1;
  117. ksft_print_msg("test gettimeofday()\n");
  118. if (sys_gtod(&tv_sys1, &tz_sys) != 0)
  119. ksft_exit_fail_msg("syscall gettimeofday: %s\n", strerror(errno));
  120. if (vdso_gtod)
  121. ret_vdso = vdso_gtod(&tv_vdso, &tz_vdso);
  122. if (vsyscall_map_x)
  123. ret_vsys = vgtod(&tv_vsys, &tz_vsys);
  124. if (sys_gtod(&tv_sys2, &tz_sys) != 0)
  125. ksft_exit_fail_msg("syscall gettimeofday: %s\n", strerror(errno));
  126. if (vdso_gtod) {
  127. if (ret_vdso == 0)
  128. check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vDSO", &tv_vdso, &tz_vdso);
  129. else
  130. ksft_test_result_fail("vDSO gettimeofday() failed: %ld\n", ret_vdso);
  131. } else {
  132. ksft_test_result_skip("vdso_gtod isn't set\n");
  133. }
  134. if (vsyscall_map_x) {
  135. if (ret_vsys == 0)
  136. check_gtod(&tv_sys1, &tv_sys2, &tz_sys, "vsyscall", &tv_vsys, &tz_vsys);
  137. else
  138. ksft_test_result_fail("vsys gettimeofday() failed: %ld\n", ret_vsys);
  139. } else {
  140. ksft_test_result_skip("vsyscall_map_x isn't set\n");
  141. }
  142. }
  143. static void test_time(void)
  144. {
  145. long t_sys1, t_sys2, t_vdso = 0, t_vsys = 0;
  146. long t2_sys1 = -1, t2_sys2 = -1, t2_vdso = -1, t2_vsys = -1;
  147. ksft_print_msg("test time()\n");
  148. t_sys1 = sys_time(&t2_sys1);
  149. if (vdso_time)
  150. t_vdso = vdso_time(&t2_vdso);
  151. if (vsyscall_map_x)
  152. t_vsys = vtime(&t2_vsys);
  153. t_sys2 = sys_time(&t2_sys2);
  154. if (t_sys1 < 0 || t_sys1 != t2_sys1 || t_sys2 < 0 || t_sys2 != t2_sys2) {
  155. ksft_print_msg("syscall failed (ret1:%ld output1:%ld ret2:%ld output2:%ld)\n",
  156. t_sys1, t2_sys1, t_sys2, t2_sys2);
  157. ksft_test_result_skip("vdso_time\n");
  158. ksft_test_result_skip("vdso_time\n");
  159. return;
  160. }
  161. if (vdso_time) {
  162. if (t_vdso < 0 || t_vdso != t2_vdso)
  163. ksft_test_result_fail("vDSO failed (ret:%ld output:%ld)\n",
  164. t_vdso, t2_vdso);
  165. else if (t_vdso < t_sys1 || t_vdso > t_sys2)
  166. ksft_test_result_fail("vDSO returned the wrong time (%ld %ld %ld)\n",
  167. t_sys1, t_vdso, t_sys2);
  168. else
  169. ksft_test_result_pass("vDSO time() is okay\n");
  170. } else {
  171. ksft_test_result_skip("vdso_time isn't set\n");
  172. }
  173. if (vsyscall_map_x) {
  174. if (t_vsys < 0 || t_vsys != t2_vsys)
  175. ksft_test_result_fail("vsyscall failed (ret:%ld output:%ld)\n",
  176. t_vsys, t2_vsys);
  177. else if (t_vsys < t_sys1 || t_vsys > t_sys2)
  178. ksft_test_result_fail("vsyscall returned the wrong time (%ld %ld %ld)\n",
  179. t_sys1, t_vsys, t_sys2);
  180. else
  181. ksft_test_result_pass("vsyscall time() is okay\n");
  182. } else {
  183. ksft_test_result_skip("vsyscall_map_x isn't set\n");
  184. }
  185. }
  186. static void test_getcpu(int cpu)
  187. {
  188. unsigned int cpu_sys, cpu_vdso, cpu_vsys, node_sys, node_vdso, node_vsys;
  189. long ret_sys, ret_vdso = -1, ret_vsys = -1;
  190. unsigned int node = 0;
  191. bool have_node = false;
  192. cpu_set_t cpuset;
  193. ksft_print_msg("getcpu() on CPU %d\n", cpu);
  194. CPU_ZERO(&cpuset);
  195. CPU_SET(cpu, &cpuset);
  196. if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
  197. ksft_print_msg("failed to force CPU %d\n", cpu);
  198. ksft_test_result_skip("vdso_getcpu\n");
  199. ksft_test_result_skip("vsyscall_map_x\n");
  200. return;
  201. }
  202. ret_sys = sys_getcpu(&cpu_sys, &node_sys, 0);
  203. if (vdso_getcpu)
  204. ret_vdso = vdso_getcpu(&cpu_vdso, &node_vdso, 0);
  205. if (vsyscall_map_x)
  206. ret_vsys = vgetcpu(&cpu_vsys, &node_vsys, 0);
  207. if (ret_sys == 0) {
  208. if (cpu_sys != cpu)
  209. ksft_print_msg("syscall reported CPU %u but should be %d\n",
  210. cpu_sys, cpu);
  211. have_node = true;
  212. node = node_sys;
  213. }
  214. if (vdso_getcpu) {
  215. if (ret_vdso) {
  216. ksft_test_result_fail("vDSO getcpu() failed\n");
  217. } else {
  218. if (!have_node) {
  219. have_node = true;
  220. node = node_vdso;
  221. }
  222. if (cpu_vdso != cpu || node_vdso != node) {
  223. if (cpu_vdso != cpu)
  224. ksft_print_msg("vDSO reported CPU %u but should be %d\n",
  225. cpu_vdso, cpu);
  226. if (node_vdso != node)
  227. ksft_print_msg("vDSO reported node %u but should be %u\n",
  228. node_vdso, node);
  229. ksft_test_result_fail("Wrong values\n");
  230. } else {
  231. ksft_test_result_pass("vDSO reported correct CPU and node\n");
  232. }
  233. }
  234. } else {
  235. ksft_test_result_skip("vdso_getcpu isn't set\n");
  236. }
  237. if (vsyscall_map_x) {
  238. if (ret_vsys) {
  239. ksft_test_result_fail("vsyscall getcpu() failed\n");
  240. } else {
  241. if (!have_node) {
  242. have_node = true;
  243. node = node_vsys;
  244. }
  245. if (cpu_vsys != cpu || node_vsys != node) {
  246. if (cpu_vsys != cpu)
  247. ksft_print_msg("vsyscall reported CPU %u but should be %d\n",
  248. cpu_vsys, cpu);
  249. if (node_vsys != node)
  250. ksft_print_msg("vsyscall reported node %u but should be %u\n",
  251. node_vsys, node);
  252. ksft_test_result_fail("Wrong values\n");
  253. } else {
  254. ksft_test_result_pass("vsyscall reported correct CPU and node\n");
  255. }
  256. }
  257. } else {
  258. ksft_test_result_skip("vsyscall_map_x isn't set\n");
  259. }
  260. }
  261. #ifdef __x86_64__
  262. static jmp_buf jmpbuf;
  263. static volatile unsigned long segv_err, segv_trapno;
  264. static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
  265. {
  266. ucontext_t *ctx = (ucontext_t *)ctx_void;
  267. segv_trapno = ctx->uc_mcontext.gregs[REG_TRAPNO];
  268. segv_err = ctx->uc_mcontext.gregs[REG_ERR];
  269. siglongjmp(jmpbuf, 1);
  270. }
  271. static void test_vsys_r(void)
  272. {
  273. ksft_print_msg("Checking read access to the vsyscall page\n");
  274. bool can_read;
  275. if (sigsetjmp(jmpbuf, 1) == 0) {
  276. *(volatile int *)0xffffffffff600000;
  277. can_read = true;
  278. } else {
  279. can_read = false;
  280. }
  281. if (can_read && !vsyscall_map_r)
  282. ksft_test_result_fail("We have read access, but we shouldn't\n");
  283. else if (!can_read && vsyscall_map_r)
  284. ksft_test_result_fail("We don't have read access, but we should\n");
  285. else if (can_read)
  286. ksft_test_result_pass("We have read access\n");
  287. else
  288. ksft_test_result_pass("We do not have read access (trap=%ld, error=0x%lx)\n",
  289. segv_trapno, segv_err);
  290. }
  291. static void test_vsys_x(void)
  292. {
  293. if (vsyscall_map_x) {
  294. /* We already tested this adequately. */
  295. ksft_test_result_pass("vsyscall_map_x is true\n");
  296. return;
  297. }
  298. ksft_print_msg("Make sure that vsyscalls really cause a fault\n");
  299. bool can_exec;
  300. if (sigsetjmp(jmpbuf, 1) == 0) {
  301. vgtod(NULL, NULL);
  302. can_exec = true;
  303. } else {
  304. can_exec = false;
  305. }
  306. if (can_exec)
  307. ksft_test_result_fail("Executing the vsyscall did not fault\n");
  308. /* #GP or #PF (with X86_PF_INSTR) */
  309. else if ((segv_trapno == 13) || ((segv_trapno == 14) && (segv_err & (1 << 4))))
  310. ksft_test_result_pass("Executing the vsyscall page failed (trap=%ld, error=0x%lx)\n",
  311. segv_trapno, segv_err);
  312. else
  313. ksft_test_result_fail("Execution failed with the wrong error (trap=%ld, error=0x%lx)\n",
  314. segv_trapno, segv_err);
  315. }
  316. /*
  317. * Debuggers expect ptrace() to be able to peek at the vsyscall page.
  318. * Use process_vm_readv() as a proxy for ptrace() to test this. We
  319. * want it to work in the vsyscall=emulate case and to fail in the
  320. * vsyscall=xonly case.
  321. *
  322. * It's worth noting that this ABI is a bit nutty. write(2) can't
  323. * read from the vsyscall page on any kernel version or mode. The
  324. * fact that ptrace() ever worked was a nice courtesy of old kernels,
  325. * but the code to support it is fairly gross.
  326. */
  327. static void test_process_vm_readv(void)
  328. {
  329. char buf[4096];
  330. struct iovec local, remote;
  331. int ret;
  332. ksft_print_msg("process_vm_readv() from vsyscall page\n");
  333. local.iov_base = buf;
  334. local.iov_len = 4096;
  335. remote.iov_base = (void *)0xffffffffff600000;
  336. remote.iov_len = 4096;
  337. ret = process_vm_readv(getpid(), &local, 1, &remote, 1, 0);
  338. if (ret != 4096) {
  339. /*
  340. * We expect process_vm_readv() to work if and only if the
  341. * vsyscall page is readable.
  342. */
  343. ksft_test_result(!vsyscall_map_r,
  344. "process_vm_readv() failed (ret = %d, errno = %d)\n", ret, errno);
  345. return;
  346. }
  347. if (vsyscall_map_r)
  348. ksft_test_result(!memcmp(buf, remote.iov_base, sizeof(buf)), "Read data\n");
  349. else
  350. ksft_test_result_fail("process_rm_readv() succeeded, but it should have failed in this configuration\n");
  351. }
  352. static void init_vsys(void)
  353. {
  354. int nerrs = 0;
  355. FILE *maps;
  356. char line[MAPS_LINE_LEN];
  357. bool found = false;
  358. maps = fopen("/proc/self/maps", "r");
  359. if (!maps) {
  360. ksft_test_result_skip("Could not open /proc/self/maps -- assuming vsyscall is r-x\n");
  361. vsyscall_map_r = true;
  362. return;
  363. }
  364. while (fgets(line, MAPS_LINE_LEN, maps)) {
  365. char r, x;
  366. void *start, *end;
  367. char name[MAPS_LINE_LEN];
  368. /* sscanf() is safe here as strlen(name) >= strlen(line) */
  369. if (sscanf(line, "%p-%p %c-%cp %*x %*x:%*x %*u %s",
  370. &start, &end, &r, &x, name) != 5)
  371. continue;
  372. if (strcmp(name, "[vsyscall]"))
  373. continue;
  374. ksft_print_msg("vsyscall map: %s", line);
  375. if (start != (void *)0xffffffffff600000 ||
  376. end != (void *)0xffffffffff601000) {
  377. ksft_print_msg("address range is nonsense\n");
  378. nerrs++;
  379. }
  380. ksft_print_msg("vsyscall permissions are %c-%c\n", r, x);
  381. vsyscall_map_r = (r == 'r');
  382. vsyscall_map_x = (x == 'x');
  383. found = true;
  384. break;
  385. }
  386. fclose(maps);
  387. if (!found) {
  388. ksft_print_msg("no vsyscall map in /proc/self/maps\n");
  389. vsyscall_map_r = false;
  390. vsyscall_map_x = false;
  391. }
  392. ksft_test_result(!nerrs, "vsyscall map\n");
  393. }
  394. static volatile sig_atomic_t num_vsyscall_traps;
  395. static void sigtrap(int sig, siginfo_t *info, void *ctx_void)
  396. {
  397. ucontext_t *ctx = (ucontext_t *)ctx_void;
  398. unsigned long ip = ctx->uc_mcontext.gregs[REG_RIP];
  399. if (((ip ^ 0xffffffffff600000UL) & ~0xfffUL) == 0)
  400. num_vsyscall_traps++;
  401. }
  402. static void test_emulation(void)
  403. {
  404. time_t tmp;
  405. bool is_native;
  406. if (!vsyscall_map_x) {
  407. ksft_test_result_skip("vsyscall_map_x isn't set\n");
  408. return;
  409. }
  410. ksft_print_msg("checking that vsyscalls are emulated\n");
  411. sethandler(SIGTRAP, sigtrap, 0);
  412. set_eflags(get_eflags() | X86_EFLAGS_TF);
  413. vtime(&tmp);
  414. set_eflags(get_eflags() & ~X86_EFLAGS_TF);
  415. /*
  416. * If vsyscalls are emulated, we expect a single trap in the
  417. * vsyscall page -- the call instruction will trap with RIP
  418. * pointing to the entry point before emulation takes over.
  419. * In native mode, we expect two traps, since whatever code
  420. * the vsyscall page contains will be more than just a ret
  421. * instruction.
  422. */
  423. is_native = (num_vsyscall_traps > 1);
  424. ksft_test_result(!is_native, "vsyscalls are %s (%d instructions in vsyscall page)\n",
  425. (is_native ? "native" : "emulated"), (int)num_vsyscall_traps);
  426. }
  427. #endif
  428. int main(int argc, char **argv)
  429. {
  430. int total_tests = TOTAL_TESTS;
  431. ksft_print_header();
  432. ksft_set_plan(total_tests);
  433. init_vdso();
  434. #ifdef __x86_64__
  435. init_vsys();
  436. #endif
  437. test_gtod();
  438. test_time();
  439. test_getcpu(0);
  440. test_getcpu(1);
  441. #ifdef __x86_64__
  442. sethandler(SIGSEGV, sigsegv, 0);
  443. test_vsys_r();
  444. test_vsys_x();
  445. test_process_vm_readv();
  446. test_emulation();
  447. #endif
  448. ksft_finished();
  449. }