sgx_exception.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. __sigemptyset((__sigset_t *) &action.sa_mask);
  77. __sigaddset((__sigset_t *) &action.sa_mask, SIGCONT);
  78. for (int i = 0 ; i < nsig ; i++) {
  79. if (sigs[i] == SIGCHLD)
  80. action.sa_flags |= SA_NOCLDSTOP|SA_NOCLDWAIT;
  81. #if defined(__i386__)
  82. int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
  83. #else
  84. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i], &action, NULL,
  85. sizeof(sigset_t));
  86. #endif
  87. if (IS_ERR(ret))
  88. return -ERRNO(ret);
  89. action.sa_flags &= ~(SA_NOCLDSTOP|SA_NOCLDWAIT);
  90. }
  91. int ret = 0;
  92. __sigset_t mask;
  93. __sigemptyset(&mask);
  94. for (int i = 0 ; i < nsig ; i++)
  95. __sigaddset(&mask, sigs[i]);
  96. #if defined(__i386__)
  97. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  98. #else
  99. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  100. sizeof(sigset_t));
  101. #endif
  102. if (IS_ERR(ret))
  103. return -ERRNO(ret);
  104. return 0;
  105. }
  106. int block_signals (int * sigs, int nsig)
  107. {
  108. int ret = 0;
  109. __sigset_t mask;
  110. __sigemptyset(&mask);
  111. for (int i = 0 ; i < nsig ; i++)
  112. __sigaddset(&mask, sigs[i]);
  113. #if defined(__i386__)
  114. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_BLOCK, &mask, NULL)
  115. #else
  116. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_BLOCK, &mask, NULL,
  117. sizeof(sigset_t));
  118. #endif
  119. if (IS_ERR(ret))
  120. return -ERRNO(ret);
  121. return 0;
  122. }
  123. int unblock_signals (int * sigs, int nsig)
  124. {
  125. int ret = 0;
  126. __sigset_t mask;
  127. __sigemptyset(&mask);
  128. for (int i = 0 ; i < nsig ; i++)
  129. __sigaddset(&mask, sigs[i]);
  130. #if defined(__i386__)
  131. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  132. #else
  133. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  134. sizeof(sigset_t));
  135. #endif
  136. if (IS_ERR(ret))
  137. return -ERRNO(ret);
  138. return 0;
  139. }
  140. int unset_sighandler (int * sigs, int nsig)
  141. {
  142. for (int i = 0 ; i < nsig ; i++) {
  143. #if defined(__i386__)
  144. int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], SIG_DFL, NULL)
  145. #else
  146. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i],
  147. (struct sigaction *) SIG_DFL, NULL,
  148. sizeof(__sigset_t));
  149. #endif
  150. if (IS_ERR(ret))
  151. return -ERRNO(ret);
  152. }
  153. return 0;
  154. }
  155. static int get_event_num (int signum)
  156. {
  157. switch(signum) {
  158. case SIGFPE: return PAL_EVENT_ARITHMETIC_ERROR;
  159. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  160. case SIGILL: return PAL_EVENT_ILLEGAL;
  161. case SIGTERM: return PAL_EVENT_QUIT;
  162. case SIGINT: return PAL_EVENT_SUSPEND;
  163. case SIGCONT: return PAL_EVENT_RESUME;
  164. default: return -1;
  165. }
  166. }
  167. void sgx_entry_return (void);
  168. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  169. struct ucontext * uc)
  170. {
  171. __UNUSED(info);
  172. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  173. if (rip != (unsigned long) async_exit_pointer) {
  174. uc->uc_mcontext.gregs[REG_RIP] = (uint64_t) sgx_entry_return;
  175. uc->uc_mcontext.gregs[REG_RDI] = -PAL_ERROR_INTERRUPTED;
  176. uc->uc_mcontext.gregs[REG_RSI] = get_event_num(signum);
  177. } else {
  178. sgx_raise(get_event_num(signum));
  179. }
  180. }
  181. static void _DkResumeSighandler (int signum, siginfo_t * info,
  182. struct ucontext * uc)
  183. {
  184. __UNUSED(info);
  185. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  186. if (rip != (unsigned long) async_exit_pointer) {
  187. switch (signum) {
  188. case SIGSEGV:
  189. SGX_DBG(DBG_E, "Segmentation Fault in Untrusted Code (RIP = %08lx)\n", rip);
  190. break;
  191. case SIGILL:
  192. SGX_DBG(DBG_E, "Illegal Instruction in Untrusted Code (RIP = %08lx)\n", rip);
  193. break;
  194. case SIGFPE:
  195. SGX_DBG(DBG_E, "Arithmetic Exception in Untrusted Code (RIP = %08lx)\n", rip);
  196. break;
  197. case SIGBUS:
  198. SGX_DBG(DBG_E, "Memory Mapping Exception in Untrusted Code (RIP = %08lx)\n", rip);
  199. break;
  200. }
  201. INLINE_SYSCALL(exit, 1, 1);
  202. }
  203. int event = get_event_num(signum);
  204. sgx_raise(event);
  205. }
  206. int sgx_signal_setup (void)
  207. {
  208. int ret, sig[4];
  209. sig[0] = SIGTERM;
  210. sig[1] = SIGINT;
  211. sig[2] = SIGCONT;
  212. if ((ret = set_sighandler(sig, 3, &_DkTerminateSighandler)) < 0)
  213. goto err;
  214. sig[0] = SIGSEGV;
  215. sig[1] = SIGILL;
  216. sig[2] = SIGFPE;
  217. sig[3] = SIGBUS;
  218. if ((ret = set_sighandler(sig, 4, &_DkResumeSighandler)) < 0)
  219. goto err;
  220. return 0;
  221. err:
  222. return ret;
  223. }