test_syscall_vdso.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. " movq %r8, %r10\n"
  99. " movq %r8, %r11\n"
  100. " movq %r8, %r12\n"
  101. " movq %r8, %r13\n"
  102. " movq %r8, %r14\n"
  103. " movq %r8, %r15\n"
  104. " ret\n"
  105. " .code32\n"
  106. " .popsection\n"
  107. );
  108. extern void get_regs64(void);
  109. extern void poison_regs64(void);
  110. extern unsigned long call64_from_32(void (*function)(void));
  111. void print_regs64(void)
  112. {
  113. if (!kernel_is_64bit)
  114. return;
  115. printf("ax:%016llx bx:%016llx cx:%016llx dx:%016llx\n", regs64.rax, regs64.rbx, regs64.rcx, regs64.rdx);
  116. printf("si:%016llx di:%016llx bp:%016llx sp:%016llx\n", regs64.rsi, regs64.rdi, regs64.rbp, regs64.rsp);
  117. printf(" 8:%016llx 9:%016llx 10:%016llx 11:%016llx\n", regs64.r8 , regs64.r9 , regs64.r10, regs64.r11);
  118. printf("12:%016llx 13:%016llx 14:%016llx 15:%016llx\n", regs64.r12, regs64.r13, regs64.r14, regs64.r15);
  119. }
  120. int check_regs64(void)
  121. {
  122. int err = 0;
  123. int num = 8;
  124. uint64_t *r64 = &regs64.r8;
  125. if (!kernel_is_64bit)
  126. return 0;
  127. do {
  128. if (*r64 == 0x7f7f7f7f7f7f7f7fULL)
  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. /* INT80 syscall entrypoint can be used by
  143. * 64-bit programs too, unlike SYSCALL/SYSENTER.
  144. * Therefore it must preserve R12+
  145. * (they are callee-saved registers in 64-bit C ABI).
  146. *
  147. * This was probably historically not intended,
  148. * but R8..11 are clobbered (cleared to 0).
  149. * IOW: they are the only registers which aren't
  150. * preserved across INT80 syscall.
  151. */
  152. if (*r64 == 0 && num <= 11)
  153. continue;
  154. }
  155. printf("[FAIL]\tR%d has changed:%016llx\n", num, *r64);
  156. err++;
  157. } while (r64++, ++num < 16);
  158. if (!err)
  159. printf("[OK]\tR8..R15 did not leak kernel data\n");
  160. return err;
  161. }
  162. int nfds;
  163. fd_set rfds;
  164. fd_set wfds;
  165. fd_set efds;
  166. struct timespec timeout;
  167. sigset_t sigmask;
  168. struct {
  169. sigset_t *sp;
  170. int sz;
  171. } sigmask_desc;
  172. void prep_args()
  173. {
  174. nfds = 42;
  175. FD_ZERO(&rfds);
  176. FD_ZERO(&wfds);
  177. FD_ZERO(&efds);
  178. FD_SET(0, &rfds);
  179. FD_SET(1, &wfds);
  180. FD_SET(2, &efds);
  181. timeout.tv_sec = 0;
  182. timeout.tv_nsec = 123;
  183. sigemptyset(&sigmask);
  184. sigaddset(&sigmask, SIGINT);
  185. sigaddset(&sigmask, SIGUSR2);
  186. sigaddset(&sigmask, SIGRTMAX);
  187. sigmask_desc.sp = &sigmask;
  188. sigmask_desc.sz = 8; /* bytes */
  189. }
  190. static void print_flags(const char *name, unsigned long r)
  191. {
  192. static const char *bitarray[] = {
  193. "\n" ,"c\n" ,/* Carry Flag */
  194. "0 " ,"1 " ,/* Bit 1 - always on */
  195. "" ,"p " ,/* Parity Flag */
  196. "0 " ,"3? " ,
  197. "" ,"a " ,/* Auxiliary carry Flag */
  198. "0 " ,"5? " ,
  199. "" ,"z " ,/* Zero Flag */
  200. "" ,"s " ,/* Sign Flag */
  201. "" ,"t " ,/* Trap Flag */
  202. "" ,"i " ,/* Interrupt Flag */
  203. "" ,"d " ,/* Direction Flag */
  204. "" ,"o " ,/* Overflow Flag */
  205. "0 " ,"1 " ,/* I/O Privilege Level (2 bits) */
  206. "0" ,"1" ,/* I/O Privilege Level (2 bits) */
  207. "" ,"n " ,/* Nested Task */
  208. "0 " ,"15? ",
  209. "" ,"r " ,/* Resume Flag */
  210. "" ,"v " ,/* Virtual Mode */
  211. "" ,"ac " ,/* Alignment Check/Access Control */
  212. "" ,"vif ",/* Virtual Interrupt Flag */
  213. "" ,"vip ",/* Virtual Interrupt Pending */
  214. "" ,"id " ,/* CPUID detection */
  215. NULL
  216. };
  217. const char **bitstr;
  218. int bit;
  219. printf("%s=%016lx ", name, r);
  220. bitstr = bitarray + 42;
  221. bit = 21;
  222. if ((r >> 22) != 0)
  223. printf("(extra bits are set) ");
  224. do {
  225. if (bitstr[(r >> bit) & 1][0])
  226. fputs(bitstr[(r >> bit) & 1], stdout);
  227. bitstr -= 2;
  228. bit--;
  229. } while (bit >= 0);
  230. }
  231. int run_syscall(void)
  232. {
  233. long flags, bad_arg;
  234. prep_args();
  235. if (kernel_is_64bit)
  236. call64_from_32(poison_regs64);
  237. /*print_regs64();*/
  238. asm("\n"
  239. /* Try 6-arg syscall: pselect. It should return quickly */
  240. " push %%ebp\n"
  241. " mov $308, %%eax\n" /* PSELECT */
  242. " mov nfds, %%ebx\n" /* ebx arg1 */
  243. " mov $rfds, %%ecx\n" /* ecx arg2 */
  244. " mov $wfds, %%edx\n" /* edx arg3 */
  245. " mov $efds, %%esi\n" /* esi arg4 */
  246. " mov $timeout, %%edi\n" /* edi arg5 */
  247. " mov $sigmask_desc, %%ebp\n" /* %ebp arg6 */
  248. " push $0x200ed7\n" /* set almost all flags */
  249. " popf\n" /* except TF, IOPL, NT, RF, VM, AC, VIF, VIP */
  250. " call *syscall_addr\n"
  251. /* Check that registers are not clobbered */
  252. " pushf\n"
  253. " pop %%eax\n"
  254. " cld\n"
  255. " cmp nfds, %%ebx\n" /* ebx arg1 */
  256. " mov $1, %%ebx\n"
  257. " jne 1f\n"
  258. " cmp $rfds, %%ecx\n" /* ecx arg2 */
  259. " mov $2, %%ebx\n"
  260. " jne 1f\n"
  261. " cmp $wfds, %%edx\n" /* edx arg3 */
  262. " mov $3, %%ebx\n"
  263. " jne 1f\n"
  264. " cmp $efds, %%esi\n" /* esi arg4 */
  265. " mov $4, %%ebx\n"
  266. " jne 1f\n"
  267. " cmp $timeout, %%edi\n" /* edi arg5 */
  268. " mov $5, %%ebx\n"
  269. " jne 1f\n"
  270. " cmpl $sigmask_desc, %%ebp\n" /* %ebp arg6 */
  271. " mov $6, %%ebx\n"
  272. " jne 1f\n"
  273. " mov $0, %%ebx\n"
  274. "1:\n"
  275. " pop %%ebp\n"
  276. : "=a" (flags), "=b" (bad_arg)
  277. :
  278. : "cx", "dx", "si", "di"
  279. );
  280. if (kernel_is_64bit) {
  281. memset(&regs64, 0x77, sizeof(regs64));
  282. call64_from_32(get_regs64);
  283. /*print_regs64();*/
  284. }
  285. /*
  286. * On paravirt kernels, flags are not preserved across syscalls.
  287. * Thus, we do not consider it a bug if some are changed.
  288. * We just show ones which do.
  289. */
  290. if ((0x200ed7 ^ flags) != 0) {
  291. print_flags("[WARN]\tFlags before", 0x200ed7);
  292. print_flags("[WARN]\tFlags after", flags);
  293. print_flags("[WARN]\tFlags change", (0x200ed7 ^ flags));
  294. }
  295. if (bad_arg) {
  296. printf("[FAIL]\targ#%ld clobbered\n", bad_arg);
  297. return 1;
  298. }
  299. printf("[OK]\tArguments are preserved across syscall\n");
  300. return check_regs64();
  301. }
  302. int run_syscall_twice()
  303. {
  304. int exitcode = 0;
  305. long sv;
  306. if (syscall_addr) {
  307. printf("[RUN]\tExecuting 6-argument 32-bit syscall via VDSO\n");
  308. exitcode = run_syscall();
  309. }
  310. sv = syscall_addr;
  311. syscall_addr = (long)&int80;
  312. printf("[RUN]\tExecuting 6-argument 32-bit syscall via INT 80\n");
  313. exitcode += run_syscall();
  314. syscall_addr = sv;
  315. return exitcode;
  316. }
  317. void ptrace_me()
  318. {
  319. pid_t pid;
  320. fflush(NULL);
  321. pid = fork();
  322. if (pid < 0)
  323. exit(1);
  324. if (pid == 0) {
  325. /* child */
  326. if (ptrace(PTRACE_TRACEME, 0L, 0L, 0L) != 0)
  327. exit(0);
  328. raise(SIGSTOP);
  329. return;
  330. }
  331. /* parent */
  332. printf("[RUN]\tRunning tests under ptrace\n");
  333. while (1) {
  334. int status;
  335. pid = waitpid(-1, &status, __WALL);
  336. if (WIFEXITED(status))
  337. exit(WEXITSTATUS(status));
  338. if (WIFSIGNALED(status))
  339. exit(WTERMSIG(status));
  340. if (pid <= 0 || !WIFSTOPPED(status)) /* paranoia */
  341. exit(255);
  342. /*
  343. * Note: we do not inject sig = WSTOPSIG(status).
  344. * We probably should, but careful: do not inject SIGTRAP
  345. * generated by syscall entry/exit stops.
  346. * That kills the child.
  347. */
  348. ptrace(PTRACE_SYSCALL, pid, 0L, 0L /*sig*/);
  349. }
  350. }
  351. int main(int argc, char **argv, char **envp)
  352. {
  353. int exitcode = 0;
  354. int cs;
  355. asm("\n"
  356. " movl %%cs, %%eax\n"
  357. : "=a" (cs)
  358. );
  359. kernel_is_64bit = (cs == 0x23);
  360. if (!kernel_is_64bit)
  361. printf("[NOTE]\tNot a 64-bit kernel, won't test R8..R15 leaks\n");
  362. /* This only works for non-static builds:
  363. * syscall_addr = dlsym(dlopen("linux-gate.so.1", RTLD_NOW), "__kernel_vsyscall");
  364. */
  365. syscall_addr = get_syscall(envp);
  366. exitcode += run_syscall_twice();
  367. ptrace_me();
  368. exitcode += run_syscall_twice();
  369. return exitcode;
  370. }
  371. #endif