Skip to contentSkip to conversation

x86/sgx: Drop enclave reference on MMU notifier registration failure

PATCH In review Ruoyu Wang 4 participants 4 messages Aug 14 – Aug 21

Subsystems (2)
lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH 1/1 x86/sgx: Drop enclave reference on MMU notifier registration failure

Each sgx_encl_mm instance holds an enclave reference so the enclave outlives its MMU notifier. sgx_encl_mm_add() acquires that reference before calling __mmu_notifier_register().

Notifier registration can fail while allocating subscription state or when mm_take_all_locks() is interrupted. In that case no notifier or mm_list entry takes ownership of encl_mm. The error path frees encl_mm but leaves the enclave reference behind, preventing the enclave and its resources from being released.

Drop the enclave reference before freeing the unpublished encl_mm. This balances the acquisition without changing the successful registration and teardown paths.

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 2ade0d60939b ("x86/sgx: Maintain encl->refcount for each encl->mm_list entry")
---
 arch/x86/kernel/cpu/sgx/encl.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/sgx/encl.c +1 -0
index 3f0222d10f6e6..04fbf7e703e1e 100644
@@ -866,6 +866,7 @@ int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm)
 
 	ret = __mmu_notifier_register(&encl_mm->mmu_notifier, mm);
 	if (ret) {
+		kref_put(&encl->refcount, sgx_encl_release);
 		kfree(encl_mm);
 		return ret;
 	}
Signature · 1 line
2.51.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Ruoyu Wang

Each sgx_encl_mm instance holds an enclave reference so the enclave outlives its MMU notifier. sgx_encl_mm_add() acquires that reference before calling __mmu_notifier_register().

Notifier registration can fail while allocating subscription state or when mm_take_all_locks() is interrupted. In that case no notifier or mm_list entry takes ownership of encl_mm. The error path frees encl_mm but leaves the enclave reference behind, preventing the enclave and its resources from being released.

Drop the enclave reference before freeing the unpublished encl_mm. This balances the acquisition without changing the successful registration and teardown paths.

Quoted 5 lines

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 2ade0d60939b ("x86/sgx: Maintain encl->refcount for each encl->mm_list entry")
#2 lore raw
In reply to Ruoyu Wang

…

Quoted 3 lines

Drop the enclave reference before freeing the unpublished encl_mm. This balances the acquisition without changing the successful registration and teardown paths.

…

How do you think about to increase the application of scope-based resource management also for an improved implementation of a function like sgx_encl_mm_add()?

Regards,
Markus

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Ruoyu Wang
Quoted 38 lines · Ruoyu Wang

Each sgx_encl_mm instance holds an enclave reference so the enclave outlives its MMU notifier. sgx_encl_mm_add() acquires that reference before calling __mmu_notifier_register().

Notifier registration can fail while allocating subscription state or when mm_take_all_locks() is interrupted. In that case no notifier or mm_list entry takes ownership of encl_mm. The error path frees encl_mm but leaves the enclave reference behind, preventing the enclave and its resources from being released.

Drop the enclave reference before freeing the unpublished encl_mm. This balances the acquisition without changing the successful registration and teardown paths.

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes 2ade0d60939b ("x86/sgx: Maintain encl->refcount for each encl->mm_list entry")
---
 arch/x86/kernel/cpu/sgx/encl.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/sgx/encl.c +1 -0
index 3f0222d10f6e6..04fbf7e703e1e 100644
@@ -866,6 +866,7 @@ int sgx_encl_mm_add(struct sgx_encl *encl, struct mm_struct *mm)
 
 	ret = __mmu_notifier_register(&encl_mm->mmu_notifier, mm);
 	if (ret) {
+		kref_put(&encl->refcount, sgx_encl_release);
 		kfree(encl_mm);
 		return ret;
 	}
Signature · 1 line
2.51.0

BR, Jarkko