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=1796362558; b=tsfm84Ochv9yADWYcb/s+woKGS5+BXegaId+vmyWwHIp+nuEdqKePKn0SMelqC1QTPz3 o+EhaPkNyjPrCjVnuIZ3WbtbhofuGN36e1H0xgHIuEahgLfEH6YmXeWC9KurRw4r0F8pX DH4bBm0+p0z7wAbIR4oh6YQ/awWOS4iUVvu//92he6QzHmeUh/g3DTmCJXgkmqoloBWZZ 5t6RQjo6t25pjCgfSBg8MEdORH/dWvCPewfI0WYAl2o5cdhbsNdhzOAHVSzKPBjH0S73p eVCbi5A30l3CAgPlI6U4d0Ib6jTNQNyarsUPb3yaqYSe+ASq3R6snPrsOK47Bf49baw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796362558; h=Received:DKIM-Signature:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding:Date; bh=vUk++fkRp6p/ZaWkZmU/IzpagzmrTVojEx+2a6NdQLg=; b=sTMWMSl5VBhrrA12bE+LZtl78MFXmpFKNASb1pWGy92Mm2thzFVI0y2w9an5N6LSXEFb Xm7omiou9d5wxx0r8cb+4TDH5ERNEv6rsPUFqLzmusHmecTV9UtgFZn4mDQrY2ATDo5UO WTVFXChRjQkElU+cQdumCQcjnDjupeyMZndKym/Sxi/k3ULmsogvoTvrZUeuSWr0q8IUc OlJIEtv2ERb+lpwaJw1hz9jZfPDSCmkNDnTIDJ/TgrL/AswHDQqdd3xT7Il48+ODAykjV qqOhOorB2FQjOMR/e/tuiHE/GpmrmTkosgQ6wL8CG6h2sm3Q+fi/2jqMgmYAK962d/Q== 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=HlA48lUa; 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 D4759606A32C for ; Fri, 4 Dec 2026 05:35:58 +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=HlA48lUa Received: by smtp.kernel.org (Postfix) with UTF8SMTPSA id 5C1131F000C2; Fri, 4 Dec 2026 05:35:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796362558; bh=vUk++fkRp6p/ZaWkZmU/IzpagzmrTVojEx+2a6NdQLg=; h=From:To:Cc:Subject:Date; b=HlA48lUax7lC0BM9Er4ocHm9lp5haNf5O+8Y8JeIguV8Eh9Uokx0GXaToWDzUm/Tc CUdK+k/Q8qfc320Og/6K/nvZMs0qhkDBztLsA+UjPO3vPBXS588GkFYLhMNz55Fvic x3dfi5zCdS/4lq/uwTp2/SNGuzma+obuN+l/1Sd4RlvIy21O814uSa7mruzk63s1fP M9DFHSeS0rSSAsQwZ54Ldb7FCZF2ScWOTpGUSQDJy0eppG2xQcWO+VByOyr6Fm9hdp vhFpupK62zHNYDgoQU3sLiljdVJGRnM1zmbWEmRBsxuM02ZlkHLmbVJ/5CByNOiDvB r3XItaUIiaBLA== From: "syzbot" To: syzkaller-upstream-moderation@googlegroups.com Cc: syzbot@lists.linux.dev Subject: [PATCH RFC] media: em28xx: fix v4l2_device use-after-free on init failure Message-ID: <7e831751-8791-4609-923b-bc1772083061@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: Fri, 4 Dec 2026 05:35:58 +0000 (UTC) When em28xx_v4l2_init() fails after registering video device nodes, it unregisters them and drops its initial v4l2_device reference via v4l2_device_put() without clearing dev->v4l2, relying on em28xx_free_v4l2() to clear it once the refcount reaches zero. If userspace opens a video node before it is unregistered, v4l2_open() holds a reference that keeps v4l2_device alive so em28xx_free_v4l2() does not run, and em28xx_v4l2_open() succeeds once it acquires dev->lock because it does not check video_is_registered(). On USB disconnect, em28xx_v4l2_fini() sees dev->v4l2 still non-NULL and calls v4l2_device_put() a second time, freeing struct em28xx_v4l2 while the file handle is still open and triggering a KASAN use-after-free in v4l2_ioctl() when reading vdev->fops. Fix this by clearing dev->v4l2 under dev->lock after unregistering the video devices in both em28xx_v4l2_init() and em28xx_v4l2_fini(), reading and checking dev->v4l2 under dev->lock in em28xx_v4l2_fini(), and removing the unlocked dev->v4l2 assignment from em28xx_free_v4l2(). Also check video_is_registered() under dev->lock in em28xx_v4l2_open() to reject opens on already-unregistered nodes with -ENODEV, and resolve v4l2 from vdev->v4l2_dev in em28xx_v4l2_close() instead of reading dev->v4l2, deferring interface_to_usbdev() until after the dev->disconnected check. In addition, split media device cleanup from unregistration so dev->media_dev stays allocated until em28xx_free_device() runs. When an open video file handle outlives disconnect, v4l2_device_release() and em28xx_free_v4l2() still access v4l2_dev->mdev after em28xx_release_resources() has unregistered the media device. Keep media_device_unregister() in em28xx_unregister_media_device(), move media_device_cleanup() and freeing into a new em28xx_cleanup_media_device() helper called from em28xx_free_device() and the em28xx_init_dev() error path, and initialize dev->media_dev to NULL in em28xx_media_device_init(). 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+b2391895514ed9ef4a8e@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=b2391895514ed9ef4a8e Link: https://syzkaller.appspot.com/ai_job?id=68bb1a13-9f5b-4a81-8574-aa23f4847291 To: To: "Mauro Carvalho Chehab" To: "Hans Verkuil" --- diff --git a/drivers/media/usb/em28xx/em28xx-cards.c b/drivers/media/usb/em28xx/em28xx-cards.c index 1d4f30cba..a1f74ed8f 100644 --- a/drivers/media/usb/em28xx/em28xx-cards.c +++ b/drivers/media/usb/em28xx/em28xx-cards.c @@ -3754,6 +3754,8 @@ static int em28xx_media_device_init(struct em28xx *dev, #ifdef CONFIG_MEDIA_CONTROLLER struct media_device *mdev; + dev->media_dev = NULL; + mdev = kzalloc_obj(*mdev); if (!mdev) return -ENOMEM; @@ -3773,8 +3775,15 @@ static int em28xx_media_device_init(struct em28xx *dev, static void em28xx_unregister_media_device(struct em28xx *dev) { #ifdef CONFIG_MEDIA_CONTROLLER - if (dev->media_dev) { + if (dev->media_dev) media_device_unregister(dev->media_dev); +#endif +} + +static void em28xx_cleanup_media_device(struct em28xx *dev) +{ +#ifdef CONFIG_MEDIA_CONTROLLER + if (dev->media_dev) { media_device_cleanup(dev->media_dev); kfree(dev->media_dev); dev->media_dev = NULL; @@ -3826,6 +3835,8 @@ void em28xx_free_device(struct kref *ref) if (!dev->disconnected) em28xx_release_resources(dev); + em28xx_cleanup_media_device(dev); + if (dev->ts == PRIMARY_TS) kfree(dev->alt_max_pkt_size_isoc); @@ -3996,7 +4007,7 @@ static int em28xx_init_dev(struct em28xx *dev, struct usb_device *udev, err_unreg_i2c: em28xx_i2c_unregister(dev, 0); err_deinit_media: - em28xx_unregister_media_device(dev); + em28xx_cleanup_media_device(dev); return retval; } diff --git a/drivers/media/usb/em28xx/em28xx-video.c b/drivers/media/usb/em28xx/em28xx-video.c index 84424284d..a1a9e1ddd 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); } @@ -2367,7 +2366,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 +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,7 @@ static int em28xx_v4l2_fini(struct em28xx *dev) vb2_video_unregister_device(&v4l2->vdev); } + dev->v4l2 = NULL; mutex_unlock(&dev->lock); /* @@ -2502,8 +2505,9 @@ 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); + struct usb_device *udev; int err; bool last_user; @@ -2516,6 +2520,8 @@ static int em28xx_v4l2_close(struct file *filp) if (dev->disconnected) 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 +3135,7 @@ 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.