trts.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include "sgx_trts.h"
  32. #include "sgx_edger8r.h"
  33. #include "trts_inst.h"
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "util.h"
  37. #include "thread_data.h"
  38. #include "global_data.h"
  39. #include "trts_internal.h"
  40. #include "internal/rts.h"
  41. #ifdef SE_SIM
  42. #include "t_instructions.h" /* for `g_global_data_sim' */
  43. #include "sgx_spinlock.h"
  44. #endif
  45. #ifndef SE_SIM
  46. #include "se_cdefs.h"
  47. // add a version to trts
  48. SGX_ACCESS_VERSION(trts, 1);
  49. #endif
  50. // sgx_is_within_enclave()
  51. // Parameters:
  52. // addr - the start address of the buffer
  53. // size - the size of the buffer
  54. // Return Value:
  55. // 1 - the buffer is strictly within the enclave
  56. // 0 - the whole buffer or part of the buffer is not within the enclave,
  57. // or the buffer is wrap around
  58. //
  59. int sgx_is_within_enclave(__attribute__((unused))const void *addr, __attribute__((unused))size_t size)
  60. {
  61. /* size_t start = reinterpret_cast<size_t>(addr);
  62. size_t end = 0;
  63. size_t enclave_start = (size_t)&__ImageBase;
  64. size_t enclave_end = enclave_start + g_global_data.enclave_size - 1;
  65. // g_global_data.enclave_end = enclave_base + enclave_size - 1;
  66. // so the enclave range is [enclave_start, enclave_end] inclusively
  67. if(size > 0)
  68. {
  69. end = start + size - 1;
  70. }
  71. else
  72. {
  73. end = start;
  74. }
  75. if( (start <= end) && (start >= enclave_start) && (end <= enclave_end) )
  76. {
  77. return 1;
  78. }
  79. return 0;
  80. */ return 1;
  81. }
  82. // sgx_is_outside_enclave()
  83. // Parameters:
  84. // addr - the start address of the buffer
  85. // size - the size of the buffer
  86. // Return Value:
  87. // 1 - the buffer is strictly outside the enclave
  88. // 0 - the whole buffer or part of the buffer is not outside the enclave,
  89. // or the buffer is wrap around
  90. //
  91. int sgx_is_outside_enclave(const void *addr, size_t size)
  92. {
  93. size_t start = reinterpret_cast<size_t>(addr);
  94. size_t end = 0;
  95. size_t enclave_start = (size_t)&__ImageBase;
  96. size_t enclave_end = enclave_start + g_global_data.enclave_size - 1;
  97. // g_global_data.enclave_end = enclave_base + enclave_size - 1;
  98. // so the enclave range is [enclave_start, enclave_end] inclusively
  99. if(size > 0)
  100. {
  101. end = start + size - 1;
  102. }
  103. else
  104. {
  105. end = start;
  106. }
  107. if( (start <= end) && ((end < enclave_start) || (start > enclave_end)) )
  108. {
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. // sgx_ocalloc()
  114. // Parameters:
  115. // size - bytes to allocate on the outside stack
  116. // Return Value:
  117. // the pointer to the allocated space on the outside stack
  118. // NULL - fail to allocate
  119. //
  120. // sgx_ocalloc allocates memory on the outside stack. It is only used for OCALL, and will be auto freed when ECALL returns.
  121. // To achieve this, the outside stack pointer in SSA is updated when the stack memory is allocated,
  122. // but the outside stack pointer saved in the ECALL stack frame is not changed accordingly.
  123. // When doing an OCALL, the stack pointer is set as the value in SSA and EEXIT.
  124. // When ECALL or exception handling returns, the stack pointer is set as the value in the ECALL stack frame and then EEXIT,
  125. // so the outside stack is automatically unwind.
  126. // In addition, sgx_ocalloc needs perform outside stack probe to make sure it is not allocating beyond the end of the stack.
  127. #define OC_ROUND 16
  128. void * sgx_ocalloc(size_t size)
  129. {
  130. // read the outside stack address from current SSA
  131. thread_data_t *thread_data = get_thread_data();
  132. ssa_gpr_t *ssa_gpr = reinterpret_cast<ssa_gpr_t *>(thread_data->first_ssa_gpr);
  133. size_t addr = ssa_gpr->REG(sp_u);
  134. // check u_rsp points to the untrusted address.
  135. // if the check fails, it should be hacked. call abort directly
  136. if(!sgx_is_outside_enclave(reinterpret_cast<void *>(addr), sizeof(size_t)))
  137. {
  138. abort();
  139. }
  140. // size is too large to allocate. call abort() directly.
  141. if(addr < size)
  142. {
  143. abort();
  144. }
  145. // calculate the start address for the allocated memory
  146. addr -= size;
  147. addr &= ~(static_cast<size_t>(OC_ROUND - 1)); // for stack alignment
  148. // the allocated memory has overlap with enclave, abort the enclave
  149. if(!sgx_is_outside_enclave(reinterpret_cast<void *>(addr), size))
  150. {
  151. abort();
  152. }
  153. // probe the outside stack to ensure that we do not skip over the stack3 guard page
  154. // we need to probe all the pages including the first page and the last page
  155. // the first page need to be probed in case uRTS didnot touch that page before EENTER enclave
  156. // the last page need to be probed in case the enclave didnot touch that page before another OCALLOC
  157. size_t first_page = TRIM_TO_PAGE(ssa_gpr->REG(sp_u) - 1);
  158. size_t last_page = TRIM_TO_PAGE(addr);
  159. // To avoid the dead-loop in the following for(...) loop.
  160. // Attacker might fake a stack address that is within address 0x4095.
  161. if (last_page == 0)
  162. {
  163. abort();
  164. }
  165. // the compiler may optimize the following code to probe the pages in any order
  166. // while we only expect the probe order should be from higher addr to lower addr
  167. // so use volatile to avoid optimization by the compiler
  168. for(volatile size_t page = first_page; page >= last_page; page -= SE_PAGE_SIZE)
  169. {
  170. // OS may refuse to commit a physical page if the page fault address is smaller than RSP
  171. // So update the outside stack address before probe the page
  172. ssa_gpr->REG(sp_u) = page;
  173. *reinterpret_cast<uint8_t *>(page) = 0;
  174. }
  175. // update the outside stack address in the SSA to the allocated address
  176. ssa_gpr->REG(sp_u) = addr;
  177. return reinterpret_cast<void *>(addr);
  178. }
  179. // sgx_ocfree()
  180. // Parameters:
  181. // N/A
  182. // Return Value:
  183. // N/A
  184. // sgx_ocfree restores the original outside stack pointer in the SSA.
  185. // Do not call this function if you still need the buffer allocated by sgx_ocalloc within the ECALL.
  186. void sgx_ocfree()
  187. {
  188. // ECALL stack frame
  189. // last_sp -> | |
  190. // -------------
  191. // | ret_addr |
  192. // | xbp_u |
  193. // | xsp_u |
  194. thread_data_t *thread_data = get_thread_data();
  195. ssa_gpr_t *ssa_gpr = reinterpret_cast<ssa_gpr_t *>(thread_data->first_ssa_gpr);
  196. uintptr_t *addr = reinterpret_cast<uintptr_t *>(thread_data->last_sp);
  197. uintptr_t usp = *(addr - 3);
  198. if(!sgx_is_outside_enclave(reinterpret_cast<void *>(usp), sizeof(uintptr_t)))
  199. {
  200. abort();
  201. }
  202. ssa_gpr->REG(sp_u) = usp;
  203. }
  204. #ifdef SE_SIM
  205. static sgx_spinlock_t g_seed_lock = SGX_SPINLOCK_INITIALIZER;
  206. static uint32_t get_rand_lcg()
  207. {
  208. sgx_spin_lock(&g_seed_lock);
  209. uint64_t& seed = g_global_data_sim.seed;
  210. seed = (uint64_t)(6364136223846793005ULL * seed + 1);
  211. uint32_t n = (uint32_t)(seed >> 32);
  212. sgx_spin_unlock(&g_seed_lock);
  213. return n;
  214. }
  215. #endif
  216. static sgx_status_t __do_get_rand32(uint32_t* rand_num)
  217. {
  218. #ifndef SE_SIM
  219. /* We expect the CPU has RDRAND support for HW mode. Otherwise, an exception will be thrown
  220. * do_rdrand() will try to call RDRAND for 10 times
  221. */
  222. if(0 == do_rdrand(rand_num))
  223. return SGX_ERROR_UNEXPECTED;
  224. #else
  225. /* use LCG in simulation mode */
  226. *rand_num = get_rand_lcg();
  227. #endif
  228. return SGX_SUCCESS;
  229. }
  230. sgx_status_t sgx_read_rand(unsigned char *rand, size_t length_in_bytes)
  231. {
  232. // check parameters
  233. //
  234. // rand can be within or outside the enclave
  235. if(!rand || !length_in_bytes)
  236. {
  237. return SGX_ERROR_INVALID_PARAMETER;
  238. }
  239. if(!sgx_is_within_enclave(rand, length_in_bytes) && !sgx_is_outside_enclave(rand, length_in_bytes))
  240. {
  241. return SGX_ERROR_INVALID_PARAMETER;
  242. }
  243. // loop to rdrand
  244. uint32_t rand_num = 0;
  245. while(length_in_bytes > 0)
  246. {
  247. sgx_status_t status = __do_get_rand32(&rand_num);
  248. if(status != SGX_SUCCESS)
  249. {
  250. return status;
  251. }
  252. size_t size = (length_in_bytes < sizeof(rand_num)) ? length_in_bytes : sizeof(rand_num);
  253. memcpy(rand, &rand_num, size);
  254. rand += size;
  255. length_in_bytes -= size;
  256. }
  257. memset_s(&rand_num, sizeof(rand_num), 0, sizeof(rand_num));
  258. return SGX_SUCCESS;
  259. }
  260. int sgx_is_enclave_crashed()
  261. {
  262. return get_enclave_state() == ENCLAVE_CRASHED;
  263. }
  264. extern uintptr_t __stack_chk_guard;
  265. int check_static_stack_canary(void *tcs)
  266. {
  267. size_t *canary = TCS2CANARY(tcs);
  268. if ( *canary != (size_t)__stack_chk_guard)
  269. {
  270. return -1;
  271. }
  272. return 0;
  273. }