Skip to contentSkip to conversation

usb: gadget: udc-xilinx: validate ep number before indexing rambase[]

PATCH In review Sergio Perez Gonzalez 2 participants 4 messages Jun 27, 2025 – Jul 1, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: udc-xilinx: validate ep number before indexing rambase[]

Issue flagged by coverity. The size of the rambase array is 8, usb_enpoint_num() can return 0 to 15, prevent out of bounds reads.

---
 drivers/usb/gadget/udc/udc-xilinx.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/usb/gadget/udc/udc-xilinx.c +6 -0
index 8d803a612bb1..0c3714de2e3b 100644
@@ -814,6 +814,12 @@ static int __xudc_ep_enable(struct xusb_ep *ep,
 	ep->is_in = ((desc->bEndpointAddress & USB_DIR_IN) != 0);
 	/* Bit 3...0:endpoint number */
 	ep->epnumber = usb_endpoint_num(desc);
+	if (ep->epnumber >= XUSB_MAX_ENDPOINTS) {
+		dev_dbg(udc->dev, "bad endpoint index %d: only 0 to %d supported\n",
+				ep->epnumber, (XUSB_MAX_ENDPOINTS - 1));
+		return -EINVAL;
+	}
+
 	ep->desc = desc;
 	ep->ep_usb.desc = desc;
 	tmp = usb_endpoint_type(desc);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 2 lines · Sergio Perez Gonzalez

Issue flagged by coverity. The size of the rambase array is 8, usb_enpoint_num() can return 0 to 15, prevent out of bounds reads.

But how can that happen with this hardware? As the array states, this hardware only has that many endpoints availble to it, so how can it ever be larger?

Quoted 2 lines

What commit id does this fix?

Quoted 9 lines
---
 drivers/usb/gadget/udc/udc-xilinx.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/usb/gadget/udc/udc-xilinx.c +0 -0
index 8d803a612bb1..0c3714de2e3b 100644

drivers/usb/gadget/udc/udc-xilinx.c static int __xudc_ep_enable(struct xusb_ep *ep, · line 820

Quoted 7 lines
drivers/usb/gadget/udc/udc-xilinx.c +4 -0
@@ -814,6 +814,12 @@ static int __xudc_ep_enable(struct xusb_ep *ep,
 	ep->is_in = ((desc->bEndpointAddress & USB_DIR_IN) != 0);
 	/* Bit 3...0:endpoint number */
 	ep->epnumber = usb_endpoint_num(desc);
+	if (ep->epnumber >= XUSB_MAX_ENDPOINTS) {
+		dev_dbg(udc->dev, "bad endpoint index %d: only 0 to %d supported\n",
+				ep->epnumber, (XUSB_MAX_ENDPOINTS - 1));
+		return -EINVAL;

Any hints as to how this was tested?

thanks,

greg k-h

#2 lore raw
Quoted 8 lines · Sergio Perez Gonzalez
Quoted 2 lines

Issue flagged by coverity. The size of the rambase array is 8, usb_enpoint_num() can return 0 to 15, prevent out of bounds reads.

But how can that happen with this hardware? As the array states, this hardware only has that many endpoints availble to it, so how can it ever be larger?

Hardware will likely behave and not report more endpoints than it supports, but I thought that there is still a possibility that this can be exploited, taking into account this patch:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7f14c7227f342d9932f9b918893c8814f86d2a0d

and this CVE:
https://www.cvedetails.com/cve/CVE-2022-27223/

However, looking more closely the above patch, the endpoint number is extracted from a struct different than the "usb_endpoint_descriptor":
"epnum = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;"
in contrast with the code that I'm touching. The CVE does not add more details to understand if the part of the code that I'm changing is not subject to the vulnerability.

Quoted 4 lines
Quoted 2 lines

What commit id does this fix?

The last commit that touches this code is : fd2f928a5f7bc2f9588 ("usb:
gadget: udc-xilinx: Use USB API functions rather than constants") , although, I think the previous code gives functionally the same behavior.

Quoted 6 lines
Quoted 6 lines

---
 drivers/usb/gadget/udc/udc-xilinx.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 14 lines

drivers/usb/gadget/udc/udc-xilinx.c static int __xudc_ep_enable(struct xusb_ep *ep, · line 820

Quoted 12 lines
drivers/usb/gadget/udc/udc-xilinx.c +4 -0
index 8d803a612bb1..0c3714de2e3b 100644
@@ -814,6 +814,12 @@ static int __xudc_ep_enable(struct xusb_ep *ep,
      ep->is_in = ((desc->bEndpointAddress & USB_DIR_IN) != 0);
      /* Bit 3...0:endpoint number */
      ep->epnumber = usb_endpoint_num(desc);
+     if (ep->epnumber >= XUSB_MAX_ENDPOINTS) {
+             dev_dbg(udc->dev, "bad endpoint index %d: only 0 to %d supported\n",
+                             ep->epnumber, (XUSB_MAX_ENDPOINTS - 1));
+             return -EINVAL;

Any hints as to how this was tested?

I don't have access to such xilinx hardware, given that it was marked as a high severity defect in coverity and it is basically extending a validation that was already added in other parts of the code, I decided to propose the patch without runtime testing.

Thanks,
Sergio

Quoted 4 lines

thanks,

greg k-h

maintainer
#3 lore raw
Quoted 9 lines · Sergio Pérez
Quoted 8 lines · Sergio Perez Gonzalez
Quoted 2 lines

Issue flagged by coverity. The size of the rambase array is 8, usb_enpoint_num() can return 0 to 15, prevent out of bounds reads.

But how can that happen with this hardware? As the array states, this hardware only has that many endpoints availble to it, so how can it ever be larger?

Quoted 4 lines

Hardware will likely behave and not report more endpoints than it supports, but I thought that there is still a possibility that this can be exploited, taking into account this patch:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7f14c7227f342d9932f9b918893c8814f86d2a0d

That patch is probably not needed, for this same reason.

Quoted 2 lines

and this CVE:
https://www.cvedetails.com/cve/CVE-2022-27223/

Odds are we should reject that CVE, want me to go do that?

Quoted 6 lines

However, looking more closely the above patch, the endpoint number is extracted from a struct different than the "usb_endpoint_descriptor":
"epnum = udc->setup.wIndex & USB_ENDPOINT_NUMBER_MASK;"
in contrast with the code that I'm touching. The CVE does not add more details to understand if the part of the code that I'm changing is not subject to the vulnerability.

Please dig deeper to determine this :)

Quoted 19 lines
Quoted 4 lines
Quoted 4 lines
---
 drivers/usb/gadget/udc/udc-xilinx.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 14 lines

drivers/usb/gadget/udc/udc-xilinx.c static int __xudc_ep_enable(struct xusb_ep *ep, · line 820

Quoted 12 lines
drivers/usb/gadget/udc/udc-xilinx.c +4 -0
index 8d803a612bb1..0c3714de2e3b 100644
@@ -814,6 +814,12 @@ static int __xudc_ep_enable(struct xusb_ep *ep,
      ep->is_in = ((desc->bEndpointAddress & USB_DIR_IN) != 0);
      /* Bit 3...0:endpoint number */
      ep->epnumber = usb_endpoint_num(desc);
+     if (ep->epnumber >= XUSB_MAX_ENDPOINTS) {
+             dev_dbg(udc->dev, "bad endpoint index %d: only 0 to %d supported\n",
+                             ep->epnumber, (XUSB_MAX_ENDPOINTS - 1));
+             return -EINVAL;

Any hints as to how this was tested?

Quoted 4 lines

I don't have access to such xilinx hardware, given that it was marked as a high severity defect in coverity and it is basically extending a validation that was already added in other parts of the code, I decided to propose the patch without runtime testing.

Never trust static analysis tools blindly. The number of false-positives stuff like coverity creates because it can not determine where data actually comes from is way too high.

thanks,

greg k-h