urts_com.h 18 KB

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