enclave_creator_hw.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "enclave.h"
  32. #include "enclave_creator_hw.h"
  33. #include "edmm_utility.h"
  34. #include "se_trace.h"
  35. #include "se_page_attr.h"
  36. #include "isgx_user.h"
  37. #include "sig_handler.h"
  38. #include "se_error_internal.h"
  39. #include "se_memcpy.h"
  40. #include "se_atomic.h"
  41. #include "se_detect.h"
  42. #include "cpuid.h"
  43. #include "rts.h"
  44. #include <assert.h>
  45. #include <sys/ioctl.h>
  46. #include <errno.h>
  47. #include <sys/mman.h>
  48. #include <stdlib.h>
  49. #define POINTER_TO_U64(A) ((__u64)((uintptr_t)(A)))
  50. static EnclaveCreatorHW g_enclave_creator_hw;
  51. EnclaveCreator* g_enclave_creator = &g_enclave_creator_hw;
  52. static uint64_t g_eid = 0x1;
  53. EnclaveCreatorHW::EnclaveCreatorHW():
  54. m_hdevice(-1),
  55. m_sig_registered(false),
  56. m_in_kernel_driver(false)
  57. {
  58. se_mutex_init(&m_sig_mutex);
  59. }
  60. EnclaveCreatorHW::~EnclaveCreatorHW()
  61. {
  62. close_se_device();
  63. }
  64. int EnclaveCreatorHW::error_driver2urts(int driver_error)
  65. {
  66. int ret = SGX_ERROR_UNEXPECTED;
  67. switch(driver_error)
  68. {
  69. case SGX_INVALID_ATTRIBUTE:
  70. ret = SGX_ERROR_INVALID_ATTRIBUTE;
  71. break;
  72. case SGX_INVALID_MEASUREMENT:
  73. ret = SE_ERROR_INVALID_MEASUREMENT;
  74. break;
  75. case SGX_INVALID_SIG_STRUCT:
  76. case SGX_INVALID_SIGNATURE:
  77. ret = SGX_ERROR_INVALID_SIGNATURE;
  78. break;
  79. case SGX_INVALID_LICENSE:
  80. ret = SE_ERROR_INVALID_LAUNCH_TOKEN;
  81. break;
  82. case SGX_INVALID_CPUSVN:
  83. ret = SGX_ERROR_INVALID_CPUSVN;
  84. break;
  85. case SGX_INVALID_ISVSVN:
  86. ret = SGX_ERROR_INVALID_ISVSVN;
  87. break;
  88. case SGX_UNMASKED_EVENT:
  89. ret = SGX_ERROR_DEVICE_BUSY;
  90. break;
  91. case (int)SGX_POWER_LOST_ENCLAVE: // [-Wc++11-narrowing]
  92. ret = SGX_ERROR_ENCLAVE_LOST;
  93. break;
  94. case (int)SGX_LE_ROLLBACK:
  95. ret = SE_ERROR_INVALID_ISVSVNLE;
  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. 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. //This alignment is guaranteed by driver
  114. void* enclave_base = mmap(NULL, (size_t)secs->size, PROT_NONE, MAP_SHARED, m_hdevice, 0);
  115. if(enclave_base == MAP_FAILED)
  116. {
  117. SE_TRACE(SE_TRACE_WARNING, "\ncreate enclave: mmap failed, errno = %d\n", errno);
  118. return SGX_ERROR_OUT_OF_MEMORY;
  119. }
  120. secs->base = enclave_base;
  121. struct sgx_enclave_create param = {0};
  122. param.src = POINTER_TO_U64(secs);
  123. int ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_CREATE, &param);
  124. if(ret)
  125. {
  126. if(ret == -1 && errno == EINTR)
  127. {
  128. return SGX_INTERNAL_ERROR_ENCLAVE_CREATE_INTERRUPTED; // Allow users to retry
  129. }
  130. SE_TRACE(SE_TRACE_WARNING, "\nSGX_IOC_ENCLAVE_CREATE failed: errno = %d\n", errno);
  131. return error_driver2urts(ret);
  132. }
  133. *enclave_id = se_atomic_inc64(&g_eid);
  134. *start_addr = secs->base;
  135. return SGX_SUCCESS;
  136. }
  137. int EnclaveCreatorHW::add_enclave_page(sgx_enclave_id_t enclave_id, void *src, uint64_t rva, const sec_info_t &sinfo, uint32_t attr)
  138. {
  139. assert((rva & ((1<<SE_PAGE_SHIFT)-1)) == 0);
  140. void* source = src;
  141. uint8_t color_page[SE_PAGE_SIZE] = { 0 };
  142. if(NULL == source)
  143. {
  144. memset(color_page, 0, SE_PAGE_SIZE);
  145. source = reinterpret_cast<void*>(&color_page);
  146. }
  147. int ret = 0;
  148. struct sgx_enclave_add_page addp = { 0, 0, 0, 0 };
  149. addp.addr = enclave_id + rva;
  150. addp.src = POINTER_TO_U64(source);
  151. addp.secinfo = POINTER_TO_U64(const_cast<sec_info_t*>(&sinfo));
  152. if(((1<<DoEEXTEND) & attr))
  153. addp.mrmask |= 0xFFFF;
  154. ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_ADD_PAGE, &addp);
  155. if(ret) {
  156. SE_TRACE(SE_TRACE_WARNING, "\nAdd Page - %p to %p... FAIL\n", source, rva);
  157. return error_driver2urts(ret);
  158. }
  159. return SGX_SUCCESS;
  160. }
  161. int EnclaveCreatorHW::try_init_enclave(sgx_enclave_id_t enclave_id, enclave_css_t *enclave_css, token_t *launch)
  162. {
  163. int ret = 0;
  164. if (m_in_kernel_driver == false)
  165. {
  166. struct sgx_enclave_init initp = { 0, 0, 0 };
  167. initp.addr = enclave_id;
  168. initp.sigstruct = POINTER_TO_U64(enclave_css);
  169. //license should NOT be NULL, because it has been checked in urts_com.h::_create_enclave(...)
  170. assert(launch != NULL);
  171. initp.einittoken = POINTER_TO_U64(launch);
  172. ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_INIT, &initp);
  173. }
  174. else
  175. {
  176. struct sgx_enclave_init_in_kernel initp = { 0, 0 };
  177. initp.addr = enclave_id;
  178. initp.sigstruct = POINTER_TO_U64(enclave_css);
  179. ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_INIT_IN_KERNEL, &initp);
  180. }
  181. if (ret) {
  182. SE_TRACE(SE_TRACE_WARNING, "\nSGX_IOC_ENCLAVE_INIT failed error = %d\n", ret);
  183. return error_driver2urts(ret);
  184. }
  185. //register signal handler
  186. se_mutex_lock(&m_sig_mutex);
  187. if(false == m_sig_registered)
  188. {
  189. reg_sig_handler();
  190. m_sig_registered = true;
  191. }
  192. se_mutex_unlock(&m_sig_mutex);
  193. return SGX_SUCCESS;
  194. }
  195. int EnclaveCreatorHW::destroy_enclave(sgx_enclave_id_t enclave_id, uint64_t enclave_size)
  196. {
  197. int ret = SGX_SUCCESS;
  198. ret = munmap((void*)enclave_id, (size_t)enclave_size);
  199. if (0 != ret) {
  200. SE_TRACE(SE_TRACE_WARNING, "destroy SGX enclave failed, error = %d\n", errno);
  201. ret = SGX_ERROR_UNEXPECTED;
  202. }
  203. return ret;
  204. }
  205. bool EnclaveCreatorHW::get_plat_cap(sgx_misc_attribute_t *misc_attr)
  206. {
  207. // need to update code to support HyperV ECO
  208. return get_plat_cap_by_cpuid(misc_attr);
  209. }
  210. bool EnclaveCreatorHW::open_se_device()
  211. {
  212. LockGuard lock(&m_dev_mutex);
  213. if(-1 != m_hdevice)
  214. return true;
  215. return ::open_se_device(&m_hdevice, &m_in_kernel_driver);
  216. }
  217. void EnclaveCreatorHW::close_se_device()
  218. {
  219. LockGuard lock(&m_dev_mutex);
  220. ::close_se_device(&m_hdevice);
  221. m_hdevice = -1;
  222. }
  223. int EnclaveCreatorHW::emodpr(uint64_t addr, uint64_t size, uint64_t flag)
  224. {
  225. sgx_modification_param params;
  226. memset(&params, 0 ,sizeof(sgx_modification_param));
  227. params.range.start_addr = (unsigned long)addr;
  228. params.range.nr_pages = (unsigned int)(size/SE_PAGE_SIZE);
  229. params.flags = (unsigned long)flag;
  230. int ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_EMODPR, &params);
  231. if (ret)
  232. {
  233. SE_TRACE(SE_TRACE_ERROR, "SGX_IOC_ENCLAVE_EMODPR failed %d\n", errno);
  234. return error_driver2urts(ret);
  235. }
  236. return SGX_SUCCESS;
  237. }
  238. int EnclaveCreatorHW::mktcs(uint64_t tcs_addr)
  239. {
  240. sgx_range params;
  241. memset(&params, 0 ,sizeof(sgx_range));
  242. params.start_addr = (unsigned long)tcs_addr;
  243. params.nr_pages = 1;
  244. int ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_MKTCS, &params);
  245. if (ret)
  246. {
  247. SE_TRACE(SE_TRACE_ERROR, "MODIFY_TYPE failed %d\n", errno);
  248. return error_driver2urts(ret);
  249. }
  250. return SGX_SUCCESS;
  251. }
  252. int EnclaveCreatorHW::trim_range(uint64_t fromaddr, uint64_t toaddr)
  253. {
  254. sgx_range params;
  255. memset(&params, 0 ,sizeof(sgx_range));
  256. params.start_addr = (unsigned long)fromaddr;
  257. params.nr_pages = (unsigned int)((toaddr - fromaddr)/SE_PAGE_SIZE);
  258. int ret= ioctl(m_hdevice, SGX_IOC_ENCLAVE_TRIM, &params);
  259. if (ret)
  260. {
  261. SE_TRACE(SE_TRACE_ERROR, "SGX_IOC_ENCLAVE_TRIM failed %d\n", errno);
  262. return error_driver2urts(ret);
  263. }
  264. return SGX_SUCCESS;
  265. }
  266. int EnclaveCreatorHW::trim_accept(uint64_t addr)
  267. {
  268. sgx_range params;
  269. memset(&params, 0 ,sizeof(sgx_range));
  270. params.start_addr = (unsigned long)addr;
  271. params.nr_pages = 1;
  272. int ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_NOTIFY_ACCEPT, &params);
  273. if (ret)
  274. {
  275. SE_TRACE(SE_TRACE_ERROR, "TRIM_RANGE_COMMIT failed %d\n", errno);
  276. return error_driver2urts(ret);
  277. }
  278. return SGX_SUCCESS;
  279. }
  280. int EnclaveCreatorHW::remove_range(uint64_t fromaddr, uint64_t numpages)
  281. {
  282. int ret = -1;
  283. uint64_t i;
  284. unsigned long start;
  285. for (i = 0; i < numpages; i++)
  286. {
  287. start = (unsigned long)fromaddr + (unsigned long)(i << SE_PAGE_SHIFT);
  288. ret = ioctl(m_hdevice, SGX_IOC_ENCLAVE_PAGE_REMOVE, &start);
  289. if (ret)
  290. {
  291. SE_TRACE(SE_TRACE_ERROR, "PAGE_REMOVE failed %d\n", errno);
  292. return error_driver2urts(ret);
  293. }
  294. }
  295. return SGX_SUCCESS;
  296. }
  297. //EDMM is supported if and only if all of the following requirements are met:
  298. //1. We operate in HW mode
  299. //2. CPU has EDMM support
  300. //3. Driver has EDMM support
  301. //4. Both the uRTS version and enclave (metadata) version are higher than 1.5
  302. bool EnclaveCreatorHW::is_EDMM_supported(sgx_enclave_id_t enclave_id)
  303. {
  304. bool supported = false, driver_supported = false, cpu_edmm = false;
  305. CEnclave *enclave = CEnclavePool::instance()->get_enclave(enclave_id);
  306. if (enclave == NULL)
  307. return false;
  308. cpu_edmm = is_cpu_support_edmm();
  309. driver_supported = is_driver_compatible();
  310. //return value of get_enclave_version() considers the version of uRTS and enclave metadata
  311. supported = use_se_hw() && cpu_edmm && driver_supported && (enclave->get_enclave_version() >= SDK_VERSION_2_0);
  312. return supported;
  313. }
  314. bool EnclaveCreatorHW::is_driver_compatible()
  315. {
  316. open_se_device();
  317. return is_driver_support_edmm(m_hdevice);
  318. }
  319. bool EnclaveCreatorHW::is_in_kernel_driver()
  320. {
  321. open_se_device();
  322. return m_in_kernel_driver;
  323. }