sgx_exception.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_signal.c
  15. *
  16. * This file contains APIs to set up handlers of exceptions issued by the
  17. * host, and the methods to pass the exceptions to the upcalls.
  18. */
  19. #include "pal_linux.h"
  20. #include "api.h"
  21. #include "ecall_types.h"
  22. #include "ocall_types.h"
  23. #include "sgx_internal.h"
  24. #include "pal_linux.h"
  25. #include <atomic.h>
  26. #include <sigset.h>
  27. #include <linux/signal.h>
  28. #include <ucontext.h>
  29. #include <asm/errno.h>
  30. #include "sgx_enclave.h"
  31. #if !defined(__i386__)
  32. /* In x86_64 kernels, sigaction is required to have a user-defined
  33. * restorer. Also, they not yet support SA_INFO. The reference:
  34. * http://lxr.linux.no/linux+v2.6.35/arch/x86/kernel/signal.c#L448
  35. *
  36. * / * x86-64 should always use SA_RESTORER. * /
  37. * if (ka->sa.sa_flags & SA_RESTORER) {
  38. * put_user_ex(ka->sa.sa_restorer, &frame->pretcode);
  39. * } else {
  40. * / * could use a vstub here * /
  41. * err |= -EFAULT;
  42. * }
  43. */
  44. #ifndef SA_RESTORER
  45. #define SA_RESTORER 0x04000000
  46. #endif
  47. #define DEFINE_RESTORE_RT(syscall) DEFINE_RESTORE_RT2(syscall)
  48. #define DEFINE_RESTORE_RT2(syscall) \
  49. __asm__ ( \
  50. " nop\n" \
  51. ".align 16\n" \
  52. ".LSTART_restore_rt:\n" \
  53. " .type __restore_rt,@function\n" \
  54. "__restore_rt:\n" \
  55. " movq $" #syscall ", %rax\n" \
  56. " syscall\n");
  57. DEFINE_RESTORE_RT(__NR_rt_sigreturn)
  58. /* Workaround for fixing an old GAS (2.27) bug that incorrectly
  59. * omits relocations when referencing this symbol */
  60. __attribute__((visibility("hidden"))) void __restore_rt(void);
  61. #endif
  62. static const int async_signals[] =
  63. {
  64. SIGTERM,
  65. SIGINT,
  66. SIGCONT,
  67. };
  68. static const int nasync_signals = ARRAY_SIZE(async_signals);
  69. int set_sighandler (int * sigs, int nsig, void * handler)
  70. {
  71. struct sigaction action;
  72. action.sa_handler = (void (*)(int)) handler;
  73. action.sa_flags = SA_SIGINFO;
  74. #if !defined(__i386__)
  75. action.sa_flags |= SA_RESTORER;
  76. action.sa_restorer = __restore_rt;
  77. #endif
  78. /* Disallow nested asynchronous signals during enclave exception handling.
  79. */
  80. __sigemptyset((__sigset_t *) &action.sa_mask);
  81. for (int i = 0; i < nasync_signals; i++)
  82. __sigaddset((__sigset_t *) &action.sa_mask, async_signals[i]);
  83. for (int i = 0 ; i < nsig ; i++) {
  84. if (sigs[i] == SIGCHLD)
  85. action.sa_flags |= SA_NOCLDSTOP|SA_NOCLDWAIT;
  86. #if defined(__i386__)
  87. int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
  88. #else
  89. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i], &action, NULL,
  90. sizeof(sigset_t));
  91. #endif
  92. if (IS_ERR(ret))
  93. return -ERRNO(ret);
  94. action.sa_flags &= ~(SA_NOCLDSTOP|SA_NOCLDWAIT);
  95. }
  96. int ret = 0;
  97. __sigset_t mask;
  98. __sigemptyset(&mask);
  99. for (int i = 0 ; i < nsig ; i++)
  100. __sigaddset(&mask, sigs[i]);
  101. #if defined(__i386__)
  102. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  103. #else
  104. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  105. sizeof(sigset_t));
  106. #endif
  107. if (IS_ERR(ret))
  108. return -ERRNO(ret);
  109. return 0;
  110. }
  111. int block_signals (bool block, const int * sigs, int nsig)
  112. {
  113. int how = block? SIG_BLOCK: SIG_UNBLOCK;
  114. int ret = 0;
  115. __sigset_t mask;
  116. __sigemptyset(&mask);
  117. for (int i = 0 ; i < nsig ; i++)
  118. __sigaddset(&mask, sigs[i]);
  119. #if defined(__i386__)
  120. ret = INLINE_SYSCALL(sigprocmask, 3, how, &mask, NULL)
  121. #else
  122. ret = INLINE_SYSCALL(rt_sigprocmask, 4, how, &mask, NULL,
  123. sizeof(sigset_t));
  124. #endif
  125. if (IS_ERR(ret))
  126. return -ERRNO(ret);
  127. return 0;
  128. }
  129. int block_async_signals (bool block)
  130. {
  131. return block_signals(block, async_signals, nasync_signals);
  132. }
  133. static int get_event_num (int signum)
  134. {
  135. switch(signum) {
  136. case SIGFPE: return PAL_EVENT_ARITHMETIC_ERROR;
  137. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  138. case SIGILL: return PAL_EVENT_ILLEGAL;
  139. case SIGTERM: return PAL_EVENT_QUIT;
  140. case SIGINT: return PAL_EVENT_SUSPEND;
  141. case SIGCONT: return PAL_EVENT_RESUME;
  142. default: return -1;
  143. }
  144. }
  145. void sgx_entry_return (void);
  146. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  147. struct ucontext * uc)
  148. {
  149. __UNUSED(info);
  150. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  151. if (rip != (unsigned long) async_exit_pointer) {
  152. uc->uc_mcontext.gregs[REG_RIP] = (uint64_t) sgx_entry_return;
  153. uc->uc_mcontext.gregs[REG_RDI] = -EINTR;
  154. uc->uc_mcontext.gregs[REG_RSI] = get_event_num(signum);
  155. } else {
  156. sgx_raise(get_event_num(signum));
  157. }
  158. }
  159. static void _DkResumeSighandler (int signum, siginfo_t * info,
  160. struct ucontext * uc)
  161. {
  162. __UNUSED(info);
  163. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  164. if (rip != (unsigned long) async_exit_pointer) {
  165. switch (signum) {
  166. case SIGSEGV:
  167. SGX_DBG(DBG_E, "Segmentation Fault in Untrusted Code (RIP = %08lx)\n", rip);
  168. break;
  169. case SIGILL:
  170. SGX_DBG(DBG_E, "Illegal Instruction in Untrusted Code (RIP = %08lx)\n", rip);
  171. break;
  172. case SIGFPE:
  173. SGX_DBG(DBG_E, "Arithmetic Exception in Untrusted Code (RIP = %08lx)\n", rip);
  174. break;
  175. case SIGBUS:
  176. SGX_DBG(DBG_E, "Memory Mapping Exception in Untrusted Code (RIP = %08lx)\n", rip);
  177. break;
  178. }
  179. INLINE_SYSCALL(exit, 1, 1);
  180. }
  181. int event = get_event_num(signum);
  182. sgx_raise(event);
  183. }
  184. int sgx_signal_setup (void)
  185. {
  186. int ret, sig[4];
  187. sig[0] = SIGTERM;
  188. sig[1] = SIGINT;
  189. sig[2] = SIGCONT;
  190. if ((ret = set_sighandler(sig, 3, &_DkTerminateSighandler)) < 0)
  191. goto err;
  192. sig[0] = SIGSEGV;
  193. sig[1] = SIGILL;
  194. sig[2] = SIGFPE;
  195. sig[3] = SIGBUS;
  196. if ((ret = set_sighandler(sig, 4, &_DkResumeSighandler)) < 0)
  197. goto err;
  198. return 0;
  199. err:
  200. return ret;
  201. }