trts_add_trim.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 <string.h>
  32. #include "sgx_utils.h"
  33. #include "trts_inst.h"
  34. #include "util.h"
  35. #include "trts_trim.h"
  36. #include "trts_util.h"
  37. #include "global_data.h"
  38. #include "se_memcpy.h"
  39. #include "se_page_attr.h"
  40. #include "trts_internal.h"
  41. #ifndef SE_SIM
  42. struct dynamic_flags_attributes
  43. {
  44. si_flags_t si_flags;
  45. uint16_t attributes;
  46. };
  47. // Low level API to EACCEPT pages on grow-up region.
  48. static int sgx_accept_backward(si_flags_t sfl, size_t lo, size_t hi)
  49. {
  50. size_t addr = hi;
  51. SE_DECLSPEC_ALIGN(sizeof(sec_info_t)) sec_info_t si;
  52. si.flags = sfl;
  53. for (uint16_t i = 0; i < (sizeof(si.reserved)/sizeof(si.reserved[0])); i++)
  54. si.reserved[i] = 0;
  55. while (lo < addr)
  56. {
  57. int rc = do_eaccept(&si, addr -= SE_PAGE_SIZE);
  58. if (rc != 0)
  59. abort();
  60. }
  61. return 0;
  62. }
  63. // Low level API to EACCEPT pages on grow-up region during exception handling.
  64. static int sgx_accept_forward_within_exception(size_t lo, size_t hi)
  65. {
  66. size_t addr = lo;
  67. SE_DECLSPEC_ALIGN(sizeof(sec_info_t)) sec_info_t si;
  68. #ifdef DEBUG
  69. unsigned int sp_value = 0;
  70. asm("mov %%esp, %0;" : "=r" (sp_value) :);
  71. if ((sp_value & (SE_PAGE_SIZE -1)) <= (SE_PAGE_SIZE - (STATIC_STACK_SIZE % SE_PAGE_SIZE)))
  72. return SGX_ERROR_UNEXPECTED;
  73. #endif
  74. si.flags = SI_FLAGS_RW | SI_FLAG_PENDING;
  75. for (uint16_t i = 0; i < (sizeof(si.reserved)/sizeof(si.reserved[0])); i++)
  76. si.reserved[i] = 0;
  77. while (addr < hi)
  78. {
  79. int rc = do_eaccept(&si, addr);
  80. if (rc != 0)
  81. abort();
  82. addr += SE_PAGE_SIZE;
  83. }
  84. return 0;
  85. }
  86. const volatile layout_t *get_dynamic_layout_by_id(uint16_t id)
  87. {
  88. for(uint32_t i = 0; i < g_global_data.layout_entry_num; i++)
  89. {
  90. if(g_global_data.layout_table[i].entry.id == id)
  91. {
  92. return &(g_global_data.layout_table[i]);
  93. }
  94. }
  95. return NULL;
  96. }
  97. // EACCEPT trim requests when the enclave completes initialization.
  98. int accept_post_remove(const volatile layout_t *layout_start, const volatile layout_t *layout_end, size_t offset)
  99. {
  100. int ret = -1;
  101. for (const volatile layout_t *layout = layout_start; layout < layout_end; layout++)
  102. {
  103. if (!IS_GROUP_ID(layout->group.id) && (layout->entry.attributes & PAGE_ATTR_POST_REMOVE))
  104. {
  105. size_t start_addr = (size_t)layout->entry.rva + offset + (size_t)get_enclave_base();
  106. uint32_t page_count = layout->entry.page_count;
  107. if (0 != (ret = sgx_accept_forward(SI_FLAG_TRIM | SI_FLAG_MODIFIED, start_addr, start_addr + ((size_t)page_count << SE_PAGE_SHIFT))))
  108. return ret;
  109. }
  110. else if (IS_GROUP_ID(layout->group.id))
  111. {
  112. size_t step = 0;
  113. for(uint32_t j = 0; j < layout->group.load_times; j++)
  114. {
  115. step += (size_t)layout->group.load_step;
  116. if(0 != (ret = accept_post_remove(&layout[-layout->group.entry_count], layout, step)))
  117. return ret;
  118. }
  119. }
  120. }
  121. return 0;
  122. }
  123. static int check_dynamic_entry_range(void *addr, size_t page_count, uint16_t entry_id, size_t entry_offset, struct dynamic_flags_attributes *fa)
  124. {
  125. const volatile layout_t *layout = NULL, *heap_max_layout;
  126. size_t entry_start_addr;
  127. uint32_t entry_page_count;
  128. if (entry_id < LAYOUT_ID_HEAP_MIN
  129. || entry_id > LAYOUT_ID_STACK_DYN_MIN
  130. || (NULL == (layout = get_dynamic_layout_by_id(entry_id))))
  131. {
  132. return -1;
  133. }
  134. entry_start_addr = (size_t)get_enclave_base() + (size_t)layout->entry.rva + entry_offset;
  135. entry_page_count = layout->entry.page_count;
  136. // if there exists LAYOUT_ID_HEAP_MAX, we should include it as well
  137. if ((entry_id == LAYOUT_ID_HEAP_INIT)
  138. && (heap_max_layout = get_dynamic_layout_by_id(LAYOUT_ID_HEAP_MAX)))
  139. {
  140. entry_page_count += heap_max_layout->entry.page_count;
  141. }
  142. if ((size_t)addr >= entry_start_addr
  143. && (size_t)addr + (page_count << SE_PAGE_SHIFT) <= entry_start_addr + ((size_t)entry_page_count << SE_PAGE_SHIFT))
  144. {
  145. if (fa != NULL)
  146. {
  147. fa->si_flags = layout->entry.si_flags;
  148. fa->attributes = layout->entry.attributes;
  149. }
  150. return 0;
  151. }
  152. else
  153. {
  154. return -1;
  155. }
  156. }
  157. // Verify if the range specified belongs to a dynamic range recorded in metadata.
  158. static int check_dynamic_range(void *addr, size_t page_count, size_t *offset, struct dynamic_flags_attributes *fa)
  159. {
  160. const volatile layout_t *dt_layout = NULL;
  161. // check heap range
  162. if (0 == check_dynamic_entry_range(addr, page_count, LAYOUT_ID_HEAP_INIT, 0, fa))
  163. return 0;
  164. // check dynamic thread entries range
  165. if (NULL != (dt_layout = get_dynamic_layout_by_id(LAYOUT_ID_THREAD_GROUP_DYN)))
  166. {
  167. for (uint16_t id = LAYOUT_ID_TCS_DYN; id <= LAYOUT_ID_STACK_DYN_MIN; id++)
  168. for (uint32_t i = 0; i < dt_layout->group.load_times + 1; i++)
  169. {
  170. if (0 == check_dynamic_entry_range(addr, page_count, id, i * ((size_t)dt_layout->group.load_step), fa))
  171. {
  172. if (offset != NULL) *offset = i * ((size_t)dt_layout->group.load_step);
  173. return 0;
  174. }
  175. }
  176. }
  177. else
  178. {
  179. // LAYOUT_ID_THREAD_GROUP_DYN does not exist, but possibly there is one single dynamic thead
  180. for (uint16_t id = LAYOUT_ID_TCS_DYN; id <= LAYOUT_ID_STACK_DYN_MIN; id++)
  181. if (0 == check_dynamic_entry_range(addr, page_count, id, 0, fa))
  182. {
  183. if (offset != NULL) *offset = 0;
  184. return 0;
  185. }
  186. }
  187. return -1;
  188. }
  189. int is_dynamic_thread(void *tcs)
  190. {
  191. struct dynamic_flags_attributes fa;
  192. if ((tcs != NULL) && (check_dynamic_range(tcs, 1, NULL, &fa) == 0) &&
  193. (fa.si_flags == SI_FLAGS_TCS))
  194. {
  195. return true;
  196. }
  197. return false;
  198. }
  199. int is_dynamic_thread_exist()
  200. {
  201. if(!EDMM_supported)
  202. return false;
  203. const volatile layout_t * layout = get_dynamic_layout_by_id(LAYOUT_ID_STACK_DYN_MIN);
  204. if (!layout)
  205. return false;
  206. else
  207. return true;
  208. }
  209. uint32_t get_dynamic_stack_max_page()
  210. {
  211. const volatile layout_t * layout = get_dynamic_layout_by_id(LAYOUT_ID_STACK_DYN_MAX);
  212. if (!layout)
  213. return 0;
  214. else
  215. return layout->entry.page_count;
  216. }
  217. #endif
  218. int sgx_accept_forward(si_flags_t sfl, size_t lo, size_t hi)
  219. {
  220. #ifdef SE_SIM
  221. (void)sfl;
  222. (void)lo;
  223. (void)hi;
  224. return 0;
  225. #else
  226. size_t addr = lo;
  227. SE_DECLSPEC_ALIGN(sizeof(sec_info_t)) sec_info_t si;
  228. si.flags = sfl;
  229. for (uint16_t i = 0; i < (sizeof(si.reserved)/sizeof(si.reserved[0])); i++)
  230. si.reserved[i] = 0;
  231. while (addr < hi)
  232. {
  233. int rc = do_eaccept(&si, addr);
  234. if (rc != 0)
  235. abort();
  236. addr += SE_PAGE_SIZE;
  237. }
  238. return 0;
  239. #endif
  240. }
  241. // High level API to EACCEPT pages, mainly used in exception handling
  242. // to deal with stack expansion.
  243. int apply_pages_within_exception(void *start_address, size_t page_count)
  244. {
  245. #ifdef SE_SIM
  246. (void)start_address;
  247. (void)page_count;
  248. return 0;
  249. #else
  250. int rc;
  251. if (start_address == NULL)
  252. return -1;
  253. if (check_dynamic_range(start_address, page_count, NULL, NULL) != 0)
  254. return -1;
  255. size_t start = (size_t)start_address;
  256. size_t end = start + (page_count << SE_PAGE_SHIFT);
  257. rc = sgx_accept_forward_within_exception(start, end);
  258. return rc;
  259. #endif
  260. }
  261. // High level API to EACCEPT pages
  262. int apply_EPC_pages(void *start_address, size_t page_count)
  263. {
  264. #ifdef SE_SIM
  265. (void)start_address;
  266. (void)page_count;
  267. return 0;
  268. #else
  269. int rc;
  270. struct dynamic_flags_attributes fa;
  271. if (start_address == NULL)
  272. return -1;
  273. if (check_dynamic_range(start_address, page_count, NULL, &fa) != 0)
  274. return -1;
  275. size_t start = (size_t)start_address;
  276. size_t end = start + (page_count << SE_PAGE_SHIFT);
  277. if (fa.attributes & PAGE_DIR_GROW_DOWN)
  278. {
  279. rc = sgx_accept_forward(SI_FLAGS_RW | SI_FLAG_PENDING, start, end);
  280. }
  281. else
  282. {
  283. rc = sgx_accept_backward(SI_FLAGS_RW | SI_FLAG_PENDING, start, end);
  284. }
  285. return rc;
  286. #endif
  287. }
  288. // High level API to trim previously EAUG-ed pages.
  289. int trim_EPC_pages(void *start_address, size_t page_count)
  290. {
  291. #ifdef SE_SIM
  292. (void)start_address;
  293. (void)page_count;
  294. return 0;
  295. #else
  296. int rc;
  297. if (start_address == NULL)
  298. return -1;
  299. // check range
  300. if (check_dynamic_range(start_address, page_count, NULL, NULL) != 0)
  301. return -1;
  302. size_t start = (size_t)start_address;
  303. size_t end = start + (page_count << SE_PAGE_SHIFT);
  304. // trim ocall
  305. rc = trim_range_ocall(start, end);
  306. assert(rc == 0);
  307. rc = sgx_accept_forward(SI_FLAG_TRIM | SI_FLAG_MODIFIED, start, end);
  308. assert(rc == 0);
  309. // trim commit ocall
  310. size_t i = start;
  311. while (i < end)
  312. {
  313. rc = trim_range_commit_ocall(i);
  314. assert(rc == 0);
  315. i += SE_PAGE_SIZE;
  316. }
  317. return rc;
  318. #endif
  319. }
  320. // Create a thread dynamically.
  321. // It will add necessary pages and transform one of them into type TCS.
  322. sgx_status_t do_add_thread(void *ptcs)
  323. {
  324. #ifdef SE_SIM
  325. (void)ptcs;
  326. return SGX_SUCCESS;
  327. #else
  328. int ret = SGX_ERROR_UNEXPECTED;
  329. tcs_t *tcs = (tcs_t *)ptcs;
  330. tcs_t *tcs_template = NULL;
  331. size_t offset = 0;
  332. size_t enclave_base = (size_t)get_enclave_base();
  333. if ( 0 != check_dynamic_range((void *)tcs, 1, &offset, NULL))
  334. return SGX_ERROR_UNEXPECTED;
  335. // check if the tcs provided exactly matches the one in signtool
  336. const volatile layout_t *tcs_layout = get_dynamic_layout_by_id(LAYOUT_ID_TCS_DYN);
  337. if (!tcs_layout)
  338. return SGX_ERROR_UNEXPECTED;
  339. if ((size_t)(enclave_base + tcs_layout->entry.rva + offset) != (size_t)(tcs))
  340. return SGX_ERROR_UNEXPECTED;
  341. // adding page for all the dynamic entries
  342. for (uint16_t id = LAYOUT_ID_TCS_DYN; id <= LAYOUT_ID_STACK_DYN_MIN; id++)
  343. {
  344. const volatile layout_t *layout = get_dynamic_layout_by_id(id);
  345. if (layout && (layout->entry.attributes & PAGE_ATTR_DYN_THREAD))
  346. {
  347. ret = apply_EPC_pages((void *)(enclave_base + layout->entry.rva + offset), layout->entry.page_count);
  348. if (ret != 0)
  349. return SGX_ERROR_UNEXPECTED;
  350. }
  351. }
  352. //Copy and initialize TCS
  353. tcs_template = (tcs_t *)g_global_data.tcs_template;
  354. memcpy_s(tcs, TCS_SIZE, tcs_template, sizeof(g_global_data.tcs_template));
  355. //Adjust the tcs fields
  356. tcs->ossa = (size_t)GET_PTR(size_t, (void *)tcs, tcs->ossa) - enclave_base;
  357. tcs->ofs_base = (size_t)GET_PTR(size_t, (void *)tcs, tcs->ofs_base) - enclave_base;
  358. tcs->ogs_base = (size_t)GET_PTR(size_t, (void *)tcs, tcs->ogs_base) - enclave_base;
  359. //OCALL for MKTCS
  360. ret = sgx_ocall(0, tcs);
  361. if (ret != 0)
  362. return SGX_ERROR_UNEXPECTED;
  363. //EACCEPT for MKTCS
  364. ret = sgx_accept_backward(SI_FLAG_TCS | SI_FLAG_MODIFIED, (size_t)tcs, (size_t)tcs + SE_PAGE_SIZE);
  365. if (ret != 0)
  366. return SGX_ERROR_UNEXPECTED;
  367. return SGX_SUCCESS;
  368. #endif
  369. }