trts_ecall.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "se_memcpy.h"
  32. #include "thread_data.h"
  33. #include "global_data.h"
  34. #include "rts.h"
  35. #include "util.h"
  36. #include "xsave.h"
  37. #include "sgx_trts.h"
  38. #include "sgx_spinlock.h"
  39. #include "global_init.h"
  40. #include "trts_internal.h"
  41. #include "trts_inst.h"
  42. #include "trts_emodpr.h"
  43. #include "metadata.h"
  44. # include "linux/elf_parser.h"
  45. # define GET_TLS_INFO elf_tls_info
  46. // is_ecall_allowed()
  47. // check the index in the dynamic entry table
  48. static sgx_status_t is_ecall_allowed(uint32_t ordinal)
  49. {
  50. if(ordinal >= g_ecall_table.nr_ecall)
  51. {
  52. return SGX_ERROR_INVALID_FUNCTION;
  53. }
  54. thread_data_t *thread_data = get_thread_data();
  55. if(thread_data->last_sp == thread_data->stack_base_addr)
  56. {
  57. // root ECALL, check the priv bits.
  58. if (g_ecall_table.ecall_table[ordinal].is_priv)
  59. return SGX_ERROR_ECALL_NOT_ALLOWED;
  60. return SGX_SUCCESS;
  61. }
  62. ocall_context_t *context = reinterpret_cast<ocall_context_t*>(thread_data->last_sp);
  63. if(context->ocall_flag != OCALL_FLAG)
  64. {
  65. // abort the enclave if ocall frame is invalid
  66. abort();
  67. }
  68. uintptr_t ocall_index = context->ocall_index;
  69. if(ocall_index >= g_dyn_entry_table.nr_ocall)
  70. {
  71. return SGX_ERROR_INVALID_FUNCTION;
  72. }
  73. return (g_dyn_entry_table.entry_table[ocall_index * g_ecall_table.nr_ecall + ordinal] ? SGX_SUCCESS : SGX_ERROR_ECALL_NOT_ALLOWED);
  74. }
  75. // get_func_addr()
  76. // Get the address of ecall function from the ecall table
  77. // Parameters:
  78. // [IN] ordinal - the index of the ecall function in the ecall table
  79. // Return Value:
  80. // non-zero - success
  81. // zero - fail
  82. //
  83. static sgx_status_t get_func_addr(uint32_t ordinal, void **addr)
  84. {
  85. sgx_status_t status = is_ecall_allowed(ordinal);
  86. if(SGX_SUCCESS != status)
  87. {
  88. return status;
  89. }
  90. *addr = const_cast<void *>(g_ecall_table.ecall_table[ordinal].ecall_addr);
  91. if(!sgx_is_within_enclave(*addr, 0))
  92. {
  93. return SGX_ERROR_UNEXPECTED;
  94. }
  95. return SGX_SUCCESS;
  96. }
  97. typedef struct _tcs_node_t
  98. {
  99. uintptr_t tcs;
  100. struct _tcs_node_t *next;
  101. } tcs_node_t;
  102. static tcs_node_t *g_tcs_node = NULL;
  103. static sgx_spinlock_t g_tcs_node_lock = SGX_SPINLOCK_INITIALIZER;
  104. static uintptr_t g_tcs_cookie = 0;
  105. #define ENC_TCS_POINTER(x) (uintptr_t)(x) ^ g_tcs_cookie
  106. #define DEC_TCS_POINTER(x) (void *)((x) ^ g_tcs_cookie)
  107. // do_save_tcs()
  108. // Save tcs while function do_ecall_add_thread invoked.
  109. // Parameters:
  110. // [IN] ptcs - the tcs_t pointer which need to be saved
  111. // Return Value:
  112. // zero - success
  113. // non-zero - fail
  114. //
  115. static sgx_status_t do_save_tcs(void *ptcs)
  116. {
  117. if(unlikely(g_tcs_cookie == 0))
  118. {
  119. uintptr_t rand = 0;
  120. do
  121. {
  122. if(SGX_SUCCESS != sgx_read_rand((unsigned char *)&rand, sizeof(rand)))
  123. {
  124. return SGX_ERROR_UNEXPECTED;
  125. }
  126. } while(rand == 0);
  127. sgx_spin_lock(&g_tcs_node_lock);
  128. if(g_tcs_cookie == 0)
  129. {
  130. g_tcs_cookie = rand;
  131. }
  132. sgx_spin_unlock(&g_tcs_node_lock);
  133. }
  134. tcs_node_t *tcs_node = (tcs_node_t *)malloc(sizeof(tcs_node_t));
  135. if(!tcs_node)
  136. {
  137. return SGX_ERROR_UNEXPECTED;
  138. }
  139. tcs_node->tcs = ENC_TCS_POINTER(ptcs);
  140. sgx_spin_lock(&g_tcs_node_lock);
  141. tcs_node->next = g_tcs_node;
  142. g_tcs_node = tcs_node;
  143. sgx_spin_unlock(&g_tcs_node_lock);
  144. return SGX_SUCCESS;
  145. }
  146. // do_del_tcs()
  147. // Delete tcs from the global tcs list.
  148. // Parameters:
  149. // [IN] ptcs - the tcs_t pointer which need to be deleted
  150. // Return Value:
  151. // N/A
  152. //
  153. static void do_del_tcs(void *ptcs)
  154. {
  155. sgx_spin_lock(&g_tcs_node_lock);
  156. if (g_tcs_node != NULL)
  157. {
  158. if (DEC_TCS_POINTER(g_tcs_node->tcs) == ptcs)
  159. {
  160. tcs_node_t *tmp = g_tcs_node;
  161. g_tcs_node = g_tcs_node->next;
  162. free(tmp);
  163. }
  164. else
  165. {
  166. tcs_node_t *tcs_node = g_tcs_node->next;
  167. tcs_node_t *pre_tcs_node = g_tcs_node;
  168. while (tcs_node != NULL)
  169. {
  170. if (DEC_TCS_POINTER(tcs_node->tcs) == ptcs)
  171. {
  172. pre_tcs_node->next = tcs_node->next;
  173. free(tcs_node);
  174. break;
  175. }
  176. pre_tcs_node = tcs_node;
  177. tcs_node = tcs_node->next;
  178. }
  179. }
  180. }
  181. sgx_spin_unlock(&g_tcs_node_lock);
  182. }
  183. static volatile bool g_is_first_ecall = true;
  184. static volatile sgx_spinlock_t g_ife_lock = SGX_SPINLOCK_INITIALIZER;
  185. typedef sgx_status_t (*ecall_func_t)(void *ms);
  186. static sgx_status_t trts_ecall(uint32_t ordinal, void *ms)
  187. {
  188. sgx_status_t status = SGX_ERROR_UNEXPECTED;
  189. if (unlikely(g_is_first_ecall))
  190. {
  191. // The thread performing the global initialization cannot do a nested ECall
  192. thread_data_t *thread_data = get_thread_data();
  193. if (thread_data->last_sp != thread_data->stack_base_addr)
  194. { // nested ecall
  195. return SGX_ERROR_ECALL_NOT_ALLOWED;
  196. }
  197. sgx_spin_lock(&g_ife_lock);
  198. if (g_is_first_ecall)
  199. {
  200. #ifndef SE_SIM
  201. if(EDMM_supported)
  202. {
  203. //change back the page permission
  204. size_t enclave_start = (size_t)&__ImageBase;
  205. if((status = change_protection((void *)enclave_start)) != SGX_SUCCESS)
  206. {
  207. sgx_spin_unlock(&g_ife_lock);
  208. return status;
  209. }
  210. }
  211. #endif
  212. //invoke global object's construction
  213. init_global_object();
  214. g_is_first_ecall = false;
  215. }
  216. sgx_spin_unlock(&g_ife_lock);
  217. }
  218. void *addr = NULL;
  219. status = get_func_addr(ordinal, &addr);
  220. if(status == SGX_SUCCESS)
  221. {
  222. ecall_func_t func = (ecall_func_t)addr;
  223. status = func(ms);
  224. }
  225. return status;
  226. }
  227. extern "C" uintptr_t __stack_chk_guard;
  228. static void init_static_stack_canary(void *tcs)
  229. {
  230. size_t *canary = TCS2CANARY(tcs);
  231. *canary = (size_t)__stack_chk_guard;
  232. }
  233. static bool is_utility_thread()
  234. {
  235. thread_data_t *thread_data = get_thread_data();
  236. if ((thread_data != NULL) && (thread_data->flags & SGX_UTILITY_THREAD))
  237. {
  238. return true;
  239. }
  240. else
  241. {
  242. return false;
  243. }
  244. }
  245. sgx_status_t do_init_thread(void *tcs)
  246. {
  247. thread_data_t *thread_data = GET_PTR(thread_data_t, tcs, g_global_data.td_template.self_addr);
  248. #ifndef SE_SIM
  249. size_t saved_stack_commit_addr = thread_data->stack_commit_addr;
  250. bool thread_first_init = (saved_stack_commit_addr == 0) ? true : false;
  251. #endif
  252. size_t stack_guard = thread_data->stack_guard;
  253. size_t thread_flags = thread_data->flags;
  254. memcpy_s(thread_data, SE_PAGE_SIZE, const_cast<thread_data_t *>(&g_global_data.td_template), sizeof(thread_data_t));
  255. thread_data->last_sp += (size_t)tcs;
  256. thread_data->self_addr += (size_t)tcs;
  257. thread_data->stack_base_addr += (size_t)tcs;
  258. thread_data->stack_limit_addr += (size_t)tcs;
  259. thread_data->stack_commit_addr = thread_data->stack_limit_addr;
  260. thread_data->first_ssa_gpr += (size_t)tcs;
  261. thread_data->tls_array += (size_t)tcs;
  262. thread_data->tls_addr += (size_t)tcs;
  263. thread_data->last_sp -= (size_t)STATIC_STACK_SIZE;
  264. thread_data->stack_base_addr -= (size_t)STATIC_STACK_SIZE;
  265. thread_data->stack_guard = stack_guard;
  266. thread_data->flags = thread_flags;
  267. init_static_stack_canary(tcs);
  268. #ifndef SE_SIM
  269. if (EDMM_supported && is_dynamic_thread(tcs))
  270. {
  271. if (thread_first_init)
  272. {
  273. uint32_t page_count = get_dynamic_stack_max_page();
  274. thread_data->stack_commit_addr += ((sys_word_t)page_count << SE_PAGE_SHIFT);
  275. }
  276. else
  277. {
  278. thread_data->stack_commit_addr = saved_stack_commit_addr;
  279. }
  280. }
  281. #endif
  282. uintptr_t tls_addr = 0;
  283. size_t tdata_size = 0;
  284. if(0 != GET_TLS_INFO(&__ImageBase, &tls_addr, &tdata_size))
  285. {
  286. return SGX_ERROR_UNEXPECTED;
  287. }
  288. if(tls_addr)
  289. {
  290. memset((void *)TRIM_TO_PAGE(thread_data->tls_addr), 0, ROUND_TO_PAGE(thread_data->self_addr - thread_data->tls_addr));
  291. memcpy_s((void *)(thread_data->tls_addr), thread_data->self_addr - thread_data->tls_addr, (void *)tls_addr, tdata_size);
  292. }
  293. return SGX_SUCCESS;
  294. }
  295. sgx_status_t do_ecall(int index, void *ms, void *tcs)
  296. {
  297. sgx_status_t status = SGX_ERROR_UNEXPECTED;
  298. if(ENCLAVE_INIT_DONE != get_enclave_state())
  299. {
  300. return status;
  301. }
  302. thread_data_t *thread_data = get_thread_data();
  303. if( (NULL == thread_data) || ((thread_data->stack_base_addr == thread_data->last_sp) && (0 != g_global_data.thread_policy)))
  304. {
  305. status = do_init_thread(tcs);
  306. if(0 != status)
  307. {
  308. return status;
  309. }
  310. }
  311. status = trts_ecall(index, ms);
  312. return status;
  313. }
  314. sgx_status_t do_ecall_add_thread(void *ms, void *tcs)
  315. {
  316. sgx_status_t status = SGX_ERROR_UNEXPECTED;
  317. if(!is_utility_thread())
  318. return status;
  319. struct ms_tcs *ms_tcs = (struct ms_tcs*)ms;
  320. if (ms_tcs == NULL)
  321. {
  322. return status;
  323. }
  324. if (!sgx_is_outside_enclave(ms_tcs, sizeof(struct ms_tcs)))
  325. {
  326. abort();
  327. }
  328. void* ptcs = ms_tcs->ptcs;
  329. if (ptcs == NULL)
  330. {
  331. return status;
  332. }
  333. status = do_init_thread(tcs);
  334. if(SGX_SUCCESS != status)
  335. {
  336. return status;
  337. }
  338. status = do_save_tcs(ptcs);
  339. if(SGX_SUCCESS != status)
  340. {
  341. return status;
  342. }
  343. status = do_add_thread(ptcs);
  344. if (SGX_SUCCESS != status)
  345. {
  346. do_del_tcs(ptcs);
  347. return status;
  348. }
  349. return status;
  350. }
  351. // do_uninit_enclave()
  352. // Run the global uninitialized functions when the enclave is destroyed.
  353. // Parameters:
  354. // [IN] tcs - used for running this task
  355. // Return Value:
  356. // zero - success
  357. // non-zero - fail
  358. //
  359. sgx_status_t do_uninit_enclave(void *tcs)
  360. {
  361. #ifndef SE_SIM
  362. if(is_dynamic_thread_exist() && !is_utility_thread())
  363. return SGX_ERROR_UNEXPECTED;
  364. #endif
  365. sgx_spin_lock(&g_tcs_node_lock);
  366. tcs_node_t *tcs_node = g_tcs_node;
  367. g_tcs_node = NULL;
  368. sgx_spin_unlock(&g_tcs_node_lock);
  369. while (tcs_node != NULL)
  370. {
  371. if (DEC_TCS_POINTER(tcs_node->tcs) == tcs)
  372. {
  373. tcs_node_t *tmp = tcs_node;
  374. tcs_node = tcs_node->next;
  375. free(tmp);
  376. continue;
  377. }
  378. size_t start = (size_t)DEC_TCS_POINTER(tcs_node->tcs);
  379. size_t end = start + (1 << SE_PAGE_SHIFT);
  380. int rc = sgx_accept_forward(SI_FLAG_TRIM | SI_FLAG_MODIFIED, start, end);
  381. if(rc != 0)
  382. {
  383. return SGX_ERROR_UNEXPECTED;
  384. }
  385. tcs_node_t *tmp = tcs_node;
  386. tcs_node = tcs_node->next;
  387. free(tmp);
  388. }
  389. sgx_spin_lock(&g_ife_lock);
  390. if (!g_is_first_ecall)
  391. {
  392. uninit_global_object();
  393. }
  394. sgx_spin_unlock(&g_ife_lock);
  395. set_enclave_state(ENCLAVE_CRASHED);
  396. return SGX_SUCCESS;
  397. }
  398. extern sdk_version_t g_sdk_version;
  399. extern "C" sgx_status_t trts_mprotect(size_t start, size_t size, uint64_t perms)
  400. {
  401. int rc = -1;
  402. size_t page;
  403. sgx_status_t ret = SGX_SUCCESS;
  404. SE_DECLSPEC_ALIGN(sizeof(sec_info_t)) sec_info_t si;
  405. //Error return if start or size is not page-aligned or size is zero.
  406. if (!IS_PAGE_ALIGNED(start) || (size == 0) || !IS_PAGE_ALIGNED(size))
  407. return SGX_ERROR_INVALID_PARAMETER;
  408. if (g_sdk_version == SDK_VERSION_2_0)
  409. {
  410. ret = change_permissions_ocall(start, size, perms);
  411. if (ret != SGX_SUCCESS)
  412. return ret;
  413. }
  414. si.flags = perms|SI_FLAG_REG|SI_FLAG_PR;
  415. memset(&si.reserved, 0, sizeof(si.reserved));
  416. for(page = start; page < start + size; page += SE_PAGE_SIZE)
  417. {
  418. do_emodpe(&si, page);
  419. // If the target permission to set is RWX, no EMODPR, hence no EACCEPT.
  420. if ((perms & (SI_FLAG_W|SI_FLAG_X)) != (SI_FLAG_W|SI_FLAG_X))
  421. {
  422. rc = do_eaccept(&si, page);
  423. if(rc != 0)
  424. return (sgx_status_t)rc;
  425. }
  426. }
  427. return SGX_SUCCESS;
  428. }