trts_ocall.cpp 4.6 KB

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