From: xiaopeitux@foxmail.com
To: shuangpeng.kernel@gmail.com, mchehab@kernel.org,
mchehab+huawei@kernel.org
Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
aha310510@gmail.com, hverkuil+cisco@kernel.org,
Pei Xiao <xiaopei01@kylinos.cn>,
stable@vger.kernel.org
Subject: [PATCH 1/2] media: as102: fix memory leak in probe error path after device registration
Date: Wed, 3 Jun 2026 17:30:23 +0800 [thread overview]
Message-ID: <tencent_B3E59D0CCEA1BBC24538702F522540CA780A@qq.com> (raw)
In-Reply-To: <DAD7161C-A1DA-422E-BBC5-2893ABDC2DD0@gmail.com>
From: Pei Xiao <xiaopei01@kylinos.cn>
Commit 8bd29dbe03fc ("media: as102: fix to not free memory after the
device is registered in as102_usb_probe()") solved a use-after-free /
double-free race condition by never freeing as102_dev directly once
usb_register_dev() had succeeded. Instead, the memory is freed in
as102_usb_release() when the last open file descriptor is closed.
However, this introduced a memory leak when the probe fails after
usb_register_dev() succeeds but no userspace process has opened the
device node. In that scenario, usb_deregister_dev() removes the device
node, but as102_dev is never freed because as102_usb_release() is never
called (there is no open fd). The only reference to the object (the
interface's private data) is cleared, leaving the allocated memory
completely unreachable.
Fix the leak by calling kref_put() on as102_dev->kref in the
failed_stream error path. The kref was initialised to 1 during probe,
and as102_usb_release() is the release function that performs kfree().
If no open fd exists, the kref_put() will drop the initial reference to
zero and trigger as102_usb_release() immediately, freeing the memory.
If there are open fds, the extra references keep the object alive until
the last close, which is the correct behaviour.
Fixes: 8bd29dbe03fc ("media: as102: fix to not free memory after the device is registered in as102_usb_probe()")
Reported-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
Closes: https://lore.kernel.org/lkml/DAD7161C-A1DA-422E-BBC5-2893ABDC2DD0@gmail.com/
Cc: stable@vger.kernel.org
Signed-off-by: Pei Xiao <xiaopei01@kylinos.cn>
---
drivers/media/usb/as102/as102_usb_drv.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/media/usb/as102/as102_usb_drv.c b/drivers/media/usb/as102/as102_usb_drv.c
index a11024451ceb..ad6c5837f1d7 100644
--- a/drivers/media/usb/as102/as102_usb_drv.c
+++ b/drivers/media/usb/as102/as102_usb_drv.c
@@ -405,6 +405,7 @@ static int as102_usb_probe(struct usb_interface *intf,
failed_stream:
usb_set_intfdata(intf, NULL);
usb_deregister_dev(intf, &as102_usb_class_driver);
+ kref_put(&as102_dev->kref, as102_usb_release);
return ret;
failed:
usb_put_dev(as102_dev->bus_adap.usb_dev);
--
2.25.1
prev parent reply other threads:[~2026-06-03 9:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-02 20:38 [BUG] media: as102: kmemleak reports two leaks after dvb_register_frontend() failure Shuangpeng
2026-06-03 9:30 ` xiaopeitux [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=tencent_B3E59D0CCEA1BBC24538702F522540CA780A@qq.com \
--to=xiaopeitux@foxmail.com \
--cc=aha310510@gmail.com \
--cc=hverkuil+cisco@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab+huawei@kernel.org \
--cc=mchehab@kernel.org \
--cc=shuangpeng.kernel@gmail.com \
--cc=stable@vger.kernel.org \
--cc=xiaopei01@kylinos.cn \
/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
all inboxes | Powered by JetHome®