All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings
@ 2021-03-31 21:43 Gustavo A. R. Silva
  2021-03-31 21:44 ` [PATCH v2 1/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_send_pkt Gustavo A. R. Silva
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Gustavo A. R. Silva @ 2021-03-31 21:43 UTC (permalink / raw)
  To: linux-kernel
  Cc: Kalle Valo, David S. Miller, Jakub Kicinski, linux-wireless,
	netdev, linux-hardening, Gustavo A. R. Silva

Fix the a couple of  out-of-bounds warnings by making the code
a bit more structured.

This helps with the ongoing efforts to enable -Warray-bounds and
avoid confusing the compiler.

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

Changes in v2:
 - Update changelog text in patch 1/2.
 - Replace a couple of magic numbers with new variable sig_addr_len.

Gustavo A. R. Silva (2):
  wl3501_cs: Fix out-of-bounds warning in wl3501_send_pkt
  wl3501_cs: Fix out-of-bounds warning in wl3501_mgmt_join

 drivers/net/wireless/wl3501.h    | 28 ++++++++++++++++------------
 drivers/net/wireless/wl3501_cs.c | 11 ++++++-----
 2 files changed, 22 insertions(+), 17 deletions(-)

-- 
2.27.0


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

* [PATCH v2 1/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_send_pkt
  2021-03-31 21:43 [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings Gustavo A. R. Silva
@ 2021-03-31 21:44 ` Gustavo A. R. Silva
  2021-04-07 18:56   ` Kees Cook
  2021-03-31 21:45 ` [PATCH v2 2/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_mgmt_join Gustavo A. R. Silva
  2021-04-13 21:27 ` [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings Gustavo A. R. Silva
  2 siblings, 1 reply; 9+ messages in thread
From: Gustavo A. R. Silva @ 2021-03-31 21:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: Kalle Valo, David S. Miller, Jakub Kicinski, linux-wireless,
	netdev, linux-hardening, Gustavo A. R. Silva

Fix the following out-of-bounds warning by enclosing
structure members daddr and saddr into new struct addr:

arch/x86/include/asm/string_32.h:182:25: warning: '__builtin_memcpy' offset [18, 23] from the object at 'sig' is out of the bounds of referenced subobject 'daddr' with type 'u8[6]' {aka 'unsigned char[6]'} at offset 11 [-Warray-bounds]

Refactor the code, accordingly:

$ pahole -C wl3501_md_req drivers/net/wireless/wl3501_cs.o
struct wl3501_md_req {
	u16                        next_blk;             /*     0     2 */
	u8                         sig_id;               /*     2     1 */
	u8                         routing;              /*     3     1 */
	u16                        data;                 /*     4     2 */
	u16                        size;                 /*     6     2 */
	u8                         pri;                  /*     8     1 */
	u8                         service_class;        /*     9     1 */
	struct {
		u8                 daddr[6];             /*    10     6 */
		u8                 saddr[6];             /*    16     6 */
	} addr;                                          /*    10    12 */

	/* size: 22, cachelines: 1, members: 8 */
	/* last cacheline: 22 bytes */
};

The problem is that the original code is trying to copy data into a
couple of arrays adjacent to each other in a single call to memcpy().
Now that a new struct _addr_ enclosing those two adjacent arrays
is introduced, memcpy() doesn't overrun the length of &sig.daddr[0],
because the address of the new struct object _addr_ is used as
destination, instead.

Also, this helps with the ongoing efforts to enable -Warray-bounds and
avoid confusing the compiler.

Link: https://github.com/KSPP/linux/issues/109
Reported-by: kernel test robot <lkp@intel.com>
Build-tested-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/lkml/60641d9b.2eNLedOGSdcSoAV2%25lkp@intel.com/
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
Changes in v2:
 - Update changelog text.
 - Replace a couple of magic numbers with new variable sig_addr_len.

 drivers/net/wireless/wl3501.h    | 6 ++++--
 drivers/net/wireless/wl3501_cs.c | 7 ++++---
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/net/wireless/wl3501.h b/drivers/net/wireless/wl3501.h
index e98e04ee9a2c..ef9d605d8c88 100644
--- a/drivers/net/wireless/wl3501.h
+++ b/drivers/net/wireless/wl3501.h
@@ -471,8 +471,10 @@ struct wl3501_md_req {
 	u16	size;
 	u8	pri;
 	u8	service_class;
-	u8	daddr[ETH_ALEN];
-	u8	saddr[ETH_ALEN];
+	struct {
+		u8	daddr[ETH_ALEN];
+		u8	saddr[ETH_ALEN];
+	} addr;
 };
 
 struct wl3501_md_ind {
diff --git a/drivers/net/wireless/wl3501_cs.c b/drivers/net/wireless/wl3501_cs.c
index 8ca5789c7b37..e149ef81d6cc 100644
--- a/drivers/net/wireless/wl3501_cs.c
+++ b/drivers/net/wireless/wl3501_cs.c
@@ -469,6 +469,7 @@ static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
 	struct wl3501_md_req sig = {
 		.sig_id = WL3501_SIG_MD_REQ,
 	};
+	size_t sig_addr_len = sizeof(sig.addr);
 	u8 *pdata = (char *)data;
 	int rc = -EIO;
 
@@ -484,9 +485,9 @@ static int wl3501_send_pkt(struct wl3501_card *this, u8 *data, u16 len)
 			goto out;
 		}
 		rc = 0;
-		memcpy(&sig.daddr[0], pdata, 12);
-		pktlen = len - 12;
-		pdata += 12;
+		memcpy(&sig.addr, pdata, sig_addr_len);
+		pktlen = len - sig_addr_len;
+		pdata += sig_addr_len;
 		sig.data = bf;
 		if (((*pdata) * 256 + (*(pdata + 1))) > 1500) {
 			u8 addr4[ETH_ALEN] = {
-- 
2.27.0


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

* [PATCH v2 2/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_mgmt_join
  2021-03-31 21:43 [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings Gustavo A. R. Silva
  2021-03-31 21:44 ` [PATCH v2 1/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_send_pkt Gustavo A. R. Silva
@ 2021-03-31 21:45 ` Gustavo A. R. Silva
  2021-04-07 19:02   ` Kees Cook
  2021-04-13 21:27 ` [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings Gustavo A. R. Silva
  2 siblings, 1 reply; 9+ messages in thread
From: Gustavo A. R. Silva @ 2021-03-31 21:45 UTC (permalink / raw)
  To: linux-kernel
  Cc: Kalle Valo, David S. Miller, Jakub Kicinski, linux-wireless,
	netdev, linux-hardening, Gustavo A. R. Silva

Fix the following out-of-bounds warning by enclosing
some structure members into new struct req:

arch/x86/include/asm/string_32.h:182:25: warning: '__builtin_memcpy' offset [39, 108] from the object at 'sig' is out of the bounds of referenced subobject 'beacon_period' with type 'short unsigned int' at offset 36 [-Warray-bounds]

Refactor the code, accordingly:

$ pahole -C wl3501_join_req drivers/net/wireless/wl3501_cs.o
struct wl3501_join_req {
	u16                        next_blk;             /*     0     2 */
	u8                         sig_id;               /*     2     1 */
	u8                         reserved;             /*     3     1 */
	struct iw_mgmt_data_rset   operational_rset;     /*     4    10 */
	u16                        reserved2;            /*    14     2 */
	u16                        timeout;              /*    16     2 */
	u16                        probe_delay;          /*    18     2 */
	u8                         timestamp[8];         /*    20     8 */
	u8                         local_time[8];        /*    28     8 */
	struct {
		u16                beacon_period;        /*    36     2 */
		u16                dtim_period;          /*    38     2 */
		u16                cap_info;             /*    40     2 */
		u8                 bss_type;             /*    42     1 */
		u8                 bssid[6];             /*    43     6 */
		struct iw_mgmt_essid_pset ssid;          /*    49    34 */
		/* --- cacheline 1 boundary (64 bytes) was 19 bytes ago --- */
		struct iw_mgmt_ds_pset ds_pset;          /*    83     3 */
		struct iw_mgmt_cf_pset cf_pset;          /*    86     8 */
		struct iw_mgmt_ibss_pset ibss_pset;      /*    94     4 */
		struct iw_mgmt_data_rset bss_basic_rset; /*    98    10 */
	} req;                                           /*    36    72 */

	/* size: 108, cachelines: 2, members: 10 */
	/* last cacheline: 44 bytes */
};

The problem is that the original code is trying to copy data into a
bunch of struct members adjacent to each other in a single call to
memcpy(). Now that a new struct _req_ enclosing all those adjacent
members is introduced, memcpy() doesn't overrun the length of
&sig.beacon_period, because the address of the new struct object
_req_ is used as the destination, instead.

Also, this helps with the ongoing efforts to enable -Warray-bounds and
avoid confusing the compiler.

Link: https://github.com/KSPP/linux/issues/109
Reported-by: kernel test robot <lkp@intel.com>
Build-tested-by: kernel test robot <lkp@intel.com>
Link: https://lore.kernel.org/lkml/60641d9b.2eNLedOGSdcSoAV2%25lkp@intel.com/
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
Changes in v2:
 - None.

 drivers/net/wireless/wl3501.h    | 22 ++++++++++++----------
 drivers/net/wireless/wl3501_cs.c |  4 ++--
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/drivers/net/wireless/wl3501.h b/drivers/net/wireless/wl3501.h
index ef9d605d8c88..774d8cac046d 100644
--- a/drivers/net/wireless/wl3501.h
+++ b/drivers/net/wireless/wl3501.h
@@ -389,16 +389,18 @@ struct wl3501_join_req {
 	u16			    probe_delay;
 	u8			    timestamp[8];
 	u8			    local_time[8];
-	u16			    beacon_period;
-	u16			    dtim_period;
-	u16			    cap_info;
-	u8			    bss_type;
-	u8			    bssid[ETH_ALEN];
-	struct iw_mgmt_essid_pset   ssid;
-	struct iw_mgmt_ds_pset	    ds_pset;
-	struct iw_mgmt_cf_pset	    cf_pset;
-	struct iw_mgmt_ibss_pset    ibss_pset;
-	struct iw_mgmt_data_rset    bss_basic_rset;
+	struct {
+		u16			    beacon_period;
+		u16			    dtim_period;
+		u16			    cap_info;
+		u8			    bss_type;
+		u8			    bssid[ETH_ALEN];
+		struct iw_mgmt_essid_pset   ssid;
+		struct iw_mgmt_ds_pset	    ds_pset;
+		struct iw_mgmt_cf_pset	    cf_pset;
+		struct iw_mgmt_ibss_pset    ibss_pset;
+		struct iw_mgmt_data_rset    bss_basic_rset;
+	} req;
 };
 
 struct wl3501_join_confirm {
diff --git a/drivers/net/wireless/wl3501_cs.c b/drivers/net/wireless/wl3501_cs.c
index e149ef81d6cc..399d3bd2ae76 100644
--- a/drivers/net/wireless/wl3501_cs.c
+++ b/drivers/net/wireless/wl3501_cs.c
@@ -590,7 +590,7 @@ static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
 	struct wl3501_join_req sig = {
 		.sig_id		  = WL3501_SIG_JOIN_REQ,
 		.timeout	  = 10,
-		.ds_pset = {
+		.req.ds_pset = {
 			.el = {
 				.id  = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
 				.len = 1,
@@ -599,7 +599,7 @@ static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
 		},
 	};
 
-	memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
+	memcpy(&sig.req, &this->bss_set[stas].beacon_period, sizeof(sig.req));
 	return wl3501_esbq_exec(this, &sig, sizeof(sig));
 }
 
-- 
2.27.0


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

* Re: [PATCH v2 1/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_send_pkt
  2021-03-31 21:44 ` [PATCH v2 1/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_send_pkt Gustavo A. R. Silva
@ 2021-04-07 18:56   ` Kees Cook
  0 siblings, 0 replies; 9+ messages in thread
From: Kees Cook @ 2021-04-07 18:56 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: linux-kernel, Kalle Valo, David S. Miller, Jakub Kicinski,
	linux-wireless, netdev, linux-hardening

On Wed, Mar 31, 2021 at 04:44:29PM -0500, Gustavo A. R. Silva wrote:
> Fix the following out-of-bounds warning by enclosing
> structure members daddr and saddr into new struct addr:
> 
> arch/x86/include/asm/string_32.h:182:25: warning: '__builtin_memcpy' offset [18, 23] from the object at 'sig' is out of the bounds of referenced subobject 'daddr' with type 'u8[6]' {aka 'unsigned char[6]'} at offset 11 [-Warray-bounds]
> 
> Refactor the code, accordingly:
> 
> $ pahole -C wl3501_md_req drivers/net/wireless/wl3501_cs.o
> struct wl3501_md_req {
> 	u16                        next_blk;             /*     0     2 */
> 	u8                         sig_id;               /*     2     1 */
> 	u8                         routing;              /*     3     1 */
> 	u16                        data;                 /*     4     2 */
> 	u16                        size;                 /*     6     2 */
> 	u8                         pri;                  /*     8     1 */
> 	u8                         service_class;        /*     9     1 */
> 	struct {
> 		u8                 daddr[6];             /*    10     6 */
> 		u8                 saddr[6];             /*    16     6 */
> 	} addr;                                          /*    10    12 */
> 
> 	/* size: 22, cachelines: 1, members: 8 */
> 	/* last cacheline: 22 bytes */
> };
> 
> The problem is that the original code is trying to copy data into a
> couple of arrays adjacent to each other in a single call to memcpy().
> Now that a new struct _addr_ enclosing those two adjacent arrays
> is introduced, memcpy() doesn't overrun the length of &sig.daddr[0],
> because the address of the new struct object _addr_ is used as
> destination, instead.
> 
> Also, this helps with the ongoing efforts to enable -Warray-bounds and
> avoid confusing the compiler.
> 
> Link: https://github.com/KSPP/linux/issues/109
> Reported-by: kernel test robot <lkp@intel.com>
> Build-tested-by: kernel test robot <lkp@intel.com>
> Link: https://lore.kernel.org/lkml/60641d9b.2eNLedOGSdcSoAV2%25lkp@intel.com/
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Thanks, this makes the code much easier for the compiler to validate
at compile time. These cross-field memcpy()s are weird. I like the
solution here.

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

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

* Re: [PATCH v2 2/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_mgmt_join
  2021-03-31 21:45 ` [PATCH v2 2/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_mgmt_join Gustavo A. R. Silva
@ 2021-04-07 19:02   ` Kees Cook
  2021-04-13 21:25     ` Gustavo A. R. Silva
  0 siblings, 1 reply; 9+ messages in thread
From: Kees Cook @ 2021-04-07 19:02 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: linux-kernel, Kalle Valo, David S. Miller, Jakub Kicinski,
	linux-wireless, netdev, linux-hardening

On Wed, Mar 31, 2021 at 04:45:34PM -0500, Gustavo A. R. Silva wrote:
> Fix the following out-of-bounds warning by enclosing
> some structure members into new struct req:
> 
> arch/x86/include/asm/string_32.h:182:25: warning: '__builtin_memcpy' offset [39, 108] from the object at 'sig' is out of the bounds of referenced subobject 'beacon_period' with type 'short unsigned int' at offset 36 [-Warray-bounds]
> 
> Refactor the code, accordingly:
> 
> $ pahole -C wl3501_join_req drivers/net/wireless/wl3501_cs.o
> struct wl3501_join_req {
> 	u16                        next_blk;             /*     0     2 */
> 	u8                         sig_id;               /*     2     1 */
> 	u8                         reserved;             /*     3     1 */
> 	struct iw_mgmt_data_rset   operational_rset;     /*     4    10 */
> 	u16                        reserved2;            /*    14     2 */
> 	u16                        timeout;              /*    16     2 */
> 	u16                        probe_delay;          /*    18     2 */
> 	u8                         timestamp[8];         /*    20     8 */
> 	u8                         local_time[8];        /*    28     8 */
> 	struct {
> 		u16                beacon_period;        /*    36     2 */
> 		u16                dtim_period;          /*    38     2 */
> 		u16                cap_info;             /*    40     2 */
> 		u8                 bss_type;             /*    42     1 */
> 		u8                 bssid[6];             /*    43     6 */
> 		struct iw_mgmt_essid_pset ssid;          /*    49    34 */
> 		/* --- cacheline 1 boundary (64 bytes) was 19 bytes ago --- */
> 		struct iw_mgmt_ds_pset ds_pset;          /*    83     3 */
> 		struct iw_mgmt_cf_pset cf_pset;          /*    86     8 */
> 		struct iw_mgmt_ibss_pset ibss_pset;      /*    94     4 */
> 		struct iw_mgmt_data_rset bss_basic_rset; /*    98    10 */
> 	} req;                                           /*    36    72 */

This section is the same as a large portion of struct wl3501_scan_confirm:

struct wl3501_scan_confirm {
        u16                         next_blk;
        u8                          sig_id;
        u8                          reserved;
        u16                         status;
        char                        timestamp[8];
        char                        localtime[8];

from here
        u16                         beacon_period;
        u16                         dtim_period;
        u16                         cap_info;
        u8                          bss_type;
        u8                          bssid[ETH_ALEN];
        struct iw_mgmt_essid_pset   ssid;
        struct iw_mgmt_ds_pset      ds_pset;
        struct iw_mgmt_cf_pset      cf_pset;
        struct iw_mgmt_ibss_pset    ibss_pset;
        struct iw_mgmt_data_rset    bss_basic_rset;
through here

        u8                          rssi;
};

It seems like maybe extracting that and using it in both structures
would make more sense?

> 
> 	/* size: 108, cachelines: 2, members: 10 */
> 	/* last cacheline: 44 bytes */
> };
> 
> The problem is that the original code is trying to copy data into a
> bunch of struct members adjacent to each other in a single call to
> memcpy(). Now that a new struct _req_ enclosing all those adjacent
> members is introduced, memcpy() doesn't overrun the length of
> &sig.beacon_period, because the address of the new struct object
> _req_ is used as the destination, instead.
> 
> Also, this helps with the ongoing efforts to enable -Warray-bounds and
> avoid confusing the compiler.
> 
> Link: https://github.com/KSPP/linux/issues/109
> Reported-by: kernel test robot <lkp@intel.com>
> Build-tested-by: kernel test robot <lkp@intel.com>
> Link: https://lore.kernel.org/lkml/60641d9b.2eNLedOGSdcSoAV2%25lkp@intel.com/
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> ---
> Changes in v2:
>  - None.
> 
>  drivers/net/wireless/wl3501.h    | 22 ++++++++++++----------
>  drivers/net/wireless/wl3501_cs.c |  4 ++--
>  2 files changed, 14 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/net/wireless/wl3501.h b/drivers/net/wireless/wl3501.h
> index ef9d605d8c88..774d8cac046d 100644
> --- a/drivers/net/wireless/wl3501.h
> +++ b/drivers/net/wireless/wl3501.h
> @@ -389,16 +389,18 @@ struct wl3501_join_req {
>  	u16			    probe_delay;
>  	u8			    timestamp[8];
>  	u8			    local_time[8];
> -	u16			    beacon_period;
> -	u16			    dtim_period;
> -	u16			    cap_info;
> -	u8			    bss_type;
> -	u8			    bssid[ETH_ALEN];
> -	struct iw_mgmt_essid_pset   ssid;
> -	struct iw_mgmt_ds_pset	    ds_pset;
> -	struct iw_mgmt_cf_pset	    cf_pset;
> -	struct iw_mgmt_ibss_pset    ibss_pset;
> -	struct iw_mgmt_data_rset    bss_basic_rset;
> +	struct {
> +		u16			    beacon_period;
> +		u16			    dtim_period;
> +		u16			    cap_info;
> +		u8			    bss_type;
> +		u8			    bssid[ETH_ALEN];
> +		struct iw_mgmt_essid_pset   ssid;
> +		struct iw_mgmt_ds_pset	    ds_pset;
> +		struct iw_mgmt_cf_pset	    cf_pset;
> +		struct iw_mgmt_ibss_pset    ibss_pset;
> +		struct iw_mgmt_data_rset    bss_basic_rset;
> +	} req;
>  };
>  
>  struct wl3501_join_confirm {
> diff --git a/drivers/net/wireless/wl3501_cs.c b/drivers/net/wireless/wl3501_cs.c
> index e149ef81d6cc..399d3bd2ae76 100644
> --- a/drivers/net/wireless/wl3501_cs.c
> +++ b/drivers/net/wireless/wl3501_cs.c
> @@ -590,7 +590,7 @@ static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
>  	struct wl3501_join_req sig = {
>  		.sig_id		  = WL3501_SIG_JOIN_REQ,
>  		.timeout	  = 10,
> -		.ds_pset = {
> +		.req.ds_pset = {
>  			.el = {
>  				.id  = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
>  				.len = 1,
> @@ -599,7 +599,7 @@ static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
>  		},
>  	};
>  
> -	memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
> +	memcpy(&sig.req, &this->bss_set[stas].beacon_period, sizeof(sig.req));

If not, then probably something like this should be added to make sure
nothing unexpected happens to change structure sizes:

BUILD_BUG_ON(sizeof(sig.req) != 72);

>  	return wl3501_esbq_exec(this, &sig, sizeof(sig));
>  }
>  
> -- 
> 2.27.0
> 

-- 
Kees Cook

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

* Re: [PATCH v2 2/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_mgmt_join
  2021-04-07 19:02   ` Kees Cook
@ 2021-04-13 21:25     ` Gustavo A. R. Silva
  0 siblings, 0 replies; 9+ messages in thread
From: Gustavo A. R. Silva @ 2021-04-13 21:25 UTC (permalink / raw)
  To: Kees Cook, Gustavo A. R. Silva
  Cc: linux-kernel, Kalle Valo, David S. Miller, Jakub Kicinski,
	linux-wireless, netdev, linux-hardening

Hi all!

On 4/7/21 14:02, Kees Cook wrote:
> On Wed, Mar 31, 2021 at 04:45:34PM -0500, Gustavo A. R. Silva wrote:
>> Fix the following out-of-bounds warning by enclosing
>> some structure members into new struct req:
>>
>> arch/x86/include/asm/string_32.h:182:25: warning: '__builtin_memcpy' offset [39, 108] from the object at 'sig' is out of the bounds of referenced subobject 'beacon_period' with type 'short unsigned int' at offset 36 [-Warray-bounds]
>>
>> Refactor the code, accordingly:
>>
>> $ pahole -C wl3501_join_req drivers/net/wireless/wl3501_cs.o
>> struct wl3501_join_req {
>> 	u16                        next_blk;             /*     0     2 */
>> 	u8                         sig_id;               /*     2     1 */
>> 	u8                         reserved;             /*     3     1 */
>> 	struct iw_mgmt_data_rset   operational_rset;     /*     4    10 */
>> 	u16                        reserved2;            /*    14     2 */
>> 	u16                        timeout;              /*    16     2 */
>> 	u16                        probe_delay;          /*    18     2 */
>> 	u8                         timestamp[8];         /*    20     8 */
>> 	u8                         local_time[8];        /*    28     8 */
>> 	struct {
>> 		u16                beacon_period;        /*    36     2 */
>> 		u16                dtim_period;          /*    38     2 */
>> 		u16                cap_info;             /*    40     2 */
>> 		u8                 bss_type;             /*    42     1 */
>> 		u8                 bssid[6];             /*    43     6 */
>> 		struct iw_mgmt_essid_pset ssid;          /*    49    34 */
>> 		/* --- cacheline 1 boundary (64 bytes) was 19 bytes ago --- */
>> 		struct iw_mgmt_ds_pset ds_pset;          /*    83     3 */
>> 		struct iw_mgmt_cf_pset cf_pset;          /*    86     8 */
>> 		struct iw_mgmt_ibss_pset ibss_pset;      /*    94     4 */
>> 		struct iw_mgmt_data_rset bss_basic_rset; /*    98    10 */
>> 	} req;                                           /*    36    72 */
> 
> This section is the same as a large portion of struct wl3501_scan_confirm:
> 
> struct wl3501_scan_confirm {
>         u16                         next_blk;
>         u8                          sig_id;
>         u8                          reserved;
>         u16                         status;
>         char                        timestamp[8];
>         char                        localtime[8];
> 
> from here
>         u16                         beacon_period;
>         u16                         dtim_period;
>         u16                         cap_info;
>         u8                          bss_type;
>         u8                          bssid[ETH_ALEN];
>         struct iw_mgmt_essid_pset   ssid;
>         struct iw_mgmt_ds_pset      ds_pset;
>         struct iw_mgmt_cf_pset      cf_pset;
>         struct iw_mgmt_ibss_pset    ibss_pset;
>         struct iw_mgmt_data_rset    bss_basic_rset;
> through here
> 
>         u8                          rssi;
> };
> 
> It seems like maybe extracting that and using it in both structures
> would make more sense?

If I do this, I would therefore have to make a bunch of other changes,
accordingly. I'm OK with that but I'd like to have the opinion of the
maintainers on all this. So, I will go and ping them from the cover
letter of this series with the hope that we can get some feedback from
them. :) They have been silent for a couple of weeks now.

> 
>>
>> 	/* size: 108, cachelines: 2, members: 10 */
>> 	/* last cacheline: 44 bytes */
>> };
>>
>> The problem is that the original code is trying to copy data into a
>> bunch of struct members adjacent to each other in a single call to
>> memcpy(). Now that a new struct _req_ enclosing all those adjacent
>> members is introduced, memcpy() doesn't overrun the length of
>> &sig.beacon_period, because the address of the new struct object
>> _req_ is used as the destination, instead.
>>
>> Also, this helps with the ongoing efforts to enable -Warray-bounds and
>> avoid confusing the compiler.
>>
>> Link: https://github.com/KSPP/linux/issues/109
>> Reported-by: kernel test robot <lkp@intel.com>
>> Build-tested-by: kernel test robot <lkp@intel.com>
>> Link: https://lore.kernel.org/lkml/60641d9b.2eNLedOGSdcSoAV2%25lkp@intel.com/
>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
>> ---
>> Changes in v2:
>>  - None.
>>
>>  drivers/net/wireless/wl3501.h    | 22 ++++++++++++----------
>>  drivers/net/wireless/wl3501_cs.c |  4 ++--
>>  2 files changed, 14 insertions(+), 12 deletions(-)
>>
>> diff --git a/drivers/net/wireless/wl3501.h b/drivers/net/wireless/wl3501.h
>> index ef9d605d8c88..774d8cac046d 100644
>> --- a/drivers/net/wireless/wl3501.h
>> +++ b/drivers/net/wireless/wl3501.h
>> @@ -389,16 +389,18 @@ struct wl3501_join_req {
>>  	u16			    probe_delay;
>>  	u8			    timestamp[8];
>>  	u8			    local_time[8];
>> -	u16			    beacon_period;
>> -	u16			    dtim_period;
>> -	u16			    cap_info;
>> -	u8			    bss_type;
>> -	u8			    bssid[ETH_ALEN];
>> -	struct iw_mgmt_essid_pset   ssid;
>> -	struct iw_mgmt_ds_pset	    ds_pset;
>> -	struct iw_mgmt_cf_pset	    cf_pset;
>> -	struct iw_mgmt_ibss_pset    ibss_pset;
>> -	struct iw_mgmt_data_rset    bss_basic_rset;
>> +	struct {
>> +		u16			    beacon_period;
>> +		u16			    dtim_period;
>> +		u16			    cap_info;
>> +		u8			    bss_type;
>> +		u8			    bssid[ETH_ALEN];
>> +		struct iw_mgmt_essid_pset   ssid;
>> +		struct iw_mgmt_ds_pset	    ds_pset;
>> +		struct iw_mgmt_cf_pset	    cf_pset;
>> +		struct iw_mgmt_ibss_pset    ibss_pset;
>> +		struct iw_mgmt_data_rset    bss_basic_rset;
>> +	} req;
>>  };
>>  
>>  struct wl3501_join_confirm {
>> diff --git a/drivers/net/wireless/wl3501_cs.c b/drivers/net/wireless/wl3501_cs.c
>> index e149ef81d6cc..399d3bd2ae76 100644
>> --- a/drivers/net/wireless/wl3501_cs.c
>> +++ b/drivers/net/wireless/wl3501_cs.c
>> @@ -590,7 +590,7 @@ static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
>>  	struct wl3501_join_req sig = {
>>  		.sig_id		  = WL3501_SIG_JOIN_REQ,
>>  		.timeout	  = 10,
>> -		.ds_pset = {
>> +		.req.ds_pset = {
>>  			.el = {
>>  				.id  = IW_MGMT_INFO_ELEMENT_DS_PARAMETER_SET,
>>  				.len = 1,
>> @@ -599,7 +599,7 @@ static int wl3501_mgmt_join(struct wl3501_card *this, u16 stas)
>>  		},
>>  	};
>>  
>> -	memcpy(&sig.beacon_period, &this->bss_set[stas].beacon_period, 72);
>> +	memcpy(&sig.req, &this->bss_set[stas].beacon_period, sizeof(sig.req));
> 
> If not, then probably something like this should be added to make sure
> nothing unexpected happens to change structure sizes:
> 
> BUILD_BUG_ON(sizeof(sig.req) != 72);

Yep, this is sensible.

Thanks for the feedback!
--
Gustavo

> 
>>  	return wl3501_esbq_exec(this, &sig, sizeof(sig));
>>  }
>>  
>> -- 
>> 2.27.0
>>
> 

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

* Re: [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings
  2021-03-31 21:43 [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings Gustavo A. R. Silva
  2021-03-31 21:44 ` [PATCH v2 1/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_send_pkt Gustavo A. R. Silva
  2021-03-31 21:45 ` [PATCH v2 2/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_mgmt_join Gustavo A. R. Silva
@ 2021-04-13 21:27 ` Gustavo A. R. Silva
  2021-04-14  6:51   ` Kalle Valo
  2 siblings, 1 reply; 9+ messages in thread
From: Gustavo A. R. Silva @ 2021-04-13 21:27 UTC (permalink / raw)
  To: Gustavo A. R. Silva, linux-kernel
  Cc: Kalle Valo, David S. Miller, Jakub Kicinski, linux-wireless,
	netdev, linux-hardening, Kees Cook

Hi all,

Friendly ping: could somebody give us some feedback or take
this series, please?

Thanks
--
Gustavo

On 3/31/21 16:43, Gustavo A. R. Silva wrote:
> Fix the a couple of  out-of-bounds warnings by making the code
> a bit more structured.
> 
> This helps with the ongoing efforts to enable -Warray-bounds and
> avoid confusing the compiler.
> 
> Link: https://github.com/KSPP/linux/issues/109
> 
> Changes in v2:
>  - Update changelog text in patch 1/2.
>  - Replace a couple of magic numbers with new variable sig_addr_len.
> 
> Gustavo A. R. Silva (2):
>   wl3501_cs: Fix out-of-bounds warning in wl3501_send_pkt
>   wl3501_cs: Fix out-of-bounds warning in wl3501_mgmt_join
> 
>  drivers/net/wireless/wl3501.h    | 28 ++++++++++++++++------------
>  drivers/net/wireless/wl3501_cs.c | 11 ++++++-----
>  2 files changed, 22 insertions(+), 17 deletions(-)
> 

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

* Re: [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings
  2021-04-13 21:27 ` [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings Gustavo A. R. Silva
@ 2021-04-14  6:51   ` Kalle Valo
  2021-04-15  0:00     ` Gustavo A. R. Silva
  0 siblings, 1 reply; 9+ messages in thread
From: Kalle Valo @ 2021-04-14  6:51 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Gustavo A. R. Silva, linux-kernel, David S. Miller,
	Jakub Kicinski, linux-wireless, netdev, linux-hardening,
	Kees Cook

"Gustavo A. R. Silva" <gustavo@embeddedor.com> writes:

> Friendly ping: could somebody give us some feedback or take
> this series, please?

First patch 2 comment needs to be resolved.

-- 
https://patchwork.kernel.org/project/linux-wireless/list/

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

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

* Re: [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings
  2021-04-14  6:51   ` Kalle Valo
@ 2021-04-15  0:00     ` Gustavo A. R. Silva
  0 siblings, 0 replies; 9+ messages in thread
From: Gustavo A. R. Silva @ 2021-04-15  0:00 UTC (permalink / raw)
  To: Kalle Valo
  Cc: Gustavo A. R. Silva, linux-kernel, David S. Miller,
	Jakub Kicinski, linux-wireless, netdev, linux-hardening,
	Kees Cook



On 4/14/21 01:51, Kalle Valo wrote:
> "Gustavo A. R. Silva" <gustavo@embeddedor.com> writes:
> 
>> Friendly ping: could somebody give us some feedback or take
>> this series, please?
> 
> First patch 2 comment needs to be resolved.

Done:

https://lore.kernel.org/lkml/cover.1618442265.git.gustavoars@kernel.org/

Thanks
--
Gustavo

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

end of thread, other threads:[~2021-04-15  0:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-03-31 21:43 [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings Gustavo A. R. Silva
2021-03-31 21:44 ` [PATCH v2 1/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_send_pkt Gustavo A. R. Silva
2021-04-07 18:56   ` Kees Cook
2021-03-31 21:45 ` [PATCH v2 2/2][next] wl3501_cs: Fix out-of-bounds warning in wl3501_mgmt_join Gustavo A. R. Silva
2021-04-07 19:02   ` Kees Cook
2021-04-13 21:25     ` Gustavo A. R. Silva
2021-04-13 21:27 ` [PATCH v2 0/2][next] wl3501_cs: Fix out-of-bounds warnings Gustavo A. R. Silva
2021-04-14  6:51   ` Kalle Valo
2021-04-15  0:00     ` Gustavo A. R. Silva

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.