syscall_numbering.c 11 KB

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