* [PATCH] usb: gadget: fsl_qe_udc: Fix gadget lifetime and probe resource leaks
@ 2026-12-01 10:39 Wanhui Fu
2026-12-01 10:48 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Wanhui Fu @ 2026-12-01 10:39 UTC (permalink / raw)
To: nkapron, gregkh, haoxiang_li2024, X.Xie, arnd, leoyang.li, balbi,
sebastian
Cc: linux-usb, linuxppc-dev, Wanhui Fu
qe_udc_config() allocates an endpoint parameter block in MURAM.
However, failures in qe_udc_probe() free the controller without
releasing this block.
Furthermore, usb_add_gadget_udc_release() drops the gadget
reference on registration failure, potentially invoking
qe_udc_release() before probe has finished unwinding. This can
result in a NULL pointer dereference in complete(), use-after-free
and double free. A similar lifetime issue exists on removal.
Initialize the gadget immediately after qe_udc_config() succeeds
and register it separately using usb_add_gadget(). Release the
initial gadget reference only after the remaining resources have
been cleaned up on probe failure and removal.
Found it by self-developed static analysis tool. Compile-test
passed.
Fixes: 3948f0e0c999 ("usb: add Freescale QE/CPM USB peripheral controller driver")
Fixes: d77c1198666d ("usb: gadget: fsl_qe_udc: convert to new style start/stop")
Signed-off-by: Wanhui Fu <fuwh1120@163.com>
---
drivers/usb/gadget/udc/fsl_qe_udc.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/gadget/udc/fsl_qe_udc.c b/drivers/usb/gadget/udc/fsl_qe_udc.c
index 603c77ff129f..bca063882863 100644
--- a/drivers/usb/gadget/udc/fsl_qe_udc.c
+++ b/drivers/usb/gadget/udc/fsl_qe_udc.c
@@ -2461,14 +2461,14 @@ static int qe_ep_config(struct qe_udc *udc, unsigned char pipe_num)
static void qe_udc_release(struct device *dev)
{
struct qe_udc *udc = container_of(dev, struct qe_udc, gadget.dev);
- int i;
+ struct completion *done = udc->done;
- complete(udc->done);
cpm_muram_free(cpm_muram_offset(udc->ep_param[0]));
- for (i = 0; i < USB_MAX_ENDPOINTS; i++)
- udc->ep_param[i] = NULL;
kfree(udc);
+
+ if (done)
+ complete(done);
}
/* Driver probe functions */
@@ -2493,6 +2493,8 @@ static int qe_udc_probe(struct platform_device *ofdev)
return -ENOMEM;
}
+ usb_initialize_gadget(&ofdev->dev, &udc->gadget, qe_udc_release);
+
udc->soc_type = (unsigned long)device_get_match_data(&ofdev->dev);
udc->usb_regs = of_iomap(np, 0);
if (!udc->usb_regs) {
@@ -2579,8 +2581,7 @@ static int qe_udc_probe(struct platform_device *ofdev)
goto err4;
}
- ret = usb_add_gadget_udc_release(&ofdev->dev, &udc->gadget,
- qe_udc_release);
+ ret = usb_add_gadget(&udc->gadget);
if (ret)
goto err5;
@@ -2614,7 +2615,7 @@ static int qe_udc_probe(struct platform_device *ofdev)
err2:
iounmap(udc->usb_regs);
err1:
- kfree(udc);
+ usb_put_gadget(&udc->gadget);
return ret;
}
@@ -2637,9 +2638,8 @@ static void qe_udc_remove(struct platform_device *ofdev)
unsigned int size;
DECLARE_COMPLETION_ONSTACK(done);
- usb_del_gadget_udc(&udc->gadget);
+ usb_del_gadget(&udc->gadget);
- udc->done = &done;
tasklet_disable(&udc->rx_tasklet);
if (udc->nullmap) {
@@ -2679,6 +2679,8 @@ static void qe_udc_remove(struct platform_device *ofdev)
iounmap(udc->usb_regs);
+ udc->done = &done;
+ usb_put_gadget(&udc->gadget);
/* wait for release() of gadget.dev to free udc */
wait_for_completion(&done);
}
--
2.25.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] usb: gadget: fsl_qe_udc: Fix gadget lifetime and probe resource leaks
2026-12-01 10:39 [PATCH] usb: gadget: fsl_qe_udc: Fix gadget lifetime and probe resource leaks Wanhui Fu
@ 2026-12-01 10:48 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01 10:48 UTC (permalink / raw)
To: Wanhui Fu; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] usb: gadget: fsl_qe_udc: inaccurate Fixes trailer citing
d77c1198666d
--
commit 64343cbe73c11d8bc05f500bd0f35657cb66b1fd
Author: Wanhui Fu <fuwh1120@163.com>
> usb: gadget: fsl_qe_udc: Fix gadget lifetime and probe resource leaks
>
> qe_udc_config() allocates an endpoint parameter block in MURAM.
> However, failures in qe_udc_probe() free the controller without
> releasing this block.
>
> Furthermore, usb_add_gadget_udc_release() drops the gadget
> reference on registration failure, potentially invoking
> qe_udc_release() before probe has finished unwinding. This can
> result in a NULL pointer dereference in complete(), use-after-free
> and double free. A similar lifetime issue exists on removal.
>
> Initialize the gadget immediately after qe_udc_config() succeeds
> and register it separately using usb_add_gadget(). Release the
> initial gadget reference only after the remaining resources have
> been cleaned up on probe failure and removal.
>
> Found it by self-developed static analysis tool. Compile-test
> passed.
>
> Fixes: 3948f0e0c999 ("usb: add Freescale QE/CPM USB peripheral controller driver")
> Fixes: d77c1198666d ("usb: gadget: fsl_qe_udc: convert to new style start/stop")
> Signed-off-by: Wanhui Fu <fuwh1120@163.com>
[Severity: Low]
Is the second Fixes: tag accurate? Commit d77c1198666d ("usb: gadget:
fsl_qe_udc: convert to new style start/stop") only converted the start/stop
callbacks and replaced the global udc_controller pointer with a local udc
pointer without changing gadget device registration or release lifetime.
The premature gadget release on probe failure and removal was introduced when
udc-core took over gadget.dev management in commits 0f91349b89f3 ("usb:
gadget: convert all users to the new udc infrastructure"), c07d1b63ac3c
("usb: gadget: fsl_qe_udc: let udc-core manage gadget->dev"), and
29e7dbf32967 ("usb: gadget: fsl_qe_udc: don't assign gadget.dev.release
directly").
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201103911.3309038-1-fuwh1120@163.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-01 10:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 10:39 [PATCH] usb: gadget: fsl_qe_udc: Fix gadget lifetime and probe resource leaks Wanhui Fu
2026-12-01 10:48 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox