Skip to contentSkip to conversation

wifi: mt76: Replace zero-length array with flexible-array member

PATCH In review Gustavo A. R. Silva 3 participants 4 messages Apr 6, 2023 – Apr 17, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wifi: mt76: Replace zero-length array with flexible-array member

Zero-length arrays are deprecated [1] and have to be replaced by C99 flexible-array members.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help to make progress towards globally enabling -fstrict-flex-arrays=3 [2]

---
 drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h +1 -1
index a5e6ee4daf92..9bf4b4199ee3 100644
@@ -127,7 +127,7 @@ struct mt76_connac2_mcu_rxd {
 	u8 rsv1[2];
 	u8 s2d_index;
 
-	u8 tlv[0];
+	u8 tlv[];
 };
 
 struct mt76_connac2_patch_hdr {
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Gustavo A. R. Silva

Zero-length arrays are deprecated [1] and have to be replaced by C99 flexible-array members.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help to make progress towards globally enabling -fstrict-flex-arrays=3 [2]

Quoted 9 lines
---
 drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h +0 -0
index a5e6ee4daf92..9bf4b4199ee3 100644

drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h struct mt76_connac2_mcu_rxd { · line 133

Quoted 8 lines
drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h +1 -1
@@ -127,7 +127,7 @@ struct mt76_connac2_mcu_rxd {
 	u8 rsv1[2];
 	u8 s2d_index;
 
-	u8 tlv[0];
+	u8 tlv[];
 };
 
 struct mt76_connac2_patch_hdr {

Curiously -fstrict-flex-arrays=3 didn't flag this one in my environment, Ubuntu Lunar.

gcc-13 --version
gcc-13 (Ubuntu 13-20230320-1ubuntu1) 13.0.1 20230320 (experimental) [master r13-6759-g5194ad1958c] Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I did, however, notice some other problems reported by gcc-13 with -fstrict-flex-arrays=3 in drivers/net/wireless/mediatek/mt76 when run against net-next wireless. I've listed them in diff format below.

Are these on your radar too?

drivers/net/wireless/mediatek/mt76/mt7921/acpi_sar.h +5 -5
index 35268b0890ad..d09bb4eed1ec 100644
@@ -24,7 +24,7 @@ struct mt7921_asar_dyn {
 	u8 names[4];
 	u8 enable;
 	u8 nr_tbl;
-	struct mt7921_asar_dyn_limit tbl[0];
+	struct mt7921_asar_dyn_limit tbl[];
 } __packed;
 
 struct mt7921_asar_dyn_limit_v2 {
@@ -37,7 +37,7 @@ struct mt7921_asar_dyn_v2 {
 	u8 enable;
 	u8 rsvd;
 	u8 nr_tbl;
-	struct mt7921_asar_dyn_limit_v2 tbl[0];
+	struct mt7921_asar_dyn_limit_v2 tbl[];
 } __packed;
 
 struct mt7921_asar_geo_band {
@@ -55,7 +55,7 @@ struct mt7921_asar_geo {
 	u8 names[4];
 	u8 version;
 	u8 nr_tbl;
-	struct mt7921_asar_geo_limit tbl[0];
+	struct mt7921_asar_geo_limit tbl[];
 } __packed;
 
 struct mt7921_asar_geo_limit_v2 {
@@ -69,7 +69,7 @@ struct mt7921_asar_geo_v2 {
 	u8 version;
 	u8 rsvd;
 	u8 nr_tbl;
-	struct mt7921_asar_geo_limit_v2 tbl[0];
+	struct mt7921_asar_geo_limit_v2 tbl[];
 } __packed;
 
 struct mt7921_asar_cl {
@@ -85,7 +85,7 @@ struct mt7921_asar_fg {
 	u8 rsvd;
 	u8 nr_flag;
 	u8 rsvd1;
-	u8 flag[0];
+	u8 flag[];
 } __packed;
 
 struct mt7921_acpi_sar {
#2 lore raw
Quoted 8 lines · Simon Horman
Quoted 7 lines · Gustavo A. R. Silva

Zero-length arrays are deprecated [1] and have to be replaced by C99 flexible-array members.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help to make progress towards globally enabling -fstrict-flex-arrays=3 [2]

Quoted 5 lines
Quoted 3 lines

Thanks :)

Quoted 13 lines
Quoted 13 lines
---
  drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h +0 -0
index a5e6ee4daf92..9bf4b4199ee3 100644
@@ -127,7 +127,7 @@ struct mt76_connac2_mcu_rxd {
  	u8 rsv1[2];
  	u8 s2d_index;
 
Quoted 8 lines

drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h struct mt76_connac2_mcu_rxd { · line 133

Quoted 5 lines
drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h +1 -1
-	u8 tlv[0];
+	u8 tlv[];
  };
 
  struct mt76_connac2_patch_hdr {

Curiously -fstrict-flex-arrays=3 didn't flag this one in my environment, Ubuntu Lunar.

Yep; that's why I didn't include any warning message in the changelog text this time.

And the reason for that is that tlv is not being indexed anywhere in the code. However, it's being used in the pointer arithmetic operation below:

drivers/net/wireless/mediatek/mt76/mt7921/mcu.c:
164 rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
165 grant = (struct mt7921_roc_grant_tlv *)(rxd->tlv + 4);

which means that it can be considered as an array of size greater than zero at some point. Hence, it should be transformed into a C99 flexible array.

Quoted 7 lines

gcc-13 --version
gcc-13 (Ubuntu 13-20230320-1ubuntu1) 13.0.1 20230320 (experimental) [master r13-6759-g5194ad1958c] Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Quoted 5 lines

I did, however, notice some other problems reported by gcc-13 with -fstrict-flex-arrays=3 in drivers/net/wireless/mediatek/mt76 when run against net-next wireless. I've listed them in diff format below.

Are these on your radar too?

Yep; those are being addressed here:

https://lore.kernel.org/linux-hardening/ZBTUB%2FkJYQxq%2F6Cj@work/

--
Gustavo

Quoted 50 lines
drivers/net/wireless/mediatek/mt76/mt7921/acpi_sar.h +5 -5
index 35268b0890ad..d09bb4eed1ec 100644
@@ -24,7 +24,7 @@ struct mt7921_asar_dyn {
  	u8 names[4];
  	u8 enable;
  	u8 nr_tbl;
-	struct mt7921_asar_dyn_limit tbl[0];
+	struct mt7921_asar_dyn_limit tbl[];
  } __packed;
 
  struct mt7921_asar_dyn_limit_v2 {
@@ -37,7 +37,7 @@ struct mt7921_asar_dyn_v2 {
  	u8 enable;
  	u8 rsvd;
  	u8 nr_tbl;
-	struct mt7921_asar_dyn_limit_v2 tbl[0];
+	struct mt7921_asar_dyn_limit_v2 tbl[];
  } __packed;
 
  struct mt7921_asar_geo_band {
@@ -55,7 +55,7 @@ struct mt7921_asar_geo {
  	u8 names[4];
  	u8 version;
  	u8 nr_tbl;
-	struct mt7921_asar_geo_limit tbl[0];
+	struct mt7921_asar_geo_limit tbl[];
  } __packed;
 
  struct mt7921_asar_geo_limit_v2 {
@@ -69,7 +69,7 @@ struct mt7921_asar_geo_v2 {
  	u8 version;
  	u8 rsvd;
  	u8 nr_tbl;
-	struct mt7921_asar_geo_limit_v2 tbl[0];
+	struct mt7921_asar_geo_limit_v2 tbl[];
  } __packed;
 
  struct mt7921_asar_cl {
@@ -85,7 +85,7 @@ struct mt7921_asar_fg {
  	u8 rsvd;
  	u8 nr_flag;
  	u8 rsvd1;
-	u8 flag[0];
+	u8 flag[];
  } __packed;
 
  struct mt7921_acpi_sar {
#3 lore raw
Quoted 47 lines · Gustavo A. R. Silva

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 8 lines · Simon Horman
Quoted 7 lines · Gustavo A. R. Silva

Zero-length arrays are deprecated [1] and have to be replaced by C99 flexible-array members.

This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help to make progress towards globally enabling -fstrict-flex-arrays=3 [2]

Quoted 5 lines
Quoted 3 lines

Thanks :)

Quoted 13 lines
Quoted 13 lines
---
  drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h +0 -0
index a5e6ee4daf92..9bf4b4199ee3 100644
@@ -127,7 +127,7 @@ struct mt76_connac2_mcu_rxd {
     u8 rsv1[2];
     u8 s2d_index;
 
Quoted 8 lines

drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h struct mt76_connac2_mcu_rxd { · line 133

Quoted 5 lines
drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h +1 -1
-    u8 tlv[0];
+    u8 tlv[];
  };
 
  struct mt76_connac2_patch_hdr {

Curiously -fstrict-flex-arrays=3 didn't flag this one in my environment, Ubuntu Lunar.

Yep; that's why I didn't include any warning message in the changelog text this time.

And the reason for that is that tlv is not being indexed anywhere in the code. However, it's being used in the pointer arithmetic operation below:

Quoted 7 lines

drivers/net/wireless/mediatek/mt76/mt7921/mcu.c:
164 rxd = (struct mt76_connac2_mcu_rxd *)skb->data;
165 grant = (struct mt7921_roc_grant_tlv *)(rxd->tlv + 4);

which means that it can be considered as an array of size greater than zero at some point. Hence, it should be transformed into a C99 flexible array.

Understood, thanks for the explanation.

Quoted 10 lines
Quoted 10 lines

gcc-13 --version
gcc-13 (Ubuntu 13-20230320-1ubuntu1) 13.0.1 20230320 (experimental) [master r13-6759-g5194ad1958c] Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I did, however, notice some other problems reported by gcc-13 with -fstrict-flex-arrays=3 in drivers/net/wireless/mediatek/mt76 when run against net-next wireless. I've listed them in diff format below.

Quoted 5 lines
Quoted 1 line

Are these on your radar too?

Yep; those are being addressed here:

https://lore.kernel.org/linux-hardening/ZBTUB%2FkJYQxq%2F6Cj@work/

Thanks, I had forgotten about that.