trts_nsp.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2011-2018 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. /* Implement functions:
  32. * init_stack_guard()
  33. * enter_enclave()
  34. *
  35. * The functions in this source file will be called during the stack guard initialization.
  36. * They cannot be built with '-fstack-protector-strong'. Otherwise, stack guard check will
  37. * be failed before the function returns and 'ud2' will be triggered.
  38. */
  39. #include "sgx_trts.h"
  40. #include "trts_inst.h"
  41. #include "se_memcpy.h"
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include "thread_data.h"
  45. #include "global_data.h"
  46. #include "trts_internal.h"
  47. #include "internal/rts.h"
  48. static void init_stack_guard(void *tcs)
  49. {
  50. thread_data_t *thread_data = get_thread_data();
  51. if( (NULL == thread_data) || ((thread_data->stack_base_addr == thread_data->last_sp) && (0 != g_global_data.thread_policy)))
  52. {
  53. thread_data = GET_PTR(thread_data_t, tcs, g_global_data.td_template.self_addr);
  54. }
  55. else
  56. {
  57. return;
  58. }
  59. assert(thread_data != NULL);
  60. size_t tmp_stack_guard = 0;
  61. if (SGX_SUCCESS != sgx_read_rand(
  62. (unsigned char*)&tmp_stack_guard,
  63. sizeof(tmp_stack_guard)))
  64. abort();
  65. thread_data->stack_guard = tmp_stack_guard;
  66. }
  67. extern "C" int enter_enclave(int index, void *ms, void *tcs, int cssa) __attribute__((section(".nipx")));
  68. extern "C" int enter_enclave(int index, void *ms, void *tcs, int cssa)
  69. {
  70. if(sgx_is_enclave_crashed())
  71. {
  72. return SGX_ERROR_ENCLAVE_CRASHED;
  73. }
  74. sgx_status_t error = SGX_ERROR_UNEXPECTED;
  75. if(cssa == 0)
  76. {
  77. if(index >= 0)
  78. {
  79. // Initialize stack guard if necessary
  80. init_stack_guard(tcs);
  81. error = do_ecall(index, ms, tcs);
  82. }
  83. else if(index == ECMD_INIT_ENCLAVE)
  84. {
  85. error = do_init_enclave(ms, tcs);
  86. }
  87. else if(index == ECMD_ORET)
  88. {
  89. error = do_oret(ms);
  90. }
  91. else if(index == ECMD_MKTCS)
  92. {
  93. error = do_ecall_add_thread(ms);
  94. }
  95. else if(index == ECMD_UNINIT_ENCLAVE)
  96. {
  97. error = do_uninit_enclave(tcs);
  98. }
  99. }
  100. else if((cssa == 1) && (index == ECMD_EXCEPT))
  101. {
  102. error = trts_handle_exception(tcs);
  103. if (check_static_stack_canary(tcs) != 0)
  104. {
  105. error = SGX_ERROR_STACK_OVERRUN;
  106. }
  107. }
  108. if(error == SGX_ERROR_UNEXPECTED)
  109. {
  110. set_enclave_state(ENCLAVE_CRASHED);
  111. }
  112. return error;
  113. }