enclave_creator_hw_com.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "enclave.h"
  32. #include "rts.h"
  33. #include "routine.h"
  34. #include "cpu_features.h"
  35. #include "enclave_creator_hw.h"
  36. #include "se_error_internal.h"
  37. #include "prd_css_util.h"
  38. #include "se_memcpy.h"
  39. #define EDMM_ENABLE_BIT 0x1ULL
  40. bool EnclaveCreatorHW::use_se_hw() const
  41. {
  42. return true;
  43. }
  44. int EnclaveCreatorHW::initialize(sgx_enclave_id_t enclave_id)
  45. {
  46. system_features_t info;
  47. info.system_feature_set[0] = (uint64_t)1 << SYS_FEATURE_MSb;
  48. CEnclave *enclave= CEnclavePool::instance()->get_enclave(enclave_id);
  49. if(enclave == NULL)
  50. return SGX_ERROR_INVALID_ENCLAVE_ID;
  51. //Since CPUID instruction is NOT supported within enclave, we enumerate the cpu features here and send to tRTS.
  52. info.cpu_features = 0;
  53. memset(info.cpuinfo_table, 0, sizeof(info.cpuinfo_table));
  54. get_cpu_features(&info.cpu_features, (uint32_t*)info.cpuinfo_table);
  55. info.version = (sdk_version_t)MIN((uint32_t)SDK_VERSION_2_1, enclave->get_enclave_version());
  56. if (is_EDMM_supported(enclave_id))
  57. info.system_feature_set[0] |= EDMM_ENABLE_BIT;
  58. int status = enclave->ecall(ECMD_INIT_ENCLAVE, NULL, reinterpret_cast<void *>(&info));
  59. //free the tcs used by initialization;
  60. enclave->get_thread_pool()->reset();
  61. //Enclave initialization may fail caused by power transition.
  62. //The upper layer code will re-create enclave based on SGX_ERROR_ENCLAVE_LOST.
  63. if(SGX_SUCCESS == status || SGX_ERROR_ENCLAVE_LOST == status)
  64. {
  65. return status;
  66. }
  67. else
  68. {
  69. //For other error code, may be caused by tRTS bug, or caused by attacker,
  70. //so we just return SGX_ERROR_UNEXPECTED.
  71. SE_TRACE(SE_TRACE_WARNING, "initialize enclave failed\n");
  72. return SGX_ERROR_UNEXPECTED;
  73. }
  74. }
  75. int EnclaveCreatorHW::get_misc_attr(sgx_misc_attribute_t *sgx_misc_attr, metadata_t *metadata, SGXLaunchToken * const lc, uint32_t debug_flag)
  76. {
  77. sgx_attributes_t *required_attr = &metadata->attributes;
  78. enclave_css_t *enclave_css = &metadata->enclave_css;
  79. sgx_attributes_t *secs_attr = &sgx_misc_attr->secs_attr;
  80. //fp, sse must be set.
  81. uint64_t tmp = required_attr->xfrm & SGX_XFRM_LEGACY;
  82. if(SGX_XFRM_LEGACY != tmp)
  83. {
  84. SE_TRACE(SE_TRACE_WARNING, "fp/sse attributes is a must in attributes\n");
  85. return SGX_ERROR_INVALID_ATTRIBUTE;
  86. }
  87. //step 1, set enclave properties
  88. sgx_misc_attribute_t se_cap;
  89. if(!get_plat_cap(&se_cap))
  90. return SGX_ERROR_NO_DEVICE;
  91. if(debug_flag)
  92. {
  93. //If enclave is signed as product enclave, but is launched as debug enclave, we need report specific error code.
  94. if((enclave_css->body.attribute_mask.flags & SGX_FLAGS_DEBUG)
  95. && !(enclave_css->body.attributes.flags & SGX_FLAGS_DEBUG)
  96. )
  97. {
  98. return SGX_ERROR_NDEBUG_ENCLAVE;
  99. }
  100. required_attr->flags |= SGX_FLAGS_DEBUG;
  101. }
  102. else
  103. required_attr->flags &= (~SGX_FLAGS_DEBUG);
  104. secs_attr->flags = required_attr->flags & se_cap.secs_attr.flags;
  105. secs_attr->xfrm = required_attr->xfrm & se_cap.secs_attr.xfrm;
  106. //step 3, evaluate the encalve attributes in secs.
  107. //check the signature structure xfrm attribute restrictions.
  108. if((enclave_css->body.attribute_mask.xfrm & secs_attr->xfrm)
  109. != (enclave_css->body.attribute_mask.xfrm & enclave_css->body.attributes.xfrm))
  110. {
  111. SE_TRACE(SE_TRACE_WARNING, "secs attributes.xfrm does NOT match signature attributes.xfrm\n");
  112. return SGX_ERROR_INVALID_ATTRIBUTE;
  113. }
  114. //Debug bit has been checked before. For other attributes, check the signature structure flags attribute restrictions.
  115. if((enclave_css->body.attribute_mask.flags & secs_attr->flags)
  116. != (enclave_css->body.attribute_mask.flags & enclave_css->body.attributes.flags))
  117. {
  118. SE_TRACE(SE_TRACE_WARNING, "secs attributes.flag does NOT match signature attributes.flag\n");
  119. return SGX_ERROR_INVALID_ATTRIBUTE;
  120. }
  121. // Check misc_select/misc_mask
  122. // enclave_css->body.misc_select & enclave_css->body.misc_mask must be a subset of se_cap.misc_select
  123. if(~(se_cap.misc_select) & (enclave_css->body.misc_select & enclave_css->body.misc_mask))
  124. return SGX_ERROR_INVALID_MISC;
  125. // try to use maximum ablity of cpu
  126. sgx_misc_attr->misc_select = se_cap.misc_select & enclave_css->body.misc_select;
  127. if(lc != NULL)
  128. {
  129. // Read launch token from lc
  130. sgx_launch_token_t token;
  131. memset(&token, 0, sizeof(token));
  132. if(lc->get_launch_token(&token) != SGX_SUCCESS)
  133. return SGX_ERROR_UNEXPECTED;
  134. token_t *launch = (token_t *)token;
  135. if(1 == launch->body.valid)
  136. {
  137. //debug launch enclave cannot launch production enclave
  138. if( !(secs_attr->flags & SGX_FLAGS_DEBUG)
  139. && (launch->attributes_le.flags & SGX_FLAGS_DEBUG) )
  140. {
  141. SE_TRACE(SE_TRACE_WARNING, "secs attributes is non-debug, \n");
  142. return SE_ERROR_INVALID_LAUNCH_TOKEN;
  143. }
  144. //verify attributes in lictoken are the same as the enclave
  145. if(memcmp(&launch->body.attributes, secs_attr, sizeof(sgx_attributes_t)))
  146. {
  147. SE_TRACE(SE_TRACE_WARNING, "secs attributes does NOT match launch token attributes\n");
  148. return SGX_ERROR_INVALID_ATTRIBUTE;
  149. }
  150. }
  151. }
  152. return SGX_SUCCESS;
  153. }
  154. int EnclaveCreatorHW::init_enclave(sgx_enclave_id_t enclave_id, enclave_css_t *enclave_css, SGXLaunchToken * lc, le_prd_css_file_t *prd_css_file)
  155. {
  156. unsigned int ret = 0;
  157. sgx_launch_token_t token;
  158. memset(token, 0, sizeof(sgx_launch_token_t));
  159. enclave_css_t css;
  160. memcpy_s(&css, sizeof(enclave_css_t), enclave_css, sizeof(enclave_css_t));
  161. for(int i = 0; i < 2; i++)
  162. {
  163. if(SGX_SUCCESS != (ret = lc->get_launch_token(&token)))
  164. return ret;
  165. ret = try_init_enclave(enclave_id, &css, reinterpret_cast<token_t *>(token));
  166. if(i > 0)
  167. return ret;
  168. if(true == is_le(lc, &css))
  169. {
  170. // LE is loaded with the interface sgx_create_le.
  171. // Read the input prd css file and use it to init again.
  172. if(SGX_ERROR_INVALID_ATTRIBUTE == ret && prd_css_file != NULL) {
  173. if((ret = read_prd_css(prd_css_file->prd_css_name, &css)) != SGX_SUCCESS)
  174. {
  175. return ret;
  176. }
  177. prd_css_file->is_used = true;
  178. continue;
  179. }
  180. // LE is loaded with the normal interface, or LE is loaded with sgx_create_le but EINIT returns other error code
  181. // No need to get launch token and retry, so just return error code.
  182. return ret;
  183. }
  184. //If current launch token does NOT match the platform, then update the launch token.
  185. //If the hash of signer (public key) in signature does not match launch token, EINIT will return SE_INVALID_MEASUREMENT
  186. else if(!lc->is_launch_updated() && (SE_ERROR_INVALID_LAUNCH_TOKEN == ret || SGX_ERROR_INVALID_CPUSVN == ret || SE_ERROR_INVALID_MEASUREMENT == ret || SE_ERROR_INVALID_ISVSVNLE == ret))
  187. {
  188. if(SGX_SUCCESS != (ret = lc->update_launch_token(true)))
  189. {
  190. return ret;
  191. }
  192. else
  193. {
  194. continue;
  195. }
  196. }
  197. else
  198. break;
  199. }
  200. return ret;
  201. }