Skip to contentSkip to conversation

ath6kl: wmi: Replace one-element arrays with flexible-array members

PATCH Superseded Gustavo A. R. Silva 3 participants 10 messages Feb 24, 2022 – Mar 9, 2022

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover ath6kl: wmi: Replace one-element arrays with flexible-array members

This series aims to replace one-element arrays with flexible-array members in multiple structures in drivers/net/wireless/ath/ath6kl/wmi.h

There is a regular need in the kernel to provide a way to declare having a dynamically sized set of trailing elements in a structure. Kernel code should always use “flexible array members”[1] for these cases. The older style of one-element or zero-length arrays should no longer be used[2].

This helps with the ongoing efforts to globally enable -Warray-bounds and get us closer to being able to tighten the FORTIFY_SOURCE routines on memcpy().

These issues were found with the help of Coccinelle and audited and fixed, manually.

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays

Changes in v2:
- Revert changes in if-statement logic for all the affected patches:
if (len < sizeof(struct foo))

- Update changelog texts.
- Add Reviewed-by: Jeff Johnson <[email protected]> tag.

Gustavo A. R. Silva (6):
ath6kl: wmi: Replace one-element array with flexible-array member in
struct wmi_begin_scan_cmd
ath6kl: wmi: Replace one-element array with flexible-array member in
struct wmi_start_scan_cmd
ath6kl: wmi: Replace one-element array with flexible-array member in
struct wmi_channel_list_reply
ath6kl: wmi: Replace one-element array with flexible-array member in
struct wmi_connect_event
ath6kl: wmi: Replace one-element array with flexible-array member in
struct wmi_disconnect_event
ath6kl: wmi: Replace one-element array with flexible-array member in
struct wmi_aplist_event

 drivers/net/wireless/ath/ath6kl/wmi.c | 22 ++++------------------
 drivers/net/wireless/ath/ath6kl/wmi.h | 12 ++++++------
 2 files changed, 10 insertions(+), 24 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v2 1/6 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_begin_scan_cmd

Replace one-element array with flexible-array member in struct wmi_begin_scan_cmd. Also, make use of the struct_size() helper.

This issue was found with the help of Coccinelle and audited and fixed, manually.

---
Changes in v2:
- Add Reviewed-by: Jeff Johnson <[email protected]> tag.

 drivers/net/wireless/ath/ath6kl/wmi.c | 9 ++-------
 drivers/net/wireless/ath/ath6kl/wmi.h | 2 +-
 2 files changed, 3 insertions(+), 8 deletions(-)
drivers/net/wireless/ath/ath6kl/wmi.c +2 -7
index bd1ef6334997..e1c950014f3e 100644
@@ -2008,7 +2008,7 @@ int ath6kl_wmi_beginscan_cmd(struct wmi *wmi, u8 if_idx,
 	struct ieee80211_supported_band *sband;
 	struct sk_buff *skb;
 	struct wmi_begin_scan_cmd *sc;
-	s8 size, *supp_rates;
+	s8 *supp_rates;
 	int i, band, ret;
 	struct ath6kl *ar = wmi->parent_dev;
 	int num_rates;
@@ -2023,18 +2023,13 @@ int ath6kl_wmi_beginscan_cmd(struct wmi *wmi, u8 if_idx,
 						num_chan, ch_list);
 	}
 
-	size = sizeof(struct wmi_begin_scan_cmd);
-
 	if ((scan_type != WMI_LONG_SCAN) && (scan_type != WMI_SHORT_SCAN))
 		return -EINVAL;
 
 	if (num_chan > WMI_MAX_CHANNELS)
 		return -EINVAL;
 
-	if (num_chan)
-		size += sizeof(u16) * (num_chan - 1);
-
-	skb = ath6kl_wmi_get_new_buf(size);
+	skb = ath6kl_wmi_get_new_buf(struct_size(sc, ch_list, num_chan));
 	if (!skb)
 		return -ENOMEM;
 
drivers/net/wireless/ath/ath6kl/wmi.h +1 -1
index 784940ba4c90..322539ed9c12 100644
@@ -863,7 +863,7 @@ struct wmi_begin_scan_cmd {
 	u8 num_ch;
 
 	/* channels in Mhz */
-	__le16 ch_list[1];
+	__le16 ch_list[];
 } __packed;
 
 /* wmi_start_scan_cmd is to be deprecated. Use
Signature · 1 line
2.27.0
#3 lore raw

PATCH v2 2/6 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_start_scan_cmd

In reply to Gustavo A. R. Silva

Replace one-element array with flexible-array member in struct wmi_start_scan_cmd. Also, make use of the struct_size() helper.

This issue was found with the help of Coccinelle and audited and fixed, manually.

---
Changes in v2:
- None.

 drivers/net/wireless/ath/ath6kl/wmi.c | 8 +-------
 drivers/net/wireless/ath/ath6kl/wmi.h | 2 +-
 2 files changed, 2 insertions(+), 8 deletions(-)
drivers/net/wireless/ath/ath6kl/wmi.c +1 -7
index e1c950014f3e..bdfc057c5a82 100644
@@ -1959,21 +1959,15 @@ static int ath6kl_wmi_startscan_cmd(struct wmi *wmi, u8 if_idx,
 {
 	struct sk_buff *skb;
 	struct wmi_start_scan_cmd *sc;
-	s8 size;
 	int i, ret;
 
-	size = sizeof(struct wmi_start_scan_cmd);
-
 	if ((scan_type != WMI_LONG_SCAN) && (scan_type != WMI_SHORT_SCAN))
 		return -EINVAL;
 
 	if (num_chan > WMI_MAX_CHANNELS)
 		return -EINVAL;
 
-	if (num_chan)
-		size += sizeof(u16) * (num_chan - 1);
-
-	skb = ath6kl_wmi_get_new_buf(size);
+	skb = ath6kl_wmi_get_new_buf(struct_size(sc, ch_list, num_chan));
 	if (!skb)
 		return -ENOMEM;
 
drivers/net/wireless/ath/ath6kl/wmi.h +1 -1
index 322539ed9c12..9e168752bec2 100644
@@ -889,7 +889,7 @@ struct wmi_start_scan_cmd {
 	u8 num_ch;
 
 	/* channels in Mhz */
-	__le16 ch_list[1];
+	__le16 ch_list[];
 } __packed;
 
 /*
Signature · 1 line
2.27.0
#6 lore raw

PATCH v2 3/6 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_channel_list_reply

In reply to Gustavo A. R. Silva

Replace one-element array with flexible-array member in struct wmi_channel_list_reply.

This issue was found with the help of Coccinelle and audited and fixed, manually.

---
Changes in v2:
- Revert changes in if-statement logic:
if (len < sizeof(struct wmi_channel_list_reply))

- Update changelog text.
- Add Reviewed-by: Jeff Johnson <[email protected]> tag.

 drivers/net/wireless/ath/ath6kl/wmi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath6kl/wmi.h +1 -1
index 9e168752bec2..432e4f428a4a 100644
@@ -1373,7 +1373,7 @@ struct wmi_channel_list_reply {
 	u8 num_ch;
 
 	/* channel in Mhz */
-	__le16 ch_list[1];
+	__le16 ch_list[];
 } __packed;
 
 /* List of Events (target to host) */
Signature · 1 line
2.27.0
#7 lore raw

PATCH v2 4/6 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_connect_event

In reply to Gustavo A. R. Silva

Replace one-element array with flexible-array member in struct wmi_connect_event.

This issue was found with the help of Coccinelle and audited and fixed, manually.

---
Changes in v2:
- Revert changes in if-statement logic:
if (len < sizeof(struct wmi_connect_event))

- Update changelog text.
- Add Reviewed-by: Jeff Johnson <[email protected]> tag.

 drivers/net/wireless/ath/ath6kl/wmi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath6kl/wmi.h +1 -1
index 432e4f428a4a..6b064e669d87 100644
@@ -1545,7 +1545,7 @@ struct wmi_connect_event {
 	u8 beacon_ie_len;
 	u8 assoc_req_len;
 	u8 assoc_resp_len;
-	u8 assoc_info[1];
+	u8 assoc_info[];
 } __packed;
 
 /* Disconnect Event */
Signature · 1 line
2.27.0
#8 lore raw

PATCH v2 5/6 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_disconnect_event

In reply to Gustavo A. R. Silva

Replace one-element array with flexible-array member in struct wmi_disconnect_event.

This issue was found with the help of Coccinelle and audited and fixed, manually.

---
Changes in v2:
- Revert changes in if-statement logic:
if (len < sizeof(struct wmi_disconnect_event))

- Update changelog text.
- Add Reviewed-by: Jeff Johnson <[email protected]> tag.

 drivers/net/wireless/ath/ath6kl/wmi.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath6kl/wmi.h +1 -1
index 6b064e669d87..6a7fc07cd9aa 100644
@@ -1596,7 +1596,7 @@ struct wmi_disconnect_event {
 	u8 disconn_reason;
 
 	u8 assoc_resp_len;
-	u8 assoc_info[1];
+	u8 assoc_info[];
 } __packed;
 
 /*
Signature · 1 line
2.27.0
#9 lore raw

PATCH v2 6/6 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_aplist_event

In reply to Gustavo A. R. Silva

Replace one-element array with flexible-array member in struct wmi_aplist_event.

Also, make use of the struct_size() helper and remove unneeded variable ap_info_entry_size.

This issue was found with the help of Coccinelle and audited and fixed, manually.

---
Changes in v2:
- Revert changes in if-statement logic:
if (len < sizeof(struct wmi_aplist_event))

- Update changelog text.
- Add Reviewed-by: Jeff Johnson <[email protected]> tag.

 drivers/net/wireless/ath/ath6kl/wmi.c | 5 +----
 drivers/net/wireless/ath/ath6kl/wmi.h | 2 +-
 2 files changed, 2 insertions(+), 5 deletions(-)
drivers/net/wireless/ath/ath6kl/wmi.c +1 -4
index bdfc057c5a82..3787b9fb0075 100644
@@ -1750,7 +1750,6 @@ static int ath6kl_wmi_snr_threshold_event_rx(struct wmi *wmi, u8 *datap,
 
 static int ath6kl_wmi_aplist_event_rx(struct wmi *wmi, u8 *datap, int len)
 {
-	u16 ap_info_entry_size;
 	struct wmi_aplist_event *ev = (struct wmi_aplist_event *) datap;
 	struct wmi_ap_info_v1 *ap_info_v1;
 	u8 index;
@@ -1759,14 +1758,12 @@ static int ath6kl_wmi_aplist_event_rx(struct wmi *wmi, u8 *datap, int len)
 	    ev->ap_list_ver != APLIST_VER1)
 		return -EINVAL;
 
-	ap_info_entry_size = sizeof(struct wmi_ap_info_v1);
 	ap_info_v1 = (struct wmi_ap_info_v1 *) ev->ap_list;
 
 	ath6kl_dbg(ATH6KL_DBG_WMI,
 		   "number of APs in aplist event: %d\n", ev->num_ap);
 
-	if (len < (int) (sizeof(struct wmi_aplist_event) +
-			 (ev->num_ap - 1) * ap_info_entry_size))
+	if (len < struct_size(ev, ap_list, ev->num_ap))
 		return -EINVAL;
 
 	/* AP list version 1 contents */
drivers/net/wireless/ath/ath6kl/wmi.h +1 -1
index 6a7fc07cd9aa..a9732660192a 100644
@@ -1957,7 +1957,7 @@ union wmi_ap_info {
 struct wmi_aplist_event {
 	u8 ap_list_ver;
 	u8 num_ap;
-	union wmi_ap_info ap_list[1];
+	union wmi_ap_info ap_list[];
 } __packed;
 
 /* Developer Commands */
Signature · 1 line
2.27.0
Reviewed-by on v2 2/6
#4 lore raw

Re: [PATCH v2 2/6][next] ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_start_scan_cmd

In reply to Gustavo A. R. Silva
Quoted 49 lines · Gustavo A. R. Silva

Replace one-element array with flexible-array member in struct wmi_start_scan_cmd. Also, make use of the struct_size() helper.

This issue was found with the help of Coccinelle and audited and fixed, manually.

---
Changes in v2:
- None.

  drivers/net/wireless/ath/ath6kl/wmi.c | 8 +-------
  drivers/net/wireless/ath/ath6kl/wmi.h | 2 +-
  2 files changed, 2 insertions(+), 8 deletions(-)
drivers/net/wireless/ath/ath6kl/wmi.c +1 -7
index e1c950014f3e..bdfc057c5a82 100644
@@ -1959,21 +1959,15 @@ static int ath6kl_wmi_startscan_cmd(struct wmi *wmi, u8 if_idx,
  {
  	struct sk_buff *skb;
  	struct wmi_start_scan_cmd *sc;
-	s8 size;
  	int i, ret;
 
-	size = sizeof(struct wmi_start_scan_cmd);
-
  	if ((scan_type != WMI_LONG_SCAN) && (scan_type != WMI_SHORT_SCAN))
  		return -EINVAL;
 
  	if (num_chan > WMI_MAX_CHANNELS)
  		return -EINVAL;
 
-	if (num_chan)
-		size += sizeof(u16) * (num_chan - 1);
-
-	skb = ath6kl_wmi_get_new_buf(size);
+	skb = ath6kl_wmi_get_new_buf(struct_size(sc, ch_list, num_chan));
  	if (!skb)
  		return -ENOMEM;
 
drivers/net/wireless/ath/ath6kl/wmi.h +0 -0
index 322539ed9c12..9e168752bec2 100644

drivers/net/wireless/ath/ath6kl/wmi.h struct wmi_start_scan_cmd { · line 895

Quoted 8 lines
drivers/net/wireless/ath/ath6kl/wmi.h +1 -1
@@ -889,7 +889,7 @@ struct wmi_start_scan_cmd {
  	u8 num_ch;
 
  	/* channels in Mhz */
-	__le16 ch_list[1];
+	__le16 ch_list[];
  } __packed;
 
  /*

my e-mail client hung while reviewing v1, so now giving

#5 lore raw

Re: [PATCH v2 2/6][next] ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_start_scan_cmd

On Thu, Feb 24, 2022 at 03:35:07PM -0800, Jeff Johnson wrote:

[..]

Quoted 4 lines

my e-mail client hung while reviewing v1, so now giving

Thanks, Jeff.

--
Gustavo

#2 lore raw

Re: [PATCH v2 1/6][next] ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_begin_scan_cmd

In reply to Gustavo A. R. Silva
Quoted 11 lines · Gustavo A. R. Silva

Replace one-element array with flexible-array member in struct wmi_begin_scan_cmd. Also, make use of the struct_size() helper.

This issue was found with the help of Coccinelle and audited and fixed, manually.

6 patches applied to ath-next branch of ath.git, thanks.

324edddf2505 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_begin_scan_cmd 56f1257fdcc0 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_start_scan_cmd 3c5e6994eea3 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_channel_list_reply dfb0203939b1 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_connect_event 5140df50e655 ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_disconnect_event 0dff6f05a9dc ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_aplist_event

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/1ef801ea24475501fa0f296cb5435a440135206e.1645736204.git.gustavoars@kernel.org/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches