trts_veh.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Copyright (C) 2011-2017 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. /**
  32. * File: trts_veh.cpp
  33. * Description:
  34. * This file implements the support of custom exception handling.
  35. */
  36. #include "sgx_trts_exception.h"
  37. #include <stdlib.h>
  38. #include "sgx_trts.h"
  39. #include "xsave.h"
  40. #include "arch.h"
  41. #include "sgx_spinlock.h"
  42. #include "thread_data.h"
  43. #include "global_data.h"
  44. #include "trts_internal.h"
  45. #include "trts_util.h"
  46. typedef struct _handler_node_t
  47. {
  48. uintptr_t callback;
  49. struct _handler_node_t *next;
  50. } handler_node_t;
  51. static handler_node_t *g_first_node = NULL;
  52. static sgx_spinlock_t g_handler_lock = SGX_SPINLOCK_INITIALIZER;
  53. static uintptr_t g_veh_cookie = 0;
  54. #define ENC_VEH_POINTER(x) (uintptr_t)(x) ^ g_veh_cookie
  55. #define DEC_VEH_POINTER(x) (sgx_exception_handler_t)((x) ^ g_veh_cookie)
  56. // sgx_register_exception_handler()
  57. // register a custom exception handler
  58. // Parameter
  59. // is_first_handler - the order in which the handler should be called.
  60. // if the parameter is nonzero, the handler is the first handler to be called.
  61. // if the parameter is zero, the handler is the last handler to be called.
  62. // exception_handler - a pointer to the handler to be called.
  63. // Return Value
  64. // handler - success
  65. // NULL - fail
  66. void *sgx_register_exception_handler(int is_first_handler, sgx_exception_handler_t exception_handler)
  67. {
  68. // initialize g_veh_cookie for the first time sgx_register_exception_handler is called.
  69. if(unlikely(g_veh_cookie == 0))
  70. {
  71. uintptr_t rand = 0;
  72. do
  73. {
  74. if(SGX_SUCCESS != sgx_read_rand((unsigned char *)&rand, sizeof(rand)))
  75. {
  76. return NULL;
  77. }
  78. } while(rand == 0);
  79. sgx_spin_lock(&g_handler_lock);
  80. if(g_veh_cookie == 0)
  81. {
  82. g_veh_cookie = rand;
  83. }
  84. sgx_spin_unlock(&g_handler_lock);
  85. }
  86. if(!sgx_is_within_enclave((const void*)exception_handler, 0))
  87. {
  88. return NULL;
  89. }
  90. handler_node_t *node = (handler_node_t *)malloc(sizeof(handler_node_t));
  91. if(!node)
  92. {
  93. return NULL;
  94. }
  95. node->callback = ENC_VEH_POINTER(exception_handler);
  96. // write lock
  97. sgx_spin_lock(&g_handler_lock);
  98. if((g_first_node == NULL) || is_first_handler)
  99. {
  100. node->next = g_first_node;
  101. g_first_node = node;
  102. }
  103. else
  104. {
  105. handler_node_t *tmp = g_first_node;
  106. while(tmp->next != NULL)
  107. {
  108. tmp = tmp->next;
  109. }
  110. node->next = NULL;
  111. tmp->next = node;
  112. }
  113. // write unlock
  114. sgx_spin_unlock(&g_handler_lock);
  115. return node;
  116. }
  117. // sgx_unregister_exception_handler()
  118. // unregister a custom exception handler.
  119. // Parameter
  120. // handler - a handler to the custom exception handler previously
  121. // registered using the sgx_register_exception_handler function.
  122. // Return Value
  123. // none zero - success
  124. // 0 - fail
  125. int sgx_unregister_exception_handler(void *handler)
  126. {
  127. if(!handler)
  128. {
  129. return 0;
  130. }
  131. int status = 0;
  132. // write lock
  133. sgx_spin_lock(&g_handler_lock);
  134. if(g_first_node)
  135. {
  136. handler_node_t *node = g_first_node;
  137. if(node == handler)
  138. {
  139. g_first_node = node->next;
  140. status = 1;
  141. }
  142. else
  143. {
  144. while(node->next != NULL)
  145. {
  146. if(node->next == handler)
  147. {
  148. node->next = node->next->next;
  149. status = 1;
  150. break;
  151. }
  152. node = node->next;
  153. }
  154. }
  155. }
  156. // write unlock
  157. sgx_spin_unlock(&g_handler_lock);
  158. if(status) free(handler);
  159. return status;
  160. }
  161. // continue_execution(sgx_exception_info_t *info):
  162. // try to restore the thread context saved in info to current execution context.
  163. extern "C" __attribute__((regparm(1))) void continue_execution(sgx_exception_info_t *info);
  164. // internal_handle_exception(sgx_exception_info_t *info):
  165. // the 2nd phrase exception handing, which traverse registered exception handlers.
  166. // if the exception can be handled, then continue execution
  167. // otherwise, throw abortion, go back to 1st phrase, and call the default handler.
  168. extern "C" __attribute__((regparm(1))) void internal_handle_exception(sgx_exception_info_t *info)
  169. {
  170. int status = EXCEPTION_CONTINUE_SEARCH;
  171. handler_node_t *node = NULL;
  172. thread_data_t *thread_data = get_thread_data();
  173. size_t size = 0;
  174. uintptr_t *nhead = NULL;
  175. uintptr_t *ntmp = NULL;
  176. uintptr_t xsp = 0;
  177. if (thread_data->exception_flag < 0)
  178. goto failed_end;
  179. thread_data->exception_flag++;
  180. // read lock
  181. sgx_spin_lock(&g_handler_lock);
  182. node = g_first_node;
  183. while(node != NULL)
  184. {
  185. size += sizeof(uintptr_t);
  186. node = node->next;
  187. }
  188. // There's no exception handler registered
  189. if (size == 0)
  190. {
  191. sgx_spin_unlock(&g_handler_lock);
  192. //exception cannot be handled
  193. thread_data->exception_flag = -1;
  194. //instruction triggering the exception will be executed again.
  195. continue_execution(info);
  196. }
  197. if ((nhead = (uintptr_t *)malloc(size)) == NULL)
  198. {
  199. sgx_spin_unlock(&g_handler_lock);
  200. goto failed_end;
  201. }
  202. ntmp = nhead;
  203. node = g_first_node;
  204. while(node != NULL)
  205. {
  206. *ntmp = node->callback;
  207. ntmp++;
  208. node = node->next;
  209. }
  210. // read unlock
  211. sgx_spin_unlock(&g_handler_lock);
  212. // call exception handler until EXCEPTION_CONTINUE_EXECUTION is returned
  213. ntmp = nhead;
  214. while(size > 0)
  215. {
  216. sgx_exception_handler_t handler = DEC_VEH_POINTER(*ntmp);
  217. status = handler(info);
  218. if(EXCEPTION_CONTINUE_EXECUTION == status)
  219. {
  220. break;
  221. }
  222. ntmp++;
  223. size -= sizeof(sgx_exception_handler_t);
  224. }
  225. free(nhead);
  226. // call default handler
  227. // ignore invalid return value, treat to EXCEPTION_CONTINUE_SEARCH
  228. // check SP to be written on SSA is pointing to the trusted stack
  229. xsp = info->cpu_context.REG(sp);
  230. if (!is_valid_sp(xsp))
  231. {
  232. goto failed_end;
  233. }
  234. if(EXCEPTION_CONTINUE_EXECUTION == status)
  235. {
  236. //exception is handled, decrease the nested exception count
  237. thread_data->exception_flag--;
  238. }
  239. else
  240. {
  241. //exception cannot be handled
  242. thread_data->exception_flag = -1;
  243. }
  244. //instruction triggering the exception will be executed again.
  245. continue_execution(info);
  246. failed_end:
  247. thread_data->exception_flag = -1; // mark the current exception cannot be handled
  248. abort(); // throw abortion
  249. }
  250. // trts_handle_exception(void *tcs)
  251. // the entry point for the exceptoin handling
  252. // Parameter
  253. // the pointer of TCS
  254. // Return Value
  255. // none zero - success
  256. // 0 - fail
  257. #include "trts_internal.h"
  258. extern "C" sgx_status_t trts_handle_exception(void *tcs)
  259. {
  260. thread_data_t *thread_data = get_thread_data();
  261. ssa_gpr_t *ssa_gpr = NULL;
  262. sgx_exception_info_t *info = NULL;
  263. uintptr_t sp, *new_sp = NULL;
  264. size_t size = 0;
  265. if (tcs == NULL) goto default_handler;
  266. if(get_enclave_state() != ENCLAVE_INIT_DONE)
  267. {
  268. goto default_handler;
  269. }
  270. // check if the exception is raised from 2nd phrase
  271. if(thread_data->exception_flag == -1) {
  272. goto default_handler;
  273. }
  274. if ((TD2TCS(thread_data) != tcs)
  275. || (((thread_data->first_ssa_gpr)&(~0xfff)) - SE_PAGE_SIZE) != (uintptr_t)tcs) {
  276. goto default_handler;
  277. }
  278. // no need to check the result of ssa_gpr because thread_data is always trusted
  279. ssa_gpr = reinterpret_cast<ssa_gpr_t *>(thread_data->first_ssa_gpr);
  280. sp = ssa_gpr->REG(sp);
  281. if(!is_stack_addr((void*)sp, 0)) // check stack overrun only, alignment will be checked after exception handled
  282. {
  283. g_enclave_state = ENCLAVE_CRASHED;
  284. return SGX_ERROR_STACK_OVERRUN;
  285. }
  286. size = 0;
  287. #ifdef SE_GNU64
  288. size += 128; // x86_64 requires a 128-bytes red zone, which begins directly
  289. // after the return addr and includes func's arguments
  290. #endif
  291. // decrease the stack to give space for info
  292. size += sizeof(sgx_exception_info_t);
  293. sp -= size;
  294. sp = sp & ~0xF;
  295. // check the decreased sp to make sure it is in the trusted stack range
  296. if(!is_stack_addr((void *)sp, size))
  297. {
  298. g_enclave_state = ENCLAVE_CRASHED;
  299. return SGX_ERROR_STACK_OVERRUN;
  300. }
  301. if(ssa_gpr->exit_info.valid != 1)
  302. { // exception handlers are not allowed to call in a non-exception state
  303. goto default_handler;
  304. }
  305. info = (sgx_exception_info_t *)sp;
  306. // No need to check the stack as it have already been checked by assembly code
  307. // initialize the info with SSA[0]
  308. info->exception_vector = (sgx_exception_vector_t)ssa_gpr->exit_info.vector;
  309. info->exception_type = (sgx_exception_type_t)ssa_gpr->exit_info.exit_type;
  310. info->cpu_context.REG(ax) = ssa_gpr->REG(ax);
  311. info->cpu_context.REG(cx) = ssa_gpr->REG(cx);
  312. info->cpu_context.REG(dx) = ssa_gpr->REG(dx);
  313. info->cpu_context.REG(bx) = ssa_gpr->REG(bx);
  314. info->cpu_context.REG(sp) = ssa_gpr->REG(sp);
  315. info->cpu_context.REG(bp) = ssa_gpr->REG(bp);
  316. info->cpu_context.REG(si) = ssa_gpr->REG(si);
  317. info->cpu_context.REG(di) = ssa_gpr->REG(di);
  318. info->cpu_context.REG(flags) = ssa_gpr->REG(flags);
  319. info->cpu_context.REG(ip) = ssa_gpr->REG(ip);
  320. #ifdef SE_64
  321. info->cpu_context.r8 = ssa_gpr->r8;
  322. info->cpu_context.r9 = ssa_gpr->r9;
  323. info->cpu_context.r10 = ssa_gpr->r10;
  324. info->cpu_context.r11 = ssa_gpr->r11;
  325. info->cpu_context.r12 = ssa_gpr->r12;
  326. info->cpu_context.r13 = ssa_gpr->r13;
  327. info->cpu_context.r14 = ssa_gpr->r14;
  328. info->cpu_context.r15 = ssa_gpr->r15;
  329. #endif
  330. // decrease the stack to save the SSA[0]->ip
  331. size = sizeof(uintptr_t);
  332. new_sp = (uintptr_t *)(sp - size);
  333. if(!is_stack_addr(new_sp, size))
  334. {
  335. g_enclave_state = ENCLAVE_CRASHED;
  336. return SGX_ERROR_STACK_OVERRUN;
  337. }
  338. ssa_gpr->REG(ip) = (size_t)internal_handle_exception; // prepare the ip for 2nd phrase handling
  339. ssa_gpr->REG(sp) = (size_t)new_sp; // new stack for internal_handle_exception
  340. ssa_gpr->REG(ax) = (size_t)info; // 1st parameter (info) for LINUX32
  341. ssa_gpr->REG(di) = (size_t)info; // 1st parameter (info) for LINUX64, LINUX32 also uses it while restoring the context
  342. *new_sp = info->cpu_context.REG(ip); // for debugger to get call trace
  343. //mark valid to 0 to prevent eenter again
  344. ssa_gpr->exit_info.valid = 0;
  345. return SGX_SUCCESS;
  346. default_handler:
  347. g_enclave_state = ENCLAVE_CRASHED;
  348. return SGX_ERROR_ENCLAVE_CRASHED;
  349. }