Skip to content

Commit 2ade0d6

Browse files
jarkkojssuryasaimadhu
authored andcommitted
x86/sgx: Maintain encl->refcount for each encl->mm_list entry
This has been shown in tests: [ +0.000008] WARNING: CPU: 3 PID: 7620 at kernel/rcu/srcutree.c:374 cleanup_srcu_struct+0xed/0x100 This is essentially a use-after free, although SRCU notices it as an SRCU cleanup in an invalid context. == Background == SGX has a data structure (struct sgx_encl_mm) which keeps per-mm SGX metadata. This is separate from struct sgx_encl because, in theory, an enclave can be mapped from more than one mm. sgx_encl_mm includes a pointer back to the sgx_encl. This means that sgx_encl must have a longer lifetime than all of the sgx_encl_mm's that point to it. That's usually the case: sgx_encl_mm is freed only after the mmu_notifier is unregistered in sgx_release(). However, there's a race. If the process is exiting, sgx_mmu_notifier_release() can be called in parallel with sgx_release() instead of being called *by* it. The mmu_notifier path keeps encl_mm alive past when sgx_encl can be freed. This inverts the lifetime rules and means that sgx_mmu_notifier_release() can access a freed sgx_encl. == Fix == Increase encl->refcount when encl_mm->encl is established. Release this reference when encl_mm is freed. This ensures that encl outlives encl_mm. [ bp: Massage commit message. ] Fixes: 1728ab5 ("x86/sgx: Add a page reclaimer") Reported-by: Haitao Huang <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Acked-by: Dave Hansen <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 92bf226 commit 2ade0d6

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

arch/x86/kernel/cpu/sgx/driver.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ static int sgx_release(struct inode *inode, struct file *file)
7272
synchronize_srcu(&encl->srcu);
7373
mmu_notifier_unregister(&encl_mm->mmu_notifier, encl_mm->mm);
7474
kfree(encl_mm);
75+
76+
/* 'encl_mm' is gone, put encl_mm->encl reference: */
77+
kref_put(&encl->refcount, sgx_encl_release);
7578
}
7679

7780
kref_put(&encl->refcount, sgx_encl_release);

arch/x86/kernel/cpu/sgx/encl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,9 @@ static void sgx_mmu_notifier_free(struct mmu_notifier *mn)
481481
{
482482
struct sgx_encl_mm *encl_mm = container_of(mn, struct sgx_encl_mm, mmu_notifier);
483483

484+
/* 'encl_mm' is going away, put encl_mm->encl reference: */
485+
kref_put(&encl_mm->encl->refcount, sgx_encl_release);
486+
484487
kfree(encl_mm);
485488
}
486489

@@ -534,6 +537,8 @@ int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm)
534537
if (!encl_mm)
535538
return -ENOMEM;
536539

540+
/* Grab a refcount for the encl_mm->encl reference: */
541+
kref_get(&encl->refcount);
537542
encl_mm->encl = encl;
538543
encl_mm->mm = mm;
539544
encl_mm->mmu_notifier.ops = &sgx_mmu_notifier_ops;

0 commit comments

Comments
 (0)