Re: UBSAN whinge in ihci-hub.c

From: Alan Stern
Date: Wed May 18 2016 - 10:40:54 EST


On Wed, 18 May 2016, Andrey Ryabinin wrote:

> 2016-05-18 13:19 GMT+03:00 Oliver Neukum <oneukum@xxxxxxxx>:
> > On Wed, 2016-05-18 at 12:16 +0300, Andrey Ryabinin wrote:
> >> 2016-05-18 11:18 GMT+03:00 Oliver Neukum <oneukum@xxxxxxxx>:
> >> > On Wed, 2016-05-18 at 10:40 +0300, Andrey Ryabinin wrote:
> >> >> 2016-05-18 1:16 GMT+03:00 Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>:
> >> >> > On Tue, May 17, 2016 at 05:52:40PM -0400, Valdis Kletnieks wrote:
> >> >> >> So, not content in the amount of breakage I generate already, I
> >> >> >> compiled with UBSAN enabled...
> >> >> >>
> >> >> >> The immediately relevant part:
> >> >> >>
> >> >> >> [ 2.418576] ================================================================================
> >> >> >> [ 2.418579] UBSAN: Undefined behaviour in drivers/usb/host/ehci-hub.c:877:47
> >> >> >> [ 2.418582] index -1 is out of range for type 'u32 [1]'
> >> >> >
> >> >> > <snip>
> >> >> >
> >> >> > It's a known bug in ubsan,
> >> >>
> >> >> It's not a bug. int *p = &a[-1] is undefined behavior. It doesn't
> >> >> matter whether that pointer dereferenced or not.
> >> >
> >> > That is a bold statement. Pointer arithmetic is defined. How can
> >> > the computation of an address be undefined behavior while it is
> >> > not used?
> >>
> >> It's defined only if pointer points to array element or one-past-end
> >> element. Everything else is undefined.
> >>
> >> $ 6.5.6.8
> >> "If both the pointer operand and the result point to elements of
> >> the same array object,
> >> or one past the last element of the array object, the evaluation
> >> shall not produce an overflow;
> >> otherwise, the behavior is undefined."
> >
> > But we do not care whether the calculation overflows. We don't use it
> > at all in those cases.
> >
>
> This doesn't make it defined. Also that pointer is unused only if gcc
> doesn't optimize away '!wIndex' check.
> If it does, we may actually use it.

All right, I'm getting very tired of all these bug reports. Besides,
Andrey has a point: Unless you're Linus, arguing against the C standard
is futile. (Even though the language dialect used in the kernel is not
standard C.)

Does this patch make UBSAN happy? The runtime overhead is minimal.

Alan Stern



Index: usb-4.x/drivers/usb/host/ehci-hub.c
===================================================================
--- usb-4.x.orig/drivers/usb/host/ehci-hub.c
+++ usb-4.x/drivers/usb/host/ehci-hub.c
@@ -872,14 +872,17 @@ int ehci_hub_control(
) {
struct ehci_hcd *ehci = hcd_to_ehci (hcd);
int ports = HCS_N_PORTS (ehci->hcs_params);
- u32 __iomem *status_reg = &ehci->regs->port_status[
- (wIndex & 0xff) - 1];
- u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
+ u32 __iomem *status_reg, *hostpc_reg;
u32 temp, temp1, status;
unsigned long flags;
int retval = 0;
unsigned selector;

+ temp = wIndex & 0xff;
+ temp -= (temp > 0);
+ status_reg = &ehci->regs->port_status[temp];
+ hostpc_reg = &ehci->regs->hostpc[temp];
+
/*
* FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
* HCS_INDICATOR may say we can change LEDs to off/amber/green.