Skip to contentSkip to conversation

x86/sgx: Fix lock ordering bug w/ EADD

PATCH Superseded Sean Christopherson 2 participants 11 messages Aug 27, 2019

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover x86/sgx: Fix lock ordering bug w/ EADD

As discovered by Jarkko, taking mm->mmap_sem around EADD can lead to deadlock as attempting to acquire mmap_sem while holding encl->lock violates SGX's lock ordering.

Resolving the issue simply by reversing the lock ordering gets ugly due to the behavior of sgx_encl_grow(), which has a path that drops encl->lock in order to do EPC page reclaim, i.e. moving mm->mmap_sem up would require it to be dropped and reacquired as well.

Luckily, the entire flow can be simplified by preventing userspace from invoking concurrent ioctls on a single enclave. Requiring ioctls to be serialized means encl->lock doesn't need to be held to grow/shrink the enclave, since only ioctls can grow/shrink the enclave. This also eliminates theoretical cases that sgx_encl_grow() has to handle, e.g. the enclave being initialized while it's waiting on reclaim, since the protection provided by serializing ioctls isn't dropped to do reclaim.

Alternatively, the issue could be resolved by sitching to get_user_pages() to snapshot the physical page when performance the noexec check (and maybe in the future, LSM checks). Since there is no significant (dis)advantage to using gup(), and simplifying sgx_encl_grow() is a worthwhile change on its own, I'd prefer get this issue resolved and have a separate discussion on gup() vs passing the userspace address to ENCLS as is.

Patches 1 and 2 are bug fixes that are affected somewhat by serializing the ioctls. I included them here to have the full context of what the final code/semantics.

Sean Christopherson (4):
x86/sgx: Ensure enclave state is visible before marking it created x86/sgx: Preserved allowed attributes during SGX_IOC_ENCLAVE_CREATE x86/sgx: Reject concurrent ioctls on single enclave x86/sgx: Take encl->lock inside of mm->mmap_sem for EADD

 arch/x86/kernel/cpu/sgx/driver.c |   1 +
 arch/x86/kernel/cpu/sgx/encl.h   |   1 +
 arch/x86/kernel/cpu/sgx/ioctl.c  | 162 +++++++++++++++++--------------
 3 files changed, 89 insertions(+), 75 deletions(-)
Signature · 1 line
2.22.0
#1 lore raw

PATCH 1/4 x86/sgx: Ensure enclave state is visible before marking it created

Add a memory barrier pair to ensure all enclave state is visible in memory prior to SGX_ENCL_CREATED being set. Without the barries, adding pages and/or initializing the enclaves could theoretically consume stale data.

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/sgx/ioctl.c +13 -3
index 911ff3b0f061..7134d68aecb3 100644
@@ -163,6 +163,15 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
 	return encl_page;
 }
 
+static bool is_encl_created(struct sgx_encl *encl)
+{
+	bool created = encl->flags & SGX_ENCL_CREATED;
+
+	/* Pairs with smp_wmb() in sgx_encl_create(). */
+	smp_rmb();
+	return created;
+}
+
 static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
 {
 	unsigned long encl_size = secs->size + PAGE_SIZE;
@@ -231,8 +240,9 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
 	/*
 	 * Set SGX_ENCL_CREATED only after the enclave is fully prepped.  This
 	 * allows other flows to check if the enclave has been created without
-	 * taking encl->lock.
+	 * taking encl->lock.  Pairs with smp_rmb() in is_encl_created().
 	 */
+	smp_wmb();
 	encl->flags |= SGX_ENCL_CREATED;
 
 	mutex_unlock(&encl->lock);
@@ -478,7 +488,7 @@ static long sgx_ioc_enclave_add_page(struct file *filep, void __user *arg)
 	struct sgx_enclave_add_page addp;
 	struct sgx_secinfo secinfo;
 
-	if (!(encl->flags & SGX_ENCL_CREATED))
+	if (!is_encl_created(encl))
 		return -EINVAL;
 
 	if (copy_from_user(&addp, arg, sizeof(addp)))
@@ -611,7 +621,7 @@ static long sgx_ioc_enclave_init(struct file *filep, void __user *arg)
 	struct page *initp_page;
 	int ret;
 
-	if (!(encl->flags & SGX_ENCL_CREATED))
+	if (!is_encl_created(encl))
 		return -EINVAL;
 
 	if (copy_from_user(&einit, arg, sizeof(einit)))
Signature · 1 line
2.22.0
#2 lore raw

Re: [PATCH 1/4] x86/sgx: Ensure enclave state is visible before marking it created

Quoted 16 lines · Sean Christopherson

Add a memory barrier pair to ensure all enclave state is visible in memory prior to SGX_ENCL_CREATED being set. Without the barries, adding pages and/or initializing the enclaves could theoretically consume stale data.

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/sgx/ioctl.c +0 -0
index 911ff3b0f061..7134d68aecb3 100644
@@ -163,6 +163,15 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
 	return encl_page;

arch/x86/kernel/cpu/sgx/ioctl.c static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl, · line 173

Quoted 10 lines
arch/x86/kernel/cpu/sgx/ioctl.c +8 -0
 }
 
+static bool is_encl_created(struct sgx_encl *encl)
+{
+	bool created = encl->flags & SGX_ENCL_CREATED;
+
+	/* Pairs with smp_wmb() in sgx_encl_create(). */
+	smp_rmb();
+	return created;
+}

what if you just convert the flags to atomic_t? That would fix this issue and would prevent analogous issues from occuring.

/Jarkko

#3 lore raw

Re: [PATCH 1/4] x86/sgx: Ensure enclave state is visible before marking it created

Quoted 19 lines · Jarkko Sakkinen
Quoted 18 lines · Sean Christopherson

Add a memory barrier pair to ensure all enclave state is visible in memory prior to SGX_ENCL_CREATED being set. Without the barries, adding pages and/or initializing the enclaves could theoretically consume stale data.

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/sgx/ioctl.c +0 -0
index 911ff3b0f061..7134d68aecb3 100644
@@ -163,6 +163,15 @@ static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl,
 	return encl_page;
 }
 
Quoted 11 lines

arch/x86/kernel/cpu/sgx/ioctl.c static struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl, · line 173

Quoted 8 lines
arch/x86/kernel/cpu/sgx/ioctl.c +8 -0
+static bool is_encl_created(struct sgx_encl *encl)
+{
+	bool created = encl->flags & SGX_ENCL_CREATED;
+
+	/* Pairs with smp_wmb() in sgx_encl_create(). */
+	smp_rmb();
+	return created;
+}

what if you just convert the flags to atomic_t? That would fix this issue and would prevent analogous issues from occuring.

I thought about that too, but originally discarded the idea because I was worried doing so would negatively impact the other uses of flags. After actually implementing the change, I think the positives outweigh the negatives, so I'll send a v2 with this suggestion.

#4 lore raw

PATCH 2/4 x86/sgx: Preserved allowed attributes during SGX_IOC_ENCLAVE_CREATE

In reply to Sean Christopherson

Preserve any existing attributes set via ENCLAVE_SET_ATTRIBUTE when setting the always allowed attributes during ENCLAVE_CREATE. There is no requirement that ENCLAVE_SET_ATTRIBUTE can only be called after the enclave is created.

Note, this does not fix a race condition between ENCLAVE_CREATE and ENCLAVE_SET_ATTRIBUTE, as the latter doesn't take encl->lock. This will be addressed in a future patch.

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/ioctl.c +1 -1
index 7134d68aecb3..103851babc75 100644
@@ -232,7 +232,7 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs)
 
 	encl->secs.encl = encl;
 	encl->secs_attributes = secs->attributes;
-	encl->allowed_attributes = SGX_ATTR_ALLOWED_MASK;
+	encl->allowed_attributes |= SGX_ATTR_ALLOWED_MASK;
 	encl->base = secs->base;
 	encl->size = secs->size;
 	encl->ssaframesize = secs->ssa_frame_size;
Signature · 1 line
2.22.0
Acked-by on v1 2/4
#5 lore raw

Re: [PATCH 2/4] x86/sgx: Preserved allowed attributes during SGX_IOC_ENCLAVE_CREATE

Quoted 10 lines · Sean Christopherson

Preserve any existing attributes set via ENCLAVE_SET_ATTRIBUTE when setting the always allowed attributes during ENCLAVE_CREATE. There is no requirement that ENCLAVE_SET_ATTRIBUTE can only be called after the enclave is created.

Note, this does not fix a race condition between ENCLAVE_CREATE and ENCLAVE_SET_ATTRIBUTE, as the latter doesn't take encl->lock. This will be addressed in a future patch.

Thanks!

/Jarkko

#6 lore raw

Re: [PATCH 2/4] x86/sgx: Preserved allowed attributes during SGX_IOC_ENCLAVE_CREATE

Quoted 6 lines · Jarkko Sakkinen
Quoted 5 lines · Sean Christopherson

Preserve any existing attributes set via ENCLAVE_SET_ATTRIBUTE when setting the always allowed attributes during ENCLAVE_CREATE. There is no requirement that ENCLAVE_SET_ATTRIBUTE can only be called after the enclave is created.

Quoted 7 lines
Quoted 5 lines

Note, this does not fix a race condition between ENCLAVE_CREATE and ENCLAVE_SET_ATTRIBUTE, as the latter doesn't take encl->lock. This will be addressed in a future patch.

This now merged to my tree.

/Jarkko

#7 lore raw

PATCH 3/4 x86/sgx: Reject concurrent ioctls on single enclave

In reply to Sean Christopherson

Except for ENCLAVE_SET_ATTRIBUTE, all SGX ioctls() must be executed serially to successfully initialize an enclave, e.g. the kernel already strictly requires ECREATE->EADD->EINIT, and concurrent EADDs will result in an unstable MRENCLAVE. Explicitly enforce serialization by returning EINVAL if userspace attempts an ioctl while a different ioctl for the same enclave is in progress.

The primary beneficiary of explicit serialization is sgx_encl_grow() as it no longer has to deal with dropping and reacquiring encl->lock when a new VA page needs to be allocated. Eliminating the lock juggling in sgx_encl_grow() paves the way for fixing a lock ordering bug in ENCLAVE_ADD_PAGE without having to also juggle mm->mmap_sem.

Serializing ioctls also fixes a race between ENCLAVE_CREATE and ENCLAVE_SET_ATTRIBUTE, as the latter does not take encl->lock, e.g. concurrent updates to allowed_attributes could result in a stale value. The race could also be fixed by taking encl->lock, but that is less desirable as doing so would unnecessarily interfere with EPC page reclaim.

---
 arch/x86/kernel/cpu/sgx/driver.c |  1 +
 arch/x86/kernel/cpu/sgx/encl.h   |  1 +
 arch/x86/kernel/cpu/sgx/ioctl.c  | 91 ++++++++++++++------------------
 3 files changed, 43 insertions(+), 50 deletions(-)
arch/x86/kernel/cpu/sgx/driver.c +1 -0
index e740d71e2311..7ebd66050400 100644
@@ -32,6 +32,7 @@ static int sgx_open(struct inode *inode, struct file *file)
 		return -ENOMEM;
 
 	kref_init(&encl->refcount);
+	atomic_set(&encl->in_ioctl, 0);
 	INIT_LIST_HEAD(&encl->va_pages);
 	INIT_RADIX_TREE(&encl->page_tree, GFP_KERNEL);
 	mutex_init(&encl->lock);
arch/x86/kernel/cpu/sgx/encl.h +1 -0
index 37b5c4bcda7a..1505ff204703 100644
@@ -67,6 +67,7 @@ struct sgx_encl_mm {
 };
 
 struct sgx_encl {
+	atomic_t in_ioctl;
 	unsigned int flags;
 	u64 secs_attributes;
 	u64 allowed_attributes;
arch/x86/kernel/cpu/sgx/ioctl.c +41 -50

198 lines not on this page · raw message

Signature · 1 line
2.22.0
#8 lore raw

Re: [PATCH 3/4] x86/sgx: Reject concurrent ioctls on single enclave

Quoted 13 lines · Sean Christopherson

Except for ENCLAVE_SET_ATTRIBUTE, all SGX ioctls() must be executed serially to successfully initialize an enclave, e.g. the kernel already strictly requires ECREATE->EADD->EINIT, and concurrent EADDs will result in an unstable MRENCLAVE. Explicitly enforce serialization by returning EINVAL if userspace attempts an ioctl while a different ioctl for the same enclave is in progress.

The primary beneficiary of explicit serialization is sgx_encl_grow() as it no longer has to deal with dropping and reacquiring encl->lock when a new VA page needs to be allocated. Eliminating the lock juggling in sgx_encl_grow() paves the way for fixing a lock ordering bug in ENCLAVE_ADD_PAGE without having to also juggle mm->mmap_sem.

Quoted 8 lines

Serializing ioctls also fixes a race between ENCLAVE_CREATE and ENCLAVE_SET_ATTRIBUTE, as the latter does not take encl->lock, e.g. concurrent updates to allowed_attributes could result in a stale value. The race could also be fixed by taking encl->lock, but that is less desirable as doing so would unnecessarily interfere with EPC b page reclaim.

I think the error code should be -EBUSY. Our implementation is fairly coherent at the moment that -EINVAL follows from bad input data. Getting -EINVAL from this would be a bit confusing.

If atomic_t was used for the enclave flags (see my comment to 1/4), then I think we could implement this like:

if (atomic_fetch_or(SGX_ENCL_IOCTL, &encl->flags) & SGX_ENCL_IOCTL)

	return -EIOCTL;

/Jarkko

#9 lore raw

PATCH 4/4 x86/sgx: Take encl->lock inside of mm->mmap_sem for EADD

In reply to Sean Christopherson

Reverse the order in which encl->lock and mm->mmap_sem are taken during ENCLAVE_ADD_PAGE so as to adhere to SGX's lock ordering requirements. Attempting to acquire mm->mmap_sem while holding encl->lock can result in deadlock.

Refactor EEXTEND and the final bookkeeping out of __sgx_encl_add_page() so that mm->mmap_sem can be dropped after EADD without spreading the lock/unlock across multiple functions.

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 55 ++++++++++++++++++++-------------
 1 file changed, 33 insertions(+), 22 deletions(-)
arch/x86/kernel/cpu/sgx/ioctl.c +33 -22
index 170ed538b02b..4a9ae1090433 100644
@@ -317,43 +317,40 @@ static int sgx_validate_secinfo(struct sgx_secinfo *secinfo)
 static int __sgx_encl_add_page(struct sgx_encl *encl,
 			       struct sgx_encl_page *encl_page,
 			       struct sgx_epc_page *epc_page,
-			       struct sgx_secinfo *secinfo, unsigned long src,
-			       unsigned long mrmask)
+			       struct sgx_secinfo *secinfo, unsigned long src)
 {
 	struct sgx_pageinfo pginfo;
 	struct vm_area_struct *vma;
 	int ret;
-	int i;
 
 	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 = src;
 
-	down_read(&current->mm->mmap_sem);
-
 	/* Query vma's VM_MAYEXEC as an indirect path_noexec() check. */
 	if (encl_page->vm_max_prot_bits & VM_EXEC) {
 		vma = find_vma(current->mm, src);
-		if (!vma) {
-			up_read(&current->mm->mmap_sem);
+		if (!vma)
 			return -EFAULT;
-		}
 
-		if (!(vma->vm_flags & VM_MAYEXEC)) {
-			up_read(&current->mm->mmap_sem);
+		if (!(vma->vm_flags & VM_MAYEXEC))
 			return -EACCES;
-		}
 	}
 
 	__uaccess_begin();
 	ret = __eadd(&pginfo, sgx_epc_addr(epc_page));
 	__uaccess_end();
 
-	up_read(&current->mm->mmap_sem);
+	return ret ? -EFAULT : 0;
+}
 
-	if (ret)
-		return -EFAULT;
+static int __sgx_encl_extend(struct sgx_encl *encl,
+			     struct sgx_epc_page *epc_page,
+			     unsigned long mrmask)
+{
+	int ret;
+	int i;
 
 	for_each_set_bit(i, &mrmask, 16) {
 		ret = __eextend(sgx_epc_addr(encl->secs.epc_page),
@@ -364,12 +361,6 @@ static int __sgx_encl_add_page(struct sgx_encl *encl,
 			return -EFAULT;
 		}
 	}
-
-	encl_page->encl = encl;
-	encl_page->epc_page = epc_page;
-	encl->secs_child_cnt++;
-	sgx_mark_page_reclaimable(encl_page->epc_page);
-
 	return 0;
 }
 
@@ -398,19 +389,39 @@ static int sgx_encl_add_page(struct sgx_encl *encl,
 		goto err_out_free;
 	}
 
+	down_read(&current->mm->mmap_sem);
+
 	mutex_lock(&encl->lock);
 
+	/*
+	 * Insert prior to EADD in case of OOM.  EADD modifies MRENCLAVE, i.e.
+	 * can't be gracefully unwound, while failure on EADD/EXTEND is limited
+	 * to userspace errors (or kernel/hardware bugs).
+	 */
 	ret = radix_tree_insert(&encl->page_tree, PFN_DOWN(encl_page->desc),
 				encl_page);
-	if (ret)
+	if (ret) {
+		up_read(&current->mm->mmap_sem);
 		goto err_out_shrink;
+	}
 
 	ret = __sgx_encl_add_page(encl, encl_page, epc_page, secinfo,
-				  addp->src, addp->mrmask);
+				  addp->src);
+	up_read(&current->mm->mmap_sem);
+
+	if (ret)
+		goto err_out;
+
+	ret = __sgx_encl_extend(encl, epc_page, addp->mrmask);
 	if (ret)
 		goto err_out;
 
+	encl_page->encl = encl;
+	encl_page->epc_page = epc_page;
+	encl->secs_child_cnt++;
+	sgx_mark_page_reclaimable(encl_page->epc_page);
 	mutex_unlock(&encl->lock);
+
 	return 0;
 
 err_out:
Signature · 1 line
2.22.0
Acked-by on v1 4/4
#10 lore raw

Re: [PATCH 4/4] x86/sgx: Take encl->lock inside of mm->mmap_sem for EADD

Quoted 11 lines · Sean Christopherson

Reverse the order in which encl->lock and mm->mmap_sem are taken during ENCLAVE_ADD_PAGE so as to adhere to SGX's lock ordering requirements. Attempting to acquire mm->mmap_sem while holding encl->lock can result in deadlock.

Refactor EEXTEND and the final bookkeeping out of __sgx_encl_add_page() so that mm->mmap_sem can be dropped after EADD without spreading the lock/unlock across multiple functions.

Thanks, looks good.

/Jarkko