From: Aristo Chen <aristo.chen@canonical.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Neill Kapron <nkapron@google.com>
Cc: Alan Stern <stern@rowland.harvard.edu>,
Krishna Kurapati <krishna.kurapati@oss.qualcomm.com>,
Jassi Brar <jassisinghbrar@gmail.com>,
linux-usb@vger.kernel.org,
Aristo Chen <aristo.chen@canonical.com>,
stable@vger.kernel.org
Subject: [PATCH 1/2] usb: gadget: max3420_udc: fix out of bounds endpoint index from wIndex
Date: Wed, 25 Nov 2026 09:05:29 +0000 [thread overview]
Message-ID: <20261125092004.15562-2-aristo.chen@canonical.com> (raw)
In-Reply-To: <20261125092004.15562-1-aristo.chen@canonical.com>
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
next prev parent reply other threads:[~2026-11-25 9:20 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125092004.15562-2-aristo.chen@canonical.com \
--to=aristo.chen@canonical.com \
--cc=gregkh@linuxfoundation.org \
--cc=jassisinghbrar@gmail.com \
--cc=krishna.kurapati@oss.qualcomm.com \
--cc=linux-usb@vger.kernel.org \
--cc=nkapron@google.com \
--cc=stable@vger.kernel.org \
--cc=stern@rowland.harvard.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox