enclave_creator_hw_com.cpp 8.9 KB

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