2022-02-23 05:16:13

by Gustavo A. R. Silva

[permalink] [raw]
Subject: [PATCH 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.

It's also worth noting that due to the flexible array transformation,
the size of struct wmi_aplist_event changed (now the size is 8-byte
smaller), and in order to preserve the logic of before the transformation,
the following change is needed:

- if (len < sizeof(struct wmi_aplist_event))
+ if (len <= sizeof(struct wmi_aplist_event))

sizeof(struct wmi_aplist_event) before the flex-array transformation:

struct wmi_aplist_event {
u8 ap_list_ver; /* 0 1 */
u8 num_ap; /* 1 1 */
union wmi_ap_info ap_list[1]; /* 2 8 */

/* size: 10, cachelines: 1, members: 3 */
/* last cacheline: 10 bytes */
};

sizeof(struct wmi_aplist_event) after the flex-array transformation:

struct wmi_aplist_event {
u8 ap_list_ver; /* 0 1 */
u8 num_ap; /* 1 1 */
union wmi_ap_info ap_list[]; /* 2 0 */

/* size: 2, cachelines: 1, members: 3 */
/* last cacheline: 2 bytes */
};

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
Signed-off-by: Gustavo A. R. Silva <[email protected]>
---
Hi!

It'd be great if someone can confirm or comment on the following
changes described in the changelog text:

- if (len < sizeof(struct wmi_aplist_event))
+ if (len <= sizeof(struct wmi_aplist_event))

Thanks

drivers/net/wireless/ath/ath6kl/wmi.c | 7 ++-----
drivers/net/wireless/ath/ath6kl/wmi.h | 2 +-
2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/net/wireless/ath/ath6kl/wmi.c b/drivers/net/wireless/ath/ath6kl/wmi.c
index 645fb6cae3be..484d37e66ce6 100644
--- a/drivers/net/wireless/ath/ath6kl/wmi.c
+++ b/drivers/net/wireless/ath/ath6kl/wmi.c
@@ -1750,23 +1750,20 @@ 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;

- if (len < sizeof(struct wmi_aplist_event) ||
+ if (len <= sizeof(struct wmi_aplist_event) ||
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


2022-02-24 02:10:46

by Jeff Johnson

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

On 2/22/2022 6:39 PM, Gustavo A. R. Silva wrote:
> Replace one-element array with flexible-array member in struct
> wmi_aplist_event.
>
> It's also worth noting that due to the flexible array transformation,
> the size of struct wmi_aplist_event changed (now the size is 8-byte
> smaller), and in order to preserve the logic of before the transformation,
> the following change is needed:
>
> - if (len < sizeof(struct wmi_aplist_event))
> + if (len <= sizeof(struct wmi_aplist_event))
>
> sizeof(struct wmi_aplist_event) before the flex-array transformation:
>
> struct wmi_aplist_event {
> u8 ap_list_ver; /* 0 1 */
> u8 num_ap; /* 1 1 */
> union wmi_ap_info ap_list[1]; /* 2 8 */
>
> /* size: 10, cachelines: 1, members: 3 */
> /* last cacheline: 10 bytes */
> };
>
> sizeof(struct wmi_aplist_event) after the flex-array transformation:
>
> struct wmi_aplist_event {
> u8 ap_list_ver; /* 0 1 */
> u8 num_ap; /* 1 1 */
> union wmi_ap_info ap_list[]; /* 2 0 */
>
> /* size: 2, cachelines: 1, members: 3 */
> /* last cacheline: 2 bytes */
> };
>
> 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
> Signed-off-by: Gustavo A. R. Silva <[email protected]>
> ---
> Hi!
>
> It'd be great if someone can confirm or comment on the following
> changes described in the changelog text:
>
> - if (len < sizeof(struct wmi_aplist_event))
> + if (len <= sizeof(struct wmi_aplist_event))
>
> Thanks
>
> drivers/net/wireless/ath/ath6kl/wmi.c | 7 ++-----
> drivers/net/wireless/ath/ath6kl/wmi.h | 2 +-
> 2 files changed, 3 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/net/wireless/ath/ath6kl/wmi.c b/drivers/net/wireless/ath/ath6kl/wmi.c
> index 645fb6cae3be..484d37e66ce6 100644
> --- a/drivers/net/wireless/ath/ath6kl/wmi.c
> +++ b/drivers/net/wireless/ath/ath6kl/wmi.c
> @@ -1750,23 +1750,20 @@ 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;
>
> - if (len < sizeof(struct wmi_aplist_event) ||
> + if (len <= sizeof(struct wmi_aplist_event) ||

again IMO the original code is preferred since then we can handle a
0-length list

> 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))

and unlike the prior patches in this set, at least the original code
here had logic to validate len against the metadata that describes the
number of entries in the list. so this change is good, and also supports
a 0-length list

> 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 */

whether or not you modify the length check consider this:
Reviewed-by: Jeff Johnson <[email protected]>