enclave_creator_hw.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 "enclave_creator_hw.h"
  32. #include "se_trace.h"
  33. #include "se_page_attr.h"
  34. #include "isgx_user.h"
  35. #include "sig_handler.h"
  36. #include "se_error_internal.h"
  37. #include "se_memcpy.h"
  38. #include "se_atomic.h"
  39. #include "se_detect.h"
  40. #include "cpuid.h"
  41. #include <assert.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <sys/ioctl.h>
  46. #include <errno.h>
  47. #include <sys/mman.h>
  48. #include <stdlib.h>
  49. static EnclaveCreatorHW g_enclave_creator_hw;
  50. EnclaveCreator* g_enclave_creator = &g_enclave_creator_hw;
  51. static uint64_t g_eid = 0x1;
  52. EnclaveCreatorHW::EnclaveCreatorHW():
  53. m_hdevice(-1),
  54. m_sig_registered(false)
  55. {
  56. se_mutex_init(&m_sig_mutex);
  57. }
  58. EnclaveCreatorHW::~EnclaveCreatorHW()
  59. {
  60. close_se_device();
  61. }
  62. int EnclaveCreatorHW::error_driver2urts(int driver_error)
  63. {
  64. int ret = SGX_ERROR_UNEXPECTED;
  65. switch(driver_error)
  66. {
  67. #if 0
  68. case SGX_ERROR:
  69. if(ENOMEM == errno)
  70. ret = SGX_ERROR_OUT_OF_MEMORY;
  71. else
  72. ret = SGX_ERROR_NO_DEVICE;
  73. break;
  74. #endif
  75. case SGX_INVALID_ATTRIBUTE:
  76. ret = SGX_ERROR_INVALID_ATTRIBUTE;
  77. break;
  78. case SGX_INVALID_MEASUREMENT:
  79. ret = SE_ERROR_INVALID_MEASUREMENT;
  80. break;
  81. case SGX_INVALID_SIG_STRUCT:
  82. case SGX_INVALID_SIGNATURE:
  83. ret = SGX_ERROR_INVALID_SIGNATURE;
  84. break;
  85. case SGX_INVALID_CPUSVN:
  86. ret = SGX_ERROR_INVALID_CPUSVN;
  87. break;
  88. case SGX_INVALID_ISVSVN:
  89. ret = SGX_ERROR_INVALID_ISVSVN;
  90. break;
  91. case SGX_UNMASKED_EVENT:
  92. ret = SGX_ERROR_DEVICE_BUSY;
  93. break;
  94. case (int)SGX_POWER_LOST_ENCLAVE: // [-Wc++11-narrowing]
  95. ret = SGX_ERROR_ENCLAVE_LOST;
  96. break;
  97. default:
  98. SE_TRACE(SE_TRACE_WARNING, "unexpected error %#x from driver, should be uRTS/driver bug\n", driver_error);
  99. ret = SGX_ERROR_UNEXPECTED;
  100. break;
  101. }
  102. return ret;
  103. }
  104. int EnclaveCreatorHW::create_enclave(secs_t *secs, sgx_enclave_id_t *enclave_id, void **start_addr, bool ae)
  105. {
  106. assert(secs != NULL && enclave_id != NULL && start_addr != NULL);
  107. UNUSED(ae);
  108. int ret = 0;
  109. if (false == open_se_device())
  110. return SGX_ERROR_NO_DEVICE;
  111. SE_TRACE(SE_TRACE_DEBUG, "\n secs.attibutes.flags = %llx, secs.attributes.xfrm = %llx \n"
  112. , secs->attributes.flags, secs->attributes.xfrm);
  113. //SECS:BASEADDR must be naturally aligned on an SECS.SIZE boundary
  114. //This alignment is guaranteed by driver, at linux-sgx-driver/sgx_main.c:141 to 146
  115. //141 addr = current->mm->get_unmapped_area(file, addr, 2 * len, pgoff,
  116. //142 flags);
  117. //143 if (IS_ERR_VALUE(addr))
  118. //144 return addr;
  119. //145
  120. //146 addr = (addr + (len - 1)) & ~(len - 1);
  121. //147
  122. //148 return addr;
  123. //Thus the only thing to do is to let the kernel driver align the memory.
  124. void* enclave_base = mmap(NULL, (size_t)secs->size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED, m_hdevice, 0);
  125. if(enclave_base == MAP_FAILED)
  126. {
  127. SE_TRACE(SE_TRACE_WARNING, "\nISGX_IOCTL_ENCLAVE_CREATE failed: mmap failed, errno = %d\n", errno);
  128. return SGX_ERROR_OUT_OF_MEMORY;
  129. }
  130. secs->base = (void*)enclave_base;
  131. struct sgx_enclave_create param = {0};
  132. param.src = (uintptr_t)(secs);
  133. ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_CREATE, &param);
  134. if(ret) {
  135. SE_TRACE(SE_TRACE_WARNING, "\nISGX_IOCTL_ENCLAVE_CREATE failed: errno = %d\n", errno);
  136. return error_driver2urts(ret);
  137. }
  138. *enclave_id = se_atomic_inc64(&g_eid);
  139. *start_addr = secs->base;
  140. return SGX_SUCCESS;
  141. }
  142. int EnclaveCreatorHW::add_enclave_page(sgx_enclave_id_t enclave_id, void *src, uint64_t rva, const sec_info_t &sinfo, uint32_t attr)
  143. {
  144. assert((rva & ((1<<SE_PAGE_SHIFT)-1)) == 0);
  145. void* source = src;
  146. uint8_t color_page[SE_PAGE_SIZE] = { 0 };
  147. if(NULL == source)
  148. {
  149. memset(color_page, 0, SE_PAGE_SIZE);
  150. source = reinterpret_cast<void*>(&color_page);
  151. }
  152. int ret = 0;
  153. struct sgx_enclave_add_page addp = { 0, 0, 0, 0 };
  154. addp.addr = (__u64)enclave_id + (__u64)rva;
  155. addp.src = reinterpret_cast<uintptr_t>(source);
  156. addp.secinfo = reinterpret_cast<uintptr_t>(const_cast<sec_info_t *>(&sinfo));
  157. if(((1<<DoEEXTEND) & attr))
  158. addp.mrmask |= 0xFFFF;
  159. ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_ADD_PAGE, &addp);
  160. if(ret) {
  161. SE_TRACE(SE_TRACE_WARNING, "\nAdd Page - %p to %p... FAIL\n", source, rva);
  162. return error_driver2urts(ret);
  163. }
  164. return SGX_SUCCESS;
  165. }
  166. int EnclaveCreatorHW::try_init_enclave(sgx_enclave_id_t enclave_id, enclave_css_t *enclave_css, token_t *launch)
  167. {
  168. int ret = 0;
  169. struct sgx_enclave_init initp = { 0, 0, 0 };
  170. initp.addr = (__u64)enclave_id;
  171. initp.sigstruct = reinterpret_cast<uintptr_t>(enclave_css);
  172. //launch should NOT be NULL, because it has been checked in urts_com.h::_create_enclave(...)
  173. assert(launch != NULL);
  174. initp.einittoken = reinterpret_cast<uintptr_t>(launch);
  175. ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_INIT, &initp);
  176. if (ret) {
  177. SE_TRACE(SE_TRACE_WARNING, "\nISGX_IOCTL_ENCLAVE_INIT failed error = %d\n", ret);
  178. return error_driver2urts(ret);
  179. }
  180. //register signal handler
  181. se_mutex_lock(&m_sig_mutex);
  182. if(false == m_sig_registered)
  183. {
  184. reg_sig_handler();
  185. m_sig_registered = true;
  186. }
  187. se_mutex_unlock(&m_sig_mutex);
  188. return SGX_SUCCESS;
  189. }
  190. //for linux hw mode, enclave_id is actually start address here
  191. int EnclaveCreatorHW::destroy_enclave(sgx_enclave_id_t enclave_id, uint64_t enclave_size)
  192. {
  193. int ret = 0;
  194. ret = munmap((void*)enclave_id, (size_t)enclave_size);
  195. if (0 != ret) {
  196. SE_TRACE(SE_TRACE_WARNING, "destroy SGX enclave failed, error = %d\n", errno);
  197. ret = SGX_ERROR_UNEXPECTED;
  198. }
  199. else
  200. {
  201. ret = SGX_SUCCESS;
  202. }
  203. return ret;
  204. }
  205. bool EnclaveCreatorHW::get_plat_cap(sgx_misc_attribute_t *misc_attr)
  206. {
  207. return get_plat_cap_by_cpuid(misc_attr);
  208. }
  209. bool EnclaveCreatorHW::open_se_device()
  210. {
  211. LockGuard lock(&m_dev_mutex);
  212. int fd = -1;
  213. if(-1 != m_hdevice)
  214. {
  215. return true;
  216. }
  217. fd = open("/dev/isgx", O_RDWR);
  218. if (-1 == fd) {
  219. SE_TRACE(SE_TRACE_WARNING, "open isgx device failed\n");
  220. return false;
  221. }
  222. m_hdevice = fd;
  223. return true;
  224. }
  225. void EnclaveCreatorHW::close_se_device()
  226. {
  227. LockGuard lock(&m_dev_mutex);
  228. if (m_hdevice != -1)
  229. {
  230. close(m_hdevice);
  231. m_hdevice = -1;
  232. }
  233. }