* [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; 6+ 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] 6+ 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; 6+ 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] 6+ 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 14:35 ` Alan Stern 2026-11-25 9:25 ` [PATCH 0/2] usb: gadget: fix max3420_udc endpoint index and bind without pullup sashiko-bot 2 siblings, 1 reply; 6+ 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] 6+ messages in thread
* Re: [PATCH 2/2] usb: gadget: udc: do not fail bind when the UDC has no pullup callback 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 14:35 ` Alan Stern 2026-11-26 6:46 ` Aristo Chen 0 siblings, 1 reply; 6+ messages in thread From: Alan Stern @ 2026-11-25 14:35 UTC (permalink / raw) To: Aristo Chen Cc: Greg Kroah-Hartman, Neill Kapron, Krishna Kurapati, Jassi Brar, linux-usb, stable On Wed, Nov 25, 2026 at 09:05:30AM +0000, Aristo Chen wrote: > 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); Your patch may conflict with this one from Alexey Charkov: https://lore.kernel.org/linux-usb/20261117-udc-connect-v3-3-9413b491d213@flipper.net/ You should look into this and see if any changes are needed. Alan Stern ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/2] usb: gadget: udc: do not fail bind when the UDC has no pullup callback 2026-11-25 14:35 ` Alan Stern @ 2026-11-26 6:46 ` Aristo Chen 0 siblings, 0 replies; 6+ messages in thread From: Aristo Chen @ 2026-11-26 6:46 UTC (permalink / raw) To: Alan Stern, Alexey Charkov Cc: Greg Kroah-Hartman, Neill Kapron, Krishna Kurapati, Jassi Brar, linux-usb, stable On Wed, Nov 25, 2026 at 09:35:39AM -0500, Alan Stern wrote: > Your patch may conflict with this one from Alexey Charkov: > > https://lore.kernel.org/linux-usb/20261117-udc-connect-v3-3-9413b491d213@flipper.net/ > > You should look into this and see if any changes are needed. Thanks, I had missed that series. I have added Alexey. Yes, they conflict. Both patches change the same code in gadget_bind_driver(). My patch 1/2 applies on top of v3. My patch 2/2 does not. v3 3/3 also handles a UDC without a pullup callback: usb_udc_update_connection_locked() returns 0 for it, so the bind no longer fails for that reason. I ran the KUnit test from my cover letter (emulated MAX3420, with KASAN and UBSAN) on usb-linus, commit 730acad0b83d. That tree already has v3 1/3. usb-linus: The bind fails with -EOPNOTSUPP. usb-linus plus v3 2/3 and 3/3: The bind works. 2 tests pass. The 3 tests with bad endpoint numbers fail, with UBSAN and KASAN reports. 1 test is skipped. The same plus my patch 1/2: 5 tests pass. 1 test is skipped. No UBSAN or KASAN report. The test that is skipped in the last two runs only runs when the bind fails. Alexey, I saw the bind failure that you describe in 3/3 on the emulated MAX3420. Two things follow from this. 1. With v3 the bind works and the test hits the out of bounds index. So my patch 1/2 should go in before either bind fix, or together with it. 2. For the bind fix I see two ways. a) Take my patch 2/2 now, so that stable can pick it up. It only changes the bind step. v3 then takes over that code. I tried this order. With my two patches first, v3 2/3 applies cleanly. A three way merge of v3 3/3 has one conflict, in gadget_bind_driver(). Taking the v3 side of it gives the same tree as in the last test above. b) Use v3 for the bind fix. I can resend patch 1/2 alone, so that it can go in before v3 3/3 or together with it. Once v3 is in mainline, I can propose the small bind fix as an equivalent backport for the affected stable trees. I would prefer a), but I am fine with either. Which do you prefer? I used an AI coding assistant to run these tests and to draft this reply. Thanks, Aristo ^ permalink raw reply [flat|nested] 6+ 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; 6+ 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] 6+ messages in thread
end of thread, other threads:[~2026-11-26 6:47 UTC | newest] Thread overview: 6+ 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 14:35 ` Alan Stern 2026-11-26 6:46 ` 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 an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.