enclave_creator_hw.cpp 7.9 KB

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