usb: musb-new: Limit check array index before use
authorAndrew Goodbody <andrew.goodbody@linaro.org>
Tue, 30 Sep 2025 15:52:23 +0000 (16:52 +0100)
committerMarek Vasut <marek.vasut+usb@mailbox.org>
Tue, 28 Oct 2025 15:35:05 +0000 (16:35 +0100)
epnum is used as an index into an array. The limit check for this index
should be performed before using it to access an element in the array to
prevent possible bounds overrun.

This issue was found by Smatch.

Signed-off-by: Andrew Goodbody <andrew.goodbody@linaro.org>
Reviewed-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
drivers/usb/musb-new/musb_gadget_ep0.c

index ea65326..25b1de6 100644 (file)
@@ -96,6 +96,9 @@ static int service_tx_status_request(
                if (!epnum) {
                        result[0] = 0;
                        break;
+               } else if (epnum >= MUSB_C_NUM_EPS) {
+                       handled = -EINVAL;
+                       break;
                }
 
                is_in = epnum & USB_DIR_IN;
@@ -107,7 +110,7 @@ static int service_tx_status_request(
                }
                regs = musb->endpoints[epnum].regs;
 
-               if (epnum >= MUSB_C_NUM_EPS || !ep->desc) {
+               if (!ep->desc) {
                        handled = -EINVAL;
                        break;
                }