Commit 846cbf98 authored by Alan Stern's avatar Alan Stern Committed by Greg Kroah-Hartman

USB: EHCI: Improve port index sanitizing

Now that Kees Cook has added a definition for HCS_N_PORTS_MAX in
commit 72dd1843 ("USB: EHCI: Add register array bounds to HCS
ports"), the code in ehci_hub_control() which sanitizes port index
values can be improved a little.

The idea behind this change is that it prevents a possible
out-of-bounds pointer computation, which the compiler might be able to
detect since the port_status[] array now has a fixed length rather
than a variable length.
Signed-off-by: default avatarAlan Stern <stern@rowland.harvard.edu>
Link: https://lore.kernel.org/r/20211002190217.GA537967@rowland.harvard.eduSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent ef53d3db
...@@ -745,12 +745,13 @@ int ehci_hub_control( ...@@ -745,12 +745,13 @@ int ehci_hub_control(
unsigned selector; unsigned selector;
/* /*
* Avoid underflow while calculating (wIndex & 0xff) - 1. * Avoid out-of-bounds values while calculating the port index
* The compiler might deduce that wIndex can never be 0 and then * from wIndex. The compiler doesn't like pointers to invalid
* optimize away the tests for !wIndex below. * addresses, even if they are never used.
*/ */
temp = wIndex & 0xff; temp = (wIndex - 1) & 0xff;
temp -= (temp > 0); if (temp >= HCS_N_PORTS_MAX)
temp = 0;
status_reg = &ehci->regs->port_status[temp]; status_reg = &ehci->regs->port_status[temp];
hostpc_reg = &ehci->regs->hostpc[temp]; hostpc_reg = &ehci->regs->hostpc[temp];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment