ioctl.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2016-20 Intel Corporation. */
  3. #include <asm/mman.h>
  4. #include <asm/sgx.h>
  5. #include <linux/mman.h>
  6. #include <linux/delay.h>
  7. #include <linux/file.h>
  8. #include <linux/hashtable.h>
  9. #include <linux/highmem.h>
  10. #include <linux/ratelimit.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/shmem_fs.h>
  13. #include <linux/slab.h>
  14. #include <linux/suspend.h>
  15. #include "driver.h"
  16. #include "encl.h"
  17. #include "encls.h"
  18. struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl, bool reclaim)
  19. {
  20. struct sgx_va_page *va_page = NULL;
  21. void *err;
  22. BUILD_BUG_ON(SGX_VA_SLOT_COUNT !=
  23. (SGX_ENCL_PAGE_VA_OFFSET_MASK >> 3) + 1);
  24. if (!(encl->page_cnt % SGX_VA_SLOT_COUNT)) {
  25. va_page = kzalloc(sizeof(*va_page), GFP_KERNEL);
  26. if (!va_page)
  27. return ERR_PTR(-ENOMEM);
  28. va_page->epc_page = sgx_alloc_va_page(reclaim);
  29. if (IS_ERR(va_page->epc_page)) {
  30. err = ERR_CAST(va_page->epc_page);
  31. kfree(va_page);
  32. return err;
  33. }
  34. WARN_ON_ONCE(encl->page_cnt % SGX_VA_SLOT_COUNT);
  35. }
  36. encl->page_cnt++;
  37. return va_page;
  38. }
  39. void sgx_encl_shrink(struct sgx_encl *encl, struct sgx_va_page *va_page)
  40. {
  41. encl->page_cnt--;
  42. if (va_page) {
  43. sgx_encl_free_epc_page(va_page->epc_page);
  44. list_del(&va_page->list);
  45. kfree(va_page);
  46. }
  47. }
  48. static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
  49. {
  50. struct sgx_epc_page *secs_epc;
  51. struct sgx_va_page *va_page;
  52. struct sgx_pageinfo pginfo;
  53. struct sgx_secinfo secinfo;
  54. unsigned long encl_size;
  55. struct file *backing;
  56. long ret;
  57. /*
  58. * ECREATE would detect this too, but checking here also ensures
  59. * that the 'encl_size' calculations below can never overflow.
  60. */
  61. if (!is_power_of_2(secs->size))
  62. return -EINVAL;
  63. va_page = sgx_encl_grow(encl, true);
  64. if (IS_ERR(va_page))
  65. return PTR_ERR(va_page);
  66. else if (va_page)
  67. list_add(&va_page->list, &encl->va_pages);
  68. /* else the tail page of the VA page list had free slots. */
  69. /* The extra page goes to SECS. */
  70. encl_size = secs->size + PAGE_SIZE;
  71. backing = shmem_file_setup("SGX backing", encl_size + (encl_size >> 5),
  72. VM_NORESERVE);
  73. if (IS_ERR(backing)) {
  74. ret = PTR_ERR(backing);
  75. goto err_out_shrink;
  76. }
  77. encl->backing = backing;
  78. secs_epc = sgx_alloc_epc_page(&encl->secs, true);
  79. if (IS_ERR(secs_epc)) {
  80. ret = PTR_ERR(secs_epc);
  81. goto err_out_backing;
  82. }
  83. encl->secs.epc_page = secs_epc;
  84. pginfo.addr = 0;
  85. pginfo.contents = (unsigned long)secs;
  86. pginfo.metadata = (unsigned long)&secinfo;
  87. pginfo.secs = 0;
  88. memset(&secinfo, 0, sizeof(secinfo));
  89. ret = __ecreate((void *)&pginfo, sgx_get_epc_virt_addr(secs_epc));
  90. if (ret) {
  91. ret = -EIO;
  92. goto err_out;
  93. }
  94. if (secs->attributes & SGX_ATTR_DEBUG)
  95. set_bit(SGX_ENCL_DEBUG, &encl->flags);
  96. encl->secs.encl = encl;
  97. encl->secs.type = SGX_PAGE_TYPE_SECS;
  98. encl->base = secs->base;
  99. encl->size = secs->size;
  100. encl->attributes = secs->attributes;
  101. encl->attributes_mask = SGX_ATTR_UNPRIV_MASK;
  102. /* Set only after completion, as encl->lock has not been taken. */
  103. set_bit(SGX_ENCL_CREATED, &encl->flags);
  104. return 0;
  105. err_out:
  106. sgx_encl_free_epc_page(encl->secs.epc_page);
  107. encl->secs.epc_page = NULL;
  108. err_out_backing:
  109. fput(encl->backing);
  110. encl->backing = NULL;
  111. err_out_shrink:
  112. sgx_encl_shrink(encl, va_page);
  113. return ret;
  114. }
  115. /**
  116. * sgx_ioc_enclave_create() - handler for %SGX_IOC_ENCLAVE_CREATE
  117. * @encl: An enclave pointer.
  118. * @arg: The ioctl argument.
  119. *
  120. * Allocate kernel data structures for the enclave and invoke ECREATE.
  121. *
  122. * Return:
  123. * - 0: Success.
  124. * - -EIO: ECREATE failed.
  125. * - -errno: POSIX error.
  126. */
  127. static long sgx_ioc_enclave_create(struct sgx_encl *encl, void __user *arg)
  128. {
  129. struct sgx_enclave_create create_arg;
  130. void *secs;
  131. int ret;
  132. if (test_bit(SGX_ENCL_CREATED, &encl->flags))
  133. return -EINVAL;
  134. if (copy_from_user(&create_arg, arg, sizeof(create_arg)))
  135. return -EFAULT;
  136. secs = kmalloc(PAGE_SIZE, GFP_KERNEL);
  137. if (!secs)
  138. return -ENOMEM;
  139. if (copy_from_user(secs, (void __user *)create_arg.src, PAGE_SIZE))
  140. ret = -EFAULT;
  141. else
  142. ret = sgx_encl_create(encl, secs);
  143. kfree(secs);
  144. return ret;
  145. }
  146. static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
  147. {
  148. u64 perm = secinfo->flags & SGX_SECINFO_PERMISSION_MASK;
  149. u64 pt = secinfo->flags & SGX_SECINFO_PAGE_TYPE_MASK;
  150. if (pt != SGX_SECINFO_REG && pt != SGX_SECINFO_TCS)
  151. return -EINVAL;
  152. if ((perm & SGX_SECINFO_W) && !(perm & SGX_SECINFO_R))
  153. return -EINVAL;
  154. /*
  155. * CPU will silently overwrite the permissions as zero, which means
  156. * that we need to validate it ourselves.
  157. */
  158. if (pt == SGX_SECINFO_TCS && perm)
  159. return -EINVAL;
  160. if (secinfo->flags & SGX_SECINFO_RESERVED_MASK)
  161. return -EINVAL;
  162. if (memchr_inv(secinfo->reserved, 0, sizeof(secinfo->reserved)))
  163. return -EINVAL;
  164. return 0;
  165. }
  166. static int __sgx_encl_add_page(struct sgx_encl *encl,
  167. struct sgx_encl_page *encl_page,
  168. struct sgx_epc_page *epc_page,
  169. struct sgx_secinfo *secinfo, unsigned long src)
  170. {
  171. struct sgx_pageinfo pginfo;
  172. struct vm_area_struct *vma;
  173. struct page *src_page;
  174. int ret;
  175. /* Deny noexec. */
  176. vma = find_vma(current->mm, src);
  177. if (!vma)
  178. return -EFAULT;
  179. if (!(vma->vm_flags & VM_MAYEXEC))
  180. return -EACCES;
  181. ret = get_user_pages(src, 1, 0, &src_page);
  182. if (ret < 1)
  183. return -EFAULT;
  184. pginfo.secs = (unsigned long)sgx_get_epc_virt_addr(encl->secs.epc_page);
  185. pginfo.addr = encl_page->desc & PAGE_MASK;
  186. pginfo.metadata = (unsigned long)secinfo;
  187. pginfo.contents = (unsigned long)kmap_local_page(src_page);
  188. ret = __eadd(&pginfo, sgx_get_epc_virt_addr(epc_page));
  189. kunmap_local((void *)pginfo.contents);
  190. put_page(src_page);
  191. return ret ? -EIO : 0;
  192. }
  193. /*
  194. * If the caller requires measurement of the page as a proof for the content,
  195. * use EEXTEND to add a measurement for 256 bytes of the page. Repeat this
  196. * operation until the entire page is measured."
  197. */
  198. static int __sgx_encl_extend(struct sgx_encl *encl,
  199. struct sgx_epc_page *epc_page)
  200. {
  201. unsigned long offset;
  202. int ret;
  203. for (offset = 0; offset < PAGE_SIZE; offset += SGX_EEXTEND_BLOCK_SIZE) {
  204. ret = __eextend(sgx_get_epc_virt_addr(encl->secs.epc_page),
  205. sgx_get_epc_virt_addr(epc_page) + offset);
  206. if (ret) {
  207. if (encls_failed(ret))
  208. ENCLS_WARN(ret, "EEXTEND");
  209. return -EIO;
  210. }
  211. }
  212. return 0;
  213. }
  214. static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
  215. unsigned long offset, struct sgx_secinfo *secinfo,
  216. unsigned long flags)
  217. {
  218. struct sgx_encl_page *encl_page;
  219. struct sgx_epc_page *epc_page;
  220. struct sgx_va_page *va_page;
  221. int ret;
  222. encl_page = sgx_encl_page_alloc(encl, offset, secinfo->flags);
  223. if (IS_ERR(encl_page))
  224. return PTR_ERR(encl_page);
  225. epc_page = sgx_alloc_epc_page(encl_page, true);
  226. if (IS_ERR(epc_page)) {
  227. kfree(encl_page);
  228. return PTR_ERR(epc_page);
  229. }
  230. va_page = sgx_encl_grow(encl, true);
  231. if (IS_ERR(va_page)) {
  232. ret = PTR_ERR(va_page);
  233. goto err_out_free;
  234. }
  235. mmap_read_lock(current->mm);
  236. mutex_lock(&encl->lock);
  237. /*
  238. * Adding to encl->va_pages must be done under encl->lock. Ditto for
  239. * deleting (via sgx_encl_shrink()) in the error path.
  240. */
  241. if (va_page)
  242. list_add(&va_page->list, &encl->va_pages);
  243. /*
  244. * Insert prior to EADD in case of OOM. EADD modifies MRENCLAVE, i.e.
  245. * can't be gracefully unwound, while failure on EADD/EXTEND is limited
  246. * to userspace errors (or kernel/hardware bugs).
  247. */
  248. ret = xa_insert(&encl->page_array, PFN_DOWN(encl_page->desc),
  249. encl_page, GFP_KERNEL);
  250. if (ret)
  251. goto err_out_unlock;
  252. ret = __sgx_encl_add_page(encl, encl_page, epc_page, secinfo,
  253. src);
  254. if (ret)
  255. goto err_out;
  256. /*
  257. * Complete the "add" before doing the "extend" so that the "add"
  258. * isn't in a half-baked state in the extremely unlikely scenario
  259. * the enclave will be destroyed in response to EEXTEND failure.
  260. */
  261. encl_page->encl = encl;
  262. encl_page->epc_page = epc_page;
  263. encl_page->type = (secinfo->flags & SGX_SECINFO_PAGE_TYPE_MASK) >> 8;
  264. encl->secs_child_cnt++;
  265. if (flags & SGX_PAGE_MEASURE) {
  266. ret = __sgx_encl_extend(encl, epc_page);
  267. if (ret)
  268. goto err_out;
  269. }
  270. sgx_mark_page_reclaimable(encl_page->epc_page);
  271. mutex_unlock(&encl->lock);
  272. mmap_read_unlock(current->mm);
  273. return ret;
  274. err_out:
  275. xa_erase(&encl->page_array, PFN_DOWN(encl_page->desc));
  276. err_out_unlock:
  277. sgx_encl_shrink(encl, va_page);
  278. mutex_unlock(&encl->lock);
  279. mmap_read_unlock(current->mm);
  280. err_out_free:
  281. sgx_encl_free_epc_page(epc_page);
  282. kfree(encl_page);
  283. return ret;
  284. }
  285. /*
  286. * Ensure user provided offset and length values are valid for
  287. * an enclave.
  288. */
  289. static int sgx_validate_offset_length(struct sgx_encl *encl,
  290. unsigned long offset,
  291. unsigned long length)
  292. {
  293. if (!IS_ALIGNED(offset, PAGE_SIZE))
  294. return -EINVAL;
  295. if (!length || !IS_ALIGNED(length, PAGE_SIZE))
  296. return -EINVAL;
  297. if (offset + length < offset)
  298. return -EINVAL;
  299. if (offset + length - PAGE_SIZE >= encl->size)
  300. return -EINVAL;
  301. return 0;
  302. }
  303. /**
  304. * sgx_ioc_enclave_add_pages() - The handler for %SGX_IOC_ENCLAVE_ADD_PAGES
  305. * @encl: an enclave pointer
  306. * @arg: a user pointer to a struct sgx_enclave_add_pages instance
  307. *
  308. * Add one or more pages to an uninitialized enclave, and optionally extend the
  309. * measurement with the contents of the page. The SECINFO and measurement mask
  310. * are applied to all pages.
  311. *
  312. * A SECINFO for a TCS is required to always contain zero permissions because
  313. * CPU silently zeros them. Allowing anything else would cause a mismatch in
  314. * the measurement.
  315. *
  316. * mmap()'s protection bits are capped by the page permissions. For each page
  317. * address, the maximum protection bits are computed with the following
  318. * heuristics:
  319. *
  320. * 1. A regular page: PROT_R, PROT_W and PROT_X match the SECINFO permissions.
  321. * 2. A TCS page: PROT_R | PROT_W.
  322. *
  323. * mmap() is not allowed to surpass the minimum of the maximum protection bits
  324. * within the given address range.
  325. *
  326. * The function deinitializes kernel data structures for enclave and returns
  327. * -EIO in any of the following conditions:
  328. *
  329. * - Enclave Page Cache (EPC), the physical memory holding enclaves, has
  330. * been invalidated. This will cause EADD and EEXTEND to fail.
  331. * - If the source address is corrupted somehow when executing EADD.
  332. *
  333. * Return:
  334. * - 0: Success.
  335. * - -EACCES: The source page is located in a noexec partition.
  336. * - -ENOMEM: Out of EPC pages.
  337. * - -EINTR: The call was interrupted before data was processed.
  338. * - -EIO: Either EADD or EEXTEND failed because invalid source address
  339. * or power cycle.
  340. * - -errno: POSIX error.
  341. */
  342. static long sgx_ioc_enclave_add_pages(struct sgx_encl *encl, void __user *arg)
  343. {
  344. struct sgx_enclave_add_pages add_arg;
  345. struct sgx_secinfo secinfo;
  346. unsigned long c;
  347. int ret;
  348. if (!test_bit(SGX_ENCL_CREATED, &encl->flags) ||
  349. test_bit(SGX_ENCL_INITIALIZED, &encl->flags))
  350. return -EINVAL;
  351. if (copy_from_user(&add_arg, arg, sizeof(add_arg)))
  352. return -EFAULT;
  353. if (!IS_ALIGNED(add_arg.src, PAGE_SIZE))
  354. return -EINVAL;
  355. if (sgx_validate_offset_length(encl, add_arg.offset, add_arg.length))
  356. return -EINVAL;
  357. if (copy_from_user(&secinfo, (void __user *)add_arg.secinfo,
  358. sizeof(secinfo)))
  359. return -EFAULT;
  360. if (sgx_validate_secinfo(&secinfo))
  361. return -EINVAL;
  362. for (c = 0 ; c < add_arg.length; c += PAGE_SIZE) {
  363. if (signal_pending(current)) {
  364. if (!c)
  365. ret = -ERESTARTSYS;
  366. break;
  367. }
  368. if (need_resched())
  369. cond_resched();
  370. ret = sgx_encl_add_page(encl, add_arg.src + c, add_arg.offset + c,
  371. &secinfo, add_arg.flags);
  372. if (ret)
  373. break;
  374. }
  375. add_arg.count = c;
  376. if (copy_to_user(arg, &add_arg, sizeof(add_arg)))
  377. return -EFAULT;
  378. return ret;
  379. }
  380. static int __sgx_get_key_hash(struct crypto_shash *tfm, const void *modulus,
  381. void *hash)
  382. {
  383. SHASH_DESC_ON_STACK(shash, tfm);
  384. shash->tfm = tfm;
  385. return crypto_shash_digest(shash, modulus, SGX_MODULUS_SIZE, hash);
  386. }
  387. static int sgx_get_key_hash(const void *modulus, void *hash)
  388. {
  389. struct crypto_shash *tfm;
  390. int ret;
  391. tfm = crypto_alloc_shash("sha256", 0, CRYPTO_ALG_ASYNC);
  392. if (IS_ERR(tfm))
  393. return PTR_ERR(tfm);
  394. ret = __sgx_get_key_hash(tfm, modulus, hash);
  395. crypto_free_shash(tfm);
  396. return ret;
  397. }
  398. static int sgx_encl_init(struct sgx_encl *encl, struct sgx_sigstruct *sigstruct,
  399. void *token)
  400. {
  401. u64 mrsigner[4];
  402. int i, j;
  403. void *addr;
  404. int ret;
  405. /*
  406. * Deny initializing enclaves with attributes (namely provisioning)
  407. * that have not been explicitly allowed.
  408. */
  409. if (encl->attributes & ~encl->attributes_mask)
  410. return -EACCES;
  411. /*
  412. * Attributes should not be enforced *only* against what's available on
  413. * platform (done in sgx_encl_create) but checked and enforced against
  414. * the mask for enforcement in sigstruct. For example an enclave could
  415. * opt to sign with AVX bit in xfrm, but still be loadable on a platform
  416. * without it if the sigstruct->body.attributes_mask does not turn that
  417. * bit on.
  418. */
  419. if (sigstruct->body.attributes & sigstruct->body.attributes_mask &
  420. sgx_attributes_reserved_mask)
  421. return -EINVAL;
  422. if (sigstruct->body.miscselect & sigstruct->body.misc_mask &
  423. sgx_misc_reserved_mask)
  424. return -EINVAL;
  425. if (sigstruct->body.xfrm & sigstruct->body.xfrm_mask &
  426. sgx_xfrm_reserved_mask)
  427. return -EINVAL;
  428. ret = sgx_get_key_hash(sigstruct->modulus, mrsigner);
  429. if (ret)
  430. return ret;
  431. mutex_lock(&encl->lock);
  432. /*
  433. * ENCLS[EINIT] is interruptible because it has such a high latency,
  434. * e.g. 50k+ cycles on success. If an IRQ/NMI/SMI becomes pending,
  435. * EINIT may fail with SGX_UNMASKED_EVENT so that the event can be
  436. * serviced.
  437. */
  438. for (i = 0; i < SGX_EINIT_SLEEP_COUNT; i++) {
  439. for (j = 0; j < SGX_EINIT_SPIN_COUNT; j++) {
  440. addr = sgx_get_epc_virt_addr(encl->secs.epc_page);
  441. preempt_disable();
  442. sgx_update_lepubkeyhash(mrsigner);
  443. ret = __einit(sigstruct, token, addr);
  444. preempt_enable();
  445. if (ret == SGX_UNMASKED_EVENT)
  446. continue;
  447. else
  448. break;
  449. }
  450. if (ret != SGX_UNMASKED_EVENT)
  451. break;
  452. msleep_interruptible(SGX_EINIT_SLEEP_TIME);
  453. if (signal_pending(current)) {
  454. ret = -ERESTARTSYS;
  455. goto err_out;
  456. }
  457. }
  458. if (encls_faulted(ret)) {
  459. if (encls_failed(ret))
  460. ENCLS_WARN(ret, "EINIT");
  461. ret = -EIO;
  462. } else if (ret) {
  463. pr_debug("EINIT returned %d\n", ret);
  464. ret = -EPERM;
  465. } else {
  466. set_bit(SGX_ENCL_INITIALIZED, &encl->flags);
  467. }
  468. err_out:
  469. mutex_unlock(&encl->lock);
  470. return ret;
  471. }
  472. /**
  473. * sgx_ioc_enclave_init() - handler for %SGX_IOC_ENCLAVE_INIT
  474. * @encl: an enclave pointer
  475. * @arg: userspace pointer to a struct sgx_enclave_init instance
  476. *
  477. * Flush any outstanding enqueued EADD operations and perform EINIT. The
  478. * Launch Enclave Public Key Hash MSRs are rewritten as necessary to match
  479. * the enclave's MRSIGNER, which is calculated from the provided sigstruct.
  480. *
  481. * Return:
  482. * - 0: Success.
  483. * - -EPERM: Invalid SIGSTRUCT.
  484. * - -EIO: EINIT failed because of a power cycle.
  485. * - -errno: POSIX error.
  486. */
  487. static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
  488. {
  489. struct sgx_sigstruct *sigstruct;
  490. struct sgx_enclave_init init_arg;
  491. void *token;
  492. int ret;
  493. if (!test_bit(SGX_ENCL_CREATED, &encl->flags) ||
  494. test_bit(SGX_ENCL_INITIALIZED, &encl->flags))
  495. return -EINVAL;
  496. if (copy_from_user(&init_arg, arg, sizeof(init_arg)))
  497. return -EFAULT;
  498. /*
  499. * 'sigstruct' must be on a page boundary and 'token' on a 512 byte
  500. * boundary. kmalloc() will give this alignment when allocating
  501. * PAGE_SIZE bytes.
  502. */
  503. sigstruct = kmalloc(PAGE_SIZE, GFP_KERNEL);
  504. if (!sigstruct)
  505. return -ENOMEM;
  506. token = (void *)((unsigned long)sigstruct + PAGE_SIZE / 2);
  507. memset(token, 0, SGX_LAUNCH_TOKEN_SIZE);
  508. if (copy_from_user(sigstruct, (void __user *)init_arg.sigstruct,
  509. sizeof(*sigstruct))) {
  510. ret = -EFAULT;
  511. goto out;
  512. }
  513. /*
  514. * A legacy field used with Intel signed enclaves. These used to mean
  515. * regular and architectural enclaves. The CPU only accepts these values
  516. * but they do not have any other meaning.
  517. *
  518. * Thus, reject any other values.
  519. */
  520. if (sigstruct->header.vendor != 0x0000 &&
  521. sigstruct->header.vendor != 0x8086) {
  522. ret = -EINVAL;
  523. goto out;
  524. }
  525. ret = sgx_encl_init(encl, sigstruct, token);
  526. out:
  527. kfree(sigstruct);
  528. return ret;
  529. }
  530. /**
  531. * sgx_ioc_enclave_provision() - handler for %SGX_IOC_ENCLAVE_PROVISION
  532. * @encl: an enclave pointer
  533. * @arg: userspace pointer to a struct sgx_enclave_provision instance
  534. *
  535. * Allow ATTRIBUTE.PROVISION_KEY for an enclave by providing a file handle to
  536. * /dev/sgx_provision.
  537. *
  538. * Return:
  539. * - 0: Success.
  540. * - -errno: Otherwise.
  541. */
  542. static long sgx_ioc_enclave_provision(struct sgx_encl *encl, void __user *arg)
  543. {
  544. struct sgx_enclave_provision params;
  545. if (copy_from_user(&params, arg, sizeof(params)))
  546. return -EFAULT;
  547. return sgx_set_attribute(&encl->attributes_mask, params.fd);
  548. }
  549. /*
  550. * Ensure enclave is ready for SGX2 functions. Readiness is checked
  551. * by ensuring the hardware supports SGX2 and the enclave is initialized
  552. * and thus able to handle requests to modify pages within it.
  553. */
  554. static int sgx_ioc_sgx2_ready(struct sgx_encl *encl)
  555. {
  556. if (!(cpu_feature_enabled(X86_FEATURE_SGX2)))
  557. return -ENODEV;
  558. if (!test_bit(SGX_ENCL_INITIALIZED, &encl->flags))
  559. return -EINVAL;
  560. return 0;
  561. }
  562. /*
  563. * Some SGX functions require that no cached linear-to-physical address
  564. * mappings are present before they can succeed. Collaborate with
  565. * hardware via ENCLS[ETRACK] to ensure that all cached
  566. * linear-to-physical address mappings belonging to all threads of
  567. * the enclave are cleared. See sgx_encl_cpumask() for details.
  568. *
  569. * Must be called with enclave's mutex held from the time the
  570. * SGX function requiring that no cached linear-to-physical mappings
  571. * are present is executed until this ETRACK flow is complete.
  572. */
  573. static int sgx_enclave_etrack(struct sgx_encl *encl)
  574. {
  575. void *epc_virt;
  576. int ret;
  577. epc_virt = sgx_get_epc_virt_addr(encl->secs.epc_page);
  578. ret = __etrack(epc_virt);
  579. if (ret) {
  580. /*
  581. * ETRACK only fails when there is an OS issue. For
  582. * example, two consecutive ETRACK was sent without
  583. * completed IPI between.
  584. */
  585. pr_err_once("ETRACK returned %d (0x%x)", ret, ret);
  586. /*
  587. * Send IPIs to kick CPUs out of the enclave and
  588. * try ETRACK again.
  589. */
  590. on_each_cpu_mask(sgx_encl_cpumask(encl), sgx_ipi_cb, NULL, 1);
  591. ret = __etrack(epc_virt);
  592. if (ret) {
  593. pr_err_once("ETRACK repeat returned %d (0x%x)",
  594. ret, ret);
  595. return -EFAULT;
  596. }
  597. }
  598. on_each_cpu_mask(sgx_encl_cpumask(encl), sgx_ipi_cb, NULL, 1);
  599. return 0;
  600. }
  601. /**
  602. * sgx_enclave_restrict_permissions() - Restrict EPCM permissions
  603. * @encl: Enclave to which the pages belong.
  604. * @modp: Checked parameters from user on which pages need modifying and
  605. * their new permissions.
  606. *
  607. * Return:
  608. * - 0: Success.
  609. * - -errno: Otherwise.
  610. */
  611. static long
  612. sgx_enclave_restrict_permissions(struct sgx_encl *encl,
  613. struct sgx_enclave_restrict_permissions *modp)
  614. {
  615. struct sgx_encl_page *entry;
  616. struct sgx_secinfo secinfo;
  617. unsigned long addr;
  618. unsigned long c;
  619. void *epc_virt;
  620. int ret;
  621. memset(&secinfo, 0, sizeof(secinfo));
  622. secinfo.flags = modp->permissions & SGX_SECINFO_PERMISSION_MASK;
  623. for (c = 0 ; c < modp->length; c += PAGE_SIZE) {
  624. addr = encl->base + modp->offset + c;
  625. sgx_reclaim_direct();
  626. mutex_lock(&encl->lock);
  627. entry = sgx_encl_load_page(encl, addr);
  628. if (IS_ERR(entry)) {
  629. ret = PTR_ERR(entry) == -EBUSY ? -EAGAIN : -EFAULT;
  630. goto out_unlock;
  631. }
  632. /*
  633. * Changing EPCM permissions is only supported on regular
  634. * SGX pages. Attempting this change on other pages will
  635. * result in #PF.
  636. */
  637. if (entry->type != SGX_PAGE_TYPE_REG) {
  638. ret = -EINVAL;
  639. goto out_unlock;
  640. }
  641. /*
  642. * Apart from ensuring that read-access remains, do not verify
  643. * the permission bits requested. Kernel has no control over
  644. * how EPCM permissions can be relaxed from within the enclave.
  645. * ENCLS[EMODPR] can only remove existing EPCM permissions,
  646. * attempting to set new permissions will be ignored by the
  647. * hardware.
  648. */
  649. /* Change EPCM permissions. */
  650. epc_virt = sgx_get_epc_virt_addr(entry->epc_page);
  651. ret = __emodpr(&secinfo, epc_virt);
  652. if (encls_faulted(ret)) {
  653. /*
  654. * All possible faults should be avoidable:
  655. * parameters have been checked, will only change
  656. * permissions of a regular page, and no concurrent
  657. * SGX1/SGX2 ENCLS instructions since these
  658. * are protected with mutex.
  659. */
  660. pr_err_once("EMODPR encountered exception %d\n",
  661. ENCLS_TRAPNR(ret));
  662. ret = -EFAULT;
  663. goto out_unlock;
  664. }
  665. if (encls_failed(ret)) {
  666. modp->result = ret;
  667. ret = -EFAULT;
  668. goto out_unlock;
  669. }
  670. ret = sgx_enclave_etrack(encl);
  671. if (ret) {
  672. ret = -EFAULT;
  673. goto out_unlock;
  674. }
  675. mutex_unlock(&encl->lock);
  676. }
  677. ret = 0;
  678. goto out;
  679. out_unlock:
  680. mutex_unlock(&encl->lock);
  681. out:
  682. modp->count = c;
  683. return ret;
  684. }
  685. /**
  686. * sgx_ioc_enclave_restrict_permissions() - handler for
  687. * %SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS
  688. * @encl: an enclave pointer
  689. * @arg: userspace pointer to a &struct sgx_enclave_restrict_permissions
  690. * instance
  691. *
  692. * SGX2 distinguishes between relaxing and restricting the enclave page
  693. * permissions maintained by the hardware (EPCM permissions) of pages
  694. * belonging to an initialized enclave (after SGX_IOC_ENCLAVE_INIT).
  695. *
  696. * EPCM permissions cannot be restricted from within the enclave, the enclave
  697. * requires the kernel to run the privileged level 0 instructions ENCLS[EMODPR]
  698. * and ENCLS[ETRACK]. An attempt to relax EPCM permissions with this call
  699. * will be ignored by the hardware.
  700. *
  701. * Return:
  702. * - 0: Success
  703. * - -errno: Otherwise
  704. */
  705. static long sgx_ioc_enclave_restrict_permissions(struct sgx_encl *encl,
  706. void __user *arg)
  707. {
  708. struct sgx_enclave_restrict_permissions params;
  709. long ret;
  710. ret = sgx_ioc_sgx2_ready(encl);
  711. if (ret)
  712. return ret;
  713. if (copy_from_user(&params, arg, sizeof(params)))
  714. return -EFAULT;
  715. if (sgx_validate_offset_length(encl, params.offset, params.length))
  716. return -EINVAL;
  717. if (params.permissions & ~SGX_SECINFO_PERMISSION_MASK)
  718. return -EINVAL;
  719. /*
  720. * Fail early if invalid permissions requested to prevent ENCLS[EMODPR]
  721. * from faulting later when the CPU does the same check.
  722. */
  723. if ((params.permissions & SGX_SECINFO_W) &&
  724. !(params.permissions & SGX_SECINFO_R))
  725. return -EINVAL;
  726. if (params.result || params.count)
  727. return -EINVAL;
  728. ret = sgx_enclave_restrict_permissions(encl, &params);
  729. if (copy_to_user(arg, &params, sizeof(params)))
  730. return -EFAULT;
  731. return ret;
  732. }
  733. /**
  734. * sgx_enclave_modify_types() - Modify type of SGX enclave pages
  735. * @encl: Enclave to which the pages belong.
  736. * @modt: Checked parameters from user about which pages need modifying
  737. * and their new page type.
  738. *
  739. * Return:
  740. * - 0: Success
  741. * - -errno: Otherwise
  742. */
  743. static long sgx_enclave_modify_types(struct sgx_encl *encl,
  744. struct sgx_enclave_modify_types *modt)
  745. {
  746. unsigned long max_prot_restore;
  747. enum sgx_page_type page_type;
  748. struct sgx_encl_page *entry;
  749. struct sgx_secinfo secinfo;
  750. unsigned long prot;
  751. unsigned long addr;
  752. unsigned long c;
  753. void *epc_virt;
  754. int ret;
  755. page_type = modt->page_type & SGX_PAGE_TYPE_MASK;
  756. /*
  757. * The only new page types allowed by hardware are PT_TCS and PT_TRIM.
  758. */
  759. if (page_type != SGX_PAGE_TYPE_TCS && page_type != SGX_PAGE_TYPE_TRIM)
  760. return -EINVAL;
  761. memset(&secinfo, 0, sizeof(secinfo));
  762. secinfo.flags = page_type << 8;
  763. for (c = 0 ; c < modt->length; c += PAGE_SIZE) {
  764. addr = encl->base + modt->offset + c;
  765. sgx_reclaim_direct();
  766. mutex_lock(&encl->lock);
  767. entry = sgx_encl_load_page(encl, addr);
  768. if (IS_ERR(entry)) {
  769. ret = PTR_ERR(entry) == -EBUSY ? -EAGAIN : -EFAULT;
  770. goto out_unlock;
  771. }
  772. /*
  773. * Borrow the logic from the Intel SDM. Regular pages
  774. * (SGX_PAGE_TYPE_REG) can change type to SGX_PAGE_TYPE_TCS
  775. * or SGX_PAGE_TYPE_TRIM but TCS pages can only be trimmed.
  776. * CET pages not supported yet.
  777. */
  778. if (!(entry->type == SGX_PAGE_TYPE_REG ||
  779. (entry->type == SGX_PAGE_TYPE_TCS &&
  780. page_type == SGX_PAGE_TYPE_TRIM))) {
  781. ret = -EINVAL;
  782. goto out_unlock;
  783. }
  784. max_prot_restore = entry->vm_max_prot_bits;
  785. /*
  786. * Once a regular page becomes a TCS page it cannot be
  787. * changed back. So the maximum allowed protection reflects
  788. * the TCS page that is always RW from kernel perspective but
  789. * will be inaccessible from within enclave. Before doing
  790. * so, do make sure that the new page type continues to
  791. * respect the originally vetted page permissions.
  792. */
  793. if (entry->type == SGX_PAGE_TYPE_REG &&
  794. page_type == SGX_PAGE_TYPE_TCS) {
  795. if (~entry->vm_max_prot_bits & (VM_READ | VM_WRITE)) {
  796. ret = -EPERM;
  797. goto out_unlock;
  798. }
  799. prot = PROT_READ | PROT_WRITE;
  800. entry->vm_max_prot_bits = calc_vm_prot_bits(prot, 0);
  801. /*
  802. * Prevent page from being reclaimed while mutex
  803. * is released.
  804. */
  805. if (sgx_unmark_page_reclaimable(entry->epc_page)) {
  806. ret = -EAGAIN;
  807. goto out_entry_changed;
  808. }
  809. /*
  810. * Do not keep encl->lock because of dependency on
  811. * mmap_lock acquired in sgx_zap_enclave_ptes().
  812. */
  813. mutex_unlock(&encl->lock);
  814. sgx_zap_enclave_ptes(encl, addr);
  815. mutex_lock(&encl->lock);
  816. sgx_mark_page_reclaimable(entry->epc_page);
  817. }
  818. /* Change EPC type */
  819. epc_virt = sgx_get_epc_virt_addr(entry->epc_page);
  820. ret = __emodt(&secinfo, epc_virt);
  821. if (encls_faulted(ret)) {
  822. /*
  823. * All possible faults should be avoidable:
  824. * parameters have been checked, will only change
  825. * valid page types, and no concurrent
  826. * SGX1/SGX2 ENCLS instructions since these are
  827. * protected with mutex.
  828. */
  829. pr_err_once("EMODT encountered exception %d\n",
  830. ENCLS_TRAPNR(ret));
  831. ret = -EFAULT;
  832. goto out_entry_changed;
  833. }
  834. if (encls_failed(ret)) {
  835. modt->result = ret;
  836. ret = -EFAULT;
  837. goto out_entry_changed;
  838. }
  839. ret = sgx_enclave_etrack(encl);
  840. if (ret) {
  841. ret = -EFAULT;
  842. goto out_unlock;
  843. }
  844. entry->type = page_type;
  845. mutex_unlock(&encl->lock);
  846. }
  847. ret = 0;
  848. goto out;
  849. out_entry_changed:
  850. entry->vm_max_prot_bits = max_prot_restore;
  851. out_unlock:
  852. mutex_unlock(&encl->lock);
  853. out:
  854. modt->count = c;
  855. return ret;
  856. }
  857. /**
  858. * sgx_ioc_enclave_modify_types() - handler for %SGX_IOC_ENCLAVE_MODIFY_TYPES
  859. * @encl: an enclave pointer
  860. * @arg: userspace pointer to a &struct sgx_enclave_modify_types instance
  861. *
  862. * Ability to change the enclave page type supports the following use cases:
  863. *
  864. * * It is possible to add TCS pages to an enclave by changing the type of
  865. * regular pages (%SGX_PAGE_TYPE_REG) to TCS (%SGX_PAGE_TYPE_TCS) pages.
  866. * With this support the number of threads supported by an initialized
  867. * enclave can be increased dynamically.
  868. *
  869. * * Regular or TCS pages can dynamically be removed from an initialized
  870. * enclave by changing the page type to %SGX_PAGE_TYPE_TRIM. Changing the
  871. * page type to %SGX_PAGE_TYPE_TRIM marks the page for removal with actual
  872. * removal done by handler of %SGX_IOC_ENCLAVE_REMOVE_PAGES ioctl() called
  873. * after ENCLU[EACCEPT] is run on %SGX_PAGE_TYPE_TRIM page from within the
  874. * enclave.
  875. *
  876. * Return:
  877. * - 0: Success
  878. * - -errno: Otherwise
  879. */
  880. static long sgx_ioc_enclave_modify_types(struct sgx_encl *encl,
  881. void __user *arg)
  882. {
  883. struct sgx_enclave_modify_types params;
  884. long ret;
  885. ret = sgx_ioc_sgx2_ready(encl);
  886. if (ret)
  887. return ret;
  888. if (copy_from_user(&params, arg, sizeof(params)))
  889. return -EFAULT;
  890. if (sgx_validate_offset_length(encl, params.offset, params.length))
  891. return -EINVAL;
  892. if (params.page_type & ~SGX_PAGE_TYPE_MASK)
  893. return -EINVAL;
  894. if (params.result || params.count)
  895. return -EINVAL;
  896. ret = sgx_enclave_modify_types(encl, &params);
  897. if (copy_to_user(arg, &params, sizeof(params)))
  898. return -EFAULT;
  899. return ret;
  900. }
  901. /**
  902. * sgx_encl_remove_pages() - Remove trimmed pages from SGX enclave
  903. * @encl: Enclave to which the pages belong
  904. * @params: Checked parameters from user on which pages need to be removed
  905. *
  906. * Return:
  907. * - 0: Success.
  908. * - -errno: Otherwise.
  909. */
  910. static long sgx_encl_remove_pages(struct sgx_encl *encl,
  911. struct sgx_enclave_remove_pages *params)
  912. {
  913. struct sgx_encl_page *entry;
  914. struct sgx_secinfo secinfo;
  915. unsigned long addr;
  916. unsigned long c;
  917. void *epc_virt;
  918. int ret;
  919. memset(&secinfo, 0, sizeof(secinfo));
  920. secinfo.flags = SGX_SECINFO_R | SGX_SECINFO_W | SGX_SECINFO_X;
  921. for (c = 0 ; c < params->length; c += PAGE_SIZE) {
  922. addr = encl->base + params->offset + c;
  923. sgx_reclaim_direct();
  924. mutex_lock(&encl->lock);
  925. entry = sgx_encl_load_page(encl, addr);
  926. if (IS_ERR(entry)) {
  927. ret = PTR_ERR(entry) == -EBUSY ? -EAGAIN : -EFAULT;
  928. goto out_unlock;
  929. }
  930. if (entry->type != SGX_PAGE_TYPE_TRIM) {
  931. ret = -EPERM;
  932. goto out_unlock;
  933. }
  934. /*
  935. * ENCLS[EMODPR] is a no-op instruction used to inform if
  936. * ENCLU[EACCEPT] was run from within the enclave. If
  937. * ENCLS[EMODPR] is run with RWX on a trimmed page that is
  938. * not yet accepted then it will return
  939. * %SGX_PAGE_NOT_MODIFIABLE, after the trimmed page is
  940. * accepted the instruction will encounter a page fault.
  941. */
  942. epc_virt = sgx_get_epc_virt_addr(entry->epc_page);
  943. ret = __emodpr(&secinfo, epc_virt);
  944. if (!encls_faulted(ret) || ENCLS_TRAPNR(ret) != X86_TRAP_PF) {
  945. ret = -EPERM;
  946. goto out_unlock;
  947. }
  948. if (sgx_unmark_page_reclaimable(entry->epc_page)) {
  949. ret = -EBUSY;
  950. goto out_unlock;
  951. }
  952. /*
  953. * Do not keep encl->lock because of dependency on
  954. * mmap_lock acquired in sgx_zap_enclave_ptes().
  955. */
  956. mutex_unlock(&encl->lock);
  957. sgx_zap_enclave_ptes(encl, addr);
  958. mutex_lock(&encl->lock);
  959. sgx_encl_free_epc_page(entry->epc_page);
  960. encl->secs_child_cnt--;
  961. entry->epc_page = NULL;
  962. xa_erase(&encl->page_array, PFN_DOWN(entry->desc));
  963. sgx_encl_shrink(encl, NULL);
  964. kfree(entry);
  965. mutex_unlock(&encl->lock);
  966. }
  967. ret = 0;
  968. goto out;
  969. out_unlock:
  970. mutex_unlock(&encl->lock);
  971. out:
  972. params->count = c;
  973. return ret;
  974. }
  975. /**
  976. * sgx_ioc_enclave_remove_pages() - handler for %SGX_IOC_ENCLAVE_REMOVE_PAGES
  977. * @encl: an enclave pointer
  978. * @arg: userspace pointer to &struct sgx_enclave_remove_pages instance
  979. *
  980. * Final step of the flow removing pages from an initialized enclave. The
  981. * complete flow is:
  982. *
  983. * 1) User changes the type of the pages to be removed to %SGX_PAGE_TYPE_TRIM
  984. * using the %SGX_IOC_ENCLAVE_MODIFY_TYPES ioctl().
  985. * 2) User approves the page removal by running ENCLU[EACCEPT] from within
  986. * the enclave.
  987. * 3) User initiates actual page removal using the
  988. * %SGX_IOC_ENCLAVE_REMOVE_PAGES ioctl() that is handled here.
  989. *
  990. * First remove any page table entries pointing to the page and then proceed
  991. * with the actual removal of the enclave page and data in support of it.
  992. *
  993. * VA pages are not affected by this removal. It is thus possible that the
  994. * enclave may end up with more VA pages than needed to support all its
  995. * pages.
  996. *
  997. * Return:
  998. * - 0: Success
  999. * - -errno: Otherwise
  1000. */
  1001. static long sgx_ioc_enclave_remove_pages(struct sgx_encl *encl,
  1002. void __user *arg)
  1003. {
  1004. struct sgx_enclave_remove_pages params;
  1005. long ret;
  1006. ret = sgx_ioc_sgx2_ready(encl);
  1007. if (ret)
  1008. return ret;
  1009. if (copy_from_user(&params, arg, sizeof(params)))
  1010. return -EFAULT;
  1011. if (sgx_validate_offset_length(encl, params.offset, params.length))
  1012. return -EINVAL;
  1013. if (params.count)
  1014. return -EINVAL;
  1015. ret = sgx_encl_remove_pages(encl, &params);
  1016. if (copy_to_user(arg, &params, sizeof(params)))
  1017. return -EFAULT;
  1018. return ret;
  1019. }
  1020. long sgx_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  1021. {
  1022. struct sgx_encl *encl = filep->private_data;
  1023. int ret;
  1024. if (test_and_set_bit(SGX_ENCL_IOCTL, &encl->flags))
  1025. return -EBUSY;
  1026. switch (cmd) {
  1027. case SGX_IOC_ENCLAVE_CREATE:
  1028. ret = sgx_ioc_enclave_create(encl, (void __user *)arg);
  1029. break;
  1030. case SGX_IOC_ENCLAVE_ADD_PAGES:
  1031. ret = sgx_ioc_enclave_add_pages(encl, (void __user *)arg);
  1032. break;
  1033. case SGX_IOC_ENCLAVE_INIT:
  1034. ret = sgx_ioc_enclave_init(encl, (void __user *)arg);
  1035. break;
  1036. case SGX_IOC_ENCLAVE_PROVISION:
  1037. ret = sgx_ioc_enclave_provision(encl, (void __user *)arg);
  1038. break;
  1039. case SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS:
  1040. ret = sgx_ioc_enclave_restrict_permissions(encl,
  1041. (void __user *)arg);
  1042. break;
  1043. case SGX_IOC_ENCLAVE_MODIFY_TYPES:
  1044. ret = sgx_ioc_enclave_modify_types(encl, (void __user *)arg);
  1045. break;
  1046. case SGX_IOC_ENCLAVE_REMOVE_PAGES:
  1047. ret = sgx_ioc_enclave_remove_pages(encl, (void __user *)arg);
  1048. break;
  1049. default:
  1050. ret = -ENOIOCTLCMD;
  1051. break;
  1052. }
  1053. clear_bit(SGX_ENCL_IOCTL, &encl->flags);
  1054. return ret;
  1055. }