media: v4l2-dev: fix media controller registration error handling
Subsystems (2)
PATCH v2 1/1 media: v4l2-dev: fix media controller registration error handling
__video_register_device() ignores failures from video_register_media_controller(). If media_devnode_create() fails, vdev->intf_devnode remains NULL but the video device is still marked as registered. A later video_unregister_device() reaches v4l2_device_release(), which calls media_devnode_remove() and dereferences the NULL pointer.
If media_create_intf_link() fails, video_register_media_controller() removes vdev->intf_devnode but leaves the stale pointer behind.
Fix this by propagating video_register_media_controller() failures from __video_register_device(). Since device_register() has already succeeded, unwind the device with device_unregister(). Also make media_devnode_remove() tolerate NULL devnodes and clear vdev->intf_devnode after removing it in the link failure path.
---
v1: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Move the NULL check to media_devnode_remove().
- Use device_unregister() after device_register() has succeeded.
- Keep clearing vdev->intf_devnode in the link failure path.
drivers/media/mc/mc-entity.c | 3 +++ drivers/media/v4l2-core/v4l2-dev.c | 6 ++++++ 2 files changed, 9 insertions(+)
drivers/media/mc/mc-entity.c +3 -0
@@ -1563,6 +1563,9 @@ EXPORT_SYMBOL_GPL(media_devnode_create);void media_devnode_remove (struct media_intf_devnode *devnode) { +if (!devnode) +return ; +media_remove_intf_links (&devnode->intf);media_gobj_destroy (&devnode->intf.graph_obj);kfree (devnode);
drivers/media/v4l2-core/v4l2-dev.c +6 -0
@@ -896,6 +896,7 @@ static int video_register_media_controller(struct video_device *vdev) MEDIA_LNK_FL_IMMUTABLE);if (!link) {media_devnode_remove (vdev->intf_devnode); + vdev->intf_devnode =NULL ;media_device_unregister_entity (&vdev->entity);return -ENOMEM ; }
@@ -1092,6 +1093,11 @@ int __video_register_device(struct video_device *vdev,/* Part 5: Register the entity. */ ret =video_register_media_controller (vdev); +if (ret <0 ) { +mutex_unlock (&videodev_lock); +device_unregister (&vdev->dev); +return ret; + }/* Part 6: Activate this minor. The char device can now be used. */ set_bit (V4L2_FL_REGISTERED , &vdev->flags);
Signature · 1 line
2.34.1
Quoted 57 lines · Shih-Sheng Yang
__video_register_device()ignores failures fromvideo_register_media_controller(). Ifmedia_devnode_create()fails,vdev->intf_devnoderemains NULL but the video device is still marked as registered. A latervideo_unregister_device()reachesv4l2_device_release(), which callsmedia_devnode_remove()and dereferences the NULL pointer.If
media_create_intf_link()fails,video_register_media_controller()removesvdev->intf_devnodebut leaves the stale pointer behind.Fix this by propagating
video_register_media_controller()failures from__video_register_device(). Sincedevice_register()has already succeeded, unwind the device withdevice_unregister(). Also makemedia_devnode_remove()tolerate NULL devnodes and clearvdev->intf_devnodeafter removing it in the link failure path.Suggested-by Laurent Pinchart <[email protected]>Signed-off-by Shih-Sheng Yang <[email protected]>---
v1: https://lore.kernel.org/r/[email protected]Changes in v2:
- Move the NULL check tomedia_devnode_remove().
- Usedevice_unregister()afterdevice_register()has succeeded.
- Keep clearingvdev->intf_devnodein the link failure path.drivers/media/mc/mc-entity.c | 3 +++ drivers/media/v4l2-core/v4l2-dev.c | 6 ++++++ 2 files changed, 9 insertions(+)drivers/media/mc/mc-entity.c +3
-0@@ -1563,6 +1563,9 @@ EXPORT_SYMBOL_GPL(media_devnode_create);void media_devnode_remove (struct media_intf_devnode *devnode) { +if (!devnode) +return ; +media_remove_intf_links (&devnode->intf);media_gobj_destroy (&devnode->intf.graph_obj);kfree (devnode);drivers/media/v4l2-core/v4l2-dev.c +1
-0@@ -896,6 +896,7 @@ static int video_register_media_controller(struct video_device *vdev) MEDIA_LNK_FL_IMMUTABLE);if (!link) {media_devnode_remove (vdev->intf_devnode); + vdev->intf_devnode =NULL ;media_device_unregister_entity (&vdev->entity);return -ENOMEM ; }
drivers/media/v4l2-core/v4l2-dev.c int __video_register_device(struct video_device *vdev, · line 1098
Quoted 6 lines
drivers/media/v4l2-core/v4l2-dev.c +3
-0@@ -1092,6 +1093,11 @@ int __video_register_device(struct video_device *vdev,/* Part 5: Register the entity. */ ret =video_register_media_controller (vdev); +if (ret <0 ) { +mutex_unlock (&videodev_lock); +device_unregister (&vdev->dev);
This causes problems. See this series for more information:
https://patchwork.linuxtv.org/project/linux-media/list/?series=27907
I think it is better to limit yourself to just setting vdev->intf_devnode to NULL and checking for a NULL pointer in media_devnode_remove().
As mentioned in that series, this function really needs to be split into two parts. But that's a much larger effort.
There is just a lot of history here, and no easy fix.
Regards,
Hans
Quoted 5 lines
drivers/media/v4l2-core/v4l2-dev.c +2
-0+return ret; + }/* Part 6: Activate this minor. The char device can now be used. */ set_bit (V4L2_FL_REGISTERED , &vdev->flags);