sgx_exception.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. void restore_rt (void) asm ("__restore_rt");
  50. #ifndef SA_RESTORER
  51. #define SA_RESTORER 0x04000000
  52. #endif
  53. #define DEFINE_RESTORE_RT(syscall) DEFINE_RESTORE_RT2(syscall)
  54. # define DEFINE_RESTORE_RT2(syscall) \
  55. asm ( \
  56. " nop\n" \
  57. ".align 16\n" \
  58. ".LSTART_restore_rt:\n" \
  59. " .type __restore_rt,@function\n" \
  60. "__restore_rt:\n" \
  61. " movq $" #syscall ", %rax\n" \
  62. " syscall\n");
  63. DEFINE_RESTORE_RT(__NR_rt_sigreturn)
  64. #endif
  65. int set_sighandler (int * sigs, int nsig, void * handler)
  66. {
  67. struct sigaction action;
  68. action.sa_handler = (void (*)(int)) handler;
  69. action.sa_flags = SA_SIGINFO;
  70. #if !defined(__i386__)
  71. action.sa_flags |= SA_RESTORER;
  72. action.sa_restorer = restore_rt;
  73. #endif
  74. __sigemptyset((__sigset_t *) &action.sa_mask);
  75. __sigaddset((__sigset_t *) &action.sa_mask, SIGCONT);
  76. for (int i = 0 ; i < nsig ; i++) {
  77. if (sigs[i] == SIGCHLD)
  78. action.sa_flags |= SA_NOCLDSTOP|SA_NOCLDWAIT;
  79. #if defined(__i386__)
  80. int ret = INLINE_SYSCALL(sigaction, 3, sigs[i], &action, NULL)
  81. #else
  82. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i], &action, NULL,
  83. sizeof(sigset_t));
  84. #endif
  85. if (IS_ERR(ret))
  86. return -ERRNO(ret);
  87. action.sa_flags &= ~(SA_NOCLDSTOP|SA_NOCLDWAIT);
  88. }
  89. int ret = 0;
  90. __sigset_t mask;
  91. __sigemptyset(&mask);
  92. for (int i = 0 ; i < nsig ; i++)
  93. __sigaddset(&mask, sigs[i]);
  94. #if defined(__i386__)
  95. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  96. #else
  97. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  98. sizeof(sigset_t));
  99. #endif
  100. if (IS_ERR(ret))
  101. return -ERRNO(ret);
  102. return 0;
  103. }
  104. int block_signals (int * sigs, int nsig)
  105. {
  106. int ret = 0;
  107. __sigset_t mask;
  108. __sigemptyset(&mask);
  109. for (int i = 0 ; i < nsig ; i++)
  110. __sigaddset(&mask, sigs[i]);
  111. #if defined(__i386__)
  112. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_BLOCK, &mask, NULL)
  113. #else
  114. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_BLOCK, &mask, NULL,
  115. sizeof(sigset_t));
  116. #endif
  117. if (IS_ERR(ret))
  118. return -ERRNO(ret);
  119. return 0;
  120. }
  121. int unblock_signals (int * sigs, int nsig)
  122. {
  123. int ret = 0;
  124. __sigset_t mask;
  125. __sigemptyset(&mask);
  126. for (int i = 0 ; i < nsig ; i++)
  127. __sigaddset(&mask, sigs[i]);
  128. #if defined(__i386__)
  129. ret = INLINE_SYSCALL(sigprocmask, 3, SIG_UNBLOCK, &mask, NULL)
  130. #else
  131. ret = INLINE_SYSCALL(rt_sigprocmask, 4, SIG_UNBLOCK, &mask, NULL,
  132. sizeof(sigset_t));
  133. #endif
  134. if (IS_ERR(ret))
  135. return -ERRNO(ret);
  136. return 0;
  137. }
  138. int unset_sighandler (int * sigs, int nsig)
  139. {
  140. for (int i = 0 ; i < nsig ; i++) {
  141. #if defined(__i386__)
  142. int ret = INLINE_SYSCALL(sigaction, 4, sigs[i], SIG_DFL, NULL)
  143. #else
  144. int ret = INLINE_SYSCALL(rt_sigaction, 4, sigs[i],
  145. (struct sigaction *) SIG_DFL, NULL,
  146. sizeof(__sigset_t));
  147. #endif
  148. if (IS_ERR(ret))
  149. return -ERRNO(ret);
  150. }
  151. return 0;
  152. }
  153. static int get_event_num (int signum)
  154. {
  155. switch(signum) {
  156. case SIGFPE: return PAL_EVENT_DIVZERO;
  157. case SIGSEGV: case SIGBUS: return PAL_EVENT_MEMFAULT;
  158. case SIGILL: return PAL_EVENT_ILLEGAL;
  159. case SIGTERM: return PAL_EVENT_QUIT;
  160. case SIGINT: return PAL_EVENT_SUSPEND;
  161. case SIGCONT: return PAL_EVENT_RESUME;
  162. default: return -1;
  163. }
  164. }
  165. void sgx_entry_return (void);
  166. static void _DkTerminateSighandler (int signum, siginfo_t * info,
  167. struct ucontext * uc)
  168. {
  169. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  170. #if SGX_HAS_FSGSBASE == 0
  171. if (rip != (unsigned long) async_exit_pointer &&
  172. rip != (unsigned long) double_async_exit) {
  173. #else
  174. if (rip != (unsigned long) async_exit_pointer) {
  175. #endif
  176. uc->uc_mcontext.gregs[REG_RIP] = (uint64_t) sgx_entry_return;
  177. uc->uc_mcontext.gregs[REG_RDI] = -PAL_ERROR_INTERRUPTED;
  178. uc->uc_mcontext.gregs[REG_RSI] = get_event_num(signum);
  179. } else {
  180. #if SGX_HAS_FSGSBASE != 0
  181. sgx_raise(get_event_num(signum));
  182. #else
  183. uc->uc_mcontext.gregs[REG_R9] = get_event_num(signum);
  184. #endif
  185. }
  186. }
  187. static void _DkResumeSighandler (int signum, siginfo_t * info,
  188. struct ucontext * uc)
  189. {
  190. unsigned long rip = uc->uc_mcontext.gregs[REG_RIP];
  191. #if SGX_HAS_FSGSBASE == 0
  192. if (rip != (unsigned long) async_exit_pointer &&
  193. rip != (unsigned long) double_async_exit) {
  194. #else
  195. if (rip != (unsigned long) async_exit_pointer) {
  196. #endif
  197. switch (signum) {
  198. case SIGSEGV:
  199. SGX_DBG(DBG_E, "Segmentation Fault in Untrusted Code (RIP = %08lx)\n", rip);
  200. break;
  201. case SIGILL:
  202. SGX_DBG(DBG_E, "Illegal Instruction in Untrusted Code (RIP = %08lx)\n", rip);
  203. break;
  204. case SIGFPE:
  205. SGX_DBG(DBG_E, "Arithmetic Exception in Untrusted Code (RIP = %08lx)\n", rip);
  206. break;
  207. case SIGBUS:
  208. SGX_DBG(DBG_E, "Memory Mapping Exception in Untrusted Code (RIP = %08lx)\n", rip);
  209. break;
  210. }
  211. INLINE_SYSCALL(exit, 1, 1);
  212. }
  213. int event = 0;
  214. switch(signum) {
  215. case SIGBUS:
  216. case SIGSEGV:
  217. event = PAL_EVENT_MEMFAULT;
  218. break;
  219. case SIGILL:
  220. event = PAL_EVENT_ILLEGAL;
  221. break;
  222. }
  223. #if SGX_HAS_FSGSBASE != 0
  224. sgx_raise(event);
  225. #else
  226. uc->uc_mcontext.gregs[REG_R9] = event;
  227. #endif
  228. }
  229. int sgx_signal_setup (void)
  230. {
  231. int ret, sig[4];
  232. sig[0] = SIGTERM;
  233. sig[1] = SIGINT;
  234. sig[2] = SIGCONT;
  235. if ((ret = set_sighandler(sig, 3, &_DkTerminateSighandler)) < 0)
  236. goto err;
  237. sig[0] = SIGSEGV;
  238. sig[1] = SIGILL;
  239. sig[2] = SIGFPE;
  240. sig[3] = SIGBUS;
  241. if ((ret = set_sighandler(sig, 4, &_DkResumeSighandler)) < 0)
  242. goto err;
  243. return 0;
  244. err:
  245. return ret;
  246. }