2022-02-24 21:26:27

by Gustavo A. R. Silva

[permalink] [raw]
Subject: [PATCH v2 0/6][next] 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

Link: https://github.com/KSPP/linux/issues/79

Changes in v2:
- Revert changes in if-statement logic for all the affected patches:
if (len < sizeof(struct foo))
Link: https://lore.kernel.org/linux-hardening/[email protected]/
- 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(-)

--
2.27.0


2022-02-24 21:30:17

by Gustavo A. R. Silva

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

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.

Link: https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays
Link: https://github.com/KSPP/linux/issues/79
Signed-off-by: Gustavo A. R. Silva <[email protected]>
---
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(-)

diff --git a/drivers/net/wireless/ath/ath6kl/wmi.c b/drivers/net/wireless/ath/ath6kl/wmi.c
index e1c950014f3e..bdfc057c5a82 100644
--- a/drivers/net/wireless/ath/ath6kl/wmi.c
+++ b/drivers/net/wireless/ath/ath6kl/wmi.c
@@ -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;

diff --git a/drivers/net/wireless/ath/ath6kl/wmi.h b/drivers/net/wireless/ath/ath6kl/wmi.h
index 322539ed9c12..9e168752bec2 100644
--- a/drivers/net/wireless/ath/ath6kl/wmi.h
+++ b/drivers/net/wireless/ath/ath6kl/wmi.h
@@ -889,7 +889,7 @@ struct wmi_start_scan_cmd {
u8 num_ch;

/* channels in Mhz */
- __le16 ch_list[1];
+ __le16 ch_list[];
} __packed;

/*
--
2.27.0

2022-02-25 00:59:11

by Jeff Johnson

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

On 2/24/2022 1:16 PM, Gustavo A. R. Silva wrote:
> 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.
>
> Link: https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays
> Link: https://github.com/KSPP/linux/issues/79
> Signed-off-by: Gustavo A. R. Silva <[email protected]>
> ---
> 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(-)
>
> diff --git a/drivers/net/wireless/ath/ath6kl/wmi.c b/drivers/net/wireless/ath/ath6kl/wmi.c
> index e1c950014f3e..bdfc057c5a82 100644
> --- a/drivers/net/wireless/ath/ath6kl/wmi.c
> +++ b/drivers/net/wireless/ath/ath6kl/wmi.c
> @@ -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;
>
> diff --git a/drivers/net/wireless/ath/ath6kl/wmi.h b/drivers/net/wireless/ath/ath6kl/wmi.h
> index 322539ed9c12..9e168752bec2 100644
> --- a/drivers/net/wireless/ath/ath6kl/wmi.h
> +++ b/drivers/net/wireless/ath/ath6kl/wmi.h
> @@ -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

Reviewed-by: Jeff Johnson <[email protected]>

2022-02-25 01:00:18

by Gustavo A. R. Silva

[permalink] [raw]
Subject: [PATCH v2 6/6][next] ath6kl: wmi: Replace one-element array with flexible-array member in struct wmi_aplist_event

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.

Link: https://www.kernel.org/doc/html/v5.16/process/deprecated.html#zero-length-and-one-element-arrays
Link: https://github.com/KSPP/linux/issues/79
Reviewed-by: Jeff Johnson <[email protected]>
Signed-off-by: Gustavo A. R. Silva <[email protected]>
---
Changes in v2:
- Revert changes in if-statement logic:
if (len < sizeof(struct wmi_aplist_event))
Link: https://lore.kernel.org/linux-hardening/[email protected]/
- 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(-)

diff --git a/drivers/net/wireless/ath/ath6kl/wmi.c b/drivers/net/wireless/ath/ath6kl/wmi.c
index bdfc057c5a82..3787b9fb0075 100644
--- a/drivers/net/wireless/ath/ath6kl/wmi.c
+++ b/drivers/net/wireless/ath/ath6kl/wmi.c
@@ -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 */
diff --git a/drivers/net/wireless/ath/ath6kl/wmi.h b/drivers/net/wireless/ath/ath6kl/wmi.h
index 6a7fc07cd9aa..a9732660192a 100644
--- a/drivers/net/wireless/ath/ath6kl/wmi.h
+++ b/drivers/net/wireless/ath/ath6kl/wmi.h
@@ -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 */
--
2.27.0