sgx_exception.c 7.9 KB

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