2021-06-13 10:25:19

by Jhih-Ming Huang

[permalink] [raw]
Subject: [PATCH] rtw_security: fix cast to restricted __le32

This patch fixes the sparse warning of fix cast to restricted __le32.

Signed-off-by: Jhih-Ming Huang <[email protected]>
---
drivers/staging/rtl8723bs/core/rtw_security.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/rtl8723bs/core/rtw_security.c b/drivers/staging/rtl8723bs/core/rtw_security.c
index a99f439328f1..2f4da67e31c6 100644
--- a/drivers/staging/rtl8723bs/core/rtw_security.c
+++ b/drivers/staging/rtl8723bs/core/rtw_security.c
@@ -121,7 +121,7 @@ void rtw_wep_decrypt(struct adapter *padapter, u8 *precvframe)
arc4_crypt(ctx, payload, payload, length);

/* calculate icv and compare the icv */
- *((u32 *)crc) = le32_to_cpu(~crc32_le(~0, payload, length - 4));
+ *((u32 *)crc) = le32_to_cpu((__force __le32)~crc32_le(~0, payload, length - 4));

}
}
@@ -618,7 +618,8 @@ u32 rtw_tkip_decrypt(struct adapter *padapter, u8 *precvframe)
arc4_setkey(ctx, rc4key, 16);
arc4_crypt(ctx, payload, payload, length);

- *((u32 *)crc) = le32_to_cpu(~crc32_le(~0, payload, length - 4));
+ *((u32 *)crc) = le32_to_cpu((__force __le32)~crc32_le(~0, payload,
+ length - 4));

if (crc[3] != payload[length - 1] || crc[2] != payload[length - 2] ||
crc[1] != payload[length - 3] || crc[0] != payload[length - 4])
--
2.25.1


2021-06-13 10:29:40

by Greg KH

[permalink] [raw]
Subject: Re: [PATCH] rtw_security: fix cast to restricted __le32

On Sun, Jun 13, 2021 at 06:22:29PM +0800, Jhih-Ming Huang wrote:
> This patch fixes the sparse warning of fix cast to restricted __le32.
>
> Signed-off-by: Jhih-Ming Huang <[email protected]>
> ---
> drivers/staging/rtl8723bs/core/rtw_security.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/staging/rtl8723bs/core/rtw_security.c b/drivers/staging/rtl8723bs/core/rtw_security.c
> index a99f439328f1..2f4da67e31c6 100644
> --- a/drivers/staging/rtl8723bs/core/rtw_security.c
> +++ b/drivers/staging/rtl8723bs/core/rtw_security.c
> @@ -121,7 +121,7 @@ void rtw_wep_decrypt(struct adapter *padapter, u8 *precvframe)
> arc4_crypt(ctx, payload, payload, length);
>
> /* calculate icv and compare the icv */
> - *((u32 *)crc) = le32_to_cpu(~crc32_le(~0, payload, length - 4));
> + *((u32 *)crc) = le32_to_cpu((__force __le32)~crc32_le(~0, payload, length - 4));

When you use __force, that usually implies something is wrong here.

Look at the recent changes that were just submitted for this a week or
so ago in the mailing list archives, and the discussion about them, to
see the issues involved here.

I would need to see a _LOT_ of description in the changelog about why
these changes are actually correct, before I can take them.

thanks,

greg k-h

2021-06-14 20:52:51

by David Laight

[permalink] [raw]
Subject: RE: [PATCH] rtw_security: fix cast to restricted __le32

From: Jhih-Ming Huang
> Sent: 13 June 2021 11:22
>
> This patch fixes the sparse warning of fix cast to restricted __le32.
>
> Signed-off-by: Jhih-Ming Huang <[email protected]>
> ---
> drivers/staging/rtl8723bs/core/rtw_security.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/staging/rtl8723bs/core/rtw_security.c
> b/drivers/staging/rtl8723bs/core/rtw_security.c
> index a99f439328f1..2f4da67e31c6 100644
> --- a/drivers/staging/rtl8723bs/core/rtw_security.c
> +++ b/drivers/staging/rtl8723bs/core/rtw_security.c
> @@ -121,7 +121,7 @@ void rtw_wep_decrypt(struct adapter *padapter, u8 *precvframe)
> arc4_crypt(ctx, payload, payload, length);
>
> /* calculate icv and compare the icv */
> - *((u32 *)crc) = le32_to_cpu(~crc32_le(~0, payload, length - 4));
> + *((u32 *)crc) = le32_to_cpu((__force __le32)~crc32_le(~0, payload, length - 4));
>
> }
> }
> @@ -618,7 +618,8 @@ u32 rtw_tkip_decrypt(struct adapter *padapter, u8 *precvframe)
> arc4_setkey(ctx, rc4key, 16);
> arc4_crypt(ctx, payload, payload, length);

Is someone using 'rc4' and 'security' in the same file?

>
> - *((u32 *)crc) = le32_to_cpu(~crc32_le(~0, payload, length - 4));
> + *((u32 *)crc) = le32_to_cpu((__force __le32)~crc32_le(~0, payload,
> + length - 4));
>
> if (crc[3] != payload[length - 1] || crc[2] != payload[length - 2] ||
> crc[1] != payload[length - 3] || crc[0] != payload[length - 4])

In both cases the *((u32 *)crc) is also very dubious.
Casts of pointers to integer types are almost always broken.
Given the code that follows the second hunk I'm guessing you
have 'u8 crc[4];' (or equivalent).
That can be misaligned and then access will panic on some systems.

There is a get_unaligned_be32() (or something similar) that can be used
to read the crc from (payload + length - 4).

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

2021-06-18 19:42:58

by Jhih-Ming Huang

[permalink] [raw]
Subject: Re: [PATCH] rtw_security: fix cast to restricted __le32

On Tue, Jun 15, 2021 at 4:48 AM David Laight <[email protected]> wrote:
>
> From: Jhih-Ming Huang
> > Sent: 13 June 2021 11:22
> >
> > This patch fixes the sparse warning of fix cast to restricted __le32.
> >
> > Signed-off-by: Jhih-Ming Huang <[email protected]>
> > ---
> > drivers/staging/rtl8723bs/core/rtw_security.c | 5 +++--
> > 1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/staging/rtl8723bs/core/rtw_security.c
> > b/drivers/staging/rtl8723bs/core/rtw_security.c
> > index a99f439328f1..2f4da67e31c6 100644
> > --- a/drivers/staging/rtl8723bs/core/rtw_security.c
> > +++ b/drivers/staging/rtl8723bs/core/rtw_security.c
> > @@ -121,7 +121,7 @@ void rtw_wep_decrypt(struct adapter *padapter, u8 *precvframe)
> > arc4_crypt(ctx, payload, payload, length);
> >
> > /* calculate icv and compare the icv */
> > - *((u32 *)crc) = le32_to_cpu(~crc32_le(~0, payload, length - 4));
> > + *((u32 *)crc) = le32_to_cpu((__force __le32)~crc32_le(~0, payload, length - 4));
> >
> > }
> > }
> > @@ -618,7 +618,8 @@ u32 rtw_tkip_decrypt(struct adapter *padapter, u8 *precvframe)
> > arc4_setkey(ctx, rc4key, 16);
> > arc4_crypt(ctx, payload, payload, length);
>
> Is someone using 'rc4' and 'security' in the same file?
Sorry, I don't get it. Could you explain it more? thanks.

>
> >
> > - *((u32 *)crc) = le32_to_cpu(~crc32_le(~0, payload, length - 4));
> > + *((u32 *)crc) = le32_to_cpu((__force __le32)~crc32_le(~0, payload,
> > + length - 4));
> >
> > if (crc[3] != payload[length - 1] || crc[2] != payload[length - 2] ||
> > crc[1] != payload[length - 3] || crc[0] != payload[length - 4])
>
> In both cases the *((u32 *)crc) is also very dubious.
> Casts of pointers to integer types are almost always broken.
> Given the code that follows the second hunk I'm guessing you
> have 'u8 crc[4];' (or equivalent).
> That can be misaligned and then access will panic on some systems.
>
yes, I've sent the v3 PATCH and used Al Viro's suggestions.

> There is a get_unaligned_be32() (or something similar) that can be used
> to read the crc from (payload + length - 4).
I also used memcmp for checking.

thanks.

--jmhuang

>
> David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
>