media: uvcvideo: Fix memory leak if uvc_ctrl_add_mapping fails
Subsystems (2)
PATCH 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.
Signed-off-by Ricardo Ribalda <[email protected]>
---
To be applied after [PATCH][next] media: uvcvideo: Fix memory leak of object map on error exit path
drivers/media/usb/uvc/uvc_ctrl.c | 10 ++++++++++ drivers/media/usb/uvc/uvc_v4l2.c | 10 +++++++--- 2 files changed, 17 insertions(+), 3 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +10 -0
@@ -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 =kmemdup (mapping->name,strlen (mapping->name) +1 ,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 +7 -3
@@ -48,6 +48,8 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain, ret = -ENOMEM ; goto free_map; } +/* Make sure there is a terminating null byte */ + map->name[sizeof (xmap->name) -1 ] ='\0' ; }memcpy (map->entity, xmap->entity,sizeof (map->entity)); map->selector = xmap->selector;
@@ -69,14 +71,14 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,if (xmap->menu_count ==0 || xmap->menu_count >UVC_MAX_CONTROL_MENU_ENTRIES ) { ret = -EINVAL ; - goto free_map; + goto free_map_name; } size = xmap->menu_count *sizeof (*map->menu_info); map->menu_info =memdup_user (xmap->menu_info, size);if (IS_ERR (map->menu_info)) { ret =PTR_ERR (map->menu_info); - goto free_map; + goto free_map_name; } map->menu_count = xmap->menu_count;
@@ -86,12 +88,14 @@ static int uvc_ioctl_ctrl_map(struct uvc_video_chain *chain,uvc_dbg (chain->dev,CONTROL ,"Unsupported V4L2 control type %u\n" , xmap->v4l2_type); ret = -ENOTTY ; - goto free_map; + goto free_map_name; } ret =uvc_ctrl_add_mapping (chain, map);kfree (map->menu_info); +free_map_name: +kfree (map->name); free_map:kfree (map);
Signature · 1 line
2.33.0.464.g1972c5931b-goog