linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
@ 2022-12-07 15:23 Li Zetao
  2022-12-09  5:11 ` Ping-Ke Shih
  0 siblings, 1 reply; 12+ messages in thread
From: Li Zetao @ 2022-12-07 15:23 UTC (permalink / raw)
  To: pkshih, kvalo, davem, edumazet, kuba, pabeni
  Cc: lizetao1, Larry.Finger, linville, linux-wireless, netdev, linux-kernel

There is a global-out-of-bounds reported by KASAN:

  BUG: KASAN: global-out-of-bounds in
  _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
  Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411

  CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
  6.1.0-rc8+ #144 e15588508517267d37
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
  Call Trace:
   <TASK>
   ...
   kasan_report+0xbb/0x1c0
   _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
   rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
   rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
   ...
   </TASK>

The root cause of the problem is that the comparison order of
"prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
_rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
strings, so this requires the length of the two strings be greater
than or equal to n. In the  _rtl8812ae_phy_set_txpower_limit(), it was
originally intended to meet this requirement by carefully designing
the comparison order. For example, "pregulation" and "pbandwidth" are
compared in order of length from small to large, first is 3 and last
is 4. However, the comparison order of "prate_section" dose not obey
such order requirement, therefore when "prate_section" is "HT", it will
lead to access out of bounds in _rtl8812ae_eq_n_byte().

Fix it by adding a length check in _rtl8812ae_eq_n_byte(). Although it
can be fixed by adjusting the comparison order of "prate_section", this
may cause the value of "rate_section" to not be from 0 to 5. In
addition, commit "21e4b0726dc6" not only moved driver from staging to
regular tree, but also added setting txpower limit function during the
driver config phase, so the problem was introduced by this commit.

Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
 drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
index a29321e2fa72..720114a9ddb2 100644
--- a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
+++ b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
@@ -1600,7 +1600,7 @@ static bool _rtl8812ae_get_integer_from_string(const char *str, u8 *pint)
 
 static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2, u32 num)
 {
-	if (num == 0)
+	if (num == 0 || strlen(str1) < num)
 		return false;
 	while (num > 0) {
 		num--;
-- 
2.31.1


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

* RE: [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-07 15:23 [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit() Li Zetao
@ 2022-12-09  5:11 ` Ping-Ke Shih
  2022-12-10 12:47   ` Li Zetao
  2022-12-10 16:23   ` [PATCH v2] " Li Zetao
  0 siblings, 2 replies; 12+ messages in thread
From: Ping-Ke Shih @ 2022-12-09  5:11 UTC (permalink / raw)
  To: Li Zetao, kvalo, davem, edumazet, kuba, pabeni
  Cc: Larry.Finger, linville, linux-wireless, netdev, linux-kernel



> -----Original Message-----
> From: Li Zetao <lizetao1@huawei.com>
> Sent: Wednesday, December 7, 2022 11:23 PM
> To: Ping-Ke Shih <pkshih@realtek.com>; kvalo@kernel.org; davem@davemloft.net; edumazet@google.com;
> kuba@kernel.org; pabeni@redhat.com
> Cc: lizetao1@huawei.com; Larry.Finger@lwfinger.net; linville@tuxdriver.com;
> linux-wireless@vger.kernel.org; netdev@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
> 
> There is a global-out-of-bounds reported by KASAN:
> 
>   BUG: KASAN: global-out-of-bounds in
>   _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>   Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411
> 
>   CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
>   6.1.0-rc8+ #144 e15588508517267d37
>   Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
>   Call Trace:
>    <TASK>
>    ...
>    kasan_report+0xbb/0x1c0
>    _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>    rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
>    rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
>    ...
>    </TASK>
> 
> The root cause of the problem is that the comparison order of
> "prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
> _rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
> strings, so this requires the length of the two strings be greater
> than or equal to n. In the  _rtl8812ae_phy_set_txpower_limit(), it was
> originally intended to meet this requirement by carefully designing
> the comparison order. For example, "pregulation" and "pbandwidth" are
> compared in order of length from small to large, first is 3 and last
> is 4. However, the comparison order of "prate_section" dose not obey
> such order requirement, therefore when "prate_section" is "HT", it will
> lead to access out of bounds in _rtl8812ae_eq_n_byte().
> 
> Fix it by adding a length check in _rtl8812ae_eq_n_byte(). Although it
> can be fixed by adjusting the comparison order of "prate_section", this
> may cause the value of "rate_section" to not be from 0 to 5. In
> addition, commit "21e4b0726dc6" not only moved driver from staging to
> regular tree, but also added setting txpower limit function during the
> driver config phase, so the problem was introduced by this commit.
> 
> Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> ---
>  drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> index a29321e2fa72..720114a9ddb2 100644
> --- a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> +++ b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> @@ -1600,7 +1600,7 @@ static bool _rtl8812ae_get_integer_from_string(const char *str, u8 *pint)
> 
>  static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2, u32 num)
>  {

This can causes problem because it compares characters from tail to head, and
we can't simply replace this by strncmp() that does similar work. But, I also
don't like strlen() to loop 'str1' constantly.

How about having a simple loop to compare characters forward:

for (i = 0; i < num; i++)
    if (str1[i] != str2[i])
         return false;

return true;

> -	if (num == 0)
> +	if (num == 0 || strlen(str1) < num)
>  		return false;
>  	while (num > 0) {
>  		num--;
> --
> 2.31.1
> 
> 
> ------Please consider the environment before printing this e-mail.

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

* Re: [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-09  5:11 ` Ping-Ke Shih
@ 2022-12-10 12:47   ` Li Zetao
  2022-12-10 13:15     ` Ping-Ke Shih
  2022-12-10 16:23   ` [PATCH v2] " Li Zetao
  1 sibling, 1 reply; 12+ messages in thread
From: Li Zetao @ 2022-12-10 12:47 UTC (permalink / raw)
  To: Ping-Ke Shih, kvalo, davem, edumazet, kuba, pabeni
  Cc: Larry.Finger, linville, linux-wireless, netdev, linux-kernel

Hi Ping-Ke,

On 2022/12/9 13:11, Ping-Ke Shih wrote:
>
>> -----Original Message-----
>> From: Li Zetao <lizetao1@huawei.com>
>> Sent: Wednesday, December 7, 2022 11:23 PM
>> To: Ping-Ke Shih <pkshih@realtek.com>; kvalo@kernel.org; davem@davemloft.net; edumazet@google.com;
>> kuba@kernel.org; pabeni@redhat.com
>> Cc: lizetao1@huawei.com; Larry.Finger@lwfinger.net; linville@tuxdriver.com;
>> linux-wireless@vger.kernel.org; netdev@vger.kernel.org; linux-kernel@vger.kernel.org
>> Subject: [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
>>
>> There is a global-out-of-bounds reported by KASAN:
>>
>>    BUG: KASAN: global-out-of-bounds in
>>    _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>>    Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411
>>
>>    CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
>>    6.1.0-rc8+ #144 e15588508517267d37
>>    Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
>>    Call Trace:
>>     <TASK>
>>     ...
>>     kasan_report+0xbb/0x1c0
>>     _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>>     rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
>>     rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
>>     ...
>>     </TASK>
>>
>> The root cause of the problem is that the comparison order of
>> "prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
>> _rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
>> strings, so this requires the length of the two strings be greater
>> than or equal to n. In the  _rtl8812ae_phy_set_txpower_limit(), it was
>> originally intended to meet this requirement by carefully designing
>> the comparison order. For example, "pregulation" and "pbandwidth" are
>> compared in order of length from small to large, first is 3 and last
>> is 4. However, the comparison order of "prate_section" dose not obey
>> such order requirement, therefore when "prate_section" is "HT", it will
>> lead to access out of bounds in _rtl8812ae_eq_n_byte().
>>
>> Fix it by adding a length check in _rtl8812ae_eq_n_byte(). Although it
>> can be fixed by adjusting the comparison order of "prate_section", this
>> may cause the value of "rate_section" to not be from 0 to 5. In
>> addition, commit "21e4b0726dc6" not only moved driver from staging to
>> regular tree, but also added setting txpower limit function during the
>> driver config phase, so the problem was introduced by this commit.
>>
>> Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
>> Signed-off-by: Li Zetao <lizetao1@huawei.com>
>> ---
>>   drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
>> b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
>> index a29321e2fa72..720114a9ddb2 100644
>> --- a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
>> +++ b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
>> @@ -1600,7 +1600,7 @@ static bool _rtl8812ae_get_integer_from_string(const char *str, u8 *pint)
>>
>>   static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2, u32 num)
>>   {
> This can causes problem because it compares characters from tail to head, and
> we can't simply replace this by strncmp() that does similar work. But, I also
> don't like strlen() to loop 'str1' constantly.
>
> How about having a simple loop to compare characters forward:
>
> for (i = 0; i < num; i++)
>      if (str1[i] != str2[i])
>           return false;
>
> return true;

Thanks for your comment, but I don't think the problem has anything to 
do with head-to-tail or

tail-to-head comparison. The problem is that num is the length of str2, 
but the length of str1 may

be less than num, which may lead to reading str1 out of bounds, for 
example, when comparing

"prate_section", str1 may be "HT", while str2 may by "CCK", and num is 
3. So I think it is neccssary

to check the length of str1 to ensure that will not read out of bounds.


Looking forward to your comments.


With Best Regards,

Li Zetao

>> -	if (num == 0)
>> +	if (num == 0 || strlen(str1) < num)
>>   		return false;
>>   	while (num > 0) {
>>   		num--;
>> --
>> 2.31.1
>>
>>
>> ------Please consider the environment before printing this e-mail.

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

* Re: [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-10 12:47   ` Li Zetao
@ 2022-12-10 13:15     ` Ping-Ke Shih
  2022-12-10 13:52       ` Li Zetao
  0 siblings, 1 reply; 12+ messages in thread
From: Ping-Ke Shih @ 2022-12-10 13:15 UTC (permalink / raw)
  To: kvalo, edumazet, davem, kuba, lizetao1, pabeni
  Cc: linux-wireless, Larry.Finger, linville, netdev, linux-kernel

On Sat, 2022-12-10 at 20:47 +0800, Li Zetao wrote:
> Hi Ping-Ke,
> 
> On 2022/12/9 13:11, Ping-Ke Shih wrote:
> > > -----Original Message-----
> > > From: Li Zetao <lizetao1@huawei.com>
> > > Sent: Wednesday, December 7, 2022 11:23 PM
> > > To: Ping-Ke Shih <pkshih@realtek.com>; kvalo@kernel.org; davem@davemloft.net; 
> > > edumazet@google.com;
> > > kuba@kernel.org; pabeni@redhat.com
> > > Cc: lizetao1@huawei.com; Larry.Finger@lwfinger.net; linville@tuxdriver.com;
> > > linux-wireless@vger.kernel.org; netdev@vger.kernel.org; linux-kernel@vger.kernel.org
> > > Subject: [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in
> > > _rtl8812ae_phy_set_txpower_limit()
> > > 
> > > There is a global-out-of-bounds reported by KASAN:
> > > 
> > >    BUG: KASAN: global-out-of-bounds in
> > >    _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
> > >    Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411
> > > 
> > >    CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
> > >    6.1.0-rc8+ #144 e15588508517267d37
> > >    Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
> > >    Call Trace:
> > >     <TASK>
> > >     ...
> > >     kasan_report+0xbb/0x1c0
> > >     _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
> > >     rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
> > >     rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
> > >     ...
> > >     </TASK>
> > > 
> > > The root cause of the problem is that the comparison order of
> > > "prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
> > > _rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
> > > strings, so this requires the length of the two strings be greater
> > > than or equal to n. In the  _rtl8812ae_phy_set_txpower_limit(), it was
> > > originally intended to meet this requirement by carefully designing
> > > the comparison order. For example, "pregulation" and "pbandwidth" are
> > > compared in order of length from small to large, first is 3 and last
> > > is 4. However, the comparison order of "prate_section" dose not obey
> > > such order requirement, therefore when "prate_section" is "HT", it will
> > > lead to access out of bounds in _rtl8812ae_eq_n_byte().
> > > 
> > > Fix it by adding a length check in _rtl8812ae_eq_n_byte(). Although it
> > > can be fixed by adjusting the comparison order of "prate_section", this
> > > may cause the value of "rate_section" to not be from 0 to 5. In
> > > addition, commit "21e4b0726dc6" not only moved driver from staging to
> > > regular tree, but also added setting txpower limit function during the
> > > driver config phase, so the problem was introduced by this commit.
> > > 
> > > Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
> > > Signed-off-by: Li Zetao <lizetao1@huawei.com>
> > > ---
> > >   drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c | 2 +-
> > >   1 file changed, 1 insertion(+), 1 deletion(-)
> > > 
> > > diff --git a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> > > b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> > > index a29321e2fa72..720114a9ddb2 100644
> > > --- a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> > > +++ b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> > > @@ -1600,7 +1600,7 @@ static bool _rtl8812ae_get_integer_from_string(const char *str, u8
> > > *pint)
> > > 
> > >   static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2, u32 num)
> > >   {
> > This can causes problem because it compares characters from tail to head, and
> > we can't simply replace this by strncmp() that does similar work. But, I also
> > don't like strlen() to loop 'str1' constantly.
> > 
> > How about having a simple loop to compare characters forward:
> > 
> > for (i = 0; i < num; i++)
> >      if (str1[i] != str2[i])
> >           return false;
> > 
> > return true;
> 
> Thanks for your comment, but I don't think the problem has anything to 
> do with head-to-tail or
> 
> tail-to-head comparison. The problem is that num is the length of str2, 
> but the length of str1 may
> 
> be less than num, which may lead to reading str1 out of bounds, for 
> example, when comparing
> 
> "prate_section", str1 may be "HT", while str2 may by "CCK", and num is 
> 3. So I think it is neccssary
> 
> to check the length of str1 to ensure that will not read out of bounds.
> 

I know your point, and I believe your patch can work well, but I would like
to have simple code that can solve this specific problem.

Since both str1 and str2 are null-terminator strings, so str1[2]='\0' is
accessible if str1="HT", right? Then, if length of str1 and str2 is
different, null-terminator can help to break head-to-tail loop.

Take "12" and "1234" as an example:
Then, num=4,

head-to-tail                tail-to-head
-------------------        -------------------------------------------------
str1[0] == str2[0]          str1[3] >< str2[3]   (str1[3] is inaccessible)
str1[1] == str2[1]
str1[2] != str2[2]


I hope this can help to explain my point.


After I think deeper, it seems like third parameter 'u32 num' isn't necessary,
and then just strcmp(str1, str2) is enough.

Ping-Ke



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

* Re: [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-10 13:15     ` Ping-Ke Shih
@ 2022-12-10 13:52       ` Li Zetao
  0 siblings, 0 replies; 12+ messages in thread
From: Li Zetao @ 2022-12-10 13:52 UTC (permalink / raw)
  To: Ping-Ke Shih, kvalo, edumazet, davem, kuba, pabeni
  Cc: linux-wireless, Larry.Finger, linville, netdev, linux-kernel

Hi Ping-Ke,

On 2022/12/10 21:15, Ping-Ke Shih wrote:
> On Sat, 2022-12-10 at 20:47 +0800, Li Zetao wrote:
>> Hi Ping-Ke,
>>
>> On 2022/12/9 13:11, Ping-Ke Shih wrote:
>>>> -----Original Message-----
>>>> From: Li Zetao <lizetao1@huawei.com>
>>>> Sent: Wednesday, December 7, 2022 11:23 PM
>>>> To: Ping-Ke Shih <pkshih@realtek.com>; kvalo@kernel.org; davem@davemloft.net;
>>>> edumazet@google.com;
>>>> kuba@kernel.org; pabeni@redhat.com
>>>> Cc: lizetao1@huawei.com; Larry.Finger@lwfinger.net; linville@tuxdriver.com;
>>>> linux-wireless@vger.kernel.org; netdev@vger.kernel.org; linux-kernel@vger.kernel.org
>>>> Subject: [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in
>>>> _rtl8812ae_phy_set_txpower_limit()
>>>>
>>>> There is a global-out-of-bounds reported by KASAN:
>>>>
>>>>     BUG: KASAN: global-out-of-bounds in
>>>>     _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>>>>     Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411
>>>>
>>>>     CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
>>>>     6.1.0-rc8+ #144 e15588508517267d37
>>>>     Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
>>>>     Call Trace:
>>>>      <TASK>
>>>>      ...
>>>>      kasan_report+0xbb/0x1c0
>>>>      _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>>>>      rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
>>>>      rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
>>>>      ...
>>>>      </TASK>
>>>>
>>>> The root cause of the problem is that the comparison order of
>>>> "prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
>>>> _rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
>>>> strings, so this requires the length of the two strings be greater
>>>> than or equal to n. In the  _rtl8812ae_phy_set_txpower_limit(), it was
>>>> originally intended to meet this requirement by carefully designing
>>>> the comparison order. For example, "pregulation" and "pbandwidth" are
>>>> compared in order of length from small to large, first is 3 and last
>>>> is 4. However, the comparison order of "prate_section" dose not obey
>>>> such order requirement, therefore when "prate_section" is "HT", it will
>>>> lead to access out of bounds in _rtl8812ae_eq_n_byte().
>>>>
>>>> Fix it by adding a length check in _rtl8812ae_eq_n_byte(). Although it
>>>> can be fixed by adjusting the comparison order of "prate_section", this
>>>> may cause the value of "rate_section" to not be from 0 to 5. In
>>>> addition, commit "21e4b0726dc6" not only moved driver from staging to
>>>> regular tree, but also added setting txpower limit function during the
>>>> driver config phase, so the problem was introduced by this commit.
>>>>
>>>> Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
>>>> Signed-off-by: Li Zetao <lizetao1@huawei.com>
>>>> ---
>>>>    drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c | 2 +-
>>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
>>>> b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
>>>> index a29321e2fa72..720114a9ddb2 100644
>>>> --- a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
>>>> +++ b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
>>>> @@ -1600,7 +1600,7 @@ static bool _rtl8812ae_get_integer_from_string(const char *str, u8
>>>> *pint)
>>>>
>>>>    static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2, u32 num)
>>>>    {
>>> This can causes problem because it compares characters from tail to head, and
>>> we can't simply replace this by strncmp() that does similar work. But, I also
>>> don't like strlen() to loop 'str1' constantly.
>>>
>>> How about having a simple loop to compare characters forward:
>>>
>>> for (i = 0; i < num; i++)
>>>       if (str1[i] != str2[i])
>>>            return false;
>>>
>>> return true;
>> Thanks for your comment, but I don't think the problem has anything to
>> do with head-to-tail or
>>
>> tail-to-head comparison. The problem is that num is the length of str2,
>> but the length of str1 may
>>
>> be less than num, which may lead to reading str1 out of bounds, for
>> example, when comparing
>>
>> "prate_section", str1 may be "HT", while str2 may by "CCK", and num is
>> 3. So I think it is neccssary
>>
>> to check the length of str1 to ensure that will not read out of bounds.
>>
> I know your point, and I believe your patch can work well, but I would like
> to have simple code that can solve this specific problem.
>
> Since both str1 and str2 are null-terminator strings, so str1[2]='\0' is
> accessible if str1="HT", right? Then, if length of str1 and str2 is
> different, null-terminator can help to break head-to-tail loop.
>
> Take "12" and "1234" as an example:
> Then, num=4,
>
> head-to-tail                tail-to-head
> -------------------        -------------------------------------------------
> str1[0] == str2[0]          str1[3] >< str2[3]   (str1[3] is inaccessible)
> str1[1] == str2[1]
> str1[2] != str2[2]
>
>
> I hope this can help to explain my point.
Thank you, I get your point now.
>
> After I think deeper, it seems like third parameter 'u32 num' isn't necessary,
> and then just strcmp(str1, str2) is enough.
>
> Ping-Ke

I will replace _rtl8812ae_eq_n_byte with strcmp in the v2 and do a full 
test.


With Best Regards,

Li Zetao

>

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

* [PATCH v2] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-09  5:11 ` Ping-Ke Shih
  2022-12-10 12:47   ` Li Zetao
@ 2022-12-10 16:23   ` Li Zetao
  2022-12-11 11:21     ` Ping-Ke Shih
  1 sibling, 1 reply; 12+ messages in thread
From: Li Zetao @ 2022-12-10 16:23 UTC (permalink / raw)
  To: pkshih
  Cc: Larry.Finger, davem, edumazet, kuba, kvalo, linux-kernel,
	linux-wireless, linville, lizetao1, netdev, pabeni

There is a global-out-of-bounds reported by KASAN:

  BUG: KASAN: global-out-of-bounds in
  _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
  Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411

  CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
  6.1.0-rc8+ #144 e15588508517267d37
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
  Call Trace:
   <TASK>
   ...
   kasan_report+0xbb/0x1c0
   _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
   rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
   rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
   ...
   </TASK>

The root cause of the problem is that the comparison order of
"prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
_rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
strings from tail to head, which causes the problem. In the
_rtl8812ae_phy_set_txpower_limit(), it was originally intended to meet
this requirement by carefully designing the comparison order.
For example, "pregulation" and "pbandwidth" are compared in order of
length from small to large, first is 3 and last is 4. However, the
comparison order of "prate_section" dose not obey such order requirement,
therefore when "prate_section" is "HT", when comparing from tail to head,
it will lead to access out of bounds in _rtl8812ae_eq_n_byte(). As
mentioned above, the _rtl8812ae_eq_n_byte() has the same function as
strcmp(), so just strcmp() is enough.

Fix it by replacing _rtl8812ae_eq_n_byte() with strcmp(). Although it
can be fixed by adjusting the comparison order of "prate_section", this
may cause the value of "rate_section" to not be from 0 to 5. In
addition, commit "21e4b0726dc6" not only moved driver from staging to
regular tree, but also added setting txpower limit function during the
driver config phase, so the problem was introduced by this commit.

Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
v1 was posted at: https://lore.kernel.org/all/20221207152319.3135500-1-lizetao1@huawei.com/
v1 -> v2: delete the third parameter of _rtl8812ae_eq_n_byte() and use
strcmp to replace loop comparison.

 .../wireless/realtek/rtlwifi/rtl8821ae/phy.c  | 51 ++++++++-----------
 1 file changed, 22 insertions(+), 29 deletions(-)

diff --git a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
index a29321e2fa72..14b569d7d8fa 100644
--- a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
+++ b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
@@ -1598,16 +1598,9 @@ static bool _rtl8812ae_get_integer_from_string(const char *str, u8 *pint)
 	return true;
 }
 
-static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2, u32 num)
+static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2)
 {
-	if (num == 0)
-		return false;
-	while (num > 0) {
-		num--;
-		if (str1[num] != str2[num])
-			return false;
-	}
-	return true;
+	return strcmp(str1, str2) == 0;
 }
 
 static s8 _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(struct ieee80211_hw *hw,
@@ -1659,42 +1652,42 @@ static void _rtl8812ae_phy_set_txpower_limit(struct ieee80211_hw *hw,
 	power_limit = power_limit > MAX_POWER_INDEX ?
 		      MAX_POWER_INDEX : power_limit;
 
-	if (_rtl8812ae_eq_n_byte(pregulation, "FCC", 3))
+	if (_rtl8812ae_eq_n_byte(pregulation, "FCC"))
 		regulation = 0;
-	else if (_rtl8812ae_eq_n_byte(pregulation, "MKK", 3))
+	else if (_rtl8812ae_eq_n_byte(pregulation, "MKK"))
 		regulation = 1;
-	else if (_rtl8812ae_eq_n_byte(pregulation, "ETSI", 4))
+	else if (_rtl8812ae_eq_n_byte(pregulation, "ETSI"))
 		regulation = 2;
-	else if (_rtl8812ae_eq_n_byte(pregulation, "WW13", 4))
+	else if (_rtl8812ae_eq_n_byte(pregulation, "WW13"))
 		regulation = 3;
 
-	if (_rtl8812ae_eq_n_byte(prate_section, "CCK", 3))
+	if (_rtl8812ae_eq_n_byte(prate_section, "CCK"))
 		rate_section = 0;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "OFDM", 4))
+	else if (_rtl8812ae_eq_n_byte(prate_section, "OFDM"))
 		rate_section = 1;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "HT", 2) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "1T", 2))
+	else if (_rtl8812ae_eq_n_byte(prate_section, "HT") &&
+		 _rtl8812ae_eq_n_byte(prf_path, "1T"))
 		rate_section = 2;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "HT", 2) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "2T", 2))
+	else if (_rtl8812ae_eq_n_byte(prate_section, "HT") &&
+		 _rtl8812ae_eq_n_byte(prf_path, "2T"))
 		rate_section = 3;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT", 3) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "1T", 2))
+	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT") &&
+		 _rtl8812ae_eq_n_byte(prf_path, "1T"))
 		rate_section = 4;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT", 3) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "2T", 2))
+	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT") &&
+		 _rtl8812ae_eq_n_byte(prf_path, "2T"))
 		rate_section = 5;
 
-	if (_rtl8812ae_eq_n_byte(pbandwidth, "20M", 3))
+	if (_rtl8812ae_eq_n_byte(pbandwidth, "20M"))
 		bandwidth = 0;
-	else if (_rtl8812ae_eq_n_byte(pbandwidth, "40M", 3))
+	else if (_rtl8812ae_eq_n_byte(pbandwidth, "40M"))
 		bandwidth = 1;
-	else if (_rtl8812ae_eq_n_byte(pbandwidth, "80M", 3))
+	else if (_rtl8812ae_eq_n_byte(pbandwidth, "80M"))
 		bandwidth = 2;
-	else if (_rtl8812ae_eq_n_byte(pbandwidth, "160M", 4))
+	else if (_rtl8812ae_eq_n_byte(pbandwidth, "160M"))
 		bandwidth = 3;
 
-	if (_rtl8812ae_eq_n_byte(pband, "2.4G", 4)) {
+	if (_rtl8812ae_eq_n_byte(pband, "2.4G")) {
 		ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
 							       BAND_ON_2_4G,
 							       channel);
@@ -1718,7 +1711,7 @@ static void _rtl8812ae_phy_set_txpower_limit(struct ieee80211_hw *hw,
 			regulation, bandwidth, rate_section, channel_index,
 			rtlphy->txpwr_limit_2_4g[regulation][bandwidth]
 				[rate_section][channel_index][RF90_PATH_A]);
-	} else if (_rtl8812ae_eq_n_byte(pband, "5G", 2)) {
+	} else if (_rtl8812ae_eq_n_byte(pband, "5G")) {
 		ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
 							       BAND_ON_5G,
 							       channel);
-- 
2.31.1


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

* Re: [PATCH v2] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-10 16:23   ` [PATCH v2] " Li Zetao
@ 2022-12-11 11:21     ` Ping-Ke Shih
  2022-12-12  2:35       ` [PATCH v3] " Li Zetao
  0 siblings, 1 reply; 12+ messages in thread
From: Ping-Ke Shih @ 2022-12-11 11:21 UTC (permalink / raw)
  To: lizetao1
  Cc: davem, pabeni, kvalo, linux-kernel, linux-wireless, Larry.Finger,
	kuba, edumazet, linville, netdev

On Sun, 2022-12-11 at 00:23 +0800, Li Zetao wrote:
> There is a global-out-of-bounds reported by KASAN:
> 
>   BUG: KASAN: global-out-of-bounds in
>   _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>   Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411
> 
>   CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
>   6.1.0-rc8+ #144 e15588508517267d37
>   Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
>   Call Trace:
>    <TASK>
>    ...
>    kasan_report+0xbb/0x1c0
>    _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>    rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
>    rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
>    ...
>    </TASK>
> 
> The root cause of the problem is that the comparison order of
> "prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
> _rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
> strings from tail to head, which causes the problem. In the
> _rtl8812ae_phy_set_txpower_limit(), it was originally intended to meet
> this requirement by carefully designing the comparison order.
> For example, "pregulation" and "pbandwidth" are compared in order of
> length from small to large, first is 3 and last is 4. However, the
> comparison order of "prate_section" dose not obey such order requirement,
> therefore when "prate_section" is "HT", when comparing from tail to head,
> it will lead to access out of bounds in _rtl8812ae_eq_n_byte(). As
> mentioned above, the _rtl8812ae_eq_n_byte() has the same function as
> strcmp(), so just strcmp() is enough.
> 
> Fix it by replacing _rtl8812ae_eq_n_byte() with strcmp(). Although it
> can be fixed by adjusting the comparison order of "prate_section", this
> may cause the value of "rate_section" to not be from 0 to 5. In
> addition, commit "21e4b0726dc6" not only moved driver from staging to
> regular tree, but also added setting txpower limit function during the
> driver config phase, so the problem was introduced by this commit.
> 
> Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> ---
> v1 was posted at: https://lore.kernel.org/all/20221207152319.3135500-1-lizetao1@huawei.com/
> v1 -> v2: delete the third parameter of _rtl8812ae_eq_n_byte() and use
> strcmp to replace loop comparison.
> 
>  .../wireless/realtek/rtlwifi/rtl8821ae/phy.c  | 51 ++++++++-----------
>  1 file changed, 22 insertions(+), 29 deletions(-)
> 
> diff --git a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> index a29321e2fa72..14b569d7d8fa 100644
> --- a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> +++ b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
> @@ -1598,16 +1598,9 @@ static bool _rtl8812ae_get_integer_from_string(const char *str, u8 *pint)
>  	return true;
>  }
>  
> -static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2, u32 num)
> +static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2)
>  {
> -	if (num == 0)
> -		return false;
> -	while (num > 0) {
> -		num--;
> -		if (str1[num] != str2[num])
> -			return false;
> -	}
> -	return true;
> +	return strcmp(str1, str2) == 0;
>  }

I suggest to remove _rtl8812ae_eq_n_byte() and use strcmp() barely.
That would be more clear, and people don't need to check detail of
unnecessary _rtl8812ae_eq_n_byte().


>  
>  static s8 _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(struct ieee80211_hw *hw,
> @@ -1659,42 +1652,42 @@ static void _rtl8812ae_phy_set_txpower_limit(struct ieee80211_hw *hw,
>  	power_limit = power_limit > MAX_POWER_INDEX ?
>  		      MAX_POWER_INDEX : power_limit;
>  
> -	if (_rtl8812ae_eq_n_byte(pregulation, "FCC", 3))
> +	if (_rtl8812ae_eq_n_byte(pregulation, "FCC"))
>  		regulation = 0;
> -	else if (_rtl8812ae_eq_n_byte(pregulation, "MKK", 3))
> +	else if (_rtl8812ae_eq_n_byte(pregulation, "MKK"))
>  		regulation = 1;
> -	else if (_rtl8812ae_eq_n_byte(pregulation, "ETSI", 4))
> +	else if (_rtl8812ae_eq_n_byte(pregulation, "ETSI"))
>  		regulation = 2;
> -	else if (_rtl8812ae_eq_n_byte(pregulation, "WW13", 4))
> +	else if (_rtl8812ae_eq_n_byte(pregulation, "WW13"))
>  		regulation = 3;
>  
> -	if (_rtl8812ae_eq_n_byte(prate_section, "CCK", 3))
> +	if (_rtl8812ae_eq_n_byte(prate_section, "CCK"))
>  		rate_section = 0;
> -	else if (_rtl8812ae_eq_n_byte(prate_section, "OFDM", 4))
> +	else if (_rtl8812ae_eq_n_byte(prate_section, "OFDM"))
>  		rate_section = 1;
> -	else if (_rtl8812ae_eq_n_byte(prate_section, "HT", 2) &&
> -		 _rtl8812ae_eq_n_byte(prf_path, "1T", 2))
> +	else if (_rtl8812ae_eq_n_byte(prate_section, "HT") &&
> +		 _rtl8812ae_eq_n_byte(prf_path, "1T"))
>  		rate_section = 2;
> -	else if (_rtl8812ae_eq_n_byte(prate_section, "HT", 2) &&
> -		 _rtl8812ae_eq_n_byte(prf_path, "2T", 2))
> +	else if (_rtl8812ae_eq_n_byte(prate_section, "HT") &&
> +		 _rtl8812ae_eq_n_byte(prf_path, "2T"))
>  		rate_section = 3;
> -	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT", 3) &&
> -		 _rtl8812ae_eq_n_byte(prf_path, "1T", 2))
> +	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT") &&
> +		 _rtl8812ae_eq_n_byte(prf_path, "1T"))
>  		rate_section = 4;
> -	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT", 3) &&
> -		 _rtl8812ae_eq_n_byte(prf_path, "2T", 2))
> +	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT") &&
> +		 _rtl8812ae_eq_n_byte(prf_path, "2T"))
>  		rate_section = 5;
>  
> -	if (_rtl8812ae_eq_n_byte(pbandwidth, "20M", 3))
> +	if (_rtl8812ae_eq_n_byte(pbandwidth, "20M"))
>  		bandwidth = 0;
> -	else if (_rtl8812ae_eq_n_byte(pbandwidth, "40M", 3))
> +	else if (_rtl8812ae_eq_n_byte(pbandwidth, "40M"))
>  		bandwidth = 1;
> -	else if (_rtl8812ae_eq_n_byte(pbandwidth, "80M", 3))
> +	else if (_rtl8812ae_eq_n_byte(pbandwidth, "80M"))
>  		bandwidth = 2;
> -	else if (_rtl8812ae_eq_n_byte(pbandwidth, "160M", 4))
> +	else if (_rtl8812ae_eq_n_byte(pbandwidth, "160M"))
>  		bandwidth = 3;
>  
> -	if (_rtl8812ae_eq_n_byte(pband, "2.4G", 4)) {
> +	if (_rtl8812ae_eq_n_byte(pband, "2.4G")) {
>  		ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
>  							       BAND_ON_2_4G,
>  							       channel);
> @@ -1718,7 +1711,7 @@ static void _rtl8812ae_phy_set_txpower_limit(struct ieee80211_hw *hw,
>  			regulation, bandwidth, rate_section, channel_index,
>  			rtlphy->txpwr_limit_2_4g[regulation][bandwidth]
>  				[rate_section][channel_index][RF90_PATH_A]);
> -	} else if (_rtl8812ae_eq_n_byte(pband, "5G", 2)) {
> +	} else if (_rtl8812ae_eq_n_byte(pband, "5G")) {
>  		ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
>  							       BAND_ON_5G,
>  							       channel);
> -- 
> 2.31.1
> 
> 
> ------Please consider the environment before printing this e-mail.

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

* RE: [PATCH v3] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-12  2:35       ` [PATCH v3] " Li Zetao
@ 2022-12-12  1:35         ` Ping-Ke Shih
  2022-12-12  1:37         ` Ping-Ke Shih
  1 sibling, 0 replies; 12+ messages in thread
From: Ping-Ke Shih @ 2022-12-12  1:35 UTC (permalink / raw)
  To: Li Zetao
  Cc: Larry.Finger, davem, edumazet, kuba, kvalo, linux-kernel,
	linux-wireless, linville, netdev, pabeni



> -----Original Message-----
> From: Li Zetao <lizetao1@huawei.com>
> Sent: Monday, December 12, 2022 10:36 AM
> To: Ping-Ke Shih <pkshih@realtek.com>
> Cc: Larry.Finger@lwfinger.net; davem@davemloft.net; edumazet@google.com; kuba@kernel.org;
> kvalo@kernel.org; linux-kernel@vger.kernel.org; linux-wireless@vger.kernel.org; linville@tuxdriver.com;
> lizetao1@huawei.com; netdev@vger.kernel.org; pabeni@redhat.com
> Subject: [PATCH v3] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
> 
> There is a global-out-of-bounds reported by KASAN:
> 
>   BUG: KASAN: global-out-of-bounds in
>   _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>   Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411
> 
>   CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
>   6.1.0-rc8+ #144 e15588508517267d37
>   Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
>   Call Trace:
>    <TASK>
>    ...
>    kasan_report+0xbb/0x1c0
>    _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>    rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
>    rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
>    ...
>    </TASK>
> 
> The root cause of the problem is that the comparison order of
> "prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
> _rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
> strings from tail to head, which causes the problem. In the
> _rtl8812ae_phy_set_txpower_limit(), it was originally intended to meet
> this requirement by carefully designing the comparison order.
> For example, "pregulation" and "pbandwidth" are compared in order of
> length from small to large, first is 3 and last is 4. However, the
> comparison order of "prate_section" dose not obey such order requirement,
> therefore when "prate_section" is "HT", when comparing from tail to head,
> it will lead to access out of bounds in _rtl8812ae_eq_n_byte(). As
> mentioned above, the _rtl8812ae_eq_n_byte() has the same function as
> strcmp(), so just strcmp() is enough.
> 
> Fix it by removing _rtl8812ae_eq_n_byte() and use strcmp() barely.
> Although it can be fixed by adjusting the comparison order of
> "prate_section", this may cause the value of "rate_section" to not be
> from 0 to 5. In addition, commit "21e4b0726dc6" not only moved driver
> from staging to regular tree, but also added setting txpower limit
> function during the driver config phase, so the problem was introduced
> by this commit.
> 
> Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
> Signed-off-by: Li Zetao <lizetao1@huawei.com>

Thanks for your fix.

Acked-by: Ping-Ke Shih <pkshih@realtek.com>

> ---
> v1 -> v2: delete the third parameter of _rtl8812ae_eq_n_byte() and use
> strcmp to replace loop comparison.
> v2 -> v3: remove _rtl8812ae_eq_n_byte() and use strcmp() barely.
> 
>  .../wireless/realtek/rtlwifi/rtl8821ae/phy.c  | 52 +++++++------------
>  1 file changed, 20 insertions(+), 32 deletions(-)
> 

[...]


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

* RE: [PATCH v3] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-12  2:35       ` [PATCH v3] " Li Zetao
  2022-12-12  1:35         ` Ping-Ke Shih
@ 2022-12-12  1:37         ` Ping-Ke Shih
  2022-12-12  2:58           ` [PATCH v4] wifi: rtlwifi: " Li Zetao
  1 sibling, 1 reply; 12+ messages in thread
From: Ping-Ke Shih @ 2022-12-12  1:37 UTC (permalink / raw)
  To: Li Zetao
  Cc: Larry.Finger, davem, edumazet, kuba, kvalo, linux-kernel,
	linux-wireless, linville, netdev, pabeni



> -----Original Message-----
> From: Ping-Ke Shih
> Sent: Monday, December 12, 2022 9:35 AM
> To: 'Li Zetao' <lizetao1@huawei.com>
> Cc: Larry.Finger@lwfinger.net; davem@davemloft.net; edumazet@google.com; kuba@kernel.org;
> kvalo@kernel.org; linux-kernel@vger.kernel.org; linux-wireless@vger.kernel.org; linville@tuxdriver.com;
> netdev@vger.kernel.org; pabeni@redhat.com
> Subject: RE: [PATCH v3] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in
> _rtl8812ae_phy_set_txpower_limit()
> 
> 
> 
> > -----Original Message-----
> > From: Li Zetao <lizetao1@huawei.com>
> > Sent: Monday, December 12, 2022 10:36 AM
> > To: Ping-Ke Shih <pkshih@realtek.com>
> > Cc: Larry.Finger@lwfinger.net; davem@davemloft.net; edumazet@google.com; kuba@kernel.org;
> > kvalo@kernel.org; linux-kernel@vger.kernel.org; linux-wireless@vger.kernel.org;
> linville@tuxdriver.com;
> > lizetao1@huawei.com; netdev@vger.kernel.org; pabeni@redhat.com
> > Subject: [PATCH v3] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in
> _rtl8812ae_phy_set_txpower_limit()

Oops. Subject prefix should be "wifi: rtlwifi: ...".

If it isn't hard to you, please fix it, and add my acked-by along with v4.

> >
> > There is a global-out-of-bounds reported by KASAN:
> >
> >   BUG: KASAN: global-out-of-bounds in
> >   _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
> >   Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411
> >
> >   CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
> >   6.1.0-rc8+ #144 e15588508517267d37
> >   Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
> >   Call Trace:
> >    <TASK>
> >    ...
> >    kasan_report+0xbb/0x1c0
> >    _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
> >    rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
> >    rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
> >    ...
> >    </TASK>
> >
> > The root cause of the problem is that the comparison order of
> > "prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
> > _rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
> > strings from tail to head, which causes the problem. In the
> > _rtl8812ae_phy_set_txpower_limit(), it was originally intended to meet
> > this requirement by carefully designing the comparison order.
> > For example, "pregulation" and "pbandwidth" are compared in order of
> > length from small to large, first is 3 and last is 4. However, the
> > comparison order of "prate_section" dose not obey such order requirement,
> > therefore when "prate_section" is "HT", when comparing from tail to head,
> > it will lead to access out of bounds in _rtl8812ae_eq_n_byte(). As
> > mentioned above, the _rtl8812ae_eq_n_byte() has the same function as
> > strcmp(), so just strcmp() is enough.
> >
> > Fix it by removing _rtl8812ae_eq_n_byte() and use strcmp() barely.
> > Although it can be fixed by adjusting the comparison order of
> > "prate_section", this may cause the value of "rate_section" to not be
> > from 0 to 5. In addition, commit "21e4b0726dc6" not only moved driver
> > from staging to regular tree, but also added setting txpower limit
> > function during the driver config phase, so the problem was introduced
> > by this commit.
> >
> > Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
> > Signed-off-by: Li Zetao <lizetao1@huawei.com>
> 
> Thanks for your fix.
> 
> Acked-by: Ping-Ke Shih <pkshih@realtek.com>
> 
> > ---
> > v1 -> v2: delete the third parameter of _rtl8812ae_eq_n_byte() and use
> > strcmp to replace loop comparison.
> > v2 -> v3: remove _rtl8812ae_eq_n_byte() and use strcmp() barely.
> >
> >  .../wireless/realtek/rtlwifi/rtl8821ae/phy.c  | 52 +++++++------------
> >  1 file changed, 20 insertions(+), 32 deletions(-)
> >
> 
> [...]


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

* [PATCH v3] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-11 11:21     ` Ping-Ke Shih
@ 2022-12-12  2:35       ` Li Zetao
  2022-12-12  1:35         ` Ping-Ke Shih
  2022-12-12  1:37         ` Ping-Ke Shih
  0 siblings, 2 replies; 12+ messages in thread
From: Li Zetao @ 2022-12-12  2:35 UTC (permalink / raw)
  To: pkshih
  Cc: Larry.Finger, davem, edumazet, kuba, kvalo, linux-kernel,
	linux-wireless, linville, lizetao1, netdev, pabeni

There is a global-out-of-bounds reported by KASAN:

  BUG: KASAN: global-out-of-bounds in
  _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
  Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411

  CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
  6.1.0-rc8+ #144 e15588508517267d37
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
  Call Trace:
   <TASK>
   ...
   kasan_report+0xbb/0x1c0
   _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
   rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
   rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
   ...
   </TASK>

The root cause of the problem is that the comparison order of
"prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
_rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
strings from tail to head, which causes the problem. In the
_rtl8812ae_phy_set_txpower_limit(), it was originally intended to meet
this requirement by carefully designing the comparison order.
For example, "pregulation" and "pbandwidth" are compared in order of
length from small to large, first is 3 and last is 4. However, the
comparison order of "prate_section" dose not obey such order requirement,
therefore when "prate_section" is "HT", when comparing from tail to head,
it will lead to access out of bounds in _rtl8812ae_eq_n_byte(). As
mentioned above, the _rtl8812ae_eq_n_byte() has the same function as
strcmp(), so just strcmp() is enough.

Fix it by removing _rtl8812ae_eq_n_byte() and use strcmp() barely.
Although it can be fixed by adjusting the comparison order of
"prate_section", this may cause the value of "rate_section" to not be
from 0 to 5. In addition, commit "21e4b0726dc6" not only moved driver
from staging to regular tree, but also added setting txpower limit
function during the driver config phase, so the problem was introduced
by this commit.

Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
v1 -> v2: delete the third parameter of _rtl8812ae_eq_n_byte() and use
strcmp to replace loop comparison.
v2 -> v3: remove _rtl8812ae_eq_n_byte() and use strcmp() barely.

 .../wireless/realtek/rtlwifi/rtl8821ae/phy.c  | 52 +++++++------------
 1 file changed, 20 insertions(+), 32 deletions(-)

diff --git a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
index a29321e2fa72..5323ead30db0 100644
--- a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
+++ b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
@@ -1598,18 +1598,6 @@ static bool _rtl8812ae_get_integer_from_string(const char *str, u8 *pint)
 	return true;
 }
 
-static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2, u32 num)
-{
-	if (num == 0)
-		return false;
-	while (num > 0) {
-		num--;
-		if (str1[num] != str2[num])
-			return false;
-	}
-	return true;
-}
-
 static s8 _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(struct ieee80211_hw *hw,
 					      u8 band, u8 channel)
 {
@@ -1659,42 +1647,42 @@ static void _rtl8812ae_phy_set_txpower_limit(struct ieee80211_hw *hw,
 	power_limit = power_limit > MAX_POWER_INDEX ?
 		      MAX_POWER_INDEX : power_limit;
 
-	if (_rtl8812ae_eq_n_byte(pregulation, "FCC", 3))
+	if (strcmp(pregulation, "FCC") == 0)
 		regulation = 0;
-	else if (_rtl8812ae_eq_n_byte(pregulation, "MKK", 3))
+	else if (strcmp(pregulation, "MKK") == 0)
 		regulation = 1;
-	else if (_rtl8812ae_eq_n_byte(pregulation, "ETSI", 4))
+	else if (strcmp(pregulation, "ETSI") == 0)
 		regulation = 2;
-	else if (_rtl8812ae_eq_n_byte(pregulation, "WW13", 4))
+	else if (strcmp(pregulation, "WW13") == 0)
 		regulation = 3;
 
-	if (_rtl8812ae_eq_n_byte(prate_section, "CCK", 3))
+	if (strcmp(prate_section, "CCK") == 0)
 		rate_section = 0;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "OFDM", 4))
+	else if (strcmp(prate_section, "OFDM") == 0)
 		rate_section = 1;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "HT", 2) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "1T", 2))
+	else if (strcmp(prate_section, "HT") == 0 &&
+		 strcmp(prf_path, "1T") == 0)
 		rate_section = 2;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "HT", 2) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "2T", 2))
+	else if (strcmp(prate_section, "HT") == 0 &&
+		 strcmp(prf_path, "2T") == 0)
 		rate_section = 3;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT", 3) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "1T", 2))
+	else if (strcmp(prate_section, "VHT") == 0 &&
+		 strcmp(prf_path, "1T") == 0)
 		rate_section = 4;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT", 3) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "2T", 2))
+	else if (strcmp(prate_section, "VHT") == 0 &&
+		 strcmp(prf_path, "2T") == 0)
 		rate_section = 5;
 
-	if (_rtl8812ae_eq_n_byte(pbandwidth, "20M", 3))
+	if (strcmp(pbandwidth, "20M") == 0)
 		bandwidth = 0;
-	else if (_rtl8812ae_eq_n_byte(pbandwidth, "40M", 3))
+	else if (strcmp(pbandwidth, "40M") == 0)
 		bandwidth = 1;
-	else if (_rtl8812ae_eq_n_byte(pbandwidth, "80M", 3))
+	else if (strcmp(pbandwidth, "80M") == 0)
 		bandwidth = 2;
-	else if (_rtl8812ae_eq_n_byte(pbandwidth, "160M", 4))
+	else if (strcmp(pbandwidth, "160M") == 0)
 		bandwidth = 3;
 
-	if (_rtl8812ae_eq_n_byte(pband, "2.4G", 4)) {
+	if (strcmp(pband, "2.4G") == 0) {
 		ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
 							       BAND_ON_2_4G,
 							       channel);
@@ -1718,7 +1706,7 @@ static void _rtl8812ae_phy_set_txpower_limit(struct ieee80211_hw *hw,
 			regulation, bandwidth, rate_section, channel_index,
 			rtlphy->txpwr_limit_2_4g[regulation][bandwidth]
 				[rate_section][channel_index][RF90_PATH_A]);
-	} else if (_rtl8812ae_eq_n_byte(pband, "5G", 2)) {
+	} else if (strcmp(pband, "5G") == 0) {
 		ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
 							       BAND_ON_5G,
 							       channel);
-- 
2.31.1


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

* [PATCH v4] wifi: rtlwifi: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-12  1:37         ` Ping-Ke Shih
@ 2022-12-12  2:58           ` Li Zetao
  2022-12-14 12:27             ` Kalle Valo
  0 siblings, 1 reply; 12+ messages in thread
From: Li Zetao @ 2022-12-12  2:58 UTC (permalink / raw)
  To: pkshih
  Cc: Larry.Finger, davem, edumazet, kuba, kvalo, linux-kernel,
	linux-wireless, linville, lizetao1, netdev, pabeni

There is a global-out-of-bounds reported by KASAN:

  BUG: KASAN: global-out-of-bounds in
  _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
  Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411

  CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
  6.1.0-rc8+ #144 e15588508517267d37
  Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
  Call Trace:
   <TASK>
   ...
   kasan_report+0xbb/0x1c0
   _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
   rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
   rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
   ...
   </TASK>

The root cause of the problem is that the comparison order of
"prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
_rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
strings from tail to head, which causes the problem. In the
_rtl8812ae_phy_set_txpower_limit(), it was originally intended to meet
this requirement by carefully designing the comparison order.
For example, "pregulation" and "pbandwidth" are compared in order of
length from small to large, first is 3 and last is 4. However, the
comparison order of "prate_section" dose not obey such order requirement,
therefore when "prate_section" is "HT", when comparing from tail to head,
it will lead to access out of bounds in _rtl8812ae_eq_n_byte(). As
mentioned above, the _rtl8812ae_eq_n_byte() has the same function as
strcmp(), so just strcmp() is enough.

Fix it by removing _rtl8812ae_eq_n_byte() and use strcmp() barely.
Although it can be fixed by adjusting the comparison order of
"prate_section", this may cause the value of "rate_section" to not be
from 0 to 5. In addition, commit "21e4b0726dc6" not only moved driver
from staging to regular tree, but also added setting txpower limit
function during the driver config phase, so the problem was introduced
by this commit.

Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
Signed-off-by: Li Zetao <lizetao1@huawei.com>
Acked-by: Ping-Ke Shih <pkshih@realtek.com>
---
v1 -> v2: delete the third parameter of _rtl8812ae_eq_n_byte() and use
strcmp to replace loop comparison.
v2 -> v3: remove _rtl8812ae_eq_n_byte() and use strcmp() barely.
v3 -> v4: fix subject prefix.

 .../wireless/realtek/rtlwifi/rtl8821ae/phy.c  | 52 +++++++------------
 1 file changed, 20 insertions(+), 32 deletions(-)

diff --git a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
index a29321e2fa72..5323ead30db0 100644
--- a/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
+++ b/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/phy.c
@@ -1598,18 +1598,6 @@ static bool _rtl8812ae_get_integer_from_string(const char *str, u8 *pint)
 	return true;
 }
 
-static bool _rtl8812ae_eq_n_byte(const char *str1, const char *str2, u32 num)
-{
-	if (num == 0)
-		return false;
-	while (num > 0) {
-		num--;
-		if (str1[num] != str2[num])
-			return false;
-	}
-	return true;
-}
-
 static s8 _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(struct ieee80211_hw *hw,
 					      u8 band, u8 channel)
 {
@@ -1659,42 +1647,42 @@ static void _rtl8812ae_phy_set_txpower_limit(struct ieee80211_hw *hw,
 	power_limit = power_limit > MAX_POWER_INDEX ?
 		      MAX_POWER_INDEX : power_limit;
 
-	if (_rtl8812ae_eq_n_byte(pregulation, "FCC", 3))
+	if (strcmp(pregulation, "FCC") == 0)
 		regulation = 0;
-	else if (_rtl8812ae_eq_n_byte(pregulation, "MKK", 3))
+	else if (strcmp(pregulation, "MKK") == 0)
 		regulation = 1;
-	else if (_rtl8812ae_eq_n_byte(pregulation, "ETSI", 4))
+	else if (strcmp(pregulation, "ETSI") == 0)
 		regulation = 2;
-	else if (_rtl8812ae_eq_n_byte(pregulation, "WW13", 4))
+	else if (strcmp(pregulation, "WW13") == 0)
 		regulation = 3;
 
-	if (_rtl8812ae_eq_n_byte(prate_section, "CCK", 3))
+	if (strcmp(prate_section, "CCK") == 0)
 		rate_section = 0;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "OFDM", 4))
+	else if (strcmp(prate_section, "OFDM") == 0)
 		rate_section = 1;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "HT", 2) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "1T", 2))
+	else if (strcmp(prate_section, "HT") == 0 &&
+		 strcmp(prf_path, "1T") == 0)
 		rate_section = 2;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "HT", 2) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "2T", 2))
+	else if (strcmp(prate_section, "HT") == 0 &&
+		 strcmp(prf_path, "2T") == 0)
 		rate_section = 3;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT", 3) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "1T", 2))
+	else if (strcmp(prate_section, "VHT") == 0 &&
+		 strcmp(prf_path, "1T") == 0)
 		rate_section = 4;
-	else if (_rtl8812ae_eq_n_byte(prate_section, "VHT", 3) &&
-		 _rtl8812ae_eq_n_byte(prf_path, "2T", 2))
+	else if (strcmp(prate_section, "VHT") == 0 &&
+		 strcmp(prf_path, "2T") == 0)
 		rate_section = 5;
 
-	if (_rtl8812ae_eq_n_byte(pbandwidth, "20M", 3))
+	if (strcmp(pbandwidth, "20M") == 0)
 		bandwidth = 0;
-	else if (_rtl8812ae_eq_n_byte(pbandwidth, "40M", 3))
+	else if (strcmp(pbandwidth, "40M") == 0)
 		bandwidth = 1;
-	else if (_rtl8812ae_eq_n_byte(pbandwidth, "80M", 3))
+	else if (strcmp(pbandwidth, "80M") == 0)
 		bandwidth = 2;
-	else if (_rtl8812ae_eq_n_byte(pbandwidth, "160M", 4))
+	else if (strcmp(pbandwidth, "160M") == 0)
 		bandwidth = 3;
 
-	if (_rtl8812ae_eq_n_byte(pband, "2.4G", 4)) {
+	if (strcmp(pband, "2.4G") == 0) {
 		ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
 							       BAND_ON_2_4G,
 							       channel);
@@ -1718,7 +1706,7 @@ static void _rtl8812ae_phy_set_txpower_limit(struct ieee80211_hw *hw,
 			regulation, bandwidth, rate_section, channel_index,
 			rtlphy->txpwr_limit_2_4g[regulation][bandwidth]
 				[rate_section][channel_index][RF90_PATH_A]);
-	} else if (_rtl8812ae_eq_n_byte(pband, "5G", 2)) {
+	} else if (strcmp(pband, "5G") == 0) {
 		ret = _rtl8812ae_phy_get_chnl_idx_of_txpwr_lmt(hw,
 							       BAND_ON_5G,
 							       channel);
-- 
2.31.1


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

* Re: [PATCH v4] wifi: rtlwifi: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()
  2022-12-12  2:58           ` [PATCH v4] wifi: rtlwifi: " Li Zetao
@ 2022-12-14 12:27             ` Kalle Valo
  0 siblings, 0 replies; 12+ messages in thread
From: Kalle Valo @ 2022-12-14 12:27 UTC (permalink / raw)
  To: Li Zetao
  Cc: pkshih, Larry.Finger, davem, edumazet, kuba, linux-kernel,
	linux-wireless, linville, lizetao1, netdev, pabeni

Li Zetao <lizetao1@huawei.com> wrote:

> There is a global-out-of-bounds reported by KASAN:
> 
>   BUG: KASAN: global-out-of-bounds in
>   _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>   Read of size 1 at addr ffffffffa0773c43 by task NetworkManager/411
> 
>   CPU: 6 PID: 411 Comm: NetworkManager Tainted: G      D
>   6.1.0-rc8+ #144 e15588508517267d37
>   Hardware name: QEMU Standard PC (Q35 + ICH9, 2009),
>   Call Trace:
>    <TASK>
>    ...
>    kasan_report+0xbb/0x1c0
>    _rtl8812ae_eq_n_byte.part.0+0x3d/0x84 [rtl8821ae]
>    rtl8821ae_phy_bb_config.cold+0x346/0x641 [rtl8821ae]
>    rtl8821ae_hw_init+0x1f5e/0x79b0 [rtl8821ae]
>    ...
>    </TASK>
> 
> The root cause of the problem is that the comparison order of
> "prate_section" in _rtl8812ae_phy_set_txpower_limit() is wrong. The
> _rtl8812ae_eq_n_byte() is used to compare the first n bytes of the two
> strings from tail to head, which causes the problem. In the
> _rtl8812ae_phy_set_txpower_limit(), it was originally intended to meet
> this requirement by carefully designing the comparison order.
> For example, "pregulation" and "pbandwidth" are compared in order of
> length from small to large, first is 3 and last is 4. However, the
> comparison order of "prate_section" dose not obey such order requirement,
> therefore when "prate_section" is "HT", when comparing from tail to head,
> it will lead to access out of bounds in _rtl8812ae_eq_n_byte(). As
> mentioned above, the _rtl8812ae_eq_n_byte() has the same function as
> strcmp(), so just strcmp() is enough.
> 
> Fix it by removing _rtl8812ae_eq_n_byte() and use strcmp() barely.
> Although it can be fixed by adjusting the comparison order of
> "prate_section", this may cause the value of "rate_section" to not be
> from 0 to 5. In addition, commit "21e4b0726dc6" not only moved driver
> from staging to regular tree, but also added setting txpower limit
> function during the driver config phase, so the problem was introduced
> by this commit.
> 
> Fixes: 21e4b0726dc6 ("rtlwifi: rtl8821ae: Move driver from staging to regular tree")
> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> Acked-by: Ping-Ke Shih <pkshih@realtek.com>

Patch applied to wireless-next.git, thanks.

117dbeda22ec wifi: rtlwifi: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit()

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/20221212025812.1541311-1-lizetao1@huawei.com/

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


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

end of thread, other threads:[~2022-12-14 12:30 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-07 15:23 [PATCH] rtlwifi: rtl8821ae: Fix global-out-of-bounds bug in _rtl8812ae_phy_set_txpower_limit() Li Zetao
2022-12-09  5:11 ` Ping-Ke Shih
2022-12-10 12:47   ` Li Zetao
2022-12-10 13:15     ` Ping-Ke Shih
2022-12-10 13:52       ` Li Zetao
2022-12-10 16:23   ` [PATCH v2] " Li Zetao
2022-12-11 11:21     ` Ping-Ke Shih
2022-12-12  2:35       ` [PATCH v3] " Li Zetao
2022-12-12  1:35         ` Ping-Ke Shih
2022-12-12  1:37         ` Ping-Ke Shih
2022-12-12  2:58           ` [PATCH v4] wifi: rtlwifi: " Li Zetao
2022-12-14 12:27             ` 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).