Re: [PATCH] rtw_security: fix cast to restricted __le32

From: Jhih Ming Huang
Date: Fri Jun 18 2021 - 14:37:05 EST


On Tue, Jun 15, 2021 at 4:48 AM David Laight <David.Laight@xxxxxxxxxx> 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 <fbihjmeric@xxxxxxxxx>
> > ---
> > 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)
>