Skip to contentSkip to conversation

usb: chipidea: host: fix port index underflow and UBSAN complains

PATCH Superseded Li Jun 2 participants 4 messages Jun 18, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li Jun

author
#0 lore raw

PATCH v2 1/1 usb: chipidea: host: fix port index underflow and UBSAN complains

If wIndex is 0 (and it often is), these calculations underflow and UBSAN complains, here resolve this by not decrementing the index when it is equal to 0, this copies the solution from commit 85e3990bea49 ("USB: EHCI: avoid undefined pointer arithmetic and placate UBSAN")

---
Change for v2:
- Add wIndex range check to ensure a correct port index value.

 drivers/usb/chipidea/host.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/usb/chipidea/host.c +11 -3
index e86d13c04bdb..bdc3885c0d49 100644
@@ -240,15 +240,18 @@ static int ci_ehci_hub_control(
 )
 {
 	struct ehci_hcd	*ehci = hcd_to_ehci(hcd);
+	unsigned int	ports = HCS_N_PORTS(ehci->hcs_params);
 	u32 __iomem	*status_reg;
-	u32		temp;
+	u32		temp, port_index;
 	unsigned long	flags;
 	int		retval = 0;
 	bool		done = false;
 	struct device *dev = hcd->self.controller;
 	struct ci_hdrc *ci = dev_get_drvdata(dev);
 
-	status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
+	port_index = wIndex & 0xff;
+	port_index -= (port_index > 0);
+	status_reg = &ehci->regs->port_status[port_index];
 
 	spin_lock_irqsave(&ehci->lock, flags);
 
@@ -260,6 +263,11 @@ static int ci_ehci_hub_control(
 	}
 
 	if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
+		if (!wIndex || wIndex > ports) {
+			retval = -EPIPE;
+			goto done;
+		}
+
 		temp = ehci_readl(ehci, status_reg);
 		if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
 			retval = -EPIPE;
@@ -288,7 +296,7 @@ static int ci_ehci_hub_control(
 			ehci_writel(ehci, temp, status_reg);
 		}
 
-		set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
+		set_bit(port_index, &ehci->suspended_ports);
 		goto done;
 	}
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 6 lines · Li Jun

If wIndex is 0 (and it often is), these calculations underflow and UBSAN complains, here resolve this by not decrementing the index when it is equal to 0, this copies the solution from commit 85e3990bea49 ("USB: EHCI: avoid undefined pointer arithmetic and placate UBSAN")

Please use a name here that makes sense, "Zhipeng Wang"?

thanks,

greg k-h

maintainer
#2 lore raw
Quoted 9 lines · Greg KH
Quoted 6 lines · Li Jun

If wIndex is 0 (and it often is), these calculations underflow and UBSAN complains, here resolve this by not decrementing the index when it is equal to 0, this copies the solution from commit 85e3990bea49 ("USB: EHCI: avoid undefined pointer arithmetic and placate UBSAN")

Please use a name here that makes sense, "Zhipeng Wang"?

Specifically, the "." in the name as written originally does not make sense, right?

author
#3 lore raw
Quoted 20 lines

-----Original Message-----
From: Greg KH <[email protected]>
Sent: Friday, June 18, 2021 3:58 PM
To: Jun Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; dl-linux-imx <[email protected]>; Zhipeng Wang <[email protected]>
Subject: Re: [PATCH v2] usb: chipidea: host: fix port index underflow and UBSAN complains

Quoted 7 lines · Greg KH
Quoted 6 lines · Li Jun

If wIndex is 0 (and it often is), these calculations underflow and UBSAN complains, here resolve this by not decrementing the index when it is equal to 0, this copies the solution from commit 85e3990bea49
("USB: EHCI: avoid undefined pointer arithmetic and placate UBSAN")

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Please use a name here that makes sense, "Zhipeng Wang"?

Specifically, the "." in the name as written originally does not make sense, right?

Yes, should be "Zhipeng Wang", I will change and send v3.

Thanks
Li Jun