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