Skip to contentSkip to conversation

x86/sgx: Fix use-after-free in sgx_mmu_notifier_release()

PATCH In review Jarkko Sakkinen 2 participants 6 messages Jan 28, 2021 – Feb 3, 2021

Subsystems (3)
lore
Share & tools
lore

Jarkko Sakkinen

author maintainer
#0 lore raw

PATCH v5 1/1 x86/sgx: Fix use-after-free in sgx_mmu_notifier_release()

The most trivial example of a race condition can be demonstrated by this sequence where mm_list contains just one entry:

CPU A CPU B
-> sgx_release()

                                -> sgx_mmu_notifier_release()
                                -> list_del_rcu()
                                <- list_del_rcu()
-> kref_put()
-> sgx_encl_release()
                                -> synchronize_srcu()

-> cleanup_srcu_struct()

A sequence similar to this has also been spotted in tests under high stress:

[ +0.000008] WARNING: CPU: 3 PID: 7620 at kernel/rcu/srcutree.c:374 cleanup_srcu_struct+0xed/0x100

Albeit not spotted in the tests, it's also entirely possible that the following scenario could happen:

CPU A CPU B
-> sgx_release()

                                -> sgx_mmu_notifier_release()
                                -> list_del_rcu()

-> kref_put()
-> sgx_encl_release()
-> cleanup_srcu_struct()
<- cleanup_srcu_struct()

                                -> synchronize_srcu()

This scenario would lead into use-after free in cleaup_srcu_struct().

Fix this by taking a reference to the enclave in sgx_mmu_notifier_release().

Fixes 1728ab54b4be ("x86/sgx: Add a page reclaimer")

---
v5:
- To make sure that the instance does not get deleted use kref_get() kref_put(). This also removes the need for additional synchronize_srcu().
v4:
- Rewrite the commit message.
- Just change the call order. *_expedited() is out of scope for this bug fix.
v3: Fine-tuned tags, and added missing change log for v2.
v2: Switch to synchronize_srcu_expedited().

 arch/x86/kernel/cpu/sgx/encl.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kernel/cpu/sgx/encl.c +2 -0
index ee50a5010277..5ecbcf94ec2a 100644
@@ -465,6 +465,7 @@ static void sgx_mmu_notifier_release(struct mmu_notifier *mn,
 	spin_lock(&encl_mm->encl->mm_lock);
 	list_for_each_entry(tmp, &encl_mm->encl->mm_list, list) {
 		if (tmp == encl_mm) {
+			kref_get(&encl_mm->encl->refcount);
 			list_del_rcu(&encl_mm->list);
 			break;
 		}
@@ -474,6 +475,7 @@ static void sgx_mmu_notifier_release(struct mmu_notifier *mn,
 	if (tmp == encl_mm) {
 		synchronize_srcu(&encl_mm->encl->srcu);
 		mmu_notifier_put(mn);
+		kref_put(&encl_mm->encl->refcount, sgx_encl_release);
 	}
 }
 
Signature · 1 line
2.30.0
#1 lore raw
Quoted 5 lines · Jarkko Sakkinen

The most trivial example of a race condition can be demonstrated by this sequence where mm_list contains just one entry:

CPU A CPU B
-> sgx_release()

Quoted 7 lines
                                -> sgx_mmu_notifier_release()
                                -> list_del_rcu()
                                <- list_del_rcu()
-> kref_put()
-> sgx_encl_release()
                                -> synchronize_srcu()

-> cleanup_srcu_struct()

This is missing some key details including a clear, unambiguous, problem statement. To me, the patch should concentrate on the SRCU warning since that's where we started. Here's the detail that needs to be added about the issue and the locking in general in this path:

sgx_release() also does this:

	mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm);

which does another synchronize_srcu() on the mmu_notifier's srcu_struct.
*But*, it only does this if its own list_del_rcu() is successful. It
does all of this before the kref_put().

In other words, sgx_release() can *only* get to this buggy path if sgx_mmu_notifier_release() races with sgx_release and does a list_del_rcu() first.

The key to this patch is that the sgx_mmu_notifier_release() will now take an 'encl' reference in that case, which prevents kref_put() from calling sgx_release() which cleans up and frees 'encl'.

I was actually also hoping to see some better comments about the new refcount, and the locking in general. There are *TWO* struct_srcu's in play, a spinlock and a refcount. I took me several days with Sean and your help to identify the actual path and get a proper fix (versions 1-4 did *not* fix the race).

Also, the use-after-free is *fixed* in sgx_mmu_notifier_release() but does not *occur* in sgx_mmu_notifier_release(). The subject here is a bit misleading in that regard.

author maintainer
#2 lore raw
Quoted 35 lines · Dave Hansen
Quoted 12 lines · Jarkko Sakkinen

The most trivial example of a race condition can be demonstrated by this sequence where mm_list contains just one entry:

CPU A                           CPU B
-> sgx_release()
                                -> sgx_mmu_notifier_release() -> list_del_rcu() <- list_del_rcu() -> kref_put()
-> sgx_encl_release()
                                -> synchronize_srcu() -> cleanup_srcu_struct()

This is missing some key details including a clear, unambiguous, problem statement.  To me, the patch should concentrate on the SRCU warning since that's where we started.  Here's the detail that needs to be added about the issue and the locking in general in this path:

sgx_release() also does this:

        mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm);

which does another synchronize_srcu() on the mmu_notifier's srcu_struct. *But*, it only does this if its own list_del_rcu() is successful.  It does all of this before the kref_put().

In other words, sgx_release() can *only* get to this buggy path if sgx_mmu_notifier_release() races with sgx_release and does a list_del_rcu() first.

The key to this patch is that the sgx_mmu_notifier_release() will now take an 'encl' reference in that case, which prevents kref_put() from calling sgx_release() which cleans up and frees 'encl'.

Quoted 5 lines

I was actually also hoping to see some better comments about the new refcount, and the locking in general.  There are *TWO* struct_srcu's in play, a spinlock and a refcount.  I took me several days with Sean and your help to identify the actual path and get a proper fix (versions 1-4 did *not* fix the race).

This was really good input, thank you. It made realize something but now I need a sanity check.

I think that this bug fix is *neither* a legit one :-)

Example scenario would such that all removals "side-channel" through the notifier callback. Then mmu_notifier_unregister() gets called exactly zero times. No MMU notifier srcu sync would be then happening.

NOTE: There's bunch of other examples, I'm just giving one.

How I think this should be actually fixed is:

1. Whenever MMU notifier is *registered* kref_get() should be called for the enclave reference count.
2. *BOTH* sgx_release() and sgx_mmu_notifier_release() should decrease the refcount when they process an entry.

I.e. the fix that I sent does kref_get() in wrong location. Please sanity check my conclusion.

Quoted 3 lines

Also, the use-after-free is *fixed* in sgx_mmu_notifier_release() but does not *occur* in sgx_mmu_notifier_release().  The subject here is a bit misleading in that regard.

Right, this is a valid point. It's incorrect. So if I just change the short summary by substituting sgx_mmu_notifier_release() with sgx_release()?

/Jarkko

author maintainer
#3 lore raw
Quoted 63 lines · Jarkko Sakkinen
Quoted 35 lines · Dave Hansen
Quoted 12 lines · Jarkko Sakkinen

The most trivial example of a race condition can be demonstrated by this sequence where mm_list contains just one entry:

CPU A                           CPU B
-> sgx_release()
                                -> sgx_mmu_notifier_release() -> list_del_rcu() <- list_del_rcu() -> kref_put()
-> sgx_encl_release()
                                -> synchronize_srcu() -> cleanup_srcu_struct()

This is missing some key details including a clear, unambiguous, problem statement.  To me, the patch should concentrate on the SRCU warning since that's where we started.  Here's the detail that needs to be added about the issue and the locking in general in this path:

sgx_release() also does this:

        mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm);

which does another synchronize_srcu() on the mmu_notifier's srcu_struct. *But*, it only does this if its own list_del_rcu() is successful.  It does all of this before the kref_put().

In other words, sgx_release() can *only* get to this buggy path if sgx_mmu_notifier_release() races with sgx_release and does a list_del_rcu() first.

The key to this patch is that the sgx_mmu_notifier_release() will now take an 'encl' reference in that case, which prevents kref_put() from calling sgx_release() which cleans up and frees 'encl'.

Quoted 5 lines

I was actually also hoping to see some better comments about the new refcount, and the locking in general.  There are *TWO* struct_srcu's in play, a spinlock and a refcount.  I took me several days with Sean and your help to identify the actual path and get a proper fix (versions 1-4 did *not* fix the race).

This was really good input, thank you. It made realize something but now I need a sanity check.

I think that this bug fix is *neither* a legit one :-)

Example scenario would such that all removals "side-channel" through the notifier callback. Then mmu_notifier_unregister() gets called exactly zero times. No MMU notifier srcu sync would be then happening.

NOTE: There's bunch of other examples, I'm just giving one.

How I think this should be actually fixed is:

1. Whenever MMU notifier is *registered* kref_get() should be called for the enclave reference count.
2. *BOTH* sgx_release() and sgx_mmu_notifier_release() should decrease the refcount when they process an entry.

I.e. the fix that I sent does kref_get() in wrong location. Please sanity check my conclusion.

Quoted 7 lines
Quoted 3 lines

Also, the use-after-free is *fixed* in sgx_mmu_notifier_release() but does not *occur* in sgx_mmu_notifier_release().  The subject here is a bit misleading in that regard.

Right, this is a valid point. It's incorrect. So if I just change the short summary by substituting sgx_mmu_notifier_release() with sgx_release()?

I.e. refcount should be increased in sgx_encl_mm_add(). That way the whole thing should be somewhat stable.

/Jarkko

#4 lore raw
In reply to Jarkko Sakkinen

On 1/30/21 11:20 AM, Jarkko Sakkinen wrote:
...

Quoted 5 lines

Example scenario would such that all removals "side-channel" through the notifier callback. Then mmu_notifier_unregister() gets called exactly zero times. No MMU notifier srcu sync would be then happening.

NOTE: There's bunch of other examples, I'm just giving one.

Could you flesh this out a bit? I don't quite understand the scenario from what you describe above.

In any case, I'm open to other implementations that fix the race we know about. If you think you have a better fix, I'm happy to review it and make sure it closes the other race.

author maintainer
#5 lore raw
Quoted 6 lines · Dave Hansen

On 1/30/21 11:20 AM, Jarkko Sakkinen wrote:
...

Quoted 4 lines

Example scenario would such that all removals "side-channel" through the notifier callback. Then mmu_notifier_unregister() gets called exactly zero times. No MMU notifier srcu sync would be then happening.

Quoted 8 lines
Quoted 1 line

NOTE: There's bunch of other examples, I'm just giving one.

Could you flesh this out a bit? I don't quite understand the scenario from what you describe above.

In any case, I'm open to other implementations that fix the race we know about. If you think you have a better fix, I'm happy to review it and make sure it closes the other race.

I'll bake up a new patch. Generally speaking, I think why this has been so difficult, is because of a chicken-egg-problem. The whole issue should be sorted when a new entry is first added to the mm_list, i.e. increase the refcount for each added entry.

/Jarkko