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

From: Kim Phillips

Date: Tue Nov 17 2026 - 17:20:08 EST


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@xxxxxxxxxx>
Closes: https://sashiko.dev/#/bug/linux-c271284a-0608-480f-8b5c-eee039d6b4c0
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/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