init_enclave.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  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. // The global cpu feature bits from uRTS
  47. uint64_t g_cpu_feature_indicator = 0;
  48. const volatile global_data_t g_global_data = {1, 2, 3, 4, 0,
  49. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0}}};
  50. uint32_t g_enclave_state = ENCLAVE_INIT_NOT_STARTED;
  51. extern "C" {
  52. uintptr_t __stack_chk_guard = 0;
  53. #define __weak_alias(alias,sym) \
  54. __asm__(".weak " __STRING(alias) " ; " \
  55. __STRING(alias) " = " __STRING(sym))
  56. __weak_alias(__intel_security_cookie, __stack_chk_guard);
  57. }
  58. // init_enclave()
  59. // Initialize enclave.
  60. // Parameters:
  61. // [IN] enclave_base - the enclave base address
  62. // [IN] ms - the marshalling structure passed by uRTS
  63. // Return Value:
  64. // 0 - success
  65. // -1 - fail
  66. //
  67. extern "C" int init_enclave(void *enclave_base, void *ms)
  68. {
  69. if(enclave_base == NULL || ms == NULL)
  70. {
  71. return -1;
  72. }
  73. // relocation
  74. if(0 != relocate_enclave(enclave_base))
  75. {
  76. return -1;
  77. }
  78. // Check if the ms is outside the enclave.
  79. // sgx_is_outside_enclave() should be placed after relocate_enclave()
  80. cpu_sdk_info_t *info = (cpu_sdk_info_t *)ms;
  81. if(!sgx_is_outside_enclave(info, sizeof(cpu_sdk_info_t)))
  82. {
  83. return -1;
  84. }
  85. const sdk_version_t sdk_version = info->version;
  86. const uint64_t cpu_features = info->cpu_features;
  87. if (sdk_version != SDK_VERSION_1_5)
  88. return -1;
  89. // xsave
  90. uint64_t xfrm = get_xfeature_state();
  91. // optimized libs
  92. if(0 != init_optimized_libs(cpu_features, xfrm))
  93. {
  94. CLEAN_XFEATURE_REGS
  95. return -1;
  96. }
  97. if(SGX_SUCCESS != sgx_read_rand((unsigned char*)&__stack_chk_guard,
  98. sizeof(__stack_chk_guard)))
  99. {
  100. CLEAN_XFEATURE_REGS
  101. return -1;
  102. }
  103. // clean extended registers, no need to save
  104. CLEAN_XFEATURE_REGS
  105. return 0;
  106. }
  107. sgx_status_t do_init_enclave(void *ms)
  108. {
  109. void *enclave_base = get_enclave_base();
  110. if(ENCLAVE_INIT_NOT_STARTED != lock_enclave())
  111. {
  112. return SGX_ERROR_UNEXPECTED;
  113. }
  114. if(0 != init_enclave(enclave_base, ms))
  115. {
  116. return SGX_ERROR_UNEXPECTED;
  117. }
  118. memset(GET_PTR(void, enclave_base, g_global_data.heap_offset), 0, g_global_data.heap_size);
  119. g_enclave_state = ENCLAVE_INIT_DONE;
  120. return SGX_SUCCESS;
  121. }