In em28xx, dev->v4l2 was cleared asynchronously inside the v4l2_device release callback em28xx_free_v4l2() without holding dev->lock, and em28xx_v4l2_fini() read dev->v4l2 before acquiring dev->lock. If em28xx_v4l2_init() failed after registering a video_device while a concurrent open() held a reference on vdev->dev, dropping the initial v4l2_device reference in the unregister_dev error path did not trigger em28xx_free_v4l2(), leaving dev->v4l2 set. The blocked em28xx_v4l2_open() then succeeded on the unregistered device, and a subsequent em28xx_v4l2_fini() on disconnect called v4l2_device_put() a second time, freeing struct em28xx_v4l2 prematurely while vdev and its media entity were still alive and triggering a KASAN slab-use-after-free in media_device_unregister() or em28xx_v4l2_fini() as well as a refcount_t underflow. Clear dev->v4l2 synchronously under dev->lock after unregistering the video devices in both em28xx_v4l2_fini() and the unregister_dev error path of em28xx_v4l2_init(), and remove the unlocked assignment from em28xx_free_v4l2(). Also move the read and NULL check of dev->v4l2 in em28xx_v4l2_fini() inside dev->lock so teardown cannot run twice or race with asynchronous release. Since dev->v4l2 can now become NULL before open file handles are closed, update em28xx_v4l2_open() to fail with -ENODEV when !video_is_registered(vdev), and update em28xx_v4l2_close(), queue_setup(), and vbi_queue_setup() to obtain struct em28xx_v4l2 via container_of() rather than dev->v4l2 while checking video_is_registered() where needed. In em28xx_v4l2_close(), also guard _vb2_fop_release() on vdev->queue (falling back to v4l2_fh_release() for devices without a queue such as radio) and skip tuner standby and usb_set_interface() when the device is disconnected or unregistered. 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+16062f26c6480975e5ed@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=16062f26c6480975e5ed Link: https://syzkaller.appspot.com/ai_job?id=355b977e-d642-4224-921d-692a78f75fa1 To: To: "Mauro Carvalho Chehab" To: "Hans Verkuil" --- diff --git a/drivers/media/usb/em28xx/em28xx-vbi.c b/drivers/media/usb/em28xx/em28xx-vbi.c index 8c5d95181..03a5ab7c9 100644 --- a/drivers/media/usb/em28xx/em28xx-vbi.c +++ b/drivers/media/usb/em28xx/em28xx-vbi.c @@ -22,9 +22,14 @@ static int vbi_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[]) { - struct em28xx *dev = vb2_get_drv_priv(vq); - struct em28xx_v4l2 *v4l2 = dev->v4l2; - unsigned long size = v4l2->vbi_width * v4l2->vbi_height * 2; + struct em28xx_v4l2 *v4l2 = + container_of(vq, struct em28xx_v4l2, vb_vbiq); + unsigned long size; + + if (!video_is_registered(&v4l2->vbi_dev)) + return -ENODEV; + + size = v4l2->vbi_width * v4l2->vbi_height * 2; if (*nbuffers < 2) *nbuffers = 2; diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c index 84424284d..f777df53a 100644 --- a/drivers/media/usb/em28xx/em28xx-video.c +++ b/drivers/media/usb/em28xx/em28xx-video.c @@ -1195,9 +1195,14 @@ static int queue_setup(struct vb2_queue *vq, unsigned int sizes[], struct device *alloc_devs[]) { struct em28xx *dev = vb2_get_drv_priv(vq); - struct em28xx_v4l2 *v4l2 = dev->v4l2; - unsigned long size = - (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3; + struct em28xx_v4l2 *v4l2 = + container_of(vq, struct em28xx_v4l2, vb_vidq); + unsigned long size; + + if (!video_is_registered(&v4l2->vdev)) + return -ENODEV; + + size = (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3; if (*nplanes) return sizes[0] < size ? -EINVAL : 0; @@ -2333,7 +2338,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); } @@ -2367,7 +2371,7 @@ static int em28xx_v4l2_open(struct file *filp) return -ERESTARTSYS; v4l2 = dev->v4l2; - if (!v4l2) { + if (!v4l2 || !video_is_registered(vdev)) { mutex_unlock(&dev->lock); return -ENODEV; } @@ -2414,7 +2418,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 +2430,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 +2462,8 @@ static int em28xx_v4l2_fini(struct em28xx *dev) vb2_video_unregister_device(&v4l2->vdev); } + dev->v4l2 = NULL; + mutex_unlock(&dev->lock); /* @@ -2502,20 +2511,27 @@ 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 usb_device *udev = interface_to_usbdev(dev->intf); + struct em28xx_v4l2 *v4l2 = + container_of(vdev->v4l2_dev, struct em28xx_v4l2, v4l2_dev); int err; bool last_user; mutex_lock(&dev->lock); last_user = v4l2_fh_is_singular_file(filp); - _vb2_fop_release(filp, vdev->queue->lock); + if (vdev->queue) + _vb2_fop_release(filp, vdev->queue->lock); + else + v4l2_fh_release(filp); if (last_user) { + struct usb_device *udev; + /* No sense to try to write to the device */ - if (dev->disconnected) + if (dev->disconnected || !video_is_registered(vdev)) goto exit; + udev = interface_to_usbdev(dev->intf); + /* Save some power by putting tuner to sleep */ v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, standby); @@ -3129,6 +3145,8 @@ static int em28xx_v4l2_init(struct em28xx *dev) vb2_video_unregister_device(&v4l2->vdev); } + dev->v4l2 = NULL; + mutex_unlock(&dev->lock); /* base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 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.