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,
syzbot+fb0d2df09b56117605fc@syzkaller.appspotmail.com,
stable@vger.kernel.org
Subject: Re: [PATCH] media: em28xx: clear dev->v4l2 on init failure and fini to avoid double put
Date: Wed, 25 Nov 2026 08:40:12 +0100 [thread overview]
Message-ID: <4724fb41-06a0-457c-be3a-25ad9e49e25f@kernel.org> (raw)
In-Reply-To: <20261122190008.21278-2-arbabhaider649@gmail.com>
On 22/11/2026 20:00, Arbab Haider wrote:
> Commit 288254383674 ("media: em28xx: use v4l2_device release callback")
> switched em28xx_v4l2 teardown to v4l2_device reference counting, where
> dev->v4l2 is only cleared inside em28xx_free_v4l2() once the refcount
> reaches zero.
>
> If em28xx_v4l2_init() fails after video_register_device() (for example,
> when v4l2_mc_create_media_graph() returns -EINVAL) while userspace has
> opened the video node, the unregister_dev error path calls
> v4l2_device_put(&v4l2->v4l2_dev) to drop the initial reference, but
> em28xx_free_v4l2() does not run yet because the open file handle still
> holds a reference, leaving dev->v4l2 non-NULL.
>
> When the USB device is subsequently disconnected, em28xx_v4l2_fini()
> sees dev->v4l2 != NULL and calls v4l2_device_put(&v4l2->v4l2_dev) a
> second time for the initial reference, freeing struct em28xx_v4l2 while
> the video device node is still referenced and causing a
> slab-use-after-free in media_device_unregister() /
> __media_entity_remove_links().
>
> Fix this by:
> - Calling v4l2_device_disconnect() and clearing dev->v4l2 = NULL under
> dev->lock in em28xx_v4l2_init()'s unregister_dev path before dropping
> the initial v4l2_device reference.
> - Checking and clearing dev->v4l2 under dev->lock in em28xx_v4l2_fini()
> so the initial reference is dropped at most once.
> - Reading dev->v4l2 under dev->lock and checking for !v4l2 in
> em28xx_v4l2_close() so a close racing with teardown cannot dereference
> a NULL pointer.
>
> Tested locally in QEMU (x86_64, KASAN+UBSAN) using dummy_hcd and
> raw-gadget emulating an EM2820 device with failed media graph creation
> and concurrent /dev/video0 open/disconnect.
>
> Reported-by: syzbot+fb0d2df09b56117605fc@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=fb0d2df09b56117605fc
> Fixes: 288254383674 ("media: em28xx: use v4l2_device release callback")
> 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?
This patch might not be needed anymore, it's unclear.
Regards,
Hans
> ---
> drivers/media/usb/em28xx/em28xx-video.c | 23 ++++++++++++++++-------
> 1 file changed, 16 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c
> index 84424284d..50998aa1c 100644
> --- a/drivers/media/usb/em28xx/em28xx-video.c
> +++ b/drivers/media/usb/em28xx/em28xx-video.c
> @@ -2333,7 +2333,6 @@ static void em28xx_free_v4l2(struct v4l2_device *v4l2_dev)
>
> v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
> v4l2_device_unregister(v4l2_dev);
> - dev->v4l2 = NULL;
> kfree(v4l2);
> kref_put(&dev->ref, em28xx_free_device);
> }
> @@ -2414,7 +2413,7 @@ static int em28xx_v4l2_open(struct file *filp)
> */
> static int em28xx_v4l2_fini(struct em28xx *dev)
> {
> - struct em28xx_v4l2 *v4l2 = dev->v4l2;
> + struct em28xx_v4l2 *v4l2;
>
> if (dev->is_audio_only) {
> /* Shouldn't initialize IR for this interface */
> @@ -2426,13 +2425,16 @@ static int em28xx_v4l2_fini(struct em28xx *dev)
> return 0;
> }
>
> - if (!v4l2)
> + mutex_lock(&dev->lock);
> +
> + v4l2 = dev->v4l2;
> + if (!v4l2) {
> + mutex_unlock(&dev->lock);
> return 0;
> + }
>
> dev_info(&dev->intf->dev, "Closing video extension\n");
>
> - mutex_lock(&dev->lock);
> -
> v4l2_device_disconnect(&v4l2->v4l2_dev);
>
> em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
> @@ -2455,6 +2457,8 @@ static int em28xx_v4l2_fini(struct em28xx *dev)
> vb2_video_unregister_device(&v4l2->vdev);
> }
>
> + dev->v4l2 = NULL;
> +
> mutex_unlock(&dev->lock);
>
> /*
> @@ -2502,18 +2506,19 @@ static int em28xx_v4l2_close(struct file *filp)
> {
> struct video_device *vdev = video_devdata(filp);
> struct em28xx *dev = video_drvdata(filp);
> - struct em28xx_v4l2 *v4l2 = dev->v4l2;
> + struct em28xx_v4l2 *v4l2;
> struct usb_device *udev = interface_to_usbdev(dev->intf);
> int err;
> bool last_user;
>
> mutex_lock(&dev->lock);
> + v4l2 = dev->v4l2;
> last_user = v4l2_fh_is_singular_file(filp);
> _vb2_fop_release(filp, vdev->queue->lock);
>
> if (last_user) {
> /* No sense to try to write to the device */
> - if (dev->disconnected)
> + if (dev->disconnected || !v4l2)
> goto exit;
>
> /* Save some power by putting tuner to sleep */
> @@ -3110,6 +3115,8 @@ static int em28xx_v4l2_init(struct em28xx *dev)
> return 0;
>
> unregister_dev:
> + v4l2_device_disconnect(&v4l2->v4l2_dev);
> +
> if (video_is_registered(&v4l2->radio_dev)) {
> dev_info(&dev->intf->dev,
> "V4L2 device %s deregistered\n",
> @@ -3129,6 +3136,8 @@ static int em28xx_v4l2_init(struct em28xx *dev)
> vb2_video_unregister_device(&v4l2->vdev);
> }
>
> + dev->v4l2 = NULL;
> +
> mutex_unlock(&dev->lock);
>
> /*
prev parent reply other threads:[~2026-11-25 7:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-22 19:00 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=4724fb41-06a0-457c-be3a-25ad9e49e25f@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 \
--cc=syzbot+fb0d2df09b56117605fc@syzkaller.appspotmail.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
all inboxes | Powered by JetHome®