media: em28xx: clear dev->v4l2 when v4l2 init fails
Subsystems (2)
syzbot
syzbotIn em28xx_v4l2_init(), if an error occurs after video_register_device() succeeds, the cleanup path at unregister_dev unregisters the devices and puts the v4l2_dev reference, but leaves dev->v4l2 set. While new opens after node unregistration already fail in v4l2_open(), an open already in progress (such as v4l_id inside em28xx_v4l2_open(), blocked on dev->lock while em28xx_v4l2_init() holds it) sees dev->v4l2 still set after init fails and releases dev->lock, succeeding on the unregistered node. When the device is later disconnected, em28xx_close_extension() invokes em28xx_v4l2_fini(), which sees dev->v4l2 is still set and calls v4l2_device_put() a second time. This prematurely drops the reference count to zero while v4l_id still holds the device, leading to a slab-use-after-free in v4l2_open().
Fix this by clearing dev->v4l2 under dev->lock in the unregister_dev error path of em28xx_v4l2_init(). This ensures em28xx_v4l2_open() fails with -ENODEV and prevents em28xx_v4l2_fini() from tearing down the instance a second time upon disconnect.
To: <[email protected]>
To: "Mauro Carvalho Chehab" <[email protected]>
To: "Hans Verkuil" <[email protected]>
Cc: <[email protected]>
---
v3:
- Moved em28xx_vb2_setup() to just before video_register_device() with a dev_err() message on failure, keeping queue mutex initialization and q->lock assignment in em28xx_v4l2_init().
- Reworded the commit description to clarify that the race affects an open already in progress in em28xx_v4l2_open(), removed the synthesized KASAN report, and dropped the em28xx_vb2_setup() paragraph.
v2:
- Update patch subject to reflect clearing dev->v4l2 on init failure
- Rewrite commit description to explain the slab-use-after-free and replace the lockdep/GPF report with the KASAN report
- Drop setting dev->v4l2 to NULL in em28xx_v4l2_fini()
- Add comment explaining why dev->v4l2 is cleared in em28xx_v4l2_init()
- Add Cc: [email protected] tag
https://lore.kernel.org/all/[email protected]/T/
v1:
https://lore.kernel.org/all/[email protected]/T/
---
drivers/media/usb/em28xx/em28xx-video.c +17 -3
@@ -1410,6 +1410,9 @@ static int em28xx_vb2_setup(struct em28xx *dev)if (rc <0 )return rc; +if (!em28xx_vbi_supported (dev)) +return 0 ; +/* Setup Videobuf2 for VBI capture */ q = &v4l2->vb_vbiq; q->type =V4L2_BUF_TYPE_VBI_CAPTURE ;
@@ -2983,6 +2986,14 @@ static int em28xx_v4l2_init(struct em28xx *dev)if (dev->chip_id ==CHIP_ID_EM2828X || dev->board.decoder ==EM28XX_BUILTIN )v4l2_disable_ioctl (&v4l2->vdev,VIDIOC_ENUM_FRAMESIZES ); +/* initialize videobuf2 stuff */ + ret =em28xx_vb2_setup (dev); +if (ret) { +dev_err (&dev->intf->dev, +"unable to setup videobuf queues (error=%i).\n" , ret); + goto unregister_dev; + } +/* register v4l2 video video_device */ ret =video_register_device (&v4l2->vdev,VFL_TYPE_VIDEO , video_nr[dev->devno]);
@@ -3068,9 +3079,6 @@ static int em28xx_v4l2_init(struct em28xx *dev)/* Save some power by putting tuner to sleep */ v4l2_device_call_all (&v4l2->v4l2_dev,0 , tuner, standby); -/* initialize videobuf2 stuff */ -em28xx_vb2_setup (dev); -dev_info (&dev->intf->dev,"V4L2 extension successfully initialized\n" );
@@ -3097,6 +3105,12 @@ static int em28xx_v4l2_init(struct em28xx *dev)vb2_video_unregister_device (&v4l2->vdev); } +/* +* Clear dev->v4l2 to prevent em28xx_v4l2_open() from opening an +* unregistered node (failing with -ENODEV instead) and to prevent +* em28xx_v4l2_fini() from tearing down the instance a second time. +*/ + dev->v4l2 =NULL ;mutex_unlock (&dev->lock);/*
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
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].