sig_handler.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2011-2018 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include "arch.h"
  32. #include "sgx_error.h"
  33. #include "tcs.h"
  34. #include "se_trace.h"
  35. #include "rts.h"
  36. #include "enclave.h"
  37. #include <assert.h>
  38. #include <signal.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. typedef struct _ecall_param_t
  42. {
  43. tcs_t *tcs;
  44. long fn; //long because we need register bandwith align on stack, refer to enter_enclave.h;
  45. void *ocall_table;
  46. void *ms;
  47. CTrustThread *trust_thread;
  48. } ecall_param_t;
  49. #ifdef __x86_64__
  50. #define REG_XIP REG_RIP
  51. #define REG_XAX REG_RAX
  52. #define REG_XBX REG_RBX
  53. #define REG_XSI REG_RSI
  54. #define REG_XBP REG_RBP
  55. /*
  56. * refer to enter_enclave.h
  57. * stack high address <-------------
  58. * |rip|rbp|rbx|r10|r13|r14|r15|r8|rcx|rdx|rsi|rdi|
  59. * ^ ^
  60. * | <-rbp | <-param4
  61. */
  62. #define ECALL_PARAM (reinterpret_cast<ecall_param_t*>(context->uc_mcontext.gregs[REG_RBP] - 10 * 8))
  63. #else
  64. #define REG_XIP REG_EIP
  65. #define REG_XAX REG_EAX
  66. #define REG_XBX REG_EBX
  67. #define REG_XSI REG_ESI
  68. #define REG_XBP REG_EBP
  69. /*
  70. * refer to enter_enclave.h
  71. * stack high address <-------------
  72. * |param4|param3|param2|param2|param0|eip|ebp|
  73. * ^
  74. * | <-ebp
  75. */
  76. #define ECALL_PARAM (reinterpret_cast<ecall_param_t*>(context->uc_mcontext.gregs[REG_EBP] + 2 * 4))
  77. #endif
  78. extern "C" void *get_aep();
  79. extern "C" void *get_eenterp();
  80. extern "C" void *get_eretp();
  81. static struct sigaction g_old_sigact[_NSIG];
  82. void reg_sig_handler();
  83. void sig_handler(int signum, siginfo_t* siginfo, void *priv)
  84. {
  85. SE_TRACE(SE_TRACE_DEBUG, "signal handler is triggered\n");
  86. ucontext_t* context = reinterpret_cast<ucontext_t *>(priv);
  87. unsigned int *xip = reinterpret_cast<unsigned int *>(context->uc_mcontext.gregs[REG_XIP]);
  88. size_t xax = context->uc_mcontext.gregs[REG_XAX];
  89. #ifndef NDEBUG
  90. /* `xbx' is only used in assertions. */
  91. size_t xbx = context->uc_mcontext.gregs[REG_XBX];
  92. #endif
  93. ecall_param_t *param = ECALL_PARAM;
  94. //the case of exception on ERESUME or within enclave.
  95. //We can't distinguish ERESUME exception from exception within enclave. We assume it is the exception within enclave.
  96. //If it is ERESUME exception, it will raise another exception in ecall and ecall will return error.
  97. if(xip == get_aep()
  98. && SE_ERESUME == xax)
  99. {
  100. assert(ENCLU == (*xip & 0xffffff));
  101. //suppose the exception is within enclave.
  102. SE_TRACE(SE_TRACE_NOTICE, "exception on ERESUME\n");
  103. //The ecall looks recursively, but it will not cause infinite call.
  104. //If exception is raised in trts again and again, the SSA will overflow, and finally it is EENTER exception.
  105. assert(reinterpret_cast<tcs_t *>(xbx) == param->tcs);
  106. CEnclave *enclave = param->trust_thread->get_enclave();
  107. unsigned int ret = enclave->ecall(ECMD_EXCEPT, param->ocall_table, NULL);
  108. if(SGX_SUCCESS == ret)
  109. {
  110. //ERESUME execute
  111. return;
  112. }
  113. //If the exception is caused by enclave lost or internal stack overrun, then return the error code to ecall caller elegantly.
  114. else if(SGX_ERROR_ENCLAVE_LOST == ret || SGX_ERROR_STACK_OVERRUN == ret)
  115. {
  116. //enter_enlcave function will return with ret which is from tRTS;
  117. context->uc_mcontext.gregs[REG_XIP] = reinterpret_cast<greg_t>(get_eretp());
  118. context->uc_mcontext.gregs[REG_XSI] = ret;
  119. return;
  120. }
  121. //If we can't fix the exception within enclave, then give the handle to other signal hanlder.
  122. //Call the previous signal handler. The default signal handler should terminate the application.
  123. enclave->rdunlock();
  124. CEnclavePool::instance()->unref_enclave(enclave);
  125. }
  126. //the case of exception on EENTER instruction.
  127. else if(xip == get_eenterp()
  128. && SE_EENTER == xax)
  129. {
  130. assert(reinterpret_cast<tcs_t *>(xbx) == param->tcs);
  131. assert(ENCLU == (*xip & 0xffffff));
  132. SE_TRACE(SE_TRACE_NOTICE, "exception on EENTER\n");
  133. //enter_enlcave function will return with SE_ERROR_ENCLAVE_LOST
  134. context->uc_mcontext.gregs[REG_XIP] = reinterpret_cast<greg_t>(get_eretp());
  135. context->uc_mcontext.gregs[REG_XSI] = SGX_ERROR_ENCLAVE_LOST;
  136. return;
  137. }
  138. SE_TRACE(SE_TRACE_DEBUG, "NOT enclave signal\n");
  139. //it is not SE exception. if the old signal handler is default signal handler, we reset signal handler.
  140. //raise the signal again, and the default signal handler will be called.
  141. if(SIG_DFL == g_old_sigact[signum].sa_handler)
  142. {
  143. signal(signum, SIG_DFL);
  144. raise(signum);
  145. }
  146. //if there is old signal handler, we need transfer the signal to the old signal handler;
  147. else
  148. {
  149. if(!(g_old_sigact[signum].sa_flags & SA_NODEFER))
  150. sigaddset(&g_old_sigact[signum].sa_mask, signum);
  151. sigset_t cur_set;
  152. pthread_sigmask(SIG_SETMASK, &g_old_sigact[signum].sa_mask, &cur_set);
  153. if(g_old_sigact[signum].sa_flags & SA_SIGINFO)
  154. {
  155. g_old_sigact[signum].sa_sigaction(signum, siginfo, priv);
  156. }
  157. else
  158. {
  159. g_old_sigact[signum].sa_handler(signum);
  160. }
  161. pthread_sigmask(SIG_SETMASK, &cur_set, NULL);
  162. //If the g_old_sigact set SA_RESETHAND, it will break the chain which means
  163. //g_old_sigact->next_old_sigact will not be called. Our signal handler does not
  164. //responsable for that. We just follow what os do on SA_RESETHAND.
  165. if(g_old_sigact[signum].sa_flags & SA_RESETHAND)
  166. g_old_sigact[signum].sa_handler = SIG_DFL;
  167. }
  168. }
  169. void reg_sig_handler()
  170. {
  171. int ret = 0;
  172. struct sigaction sig_act;
  173. SE_TRACE(SE_TRACE_DEBUG, "signal handler is registered\n");
  174. memset(&sig_act, 0, sizeof(sig_act));
  175. sig_act.sa_sigaction = sig_handler;
  176. sig_act.sa_flags = SA_SIGINFO | SA_NODEFER | SA_RESTART;
  177. sigemptyset(&sig_act.sa_mask);
  178. if(sigprocmask(SIG_SETMASK, NULL, &sig_act.sa_mask))
  179. {
  180. SE_TRACE(SE_TRACE_WARNING, "%s\n", strerror(errno));
  181. }
  182. else
  183. {
  184. sigdelset(&sig_act.sa_mask, SIGSEGV);
  185. sigdelset(&sig_act.sa_mask, SIGFPE);
  186. sigdelset(&sig_act.sa_mask, SIGILL);
  187. sigdelset(&sig_act.sa_mask, SIGBUS);
  188. sigdelset(&sig_act.sa_mask, SIGTRAP);
  189. }
  190. ret = sigaction(SIGSEGV, &sig_act, &g_old_sigact[SIGSEGV]);
  191. if (0 != ret) abort();
  192. ret = sigaction(SIGFPE, &sig_act, &g_old_sigact[SIGFPE]);
  193. if (0 != ret) abort();
  194. ret = sigaction(SIGILL, &sig_act, &g_old_sigact[SIGILL]);
  195. if (0 != ret) abort();
  196. ret = sigaction(SIGBUS, &sig_act, &g_old_sigact[SIGBUS]);
  197. if (0 != ret) abort();
  198. ret = sigaction(SIGTRAP, &sig_act, &g_old_sigact[SIGTRAP]);
  199. if (0 != ret) abort();
  200. }
  201. //trust_thread is saved at stack for ocall.
  202. #define enter_enclave __morestack
  203. extern "C" int enter_enclave(const tcs_t *tcs, const long fn, const void *ocall_table, const void *ms, CTrustThread *trust_thread);
  204. int do_ecall(const int fn, const void *ocall_table, const void *ms, CTrustThread *trust_thread)
  205. {
  206. int status = SGX_ERROR_UNEXPECTED;
  207. #ifdef SE_SIM
  208. CEnclave* enclave = trust_thread->get_enclave();
  209. //check if it is current pid, it is to simulate fork() scenario on HW
  210. sgx_enclave_id_t eid = enclave->get_enclave_id();
  211. if((pid_t)(eid >> 32) != getpid())
  212. return SGX_ERROR_ENCLAVE_LOST;
  213. #endif
  214. tcs_t *tcs = trust_thread->get_tcs();
  215. status = enter_enclave(tcs, fn, ocall_table, ms, trust_thread);
  216. return status;
  217. }
  218. int do_ocall(const bridge_fn_t bridge, void *ms)
  219. {
  220. int error = SGX_ERROR_UNEXPECTED;
  221. error = bridge(ms);
  222. return error;
  223. }