init_enclave.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /**
  32. * File: init_enclave.cpp
  33. * Description:
  34. * Initialize enclave by rebasing the image to the enclave base
  35. */
  36. #include <string.h>
  37. #include "thread_data.h"
  38. #include "global_data.h"
  39. #include "util.h"
  40. #include "xsave.h"
  41. #include "sgx_trts.h"
  42. #include "sgx_lfence.h"
  43. #include "init_optimized_lib.h"
  44. #include "trts_internal.h"
  45. #include "linux/elf_parser.h"
  46. #include "rts.h"
  47. #include "trts_util.h"
  48. #include "se_memcpy.h"
  49. // The global cpu feature bits from uRTS
  50. uint64_t g_cpu_feature_indicator = 0;
  51. int EDMM_supported = 0;
  52. sdk_version_t g_sdk_version = SDK_VERSION_1_5;
  53. const volatile global_data_t g_global_data __attribute__((section(".niprod"))) = {1, 2, 3, 4,
  54. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0}, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0, {{{0, 0, 0, 0, 0, 0, 0}}}};
  55. uint32_t g_enclave_state __attribute__((section(".nipd"))) = ENCLAVE_INIT_NOT_STARTED;
  56. extern "C" {
  57. uintptr_t __stack_chk_guard = 0;
  58. #define __weak_alias(alias,sym) \
  59. __asm__(".weak " __STRING(alias) " ; " \
  60. __STRING(alias) " = " __STRING(sym))
  61. __weak_alias(__intel_security_cookie, __stack_chk_guard);
  62. }
  63. extern sgx_status_t pcl_entry(void* enclave_base,void* ms) __attribute__((weak));
  64. extern "C" int init_enclave(void *enclave_base, void *ms) __attribute__((section(".nipx")));
  65. // init_enclave()
  66. // Initialize enclave.
  67. // Parameters:
  68. // [IN] enclave_base - the enclave base address
  69. // [IN] ms - the marshalling structure passed by uRTS
  70. // Return Value:
  71. // 0 - success
  72. // -1 - fail
  73. //
  74. extern "C" int init_enclave(void *enclave_base, void *ms)
  75. {
  76. if(enclave_base == NULL || ms == NULL)
  77. {
  78. return -1;
  79. }
  80. if(NULL != pcl_entry)
  81. {
  82. // LFENCE before pcl_entry
  83. sgx_lfence();
  84. system_features_t * csi = (system_features_t *)ms;
  85. if(NULL == csi->sealed_key)
  86. {
  87. return -1;
  88. }
  89. sgx_status_t ret = pcl_entry(enclave_base, csi->sealed_key);
  90. if(SGX_SUCCESS != ret)
  91. {
  92. return -1;
  93. }
  94. }
  95. // relocation
  96. if(0 != relocate_enclave(enclave_base))
  97. {
  98. return -1;
  99. }
  100. // Check if the ms is outside the enclave.
  101. // sgx_is_outside_enclave() should be placed after relocate_enclave()
  102. system_features_t *info = (system_features_t *)ms;
  103. if(!sgx_is_outside_enclave(info, sizeof(system_features_t)))
  104. {
  105. return -1;
  106. }
  107. sgx_lfence();
  108. const system_features_t sys_features = *info;
  109. g_sdk_version = sys_features.version;
  110. if (g_sdk_version == SDK_VERSION_1_5)
  111. {
  112. EDMM_supported = 0;
  113. }
  114. else if (g_sdk_version >= SDK_VERSION_2_0)
  115. {
  116. EDMM_supported = feature_supported((const uint64_t *)sys_features.system_feature_set, 0);
  117. }
  118. else
  119. {
  120. return -1;
  121. }
  122. if (heap_init(get_heap_base(), get_heap_size(), get_heap_min_size(), EDMM_supported) != SGX_SUCCESS)
  123. return -1;
  124. // xsave
  125. uint64_t xfrm = get_xfeature_state();
  126. // optimized libs
  127. if (SDK_VERSION_2_0 < g_sdk_version) {
  128. if(0 != init_optimized_libs((const uint64_t)sys_features.cpu_features, (uint32_t*)sys_features.cpuinfo_table, xfrm))
  129. {
  130. return -1;
  131. }
  132. } else {
  133. if(0 != init_optimized_libs((const uint64_t)sys_features.cpu_features, NULL, xfrm))
  134. {
  135. return -1;
  136. }
  137. }
  138. if(SGX_SUCCESS != sgx_read_rand((unsigned char*)&__stack_chk_guard,
  139. sizeof(__stack_chk_guard)))
  140. {
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. #ifndef SE_SIM
  146. int accept_post_remove(const volatile layout_t *layout_start, const volatile layout_t *layout_end, size_t offset);
  147. #endif
  148. sgx_status_t do_init_enclave(void *ms, void *tcs)
  149. {
  150. #ifdef SE_SIM
  151. UNUSED(tcs);
  152. #endif
  153. void *enclave_base = get_enclave_base();
  154. if(ENCLAVE_INIT_NOT_STARTED != lock_enclave())
  155. {
  156. return SGX_ERROR_UNEXPECTED;
  157. }
  158. if(0 != init_enclave(enclave_base, ms))
  159. {
  160. return SGX_ERROR_UNEXPECTED;
  161. }
  162. #ifndef SE_SIM
  163. if (SGX_SUCCESS != do_init_thread(tcs, true))
  164. {
  165. return SGX_ERROR_UNEXPECTED;
  166. }
  167. /* for EDMM, we need to accept the trimming of the POST_REMOVE pages. */
  168. if (EDMM_supported)
  169. {
  170. if (0 != accept_post_remove(&g_global_data.layout_table[0], &g_global_data.layout_table[0] + g_global_data.layout_entry_num, 0))
  171. return SGX_ERROR_UNEXPECTED;
  172. size_t heap_min_size = get_heap_min_size();
  173. memset_s(GET_PTR(void, enclave_base, g_global_data.heap_offset), heap_min_size, 0, heap_min_size);
  174. }
  175. else
  176. #endif
  177. {
  178. memset_s(GET_PTR(void, enclave_base, g_global_data.heap_offset), g_global_data.heap_size, 0, g_global_data.heap_size);
  179. }
  180. g_enclave_state = ENCLAVE_INIT_DONE;
  181. return SGX_SUCCESS;
  182. }