From: Kim Phillips <kim.phillips@amd.com>
To: Tom Lendacky <thomas.lendacky@amd.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
"David S . Miller" <davem@davemloft.net>
Cc: Ashish Kalra <ashish.kalra@amd.com>,
Rijo Thomas <Rijo-john.Thomas@amd.com>,
Mario Limonciello <mario.limonciello@amd.com>,
<linux-crypto@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<stable@vger.kernel.org>, Kim Phillips <kim.phillips@amd.com>,
Sashiko <sashiko-bot@kernel.org>
Subject: [PATCH 2/2] crypto: ccp - fix leak and stale misc_dev on SEV misc_register() failure
Date: Tue, 17 Nov 2026 16:19:01 -0600 [thread overview]
Message-ID: <20261117221901.746427-3-kim.phillips@amd.com> (raw)
In-Reply-To: <20261117221901.746427-1-kim.phillips@amd.com>
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
prev parent reply other threads:[~2026-11-17 22:19 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117221901.746427-3-kim.phillips@amd.com \
--to=kim.phillips@amd.com \
--cc=Rijo-john.Thomas@amd.com \
--cc=ashish.kalra@amd.com \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mario.limonciello@amd.com \
--cc=sashiko-bot@kernel.org \
--cc=stable@vger.kernel.org \
--cc=thomas.lendacky@amd.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox