enclave_creator_hw_com.cpp 8.8 KB

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