test_syscall_vdso.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * 32-bit syscall ABI conformance test.
  4. *
  5. * Copyright (c) 2015 Denys Vlasenko
  6. */
  7. /*
  8. * Can be built statically:
  9. * gcc -Os -Wall -static -m32 test_syscall_vdso.c thunks_32.S
  10. */
  11. #undef _GNU_SOURCE
  12. #define _GNU_SOURCE 1
  13. #undef __USE_GNU
  14. #define __USE_GNU 1
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <signal.h>
  20. #include <sys/types.h>
  21. #include <sys/select.h>
  22. #include <sys/time.h>
  23. #include <elf.h>
  24. #include <sys/ptrace.h>
  25. #include <sys/wait.h>
  26. #if !defined(__i386__)
  27. int main(int argc, char **argv, char **envp)
  28. {
  29. printf("[SKIP]\tNot a 32-bit x86 userspace\n");
  30. return 0;
  31. }
  32. #else
  33. long syscall_addr;
  34. long get_syscall(char **envp)
  35. {
  36. Elf32_auxv_t *auxv;
  37. while (*envp++ != NULL)
  38. continue;
  39. for (auxv = (void *)envp; auxv->a_type != AT_NULL; auxv++)
  40. if (auxv->a_type == AT_SYSINFO)
  41. return auxv->a_un.a_val;
  42. printf("[WARN]\tAT_SYSINFO not supplied\n");
  43. return 0;
  44. }
  45. asm (
  46. " .pushsection .text\n"
  47. " .global int80\n"
  48. "int80:\n"
  49. " int $0x80\n"
  50. " ret\n"
  51. " .popsection\n"
  52. );
  53. extern char int80;
  54. struct regs64 {
  55. uint64_t rax, rbx, rcx, rdx;
  56. uint64_t rsi, rdi, rbp, rsp;
  57. uint64_t r8, r9, r10, r11;
  58. uint64_t r12, r13, r14, r15;
  59. };
  60. struct regs64 regs64;
  61. int kernel_is_64bit;
  62. asm (
  63. " .pushsection .text\n"
  64. " .code64\n"
  65. "get_regs64:\n"
  66. " push %rax\n"
  67. " mov $regs64, %eax\n"
  68. " pop 0*8(%rax)\n"
  69. " movq %rbx, 1*8(%rax)\n"
  70. " movq %rcx, 2*8(%rax)\n"
  71. " movq %rdx, 3*8(%rax)\n"
  72. " movq %rsi, 4*8(%rax)\n"
  73. " movq %rdi, 5*8(%rax)\n"
  74. " movq %rbp, 6*8(%rax)\n"
  75. " movq %rsp, 7*8(%rax)\n"
  76. " movq %r8, 8*8(%rax)\n"
  77. " movq %r9, 9*8(%rax)\n"
  78. " movq %r10, 10*8(%rax)\n"
  79. " movq %r11, 11*8(%rax)\n"
  80. " movq %r12, 12*8(%rax)\n"
  81. " movq %r13, 13*8(%rax)\n"
  82. " movq %r14, 14*8(%rax)\n"
  83. " movq %r15, 15*8(%rax)\n"
  84. " ret\n"
  85. "poison_regs64:\n"
  86. " movq $0x7f7f7f7f, %r8\n"
  87. " shl $32, %r8\n"
  88. " orq $0x7f7f7f7f, %r8\n"
  89. " movq %r8, %r9\n"
  90. " incq %r9\n"
  91. " movq %r9, %r10\n"
  92. " incq %r10\n"
  93. " movq %r10, %r11\n"
  94. " incq %r11\n"
  95. " movq %r11, %r12\n"
  96. " incq %r12\n"
  97. " movq %r12, %r13\n"
  98. " incq %r13\n"
  99. " movq %r13, %r14\n"
  100. " incq %r14\n"
  101. " movq %r14, %r15\n"
  102. " incq %r15\n"
  103. " ret\n"
  104. " .code32\n"
  105. " .popsection\n"
  106. );
  107. extern void get_regs64(void);
  108. extern void poison_regs64(void);
  109. extern unsigned long call64_from_32(void (*function)(void));
  110. void print_regs64(void)
  111. {
  112. if (!kernel_is_64bit)
  113. return;
  114. printf("ax:%016llx bx:%016llx cx:%016llx dx:%016llx\n", regs64.rax, regs64.rbx, regs64.rcx, regs64.rdx);
  115. printf("si:%016llx di:%016llx bp:%016llx sp:%016llx\n", regs64.rsi, regs64.rdi, regs64.rbp, regs64.rsp);
  116. printf(" 8:%016llx 9:%016llx 10:%016llx 11:%016llx\n", regs64.r8 , regs64.r9 , regs64.r10, regs64.r11);
  117. printf("12:%016llx 13:%016llx 14:%016llx 15:%016llx\n", regs64.r12, regs64.r13, regs64.r14, regs64.r15);
  118. }
  119. int check_regs64(void)
  120. {
  121. int err = 0;
  122. int num = 8;
  123. uint64_t *r64 = &regs64.r8;
  124. uint64_t expected = 0x7f7f7f7f7f7f7f7fULL;
  125. if (!kernel_is_64bit)
  126. return 0;
  127. do {
  128. if (*r64 == expected++)
  129. continue; /* register did not change */
  130. if (syscall_addr != (long)&int80) {
  131. /*
  132. * Non-INT80 syscall entrypoints are allowed to clobber R8+ regs:
  133. * either clear them to 0, or for R11, load EFLAGS.
  134. */
  135. if (*r64 == 0)
  136. continue;
  137. if (num == 11) {
  138. printf("[NOTE]\tR11 has changed:%016llx - assuming clobbered by SYSRET insn\n", *r64);
  139. continue;
  140. }
  141. } else {
  142. /*
  143. * INT80 syscall entrypoint can be used by
  144. * 64-bit programs too, unlike SYSCALL/SYSENTER.
  145. * Therefore it must preserve R12+
  146. * (they are callee-saved registers in 64-bit C ABI).
  147. *
  148. * Starting in Linux 4.17 (and any kernel that
  149. * backports the change), R8..11 are preserved.
  150. * Historically (and probably unintentionally), they
  151. * were clobbered or zeroed.
  152. */
  153. }
  154. printf("[FAIL]\tR%d has changed:%016llx\n", num, *r64);
  155. err++;
  156. } while (r64++, ++num < 16);
  157. if (!err)
  158. printf("[OK]\tR8..R15 did not leak kernel data\n");
  159. return err;
  160. }
  161. int nfds;
  162. fd_set rfds;
  163. fd_set wfds;
  164. fd_set efds;
  165. struct timespec timeout;
  166. sigset_t sigmask;
  167. struct {
  168. sigset_t *sp;
  169. int sz;
  170. } sigmask_desc;
  171. void prep_args()
  172. {
  173. nfds = 42;
  174. FD_ZERO(&rfds);
  175. FD_ZERO(&wfds);
  176. FD_ZERO(&efds);
  177. FD_SET(0, &rfds);
  178. FD_SET(1, &wfds);
  179. FD_SET(2, &efds);
  180. timeout.tv_sec = 0;
  181. timeout.tv_nsec = 123;
  182. sigemptyset(&sigmask);
  183. sigaddset(&sigmask, SIGINT);
  184. sigaddset(&sigmask, SIGUSR2);
  185. sigaddset(&sigmask, SIGRTMAX);
  186. sigmask_desc.sp = &sigmask;
  187. sigmask_desc.sz = 8; /* bytes */
  188. }
  189. static void print_flags(const char *name, unsigned long r)
  190. {
  191. static const char *bitarray[] = {
  192. "\n" ,"c\n" ,/* Carry Flag */
  193. "0 " ,"1 " ,/* Bit 1 - always on */
  194. "" ,"p " ,/* Parity Flag */
  195. "0 " ,"3? " ,
  196. "" ,"a " ,/* Auxiliary carry Flag */
  197. "0 " ,"5? " ,
  198. "" ,"z " ,/* Zero Flag */
  199. "" ,"s " ,/* Sign Flag */
  200. "" ,"t " ,/* Trap Flag */
  201. "" ,"i " ,/* Interrupt Flag */
  202. "" ,"d " ,/* Direction Flag */
  203. "" ,"o " ,/* Overflow Flag */
  204. "0 " ,"1 " ,/* I/O Privilege Level (2 bits) */
  205. "0" ,"1" ,/* I/O Privilege Level (2 bits) */
  206. "" ,"n " ,/* Nested Task */
  207. "0 " ,"15? ",
  208. "" ,"r " ,/* Resume Flag */
  209. "" ,"v " ,/* Virtual Mode */
  210. "" ,"ac " ,/* Alignment Check/Access Control */
  211. "" ,"vif ",/* Virtual Interrupt Flag */
  212. "" ,"vip ",/* Virtual Interrupt Pending */
  213. "" ,"id " ,/* CPUID detection */
  214. NULL
  215. };
  216. const char **bitstr;
  217. int bit;
  218. printf("%s=%016lx ", name, r);
  219. bitstr = bitarray + 42;
  220. bit = 21;
  221. if ((r >> 22) != 0)
  222. printf("(extra bits are set) ");
  223. do {
  224. if (bitstr[(r >> bit) & 1][0])
  225. fputs(bitstr[(r >> bit) & 1], stdout);
  226. bitstr -= 2;
  227. bit--;
  228. } while (bit >= 0);
  229. }
  230. int run_syscall(void)
  231. {
  232. long flags, bad_arg;
  233. prep_args();
  234. if (kernel_is_64bit)
  235. call64_from_32(poison_regs64);
  236. /*print_regs64();*/
  237. asm("\n"
  238. /* Try 6-arg syscall: pselect. It should return quickly */
  239. " push %%ebp\n"
  240. " mov $308, %%eax\n" /* PSELECT */
  241. " mov nfds, %%ebx\n" /* ebx arg1 */
  242. " mov $rfds, %%ecx\n" /* ecx arg2 */
  243. " mov $wfds, %%edx\n" /* edx arg3 */
  244. " mov $efds, %%esi\n" /* esi arg4 */
  245. " mov $timeout, %%edi\n" /* edi arg5 */
  246. " mov $sigmask_desc, %%ebp\n" /* %ebp arg6 */
  247. " push $0x200ed7\n" /* set almost all flags */
  248. " popf\n" /* except TF, IOPL, NT, RF, VM, AC, VIF, VIP */
  249. " call *syscall_addr\n"
  250. /* Check that registers are not clobbered */
  251. " pushf\n"
  252. " pop %%eax\n"
  253. " cld\n"
  254. " cmp nfds, %%ebx\n" /* ebx arg1 */
  255. " mov $1, %%ebx\n"
  256. " jne 1f\n"
  257. " cmp $rfds, %%ecx\n" /* ecx arg2 */
  258. " mov $2, %%ebx\n"
  259. " jne 1f\n"
  260. " cmp $wfds, %%edx\n" /* edx arg3 */
  261. " mov $3, %%ebx\n"
  262. " jne 1f\n"
  263. " cmp $efds, %%esi\n" /* esi arg4 */
  264. " mov $4, %%ebx\n"
  265. " jne 1f\n"
  266. " cmp $timeout, %%edi\n" /* edi arg5 */
  267. " mov $5, %%ebx\n"
  268. " jne 1f\n"
  269. " cmpl $sigmask_desc, %%ebp\n" /* %ebp arg6 */
  270. " mov $6, %%ebx\n"
  271. " jne 1f\n"
  272. " mov $0, %%ebx\n"
  273. "1:\n"
  274. " pop %%ebp\n"
  275. : "=a" (flags), "=b" (bad_arg)
  276. :
  277. : "cx", "dx", "si", "di"
  278. );
  279. if (kernel_is_64bit) {
  280. memset(&regs64, 0x77, sizeof(regs64));
  281. call64_from_32(get_regs64);
  282. /*print_regs64();*/
  283. }
  284. /*
  285. * On paravirt kernels, flags are not preserved across syscalls.
  286. * Thus, we do not consider it a bug if some are changed.
  287. * We just show ones which do.
  288. */
  289. if ((0x200ed7 ^ flags) != 0) {
  290. print_flags("[WARN]\tFlags before", 0x200ed7);
  291. print_flags("[WARN]\tFlags after", flags);
  292. print_flags("[WARN]\tFlags change", (0x200ed7 ^ flags));
  293. }
  294. if (bad_arg) {
  295. printf("[FAIL]\targ#%ld clobbered\n", bad_arg);
  296. return 1;
  297. }
  298. printf("[OK]\tArguments are preserved across syscall\n");
  299. return check_regs64();
  300. }
  301. int run_syscall_twice()
  302. {
  303. int exitcode = 0;
  304. long sv;
  305. if (syscall_addr) {
  306. printf("[RUN]\tExecuting 6-argument 32-bit syscall via VDSO\n");
  307. exitcode = run_syscall();
  308. }
  309. sv = syscall_addr;
  310. syscall_addr = (long)&int80;
  311. printf("[RUN]\tExecuting 6-argument 32-bit syscall via INT 80\n");
  312. exitcode += run_syscall();
  313. syscall_addr = sv;
  314. return exitcode;
  315. }
  316. void ptrace_me()
  317. {
  318. pid_t pid;
  319. fflush(NULL);
  320. pid = fork();
  321. if (pid < 0)
  322. exit(1);
  323. if (pid == 0) {
  324. /* child */
  325. if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) != 0)
  326. exit(0);
  327. raise(SIGSTOP);
  328. return;
  329. }
  330. /* parent */
  331. printf("[RUN]\tRunning tests under ptrace\n");
  332. while (1) {
  333. int status;
  334. pid = waitpid(-1, &status, __WALL);
  335. if (WIFEXITED(status))
  336. exit(WEXITSTATUS(status));
  337. if (WIFSIGNALED(status))
  338. exit(WTERMSIG(status));
  339. if (pid <= 0 || !WIFSTOPPED(status)) /* paranoia */
  340. exit(255);
  341. /*
  342. * Note: we do not inject sig = WSTOPSIG(status).
  343. * We probably should, but careful: do not inject SIGTRAP
  344. * generated by syscall entry/exit stops.
  345. * That kills the child.
  346. */
  347. ptrace(PTRACE_SYSCALL, pid, 0L, 0L /*sig*/);
  348. }
  349. }
  350. int main(int argc, char **argv, char **envp)
  351. {
  352. int exitcode = 0;
  353. int cs;
  354. asm("\n"
  355. " movl %%cs, %%eax\n"
  356. : "=a" (cs)
  357. );
  358. kernel_is_64bit = (cs == 0x23);
  359. if (!kernel_is_64bit)
  360. printf("[NOTE]\tNot a 64-bit kernel, won't test R8..R15 leaks\n");
  361. /* This only works for non-static builds:
  362. * syscall_addr = dlsym(dlopen("linux-gate.so.1", RTLD_NOW), "__kernel_vsyscall");
  363. */
  364. syscall_addr = get_syscall(envp);
  365. exitcode += run_syscall_twice();
  366. ptrace_me();
  367. exitcode += run_syscall_twice();
  368. return exitcode;
  369. }
  370. #endif