urts_com.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. #ifndef _URTS_COM_H_
  32. #define _URTS_COM_H_
  33. #include "arch.h"
  34. #include "sgx_error.h"
  35. #include "se_error_internal.h"
  36. #include "se_trace.h"
  37. #include "file.h"
  38. #include "sgx_eid.h"
  39. #include "se_map.h"
  40. #include "launch_checker.h"
  41. #include "debugger_support.h"
  42. #include "loader.h"
  43. #include "binparser.h"
  44. #include "cpuid.h"
  45. #include "se_macro.h"
  46. #include "prd_css_util.h"
  47. #include "se_detect.h"
  48. #include "rts.h"
  49. #include "enclave_creator_hw.h"
  50. #include <sys/mman.h>
  51. #ifndef PARSER
  52. #include "elfparser.h"
  53. #define PARSER ElfParser
  54. #endif
  55. #include "xsave.h"
  56. #include "ittnotify.h"
  57. #include "ittnotify_config.h"
  58. #include "ittnotify_types.h"
  59. extern "C" int __itt_init_ittlib(const char*, __itt_group_id);
  60. extern "C" __itt_global* __itt_get_ittapi_global();
  61. #define HSW_C0 0x306c3
  62. #define GPR_A0 0x406e0
  63. #define GPR_B0 0x406e1
  64. #define GPR_P0 0x506e0
  65. #ifndef SE_SIM
  66. static int validate_platform()
  67. {
  68. int cpu_info[4] = {0, 0, 0, 0};
  69. __cpuid(cpu_info, 1);
  70. // The compatibility between SDK and PSW is checked by the metadata version.
  71. // Below check the compatibility between the platform and uRTS only.
  72. // It is HSW users' responsibility to make the uRTS version to consistent with the HSW patch.
  73. if(cpu_info[0] == HSW_C0)
  74. {
  75. return SGX_SUCCESS;
  76. }
  77. // GPR region
  78. else if(cpu_info[0] == GPR_A0 || cpu_info[0] == GPR_B0 || cpu_info[0] == GPR_P0)
  79. {
  80. SE_TRACE(SE_TRACE_ERROR, "ERROR: The enclave cannot be launched on current platform.\n");
  81. return SGX_ERROR_INVALID_VERSION;
  82. }
  83. return SGX_SUCCESS;
  84. }
  85. #endif
  86. static sgx_status_t get_metadata(BinParser *parser, const int debug, metadata_t **metadata, sgx_misc_attribute_t *sgx_misc_attr)
  87. {
  88. assert(parser != NULL && metadata != NULL && sgx_misc_attr != NULL);
  89. uint64_t meta_rva = parser->get_metadata_offset();
  90. const uint8_t *base_addr = parser->get_start_addr();
  91. uint64_t supported_metadata_version_list[] = {
  92. META_DATA_MAKE_VERSION(MAJOR_VERSION,MINOR_VERSION ),
  93. META_DATA_MAKE_VERSION(SGX_1_9_MAJOR_VERSION,SGX_1_9_MINOR_VERSION ),
  94. META_DATA_MAKE_VERSION(SGX_1_5_MAJOR_VERSION,SGX_1_5_MINOR_VERSION ),
  95. 0
  96. };
  97. uint64_t *pmetadata_version = &supported_metadata_version_list[0];
  98. #ifndef SE_SIM
  99. EnclaveCreatorHW *enclave_creator = static_cast<EnclaveCreatorHW *>(get_enclave_creator());
  100. if (!(enclave_creator->is_cpu_edmm()) || !(enclave_creator->is_driver_compatible()))
  101. {
  102. // cannot support EDMM, adjust the possibly highest metadata version supported
  103. pmetadata_version = &supported_metadata_version_list[1];
  104. }
  105. #else
  106. //for simulation, use the metadata of 1.9
  107. pmetadata_version = &supported_metadata_version_list[1];
  108. #endif
  109. //scan multiple metadata list in sgx_metadata section
  110. for (; *pmetadata_version != 0; pmetadata_version++)
  111. {
  112. meta_rva = parser->get_metadata_offset();
  113. //scan multiple metadata list in sgx_metadata section
  114. do {
  115. *metadata = GET_PTR(metadata_t, base_addr, meta_rva);
  116. if(metadata == NULL)
  117. {
  118. return SGX_ERROR_INVALID_METADATA;
  119. }
  120. if((*metadata)->magic_num != METADATA_MAGIC)
  121. break;
  122. //check metadata version
  123. if(*pmetadata_version == (*metadata)->version)
  124. goto find_metadata; //find metadata
  125. if(0 == (*metadata)->size)
  126. {
  127. SE_TRACE(SE_TRACE_ERROR, "ERROR: metadata's size can't be zero.\n");
  128. return SGX_ERROR_INVALID_METADATA;
  129. }
  130. meta_rva += (*metadata)->size; /*goto next metadata offset*/
  131. }while(1);
  132. }
  133. if(*pmetadata_version == 0)
  134. return SGX_ERROR_INVALID_METADATA;
  135. find_metadata:
  136. return (sgx_status_t)get_enclave_creator()->get_misc_attr(sgx_misc_attr, *metadata, NULL, debug);
  137. }
  138. #define MAX_LEN 256
  139. static bool is_SGX_DBG_OPTIN_variable_set()
  140. {
  141. const char sgx_dbg_optin[] = "SGX_DBG_OPTIN";
  142. const char sgx_dbg_optin_expect_val[] = "1";
  143. char *sgx_dbg_optin_val = getenv(sgx_dbg_optin);
  144. if(sgx_dbg_optin_val == NULL)
  145. {
  146. return false;
  147. }
  148. size_t expect_len = strnlen_s(sgx_dbg_optin_expect_val, MAX_LEN);
  149. size_t len = strnlen_s(sgx_dbg_optin_val, MAX_LEN);
  150. if(len != expect_len || strncmp(sgx_dbg_optin_expect_val, sgx_dbg_optin_val, expect_len) != 0)
  151. {
  152. return false;
  153. }
  154. return true;
  155. }
  156. static int __create_enclave(BinParser &parser, uint8_t* base_addr, const metadata_t *metadata, se_file_t& file, const bool debug, SGXLaunchToken *lc, le_prd_css_file_t *prd_css_file, sgx_enclave_id_t *enclave_id, sgx_misc_attribute_t *misc_attr)
  157. {
  158. // The "parser" will be registered into "loader" and "loader" will be registered into "enclave".
  159. // After enclave is created, "parser" and "loader" are not needed any more.
  160. debug_enclave_info_t *debug_info = NULL;
  161. int ret = SGX_SUCCESS;
  162. CLoader loader(base_addr, parser);
  163. ret = loader.load_enclave_ex(lc, debug, metadata, prd_css_file, misc_attr);
  164. if (ret != SGX_SUCCESS)
  165. {
  166. return ret;
  167. }
  168. CEnclave* enclave = new CEnclave(loader);
  169. uint32_t enclave_version = SDK_VERSION_1_5;
  170. // metadata->version has already been validated during load_encalve_ex()
  171. if (metadata->version == META_DATA_MAKE_VERSION(MAJOR_VERSION,MINOR_VERSION))
  172. enclave_version = SDK_VERSION_2_0;
  173. else if (metadata->version == META_DATA_MAKE_VERSION(SGX_1_5_MAJOR_VERSION,SGX_1_5_MINOR_VERSION))
  174. enclave_version = SDK_VERSION_1_5;
  175. // initialize the enclave object
  176. ret = enclave->initialize(file,
  177. loader.get_enclave_id(),
  178. const_cast<void*>(loader.get_start_addr()),
  179. metadata->enclave_size,
  180. metadata->tcs_policy,
  181. enclave_version,
  182. metadata->tcs_min_pool);
  183. if (ret != SGX_SUCCESS)
  184. {
  185. loader.destroy_enclave();
  186. delete enclave; // The `enclave' object owns the `loader' object.
  187. return ret;
  188. }
  189. // It is accurate to get debug flag from secs
  190. enclave->set_dbg_flag(!!(loader.get_secs().attributes.flags & SGX_FLAGS_DEBUG));
  191. debug_info = const_cast<debug_enclave_info_t *>(enclave->get_debug_info());
  192. enclave->set_extra_debug_info(const_cast<secs_t &>(loader.get_secs()));
  193. //add enclave to enclave pool before init_enclave because in simualtion
  194. //mode init_enclave will rely on CEnclavePool to get Enclave instance.
  195. if (FALSE == CEnclavePool::instance()->add_enclave(enclave))
  196. {
  197. loader.destroy_enclave();
  198. delete enclave;
  199. return SGX_ERROR_UNEXPECTED;
  200. }
  201. std::vector<std::pair<tcs_t *, bool>> tcs_list = loader.get_tcs_list();
  202. for (unsigned idx = 0; idx < tcs_list.size(); ++idx)
  203. {
  204. enclave->add_thread(tcs_list[idx].first, tcs_list[idx].second);
  205. SE_TRACE(SE_TRACE_DEBUG, "add tcs %p\n", tcs_list[idx].first);
  206. }
  207. if(debug)
  208. debug_info->enclave_type |= ET_DEBUG;
  209. if (!(get_enclave_creator()->use_se_hw()))
  210. debug_info->enclave_type |= ET_SIM;
  211. if(debug || !(get_enclave_creator()->use_se_hw()))
  212. {
  213. SE_TRACE(SE_TRACE_DEBUG, "Debug enclave. Checking if VTune is profiling or SGX_DBG_OPTIN is set\n");
  214. __itt_init_ittlib(NULL, __itt_group_none);
  215. bool isVTuneProfiling;
  216. if(__itt_get_ittapi_global()->api_initialized && __itt_get_ittapi_global()->lib)
  217. isVTuneProfiling = true;
  218. else
  219. isVTuneProfiling = false;
  220. bool is_SGX_DBG_OPTIN_set = false;
  221. is_SGX_DBG_OPTIN_set = is_SGX_DBG_OPTIN_variable_set();
  222. if (isVTuneProfiling || is_SGX_DBG_OPTIN_set)
  223. {
  224. SE_TRACE(SE_TRACE_DEBUG, "VTune is profiling or SGX_DBG_OPTIN is set\n");
  225. bool thread_updated;
  226. thread_updated = enclave->update_debug_flag(1);
  227. if(thread_updated == false)
  228. {
  229. SE_TRACE(SE_TRACE_DEBUG, "Failed to update debug OPTIN bit\n");
  230. }
  231. else
  232. {
  233. SE_TRACE(SE_TRACE_DEBUG, "Updated debug OPTIN bit\n");
  234. }
  235. if (isVTuneProfiling)
  236. {
  237. uint64_t enclave_start_addr;
  238. uint64_t enclave_end_addr;
  239. const char* enclave_path;
  240. enclave_start_addr = (uint64_t) loader.get_start_addr();
  241. enclave_end_addr = enclave_start_addr + (uint64_t) metadata->enclave_size;
  242. SE_TRACE(SE_TRACE_DEBUG, "Invoking VTune's module mapping API __itt_module_load \n");
  243. SE_TRACE(SE_TRACE_DEBUG, "Enclave_start_addr==0x%llx\n", enclave_start_addr);
  244. SE_TRACE(SE_TRACE_DEBUG, "Enclave_end_addr==0x%llx\n", enclave_end_addr);
  245. enclave_path = (const char*)file.name;
  246. SE_TRACE(SE_TRACE_DEBUG, "Enclave_path==%s\n", enclave_path);
  247. __itt_module_load((void*)enclave_start_addr, (void*) enclave_end_addr, enclave_path);
  248. }
  249. }
  250. else
  251. {
  252. SE_TRACE(SE_TRACE_DEBUG, "VTune is not profiling and SGX_DBG_OPTIN is not set. TCS Debug OPTIN bit not set and API to do module mapping not invoked\n");
  253. }
  254. }
  255. //send debug event to debugger when enclave is debug mode or release mode
  256. //set struct version
  257. debug_info->struct_version = enclave->get_debug_info()->struct_version;
  258. //generate load debug event after EINIT
  259. generate_enclave_debug_event(URTS_EXCEPTION_POSTINITENCLAVE, debug_info);
  260. if (get_enclave_creator()->is_EDMM_supported(loader.get_enclave_id()))
  261. {
  262. layout_t *layout_start = GET_PTR(layout_t, metadata, metadata->dirs[DIR_LAYOUT].offset);
  263. layout_t *layout_end = GET_PTR(layout_t, metadata, metadata->dirs[DIR_LAYOUT].offset + metadata->dirs[DIR_LAYOUT].size);
  264. if (SGX_SUCCESS != (ret = loader.post_init_action(layout_start, layout_end, 0)))
  265. {
  266. SE_TRACE(SE_TRACE_ERROR, "trim range error.\n");
  267. sgx_status_t status = SGX_SUCCESS;
  268. CEnclavePool::instance()->remove_enclave(loader.get_enclave_id(), status);
  269. goto fail;
  270. }
  271. }
  272. //call trts to do some intialization
  273. if(SGX_SUCCESS != (ret = get_enclave_creator()->initialize(loader.get_enclave_id())))
  274. {
  275. sgx_status_t status = SGX_SUCCESS;
  276. CEnclavePool::instance()->remove_enclave(loader.get_enclave_id(), status);
  277. goto fail;
  278. }
  279. if (get_enclave_creator()->is_EDMM_supported(loader.get_enclave_id()))
  280. {
  281. layout_t *layout_start = GET_PTR(layout_t, metadata, metadata->dirs[DIR_LAYOUT].offset);
  282. layout_t *layout_end = GET_PTR(layout_t, metadata, metadata->dirs[DIR_LAYOUT].offset + metadata->dirs[DIR_LAYOUT].size);
  283. if (SGX_SUCCESS != (ret = loader.post_init_action_commit(layout_start, layout_end, 0)))
  284. {
  285. SE_TRACE(SE_TRACE_ERROR, "trim page commit error.\n");
  286. sgx_status_t status = SGX_SUCCESS;
  287. CEnclavePool::instance()->remove_enclave(loader.get_enclave_id(), status);
  288. goto fail;
  289. }
  290. }
  291. //fill tcs mini pool
  292. if (get_enclave_creator()->is_EDMM_supported(loader.get_enclave_id()))
  293. {
  294. ret = enclave->fill_tcs_mini_pool_fn();
  295. if (ret != SGX_SUCCESS)
  296. {
  297. SE_TRACE(SE_TRACE_ERROR, "fill_tcs_mini_pool error.\n");
  298. sgx_status_t status = SGX_SUCCESS;
  299. CEnclavePool::instance()->remove_enclave(loader.get_enclave_id(), status);
  300. goto fail;
  301. }
  302. }
  303. if(SGX_SUCCESS != (ret = loader.set_memory_protection()))
  304. {
  305. sgx_status_t status = SGX_SUCCESS;
  306. CEnclavePool::instance()->remove_enclave(loader.get_enclave_id(), status);
  307. goto fail;
  308. }
  309. *enclave_id = loader.get_enclave_id();
  310. return SGX_SUCCESS;
  311. fail:
  312. loader.destroy_enclave();
  313. delete enclave;
  314. return ret;
  315. }
  316. sgx_status_t _create_enclave(const bool debug, se_file_handle_t pfile, se_file_t& file, le_prd_css_file_t *prd_css_file, sgx_launch_token_t *launch, int *launch_updated, sgx_enclave_id_t *enclave_id, sgx_misc_attribute_t *misc_attr)
  317. {
  318. unsigned int ret = SGX_SUCCESS;
  319. sgx_status_t lt_result = SGX_SUCCESS;
  320. uint32_t file_size = 0;
  321. map_handle_t* mh = NULL;
  322. sgx_misc_attribute_t sgx_misc_attr;
  323. metadata_t *metadata = NULL;
  324. SGXLaunchToken *lc = NULL;
  325. memset(&sgx_misc_attr, 0, sizeof(sgx_misc_attribute_t));
  326. if(NULL == launch || NULL == launch_updated || NULL == enclave_id)
  327. return SGX_ERROR_INVALID_PARAMETER;
  328. #ifndef SE_SIM
  329. ret = validate_platform();
  330. if(ret != SGX_SUCCESS)
  331. return (sgx_status_t)ret;
  332. #endif
  333. mh = map_file(pfile, &file_size);
  334. if (!mh)
  335. return SGX_ERROR_OUT_OF_MEMORY;
  336. PARSER parser(const_cast<uint8_t *>(mh->base_addr), (uint64_t)(file_size));
  337. if(SGX_SUCCESS != (ret = parser.run_parser()))
  338. {
  339. goto clean_return;
  340. }
  341. //Make sure HW uRTS won't load simulation enclave and vice verse.
  342. if(get_enclave_creator()->use_se_hw() != (!parser.get_symbol_rva("g_global_data_sim")))
  343. {
  344. SE_TRACE_WARNING("HW and Simulation mode incompatibility detected. The enclave is linked with the incorrect tRTS library.\n");
  345. ret = SGX_ERROR_MODE_INCOMPATIBLE;
  346. goto clean_return;
  347. }
  348. if(SGX_SUCCESS != (ret = get_metadata(&parser, debug, &metadata, &sgx_misc_attr)))
  349. {
  350. goto clean_return;
  351. }
  352. *launch_updated = FALSE;
  353. lc = new SGXLaunchToken(&metadata->enclave_css, &sgx_misc_attr.secs_attr, launch);
  354. lt_result = lc->update_launch_token(false);
  355. if(SGX_SUCCESS != lt_result)
  356. {
  357. ret = lt_result;
  358. goto clean_return;
  359. }
  360. #ifndef SE_SIM
  361. // Only LE allows the prd_css_file
  362. if(is_le(lc, &metadata->enclave_css) == false && prd_css_file != NULL)
  363. {
  364. ret = SGX_ERROR_INVALID_PARAMETER;
  365. goto clean_return;
  366. }
  367. #endif
  368. // init xave global variables for xsave/xrstor
  369. init_xsave_info();
  370. //Need to set the whole misc_attr instead of just secs_attr.
  371. do {
  372. ret = __create_enclave(parser, mh->base_addr, metadata, file, debug, lc, prd_css_file, enclave_id,
  373. misc_attr);
  374. //SGX_ERROR_ENCLAVE_LOST caused by initializing enclave while power transition occurs
  375. } while(SGX_ERROR_ENCLAVE_LOST == ret);
  376. if(SGX_ERROR_INVALID_CPUSVN == ret)
  377. ret = SGX_ERROR_UNEXPECTED;
  378. if(SE_ERROR_INVALID_LAUNCH_TOKEN == ret)
  379. ret = SGX_ERROR_INVALID_LAUNCH_TOKEN;
  380. // The launch token is updated, so the SE_INVALID_MEASUREMENT is only caused by signature.
  381. if(SE_ERROR_INVALID_MEASUREMENT == ret)
  382. ret = SGX_ERROR_INVALID_SIGNATURE;
  383. // The launch token is updated, so the SE_ERROR_INVALID_ISVSVNLE means user needs to update the LE image
  384. if (SE_ERROR_INVALID_ISVSVNLE == ret)
  385. ret = SGX_ERROR_UPDATE_NEEDED;
  386. if(SGX_SUCCESS != ret)
  387. goto clean_return;
  388. else if(lc->is_launch_updated())
  389. {
  390. *launch_updated = TRUE;
  391. ret = lc->get_launch_token(launch);
  392. }
  393. clean_return:
  394. if(mh != NULL)
  395. unmap_file(mh);
  396. if(lc != NULL)
  397. delete lc;
  398. return (sgx_status_t)ret;
  399. }
  400. extern "C" sgx_status_t sgx_destroy_enclave(const sgx_enclave_id_t enclave_id)
  401. {
  402. {
  403. CEnclave* enclave = CEnclavePool::instance()->ref_enclave(enclave_id);
  404. if(enclave)
  405. {
  406. debug_enclave_info_t *debug_info = const_cast<debug_enclave_info_t *>(enclave->get_debug_info());
  407. generate_enclave_debug_event(URTS_EXCEPTION_PREREMOVEENCLAVE, debug_info);
  408. enclave->ecall(ECMD_UNINIT_ENCLAVE, NULL, NULL);
  409. CEnclavePool::instance()->unref_enclave(enclave);
  410. }
  411. }
  412. sgx_status_t status = SGX_SUCCESS;
  413. CEnclave* enclave = CEnclavePool::instance()->remove_enclave(enclave_id, status);
  414. if (enclave)
  415. {
  416. delete enclave;
  417. }
  418. return status;
  419. }
  420. #endif