Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796331568; b=SlGm86ZL8SwsbSfRbNiXLcuyWaOG7hCwmS/YpFVZMCkthhTDR3b4xkCAxGLZUmWJpKcX UHEKeo2KbjlNzs0JjiO+pgs+n8itzGiKkJEmBj9cIkSZl8PD1IbxBj1HguSbRqIUVX9pu brpQM8dmpLqVWzvPwYp1C+8AYiDJQOYy9xyK995AvTAXdSzHvtmJP0Yp/W9lJwDi6fus2 fAWWDjwZLXSxdzEc1LJJXEPzPVg5cHaTHWQr07HEyU1c+hSTpC56g5xdp1G9XLvO6wIQa f7DdKn1J3sbZ0TCGDsPlkzW42PGRpy+QOK5oq+nhqMmkq3EW9wLPxIamYlvYixaHYTQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796331568; h=Received:DKIM-Signature:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding:Date; bh=8ZG5/ExmbKWJX7aPVV2mAkZMYm2VVNkrDfB57gviuNg=; b=2DWaN3VcMAtOgoN1Cc7/TIqi53FvCt5Jhfaz91zBB1+vQB1uJKfQw9bgUIfK/qLvNEsR 71e6oLIbODARvE7bdr6QbPSKGF++S42PLf+iRnnxm+CyTId7NDONWucxrdUgqh0CSjzFu x6jO3KFZUKxYRT8zuFJAZ0utjjTiBthtDufXaT2waPufKrW/AZ6fMcx97aPnvj81b7Urv Ui44/zQicEZ20CJipOwRFoOK4LCl0avf1uZcCVu6PZlBrG28ZbnbWsGUh9FJs7gGcGIJ9 S0PrkcsOe76HwtPGxZdz2GYYZCuhQbGAljwg7fhJlDutMfR3nDVywY1sqmpUEdj1NAw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=XUiJN2Qy; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with UTF8SMTPS id 97069606A322 for ; Thu, 3 Dec 2026 20:59:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=XUiJN2Qy Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 63EF91F000C2; Thu, 3 Dec 2026 20:59:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796331568; bh=8ZG5/ExmbKWJX7aPVV2mAkZMYm2VVNkrDfB57gviuNg=; h=From:To:Cc:Subject:Date; b=XUiJN2QyjhaejSohqKUdNoqdIbYdTOCRSC1p/UNK7K/1+ni3MxjdkqnAGH/bLEzMP BqIdhnvzdFI+VKwFUNkdRHgBS+roHLW6SxAsqwuqqnWKjh3VVCfobczZPm/oARskyn TUlOWfa72lxckRgUooLyXietQ+qwTFD4aAgvkl6bRcW+ZY+If2YZgIz8/Fqw8I+81W xIVxIN/GRhCA+KezgKQ1avBQE7qp3t4c4meGgPdgmMKayQ/3w897o/ssX6f6JX0neT jv2nsIu7VxuH/FYz4GZP6kIke8t4xNdomWYGxtOuZW9WzwxqVfiVIdEHJ4y6dpi3wl Fr56OKDC6G4Hg== From: "syzbot" To: syzkaller-upstream-moderation@googlegroups.com Cc: syzbot@lists.linux.dev Subject: [PATCH RFC] media: em28xx: clear dev->v4l2 under lock to fix use-after-free Message-ID: <5156f0a1-16b8-44fe-ac5e-3ff801068216@mail.kernel.org> X-Mailing-List: syzbot@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Date: Thu, 3 Dec 2026 20:59:28 +0000 (UTC) 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.