test_syscall_vdso.c 9.5 KB

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