Skip to contentSkip to conversation

media: uvcvideo: Fix memory leak of object map on error exit path

PATCH In review Colin King 3 participants 3 messages Sep 17, 2021 – Oct 8, 2021

Subsystems (3)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH 1/1 media: uvcvideo: Fix memory leak of object map on error exit path

From: Colin Ian King <[email protected]>

Currently when the allocation of map->name fails the error exit path does not kfree the previously allocated object map. Fix this by setting ret to -ENOMEM and taking the free_map exit error path to ensure map is kfree'd.

Addresses-Coverity: ("Resource leak")

Fixes 07adedb5c606 ("media: uvcvideo: Use control names from framework")
---
 drivers/media/usb/uvc/uvc_v4l2.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_v4l2.c +4 -2
index f4e4aff8ddf7..711556d13d03 100644
@@ -44,8 +44,10 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
 	if (v4l2_ctrl_get_name(map->id) == NULL) {
 		map->name = kmemdup(xmap->name, sizeof(xmap->name),
 				    GFP_KERNEL);
-		if (!map->name)
-			return -ENOMEM;
+		if (!map->name) {
+			ret = -ENOMEM;
+			goto free_map;
+		}
 	}
 	memcpy(map->entity, xmap->entity, sizeof(map->entity));
 	map->selector = xmap->selector;
Signature · 1 line
2.32.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Collin

Thanks for catching it up.

Quoted 11 lines · Colin King

From: Colin Ian King <[email protected]>

Currently when the allocation of map->name fails the error exit path does not kfree the previously allocated object map. Fix this by setting ret to -ENOMEM and taking the free_map exit error path to ensure map is kfree'd.

Addresses-Coverity: ("Resource leak")

Fixes 07adedb5c606 ("media: uvcvideo: Use control names from framework")
Quoted 22 lines
---
 drivers/media/usb/uvc/uvc_v4l2.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_v4l2.c +4 -3
index f4e4aff8ddf7..711556d13d03 100644
@@ -44,8 +44,10 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
        if (v4l2_ctrl_get_name(map->id) == NULL) {
                map->name = kmemdup(xmap->name, sizeof(xmap->name),
                                    GFP_KERNEL);
-               if (!map->name)
-                       return -ENOMEM;
+               if (!map->name) {
+                       ret = -ENOMEM;
+                       goto free_map;
+               }
        }
        memcpy(map->entity, xmap->entity, sizeof(map->entity));
        map->selector = xmap->selector;
--
Quoted 2 lines

2.32.0

--
Ricardo Ribalda

maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Colin King

Hi Colin,

Thank you for the patch.

Quoted 9 lines · Colin King

From: Colin Ian King <[email protected]>

Currently when the allocation of map->name fails the error exit path does not kfree the previously allocated object map. Fix this by setting ret to -ENOMEM and taking the free_map exit error path to ensure map is kfree'd.

Addresses-Coverity: ("Resource leak")

Fixes 07adedb5c606 ("media: uvcvideo: Use control names from framework")

That's not the right commit ID, it should be 70fa906d6fce.

Quoted 1 line

Mauro, could you add this in your tree for v5.16 ?

Quoted 21 lines
---
 drivers/media/usb/uvc/uvc_v4l2.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_v4l2.c +4 -2
index f4e4aff8ddf7..711556d13d03 100644
@@ -44,8 +44,10 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,
 	if (v4l2_ctrl_get_name(map->id) == NULL) {
 		map->name = kmemdup(xmap->name, sizeof(xmap->name),
 				    GFP_KERNEL);
-		if (!map->name)
-			return -ENOMEM;
+		if (!map->name) {
+			ret = -ENOMEM;
+			goto free_map;
+		}
 	}
 	memcpy(map->entity, xmap->entity, sizeof(map->entity));
 	map->selector = xmap->selector;
Signature · 3 lines
Regards,

Laurent Pinchart