Skip to content

Commit d934618

Browse files
DanisJiangopsiff
authored andcommitted
USB: gadget: f_hid: Fix memory leak in hidg_bind error path
commit 62783c30d78aecf9810dae46fd4d11420ad38b74 upstream. In hidg_bind(), if alloc_workqueue() fails after usb_assign_descriptors() has successfully allocated the USB descriptors, the current error handling does not call usb_free_all_descriptors() to free the allocated descriptors, resulting in a memory leak. Restructure the error handling by adding proper cleanup labels: - fail_free_all: cleans up workqueue and descriptors - fail_free_descs: cleans up descriptors only - fail: original cleanup for earlier failures This ensures that allocated resources are properly freed in reverse order of their allocation, preventing the memory leak when alloc_workqueue() fails. Fixes: a139c98 ("USB: gadget: f_hid: Add GET_REPORT via userspace IOCTL") Cc: [email protected] Signed-off-by: Yuhao Jiang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 0ab3ae768c484724ebdacc61a7117f83ef432419)
1 parent 9326da8 commit d934618

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

drivers/usb/gadget/function/f_hid.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,18 +1275,19 @@ static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
12751275

12761276
if (!hidg->workqueue) {
12771277
status = -ENOMEM;
1278-
goto fail;
1278+
goto fail_free_descs;
12791279
}
12801280

12811281
/* create char device */
12821282
cdev_init(&hidg->cdev, &f_hidg_fops);
12831283
status = cdev_device_add(&hidg->cdev, &hidg->dev);
12841284
if (status)
1285-
goto fail_free_descs;
1285+
goto fail_free_all;
12861286

12871287
return 0;
1288-
fail_free_descs:
1288+
fail_free_all:
12891289
destroy_workqueue(hidg->workqueue);
1290+
fail_free_descs:
12901291
usb_free_all_descriptors(f);
12911292
fail:
12921293
ERROR(f->config->cdev, "hidg_bind FAILED\n");

0 commit comments

Comments
 (0)