Skip to contentSkip to conversation

usb: ch9: Replace bmSublinkSpeedAttr 1-element array with flexible array

PATCH In review Kees Cook 4 participants 6 messages Jun 29, 2023 – Jul 6, 2023 USB

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 usb: ch9: Replace bmSublinkSpeedAttr 1-element array with flexible array

Since commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3"), UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking bmSublinkSpeedAttr will trigger a warning, so make it a proper flexible array. Add a union to keep the struct size identical for userspace in case anything was depending on the old size.

False positive warning was:

UBSAN: array-index-out-of-bounds in drivers/usb/host/xhci-hub.c:231:31 index 1 is out of range for type '__le32 [1]'

for this line of code:

	ssp_cap->bmSublinkSpeedAttr[offset++] = cpu_to_le32(attr);
---
 include/uapi/linux/usb/ch9.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
include/uapi/linux/usb/ch9.h +5 -1
index b17e3a21b15f..3ff98c7ba7e3 100644
@@ -981,7 +981,11 @@ struct usb_ssp_cap_descriptor {
 #define USB_SSP_MIN_RX_LANE_COUNT		(0xf << 8)
 #define USB_SSP_MIN_TX_LANE_COUNT		(0xf << 12)
 	__le16 wReserved;
-	__le32 bmSublinkSpeedAttr[1]; /* list of sublink speed attrib entries */
+	union {
+		__le32 legacy_padding;
+		/* list of sublink speed attrib entries */
+		__DECLARE_FLEX_ARRAY(__le32, bmSublinkSpeedAttr);
+	};
 #define USB_SSP_SUBLINK_SPEED_SSID	(0xf)		/* sublink speed ID */
 #define USB_SSP_SUBLINK_SPEED_LSE	(0x3 << 4)	/* Lanespeed exponent */
 #define USB_SSP_SUBLINK_SPEED_LSE_BPS		0
Signature · 1 line
2.34.1
#1 lore raw
Quoted 14 lines · Kees Cook

Since commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3"), UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking bmSublinkSpeedAttr will trigger a warning, so make it a proper flexible array. Add a union to keep the struct size identical for userspace in case anything was depending on the old size.

False positive warning was:

UBSAN: array-index-out-of-bounds in drivers/usb/host/xhci-hub.c:231:31 index 1 is out of range for type '__le32 [1]'

for this line of code:

	ssp_cap->bmSublinkSpeedAttr[offset++] = cpu_to_le32(attr);
Quoted 8 lines
---
 include/uapi/linux/usb/ch9.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Thanks for the quick response, I'll queue it up after 6.5-rc1 is out.

Boris, can you test if this fixes the issue you see or not?

greg k-h

author
#3 lore raw
In reply to Greg Kroah-Hartman
Quoted 24 lines · Greg Kroah-Hartman
Quoted 14 lines · Kees Cook

Since commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3"), UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking bmSublinkSpeedAttr will trigger a warning, so make it a proper flexible array. Add a union to keep the struct size identical for userspace in case anything was depending on the old size.

False positive warning was:

UBSAN: array-index-out-of-bounds in drivers/usb/host/xhci-hub.c:231:31 index 1 is out of range for type '__le32 [1]'

for this line of code:

	ssp_cap->bmSublinkSpeedAttr[offset++] = cpu_to_le32(attr);
Quoted 8 lines
---
 include/uapi/linux/usb/ch9.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 1 line

Thanks for the quick response, I'll queue it up after 6.5-rc1 is out.

I'm going to send this before -rc1, since we've had another report[1] that was fixed by this. Given the verification there and Boris's testing, I think this is good to land. I'll toss it in -next now and send it to Linus on Friday after making sure there are no more surprises.

-Kees

[1] https://lore.kernel.org/lkml/[email protected]/

Signature · 1 line
Kees Cook
maintainer Reviewed-by
#4 lore raw
Quoted 27 lines · Kees Cook
Quoted 24 lines · Greg Kroah-Hartman
Quoted 14 lines · Kees Cook

Since commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3"), UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking bmSublinkSpeedAttr will trigger a warning, so make it a proper flexible array. Add a union to keep the struct size identical for userspace in case anything was depending on the old size.

False positive warning was:

UBSAN: array-index-out-of-bounds in drivers/usb/host/xhci-hub.c:231:31 index 1 is out of range for type '__le32 [1]'

for this line of code:

	ssp_cap->bmSublinkSpeedAttr[offset++] = cpu_to_le32(attr);
Quoted 8 lines
---
 include/uapi/linux/usb/ch9.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
Quoted 1 line

Thanks for the quick response, I'll queue it up after 6.5-rc1 is out.

Quoted 8 lines

I'm going to send this before -rc1, since we've had another report[1] that was fixed by this. Given the verification there and Boris's testing, I think this is good to land. I'll toss it in -next now and send it to Linus on Friday after making sure there are no more surprises.

-Kees

[1] https://lore.kernel.org/lkml/[email protected]/

No problem:

Reviewed-by on v1 1/1
#5 lore raw
In reply to Kees Cook
Quoted 12 lines · Kees Cook

Since commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3"), UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking bmSublinkSpeedAttr will trigger a warning, so make it a proper flexible array. Add a union to keep the struct size identical for userspace in case anything was depending on the old size.

False positive warning was:

UBSAN: array-index-out-of-bounds in drivers/usb/host/xhci-hub.c:231:31 index 1 is out of range for type '__le32 [1]'

for this line of code:

Quoted 7 lines
	ssp_cap->bmSublinkSpeedAttr[offset++] = cpu_to_le32(attr);

Thanks!
--
Gustavo

Quoted 21 lines
---
  include/uapi/linux/usb/ch9.h | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
include/uapi/linux/usb/ch9.h +5 -1
index b17e3a21b15f..3ff98c7ba7e3 100644
@@ -981,7 +981,11 @@ struct usb_ssp_cap_descriptor {
  #define USB_SSP_MIN_RX_LANE_COUNT		(0xf << 8)
  #define USB_SSP_MIN_TX_LANE_COUNT		(0xf << 12)
  	__le16 wReserved;
-	__le32 bmSublinkSpeedAttr[1]; /* list of sublink speed attrib entries */
+	union {
+		__le32 legacy_padding;
+		/* list of sublink speed attrib entries */
+		__DECLARE_FLEX_ARRAY(__le32, bmSublinkSpeedAttr);
+	};
  #define USB_SSP_SUBLINK_SPEED_SSID	(0xf)		/* sublink speed ID */
  #define USB_SSP_SUBLINK_SPEED_LSE	(0x3 << 4)	/* Lanespeed exponent */
  #define USB_SSP_SUBLINK_SPEED_LSE_BPS		0