In 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. While new opens after node unregistration already fail in v4l2_open(), an open already in progress (such as v4l_id inside em28xx_v4l2_open(), blocked on dev->lock while em28xx_v4l2_init() holds it) sees dev->v4l2 still set after init fails and releases dev->lock, succeeding on the unregistered node. 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 in v4l2_open(). 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. Cc: stable@vger.kernel.org Fixes: 288254383674 ("media: em28xx: use v4l2_device release callback") Assisted-by: Gemini:gemini-4-hydrogen Gemini:gemini-4-carbon Gemini:gemini-3.8-flash syzbot Reported-by: syzbot+1a7507a194fff09e5c44@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=1a7507a194fff09e5c44 Link: https://syzkaller.appspot.com/ai_job?id=3d27fc97-4d17-42f0-a8c9-6f94ce0335b2 To: To: "Mauro Carvalho Chehab" To: "Hans Verkuil" Cc: --- v3: - Moved em28xx_vb2_setup() to just before video_register_device() with a dev_err() message on failure, keeping queue mutex initialization and q->lock assignment in em28xx_v4l2_init(). - Reworded the commit description to clarify that the race affects an open already in progress in em28xx_v4l2_open(), removed the synthesized KASAN report, and dropped the em28xx_vb2_setup() paragraph. 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: stable@vger.kernel.org tag https://lore.kernel.org/all/46d5ee1f-d8ae-4555-9edd-6afa90861d4e@mail.kernel.org/T/ v1: https://lore.kernel.org/all/2fdb5af1-fad9-47fe-93dc-d1400f44b60b@mail.kernel.org/T/ --- diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c index 79af15476..200826a01 100644 --- a/drivers/media/usb/em28xx/em28xx-video.c +++ b/drivers/media/usb/em28xx/em28xx-video.c @@ -1410,6 +1410,9 @@ static int em28xx_vb2_setup(struct em28xx *dev) 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; @@ -2983,6 +2986,14 @@ static int em28xx_v4l2_init(struct em28xx *dev) if (dev->chip_id == CHIP_ID_EM2828X || dev->board.decoder == EM28XX_BUILTIN) v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_ENUM_FRAMESIZES); + /* initialize videobuf2 stuff */ + ret = em28xx_vb2_setup(dev); + if (ret) { + dev_err(&dev->intf->dev, + "unable to setup videobuf queues (error=%i).\n", ret); + goto unregister_dev; + } + /* register v4l2 video video_device */ ret = video_register_device(&v4l2->vdev, VFL_TYPE_VIDEO, video_nr[dev->devno]); @@ -3068,9 +3079,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 +3105,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 -- 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 syzkaller@googlegroups.com.