trts_ecall.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "se_memcpy.h"
  32. #include "thread_data.h"
  33. #include "global_data.h"
  34. #include "rts.h"
  35. #include "util.h"
  36. #include "xsave.h"
  37. #include "sgx_trts.h"
  38. #include "sgx_spinlock.h"
  39. #include "global_init.h"
  40. #include "trts_internal.h"
  41. # include "linux/elf_parser.h"
  42. # define GET_TLS_INFO elf_tls_info
  43. // is_ecall_allowed()
  44. // check the index in the dynamic entry table
  45. static sgx_status_t is_ecall_allowed(uint32_t ordinal)
  46. {
  47. if(ordinal >= g_ecall_table.nr_ecall)
  48. {
  49. return SGX_ERROR_INVALID_FUNCTION;
  50. }
  51. thread_data_t *thread_data = get_thread_data();
  52. if(thread_data->last_sp == thread_data->stack_base_addr)
  53. {
  54. // root ECALL, check the priv bits.
  55. if (g_ecall_table.ecall_table[ordinal].is_priv)
  56. return SGX_ERROR_ECALL_NOT_ALLOWED;
  57. return SGX_SUCCESS;
  58. }
  59. ocall_context_t *context = reinterpret_cast<ocall_context_t*>(thread_data->last_sp);
  60. if(context->ocall_flag != OCALL_FLAG)
  61. {
  62. // abort the enclave if ocall frame is invalid
  63. abort();
  64. }
  65. uintptr_t ocall_index = context->ocall_index;
  66. if(ocall_index >= g_dyn_entry_table.nr_ocall)
  67. {
  68. return SGX_ERROR_INVALID_FUNCTION;
  69. }
  70. return (g_dyn_entry_table.entry_table[ocall_index * g_ecall_table.nr_ecall + ordinal] ? SGX_SUCCESS : SGX_ERROR_ECALL_NOT_ALLOWED);
  71. }
  72. // get_func_addr()
  73. // Get the address of ecall function from the ecall table
  74. // Parameters:
  75. // [IN] ordinal - the index of the ecall function in the ecall table
  76. // Return Value:
  77. // non-zero - success
  78. // zero - fail
  79. //
  80. static sgx_status_t get_func_addr(uint32_t ordinal, void **addr)
  81. {
  82. sgx_status_t status = is_ecall_allowed(ordinal);
  83. if(SGX_SUCCESS != status)
  84. {
  85. return status;
  86. }
  87. *addr = const_cast<void *>(g_ecall_table.ecall_table[ordinal].ecall_addr);
  88. if(!sgx_is_within_enclave(*addr, 0))
  89. {
  90. return SGX_ERROR_UNEXPECTED;
  91. }
  92. return SGX_SUCCESS;
  93. }
  94. static volatile bool g_is_first_ecall = true;
  95. static volatile sgx_spinlock_t g_ife_lock = SGX_SPINLOCK_INITIALIZER;
  96. typedef sgx_status_t (*ecall_func_t)(void *ms);
  97. static sgx_status_t trts_ecall(uint32_t ordinal, void *ms)
  98. {
  99. if (unlikely(g_is_first_ecall))
  100. {
  101. // The thread performing the global initialization cannot do a nested ECall
  102. thread_data_t *thread_data = get_thread_data();
  103. if (thread_data->last_sp != thread_data->stack_base_addr)
  104. { // nested ecall
  105. return SGX_ERROR_ECALL_NOT_ALLOWED;
  106. }
  107. sgx_spin_lock(&g_ife_lock);
  108. if (g_is_first_ecall)
  109. {
  110. //invoke global object's construction
  111. init_global_object();
  112. g_is_first_ecall = false;
  113. }
  114. sgx_spin_unlock(&g_ife_lock);
  115. }
  116. void *addr = NULL;
  117. sgx_status_t status = get_func_addr(ordinal, &addr);
  118. if(status == SGX_SUCCESS)
  119. {
  120. ecall_func_t func = (ecall_func_t)addr;
  121. status = func(ms);
  122. }
  123. // clean extended registers, no need to save
  124. CLEAN_XFEATURE_REGS
  125. return status;
  126. }
  127. extern "C" void init_stack_guard();
  128. static sgx_status_t do_init_thread(void *tcs)
  129. {
  130. thread_data_t *thread_data = GET_PTR(thread_data_t, tcs, g_global_data.td_template.self_addr);
  131. memcpy_s(thread_data, SE_PAGE_SIZE, const_cast<thread_data_t *>(&g_global_data.td_template), sizeof(thread_data_t));
  132. thread_data->last_sp += (size_t)tcs;
  133. thread_data->self_addr += (size_t)tcs;
  134. thread_data->stack_base_addr += (size_t)tcs;
  135. thread_data->stack_limit_addr += (size_t)tcs;
  136. thread_data->first_ssa_gpr += (size_t)tcs;
  137. thread_data->tls_array += (size_t)tcs;
  138. thread_data->tls_addr += (size_t)tcs;
  139. thread_data->last_sp -= (size_t)STATIC_STACK_SIZE;
  140. thread_data->stack_base_addr -= (size_t)STATIC_STACK_SIZE;
  141. uintptr_t tls_addr = 0;
  142. size_t tdata_size = 0;
  143. if(0 != GET_TLS_INFO(&__ImageBase, &tls_addr, &tdata_size))
  144. {
  145. return SGX_ERROR_UNEXPECTED;
  146. }
  147. if(tls_addr)
  148. {
  149. memset((void *)TRIM_TO_PAGE(thread_data->tls_addr), 0, ROUND_TO_PAGE(thread_data->self_addr - thread_data->tls_addr));
  150. memcpy_s((void *)(thread_data->tls_addr), thread_data->self_addr - thread_data->tls_addr, (void *)tls_addr, tdata_size);
  151. }
  152. init_stack_guard();
  153. return SGX_SUCCESS;
  154. }
  155. sgx_status_t do_ecall(int index, void *ms, void *tcs)
  156. {
  157. sgx_status_t status = SGX_ERROR_UNEXPECTED;
  158. if(ENCLAVE_INIT_DONE != get_enclave_state())
  159. {
  160. return status;
  161. }
  162. thread_data_t *thread_data = get_thread_data();
  163. if( (NULL == thread_data) || ((thread_data->stack_base_addr == thread_data->last_sp) && (0 != g_global_data.thread_policy)))
  164. {
  165. status = do_init_thread(tcs);
  166. if(0 != status)
  167. {
  168. return status;
  169. }
  170. }
  171. status = trts_ecall(index, ms);
  172. return status;
  173. }