All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/sgx: Drop enclave reference on MMU notifier registration failure
@ 2026-08-14 13:40 Ruoyu Wang
  2026-08-18 11:27 ` Huang, Kai
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ruoyu Wang @ 2026-08-14 13:40 UTC (permalink / raw)
  To: Jarkko Sakkinen, Dave Hansen, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, H. Peter Anvin
  Cc: x86, linux-sgx, linux-kernel, 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")
Signed-off-by: Ruoyu Wang <ruoyuw560@gmail.com>
---
 arch/x86/kernel/cpu/sgx/encl.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
index 3f0222d10f6e6..04fbf7e703e1e 100644
--- a/arch/x86/kernel/cpu/sgx/encl.c
+++ b/arch/x86/kernel/cpu/sgx/encl.c
@@ -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;
 	}
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/sgx: Drop enclave reference on MMU notifier registration failure
  2026-08-14 13:40 [PATCH] x86/sgx: Drop enclave reference on MMU notifier registration failure Ruoyu Wang
@ 2026-08-18 11:27 ` Huang, Kai
  2026-08-20 12:30 ` Markus Elfring
  2026-08-21  1:15 ` Jarkko Sakkinen
  2 siblings, 0 replies; 4+ messages in thread
From: Huang, Kai @ 2026-08-18 11:27 UTC (permalink / raw)
  To: tglx@linutronix.de, hpa@zytor.com, mingo@redhat.com,
	ruoyuw560@gmail.com, jarkko@kernel.org,
	dave.hansen@linux.intel.com, bp@alien8.de
  Cc: linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org,
	x86@kernel.org

On Fri, 2026-08-14 at 21:40 +0800, Ruoyu Wang wrote:
> 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")
> Signed-off-by: Ruoyu Wang <ruoyuw560@gmail.com>

Reviewed-by: Kai Huang <kai.huang@intel.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/sgx: Drop enclave reference on MMU notifier registration failure
  2026-08-14 13:40 [PATCH] x86/sgx: Drop enclave reference on MMU notifier registration failure Ruoyu Wang
  2026-08-18 11:27 ` Huang, Kai
@ 2026-08-20 12:30 ` Markus Elfring
  2026-08-21  1:15 ` Jarkko Sakkinen
  2 siblings, 0 replies; 4+ messages in thread
From: Markus Elfring @ 2026-08-20 12:30 UTC (permalink / raw)
  To: Ruoyu Wang, linux-sgx, x86, Borislav Petkov, Dave Hansen,
	H. Peter Anvin, Ingo Molnar, Jarkko Sakkinen, Thomas Gleixner
  Cc: LKML> 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] x86/sgx: Drop enclave reference on MMU notifier registration failure
  2026-08-14 13:40 [PATCH] x86/sgx: Drop enclave reference on MMU notifier registration failure Ruoyu Wang
  2026-08-18 11:27 ` Huang, Kai
  2026-08-20 12:30 ` Markus Elfring
@ 2026-08-21  1:15 ` Jarkko Sakkinen
  2 siblings, 0 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2026-08-21  1:15 UTC (permalink / raw)
  To: Ruoyu Wang
  Cc: Dave Hansen, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	H. Peter Anvin, x86, linux-sgx, linux-kernel

On Fri, Aug 14, 2026 at 09:40:20PM +0800, Ruoyu Wang wrote:
> 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")
> Signed-off-by: Ruoyu Wang <ruoyuw560@gmail.com>
> ---
>  arch/x86/kernel/cpu/sgx/encl.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
> index 3f0222d10f6e6..04fbf7e703e1e 100644
> --- a/arch/x86/kernel/cpu/sgx/encl.c
> +++ b/arch/x86/kernel/cpu/sgx/encl.c
> @@ -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;
>  	}
> -- 
> 2.51.0
> 

Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>

BR, Jarkko

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-08-21  1:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-14 13:40 [PATCH] x86/sgx: Drop enclave reference on MMU notifier registration failure Ruoyu Wang
2026-08-18 11:27 ` Huang, Kai
2026-08-20 12:30 ` Markus Elfring
2026-08-21  1:15 ` Jarkko Sakkinen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.