[PATCH 1/2] crypto: ccp - fix leak and stale misc_dev on SFS misc_register() failure

From: Kim Phillips

Date: Tue Nov 17 2026 - 17:19:43 EST


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@xxxxxxxxxx>
Closes: https://sashiko.dev/#/bug/linux-126662e0-70a1-4086-a5d1-2f1e15a14f20
Cc: Ashish Kalra <ashish.kalra@xxxxxxx>
Cc: Rijo Thomas <Rijo-john.Thomas@xxxxxxx>
Cc: Tom Lendacky <thomas.lendacky@xxxxxxx>
Cc: Mario Limonciello <mario.limonciello@xxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kim Phillips <kim.phillips@xxxxxxx>
---
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