enclave_creator_sim.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (C) 2011-2016 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 "enclave_creator_sim.h"
  32. #include "enclave_mngr.h"
  33. #include "se_detect.h"
  34. #include "driver_api.h"
  35. #include "enclave.h"
  36. #include "rts.h"
  37. #include "routine.h"
  38. #include "cpu_features.h"
  39. #include "se_error_internal.h"
  40. #include "util.h"
  41. #include "cpusvn_util.h"
  42. #include "rts_sim.h"
  43. #include <assert.h>
  44. #include <time.h>
  45. EnclaveCreator* g_enclave_creator = new EnclaveCreatorSim();
  46. int EnclaveCreatorSim::create_enclave(secs_t *secs, sgx_enclave_id_t *enclave_id, void **start_addr, bool ae)
  47. {
  48. UNUSED(ae);
  49. return ::create_enclave(secs, enclave_id, start_addr);
  50. }
  51. int EnclaveCreatorSim::add_enclave_page(sgx_enclave_id_t enclave_id, void *src, uint64_t offset, const sec_info_t &sinfo, uint32_t attr)
  52. {
  53. void* source = src;
  54. uint8_t color_page[SE_PAGE_SIZE];
  55. if(!source)
  56. {
  57. memset(color_page, 0, SE_PAGE_SIZE);
  58. source = reinterpret_cast<void*>(&color_page);
  59. }
  60. return ::add_enclave_page(enclave_id, source, (size_t)offset, sinfo, attr);
  61. }
  62. int EnclaveCreatorSim::init_enclave(sgx_enclave_id_t enclave_id, enclave_css_t *enclave_css, SGXLaunchToken *lc, le_prd_css_file_t *prd_css_file)
  63. {
  64. UNUSED(prd_css_file);
  65. sgx_launch_token_t token;
  66. memset(token, 0, sizeof(sgx_launch_token_t));
  67. int ret = lc->get_launch_token(&token);
  68. if(ret != SGX_SUCCESS)
  69. return ret;
  70. return ::init_enclave(enclave_id, enclave_css, reinterpret_cast<token_t *>(token));
  71. }
  72. int EnclaveCreatorSim::get_misc_attr(sgx_misc_attribute_t *sgx_misc_attr, metadata_t *metadata, SGXLaunchToken * const lc, uint32_t debug_flag)
  73. {
  74. sgx_attributes_t *required_attr;
  75. enclave_css_t *enclave_css;
  76. sgx_attributes_t *secs_attr;
  77. uint64_t xcr0 = 0;
  78. assert(sgx_misc_attr != NULL);
  79. assert(metadata != NULL);
  80. required_attr = &metadata->attributes;
  81. enclave_css = &metadata->enclave_css;
  82. secs_attr = &sgx_misc_attr->secs_attr;
  83. // Make sure that FP/SSE is set.
  84. if (SGX_XFRM_LEGACY != (required_attr->xfrm & SGX_XFRM_LEGACY))
  85. {
  86. SE_TRACE(SE_TRACE_WARNING, "FP/SSE are must-have attributes\n");
  87. return SGX_ERROR_INVALID_ATTRIBUTE;
  88. }
  89. if (debug_flag)
  90. {
  91. //If enclave is signed as product enclave, but is launched as debug enclave, we need report specific error code.
  92. if((enclave_css->body.attribute_mask.flags & SGX_FLAGS_DEBUG)
  93. && !(enclave_css->body.attributes.flags & SGX_FLAGS_DEBUG)
  94. )
  95. {
  96. return SGX_ERROR_NDEBUG_ENCLAVE;
  97. }
  98. required_attr->flags |= SGX_FLAGS_DEBUG;
  99. }
  100. else
  101. required_attr->flags &= (~SGX_FLAGS_DEBUG);
  102. secs_attr->flags = required_attr->flags;
  103. if (! try_read_xcr0(&xcr0))
  104. {
  105. // read_xcr0() failed
  106. secs_attr->xfrm = SGX_XFRM_LEGACY;
  107. }
  108. else
  109. {
  110. secs_attr->xfrm = xcr0 & required_attr->xfrm;
  111. }
  112. // Check the signature structure xfrm attribute restrictions.
  113. if((enclave_css->body.attribute_mask.xfrm & secs_attr->xfrm)
  114. != (enclave_css->body.attribute_mask.xfrm & enclave_css->body.attributes.xfrm))
  115. {
  116. SE_TRACE(SE_TRACE_WARNING, "secs attributes.xfrm does NOT match signature attributes.xfrm\n");
  117. return SGX_ERROR_INVALID_ATTRIBUTE;
  118. }
  119. // Check the signature structure flags attribute restrictions.
  120. if((enclave_css->body.attribute_mask.flags & secs_attr->flags)
  121. != (enclave_css->body.attribute_mask.flags & enclave_css->body.attributes.flags))
  122. {
  123. SE_TRACE(SE_TRACE_WARNING, "secs attributes.flag does NOT match signature attributes.flag\n");
  124. return SGX_ERROR_INVALID_ATTRIBUTE;
  125. }
  126. if(lc != NULL)
  127. {
  128. sgx_launch_token_t token;
  129. memset(&token, 0, sizeof(token));
  130. if(lc->get_launch_token(&token) != SGX_SUCCESS)
  131. return SGX_ERROR_UNEXPECTED;
  132. token_t *launch = (token_t *)token;
  133. if( 1 == launch->body.valid)
  134. {
  135. // Debug launch enclave cannot launch production enclave
  136. if( !(secs_attr->flags & SGX_FLAGS_DEBUG)
  137. && (launch->attributes_le.flags & SGX_FLAGS_DEBUG) )
  138. {
  139. SE_TRACE(SE_TRACE_WARNING, "secs attributes is non-debug, \n");
  140. return SE_ERROR_INVALID_LAUNCH_TOKEN;
  141. }
  142. // Verify attributes in lictoken are the same as the enclave
  143. if(memcmp(&launch->body.attributes, secs_attr, sizeof(sgx_attributes_t)))
  144. {
  145. SE_TRACE(SE_TRACE_WARNING, "secs attributes does NOT match launch token attributes\n");
  146. return SGX_ERROR_INVALID_ATTRIBUTE;
  147. }
  148. }
  149. }
  150. return SGX_SUCCESS;
  151. }
  152. int EnclaveCreatorSim::destroy_enclave(sgx_enclave_id_t enclave_id, uint64_t enclave_size)
  153. {
  154. UNUSED(enclave_size);
  155. CEnclave *enclave = CEnclavePool::instance()->get_enclave(enclave_id);
  156. if(enclave == NULL)
  157. return SGX_ERROR_INVALID_ENCLAVE_ID;
  158. return ::destroy_enclave(enclave_id);
  159. }
  160. int EnclaveCreatorSim::initialize(sgx_enclave_id_t enclave_id)
  161. {
  162. CEnclave *enclave = CEnclavePool::instance()->get_enclave(enclave_id);
  163. if(enclave == NULL)
  164. {
  165. SE_TRACE(SE_TRACE_WARNING, "enclave (id = %llu) not found.\n", enclave_id);
  166. return SGX_ERROR_INVALID_ENCLAVE_ID;
  167. }
  168. // Save the SECS address (EGETKEY/EREPORT needs to know SECS).
  169. CEnclaveMngr *mngr = CEnclaveMngr::get_instance();
  170. CEnclaveSim *ce = mngr->get_enclave(enclave_id);
  171. if (ce == NULL)
  172. {
  173. SE_TRACE(SE_TRACE_WARNING, "enclave (id = %llu) not found.\n", enclave_id);
  174. return SGX_ERROR_INVALID_ENCLAVE_ID;
  175. }
  176. global_data_sim_t *global_data_sim_ptr = (global_data_sim_t *)enclave->get_symbol_address("g_global_data_sim");
  177. //We have check the symbol of "g_global_data_sim" in urts_com.h::_create_enclave(), so here global_data_sim_ptr won't be NULL.
  178. assert(global_data_sim_ptr != NULL);
  179. // Initialize the `seed' to `g_global_data_sim'.
  180. global_data_sim_ptr->seed = (uint32_t)time(NULL);
  181. global_data_sim_ptr->secs_ptr = ce->get_secs();
  182. sgx_cpu_svn_t temp_cpusvn = {{0}};
  183. int status = get_cpusvn(&temp_cpusvn);
  184. assert(status == SGX_SUCCESS);
  185. memcpy_s(&(global_data_sim_ptr->cpusvn_sim),sizeof(global_data_sim_ptr->cpusvn_sim), &temp_cpusvn, sizeof(temp_cpusvn));
  186. //Since CPUID instruction is NOT supported within enclave, we emuerate the cpu features here and send to tRTS.
  187. cpu_sdk_info_t info;
  188. info.cpu_features = 0;
  189. get_cpu_features(&info.cpu_features);
  190. info.version = SDK_VERSION_1_5;
  191. status = enclave->ecall(ECMD_INIT_ENCLAVE, NULL, reinterpret_cast<void *>(&info));
  192. //free the tcs used by initialization;
  193. enclave->get_thread_pool()->reset();
  194. if(SGX_SUCCESS == status)
  195. {
  196. return SGX_SUCCESS;
  197. }
  198. else
  199. {
  200. SE_TRACE(SE_TRACE_WARNING, "initialize enclave failed\n");
  201. return SGX_ERROR_UNEXPECTED;
  202. }
  203. }
  204. bool EnclaveCreatorSim::use_se_hw() const
  205. {
  206. return false;
  207. }
  208. bool EnclaveCreatorSim::get_plat_cap(sgx_misc_attribute_t *se_attr)
  209. {
  210. UNUSED(se_attr);
  211. return false;
  212. }