Skip to contentSkip to conversation

media: v4l2-dev: fix possible name leak in __video_register_device()

PATCH In review Yang Yingliang 2 participants 5 messages Nov 11, 2022 – Nov 14, 2022 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: v4l2-dev: fix possible name leak in __video_register_device()

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, so call kfree_const() to freed it in error case.

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
 drivers/media/v4l2-core/v4l2-dev.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/v4l2-core/v4l2-dev.c +3 -0
index 397d553177fa..1fce1c29c60f 100644
@@ -1036,6 +1036,9 @@ int __video_register_device(struct video_device *vdev,
 	ret = device_register(&vdev->dev);
 	if (ret < 0) {
 		pr_err("%s: device_register failed\n", __func__);
+		kfree_const(vdev->dev.kobj.name);
+		/* set it to null to avoid callers using a bad pointer */
+		vdev->dev.kobj.name = NULL;
 		goto cleanup;
 	}
 	/* Register the release callback that will be called when the last
Signature · 1 line
2.25.1
#1 lore raw
Quoted 15 lines · Yang Yingliang

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, so call kfree_const() to freed it in error case.

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
 drivers/media/v4l2-core/v4l2-dev.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/v4l2-core/v4l2-dev.c +0 -0
index 397d553177fa..1fce1c29c60f 100644

drivers/media/v4l2-core/v4l2-dev.c int __video_register_device(struct video_device *vdev, · line 1041

Quoted 6 lines
drivers/media/v4l2-core/v4l2-dev.c +3 -0
@@ -1036,6 +1036,9 @@ int __video_register_device(struct video_device *vdev,
 	ret = device_register(&vdev->dev);
 	if (ret < 0) {
 		pr_err("%s: device_register failed\n", __func__);
+		kfree_const(vdev->dev.kobj.name);
+		/* set it to null to avoid callers using a bad pointer */
+		vdev->dev.kobj.name = NULL;

This doesn't seem right to me. We shouldn't be poking into the kobj. This should be handled in the driver core, or if that can't be done for a reason specific to how the device is used in the V4L2 core, we need a helper function from the driver core to perform the cleanup.

Quoted 3 lines
drivers/media/v4l2-core/v4l2-dev.c +0 -0
 		goto cleanup;
 	}
 	/* Register the release callback that will be called when the last
Signature · 3 lines
Regards,

Laurent Pinchart
#2 lore raw
Quoted 11 lines · Laurent Pinchart
Quoted 10 lines · Yang Yingliang

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, so call kfree_const() to freed it in error case.

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
  drivers/media/v4l2-core/v4l2-dev.c | 3 +++
  1 file changed, 3 insertions(+)
Quoted 15 lines

drivers/media/v4l2-core/v4l2-dev.c int __video_register_device(struct video_device *vdev, · line 1041

Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +3 -0
index 397d553177fa..1fce1c29c60f 100644
@@ -1036,6 +1036,9 @@ int __video_register_device(struct video_device *vdev,
  	ret = device_register(&vdev->dev);
  	if (ret < 0) {
  		pr_err("%s: device_register failed\n", __func__);
+		kfree_const(vdev->dev.kobj.name);
+		/* set it to null to avoid callers using a bad pointer */
+		vdev->dev.kobj.name = NULL;

This doesn't seem right to me. We shouldn't be poking into the kobj. This should be handled in the driver core, or if that can't be done for a reason specific to how the device is used in the V4L2 core, we need a helper function from the driver core to perform the cleanup.

In technical, we should call put_device() here, but the release() function has not been set in this case.
May be we should  move 'vdev->dev.release = v4l2_device_release' before device_register(), then call put_device() to fix it.

Thanks,
Yang

Quoted 4 lines
Quoted 4 lines
drivers/media/v4l2-core/v4l2-dev.c +0 -0
  		goto cleanup;
  	}
  	/* Register the release callback that will be called when the last
#3 lore raw
In reply to Laurent Pinchart
Quoted 11 lines · Laurent Pinchart
Quoted 10 lines · Yang Yingliang

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, so call kfree_const() to freed it in error case.

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
  drivers/media/v4l2-core/v4l2-dev.c | 3 +++
  1 file changed, 3 insertions(+)
Quoted 15 lines

drivers/media/v4l2-core/v4l2-dev.c int __video_register_device(struct video_device *vdev, · line 1041

Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +3 -0
index 397d553177fa..1fce1c29c60f 100644
@@ -1036,6 +1036,9 @@ int __video_register_device(struct video_device *vdev,
  	ret = device_register(&vdev->dev);
  	if (ret < 0) {
  		pr_err("%s: device_register failed\n", __func__);
+		kfree_const(vdev->dev.kobj.name);
+		/* set it to null to avoid callers using a bad pointer */
+		vdev->dev.kobj.name = NULL;

This doesn't seem right to me. We shouldn't be poking into the kobj. This should be handled in the driver core, or if that can't be done for a reason specific to how the device is used in the V4L2 core, we need a helper function from the driver core to perform the cleanup.

In technical, we should call put_device() here, but the release() function has not been set in this case, and v4l2_device_release() will release something that not need be, so we can not handle it in the driver core well, I think free the name here directly is the best way to fix it.

Thanks,
Yang

Quoted 4 lines
Quoted 4 lines
drivers/media/v4l2-core/v4l2-dev.c +0 -0
  		goto cleanup;
  	}
  	/* Register the release callback that will be called when the last
#4 lore raw
Quoted 29 lines · Yang Yingliang
Quoted 11 lines · Laurent Pinchart
Quoted 10 lines · Yang Yingliang

Afer commit 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array"), the name of device is allocated dynamically, so call kfree_const() to freed it in error case.

Fixes 1fa5ae857bb1 ("driver core: get rid of struct device's bus_id string array")
---
  drivers/media/v4l2-core/v4l2-dev.c | 3 +++
  1 file changed, 3 insertions(+)
Quoted 16 lines

drivers/media/v4l2-core/v4l2-dev.c int __video_register_device(struct video_device *vdev, · line 1041

Quoted 11 lines
drivers/media/v4l2-core/v4l2-dev.c +3 -0
index 397d553177fa..1fce1c29c60f 100644
@@ -1036,6 +1036,9 @@ int __video_register_device(struct video_device *vdev,
  	ret = device_register(&vdev->dev);
  	if (ret < 0) {
  		pr_err("%s: device_register failed\n", __func__);
+		kfree_const(vdev->dev.kobj.name);
+		/* set it to null to avoid callers using a bad pointer */
+		vdev->dev.kobj.name = NULL;

This doesn't seem right to me. We shouldn't be poking into the kobj. This should be handled in the driver core, or if that can't be done for a reason specific to how the device is used in the V4L2 core, we need a helper function from the driver core to perform the cleanup.

Quoted 5 lines

In technical, we should call put_device() here, but the release() function has not been set in this case, and v4l2_device_release() will release something that not need be, so we can not handle it in the driver core well, I think free the name here directly is the best way to fix it.

The documentation of device_register() states

* NOTE: _Never_ directly free @dev after calling this function, even
* if it returned an error! Always use put_device() to give up the
* reference initialized in this function instead.

We thus need to fix this in __video_register_device(), not to pile another workaround :-)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/media/v4l2-core/v4l2-dev.c +0 -0
  		goto cleanup;
  	}
  	/* Register the release callback that will be called when the last
Signature · 3 lines
Regards,

Laurent Pinchart