init_enclave.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 = {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 = 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. // init_enclave()
  64. // Initialize enclave.
  65. // Parameters:
  66. // [IN] enclave_base - the enclave base address
  67. // [IN] ms - the marshalling structure passed by uRTS
  68. // Return Value:
  69. // 0 - success
  70. // -1 - fail
  71. //
  72. extern "C" int init_enclave(void *enclave_base, void *ms)
  73. {
  74. if(enclave_base == NULL || ms == NULL)
  75. {
  76. return -1;
  77. }
  78. // relocation
  79. if(0 != relocate_enclave(enclave_base))
  80. {
  81. return -1;
  82. }
  83. // Check if the ms is outside the enclave.
  84. // sgx_is_outside_enclave() should be placed after relocate_enclave()
  85. system_features_t *info = (system_features_t *)ms;
  86. if(!sgx_is_outside_enclave(info, sizeof(system_features_t)))
  87. {
  88. return -1;
  89. }
  90. sgx_lfence();
  91. const system_features_t sys_features = *info;
  92. g_sdk_version = sys_features.version;
  93. if (g_sdk_version == SDK_VERSION_1_5)
  94. {
  95. EDMM_supported = 0;
  96. }
  97. else if (g_sdk_version >= SDK_VERSION_2_0)
  98. {
  99. EDMM_supported = feature_supported((const uint64_t *)sys_features.system_feature_set, 0);
  100. }
  101. else
  102. {
  103. return -1;
  104. }
  105. if (heap_init(get_heap_base(), get_heap_size(), get_heap_min_size(), EDMM_supported) != SGX_SUCCESS)
  106. return -1;
  107. // xsave
  108. uint64_t xfrm = get_xfeature_state();
  109. // optimized libs
  110. if (SDK_VERSION_2_0 < g_sdk_version) {
  111. if(0 != init_optimized_libs((const uint64_t)sys_features.cpu_features, (uint32_t*)sys_features.cpuinfo_table, xfrm))
  112. {
  113. return -1;
  114. }
  115. } else {
  116. if(0 != init_optimized_libs((const uint64_t)sys_features.cpu_features, NULL, xfrm))
  117. {
  118. return -1;
  119. }
  120. }
  121. if(SGX_SUCCESS != sgx_read_rand((unsigned char*)&__stack_chk_guard,
  122. sizeof(__stack_chk_guard)))
  123. {
  124. return -1;
  125. }
  126. return 0;
  127. }
  128. #ifndef SE_SIM
  129. int accept_post_remove(const volatile layout_t *layout_start, const volatile layout_t *layout_end, size_t offset);
  130. #endif
  131. sgx_status_t do_init_enclave(void *ms, void *tcs)
  132. {
  133. #ifdef SE_SIM
  134. UNUSED(tcs);
  135. #endif
  136. void *enclave_base = get_enclave_base();
  137. if(ENCLAVE_INIT_NOT_STARTED != lock_enclave())
  138. {
  139. return SGX_ERROR_UNEXPECTED;
  140. }
  141. if(0 != init_enclave(enclave_base, ms))
  142. {
  143. return SGX_ERROR_UNEXPECTED;
  144. }
  145. #ifndef SE_SIM
  146. thread_data_t *thread_data = GET_PTR(thread_data_t, tcs, g_global_data.td_template.self_addr);
  147. if (thread_data == NULL)
  148. {
  149. return SGX_ERROR_UNEXPECTED;
  150. }
  151. do_init_thread(tcs);
  152. thread_data->flags = SGX_UTILITY_THREAD;
  153. /* for EDMM, we need to accept the trimming of the POST_REMOVE pages. */
  154. if (EDMM_supported)
  155. {
  156. if (0 != accept_post_remove(&g_global_data.layout_table[0], &g_global_data.layout_table[0] + g_global_data.layout_entry_num, 0))
  157. return SGX_ERROR_UNEXPECTED;
  158. size_t heap_min_size = get_heap_min_size();
  159. memset_s(GET_PTR(void, enclave_base, g_global_data.heap_offset), heap_min_size, 0, heap_min_size);
  160. }
  161. else
  162. #endif
  163. {
  164. memset_s(GET_PTR(void, enclave_base, g_global_data.heap_offset), g_global_data.heap_size, 0, g_global_data.heap_size);
  165. }
  166. g_enclave_state = ENCLAVE_INIT_DONE;
  167. return SGX_SUCCESS;
  168. }