linux-wireless.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan()
@ 2024-10-07 22:20 Alper Nebi Yasak
  2024-10-07 22:20 ` [PATCH v2 2/2] wifi: mwifiex: Annotate mwifiex_ie_types_wildcard_ssid_params with __counted_by() Alper Nebi Yasak
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Alper Nebi Yasak @ 2024-10-07 22:20 UTC (permalink / raw)
  To: linux-wireless, linux-kernel
  Cc: Francesco Dolcini, Johannes Berg, Kees Cook, Sascha Hauer,
	Dmitry Antipov, Gustavo A . R . Silva, Jeff Johnson, Brian Norris,
	David Lin, linux-hardening, Kalle Valo, Andy Shevchenko,
	Alper Nebi Yasak

Replace one-element array with a flexible-array member in `struct
mwifiex_ie_types_wildcard_ssid_params` to fix the following warning
on a MT8173 Chromebook (mt8173-elm-hana):

[  356.775250] ------------[ cut here ]------------
[  356.784543] memcpy: detected field-spanning write (size 6) of single field "wildcard_ssid_tlv->ssid" at drivers/net/wireless/marvell/mwifiex/scan.c:904 (size 1)
[  356.813403] WARNING: CPU: 3 PID: 742 at drivers/net/wireless/marvell/mwifiex/scan.c:904 mwifiex_scan_networks+0x4fc/0xf28 [mwifiex]

The "(size 6)" above is exactly the length of the SSID of the network
this device was connected to. The source of the warning looks like:

    ssid_len = user_scan_in->ssid_list[i].ssid_len;
    [...]
    memcpy(wildcard_ssid_tlv->ssid,
           user_scan_in->ssid_list[i].ssid, ssid_len);

There is a #define WILDCARD_SSID_TLV_MAX_SIZE that uses sizeof() on this
struct, but it already didn't account for the size of the one-element
array, so it doesn't need to be changed.

Fixes: 5e6e3a92b9a4 ("wireless: mwifiex: initial commit for Marvell mwifiex driver")
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
---

Changes in v2:
- Drop unnecessary +1 from scan.c #define WILDCARD_SSID_TLV_MAX_SIZE

v1: https://lore.kernel.org/lkml/20240917150938.843879-1-alpernebiyasak@gmail.com/

 drivers/net/wireless/marvell/mwifiex/fw.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/wireless/marvell/mwifiex/fw.h b/drivers/net/wireless/marvell/mwifiex/fw.h
index d03129d5d24e..4a96281792cc 100644
--- a/drivers/net/wireless/marvell/mwifiex/fw.h
+++ b/drivers/net/wireless/marvell/mwifiex/fw.h
@@ -875,7 +875,7 @@ struct mwifiex_ietypes_chanstats {
 struct mwifiex_ie_types_wildcard_ssid_params {
 	struct mwifiex_ie_types_header header;
 	u8 max_ssid_length;
-	u8 ssid[1];
+	u8 ssid[];
 } __packed;
 
 #define TSF_DATA_SIZE            8

base-commit: 58ca61c1a866bfdaa5e19fb19a2416764f847d75
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] wifi: mwifiex: Annotate mwifiex_ie_types_wildcard_ssid_params with __counted_by()
  2024-10-07 22:20 [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan() Alper Nebi Yasak
@ 2024-10-07 22:20 ` Alper Nebi Yasak
  2024-10-15 22:06   ` Brian Norris
  2024-10-15 21:34 ` [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan() Brian Norris
  2024-10-17 16:50 ` Kalle Valo
  2 siblings, 1 reply; 5+ messages in thread
From: Alper Nebi Yasak @ 2024-10-07 22:20 UTC (permalink / raw)
  To: linux-wireless, linux-kernel
  Cc: Francesco Dolcini, Johannes Berg, Kees Cook, Sascha Hauer,
	Dmitry Antipov, Gustavo A . R . Silva, Jeff Johnson, Brian Norris,
	David Lin, linux-hardening, Kalle Valo, Andy Shevchenko,
	Alper Nebi Yasak

Add the __counted_by compiler attribute to the flexible array member
`ssid` to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
CONFIG_FORTIFY_SOURCE.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
---
I've mimicked the commit messages from `git log -S"__counted_by("`.
Since they refer to UBSAN I tried testing with CONFIG_KASAN (w/
kasan_multi_shot), CONFIG_UBSAN and CONFIG_FORTIFY_SOURCE. I do not
get any errors relating to this module with those options, but have
others -- I even had to test on another board altogether.

This attribute was suggested in reviews, I don't fully understand it,
but I am not sure it is correct in the context of this comment from
scan.c (with irrelelvant parts omitted):

    ssid_len = user_scan_in->ssid_list[i].ssid_len;

    wildcard_ssid_tlv =
            (struct mwifiex_ie_types_wildcard_ssid_params *)
            tlv_pos;

    /*
     * max_ssid_length = 0 tells firmware to perform
     * specific scan for the SSID filled, whereas
     * max_ssid_length = IEEE80211_MAX_SSID_LEN is for
     * wildcard scan.
     */
    if (ssid_len)
            wildcard_ssid_tlv->max_ssid_length = 0;
    else
            wildcard_ssid_tlv->max_ssid_length =
                                    IEEE80211_MAX_SSID_LEN;

    memcpy(wildcard_ssid_tlv->ssid,
           user_scan_in->ssid_list[i].ssid, ssid_len);

I expect we want to use __counted_by(ssid_len) instead, but do not have
it in the struct. And max_ssid_length = 0 when ssid[] is non-empty, so
is it really appropriate as the __counted_by()? But then again, I
couldn't get this to produce a warning.

Changes in v2:
- Add patch to annotate ssid field with __counted_by(max_ssid_length)

 drivers/net/wireless/marvell/mwifiex/fw.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/wireless/marvell/mwifiex/fw.h b/drivers/net/wireless/marvell/mwifiex/fw.h
index 4a96281792cc..e4e35ba35454 100644
--- a/drivers/net/wireless/marvell/mwifiex/fw.h
+++ b/drivers/net/wireless/marvell/mwifiex/fw.h
@@ -875,7 +875,7 @@ struct mwifiex_ietypes_chanstats {
 struct mwifiex_ie_types_wildcard_ssid_params {
 	struct mwifiex_ie_types_header header;
 	u8 max_ssid_length;
-	u8 ssid[];
+	u8 ssid[] __counted_by(max_ssid_length);
 } __packed;
 
 #define TSF_DATA_SIZE            8
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan()
  2024-10-07 22:20 [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan() Alper Nebi Yasak
  2024-10-07 22:20 ` [PATCH v2 2/2] wifi: mwifiex: Annotate mwifiex_ie_types_wildcard_ssid_params with __counted_by() Alper Nebi Yasak
@ 2024-10-15 21:34 ` Brian Norris
  2024-10-17 16:50 ` Kalle Valo
  2 siblings, 0 replies; 5+ messages in thread
From: Brian Norris @ 2024-10-15 21:34 UTC (permalink / raw)
  To: Alper Nebi Yasak
  Cc: linux-wireless, linux-kernel, Francesco Dolcini, Johannes Berg,
	Kees Cook, Sascha Hauer, Dmitry Antipov, Gustavo A . R . Silva,
	Jeff Johnson, David Lin, linux-hardening, Kalle Valo,
	Andy Shevchenko

On Tue, Oct 08, 2024 at 01:20:54AM +0300, Alper Nebi Yasak wrote:
> Replace one-element array with a flexible-array member in `struct
> mwifiex_ie_types_wildcard_ssid_params` to fix the following warning
> on a MT8173 Chromebook (mt8173-elm-hana):
> 
> [  356.775250] ------------[ cut here ]------------
> [  356.784543] memcpy: detected field-spanning write (size 6) of single field "wildcard_ssid_tlv->ssid" at drivers/net/wireless/marvell/mwifiex/scan.c:904 (size 1)
> [  356.813403] WARNING: CPU: 3 PID: 742 at drivers/net/wireless/marvell/mwifiex/scan.c:904 mwifiex_scan_networks+0x4fc/0xf28 [mwifiex]
> 
> The "(size 6)" above is exactly the length of the SSID of the network
> this device was connected to. The source of the warning looks like:
> 
>     ssid_len = user_scan_in->ssid_list[i].ssid_len;
>     [...]
>     memcpy(wildcard_ssid_tlv->ssid,
>            user_scan_in->ssid_list[i].ssid, ssid_len);
> 
> There is a #define WILDCARD_SSID_TLV_MAX_SIZE that uses sizeof() on this
> struct, but it already didn't account for the size of the one-element
> array, so it doesn't need to be changed.
> 
> Fixes: 5e6e3a92b9a4 ("wireless: mwifiex: initial commit for Marvell mwifiex driver")
> Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>

For patch 1:

Acked-by: Brian Norris <briannorris@chromium.org>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] wifi: mwifiex: Annotate mwifiex_ie_types_wildcard_ssid_params with __counted_by()
  2024-10-07 22:20 ` [PATCH v2 2/2] wifi: mwifiex: Annotate mwifiex_ie_types_wildcard_ssid_params with __counted_by() Alper Nebi Yasak
@ 2024-10-15 22:06   ` Brian Norris
  0 siblings, 0 replies; 5+ messages in thread
From: Brian Norris @ 2024-10-15 22:06 UTC (permalink / raw)
  To: Alper Nebi Yasak
  Cc: linux-wireless, linux-kernel, Francesco Dolcini, Johannes Berg,
	Kees Cook, Sascha Hauer, Dmitry Antipov, Gustavo A . R . Silva,
	Jeff Johnson, David Lin, linux-hardening, Kalle Valo,
	Andy Shevchenko

Hi Alper,

On Tue, Oct 08, 2024 at 01:20:55AM +0300, Alper Nebi Yasak wrote:
> Add the __counted_by compiler attribute to the flexible array member
> `ssid` to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and
> CONFIG_FORTIFY_SOURCE.
> 
> Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
> ---
> I've mimicked the commit messages from `git log -S"__counted_by("`.
> Since they refer to UBSAN I tried testing with CONFIG_KASAN (w/
> kasan_multi_shot), CONFIG_UBSAN and CONFIG_FORTIFY_SOURCE. I do not
> get any errors relating to this module with those options, but have
> others -- I even had to test on another board altogether.

It's possible you don't have a new enough compiler to enable the
improved array bounds checks? Supposedly, that's new in GCC 15 and Clang
18. But I'm not too familiar.

> This attribute was suggested in reviews, I don't fully understand it,
> but I am not sure it is correct in the context of this comment from
> scan.c (with irrelelvant parts omitted):
> 
>     ssid_len = user_scan_in->ssid_list[i].ssid_len;
> 
>     wildcard_ssid_tlv =
>             (struct mwifiex_ie_types_wildcard_ssid_params *)
>             tlv_pos;
> 
>     /*
>      * max_ssid_length = 0 tells firmware to perform
>      * specific scan for the SSID filled, whereas
>      * max_ssid_length = IEEE80211_MAX_SSID_LEN is for
>      * wildcard scan.
>      */
>     if (ssid_len)
>             wildcard_ssid_tlv->max_ssid_length = 0;
>     else
>             wildcard_ssid_tlv->max_ssid_length =
>                                     IEEE80211_MAX_SSID_LEN;
> 
>     memcpy(wildcard_ssid_tlv->ssid,
>            user_scan_in->ssid_list[i].ssid, ssid_len);
> 
> I expect we want to use __counted_by(ssid_len) instead, but do not have
> it in the struct. And max_ssid_length = 0 when ssid[] is non-empty, so
> is it really appropriate as the __counted_by()? But then again, I
> couldn't get this to produce a warning.

The correct length would be derived from header.len. But IIUC,
__counted_by neither supports nested structs, nor derived values.

But anyway, like you suspect, __counted_by(max_ssid_length) is actually
wrong.

I'd be happy to be enlighted by other experts on CC, but otherwise,
that's a "NACK" for patch 2. Thanks for looking into this though!

Regards,
Brian

> Changes in v2:
> - Add patch to annotate ssid field with __counted_by(max_ssid_length)
> 
>  drivers/net/wireless/marvell/mwifiex/fw.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/wireless/marvell/mwifiex/fw.h b/drivers/net/wireless/marvell/mwifiex/fw.h
> index 4a96281792cc..e4e35ba35454 100644
> --- a/drivers/net/wireless/marvell/mwifiex/fw.h
> +++ b/drivers/net/wireless/marvell/mwifiex/fw.h
> @@ -875,7 +875,7 @@ struct mwifiex_ietypes_chanstats {
>  struct mwifiex_ie_types_wildcard_ssid_params {
>  	struct mwifiex_ie_types_header header;
>  	u8 max_ssid_length;
> -	u8 ssid[];
> +	u8 ssid[] __counted_by(max_ssid_length);
>  } __packed;
>  
>  #define TSF_DATA_SIZE            8
> -- 
> 2.45.2
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan()
  2024-10-07 22:20 [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan() Alper Nebi Yasak
  2024-10-07 22:20 ` [PATCH v2 2/2] wifi: mwifiex: Annotate mwifiex_ie_types_wildcard_ssid_params with __counted_by() Alper Nebi Yasak
  2024-10-15 21:34 ` [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan() Brian Norris
@ 2024-10-17 16:50 ` Kalle Valo
  2 siblings, 0 replies; 5+ messages in thread
From: Kalle Valo @ 2024-10-17 16:50 UTC (permalink / raw)
  To: Alper Nebi Yasak
  Cc: linux-wireless, linux-kernel, Francesco Dolcini, Johannes Berg,
	Kees Cook, Sascha Hauer, Dmitry Antipov, Gustavo A . R . Silva,
	Jeff Johnson, Brian Norris, David Lin, linux-hardening,
	Andy Shevchenko, Alper Nebi Yasak

Alper Nebi Yasak <alpernebiyasak@gmail.com> wrote:

> Replace one-element array with a flexible-array member in `struct
> mwifiex_ie_types_wildcard_ssid_params` to fix the following warning
> on a MT8173 Chromebook (mt8173-elm-hana):
> 
> [  356.775250] ------------[ cut here ]------------
> [  356.784543] memcpy: detected field-spanning write (size 6) of single field "wildcard_ssid_tlv->ssid" at drivers/net/wireless/marvell/mwifiex/scan.c:904 (size 1)
> [  356.813403] WARNING: CPU: 3 PID: 742 at drivers/net/wireless/marvell/mwifiex/scan.c:904 mwifiex_scan_networks+0x4fc/0xf28 [mwifiex]
> 
> The "(size 6)" above is exactly the length of the SSID of the network
> this device was connected to. The source of the warning looks like:
> 
>     ssid_len = user_scan_in->ssid_list[i].ssid_len;
>     [...]
>     memcpy(wildcard_ssid_tlv->ssid,
>            user_scan_in->ssid_list[i].ssid, ssid_len);
> 
> There is a #define WILDCARD_SSID_TLV_MAX_SIZE that uses sizeof() on this
> struct, but it already didn't account for the size of the one-element
> array, so it doesn't need to be changed.
> 
> Fixes: 5e6e3a92b9a4 ("wireless: mwifiex: initial commit for Marvell mwifiex driver")
> Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
> Acked-by: Brian Norris <briannorris@chromium.org>

Patch applied to wireless-next.git, thanks.

d241a139c2e9 wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan()

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/20241007222301.24154-1-alpernebiyasak@gmail.com/

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-10-17 16:50 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-07 22:20 [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan() Alper Nebi Yasak
2024-10-07 22:20 ` [PATCH v2 2/2] wifi: mwifiex: Annotate mwifiex_ie_types_wildcard_ssid_params with __counted_by() Alper Nebi Yasak
2024-10-15 22:06   ` Brian Norris
2024-10-15 21:34 ` [PATCH v2 1/2] wifi: mwifiex: Fix memcpy() field-spanning write warning in mwifiex_config_scan() Brian Norris
2024-10-17 16:50 ` Kalle Valo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).