[PATCH] usb: dwc2: gadget: fix off-by-one in ep_from_windex bounds check

From: Fang Xieyan

Date: Sun Oct 25 2026 - 23:13:25 EST


ep_from_windex() uses idx > hsotg->num_of_eps to validate the
endpoint index. Since valid indices are below num_of_eps, an index
equal to num_of_eps passes the check and is used to access
eps_in[] or eps_out[] out of bounds. This can cause an unintended
endpoint pointer to be used by subsequent request handling.

Reject indices greater than or equal to num_of_eps to prevent the
out-of-bounds access.

Fixes: b3f489b2b658 ("usb:hsotg:samsung: Determine number of EPs from HW configuration register")
Reviewed-by: Guanglei Zhu <zhugl3@xxxxxxxxxxxx>
Signed-off-by: Fang Xieyan <fangxy@xxxxxxxxxxxx>
---
drivers/usb/dwc2/gadget.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
index 0637bfbc0..b2a25cd5c 100644
--- a/drivers/usb/dwc2/gadget.c
+++ b/drivers/usb/dwc2/gadget.c
@@ -1582,7 +1582,7 @@ static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
if (windex >= 0x100)
return NULL;

- if (idx > hsotg->num_of_eps)
+ if (idx >= hsotg->num_of_eps)
return NULL;

return index_to_ep(hsotg, idx, dir);
--
2.50.1