* [PATCH] media: em28xx: clear dev->v4l2 on init failure and fini to avoid double put
@ 2026-11-22 19:00 Arbab Haider
2026-11-25 7:40 ` Hans Verkuil
0 siblings, 1 reply; 2+ messages in thread
From: Arbab Haider @ 2026-11-22 19:00 UTC (permalink / raw)
To: Mauro Carvalho Chehab, Hans Verkuil
Cc: linux-media, linux-kernel, syzbot+fb0d2df09b56117605fc, stable,
Arbab Haider
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>
---
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);
/*
--
2.53.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] media: em28xx: clear dev->v4l2 on init failure and fini to avoid double put
2026-11-22 19:00 [PATCH] media: em28xx: clear dev->v4l2 on init failure and fini to avoid double put Arbab Haider
@ 2026-11-25 7:40 ` Hans Verkuil
0 siblings, 0 replies; 2+ messages in thread
From: Hans Verkuil @ 2026-11-25 7:40 UTC (permalink / raw)
To: Arbab Haider, Mauro Carvalho Chehab
Cc: linux-media, linux-kernel, syzbot+fb0d2df09b56117605fc, stable
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);
>
> /*
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-25 7:40 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-22 19:00 [PATCH] media: em28xx: clear dev->v4l2 on init failure and fini to avoid double put Arbab Haider
2026-11-25 7:40 ` Hans Verkuil
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®