syscall_numbering.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * syscall_numbering.c - test calling the x86-64 kernel with various
  4. * valid and invalid system call numbers.
  5. *
  6. * Copyright (c) 2018 Andrew Lutomirski
  7. */
  8. #define _GNU_SOURCE
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <stdbool.h>
  12. #include <errno.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <fcntl.h>
  16. #include <limits.h>
  17. #include <signal.h>
  18. #include <sysexits.h>
  19. #include <sys/ptrace.h>
  20. #include <sys/user.h>
  21. #include <sys/wait.h>
  22. #include <sys/mman.h>
  23. #include <linux/ptrace.h>
  24. /* Common system call numbers */
  25. #define SYS_READ 0
  26. #define SYS_WRITE 1
  27. #define SYS_GETPID 39
  28. /* x64-only system call numbers */
  29. #define X64_IOCTL 16
  30. #define X64_READV 19
  31. #define X64_WRITEV 20
  32. /* x32-only system call numbers (without X32_BIT) */
  33. #define X32_IOCTL 514
  34. #define X32_READV 515
  35. #define X32_WRITEV 516
  36. #define X32_BIT 0x40000000
  37. static int nullfd = -1; /* File descriptor for /dev/null */
  38. static bool with_x32; /* x32 supported on this kernel? */
  39. enum ptrace_pass {
  40. PTP_NOTHING,
  41. PTP_GETREGS,
  42. PTP_WRITEBACK,
  43. PTP_FUZZRET,
  44. PTP_FUZZHIGH,
  45. PTP_INTNUM,
  46. PTP_DONE
  47. };
  48. static const char * const ptrace_pass_name[] =
  49. {
  50. [PTP_NOTHING] = "just stop, no data read",
  51. [PTP_GETREGS] = "only getregs",
  52. [PTP_WRITEBACK] = "getregs, unmodified setregs",
  53. [PTP_FUZZRET] = "modifying the default return",
  54. [PTP_FUZZHIGH] = "clobbering the top 32 bits",
  55. [PTP_INTNUM] = "sign-extending the syscall number",
  56. };
  57. /*
  58. * Shared memory block between tracer and test
  59. */
  60. struct shared {
  61. unsigned int nerr; /* Total error count */
  62. unsigned int indent; /* Message indentation level */
  63. enum ptrace_pass ptrace_pass;
  64. bool probing_syscall; /* In probe_syscall() */
  65. };
  66. static volatile struct shared *sh;
  67. static inline unsigned int offset(void)
  68. {
  69. unsigned int level = sh ? sh->indent : 0;
  70. return 8 + level * 4;
  71. }
  72. #define msg(lvl, fmt, ...) printf("%-*s" fmt, offset(), "[" #lvl "]", \
  73. ## __VA_ARGS__)
  74. #define run(fmt, ...) msg(RUN, fmt, ## __VA_ARGS__)
  75. #define info(fmt, ...) msg(INFO, fmt, ## __VA_ARGS__)
  76. #define ok(fmt, ...) msg(OK, fmt, ## __VA_ARGS__)
  77. #define fail(fmt, ...) \
  78. do { \
  79. msg(FAIL, fmt, ## __VA_ARGS__); \
  80. sh->nerr++; \
  81. } while (0)
  82. #define crit(fmt, ...) \
  83. do { \
  84. sh->indent = 0; \
  85. msg(FAIL, fmt, ## __VA_ARGS__); \
  86. msg(SKIP, "Unable to run test\n"); \
  87. exit(EX_OSERR); \
  88. } while (0)
  89. /* Sentinel for ptrace-modified return value */
  90. #define MODIFIED_BY_PTRACE -9999
  91. /*
  92. * Directly invokes the given syscall with nullfd as the first argument
  93. * and the rest zero. Avoids involving glibc wrappers in case they ever
  94. * end up intercepting some system calls for some reason, or modify
  95. * the system call number itself.
  96. */
  97. static long long probe_syscall(int msb, int lsb)
  98. {
  99. register long long arg1 asm("rdi") = nullfd;
  100. register long long arg2 asm("rsi") = 0;
  101. register long long arg3 asm("rdx") = 0;
  102. register long long arg4 asm("r10") = 0;
  103. register long long arg5 asm("r8") = 0;
  104. register long long arg6 asm("r9") = 0;
  105. long long nr = ((long long)msb << 32) | (unsigned int)lsb;
  106. long long ret;
  107. /*
  108. * We pass in an extra copy of the extended system call number
  109. * in %rbx, so we can examine it from the ptrace handler without
  110. * worrying about it being possibly modified. This is to test
  111. * the validity of struct user regs.orig_rax a.k.a.
  112. * struct pt_regs.orig_ax.
  113. */
  114. sh->probing_syscall = true;
  115. asm volatile("syscall"
  116. : "=a" (ret)
  117. : "a" (nr), "b" (nr),
  118. "r" (arg1), "r" (arg2), "r" (arg3),
  119. "r" (arg4), "r" (arg5), "r" (arg6)
  120. : "rcx", "r11", "memory", "cc");
  121. sh->probing_syscall = false;
  122. return ret;
  123. }
  124. static const char *syscall_str(int msb, int start, int end)
  125. {
  126. static char buf[64];
  127. const char * const type = (start & X32_BIT) ? "x32" : "x64";
  128. int lsb = start;
  129. /*
  130. * Improve readability by stripping the x32 bit, but round
  131. * toward zero so we don't display -1 as -1073741825.
  132. */
  133. if (lsb < 0)
  134. lsb |= X32_BIT;
  135. else
  136. lsb &= ~X32_BIT;
  137. if (start == end)
  138. snprintf(buf, sizeof buf, "%s syscall %d:%d",
  139. type, msb, lsb);
  140. else
  141. snprintf(buf, sizeof buf, "%s syscalls %d:%d..%d",
  142. type, msb, lsb, lsb + (end-start));
  143. return buf;
  144. }
  145. static unsigned int _check_for(int msb, int start, int end, long long expect,
  146. const char *expect_str)
  147. {
  148. unsigned int err = 0;
  149. sh->indent++;
  150. if (start != end)
  151. sh->indent++;
  152. for (int nr = start; nr <= end; nr++) {
  153. long long ret = probe_syscall(msb, nr);
  154. if (ret != expect) {
  155. fail("%s returned %lld, but it should have returned %s\n",
  156. syscall_str(msb, nr, nr),
  157. ret, expect_str);
  158. err++;
  159. }
  160. }
  161. if (start != end)
  162. sh->indent--;
  163. if (err) {
  164. if (start != end)
  165. fail("%s had %u failure%s\n",
  166. syscall_str(msb, start, end),
  167. err, err == 1 ? "s" : "");
  168. } else {
  169. ok("%s returned %s as expected\n",
  170. syscall_str(msb, start, end), expect_str);
  171. }
  172. sh->indent--;
  173. return err;
  174. }
  175. #define check_for(msb,start,end,expect) \
  176. _check_for(msb,start,end,expect,#expect)
  177. static bool check_zero(int msb, int nr)
  178. {
  179. return check_for(msb, nr, nr, 0);
  180. }
  181. static bool check_enosys(int msb, int nr)
  182. {
  183. return check_for(msb, nr, nr, -ENOSYS);
  184. }
  185. /*
  186. * Anyone diagnosing a failure will want to know whether the kernel
  187. * supports x32. Tell them. This can also be used to conditionalize
  188. * tests based on existence or nonexistence of x32.
  189. */
  190. static bool test_x32(void)
  191. {
  192. long long ret;
  193. pid_t mypid = getpid();
  194. run("Checking for x32 by calling x32 getpid()\n");
  195. ret = probe_syscall(0, SYS_GETPID | X32_BIT);
  196. sh->indent++;
  197. if (ret == mypid) {
  198. info("x32 is supported\n");
  199. with_x32 = true;
  200. } else if (ret == -ENOSYS) {
  201. info("x32 is not supported\n");
  202. with_x32 = false;
  203. } else {
  204. fail("x32 getpid() returned %lld, but it should have returned either %lld or -ENOSYS\n", ret, (long long)mypid);
  205. with_x32 = false;
  206. }
  207. sh->indent--;
  208. return with_x32;
  209. }
  210. static void test_syscalls_common(int msb)
  211. {
  212. enum ptrace_pass pass = sh->ptrace_pass;
  213. run("Checking some common syscalls as 64 bit\n");
  214. check_zero(msb, SYS_READ);
  215. check_zero(msb, SYS_WRITE);
  216. run("Checking some 64-bit only syscalls as 64 bit\n");
  217. check_zero(msb, X64_READV);
  218. check_zero(msb, X64_WRITEV);
  219. run("Checking out of range system calls\n");
  220. check_for(msb, -64, -2, -ENOSYS);
  221. if (pass >= PTP_FUZZRET)
  222. check_for(msb, -1, -1, MODIFIED_BY_PTRACE);
  223. else
  224. check_for(msb, -1, -1, -ENOSYS);
  225. check_for(msb, X32_BIT-64, X32_BIT-1, -ENOSYS);
  226. check_for(msb, -64-X32_BIT, -1-X32_BIT, -ENOSYS);
  227. check_for(msb, INT_MAX-64, INT_MAX-1, -ENOSYS);
  228. }
  229. static void test_syscalls_with_x32(int msb)
  230. {
  231. /*
  232. * Syscalls 512-547 are "x32" syscalls. They are
  233. * intended to be called with the x32 (0x40000000) bit
  234. * set. Calling them without the x32 bit set is
  235. * nonsense and should not work.
  236. */
  237. run("Checking x32 syscalls as 64 bit\n");
  238. check_for(msb, 512, 547, -ENOSYS);
  239. run("Checking some common syscalls as x32\n");
  240. check_zero(msb, SYS_READ | X32_BIT);
  241. check_zero(msb, SYS_WRITE | X32_BIT);
  242. run("Checking some x32 syscalls as x32\n");
  243. check_zero(msb, X32_READV | X32_BIT);
  244. check_zero(msb, X32_WRITEV | X32_BIT);
  245. run("Checking some 64-bit syscalls as x32\n");
  246. check_enosys(msb, X64_IOCTL | X32_BIT);
  247. check_enosys(msb, X64_READV | X32_BIT);
  248. check_enosys(msb, X64_WRITEV | X32_BIT);
  249. }
  250. static void test_syscalls_without_x32(int msb)
  251. {
  252. run("Checking for absence of x32 system calls\n");
  253. check_for(msb, 0 | X32_BIT, 999 | X32_BIT, -ENOSYS);
  254. }
  255. static void test_syscall_numbering(void)
  256. {
  257. static const int msbs[] = {
  258. 0, 1, -1, X32_BIT-1, X32_BIT, X32_BIT-1, -X32_BIT, INT_MAX,
  259. INT_MIN, INT_MIN+1
  260. };
  261. sh->indent++;
  262. /*
  263. * The MSB is supposed to be ignored, so we loop over a few
  264. * to test that out.
  265. */
  266. for (size_t i = 0; i < sizeof(msbs)/sizeof(msbs[0]); i++) {
  267. int msb = msbs[i];
  268. run("Checking system calls with msb = %d (0x%x)\n",
  269. msb, msb);
  270. sh->indent++;
  271. test_syscalls_common(msb);
  272. if (with_x32)
  273. test_syscalls_with_x32(msb);
  274. else
  275. test_syscalls_without_x32(msb);
  276. sh->indent--;
  277. }
  278. sh->indent--;
  279. }
  280. static void syscall_numbering_tracee(void)
  281. {
  282. enum ptrace_pass pass;
  283. if (ptrace(PTRACE_TRACEME, 0, 0, 0)) {
  284. crit("Failed to request tracing\n");
  285. return;
  286. }
  287. raise(SIGSTOP);
  288. for (sh->ptrace_pass = pass = PTP_NOTHING; pass < PTP_DONE;
  289. sh->ptrace_pass = ++pass) {
  290. run("Running tests under ptrace: %s\n", ptrace_pass_name[pass]);
  291. test_syscall_numbering();
  292. }
  293. }
  294. static void mess_with_syscall(pid_t testpid, enum ptrace_pass pass)
  295. {
  296. struct user_regs_struct regs;
  297. sh->probing_syscall = false; /* Do this on entry only */
  298. /* For these, don't even getregs */
  299. if (pass == PTP_NOTHING || pass == PTP_DONE)
  300. return;
  301. ptrace(PTRACE_GETREGS, testpid, NULL, &regs);
  302. if (regs.orig_rax != regs.rbx) {
  303. fail("orig_rax %#llx doesn't match syscall number %#llx\n",
  304. (unsigned long long)regs.orig_rax,
  305. (unsigned long long)regs.rbx);
  306. }
  307. switch (pass) {
  308. case PTP_GETREGS:
  309. /* Just read, no writeback */
  310. return;
  311. case PTP_WRITEBACK:
  312. /* Write back the same register state verbatim */
  313. break;
  314. case PTP_FUZZRET:
  315. regs.rax = MODIFIED_BY_PTRACE;
  316. break;
  317. case PTP_FUZZHIGH:
  318. regs.rax = MODIFIED_BY_PTRACE;
  319. regs.orig_rax = regs.orig_rax | 0xffffffff00000000ULL;
  320. break;
  321. case PTP_INTNUM:
  322. regs.rax = MODIFIED_BY_PTRACE;
  323. regs.orig_rax = (int)regs.orig_rax;
  324. break;
  325. default:
  326. crit("invalid ptrace_pass\n");
  327. break;
  328. }
  329. ptrace(PTRACE_SETREGS, testpid, NULL, &regs);
  330. }
  331. static void syscall_numbering_tracer(pid_t testpid)
  332. {
  333. int wstatus;
  334. do {
  335. pid_t wpid = waitpid(testpid, &wstatus, 0);
  336. if (wpid < 0 && errno != EINTR)
  337. break;
  338. if (wpid != testpid)
  339. continue;
  340. if (!WIFSTOPPED(wstatus))
  341. break; /* Thread exited? */
  342. if (sh->probing_syscall && WSTOPSIG(wstatus) == SIGTRAP)
  343. mess_with_syscall(testpid, sh->ptrace_pass);
  344. } while (sh->ptrace_pass != PTP_DONE &&
  345. !ptrace(PTRACE_SYSCALL, testpid, NULL, NULL));
  346. ptrace(PTRACE_DETACH, testpid, NULL, NULL);
  347. /* Wait for the child process to terminate */
  348. while (waitpid(testpid, &wstatus, 0) != testpid || !WIFEXITED(wstatus))
  349. /* wait some more */;
  350. }
  351. static void test_traced_syscall_numbering(void)
  352. {
  353. pid_t testpid;
  354. /* Launch the test thread; this thread continues as the tracer thread */
  355. testpid = fork();
  356. if (testpid < 0) {
  357. crit("Unable to launch tracer process\n");
  358. } else if (testpid == 0) {
  359. syscall_numbering_tracee();
  360. _exit(0);
  361. } else {
  362. syscall_numbering_tracer(testpid);
  363. }
  364. }
  365. int main(void)
  366. {
  367. unsigned int nerr;
  368. /*
  369. * It is quite likely to get a segfault on a failure, so make
  370. * sure the message gets out by setting stdout to nonbuffered.
  371. */
  372. setvbuf(stdout, NULL, _IONBF, 0);
  373. /*
  374. * Harmless file descriptor to work on...
  375. */
  376. nullfd = open("/dev/null", O_RDWR);
  377. if (nullfd < 0) {
  378. crit("Unable to open /dev/null: %s\n", strerror(errno));
  379. }
  380. /*
  381. * Set up a block of shared memory...
  382. */
  383. sh = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_READ|PROT_WRITE,
  384. MAP_ANONYMOUS|MAP_SHARED, 0, 0);
  385. if (sh == MAP_FAILED) {
  386. crit("Unable to allocated shared memory block: %s\n",
  387. strerror(errno));
  388. }
  389. with_x32 = test_x32();
  390. run("Running tests without ptrace...\n");
  391. test_syscall_numbering();
  392. test_traced_syscall_numbering();
  393. nerr = sh->nerr;
  394. if (!nerr) {
  395. ok("All system calls succeeded or failed as expected\n");
  396. return 0;
  397. } else {
  398. fail("A total of %u system call%s had incorrect behavior\n",
  399. nerr, nerr != 1 ? "s" : "");
  400. return 1;
  401. }
  402. }