Skip to contentSkip to conversation

x86/sgx: Fix double-free when EADD fails

PATCH In review Jarkko Sakkinen 2 participants 5 messages Dec 5, 2019 – Dec 12, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/sgx: Fix double-free when EADD fails

radix_tree_delete() gets called twice for the same page when EADD fails. This commit fixes the issue.

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/sgx/ioctl.c +10 -13
index ab9e48cd294b..2ff12038a8a4 100644
@@ -413,13 +413,8 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 
 	ret = __sgx_encl_add_page(encl, encl_page, epc_page, secinfo,
 				  src);
-	if (ret) {
-		/* ENCLS failure. */
-		if (ret == -EIO)
-			sgx_encl_destroy(encl);
-
+	if (ret)
 		goto err_out;
-	}
 
 	/*
 	 * Complete the "add" before doing the "extend" so that the "add"
@@ -432,17 +427,12 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 
 	if (flags & SGX_PAGE_MEASURE) {
 		ret = __sgx_encl_extend(encl, epc_page);
-
-		/* ENCLS failure. */
-		if (ret) {
-			sgx_encl_destroy(encl);
-			goto out_unlock;
-		}
+		if (ret)
+			goto err_out;
 	}
 
 	sgx_mark_page_reclaimable(encl_page->epc_page);
 
-out_unlock:
 	mutex_unlock(&encl->lock);
 	up_read(&current->mm->mmap_sem);
 	return ret;
@@ -460,6 +450,13 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 	sgx_free_page(epc_page);
 	kfree(encl_page);
 
+	/*
+	 * Destroy enclave on ENCLS failure as this means that EPC has been
+	 * invalidated.
+	 */
+	if (ret == -EIO)
+		sgx_encl_destroy(encl);
+
 	return ret;
 }
 
Signature · 1 line
2.20.1
#1 lore raw
Quoted 50 lines · Jarkko Sakkinen

radix_tree_delete() gets called twice for the same page when EADD fails. This commit fixes the issue.

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/sgx/ioctl.c +3 -13
index ab9e48cd294b..2ff12038a8a4 100644
@@ -413,13 +413,8 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 
 	ret = __sgx_encl_add_page(encl, encl_page, epc_page, secinfo,
 				  src);
-	if (ret) {
-		/* ENCLS failure. */
-		if (ret == -EIO)
-			sgx_encl_destroy(encl);
-
+	if (ret)
 		goto err_out;
-	}
 
 	/*
 	 * Complete the "add" before doing the "extend" so that the "add"
@@ -432,17 +427,12 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 
 	if (flags & SGX_PAGE_MEASURE) {
 		ret = __sgx_encl_extend(encl, epc_page);
-
-		/* ENCLS failure. */
-		if (ret) {
-			sgx_encl_destroy(encl);
-			goto out_unlock;
-		}
+		if (ret)
+			goto err_out;
 	}
 
 	sgx_mark_page_reclaimable(encl_page->epc_page);
 
-out_unlock:
 	mutex_unlock(&encl->lock);
 	up_read(&current->mm->mmap_sem);
 	return ret;

arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src, · line 455

Quoted 6 lines
arch/x86/kernel/cpu/sgx/ioctl.c +3 -0
@@ -460,6 +450,13 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 	sgx_free_page(epc_page);
 	kfree(encl_page);
 
+	/*
+	 * Destroy enclave on ENCLS failure as this means that EPC has been
+	 * invalidated.

This comment is wrong, EADD can fail due to bad userspace input, and both EADD and EEXTEND can fail due to hardware/software bugs.

arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src, · line 457

Quoted 2 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2 -0
+	 */
+	if (ret == -EIO)

Not a fan of making this dependent on -EIO, IMO invalidating iff EEXTEND fails is cleaner. In other words, I still think killing the enclave on on EADD failure is unnecessary.

Side topic, __sgx_encl_add_page() doesn't correctly get_user_pages() returning zero, e.g. the code should be something like:

	ret = get_user_pages(src, 1, 0, &src_page, NULL);
	if (!ret)
		return -EBUSY:
	if (ret < 0)
		return ret;
Quoted 8 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2 -0
+		sgx_encl_destroy(encl);
+
 	return ret;
 }
 
Signature · 1 line
2.20.1
#2 lore raw
Quoted 54 lines · Sean Christopherson
Quoted 53 lines · Jarkko Sakkinen

radix_tree_delete() gets called twice for the same page when EADD fails. This commit fixes the issue.

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/sgx/ioctl.c +3 -13
index ab9e48cd294b..2ff12038a8a4 100644
@@ -413,13 +413,8 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 
 	ret = __sgx_encl_add_page(encl, encl_page, epc_page, secinfo,
 				  src);
-	if (ret) {
-		/* ENCLS failure. */
-		if (ret == -EIO)
-			sgx_encl_destroy(encl);
-
+	if (ret)
 		goto err_out;
-	}
 
 	/*
 	 * Complete the "add" before doing the "extend" so that the "add"
@@ -432,17 +427,12 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 
 	if (flags & SGX_PAGE_MEASURE) {
 		ret = __sgx_encl_extend(encl, epc_page);
-
-		/* ENCLS failure. */
-		if (ret) {
-			sgx_encl_destroy(encl);
-			goto out_unlock;
-		}
+		if (ret)
+			goto err_out;
 	}
 
 	sgx_mark_page_reclaimable(encl_page->epc_page);
 
-out_unlock:
 	mutex_unlock(&encl->lock);
 	up_read(&current->mm->mmap_sem);
 	return ret;
@@ -460,6 +450,13 @@ static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src,
 	sgx_free_page(epc_page);
 	kfree(encl_page);
 
Quoted 6 lines

arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src, · line 455

Quoted 3 lines
arch/x86/kernel/cpu/sgx/ioctl.c +3 -0
+	/*
+	 * Destroy enclave on ENCLS failure as this means that EPC has been
+	 * invalidated.

This comment is wrong, EADD can fail due to bad userspace input, and both EADD and EEXTEND can fail due to hardware/software bugs.

Any piece of kernel code can fail because of either hardware or software bug. Still almost none of the comments explicitly state this.

However, userspace input is a valid remark.

Quoted 7 lines

arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_add_page(struct sgx_encl *encl, unsigned long src, · line 457

Quoted 3 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2 -0
+	 */
+	if (ret == -EIO)

Not a fan of making this dependent on -EIO, IMO invalidating iff EEXTEND fails is cleaner. In other words, I still think killing the enclave on on EADD failure is unnecessary.

This comes down to whether you consider them as a transaction. I do and it makes a coherent API.

If user code wants to give on purpose bad input for EADD, I think it is sane to kill the enclave also in that case. There is no legit use case where user code would do that.

Potentially that could be malicious behaviour (maybe nothing useful can be done with that primitive but that is only thing that it could be theoretically used for since legit use cases do not exist).

Quoted 8 lines

Side topic, __sgx_encl_add_page() doesn't correctly get_user_pages() returning zero, e.g. the code should be something like:

	ret = get_user_pages(src, 1, 0, &src_page, NULL);
	if (!ret)
		return -EBUSY:
	if (ret < 0)
		return ret;

Thanks! I'll update the GIT based on your feedback (it's probably better that at this point for small scoped changes you just describe the change and I'll update. With all squashing and rebasing that is fastest).

/Jarkko

#3 lore raw
Quoted 7 lines · Jarkko Sakkinen
Quoted 3 lines · Sean Christopherson

Not a fan of making this dependent on -EIO, IMO invalidating iff EEXTEND fails is cleaner. In other words, I still think killing the enclave on on EADD failure is unnecessary.

This comes down to whether you consider them as a transaction. I do and it makes a coherent API.

What's your definition of transaction in this context? My interpretation of transaction here would be that each ioctl() should either succeed, fail without modifying persistent (enclave) state, or fail and kill the enclave (because its state modifications are irreversible).

EEXTEND falls into the last case because EADD can't be unwound. EADD falls into the middle case because everything up to EADD can be cleanly undone.

#4 lore raw
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Jarkko Sakkinen
Quoted 3 lines · Sean Christopherson

Not a fan of making this dependent on -EIO, IMO invalidating iff EEXTEND fails is cleaner. In other words, I still think killing the enclave on on EADD failure is unnecessary.

This comes down to whether you consider them as a transaction. I do and it makes a coherent API.

Quoted 7 lines

What's your definition of transaction in this context? My interpretation of transaction here would be that each ioctl() should either succeed, fail without modifying persistent (enclave) state, or fail and kill the enclave (because its state modifications are irreversible).

EEXTEND falls into the last case because EADD can't be unwound. EADD falls into the middle case because everything up to EADD can be cleanly undone.

My definition is that if any of EADD/EEXTEND fails the enclave should be killed as there is no good reason to support that kind of use in any possible way. As long as it is documented, it is fine.

/Jarkko