Skip to contentSkip to conversation

media: em28xx: fix v4l2_device use-after-free on init failure

RFC PATCH syzbot 1 participant 1 message Dec 4

Subsystems (2)
lore
Share & tools
lore

syzbot

author syzbot
#0 lore raw

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 [email protected]

To: <[email protected]>
To: "Mauro Carvalho Chehab" <[email protected]>
To: "Hans Verkuil" <[email protected]>

---

drivers/media/usb/em28xx/em28xx-cards.c +13 -2
index 1d4f30cba..a1f74ed8f 100644
@@ -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;
 }
 
drivers/media/usb/em28xx/em28xx-video.c +15 -8
index 84424284d..a1a9e1ddd 100644
@@ -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

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].