trts_ocall.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 <string.h>
  32. #include "thread_data.h"
  33. #include "global_data.h"
  34. #include "sgx_edger8r.h"
  35. #include "rts.h"
  36. #include "util.h"
  37. #include "xsave.h"
  38. #include "trts_internal.h"
  39. extern "C" void force_exit_enclave();
  40. extern "C" sgx_status_t asm_oret(uintptr_t sp, void *ms);
  41. extern "C" sgx_status_t __morestack(const unsigned int index, void *ms);
  42. #define do_ocall __morestack
  43. //
  44. // sgx_ocall
  45. // Parameters:
  46. // index - the index in the ocall table
  47. // ms - the mashalling structure
  48. // Return Value:
  49. // OCALL status
  50. //
  51. sgx_status_t sgx_ocall(const unsigned int index, void *ms)
  52. {
  53. if(get_enclave_state() == ENCLAVE_CRASHED) {
  54. force_exit_enclave();
  55. }
  56. // sgx_ocall is not allowed during exception handling
  57. thread_data_t *thread_data = get_thread_data();
  58. // we have exceptions being handled
  59. if(thread_data->exception_flag != 0) {
  60. return SGX_ERROR_OCALL_NOT_ALLOWED;
  61. }
  62. // the OCALL index should be within the ocall table range
  63. if(static_cast<size_t>(index) >= g_dyn_entry_table.nr_ocall)
  64. {
  65. return SGX_ERROR_INVALID_FUNCTION;
  66. }
  67. // save and clean extended feature registers
  68. uint8_t buffer[FXSAVE_SIZE] = {0};
  69. save_and_clean_xfeature_regs(buffer);
  70. // do sgx_ocall
  71. sgx_status_t status = do_ocall(index, ms);
  72. // restore extended feature registers
  73. restore_xfeature_regs(buffer);
  74. // clear buffer to avoid secret leaking
  75. memset_s(buffer, FXSAVE_SIZE, 0, FXSAVE_SIZE);
  76. return status;
  77. }
  78. extern "C"
  79. uintptr_t update_ocall_lastsp(ocall_context_t* context)
  80. {
  81. thread_data_t* thread_data = get_thread_data();
  82. uintptr_t last_sp = 0;
  83. last_sp = thread_data->last_sp;
  84. context->pre_last_sp = last_sp;
  85. if (context->pre_last_sp == thread_data->stack_base_addr)
  86. {
  87. context->ocall_depth = 1;
  88. } else {
  89. // thread_data->last_sp is only set when ocall or exception handling occurs
  90. // ocall is block during exception handling, so last_sp is always ocall frame here
  91. ocall_context_t* context_pre = reinterpret_cast<ocall_context_t*>(context->pre_last_sp);
  92. context->ocall_depth = context_pre->ocall_depth + 1;
  93. }
  94. thread_data->last_sp = reinterpret_cast<uintptr_t>(context);
  95. return last_sp;
  96. }
  97. sgx_status_t do_oret(void *ms)
  98. {
  99. thread_data_t *thread_data = get_thread_data();
  100. uintptr_t last_sp = thread_data->last_sp;
  101. ocall_context_t *context = reinterpret_cast<ocall_context_t*>(thread_data->last_sp);
  102. if(0 == last_sp || last_sp <= (uintptr_t)&context)
  103. {
  104. return SGX_ERROR_UNEXPECTED;
  105. }
  106. // At least 1 ecall frame and 1 ocall frame are expected on stack.
  107. // 30 is an estimated value: 8 for enclave_entry and 22 for do_ocall.
  108. if(last_sp > thread_data->stack_base_addr - 30 * sizeof(size_t))
  109. {
  110. return SGX_ERROR_UNEXPECTED;
  111. }
  112. if(context->ocall_flag != OCALL_FLAG)
  113. {
  114. return SGX_ERROR_UNEXPECTED;
  115. }
  116. if(context->pre_last_sp > thread_data->stack_base_addr
  117. || context->pre_last_sp <= (uintptr_t)context)
  118. {
  119. return SGX_ERROR_UNEXPECTED;
  120. }
  121. thread_data->last_sp = context->pre_last_sp;
  122. asm_oret(last_sp, ms);
  123. // Should not come here
  124. return SGX_ERROR_UNEXPECTED;
  125. }