From: Hans Verkuil <hverkuil+cisco@kernel.org>
To: Arbab Haider <arbabhaider649@gmail.com>,
Mauro Carvalho Chehab <mchehab@kernel.org>
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH v2 1/2] media: em28xx: defer media_device cleanup and free to em28xx_free_device()
Date: Wed, 25 Nov 2026 08:40:45 +0100 [thread overview]
Message-ID: <403ad923-6430-42bc-9e16-f02033743a70@kernel.org> (raw)
In-Reply-To: <20261122222325.41868-2-arbabhaider649@gmail.com>
On 22/11/2026 23:23, Arbab Haider wrote:
> When a USB em28xx device is disconnected while a V4L2 device node
> (e.g. /dev/video0) is still open, em28xx_usb_disconnect() calls
> em28xx_release_resources() -> em28xx_unregister_media_device(), which
> calls media_device_cleanup() and kfree(dev->media_dev) immediately.
>
> However, struct em28xx_v4l2 remains alive until the last open file
> handle is closed, and v4l2->v4l2_dev.mdev still points to the freed
> media_device. When userspace subsequently closes /dev/video0,
> v4l2_release() calls v4l2_device_supports_requests(vdev->v4l2_dev),
> dereferencing vdev->v4l2_dev->mdev->ops and triggering a KASAN
> slab-use-after-free:
>
> BUG: KASAN: slab-use-after-free in v4l2_release+0x325/0x380
> Read of size 8 at addr ffff88800787a260 by task test_em28xx_uaf/53
> ...
> Freed by task 30:
> kfree+0x162/0x410
> em28xx_unregister_media_device+0xa4/0xe0
> em28xx_release_resources+0x70/0x170
> em28xx_usb_disconnect.cold+0x107/0x2a3
>
> Split em28xx_unregister_media_device() so it only calls
> media_device_unregister(dev->media_dev) on disconnect, and move
> media_device_cleanup() and kfree(dev->media_dev) into a new
> em28xx_cleanup_media_device() helper called from em28xx_free_device()
> (once the last dev->ref reference, including em28xx_free_v4l2(), has
> been dropped) and from em28xx_init_dev()'s error path.
>
> Tested locally in QEMU (x86_64, KASAN+UBSAN) using dummy_hcd and
> raw-gadget emulating an EM2820 device across open->disconnect->close,
> repeated disconnect, dual-fd open->disconnect->close, and normal
> open->close->disconnect scenarios (8/8 passed, 0 KASAN reports).
>
> Fixes: 37ecc7b1278f ("[media] em28xx: add media controller support")
> Cc: stable@vger.kernel.org
> Signed-off-by: Arbab Haider <arbabhaider649@gmail.com>
This patch no longer applies, can you rebase it on top of
https://gitlab.freedesktop.org/linux-media/media-committers, next branch?
Thank you,
Hans
> ---
> Changes in v2:
> - Add Fixes: tag as requested by Media CI.
>
> drivers/media/usb/em28xx/em28xx-cards.c | 12 +++++++++++-
> 1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c
> index 1d4f30cba..d8f2e40b7 100644
> --- a/drivers/media/usb/em28xx/em28xx-cards.c
> +++ b/drivers/media/usb/em28xx/em28xx-cards.c
> @@ -3773,8 +3773,15 @@ static int em28xx_media_device_init(struct em28xx *dev,
> static void em28xx_unregister_media_device(struct em28xx *dev)
> {
> #ifdef CONFIG_MEDIA_CONTROLLER
> - if (dev->media_dev) {
> + if (dev->media_dev)
> media_device_unregister(dev->media_dev);
> +#endif
> +}
> +
> +static void em28xx_cleanup_media_device(struct em28xx *dev)
> +{
> +#ifdef CONFIG_MEDIA_CONTROLLER
> + if (dev->media_dev) {
> media_device_cleanup(dev->media_dev);
> kfree(dev->media_dev);
> dev->media_dev = NULL;
> @@ -3826,6 +3833,8 @@ void em28xx_free_device(struct kref *ref)
> if (!dev->disconnected)
> em28xx_release_resources(dev);
>
> + em28xx_cleanup_media_device(dev);
> +
> if (dev->ts == PRIMARY_TS)
> kfree(dev->alt_max_pkt_size_isoc);
>
> @@ -3997,6 +4006,7 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev,
> em28xx_i2c_unregister(dev, 0);
> err_deinit_media:
> em28xx_unregister_media_device(dev);
> + em28xx_cleanup_media_device(dev);
> return retval;
> }
>
prev parent reply other threads:[~2026-11-25 7:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 22:23 Arbab Haider
2026-11-22 22:23 ` [PATCH v2 2/2] media: em28xx: avoid dereferencing dev->intf after disconnect in em28xx_v4l2_close() Arbab Haider
2026-11-25 7:40 ` Hans Verkuil [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=403ad923-6430-42bc-9e16-f02033743a70@kernel.org \
--to=hverkuil+cisco@kernel.org \
--cc=arbabhaider649@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=stable@vger.kernel.org \
/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®