Root Cause
The slab-use-after-free is caused by a double v4l2_device_put(&v4l2->v4l2_dev) of the initial v4l2_device reference across the error-cleanup path of em28xx_v4l2_init() and the teardown path em28xx_v4l2_fini() in drivers/media/usb/em28xx/em28xx-video.c, when a registered video device node is still open in userspace.
Step-by-step Mechanism
-
Allocation and partial registration (em28xx_v4l2_init()):
em28xx_usb_probe() (drivers/media/usb/em28xx/em28xx-cards.c) schedules request_module_async() -> em28xx_init_extension() -> em28xx_v4l2_init().
em28xx_v4l2_init() allocates struct em28xx_v4l2 *v4l2 (8192 bytes, kmalloc-8k) at drivers/media/usb/em28xx/em28xx-video.c:2751 and assigns dev->v4l2 = v4l2 (line 2757).
v4l2_device_register(&dev->intf->dev, &v4l2->v4l2_dev) initializes v4l2->v4l2_dev.ref to 1, and v4l2->v4l2_dev.release is set to em28xx_free_v4l2 (line 2778).
- For a non-webcam board such as
0x1b80:0xe309 (EM2860_BOARD_EASYCAP, which expects .decoder = EM28XX_SAA711X), v4l2_i2c_new_subdev(..., "saa7115_auto", ...) fails to probe an actual I2C decoder on the virtual USB device, so no MEDIA_ENT_F_ATV_DECODER entity is registered.
em28xx_v4l2_init() nevertheless registers v4l2->vdev via video_register_device(&v4l2->vdev, VFL_TYPE_VIDEO, ...) (line 3022). Inside __video_register_device() (drivers/media/v4l2-core/v4l2-dev.c:1105), v4l2_device_get(vdev->v4l2_dev) increments v4l2->v4l2_dev.ref to 2, and the /dev/video* character device becomes visible to userspace.
-
Userspace opens /dev/video* before em28xx_v4l2_init() fails:
udev (v4l_id) opens /dev/video* (v4l2_open() in drivers/media/v4l2-core/v4l2-dev.c:425), calling video_get(vdev) and bumping vdev->dev kref to 2.
em28xx_v4l2_init() then calls v4l2_mc_create_media_graph(dev->media_dev) (line 3085), which fails with -EINVAL (drivers/media/v4l2-core/v4l2-mc.c:100-103) because !is_webcam && !decoder.
em28xx_v4l2_init() jumps to unregister_dev: (line 3112):
vb2_video_unregister_device(&v4l2->vdev) (line 3129) unregisters v4l2->vdev, dropping vdev->dev kref from 2 to 1 (since v4l_id still holds the file open). Thus v4l2_device_release(&vdev->dev) has not run yet, and the reference on v4l2->v4l2_dev held by vdev remains active.
v4l2_device_put(&v4l2->v4l2_dev) (line 3139) drops the initial reference, decrementing v4l2->v4l2_dev.ref from 2 to 1.
- Because
v4l2->v4l2_dev.ref is still 1, em28xx_free_v4l2() is not invoked, and dev->v4l2 remains non-NULL (it is only cleared inside em28xx_free_v4l2() at line 2336).
em28xx_v4l2_init() returns -EINVAL to em28xx_init_extension() (drivers/media/usb/em28xx/em28xx-core.c:1245), which ignores the return value.
-
Double v4l2_device_put() on USB disconnect (em28xx_v4l2_fini()):
- When the USB device disconnects,
em28xx_usb_disconnect() -> em28xx_close_extension() -> em28xx_v4l2_fini(dev) runs.
em28xx_v4l2_fini() checks if (!v4l2) return 0; (drivers/media/usb/em28xx/em28xx-video.c:2429). Because dev->v4l2 is still non-NULL, it proceeds as though V4L2 initialization had succeeded.
video_is_registered(&v4l2->vdev) is already false, so vb2_video_unregister_device(&v4l2->vdev) does nothing.
- Line 2465 executes
v4l2_device_put(&v4l2->v4l2_dev) a second time for the initial reference, dropping v4l2->v4l2_dev.ref from 1 to 0.
- This invokes
v4l2_device_release() (drivers/media/v4l2-core/v4l2-device.c:51) -> em28xx_free_v4l2() (drivers/media/usb/em28xx/em28xx-video.c:2337), which calls kfree(v4l2) while v4l_id still holds an open file descriptor referencing &v4l2->vdev.
-
Use-after-free on close() (v4l2_release()):
- When
v4l_id closes the /dev/video* fd, v4l2_release() -> video_put(vdev) -> put_device(&vdev->dev) drops the last reference on vdev->dev and invokes v4l2_device_release(&vdev->dev) (drivers/media/v4l2-core/v4l2-dev.c:177).
- At
drivers/media/v4l2-core/v4l2-dev.c:208, media_device_unregister_entity(&vdev->entity) -> __media_device_unregister_entity() -> __media_entity_remove_links() (drivers/media/mc/mc-entity.c:1274) iterates &entity->links, reading offset 584 (offsetof(struct em28xx_v4l2, vdev) + offsetof(struct video_device, entity) + offsetof(struct media_entity, links) = 504 + 0 + 80) inside the freed struct em28xx_v4l2 object.
(Note: A related lifecycle issue also exists in em28xx_usb_disconnect(), which calls em28xx_release_resources() -> em28xx_unregister_media_device() to free dev->media_dev unconditionally on disconnect even when open video device file handles defer v4l2_device_release(), where vdev->v4l2_dev->mdev still points to dev->media_dev.)