Re: KASAN: use-after-free Read in v4l2_fh_open

From: Hillf Danton
Date: Sat Mar 02 2024 - 20:44:21 EST


On Mon, 15 Feb 2021 07:18:22 -0800
> syzbot found the following issue on:
>
> HEAD commit: 291009f6 Merge tag 'pm-5.11-rc8' of git://git.kernel.org/p..
> git tree: upstream
> C reproducer: https://syzkaller.appspot.com/x/repro.c?x=17179dd4d00000

#syz test https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master

--- x/drivers/media/usb/em28xx/em28xx-video.c
+++ y/drivers/media/usb/em28xx/em28xx-video.c
@@ -2152,6 +2152,10 @@ static int em28xx_v4l2_open(struct file
if (mutex_lock_interruptible(&dev->lock))
return -ERESTARTSYS;

+ if (!dev->v4l2) {
+ mutex_unlock(&dev->lock);
+ return -ENODEV;
+ }
ret = v4l2_fh_open(filp);
if (ret) {
dev_err(&dev->intf->dev,
@@ -2161,6 +2165,7 @@ static int em28xx_v4l2_open(struct file
return ret;
}

+ v4l2 = dev->v4l2;
if (v4l2->users == 0) {
em28xx_set_mode(dev, EM28XX_ANALOG_MODE);

@@ -2376,10 +2381,17 @@ static const struct v4l2_ioctl_ops video
#endif
};

+static void em28xx_video_device_release(struct video_device *vd)
+{
+ struct em28xx_v4l2 *v4l2 = container_of(vd, struct em28xx_v4l2, vdev);
+
+ kref_put(&v4l2->ref, em28xx_free_v4l2);
+}
+
static const struct video_device em28xx_video_template = {
.fops = &em28xx_v4l_fops,
.ioctl_ops = &video_ioctl_ops,
- .release = video_device_release_empty,
+ .release = em28xx_video_device_release,
.tvnorms = V4L2_STD_ALL,
};

@@ -2788,6 +2800,7 @@ static int em28xx_v4l2_init(struct em28x
"unable to register video device (error=%i).\n", ret);
goto unregister_dev;
}
+ kref_get(&v4l2->ref); /* put by video_unregister_device() */

/* Allocate and fill vbi video_device struct */
if (em28xx_vbi_supported(dev) == 1) {
--