trts_ecall.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #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. // is_ecall_allowed()
  42. // check the index in the dynamic entry table
  43. static sgx_status_t is_ecall_allowed(uint32_t ordinal)
  44. {
  45. if(ordinal >= g_ecall_table.nr_ecall)
  46. {
  47. return SGX_ERROR_INVALID_FUNCTION;
  48. }
  49. thread_data_t *thread_data = get_thread_data();
  50. if(thread_data->last_sp == thread_data->stack_base_addr)
  51. {
  52. // root ECALL, check the priv bits.
  53. if (g_ecall_table.ecall_table[ordinal].is_priv)
  54. return SGX_ERROR_ECALL_NOT_ALLOWED;
  55. return SGX_SUCCESS;
  56. }
  57. ocall_context_t *context = reinterpret_cast<ocall_context_t*>(thread_data->last_sp);
  58. if(context->ocall_flag != OCALL_FLAG)
  59. {
  60. // abort the enclave if ocall frame is invalid
  61. abort();
  62. }
  63. uintptr_t ocall_index = context->ocall_index;
  64. if(ocall_index >= g_dyn_entry_table.nr_ocall)
  65. {
  66. return SGX_ERROR_INVALID_FUNCTION;
  67. }
  68. return (g_dyn_entry_table.entry_table[ocall_index * g_ecall_table.nr_ecall + ordinal] ? SGX_SUCCESS : SGX_ERROR_ECALL_NOT_ALLOWED);
  69. }
  70. // get_func_addr()
  71. // Get the address of ecall function from the ecall table
  72. // Parameters:
  73. // [IN] ordinal - the index of the ecall function in the ecall table
  74. // Return Value:
  75. // non-zero - success
  76. // zero - fail
  77. //
  78. static sgx_status_t get_func_addr(uint32_t ordinal, void **addr)
  79. {
  80. sgx_status_t status = is_ecall_allowed(ordinal);
  81. if(SGX_SUCCESS != status)
  82. {
  83. return status;
  84. }
  85. *addr = const_cast<void *>(g_ecall_table.ecall_table[ordinal].ecall_addr);
  86. if(!sgx_is_within_enclave(*addr, 0))
  87. {
  88. return SGX_ERROR_UNEXPECTED;
  89. }
  90. return SGX_SUCCESS;
  91. }
  92. static volatile bool g_is_first_ecall = true;
  93. static volatile sgx_spinlock_t g_ife_lock = SGX_SPINLOCK_INITIALIZER;
  94. typedef sgx_status_t (*ecall_func_t)(void *ms);
  95. static sgx_status_t trts_ecall(uint32_t ordinal, void *ms)
  96. {
  97. if (unlikely(g_is_first_ecall))
  98. {
  99. // The thread performing the global initialization cannot do a nested ECall
  100. thread_data_t *thread_data = get_thread_data();
  101. if (thread_data->last_sp != thread_data->stack_base_addr)
  102. { // nested ecall
  103. return SGX_ERROR_ECALL_NOT_ALLOWED;
  104. }
  105. sgx_spin_lock(&g_ife_lock);
  106. if (g_is_first_ecall)
  107. {
  108. //invoke global object's construction
  109. init_global_object();
  110. g_is_first_ecall = false;
  111. }
  112. sgx_spin_unlock(&g_ife_lock);
  113. }
  114. void *addr = NULL;
  115. sgx_status_t status = get_func_addr(ordinal, &addr);
  116. if(status == SGX_SUCCESS)
  117. {
  118. ecall_func_t func = (ecall_func_t)addr;
  119. status = func(ms);
  120. }
  121. // clean extended registers, no need to save
  122. CLEAN_XFEATURE_REGS
  123. return status;
  124. }
  125. extern "C" sgx_status_t do_init_thread(void *tcs);
  126. sgx_status_t do_ecall(int index, void *ms, void *tcs)
  127. {
  128. sgx_status_t status = SGX_ERROR_UNEXPECTED;
  129. if(ENCLAVE_INIT_DONE != get_enclave_state())
  130. {
  131. return status;
  132. }
  133. thread_data_t *thread_data = get_thread_data();
  134. if( (NULL == thread_data) || ((thread_data->stack_base_addr == thread_data->last_sp) && (0 != g_global_data.thread_policy)))
  135. {
  136. status = do_init_thread(tcs);
  137. if(0 != status)
  138. {
  139. return status;
  140. }
  141. }
  142. status = trts_ecall(index, ms);
  143. return status;
  144. }