Re: [PATCH] media: em28xx: clear dev->v4l2 on init failure and fini to avoid double put

From: Arbab Haider

Date: Thu Nov 26 2026 - 05:33:43 EST


Hi Hans,

I checked media-committers/next at b4e3c4b6a36f. The init error path
already clears dev->v4l2 under dev->lock before dropping the initial
reference, and em28xx_v4l2_fini() checks the pointer under the same
lock. That covers the double put this patch was meant to fix, so
I'm dropping the standalone patch.

Thanks,
Arbab


On Wed, 25 Nov 2026 at 12:40, Hans Verkuil <hverkuil+cisco@xxxxxxxxxx> wrote:
>
> 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@xxxxxxxxxxxxxxxxxxxxxxxxx
> > Closes: https://syzkaller.appspot.com/bug?extid=fb0d2df09b56117605fc
> > Fixes: 288254383674 ("media: em28xx: use v4l2_device release callback")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Arbab Haider <arbabhaider649@xxxxxxxxx>
>
> 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);
> >
> > /*
>