Skip to content

Commit b2f4e9d

Browse files
DanisJianggregkh
authored andcommitted
USB: gadget: f_hid: Fix memory leak in hidg_bind error path
commit 62783c3 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]>
1 parent 00896c3 commit b2f4e9d

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

drivers/usb/gadget/function/f_hid.c

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

12791279
if (!hidg->workqueue) {
12801280
status = -ENOMEM;
1281-
goto fail;
1281+
goto fail_free_descs;
12821282
}
12831283

12841284
/* create char device */
12851285
cdev_init(&hidg->cdev, &f_hidg_fops);
12861286
status = cdev_device_add(&hidg->cdev, &hidg->dev);
12871287
if (status)
1288-
goto fail_free_descs;
1288+
goto fail_free_all;
12891289

12901290
return 0;
1291-
fail_free_descs:
1291+
fail_free_all:
12921292
destroy_workqueue(hidg->workqueue);
1293+
fail_free_descs:
12931294
usb_free_all_descriptors(f);
12941295
fail:
12951296
ERROR(f->config->cdev, "hidg_bind FAILED\n");

0 commit comments

Comments
 (0)