sig_handler.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (C) 2011-2016 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 "xsave.h"
  36. #include "rts.h"
  37. #include "enclave.h"
  38. #include <assert.h>
  39. #include <signal.h>
  40. #include <string.h>
  41. #include <errno.h>
  42. typedef struct _ecall_param_t
  43. {
  44. tcs_t *tcs;
  45. long fn; //long because we need register bandwith align on stack, refer to enter_enclave.h;
  46. void *ocall_table;
  47. void *ms;
  48. CTrustThread *trust_thread;
  49. } ecall_param_t;
  50. #ifdef __x86_64__
  51. #define REG_XIP REG_RIP
  52. #define REG_XAX REG_RAX
  53. #define REG_XBX REG_RBX
  54. #define REG_XSI REG_RSI
  55. #define REG_XBP REG_RBP
  56. /*
  57. * refer to enter_enclave.h
  58. * stack high address <-------------
  59. * |rip|rbp|rbx|r10|r13|r14|r15|r8|rcx|rdx|rsi|rdi|
  60. * ^ ^
  61. * | <-rbp | <-param4
  62. */
  63. #define ECALL_PARAM (reinterpret_cast<ecall_param_t*>(context->uc_mcontext.gregs[REG_RBP] - 10 * 8))
  64. #else
  65. #define REG_XIP REG_EIP
  66. #define REG_XAX REG_EAX
  67. #define REG_XBX REG_EBX
  68. #define REG_XSI REG_ESI
  69. #define REG_XBP REG_EBP
  70. /*
  71. * refer to enter_enclave.h
  72. * stack high address <-------------
  73. * |param4|param3|param2|param2|param0|eip|ebp|
  74. * ^
  75. * | <-ebp
  76. */
  77. #define ECALL_PARAM (reinterpret_cast<ecall_param_t*>(context->uc_mcontext.gregs[REG_EBP] + 2 * 4))
  78. #endif
  79. extern "C" void *get_aep();
  80. extern "C" void *get_eenterp();
  81. extern "C" void *get_eretp();
  82. static struct sigaction g_old_sigact[_NSIG];
  83. void reg_sig_handler();
  84. void sig_handler(int signum, siginfo_t* siginfo, void *priv)
  85. {
  86. SE_TRACE(SE_TRACE_DEBUG, "signal handler is triggered\n");
  87. ucontext_t* context = reinterpret_cast<ucontext_t *>(priv);
  88. unsigned int *xip = reinterpret_cast<unsigned int *>(context->uc_mcontext.gregs[REG_XIP]);
  89. size_t xax = context->uc_mcontext.gregs[REG_XAX];
  90. #ifndef NDEBUG
  91. /* `xbx' is only used in assertions. */
  92. size_t xbx = context->uc_mcontext.gregs[REG_XBX];
  93. #endif
  94. ecall_param_t *param = ECALL_PARAM;
  95. //the case of exception on ERESUME or within enclave.
  96. //We can't distinguish ERESUME exception from exception within enclave. We assume it is the exception within enclave.
  97. //If it is ERESUME exception, it will raise another exception in ecall and ecall will return error.
  98. if(xip == get_aep()
  99. && SE_ERESUME == xax)
  100. {
  101. assert(ENCLU == (*xip & 0xffffff));
  102. //suppose the exception is within enclave.
  103. SE_TRACE(SE_TRACE_NOTICE, "exception on ERESUME\n");
  104. //The ecall looks recursively, but it will not cause infinite call.
  105. //If exception is raised in trts again and again, the SSA will overflow, and finally it is EENTER exception.
  106. assert(reinterpret_cast<tcs_t *>(xbx) == param->tcs);
  107. CEnclave *enclave = param->trust_thread->get_enclave();
  108. unsigned int ret = enclave->ecall(ECMD_EXCEPT, param->ocall_table, NULL);
  109. if(SGX_SUCCESS == ret)
  110. {
  111. //ERESUME execute
  112. return;
  113. }
  114. //If the exception is caused by enclave lost or internal stack overrun, then return the error code to ecall caller elegantly.
  115. else if(SGX_ERROR_ENCLAVE_LOST == ret || SGX_ERROR_STACK_OVERRUN == ret)
  116. {
  117. //enter_enlcave function will return with ret which is from tRTS;
  118. context->uc_mcontext.gregs[REG_XIP] = reinterpret_cast<greg_t>(get_eretp());
  119. context->uc_mcontext.gregs[REG_XSI] = ret;
  120. return;
  121. }
  122. //If we can't fix the exception within enclave, then give the handle to other signal hanlder.
  123. //Call the previous signal handler. The default signal handler should terminate the application.
  124. enclave->rdunlock();
  125. CEnclavePool::instance()->unref_enclave(enclave);
  126. }
  127. //the case of exception on EENTER instruction.
  128. else if(xip == get_eenterp()
  129. && SE_EENTER == xax)
  130. {
  131. assert(reinterpret_cast<tcs_t *>(xbx) == param->tcs);
  132. assert(ENCLU == (*xip & 0xffffff));
  133. SE_TRACE(SE_TRACE_NOTICE, "exception on EENTER\n");
  134. //enter_enlcave function will return with SE_ERROR_ENCLAVE_LOST
  135. context->uc_mcontext.gregs[REG_XIP] = reinterpret_cast<greg_t>(get_eretp());
  136. context->uc_mcontext.gregs[REG_XSI] = SGX_ERROR_ENCLAVE_LOST;
  137. return;
  138. }
  139. SE_TRACE(SE_TRACE_DEBUG, "NOT enclave signal\n");
  140. //it is not SE exception. if the old signal handler is default signal handler, we reset signal handler.
  141. //raise the signal again, and the default signal handler will be called.
  142. if(SIG_DFL == g_old_sigact[signum].sa_handler)
  143. {
  144. signal(signum, SIG_DFL);
  145. raise(signum);
  146. }
  147. //if there is old signal handler, we need transfer the signal to the old signal handler;
  148. else
  149. {
  150. if(!(g_old_sigact[signum].sa_flags & SA_NODEFER))
  151. sigaddset(&g_old_sigact[signum].sa_mask, signum);
  152. sigset_t cur_set;
  153. pthread_sigmask(SIG_SETMASK, &g_old_sigact[signum].sa_mask, &cur_set);
  154. if(g_old_sigact[signum].sa_flags & SA_SIGINFO)
  155. {
  156. g_old_sigact[signum].sa_sigaction(signum, siginfo, priv);
  157. }
  158. else
  159. {
  160. g_old_sigact[signum].sa_handler(signum);
  161. }
  162. pthread_sigmask(SIG_SETMASK, &cur_set, NULL);
  163. //If the g_old_sigact set SA_RESETHAND, it will break the chain which means
  164. //g_old_sigact->next_old_sigact will not be called. Our signal handler does not
  165. //responsable for that. We just follow what os do on SA_RESETHAND.
  166. if(g_old_sigact[signum].sa_flags & SA_RESETHAND)
  167. g_old_sigact[signum].sa_handler = SIG_DFL;
  168. }
  169. }
  170. void reg_sig_handler()
  171. {
  172. int ret = 0;
  173. struct sigaction sig_act;
  174. SE_TRACE(SE_TRACE_DEBUG, "signal handler is registered\n");
  175. memset(&sig_act, 0, sizeof(sig_act));
  176. sig_act.sa_sigaction = sig_handler;
  177. sig_act.sa_flags = SA_SIGINFO | SA_NODEFER | SA_RESTART;
  178. sigemptyset(&sig_act.sa_mask);
  179. if(sigprocmask(SIG_SETMASK, NULL, &sig_act.sa_mask))
  180. {
  181. SE_TRACE(SE_TRACE_WARNING, "%s\n", strerror(errno));
  182. }
  183. else
  184. {
  185. sigdelset(&sig_act.sa_mask, SIGSEGV);
  186. sigdelset(&sig_act.sa_mask, SIGFPE);
  187. sigdelset(&sig_act.sa_mask, SIGILL);
  188. sigdelset(&sig_act.sa_mask, SIGBUS);
  189. sigdelset(&sig_act.sa_mask, SIGTRAP);
  190. }
  191. ret = sigaction(SIGSEGV, &sig_act, &g_old_sigact[SIGSEGV]);
  192. if (0 != ret) abort();
  193. ret = sigaction(SIGFPE, &sig_act, &g_old_sigact[SIGFPE]);
  194. if (0 != ret) abort();
  195. ret = sigaction(SIGILL, &sig_act, &g_old_sigact[SIGILL]);
  196. if (0 != ret) abort();
  197. ret = sigaction(SIGBUS, &sig_act, &g_old_sigact[SIGBUS]);
  198. if (0 != ret) abort();
  199. ret = sigaction(SIGTRAP, &sig_act, &g_old_sigact[SIGTRAP]);
  200. if (0 != ret) abort();
  201. }
  202. //trust_thread is saved at stack for ocall.
  203. #define enter_enclave __morestack
  204. extern "C" int enter_enclave(const tcs_t *tcs, const long fn, const void *ocall_table, const void *ms, CTrustThread *trust_thread);
  205. int do_ecall(const int fn, const void *ocall_table, const void *ms, CTrustThread *trust_thread)
  206. {
  207. int status = SGX_ERROR_UNEXPECTED;
  208. #ifdef SE_SIM
  209. CEnclave* enclave = trust_thread->get_enclave();
  210. //check if it is current pid, it is to simulate fork() scenario on HW
  211. sgx_enclave_id_t eid = enclave->get_enclave_id();
  212. if((pid_t)(eid >> 32) != getpid())
  213. return SGX_ERROR_ENCLAVE_LOST;
  214. #endif
  215. tcs_t *tcs = trust_thread->get_tcs();
  216. //seh_handler.cpp have the same code to save and restore pf register.
  217. //put the save register code here, because we want remind maintainer we should do it near EENTER
  218. uint8_t buffer[FXSAVE_SIZE];
  219. save_and_clean_xfeature_regs(buffer);
  220. status = enter_enclave(tcs, fn, ocall_table, ms, trust_thread);
  221. restore_xfeature_regs(buffer);
  222. return status;
  223. }
  224. int do_ocall(const bridge_fn_t bridge, void *ms)
  225. {
  226. int error = SGX_ERROR_UNEXPECTED;
  227. error = bridge(ms);
  228. save_and_clean_xfeature_regs(NULL);
  229. return error;
  230. }