Skip to content

Commit 47b2116

Browse files
Kuen-Han Tsaigregkh
authored andcommitted
usb: gadget: f_acm: Refactor bind path to use __free()
After an bind/unbind cycle, the acm->notify_req is left stale. If a subsequent bind fails, the unified error label attempts to free this stale request, leading to a NULL pointer dereference when accessing ep->ops->free_request. Refactor the error handling in the bind path to use the __free() automatic cleanup mechanism. Unable to handle kernel NULL pointer dereference at virtual address 0000000000000020 Call trace: usb_ep_free_request+0x2c/0xec gs_free_req+0x30/0x44 acm_bind+0x1b8/0x1f4 usb_add_function+0xcc/0x1f0 configfs_composite_bind+0x468/0x588 gadget_bind_driver+0x104/0x270 really_probe+0x190/0x374 __driver_probe_device+0xa0/0x12c driver_probe_device+0x3c/0x218 __device_attach_driver+0x14c/0x188 bus_for_each_drv+0x10c/0x168 __device_attach+0xfc/0x198 device_initial_probe+0x14/0x24 bus_probe_device+0x94/0x11c device_add+0x268/0x48c usb_add_gadget+0x198/0x28c dwc3_gadget_init+0x700/0x858 __dwc3_set_mode+0x3cc/0x664 process_scheduled_works+0x1d8/0x488 worker_thread+0x244/0x334 kthread+0x114/0x1bc ret_from_fork+0x10/0x20 Fixes: 1f1ba11 ("usb gadget: issue notifications from ACM function") Cc: stable@kernel.org Signed-off-by: Kuen-Han Tsai <khtsai@google.com> Link: https://lore.kernel.org/r/20250916-ready-v1-4-4997bf277548@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Link: https://lore.kernel.org/r/20250916-ready-v1-4-4997bf277548@google.com
1 parent 75a5b8d commit 47b2116

1 file changed

Lines changed: 19 additions & 23 deletions

File tree

drivers/usb/gadget/function/f_acm.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111

1212
/* #define VERBOSE_DEBUG */
1313

14+
#include <linux/cleanup.h>
1415
#include <linux/slab.h>
1516
#include <linux/kernel.h>
1617
#include <linux/module.h>
1718
#include <linux/device.h>
1819
#include <linux/err.h>
1920

21+
#include <linux/usb/gadget.h>
22+
2023
#include "u_serial.h"
2124

2225

@@ -613,6 +616,7 @@ acm_bind(struct usb_configuration *c, struct usb_function *f)
613616
struct usb_string *us;
614617
int status;
615618
struct usb_ep *ep;
619+
struct usb_request *request __free(free_usb_request) = NULL;
616620

617621
/* REVISIT might want instance-specific strings to help
618622
* distinguish instances ...
@@ -630,7 +634,7 @@ acm_bind(struct usb_configuration *c, struct usb_function *f)
630634
/* allocate instance-specific interface IDs, and patch descriptors */
631635
status = usb_interface_id(c, f);
632636
if (status < 0)
633-
goto fail;
637+
return status;
634638
acm->ctrl_id = status;
635639
acm_iad_descriptor.bFirstInterface = status;
636640

@@ -639,43 +643,41 @@ acm_bind(struct usb_configuration *c, struct usb_function *f)
639643

640644
status = usb_interface_id(c, f);
641645
if (status < 0)
642-
goto fail;
646+
return status;
643647
acm->data_id = status;
644648

645649
acm_data_interface_desc.bInterfaceNumber = status;
646650
acm_union_desc.bSlaveInterface0 = status;
647651
acm_call_mgmt_descriptor.bDataInterface = status;
648652

649-
status = -ENODEV;
650-
651653
/* allocate instance-specific endpoints */
652654
ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_in_desc);
653655
if (!ep)
654-
goto fail;
656+
return -ENODEV;
655657
acm->port.in = ep;
656658

657659
ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_out_desc);
658660
if (!ep)
659-
goto fail;
661+
return -ENODEV;
660662
acm->port.out = ep;
661663

662664
ep = usb_ep_autoconfig(cdev->gadget, &acm_fs_notify_desc);
663665
if (!ep)
664-
goto fail;
666+
return -ENODEV;
665667
acm->notify = ep;
666668

667669
acm_iad_descriptor.bFunctionProtocol = acm->bInterfaceProtocol;
668670
acm_control_interface_desc.bInterfaceProtocol = acm->bInterfaceProtocol;
669671

670672
/* allocate notification */
671-
acm->notify_req = gs_alloc_req(ep,
672-
sizeof(struct usb_cdc_notification) + 2,
673-
GFP_KERNEL);
674-
if (!acm->notify_req)
675-
goto fail;
673+
request = gs_alloc_req(ep,
674+
sizeof(struct usb_cdc_notification) + 2,
675+
GFP_KERNEL);
676+
if (!request)
677+
return -ENODEV;
676678

677-
acm->notify_req->complete = acm_cdc_notify_complete;
678-
acm->notify_req->context = acm;
679+
request->complete = acm_cdc_notify_complete;
680+
request->context = acm;
679681

680682
/* support all relevant hardware speeds... we expect that when
681683
* hardware is dual speed, all bulk-capable endpoints work at
@@ -692,22 +694,16 @@ acm_bind(struct usb_configuration *c, struct usb_function *f)
692694
status = usb_assign_descriptors(f, acm_fs_function, acm_hs_function,
693695
acm_ss_function, acm_ss_function);
694696
if (status)
695-
goto fail;
697+
return status;
698+
699+
acm->notify_req = no_free_ptr(request);
696700

697701
dev_dbg(&cdev->gadget->dev,
698702
"acm ttyGS%d: IN/%s OUT/%s NOTIFY/%s\n",
699703
acm->port_num,
700704
acm->port.in->name, acm->port.out->name,
701705
acm->notify->name);
702706
return 0;
703-
704-
fail:
705-
if (acm->notify_req)
706-
gs_free_req(acm->notify, acm->notify_req);
707-
708-
ERROR(cdev, "%s/%p: can't bind, err %d\n", f->name, f, status);
709-
710-
return status;
711707
}
712708

713709
static void acm_unbind(struct usb_configuration *c, struct usb_function *f)

0 commit comments

Comments
 (0)