Skip to contentSkip to conversation

media: uvcvideo: Fix memory leak if uvc_ctrl_add_mapping fails

PATCH Superseded Ricardo Ribalda 2 participants 2 messages Mar 24, 2022

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 media: uvcvideo: Fix memory leak if uvc_ctrl_add_mapping fails

If the mapping fails, the name field is not freed on exit. Take the same approach as with the menu_info and have two different allocations with two different life cycles.

Fixes 07adedb5c606 ("media: uvcvideo: Use control names from framework")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 10 ++++++++++
 drivers/media/usb/uvc/uvc_v4l2.c |  8 ++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +10 -0
index b4f6edf968bc..8b3bd516cb2f 100644
@@ -2188,11 +2188,21 @@ static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
 	if (map == NULL)
 		return -ENOMEM;
 
+	/* For UVCIOC_CTRL_MAP custom controls */
+	if (mapping->name) {
+		map->name = kstrdup(mapping->name, GFP_KERNEL);
+		if (!map->name) {
+			kfree(map);
+			return -ENOMEM;
+		}
+	}
+
 	INIT_LIST_HEAD(&map->ev_subs);
 
 	size = sizeof(*mapping->menu_info) * mapping->menu_count;
 	map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
 	if (map->menu_info == NULL) {
+		kfree(map->name);
 		kfree(map);
 		return -ENOMEM;
 	}
drivers/media/usb/uvc/uvc_v4l2.c +4 -4
index 711556d13d03..891c0bbc295c 100644
@@ -42,12 +42,12 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
 	map->id = xmap->id;
 	/* Non standard control id. */
 	if (v4l2_ctrl_get_name(map->id) == NULL) {
-		map->name = kmemdup(xmap->name, sizeof(xmap->name),
-				    GFP_KERNEL);
-		if (!map->name) {
-			ret = -ENOMEM;
+		if (!xmap->name) {
+			ret = -EINVAL;
 			goto free_map;
 		}
+		map->name = xmap->name;
+		map->name[sizeof(xmap->name) - 1] = '\0';
 	}
 	memcpy(map->entity, xmap->entity, sizeof(map->entity));
 	map->selector = xmap->selector;
Signature · 1 line
2.35.1.1021.g381101b075-goog
maintainer Reviewed-by on v3 1/1
#1 lore raw

Hi Ricardo,

Thank you for the patch.

Quoted 3 lines · Ricardo Ribalda

If the mapping fails, the name field is not freed on exit. Take the same approach as with the menu_info and have two different allocations with two different life cycles.

I think this needs an update.

Quoted 41 lines
Fixes 07adedb5c606 ("media: uvcvideo: Use control names from framework")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 10 ++++++++++
 drivers/media/usb/uvc/uvc_v4l2.c |  8 ++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +10 -0
index b4f6edf968bc..8b3bd516cb2f 100644
@@ -2188,11 +2188,21 @@ static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
 	if (map == NULL)
 		return -ENOMEM;
 
+	/* For UVCIOC_CTRL_MAP custom controls */
+	if (mapping->name) {
+		map->name = kstrdup(mapping->name, GFP_KERNEL);
+		if (!map->name) {
+			kfree(map);
+			return -ENOMEM;
+		}
+	}
+
 	INIT_LIST_HEAD(&map->ev_subs);
 
 	size = sizeof(*mapping->menu_info) * mapping->menu_count;
 	map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
 	if (map->menu_info == NULL) {
+		kfree(map->name);
 		kfree(map);
 		return -ENOMEM;
 	}
drivers/media/usb/uvc/uvc_v4l2.c +0 -0
index 711556d13d03..891c0bbc295c 100644
@@ -42,12 +42,12 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
 	map->id = xmap->id;
 	/* Non standard control id. */
 	if (v4l2_ctrl_get_name(map->id) == NULL) {

drivers/media/usb/uvc/uvc_v4l2.c static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, · line 50

Quoted 10 lines
drivers/media/usb/uvc/uvc_v4l2.c +4 -4
-		map->name = kmemdup(xmap->name, sizeof(xmap->name),
-				    GFP_KERNEL);
-		if (!map->name) {
-			ret = -ENOMEM;
+		if (!xmap->name) {
+			ret = -EINVAL;
 			goto free_map;
 		}
+		map->name = xmap->name;
+		map->name[sizeof(xmap->name) - 1] = '\0';

Do you mind writing this as

		xmap->name[sizeof(xmap->name) - 1] = '\0';

? It looks less like a possible bug than having map and xmap.

Quoted 3 lines
drivers/media/usb/uvc/uvc_v4l2.c +0 -0
 	}
 	memcpy(map->entity, xmap->entity, sizeof(map->entity));
 	map->selector = xmap->selector;
Signature · 3 lines
Regards,

Laurent Pinchart