enclave_creator_hw_com.cpp 8.6 KB

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