Skip to contentSkip to conversation

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

PATCH Superseded Li Jun 2 participants 3 messages Jun 16, 2021 – Jun 18, 2021

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li Jun

author
#0 lore raw

PATCH 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")

---
 drivers/usb/chipidea/host.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/usb/chipidea/host.c +5 -3
index e86d13c04bdb..25327b1b49b7 100644
@@ -241,14 +241,16 @@ static int ci_ehci_hub_control(
 {
 	struct ehci_hcd	*ehci = hcd_to_ehci(hcd);
 	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);
 
@@ -288,7 +290,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
#1 lore raw
Quoted 35 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")

---
 drivers/usb/chipidea/host.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/usb/chipidea/host.c +4 -2
index e86d13c04bdb..25327b1b49b7 100644
@@ -241,14 +241,16 @@ static int ci_ehci_hub_control(
 {
 	struct ehci_hcd	*ehci = hcd_to_ehci(hcd);
 	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);
 

drivers/usb/chipidea/host.c static int ci_ehci_hub_control( · line 295

Quoted 7 lines
drivers/usb/chipidea/host.c +1 -1
@@ -288,7 +290,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;
 	}

Does this code test anywhere to ensure that wIndex > 0 and wIndex <= number of ports?

Alan Stern

author
#2 lore raw
Quoted 50 lines

-----Original Message-----
From: Alan Stern <[email protected]>
Sent: Wednesday, June 16, 2021 11:55 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] usb: chipidea: host: fix port index underflow and UBSAN complains

Quoted 37 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")

---
 drivers/usb/chipidea/host.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/usb/chipidea/host.c +4 -2
index e86d13c04bdb..25327b1b49b7 100644
@@ -241,14 +241,16 @@ static int ci_ehci_hub_control(  {
 	struct ehci_hcd	*ehci = hcd_to_ehci(hcd);
 	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);
 
@@ -288,7 +290,7 @@ static int ci_ehci_hub_control(
 			ehci_writel(ehci, temp, status_reg);
 		}
 
Quoted 7 lines

drivers/usb/chipidea/host.c static int ci_ehci_hub_control( · line 295

Quoted 4 lines
drivers/usb/chipidea/host.c +1 -1
-		set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
+		set_bit(port_index, &ehci->suspended_ports);
 		goto done;
 	}

Does this code test anywhere to ensure that wIndex > 0 and wIndex <= number of ports?

Missed that, thanks for pointing it out, will add it in v2.

Li Jun

Quoted 2 lines

Alan Stern