Skip to contentSkip to conversation

media: em28xx: clear dev->v4l2 under lock to fix use-after-free

RFC PATCH syzbot 1 participant 1 message Dec 3

Subsystems (2)
lore
Share & tools
lore

syzbot

author syzbot
#0 lore raw

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

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

---

drivers/media/usb/em28xx/em28xx-vbi.c +8 -3
index 8c5d95181..03a5ab7c9 100644
@@ -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;
drivers/media/usb/em28xx/em28xx-video.c +31 -13

106 lines not on this page · raw message

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