init_enclave.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /**
  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 "init_optimized_lib.h"
  43. #include "trts_internal.h"
  44. #include "linux/elf_parser.h"
  45. #include "rts.h"
  46. #include "trts_util.h"
  47. #include "se_memcpy.h"
  48. // The global cpu feature bits from uRTS
  49. uint64_t g_cpu_feature_indicator = 0;
  50. int EDMM_supported = 0;
  51. sdk_version_t g_sdk_version = SDK_VERSION_1_5;
  52. const volatile global_data_t g_global_data = {1, 2, 3, 4,
  53. {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}}}};
  54. uint32_t g_enclave_state = ENCLAVE_INIT_NOT_STARTED;
  55. extern "C" {
  56. uintptr_t __stack_chk_guard = 0;
  57. #define __weak_alias(alias,sym) \
  58. __asm__(".weak " __STRING(alias) " ; " \
  59. __STRING(alias) " = " __STRING(sym))
  60. __weak_alias(__intel_security_cookie, __stack_chk_guard);
  61. }
  62. // init_enclave()
  63. // Initialize enclave.
  64. // Parameters:
  65. // [IN] enclave_base - the enclave base address
  66. // [IN] ms - the marshalling structure passed by uRTS
  67. // Return Value:
  68. // 0 - success
  69. // -1 - fail
  70. //
  71. extern "C" int init_enclave(void *enclave_base, void *ms)
  72. {
  73. if(enclave_base == NULL || ms == NULL)
  74. {
  75. return -1;
  76. }
  77. // relocation
  78. if(0 != relocate_enclave(enclave_base))
  79. {
  80. return -1;
  81. }
  82. // Check if the ms is outside the enclave.
  83. // sgx_is_outside_enclave() should be placed after relocate_enclave()
  84. system_features_t *info = (system_features_t *)ms;
  85. if(!sgx_is_outside_enclave(info, sizeof(system_features_t)))
  86. {
  87. return -1;
  88. }
  89. const system_features_t sys_features = *info;
  90. g_sdk_version = sys_features.version;
  91. if (g_sdk_version == SDK_VERSION_1_5)
  92. {
  93. EDMM_supported = 0;
  94. }
  95. else if (g_sdk_version == SDK_VERSION_2_0)
  96. {
  97. EDMM_supported = feature_supported((const uint64_t *)sys_features.system_feature_set, 0);
  98. }
  99. else
  100. {
  101. return -1;
  102. }
  103. if (heap_init(get_heap_base(), get_heap_size(), get_heap_min_size(), EDMM_supported) != SGX_SUCCESS)
  104. return -1;
  105. // xsave
  106. uint64_t xfrm = get_xfeature_state();
  107. // optimized libs
  108. if(0 != init_optimized_libs((const uint64_t)sys_features.cpu_features, xfrm))
  109. {
  110. return -1;
  111. }
  112. if(SGX_SUCCESS != sgx_read_rand((unsigned char*)&__stack_chk_guard,
  113. sizeof(__stack_chk_guard)))
  114. {
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. #ifndef SE_SIM
  120. int accept_post_remove(const volatile layout_t *layout_start, const volatile layout_t *layout_end, size_t offset);
  121. #endif
  122. sgx_status_t do_init_enclave(void *ms)
  123. {
  124. void *enclave_base = get_enclave_base();
  125. if(ENCLAVE_INIT_NOT_STARTED != lock_enclave())
  126. {
  127. return SGX_ERROR_UNEXPECTED;
  128. }
  129. if(0 != init_enclave(enclave_base, ms))
  130. {
  131. return SGX_ERROR_UNEXPECTED;
  132. }
  133. #ifndef SE_SIM
  134. /* for EDMM, we need to accept the trimming of the POST_REMOVE pages. */
  135. if (EDMM_supported)
  136. {
  137. if (0 != accept_post_remove(&g_global_data.layout_table[0], &g_global_data.layout_table[0] + g_global_data.layout_entry_num, 0))
  138. return SGX_ERROR_UNEXPECTED;
  139. size_t heap_min_size = get_heap_min_size();
  140. memset_s(GET_PTR(void, enclave_base, g_global_data.heap_offset), heap_min_size, 0, heap_min_size);
  141. }
  142. else
  143. #endif
  144. {
  145. memset_s(GET_PTR(void, enclave_base, g_global_data.heap_offset), g_global_data.heap_size, 0, g_global_data.heap_size);
  146. }
  147. g_enclave_state = ENCLAVE_INIT_DONE;
  148. return SGX_SUCCESS;
  149. }