enclave_creator_hw.cpp 11 KB

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