mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] crypto: ccp - fix misc_register() failure paths in SEV and SFS
@ 2026-11-17 22:18 Kim Phillips
  2026-11-17 22:19 ` [PATCH 1/2] crypto: ccp - fix leak and stale misc_dev on SFS misc_register() failure Kim Phillips
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Kim Phillips @ 2026-11-17 22:18 UTC (permalink / raw)
  To: Tom Lendacky, Herbert Xu, David S . Miller
  Cc: Ashish Kalra, Rijo Thomas, Mario Limonciello, linux-crypto,
	linux-kernel, stable, Kim Phillips

sev_misc_init() and sfs_misc_init() both publish their newly allocated
sev_misc_dev/sfs_misc_dev to a file-global pointer before registering
the misc device, then return early if misc_register() fails.  Neither
frees the allocation nor clears the global, and the early return is
ahead of the kref_init(), so the embedded refcount is left at the zero
from kzalloc_obj().

Both features can be detected on more than one device, and a failure
here only aborts the probe of the device that hit it.  The next device
to probe therefore sees a stale non-NULL global, takes the else branch,
and calls kref_get() on a zeroed refcount:

  refcount_t: addition on 0; use-after-free.

The count stays at zero, so the matching kref_put() on teardown warns in
the same way and never reaches sev_exit()/sfs_exit().  What is left is a
leaked allocation plus a device whose ->misc points at a miscdevice that
was never registered, while the driver still reports the feature as
available and no /dev/sev or /dev/sfs node exists.

Both patches free the allocation and reset the global on the
misc_register() error path.

Note the differing Fixes: tags.  The SEV stale-pointer half goes back to
the original SEV support commit, but the allocation was devm_kzalloc()
then, so backporting the kfree() that far would turn the leak into a
double free; the tag points at the commit from which the fix is safe.

Found by review of the equivalent bug reports; no hardware failure was
observed, as misc_register() only fails here on minor exhaustion or an
OOM.  Compile-tested on x86_64 with CONFIG_CRYPTO_DEV_SP_PSP=y.

The two patches are independent of each other, and of the pending

  https://lore.kernel.org/linux-crypto/20261116203622.615371-1-kim.phillips@amd.com/

which touches psp-dev.c only.  This series applies on top of it, but
also applies cleanly to cryptodev master without it.

Kim Phillips (2):
  crypto: ccp - fix leak and stale misc_dev on SFS misc_register()
    failure
  crypto: ccp - fix leak and stale misc_dev on SEV misc_register()
    failure

 drivers/crypto/ccp/sev-dev.c | 5 ++++-
 drivers/crypto/ccp/sfs.c     | 5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)


base-commit: 26ca5f34841f0a86d0883664a1c13be89a01d351
-- 
2.43.0


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

* [PATCH 1/2] crypto: ccp - fix leak and stale misc_dev on SFS misc_register() failure
  2026-11-17 22:18 [PATCH 0/2] crypto: ccp - fix misc_register() failure paths in SEV and SFS Kim Phillips
@ 2026-11-17 22:19 ` Kim Phillips
  2026-11-17 22:19 ` [PATCH 2/2] crypto: ccp - fix leak and stale misc_dev on SEV " Kim Phillips
  2026-11-18 16:05 ` [PATCH 0/2] crypto: ccp - fix misc_register() failure paths in SEV and SFS Mario Limonciello
  2 siblings, 0 replies; 4+ messages in thread
From: Kim Phillips @ 2026-11-17 22:19 UTC (permalink / raw)
  To: Tom Lendacky, Herbert Xu, David S . Miller
  Cc: Ashish Kalra, Rijo Thomas, Mario Limonciello, linux-crypto,
	linux-kernel, stable, Kim Phillips, Sashiko

sfs_misc_init() assigns the newly allocated structure to the global
misc_dev before registering the misc device, but returns early when
misc_register() fails, without freeing the allocation or clearing the
global pointer.  The early return also skips kref_init(), so the
embedded refcount stays at the zero left by kzalloc_obj().

SFS support can be detected on multiple PSP devices, and the failure
only aborts the probe of the device that hit it.  When a subsequent
device probes, sfs_misc_init() observes the stale non-NULL misc_dev and
takes the else branch, calling kref_get() on the zeroed refcount.  That
triggers a "refcount_t: addition on 0; use-after-free" warning and
leaves the count at zero, so the matching kref_put() in
sfs_dev_destroy() warns too and never reaches sfs_exit().  The device
goes on to report that SFS support is available even though /dev/sfs was
never created, and the original allocation is leaked.

Free the allocation and reset misc_dev to NULL on the misc_register()
error path.

Fixes: 648dbccc03a0 ("crypto: ccp - Add AMD Seamless Firmware Servicing (SFS) driver")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-126662e0-70a1-4086-a5d1-2f1e15a14f20
Cc: Ashish Kalra <ashish.kalra@amd.com>
Cc: Rijo Thomas <Rijo-john.Thomas@amd.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Mario Limonciello <mario.limonciello@amd.com>
Cc: stable@vger.kernel.org
Signed-off-by: Kim Phillips <kim.phillips@amd.com>
---
 drivers/crypto/ccp/sfs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/crypto/ccp/sfs.c b/drivers/crypto/ccp/sfs.c
index 530141f8bc74..01b16e318fc7 100644
--- a/drivers/crypto/ccp/sfs.c
+++ b/drivers/crypto/ccp/sfs.c
@@ -237,8 +237,11 @@ static int sfs_misc_init(struct sfs_device *sfs)
 		misc->mode = 0600;
 
 		ret = misc_register(misc);
-		if (ret)
+		if (ret) {
+			kfree(misc_dev);
+			misc_dev = NULL;
 			return ret;
+		}
 
 		kref_init(&misc_dev->refcount);
 	} else {
-- 
2.43.0


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

* [PATCH 2/2] crypto: ccp - fix leak and stale misc_dev on SEV misc_register() failure
  2026-11-17 22:18 [PATCH 0/2] crypto: ccp - fix misc_register() failure paths in SEV and SFS Kim Phillips
  2026-11-17 22:19 ` [PATCH 1/2] crypto: ccp - fix leak and stale misc_dev on SFS misc_register() failure Kim Phillips
@ 2026-11-17 22:19 ` Kim Phillips
  2026-11-18 16:05 ` [PATCH 0/2] crypto: ccp - fix misc_register() failure paths in SEV and SFS Mario Limonciello
  2 siblings, 0 replies; 4+ messages in thread
From: Kim Phillips @ 2026-11-17 22:19 UTC (permalink / raw)
  To: Tom Lendacky, Herbert Xu, David S . Miller
  Cc: Ashish Kalra, Rijo Thomas, Mario Limonciello, linux-crypto,
	linux-kernel, stable, Kim Phillips, Sashiko

sev_misc_init() assigns the newly allocated structure to the global
misc_dev before registering the misc device, but returns early when
misc_register() fails, without freeing the allocation or clearing the
global pointer.  The early return also skips kref_init(), so the
embedded refcount stays at the zero left by kzalloc_obj().

SEV support can be detected on multiple devices, and the failure only
aborts the probe of the device that hit it.  When a subsequent device
probes, sev_misc_init() observes the stale non-NULL misc_dev and takes
the else branch, calling kref_get() on the zeroed refcount.  That
triggers a "refcount_t: addition on 0; use-after-free" warning and
leaves the count at zero, so the matching kref_put() in
sev_dev_destroy() warns too and never reaches sev_exit().  The device
ends up with sev->misc pointing at a never-registered miscdevice while
/dev/sev does not exist, and the original allocation is leaked.

Free the allocation and reset misc_dev to NULL on the misc_register()
error path.

The stale pointer dates back to the introduction of the driver, but the
leak only appeared once the allocation moved off devm_kzalloc(), which
is also the point from which this kfree() is safe to apply.

Fixes: 1f14b57ffc0a ("crypto: ccp - Cleanup misc_dev on sev_exit()")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-c271284a-0608-480f-8b5c-eee039d6b4c0
Cc: Ashish Kalra <ashish.kalra@amd.com>
Cc: Rijo Thomas <Rijo-john.Thomas@amd.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Mario Limonciello <mario.limonciello@amd.com>
Cc: stable@vger.kernel.org
Signed-off-by: Kim Phillips <kim.phillips@amd.com>
---
 drivers/crypto/ccp/sev-dev.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c
index b980dfc7f3a3..56e38a9539da 100644
--- a/drivers/crypto/ccp/sev-dev.c
+++ b/drivers/crypto/ccp/sev-dev.c
@@ -2883,8 +2883,11 @@ static int sev_misc_init(struct sev_device *sev)
 		misc->fops = &sev_fops;
 
 		ret = misc_register(misc);
-		if (ret)
+		if (ret) {
+			kfree(misc_dev);
+			misc_dev = NULL;
 			return ret;
+		}
 
 		kref_init(&misc_dev->refcount);
 	} else {
-- 
2.43.0


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

* Re: [PATCH 0/2] crypto: ccp - fix misc_register() failure paths in SEV and SFS
  2026-11-17 22:18 [PATCH 0/2] crypto: ccp - fix misc_register() failure paths in SEV and SFS Kim Phillips
  2026-11-17 22:19 ` [PATCH 1/2] crypto: ccp - fix leak and stale misc_dev on SFS misc_register() failure Kim Phillips
  2026-11-17 22:19 ` [PATCH 2/2] crypto: ccp - fix leak and stale misc_dev on SEV " Kim Phillips
@ 2026-11-18 16:05 ` Mario Limonciello
  2 siblings, 0 replies; 4+ messages in thread
From: Mario Limonciello @ 2026-11-18 16:05 UTC (permalink / raw)
  To: Kim Phillips, Tom Lendacky, Herbert Xu, David S . Miller
  Cc: Ashish Kalra, Rijo Thomas, linux-crypto, linux-kernel, stable



On 11/17/26 16:18, Kim Phillips wrote:
> sev_misc_init() and sfs_misc_init() both publish their newly allocated
> sev_misc_dev/sfs_misc_dev to a file-global pointer before registering
> the misc device, then return early if misc_register() fails.  Neither
> frees the allocation nor clears the global, and the early return is
> ahead of the kref_init(), so the embedded refcount is left at the zero
> from kzalloc_obj().
> 
> Both features can be detected on more than one device, and a failure
> here only aborts the probe of the device that hit it.  The next device
> to probe therefore sees a stale non-NULL global, takes the else branch,
> and calls kref_get() on a zeroed refcount:
> 
>    refcount_t: addition on 0; use-after-free.
> 
> The count stays at zero, so the matching kref_put() on teardown warns in
> the same way and never reaches sev_exit()/sfs_exit().  What is left is a
> leaked allocation plus a device whose ->misc points at a miscdevice that
> was never registered, while the driver still reports the feature as
> available and no /dev/sev or /dev/sfs node exists.
> 
> Both patches free the allocation and reset the global on the
> misc_register() error path.
> 
> Note the differing Fixes: tags.  The SEV stale-pointer half goes back to
> the original SEV support commit, but the allocation was devm_kzalloc()
> then, so backporting the kfree() that far would turn the leak into a
> double free; the tag points at the commit from which the fix is safe.
> 
> Found by review of the equivalent bug reports; no hardware failure was
> observed, as misc_register() only fails here on minor exhaustion or an
> OOM.  Compile-tested on x86_64 with CONFIG_CRYPTO_DEV_SP_PSP=y.
> 
> The two patches are independent of each other, and of the pending
> 
>    https://lore.kernel.org/linux-crypto/20261116203622.615371-1-kim.phillips@amd.com/
> 
> which touches psp-dev.c only.  This series applies on top of it, but
> also applies cleanly to cryptodev master without it.
> 
> Kim Phillips (2):
>    crypto: ccp - fix leak and stale misc_dev on SFS misc_register()
>      failure
>    crypto: ccp - fix leak and stale misc_dev on SEV misc_register()
>      failure
> 
>   drivers/crypto/ccp/sev-dev.c | 5 ++++-
>   drivers/crypto/ccp/sfs.c     | 5 ++++-
>   2 files changed, 8 insertions(+), 2 deletions(-)
> 
> 
> base-commit: 26ca5f34841f0a86d0883664a1c13be89a01d351

Reviewed-by: Mario Limonciello (AMD) <superm1@kernel.org>

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

end of thread, other threads:[~2026-11-18 16:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 22:18 [PATCH 0/2] crypto: ccp - fix misc_register() failure paths in SEV and SFS Kim Phillips
2026-11-17 22:19 ` [PATCH 1/2] crypto: ccp - fix leak and stale misc_dev on SFS misc_register() failure Kim Phillips
2026-11-17 22:19 ` [PATCH 2/2] crypto: ccp - fix leak and stale misc_dev on SEV " Kim Phillips
2026-11-18 16:05 ` [PATCH 0/2] crypto: ccp - fix misc_register() failure paths in SEV and SFS Mario Limonciello

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®