linux-hardening.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] staging: rtl8723bs: Allocate full pwep structure
@ 2022-06-08 21:55 Kees Cook
  2022-06-18  7:51 ` Christophe JAILLET
  0 siblings, 1 reply; 3+ messages in thread
From: Kees Cook @ 2022-06-08 21:55 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Kees Cook, Fabio Aiuto, Hans de Goede, linux-staging,
	Michael Straube, Arnd Bergmann, linux-kernel, linux-hardening

The pwep allocation was always being allocated smaller than the true
structure size. Avoid this by always allocating the full structure.
Found with GCC 12 and -Warray-bounds:

../drivers/staging/rtl8723bs/os_dep/ioctl_linux.c: In function 'rtw_set_encryption':
../drivers/staging/rtl8723bs/os_dep/ioctl_linux.c:591:29: warning: array subscript 'struct ndis_802_11_wep[0]' is partly outside array bounds of 'void[25]' [-Warray-bounds]
  591 |                         pwep->length = wep_total_len;
      |                             ^~

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Fabio Aiuto <fabioaiuto83@gmail.com>
Cc: Hans de Goede <hdegoede@redhat.com>
Cc: linux-staging@lists.linux.dev
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/staging/rtl8723bs/os_dep/ioctl_linux.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
index ece97e37ac91..30374a820496 100644
--- a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
+++ b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
@@ -90,7 +90,8 @@ static int wpa_set_encryption(struct net_device *dev, struct ieee_param *param,
 		if (wep_key_len > 0) {
 			wep_key_len = wep_key_len <= 5 ? 5 : 13;
 			wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material);
-			pwep = kzalloc(wep_total_len, GFP_KERNEL);
+			/* Allocate a full structure to avoid potentially running off the end. */
+			pwep = kzalloc(sizeof(*pwep), GFP_KERNEL);
 			if (!pwep) {
 				ret = -ENOMEM;
 				goto exit;
@@ -582,7 +583,8 @@ static int rtw_set_encryption(struct net_device *dev, struct ieee_param *param,
 		if (wep_key_len > 0) {
 			wep_key_len = wep_key_len <= 5 ? 5 : 13;
 			wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material);
-			pwep = kzalloc(wep_total_len, GFP_KERNEL);
+			/* Allocate a full structure to avoid potentially running off the end. */
+			pwep = kzalloc(sizeof(*pwep), GFP_KERNEL);
 			if (!pwep)
 				goto exit;
 
-- 
2.32.0


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

* Re: [PATCH] staging: rtl8723bs: Allocate full pwep structure
  2022-06-08 21:55 [PATCH] staging: rtl8723bs: Allocate full pwep structure Kees Cook
@ 2022-06-18  7:51 ` Christophe JAILLET
  2022-06-20  9:55   ` Dan Carpenter
  0 siblings, 1 reply; 3+ messages in thread
From: Christophe JAILLET @ 2022-06-18  7:51 UTC (permalink / raw)
  To: Kees Cook, Greg Kroah-Hartman
  Cc: Fabio Aiuto, Hans de Goede, linux-staging, Michael Straube,
	Arnd Bergmann, linux-kernel, linux-hardening

Le 08/06/2022 à 23:55, Kees Cook a écrit :
> The pwep allocation was always being allocated smaller than the true
> structure size. Avoid this by always allocating the full structure.
> Found with GCC 12 and -Warray-bounds:
> 
> ../drivers/staging/rtl8723bs/os_dep/ioctl_linux.c: In function 'rtw_set_encryption':
> ../drivers/staging/rtl8723bs/os_dep/ioctl_linux.c:591:29: warning: array subscript 'struct ndis_802_11_wep[0]' is partly outside array bounds of 'void[25]' [-Warray-bounds]
>    591 |                         pwep->length = wep_total_len;
>        |                             ^~
> 
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Fabio Aiuto <fabioaiuto83@gmail.com>
> Cc: Hans de Goede <hdegoede@redhat.com>
> Cc: linux-staging@lists.linux.dev
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>   drivers/staging/rtl8723bs/os_dep/ioctl_linux.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
> index ece97e37ac91..30374a820496 100644
> --- a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
> +++ b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
> @@ -90,7 +90,8 @@ static int wpa_set_encryption(struct net_device *dev, struct ieee_param *param,
>   		if (wep_key_len > 0) {
>   			wep_key_len = wep_key_len <= 5 ? 5 : 13;
>   			wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material);
> -			pwep = kzalloc(wep_total_len, GFP_KERNEL);
> +			/* Allocate a full structure to avoid potentially running off the end. */
> +			pwep = kzalloc(sizeof(*pwep), GFP_KERNEL);
>   			if (!pwep) {
>   				ret = -ENOMEM;
>   				goto exit;
> @@ -582,7 +583,8 @@ static int rtw_set_encryption(struct net_device *dev, struct ieee_param *param,
>   		if (wep_key_len > 0) {
>   			wep_key_len = wep_key_len <= 5 ? 5 : 13;
>   			wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material);
> -			pwep = kzalloc(wep_total_len, GFP_KERNEL);
> +			/* Allocate a full structure to avoid potentially running off the end. */
> +			pwep = kzalloc(sizeof(*pwep), GFP_KERNEL);
>   			if (!pwep)

Hi,

while at it (and un-related tyo your patch), I think that 'ret' should 
also be updated here, otherwise we return 0.

CJ

>   				goto exit;
>   


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

* Re: [PATCH] staging: rtl8723bs: Allocate full pwep structure
  2022-06-18  7:51 ` Christophe JAILLET
@ 2022-06-20  9:55   ` Dan Carpenter
  0 siblings, 0 replies; 3+ messages in thread
From: Dan Carpenter @ 2022-06-20  9:55 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Kees Cook, Greg Kroah-Hartman, Fabio Aiuto, Hans de Goede,
	linux-staging, Michael Straube, Arnd Bergmann, linux-kernel,
	linux-hardening

On Sat, Jun 18, 2022 at 09:51:36AM +0200, Christophe JAILLET wrote:
> > diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
> > index ece97e37ac91..30374a820496 100644
> > --- a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
> > +++ b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
> > @@ -90,7 +90,8 @@ static int wpa_set_encryption(struct net_device *dev, struct ieee_param *param,
> >   		if (wep_key_len > 0) {
> >   			wep_key_len = wep_key_len <= 5 ? 5 : 13;
> >   			wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material);
> > -			pwep = kzalloc(wep_total_len, GFP_KERNEL);
> > +			/* Allocate a full structure to avoid potentially running off the end. */
> > +			pwep = kzalloc(sizeof(*pwep), GFP_KERNEL);
> >   			if (!pwep) {
> >   				ret = -ENOMEM;
> >   				goto exit;
> > @@ -582,7 +583,8 @@ static int rtw_set_encryption(struct net_device *dev, struct ieee_param *param,
> >   		if (wep_key_len > 0) {
> >   			wep_key_len = wep_key_len <= 5 ? 5 : 13;
> >   			wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material);
> > -			pwep = kzalloc(wep_total_len, GFP_KERNEL);
> > +			/* Allocate a full structure to avoid potentially running off the end. */
> > +			pwep = kzalloc(sizeof(*pwep), GFP_KERNEL);
> >   			if (!pwep)
> 
> Hi,
> 
> while at it (and un-related tyo your patch), I think that 'ret' should also
> be updated here, otherwise we return 0.
> 

Too late.  Smatch does catch that bug btw.

drivers/staging/r8188eu/os_dep/ioctl_linux.c:409 wpa_set_encryption() warn: missing error code here? 'kzalloc()' failed. 'ret' = '0'

regards,
dan carpenter


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

end of thread, other threads:[~2022-06-20  9:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 21:55 [PATCH] staging: rtl8723bs: Allocate full pwep structure Kees Cook
2022-06-18  7:51 ` Christophe JAILLET
2022-06-20  9:55   ` Dan Carpenter

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