sgx_exception.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. int unset_sighandler (int * sigs, int nsig)
  134. {
  135. for (int i = 0 ; i < nsig ; i++) {
  136. #if defined(__i386__)
  137. int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], SIG_DFL, NULL)
  138. #else
  139. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i],
  140. (struct sigaction *) SIG_DFL, NULL,
  141. sizeof(__sigset_t));
  142. #endif
  143. if (IS_ERR(ret))
  144. return -ERRNO(ret);
  145. }
  146. return 0;
  147. }
  148. static int get_event_num (int signum)
  149. {
  150. switch(signum) {
  151. case SIGFPE: return PAL_EVENT_ARITHMETIC_ERROR;
  152. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  153. case SIGILL: return PAL_EVENT_ILLEGAL;
  154. case SIGTERM: return PAL_EVENT_QUIT;
  155. case SIGINT: return PAL_EVENT_SUSPEND;
  156. case SIGCONT: return PAL_EVENT_RESUME;
  157. default: return -1;
  158. }
  159. }
  160. void sgx_entry_return (void);
  161. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  162. struct ucontext * uc)
  163. {
  164. __UNUSED(info);
  165. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  166. if (rip != (unsigned long) async_exit_pointer) {
  167. uc->uc_mcontext.gregs[REG_RIP] = (uint64_t) sgx_entry_return;
  168. uc->uc_mcontext.gregs[REG_RDI] = -EINTR;
  169. uc->uc_mcontext.gregs[REG_RSI] = get_event_num(signum);
  170. } else {
  171. sgx_raise(get_event_num(signum));
  172. }
  173. }
  174. static void _DkResumeSighandler (int signum, siginfo_t * info,
  175. struct ucontext * uc)
  176. {
  177. __UNUSED(info);
  178. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  179. if (rip != (unsigned long) async_exit_pointer) {
  180. switch (signum) {
  181. case SIGSEGV:
  182. SGX_DBG(DBG_E, "Segmentation Fault in Untrusted Code (RIP = %08lx)\n", rip);
  183. break;
  184. case SIGILL:
  185. SGX_DBG(DBG_E, "Illegal Instruction in Untrusted Code (RIP = %08lx)\n", rip);
  186. break;
  187. case SIGFPE:
  188. SGX_DBG(DBG_E, "Arithmetic Exception in Untrusted Code (RIP = %08lx)\n", rip);
  189. break;
  190. case SIGBUS:
  191. SGX_DBG(DBG_E, "Memory Mapping Exception in Untrusted Code (RIP = %08lx)\n", rip);
  192. break;
  193. }
  194. INLINE_SYSCALL(exit, 1, 1);
  195. }
  196. int event = get_event_num(signum);
  197. sgx_raise(event);
  198. }
  199. int sgx_signal_setup (void)
  200. {
  201. int ret, sig[4];
  202. sig[0] = SIGTERM;
  203. sig[1] = SIGINT;
  204. sig[2] = SIGCONT;
  205. if ((ret = set_sighandler(sig, 3, &_DkTerminateSighandler)) < 0)
  206. goto err;
  207. sig[0] = SIGSEGV;
  208. sig[1] = SIGILL;
  209. sig[2] = SIGFPE;
  210. sig[3] = SIGBUS;
  211. if ((ret = set_sighandler(sig, 4, &_DkResumeSighandler)) < 0)
  212. goto err;
  213. return 0;
  214. err:
  215. return ret;
  216. }