On Thu, Oct 10, 2019 at 04:37:53PM +0300, Jarkko Sakkinen wrote: > On Thu, Oct 10, 2019 at 02:37:45PM +0300, Jarkko Sakkinen wrote: > > tag v23-rc2 > > Tagger: Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxxxxxxxx> > > Date: Thu Oct 10 14:33:07 2019 +0300 > > > > x86/sgx: v23-rc1 patch set > > > > * Return -EIO instead of -ECANCELED when ptrace() fails to read a TCS page. > > * In the reclaimer, pin page before ENCLS[EBLOCK] because pinning can fail > > (because of OOM) even in legit behaviour and after EBLOCK the reclaiming > > flow can be only reverted by killing the whole enclave. > > * Fixed SGX_ATTR_RESERVED_MASK. Bit 7 was marked as reserved while in fact > > it should have been bit 6 (Table 37-3 in the SDM). > > * Return -EPERM from SGX_IOC_ENCLAVE_INIT when ENCLS[EINIT] returns an SGX > > error code. > > * In v22 __uaccess_begin() was used to pin the source page in > > __sgx_encl_add_page(). Switch to get_user_pages() in order to avoid > > deadlock (mmap_sem might get locked twice in the same thread). > > __uaccess_begin() is also needed to performan access checks the legit __uaccess_begin() doesn't check the address space, it temporarily disables SMAP/SMEP so that the kernel can access a user mapping. An explicit access_ok() call should be added as well. > user space address. What we can do is to use get_user_pages() just to > make sure that the page is faulted while we perform ENCLS[EADD]. > I updated the master branch with the fix for this. Now the access > pattern is: > > ret = get_user_pages(src, 1, 0, &src_page, NULL); > if (ret < 1) > return ret; > > __uaccess_begin(); This should be immediately before __eadd(). I also think it'd be a good idea to disable page faults around __eadd() so that an unexpected #PF manifests as an __eadd() failure and not a kernel hang. > pginfo.secs = (unsigned long)sgx_epc_addr(encl->secs.epc_page); > pginfo.addr = SGX_ENCL_PAGE_ADDR(encl_page); > pginfo.metadata = (unsigned long)secinfo; > pginfo.contents = (unsigned long)src; > ret = __eadd(&pginfo, sgx_epc_addr(epc_page)); > > __uaccess_end(); > put_page(src_page); Not shown here, but mmap_sem doesn't need to be held through EEXTEND. The lock issue is that down_read() will block if there is a pending down_write(), e.g. if userspace is doing mprotect() at the same time as EADD, then deadlock will occur if EADD faults. Holding encl->lock without mmap_sem is perfectly ok. I'll send a small series with the above changes.