Skip to contentSkip to conversation

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

PATCH Superseded Kees Cook 3 participants 4 messages Jun 13, 2023 – Jun 14, 2023 USB

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

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

With "-fstrict-flex-arrays=3" enabled, UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking wData 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.

Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Greg Kroah-Hartman <[email protected]>
Cc kernel test robot <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Dan Williams <[email protected]>
Cc "Jó Ágila Bitsch" <[email protected]>
---
 include/uapi/linux/usb/ch9.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/uapi/linux/usb/ch9.h +4 -1
index b17e3a21b15f..82ec6af71a1d 100644
@@ -376,7 +376,10 @@ struct usb_string_descriptor {
 	__u8  bLength;
 	__u8  bDescriptorType;
 
-	__le16 wData[1];		/* UTF-16LE encoded */
+	union {
+		__le16 legacy_padding;
+		__DECLARE_FLEX_ARRAY(__le16, wData);	/* UTF-16LE encoded */
+	};
 } __attribute__ ((packed));
 
 /* note that "string" zero is special, it holds language codes that
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Kees Cook

With "-fstrict-flex-arrays=3" enabled, UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking wData 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.

Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")

I always have mixed feelings about a 'Fixes' tag applied to a commit like this (one that enables a compiler option that avoids the introduction of buggy code), when we are addressing the potentially buggy code that the option is inteded to prevent. (thinkingface)

Quoted 6 lines
Cc Greg Kroah-Hartman <[email protected]>
Cc kernel test robot <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Dan Williams <[email protected]>
Cc "Jó Ágila Bitsch" <[email protected]>

Thanks!
--
Gustavo

Quoted 23 lines
---
 include/uapi/linux/usb/ch9.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/uapi/linux/usb/ch9.h +4 -1
index b17e3a21b15f..82ec6af71a1d 100644
@@ -376,7 +376,10 @@ struct usb_string_descriptor {
 	__u8  bLength;
 	__u8  bDescriptorType;
 
-	__le16 wData[1];		/* UTF-16LE encoded */
+	union {
+		__le16 legacy_padding;
+		__DECLARE_FLEX_ARRAY(__le16, wData);	/* UTF-16LE encoded */
+	};
 } __attribute__ ((packed));
 
 /* note that "string" zero is special, it holds language codes that
Signature · 1 line
2.34.1
#2 lore raw
Quoted 6 lines · Gustavo A. R. Silva
Quoted 5 lines · Kees Cook

With "-fstrict-flex-arrays=3" enabled, UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking wData 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.

Quoted 8 lines
Quoted 3 lines
Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")

I always have mixed feelings about a 'Fixes' tag applied to a commit like this (one that enables a compiler option that avoids the introduction of buggy code), when we are addressing the potentially buggy code that the option is inteded to prevent. (thinkingface)

Yeah, the original code here is not incorrect, it's that you added a new build warning, so this is more like an "update" :)

Quoted 8 lines
Quoted 6 lines
Cc Greg Kroah-Hartman <[email protected]>
Cc kernel test robot <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc Dan Williams <[email protected]>
Cc "Jó Ágila Bitsch" <[email protected]>

Odd that checkpatch.pl doesn't cc: the usb lists for this file. I'll go update the MAINTAINERS file with this location...

thanks,

greg k-h

author
#3 lore raw
In reply to Gustavo A. R. Silva
Quoted 6 lines · Gustavo A. R. Silva
Quoted 5 lines · Kees Cook

With "-fstrict-flex-arrays=3" enabled, UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking wData 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.

Quoted 8 lines
Quoted 3 lines
Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")

I always have mixed feelings about a 'Fixes' tag applied to a commit like this (one that enables a compiler option that avoids the introduction of buggy code), when we are addressing the potentially buggy code that the option is inteded to prevent. (thinkingface)

Yeah, I've been on the fence about this too. Since it's fixing a (modern) coding style issue, there's nothing wrong technically. i.e. I can't say "Fixes: ...usb commit..." since this isn't a bug. But it's fixing a warning introduced by the fstrict-flex-arrays=3, and tracking those issues is useful. But, it's not really fixing _that_ commit, as it's doing exactly what it should be doing. So, perhaps, in the future I can just mention it more directly in the commit log without a Fixes tag. For example, this should probably have been written as:

Since commit df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3"), UBSAN_BOUNDS no longer pretends 1-element arrays are unbounded. Walking wData 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.

https://lore.kernel.org/oe-lkp/[email protected]

-Kees

Signature · 1 line
Kees Cook