syscall_arg_fault.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * syscall_arg_fault.c - tests faults 32-bit fast syscall stack args
  3. * Copyright (c) 2015 Andrew Lutomirski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. */
  14. #define _GNU_SOURCE
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <sys/signal.h>
  19. #include <sys/ucontext.h>
  20. #include <err.h>
  21. #include <setjmp.h>
  22. #include <errno.h>
  23. /* Our sigaltstack scratch space. */
  24. static unsigned char altstack_data[SIGSTKSZ];
  25. static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
  26. int flags)
  27. {
  28. struct sigaction sa;
  29. memset(&sa, 0, sizeof(sa));
  30. sa.sa_sigaction = handler;
  31. sa.sa_flags = SA_SIGINFO | flags;
  32. sigemptyset(&sa.sa_mask);
  33. if (sigaction(sig, &sa, 0))
  34. err(1, "sigaction");
  35. }
  36. static volatile sig_atomic_t sig_traps;
  37. static sigjmp_buf jmpbuf;
  38. static volatile sig_atomic_t n_errs;
  39. static void sigsegv(int sig, siginfo_t *info, void *ctx_void)
  40. {
  41. ucontext_t *ctx = (ucontext_t*)ctx_void;
  42. if (ctx->uc_mcontext.gregs[REG_EAX] != -EFAULT) {
  43. printf("[FAIL]\tAX had the wrong value: 0x%x\n",
  44. ctx->uc_mcontext.gregs[REG_EAX]);
  45. n_errs++;
  46. } else {
  47. printf("[OK]\tSeems okay\n");
  48. }
  49. siglongjmp(jmpbuf, 1);
  50. }
  51. static void sigill(int sig, siginfo_t *info, void *ctx_void)
  52. {
  53. printf("[SKIP]\tIllegal instruction\n");
  54. siglongjmp(jmpbuf, 1);
  55. }
  56. int main()
  57. {
  58. stack_t stack = {
  59. .ss_sp = altstack_data,
  60. .ss_size = SIGSTKSZ,
  61. };
  62. if (sigaltstack(&stack, NULL) != 0)
  63. err(1, "sigaltstack");
  64. sethandler(SIGSEGV, sigsegv, SA_ONSTACK);
  65. sethandler(SIGILL, sigill, SA_ONSTACK);
  66. /*
  67. * Exercise another nasty special case. The 32-bit SYSCALL
  68. * and SYSENTER instructions (even in compat mode) each
  69. * clobber one register. A Linux system call has a syscall
  70. * number and six arguments, and the user stack pointer
  71. * needs to live in some register on return. That means
  72. * that we need eight registers, but SYSCALL and SYSENTER
  73. * only preserve seven registers. As a result, one argument
  74. * ends up on the stack. The stack is user memory, which
  75. * means that the kernel can fail to read it.
  76. *
  77. * The 32-bit fast system calls don't have a defined ABI:
  78. * we're supposed to invoke them through the vDSO. So we'll
  79. * fudge it: we set all regs to invalid pointer values and
  80. * invoke the entry instruction. The return will fail no
  81. * matter what, and we completely lose our program state,
  82. * but we can fix it up with a signal handler.
  83. */
  84. printf("[RUN]\tSYSENTER with invalid state\n");
  85. if (sigsetjmp(jmpbuf, 1) == 0) {
  86. asm volatile (
  87. "movl $-1, %%eax\n\t"
  88. "movl $-1, %%ebx\n\t"
  89. "movl $-1, %%ecx\n\t"
  90. "movl $-1, %%edx\n\t"
  91. "movl $-1, %%esi\n\t"
  92. "movl $-1, %%edi\n\t"
  93. "movl $-1, %%ebp\n\t"
  94. "movl $-1, %%esp\n\t"
  95. "sysenter"
  96. : : : "memory", "flags");
  97. }
  98. printf("[RUN]\tSYSCALL with invalid state\n");
  99. if (sigsetjmp(jmpbuf, 1) == 0) {
  100. asm volatile (
  101. "movl $-1, %%eax\n\t"
  102. "movl $-1, %%ebx\n\t"
  103. "movl $-1, %%ecx\n\t"
  104. "movl $-1, %%edx\n\t"
  105. "movl $-1, %%esi\n\t"
  106. "movl $-1, %%edi\n\t"
  107. "movl $-1, %%ebp\n\t"
  108. "movl $-1, %%esp\n\t"
  109. "syscall\n\t"
  110. "pushl $0" /* make sure we segfault cleanly */
  111. : : : "memory", "flags");
  112. }
  113. return 0;
  114. }