* [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup
@ 2026-11-25 9:05 Aristo Chen
2026-11-25 9:05 ` [PATCH 1/2] usb: gadget: max3420_udc: fix out of bounds endpoint index from wIndex Aristo Chen
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Aristo Chen @ 2026-11-25 9:05 UTC (permalink / raw)
To: Greg Kroah-Hartman, Neill Kapron
Cc: Alan Stern, Krishna Kurapati, Jassi Brar, linux-usb, Aristo Chen
This series fixes two bugs that I found while looking at max3420_udc.
Patch 1 fixes an out of bounds array index in the SETUP handling of
max3420_udc. The endpoint number comes from wIndex, which the host
controls, and the driver uses it without a range check.
Patch 2 fixes the gadget core. Since v6.7, gadget_bind_driver() fails
when the connect step returns an error, and that step returns
-EOPNOTSUPP when the UDC has no pullup callback. So no gadget driver
can bind to such a UDC. This affects max3420_udc and four other UDC
drivers.
Patch 1 comes first so that the bounds check is in place when patch 2
makes the bind work again. Mainline v5.7 to v6.6 have the driver but
not the bind problem, so they only need patch 1.
I do not have the hardware. I tested with a KUnit test under User Mode
Linux that emulates the chip on a fake SPI controller. It gives the
driver SETUP packets and records the answers. With KASAN and UBSAN, on
usb-linus:
no patch: the gadget driver does not bind (-EOPNOTSUPP)
patch 1 only: the same, the driver still cannot bind
patch 2 only: it binds, bad endpoint numbers give UBSAN and KASAN
reports
both patches: the tests pass, no reports
I also built both files with W=1 for x86_64 and arm64.
I did not test the other UDC drivers that patch 2 affects.
The pullup callbacks of pxa25x_udc, pxa27x_udc and fsl_udc_core can
return -EOPNOTSUPP themselves. This series does not change how the
bind handles that.
The test is not part of this series. I can send it on request.
AI tools were used for this series. The out of bounds index in patch 1
was found by an AI code review of the driver. I used an AI coding
assistant (Claude Code) to write the KUnit test, both patches and their
descriptions. Running that test showed the bind problem that patch 2
fixes. A second AI tool (GitHub Copilot) reviewed the patches. I have
reviewed all of it.
Aristo Chen (2):
usb: gadget: max3420_udc: fix out of bounds endpoint index from wIndex
usb: gadget: udc: do not fail bind when the UDC has no pullup callback
drivers/usb/gadget/udc/core.c | 9 ++++++---
drivers/usb/gadget/udc/max3420_udc.c | 8 +++++++-
2 files changed, 13 insertions(+), 4 deletions(-)
base-commit: b0baaf6cbb16bc773d0c76916aac17c44a6cd557
--
2.53.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/2] usb: gadget: max3420_udc: fix out of bounds endpoint index from wIndex
2026-11-25 9:05 [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup Aristo Chen
@ 2026-11-25 9:05 ` Aristo Chen
2026-11-25 9:05 ` [PATCH 2/2] usb: gadget: udc: do not fail bind when the UDC has no pullup callback Aristo Chen
2026-11-25 9:25 ` [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup sashiko-bot
2 siblings, 0 replies; 4+ messages in thread
From: Aristo Chen @ 2026-11-25 9:05 UTC (permalink / raw)
To: Greg Kroah-Hartman, Neill Kapron
Cc: Alan Stern, Krishna Kurapati, Jassi Brar, linux-usb, Aristo Chen,
stable
The MAX3420 has four endpoints, and udc->ep[] has MAX3420_MAX_EPS (4)
entries. GET_STATUS for an endpoint, and SET_FEATURE and CLEAR_FEATURE
of ENDPOINT_HALT, take the endpoint number from wIndex, which the host
controls. The driver only masks it with USB_ENDPOINT_NUMBER_MASK, so
the number can be 0 to 15. Endpoint numbers 4 to 15 index past the end
of the array.
GET_STATUS uses the indexed entry to check the endpoint direction. If
that check passes, it reports the halt state from that entry.
SET_FEATURE and CLEAR_FEATURE use the same unchecked index to update
the halt state: they change ep->todo and call spi_max3420_stall(),
which reads and writes more fields.
Stall such requests. Both functions already have a stall path for
requests that they cannot answer.
The driver has been in mainline since v5.7. Commit 0ea39e030a80 ("usb:
gadget: udc: Handle gadget_connect failure during bind operation"),
first in v6.7, makes the gadget driver bind fail on this UDC, because
the driver has no pullup callback. The next patch fixes the bind, so
this patch has to go first. Mainline v5.7 to v6.6 have the driver but
not that commit.
I do not have the hardware. I tested with a KUnit test under User Mode
Linux that emulates a MAX3420 on a fake SPI controller, with KASAN and
UBSAN. Without the bind fix, a SETUP packet sent to the emulated chip
after the failed bind got no answer. With only the bind fix applied,
UBSAN reports an array-index-out-of-bounds in both functions and KASAN
reports a slab-out-of-bounds read in the driver thread. With this patch
as well, the test gets a stall for endpoint numbers 4, 5 and 15 in all
three requests, and there is no KASAN or UBSAN report. Halting and
unhalting endpoint 1 still works.
The bug was found by an AI code review. This patch and the test were
written with an AI coding assistant.
Fixes: 48ba02b2e2b1 ("usb: gadget: add udc driver for max3420")
Cc: stable@vger.kernel.org
Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
---
drivers/usb/gadget/udc/max3420_udc.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/gadget/udc/max3420_udc.c b/drivers/usb/gadget/udc/max3420_udc.c
index 6dbea43022a7..d3a4c824a732 100644
--- a/drivers/usb/gadget/udc/max3420_udc.c
+++ b/drivers/usb/gadget/udc/max3420_udc.c
@@ -536,6 +536,7 @@ static void max3420_getstatus(struct max3420_udc *udc)
{
struct max3420_ep *ep;
u16 status = 0;
+ int id;
switch (udc->setup.bRequestType & USB_RECIP_MASK) {
case USB_RECIP_DEVICE:
@@ -548,7 +549,10 @@ static void max3420_getstatus(struct max3420_udc *udc)
goto stall;
break;
case USB_RECIP_ENDPOINT:
- ep = &udc->ep[udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK];
+ id = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;
+ if (id >= MAX3420_MAX_EPS)
+ goto stall;
+ ep = &udc->ep[id];
if (udc->setup.wIndex & USB_DIR_IN) {
if (!ep->ep_usb.caps.dir_in)
goto stall;
@@ -596,6 +600,8 @@ static void max3420_set_clear_feature(struct max3420_udc *udc)
break;
id = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;
+ if (id >= MAX3420_MAX_EPS)
+ break;
ep = &udc->ep[id];
spin_lock_irqsave(&ep->lock, flags);
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] usb: gadget: udc: do not fail bind when the UDC has no pullup callback
2026-11-25 9:05 [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup Aristo Chen
2026-11-25 9:05 ` [PATCH 1/2] usb: gadget: max3420_udc: fix out of bounds endpoint index from wIndex Aristo Chen
@ 2026-11-25 9:05 ` Aristo Chen
2026-11-25 9:25 ` [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup sashiko-bot
2 siblings, 0 replies; 4+ messages in thread
From: Aristo Chen @ 2026-11-25 9:05 UTC (permalink / raw)
To: Greg Kroah-Hartman, Neill Kapron
Cc: Alan Stern, Krishna Kurapati, Jassi Brar, linux-usb, Aristo Chen,
stable
Commit 0ea39e030a80 ("usb: gadget: udc: Handle gadget_connect failure
during bind operation") made gadget_bind_driver() fail when
usb_udc_connect_control_locked() returns an error. Before that commit
the return value was ignored.
usb_gadget_connect_locked() and usb_gadget_disconnect_locked() return
-EOPNOTSUPP when the UDC driver has no pullup callback. So since that
commit, no gadget driver can bind to such a UDC. max3420_udc,
fotg210-udc, fsl_qe_udc, goku_udc and snps_udc_core have no pullup
callback.
The core cannot control the pullup of such a UDC, so there is nothing
for it to do in this step. Skip the step during bind when there is no
pullup callback. If the step runs and returns an error, the bind still
fails, which is what the commit above wanted.
I do not have hardware for any of these drivers. I found the problem
with a KUnit test under User Mode Linux that emulates a MAX3420 on a
fake SPI controller. There the bind failed with -EOPNOTSUPP:
udc spi0.0: failed to start max3420-kunit: -95
With this patch the bind works and the emulated controller answers
SETUP packets. I did not test on hardware or with the other drivers.
This patch and the test were written with an AI coding assistant.
Fixes: 0ea39e030a80 ("usb: gadget: udc: Handle gadget_connect failure during bind operation")
Cc: stable@vger.kernel.org
Signed-off-by: Aristo Chen <aristo.chen@canonical.com>
---
drivers/usb/gadget/udc/core.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c
index 577bc15f61a1..aa6226666eb5 100644
--- a/drivers/usb/gadget/udc/core.c
+++ b/drivers/usb/gadget/udc/core.c
@@ -1680,9 +1680,12 @@ static int gadget_bind_driver(struct device *dev)
}
usb_gadget_enable_async_callbacks(udc);
udc->allow_connect = true;
- ret = usb_udc_connect_control_locked(udc);
- if (ret)
- goto err_connect_control;
+ /* Without a pullup callback the core cannot control the pullup */
+ if (gadget->ops->pullup) {
+ ret = usb_udc_connect_control_locked(udc);
+ if (ret)
+ goto err_connect_control;
+ }
mutex_unlock(&udc->connect_lock);
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup
2026-11-25 9:05 [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup Aristo Chen
2026-11-25 9:05 ` [PATCH 1/2] usb: gadget: max3420_udc: fix out of bounds endpoint index from wIndex Aristo Chen
2026-11-25 9:05 ` [PATCH 2/2] usb: gadget: udc: do not fail bind when the UDC has no pullup callback Aristo Chen
@ 2026-11-25 9:25 ` sashiko-bot
2 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-25 9:25 UTC (permalink / raw)
To: Aristo Chen; +Cc: linux-usb
Sashiko has reviewed this 2-patch series and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261125092004.15562-1-aristo.chen@canonical.com
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-25 9:25 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 9:05 [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup Aristo Chen
2026-11-25 9:05 ` [PATCH 1/2] usb: gadget: max3420_udc: fix out of bounds endpoint index from wIndex Aristo Chen
2026-11-25 9:05 ` [PATCH 2/2] usb: gadget: udc: do not fail bind when the UDC has no pullup callback Aristo Chen
2026-11-25 9:25 ` [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox