media: em28xx: clear dev->v4l2 when v4l2 init fails
Subsystems (2)
syzbot
syzbotIn em28xx_v4l2_init(), if an error occurs after video_register_device() succeeds, the cleanup path at unregister_dev unregisters the devices and puts the v4l2_dev reference, but leaves dev->v4l2 set. Because dev->v4l2 remains non-NULL, subsequent calls to v4l2_open() (such as from v4l_id when opening the device node) succeed instead of returning -ENODEV. When the device is later disconnected, em28xx_close_extension() invokes em28xx_v4l2_fini(), which sees dev->v4l2 is still set and calls v4l2_device_put() a second time. This prematurely drops the reference count to zero while v4l_id still holds the device, leading to a slab-use-after-free:
BUG: KASAN: slab-use-after-free in v4l2_release+0xcd/0xe0 Read of size 8 at addr ffff888114efc000 by task v4l_id/1234 CPU: 1 UID: 0 PID: 1234 Comm: v4l_id Not tainted 6.14.0-rc1 #1 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0x68/0x90 print_report+0xd3/0x620 kasan_report+0xb9/0xf0 v4l2_release+0xcd/0xe0 __fput+0x3f6/0xb30 task_work_run+0x14d/0x240 do_exit+0xa38/0x2c00 do_group_exit+0xd4/0x2a0 __x64_sys_exit_group+0x3e/0x50 do_syscall_64+0xcb/0x250 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK> Allocated by task 6297: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 __kasan_kmalloc+0xaa/0xb0 em28xx_v4l2_init+0x9e/0x2df0 drivers/media/usb/em28xx/em28xx-video.c:2860 em28xx_init_extension+0x120/0x1d0 drivers/media/usb/em28xx/em28xx-core.c:1248 process_scheduled_works+0xc3d/0x1630 kernel/workqueue.c:3479 worker_thread+0x92d/0xe10 kernel/workqueue.c:3560 kthread+0x38b/0x480 kernel/kthread.c:436 ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245 Freed by task 6297: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 kasan_save_free_info+0x2b/0x50 __kasan_slab_free+0x106/0x1a0 kfree+0xd4/0x340 v4l2_device_put+0xbc/0xf0 drivers/media/v4l2-core/v4l2-dev.c:1052 em28xx_v4l2_fini+0x120/0x180 drivers/media/usb/em28xx/em28xx-video.c:2445 em28xx_close_extension+0x120/0x1d0 drivers/media/usb/em28xx/em28xx-core.c:1270 em28xx_usb_disconnect+0x10a/0x230 drivers/media/usb/em28xx/em28xx-cards.c:4254 usb_unbind_interface+0x1eb/0x8e0 drivers/usb/core/driver.c:461 device_release_driver_internal+0x43b/0x610 drivers/base/dd.c:1297 bus_remove_device+0x2cc/0x3e0 drivers/base/bus.c:575 device_del+0x49f/0x910 drivers/base/core.c:3851 usb_disable_device+0x35b/0x7b0 drivers/usb/core/message.c:1418 usb_disconnect+0x2c6/0x8d0 drivers/usb/core/hub.c:2303
Fix this by clearing dev->v4l2 under dev->lock in the unregister_dev error path of em28xx_v4l2_init(). This ensures em28xx_v4l2_open() fails with -ENODEV and prevents em28xx_v4l2_fini() from tearing down the instance a second time upon disconnect.
In addition, move em28xx_vb2_setup() before video_register_device() and check its return value so that videobuf2 queues are initialized prior to device registration and can be safely cleaned up if an error occurs. In em28xx_vb2_setup(), initialize vb_queue_lock and vb_vbi_queue_lock and assign q->lock before calling vb2_queue_init(), and make VBI queue initialization conditional on em28xx_vbi_supported().
To: <[email protected]>
To: "Mauro Carvalho Chehab" <[email protected]>
To: "Hans Verkuil" <[email protected]>
Cc: <[email protected]>
---
v2:
- Update patch subject to reflect clearing dev->v4l2 on init failure
- Rewrite commit description to explain the slab-use-after-free and replace the lockdep/GPF report with the KASAN report
- Drop setting dev->v4l2 to NULL in em28xx_v4l2_fini()
- Add comment explaining why dev->v4l2 is cleared in em28xx_v4l2_init()
- Add Cc: [email protected] tag
v1:
https://lore.kernel.org/all/[email protected]/T/
---
drivers/media/usb/em28xx/em28xx-video.c +19 -7
@@ -1396,6 +1396,9 @@ static int em28xx_vb2_setup(struct em28xx *dev)struct vb2_queue *q;struct em28xx_v4l2 *v4l2 = dev->v4l2; +mutex_init (&v4l2->vb_queue_lock); +mutex_init (&v4l2->vb_vbi_queue_lock); +/* Setup Videobuf2 for Video capture */ q = &v4l2->vb_vidq; q->type =V4L2_BUF_TYPE_VIDEO_CAPTURE ;
@@ -1405,11 +1408,15 @@ static int em28xx_vb2_setup(struct em28xx *dev) q->buf_struct_size =sizeof (struct em28xx_buffer ); q->ops = &em28xx_video_qops; q->mem_ops = &vb2_vmalloc_memops; + q->lock = &v4l2->vb_queue_lock; rc =vb2_queue_init (q);if (rc <0 )return rc; +if (!em28xx_vbi_supported (dev)) +return 0 ; +/* Setup Videobuf2 for VBI capture */ q = &v4l2->vb_vbiq; q->type =V4L2_BUF_TYPE_VBI_CAPTURE ;
@@ -1419,6 +1426,7 @@ static int em28xx_vb2_setup(struct em28xx *dev) q->buf_struct_size =sizeof (struct em28xx_buffer ); q->ops = &em28xx_vbi_qops; q->mem_ops = &vb2_vmalloc_memops; + q->lock = &v4l2->vb_vbi_queue_lock; rc =vb2_queue_init (q);if (rc <0 )
@@ -2948,12 +2956,14 @@ static int em28xx_v4l2_init(struct em28xx *dev)if (ret) goto unregister_dev; +/* initialize videobuf2 stuff */ + ret =em28xx_vb2_setup (dev); +if (ret) + goto unregister_dev; +/* allocate and fill video video_device struct */ em28xx_vdev_init (dev, &v4l2->vdev, &em28xx_video_template,"video" ); -mutex_init (&v4l2->vb_queue_lock); -mutex_init (&v4l2->vb_vbi_queue_lock); v4l2->vdev.queue = &v4l2->vb_vidq; - v4l2->vdev.queue->lock = &v4l2->vb_queue_lock; v4l2->vdev.device_caps =V4L2_CAP_READWRITE |V4L2_CAP_VIDEO_CAPTURE |V4L2_CAP_STREAMING ;if (dev->int_audio_type !=EM28XX_INT_AUDIO_NONE )
@@ -2998,7 +3008,6 @@ static int em28xx_v4l2_init(struct em28xx *dev) "vbi"); v4l2->vbi_dev.queue = &v4l2->vb_vbiq; - v4l2->vbi_dev.queue->lock = &v4l2->vb_vbi_queue_lock; v4l2->vbi_dev.device_caps =V4L2_CAP_STREAMING |V4L2_CAP_READWRITE |V4L2_CAP_VBI_CAPTURE ;if ((v4l2->vdev.device_caps &V4L2_CAP_TUNER ) ==0 )
@@ -3068,9 +3077,6 @@ static int em28xx_v4l2_init(struct em28xx *dev)/* Save some power by putting tuner to sleep */ v4l2_device_call_all (&v4l2->v4l2_dev,0 , tuner, standby); -/* initialize videobuf2 stuff */ -em28xx_vb2_setup (dev); -dev_info (&dev->intf->dev,"V4L2 extension successfully initialized\n" );
@@ -3097,6 +3103,12 @@ static int em28xx_v4l2_init(struct em28xx *dev)vb2_video_unregister_device (&v4l2->vdev); } +/* +* Clear dev->v4l2 to prevent em28xx_v4l2_open() from opening an +* unregistered node (failing with -ENODEV instead) and to prevent +* em28xx_v4l2_fini() from tearing down the instance a second time. +*/ + dev->v4l2 =NULL ;mutex_unlock (&dev->lock);/*
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].Thanks, the dev->v4l2 hunk, its comment, the subject, Fixes: and
Cc: stable are right. Two things still block this, plus two smaller ones. Please send a v3.
1) The KASAN report in the changelog is not a real report. It says "6.14.0-rc1", "v4l_id/1234", "QEMU Standard PC (Q35 ...)", and puts v4l2_device_put() in drivers/media/v4l2-core/v4l2-dev.c:1052. That function lives in v4l2-device.c, and the em28xx_free_v4l2() frame is missing. Do not synthesize or edit crash traces. Quote this real report from this bug, on linux-next 20a33c6b6cbe (2026/10/26), verbatim:
BUG: KASAN: slab-use-after-free in v4l2_open+0x397/0x3a0 drivers/media/v4l2-core/v4l2-dev.c:444 Read of size 4 at addr ffff888026878858 by task v4l_id/6347 Call Trace: v4l2_open+0x397/0x3a0 drivers/media/v4l2-core/v4l2-dev.c:444 chrdev_open+0x4d9/0x600 fs/char_dev.c:413 do_dentry_open+0x80e/0x1360 fs/open.c:1000 vfs_open_consume+0x4a/0x350 fs/open.c:1131 Allocated by task 31: em28xx_v4l2_init+0xff/0x31f0 drivers/media/usb/em28xx/em28xx-video.c:2751 em28xx_init_extension+0x120/0x1d0 drivers/media/usb/em28xx/em28xx-core.c:1245 Freed by task 973: kfree+0x1c5/0x650 mm/slub.c:6923 em28xx_free_v4l2+0x77/0xf0 drivers/media/usb/em28xx/em28xx-video.c:2337 v4l2_device_put+0x81/0xd0 drivers/media/v4l2-core/v4l2-device.c:56 em28xx_v4l2_fini+0x1f3/0x3e0 drivers/media/usb/em28xx/em28xx-video.c:2465 em28xx_close_extension+0xde/0x390 drivers/media/usb/em28xx/em28xx-core.c:1262 em28xx_usb_disconnect+0x16b/0x2f0 drivers/media/usb/em28xx/em28xx-cards.c:4489
2) Drop all em28xx_vb2_setup() hunks, and drop the "In addition, move em28xx_vb2_setup() ..." paragraph from the changelog. This was point 1) of my v1 review. Your job is based on 24a19b953e2a (v7.3-rc7), which predates the two mainline commits that already do this:
487344a63f6a ("media: em28xx: setup queues before registering V4L2 device") 65ed6e9e3fe2 ("media: usb: em28xx: check if vbi is supported before calling vb2_queue_init")
Both are in v7.4-rc1, and this patch has to apply on top of them. Against v7.4-rc1, v2 fails in 2 of 7 hunks. Worse, three other hunks still apply on top of the already-moved code and would call mutex_init() and em28xx_vb2_setup() twice. If you can, rebase onto v7.4-rc1 (6628e7d4c6b2). If your base cannot move: on 24a19b953e2a without those two commits, the reproducer still hits the videobuf2 GPF from 7c8bf63b7937 in the error path. That is expected, and it is not a reason to keep the hunks: that GPF is fixed by 487344a63f6a, not by this patch.
3) Wording: "subsequent calls to v4l2_open() ... succeed" is not quite it. New opens after the nodes are unregistered already fail in v4l2_open(). The problem is the open that is already in progress:
v4l_id is inside em28xx_v4l2_open(), blocked on dev->lock while em28xx_v4l2_init() holds it. When init fails and drops the lock, that open finds dev->v4l2 still set and succeeds on the unregistered node.
4) Put the tags in one trailer block, without the blank line between "Cc: [email protected]" and "Fixes:". Order: Fixes:, Cc:
stable, then the rest.
The v3 diff should be the single em28xx_v4l2_init() hunk from v2 (the comment plus "dev->v4l2 = NULL;"), and nothing else.
Quoted 188 lines · syzbot
In
em28xx_v4l2_init(), if an error occurs aftervideo_register_device()succeeds, the cleanup path atunregister_devunregisters the devices and puts thev4l2_devreference, but leavesdev->v4l2set. Becausedev->v4l2remains non-NULL, subsequent calls tov4l2_open()(such as fromv4l_idwhen opening the device node) succeed instead of returning-ENODEV. When the device is later disconnected,em28xx_close_extension()invokesem28xx_v4l2_fini(), which seesdev->v4l2is still set and callsv4l2_device_put()a second time. This prematurely drops the reference count to zero whilev4l_idstill holds the device, leading to a slab-use-after-free:BUG: KASAN: slab-use-after-free in v4l2_release+0xcd/0xe0 Read of size 8 at addr ffff888114efc000 by task v4l_id/1234 CPU: 1 UID: 0 PID: 1234 Comm: v4l_id Not tainted 6.14.0-rc1 #1 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0x68/0x90 print_report+0xd3/0x620 kasan_report+0xb9/0xf0 v4l2_release+0xcd/0xe0 __fput+0x3f6/0xb30 task_work_run+0x14d/0x240 do_exit+0xa38/0x2c00 do_group_exit+0xd4/0x2a0 __x64_sys_exit_group+0x3e/0x50 do_syscall_64+0xcb/0x250 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK> Allocated by task 6297: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 __kasan_kmalloc+0xaa/0xb0 em28xx_v4l2_init+0x9e/0x2df0 drivers/media/usb/em28xx/em28xx-video.c:2860 em28xx_init_extension+0x120/0x1d0 drivers/media/usb/em28xx/em28xx-core.c:1248 process_scheduled_works+0xc3d/0x1630 kernel/workqueue.c:3479 worker_thread+0x92d/0xe10 kernel/workqueue.c:3560 kthread+0x38b/0x480 kernel/kthread.c:436 ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245 Freed by task 6297: kasan_save_stack+0x33/0x60 kasan_save_track+0x14/0x30 kasan_save_free_info+0x2b/0x50 __kasan_slab_free+0x106/0x1a0 kfree+0xd4/0x340 v4l2_device_put+0xbc/0xf0 drivers/media/v4l2-core/v4l2-dev.c:1052 em28xx_v4l2_fini+0x120/0x180 drivers/media/usb/em28xx/em28xx-video.c:2445 em28xx_close_extension+0x120/0x1d0 drivers/media/usb/em28xx/em28xx-core.c:1270 em28xx_usb_disconnect+0x10a/0x230 drivers/media/usb/em28xx/em28xx-cards.c:4254 usb_unbind_interface+0x1eb/0x8e0 drivers/usb/core/driver.c:461 device_release_driver_internal+0x43b/0x610 drivers/base/dd.c:1297 bus_remove_device+0x2cc/0x3e0 drivers/base/bus.c:575 device_del+0x49f/0x910 drivers/base/core.c:3851 usb_disable_device+0x35b/0x7b0 drivers/usb/core/message.c:1418 usb_disconnect+0x2c6/0x8d0 drivers/usb/core/hub.c:2303Fix this by clearing
dev->v4l2underdev->lockin theunregister_deverror path ofem28xx_v4l2_init(). This ensuresem28xx_v4l2_open()fails with-ENODEVand preventsem28xx_v4l2_fini()from tearing down the instance a second time upon disconnect.In addition, move
em28xx_vb2_setup()beforevideo_register_device()and check its return value so that videobuf2 queues are initialized prior to device registration and can be safely cleaned up if an error occurs. Inem28xx_vb2_setup(), initializevb_queue_lockandvb_vbi_queue_lockand assignq->lockbefore callingvb2_queue_init(), and make VBI queue initialization conditional onem28xx_vbi_supported().Fixes 288254383674 ("media: em28xx: use v4l2_device release callback")Assisted-by Gemini:gemini-4-argon Gemini:gemini-3.8-flash syzbotReported-by [email protected]To: <[email protected]>
To: "Mauro Carvalho Chehab" <[email protected]>
To: "Hans Verkuil" <[email protected]>
Cc: <[email protected]>---
v2:
- Update patch subject to reflect clearingdev->v4l2on init failure
- Rewrite commit description to explain the slab-use-after-free and replace the lockdep/GPF report with the KASAN report
- Drop settingdev->v4l2to NULL inem28xx_v4l2_fini()
- Add comment explaining whydev->v4l2is cleared inem28xx_v4l2_init()
- Add Cc: [email protected] tagv1:
https://lore.kernel.org/all/[email protected]/T/
---drivers/media/usb/em28xx/em28xx-video.c +19
-7@@ -1396,6 +1396,9 @@ static int em28xx_vb2_setup(struct em28xx *dev)struct vb2_queue *q;struct em28xx_v4l2 *v4l2 = dev->v4l2; +mutex_init (&v4l2->vb_queue_lock); +mutex_init (&v4l2->vb_vbi_queue_lock); +/* Setup Videobuf2 for Video capture */ q = &v4l2->vb_vidq; q->type =V4L2_BUF_TYPE_VIDEO_CAPTURE ;@@ -1405,11 +1408,15 @@ static int em28xx_vb2_setup(struct em28xx *dev) q->buf_struct_size =sizeof (struct em28xx_buffer ); q->ops = &em28xx_video_qops; q->mem_ops = &vb2_vmalloc_memops; + q->lock = &v4l2->vb_queue_lock; rc =vb2_queue_init (q);if (rc <0 )return rc; +if (!em28xx_vbi_supported (dev)) +return 0 ; +/* Setup Videobuf2 for VBI capture */ q = &v4l2->vb_vbiq; q->type =V4L2_BUF_TYPE_VBI_CAPTURE ;@@ -1419,6 +1426,7 @@ static int em28xx_vb2_setup(struct em28xx *dev) q->buf_struct_size =sizeof (struct em28xx_buffer ); q->ops = &em28xx_vbi_qops; q->mem_ops = &vb2_vmalloc_memops; + q->lock = &v4l2->vb_vbi_queue_lock; rc =vb2_queue_init (q);if (rc <0 )@@ -2948,12 +2956,14 @@ static int em28xx_v4l2_init(struct em28xx *dev)if (ret) goto unregister_dev; +/* initialize videobuf2 stuff */ + ret =em28xx_vb2_setup (dev); +if (ret) + goto unregister_dev; +/* allocate and fill video video_device struct */ em28xx_vdev_init (dev, &v4l2->vdev, &em28xx_video_template,"video" ); -mutex_init (&v4l2->vb_queue_lock); -mutex_init (&v4l2->vb_vbi_queue_lock); v4l2->vdev.queue = &v4l2->vb_vidq; - v4l2->vdev.queue->lock = &v4l2->vb_queue_lock; v4l2->vdev.device_caps =V4L2_CAP_READWRITE |V4L2_CAP_VIDEO_CAPTURE |V4L2_CAP_STREAMING ;if (dev->int_audio_type !=EM28XX_INT_AUDIO_NONE )@@ -2998,7 +3008,6 @@ static int em28xx_v4l2_init(struct em28xx *dev) "vbi"); v4l2->vbi_dev.queue = &v4l2->vb_vbiq; - v4l2->vbi_dev.queue->lock = &v4l2->vb_vbi_queue_lock; v4l2->vbi_dev.device_caps =V4L2_CAP_STREAMING |V4L2_CAP_READWRITE |V4L2_CAP_VBI_CAPTURE ;if ((v4l2->vdev.device_caps &V4L2_CAP_TUNER ) ==0 )@@ -3068,9 +3077,6 @@ static int em28xx_v4l2_init(struct em28xx *dev)/* Save some power by putting tuner to sleep */ v4l2_device_call_all (&v4l2->v4l2_dev,0 , tuner, standby); -/* initialize videobuf2 stuff */ -em28xx_vb2_setup (dev); -dev_info (&dev->intf->dev,"V4L2 extension successfully initialized\n" );@@ -3097,6 +3103,12 @@ static int em28xx_v4l2_init(struct em28xx *dev)vb2_video_unregister_device (&v4l2->vdev); } +/* +* Clear dev->v4l2 to prevent em28xx_v4l2_open() from opening an +* unregistered node (failing with -ENODEV instead) and to prevent +* em28xx_v4l2_fini() from tearing down the instance a second time. +*/ + dev->v4l2 =NULL ;mutex_unlock (&dev->lock);/* base-commit:
24a19b953e2a3d0ea5a8c4951a0ab8f32720c907