linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] staging: rtl8723bs: Fix two possible sleep-in-atomic-context bugs in translate_scan()
@ 2018-06-20  9:50 Jia-Ju Bai
  2018-06-20  9:56 ` Dan Carpenter
  0 siblings, 1 reply; 4+ messages in thread
From: Jia-Ju Bai @ 2018-06-20  9:50 UTC (permalink / raw)
  To: gregkh, quytelda, Larry.Finger, harshasharmaiitr,
	arushisinghal19971997, amitoj1606, jeremy.lefaure, teo.dacquet
  Cc: devel, linux-kernel, Jia-Ju Bai

The driver may sleep with holding a spinlock.
The function call paths (from bottom to top) in Linux-4.16.7 are:

[FUNC] kzalloc(GFP_KERNEL)
drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 323: 
		kzalloc in translate_scan
drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1554: 
		translate_scan in rtw_wx_get_scan
drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1533: 
		spin_lock_bh in rtw_wx_get_scan

[FUNC] kzalloc(GFP_KERNEL)
drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 455: 
		kzalloc in translate_scan
drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1554: 
		translate_scan in rtw_wx_get_scan
drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1533: 
		spin_lock_bh in rtw_wx_get_scan

To fix these bugs, GFP_KERNEL is replaced with GFP_ATOMIC.

These bugs are found by my static analysis tool (DSAC-2) and checked by
my code review.

Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
 drivers/staging/rtl8723bs/os_dep/ioctl_linux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
index b26533983864..7632b8974563 100644
--- a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
+++ b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
@@ -321,7 +321,7 @@ static char *translate_scan(struct adapter *padapter,
 		RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_wx_get_scan: ssid =%s\n", pnetwork->network.Ssid.Ssid));
 		RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_wx_get_scan: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
 
-		buf = kzalloc(MAX_WPA_IE_LEN*2, GFP_KERNEL);
+		buf = kzalloc(MAX_WPA_IE_LEN*2, GFP_ATOMIC);
 		if (!buf)
 			return start;
 		if (wpa_len > 0) {
@@ -453,7 +453,7 @@ static char *translate_scan(struct adapter *padapter,
 		u8 *buf;
 		u8 *p, *pos;
 
-		buf = kzalloc(MAX_WPA_IE_LEN, GFP_KERNEL);
+		buf = kzalloc(MAX_WPA_IE_LEN, GFP_ATOMIC);
 		if (!buf)
 			goto exit;
 		p = buf;
-- 
2.17.0


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

* Re: [PATCH] staging: rtl8723bs: Fix two possible sleep-in-atomic-context bugs in translate_scan()
  2018-06-20  9:50 [PATCH] staging: rtl8723bs: Fix two possible sleep-in-atomic-context bugs in translate_scan() Jia-Ju Bai
@ 2018-06-20  9:56 ` Dan Carpenter
  2018-06-20 10:05   ` Jia-Ju Bai
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2018-06-20  9:56 UTC (permalink / raw)
  To: Jia-Ju Bai, cocci, Julia Lawall
  Cc: gregkh, quytelda, Larry.Finger, harshasharmaiitr,
	arushisinghal19971997, amitoj1606, jeremy.lefaure, teo.dacquet,
	devel, linux-kernel

On Wed, Jun 20, 2018 at 05:50:16PM +0800, Jia-Ju Bai wrote:
> The driver may sleep with holding a spinlock.
> The function call paths (from bottom to top) in Linux-4.16.7 are:
> 
> [FUNC] kzalloc(GFP_KERNEL)
> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 323: 
> 		kzalloc in translate_scan
> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1554: 
> 		translate_scan in rtw_wx_get_scan
> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1533: 
> 		spin_lock_bh in rtw_wx_get_scan
> 
> [FUNC] kzalloc(GFP_KERNEL)
> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 455: 
> 		kzalloc in translate_scan
> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1554: 
> 		translate_scan in rtw_wx_get_scan
> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1533: 
> 		spin_lock_bh in rtw_wx_get_scan
> 
> To fix these bugs, GFP_KERNEL is replaced with GFP_ATOMIC.
> 
> These bugs are found by my static analysis tool (DSAC-2) and checked by
> my code review.
> 
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
>  drivers/staging/rtl8723bs/os_dep/ioctl_linux.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
> index b26533983864..7632b8974563 100644
> --- a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
> +++ b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
> @@ -321,7 +321,7 @@ static char *translate_scan(struct adapter *padapter,
>  		RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_wx_get_scan: ssid =%s\n", pnetwork->network.Ssid.Ssid));
>  		RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_wx_get_scan: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
>  
> -		buf = kzalloc(MAX_WPA_IE_LEN*2, GFP_KERNEL);
> +		buf = kzalloc(MAX_WPA_IE_LEN*2, GFP_ATOMIC);
>  		if (!buf)
>  			return start;

Thanks!  It occurs to me that another way to detect this bug is that
one of the allocations in this function already uses GFP_ATOMIC.  It
doesn't normally make sense to mix GFP_ATOMIC and GFP_KERNEL when there
isn't any locking in the function.

regards,
dan carpenter


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

* Re: [PATCH] staging: rtl8723bs: Fix two possible sleep-in-atomic-context bugs in translate_scan()
  2018-06-20  9:56 ` Dan Carpenter
@ 2018-06-20 10:05   ` Jia-Ju Bai
  2018-06-20 10:22     ` Dan Carpenter
  0 siblings, 1 reply; 4+ messages in thread
From: Jia-Ju Bai @ 2018-06-20 10:05 UTC (permalink / raw)
  To: Dan Carpenter, cocci, Julia Lawall
  Cc: gregkh, quytelda, Larry.Finger, harshasharmaiitr,
	arushisinghal19971997, amitoj1606, jeremy.lefaure, teo.dacquet,
	devel, linux-kernel



On 2018/6/20 17:56, Dan Carpenter wrote:
> On Wed, Jun 20, 2018 at 05:50:16PM +0800, Jia-Ju Bai wrote:
>> The driver may sleep with holding a spinlock.
>> The function call paths (from bottom to top) in Linux-4.16.7 are:
>>
>> [FUNC] kzalloc(GFP_KERNEL)
>> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 323:
>> 		kzalloc in translate_scan
>> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1554:
>> 		translate_scan in rtw_wx_get_scan
>> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1533:
>> 		spin_lock_bh in rtw_wx_get_scan
>>
>> [FUNC] kzalloc(GFP_KERNEL)
>> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 455:
>> 		kzalloc in translate_scan
>> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1554:
>> 		translate_scan in rtw_wx_get_scan
>> drivers/staging/rtl8723bs/os_dep/ioctl_linux.c, 1533:
>> 		spin_lock_bh in rtw_wx_get_scan
>>
>> To fix these bugs, GFP_KERNEL is replaced with GFP_ATOMIC.
>>
>> These bugs are found by my static analysis tool (DSAC-2) and checked by
>> my code review.
>>
>> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
>> ---
>>   drivers/staging/rtl8723bs/os_dep/ioctl_linux.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
>> index b26533983864..7632b8974563 100644
>> --- a/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
>> +++ b/drivers/staging/rtl8723bs/os_dep/ioctl_linux.c
>> @@ -321,7 +321,7 @@ static char *translate_scan(struct adapter *padapter,
>>   		RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_wx_get_scan: ssid =%s\n", pnetwork->network.Ssid.Ssid));
>>   		RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_wx_get_scan: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
>>   
>> -		buf = kzalloc(MAX_WPA_IE_LEN*2, GFP_KERNEL);
>> +		buf = kzalloc(MAX_WPA_IE_LEN*2, GFP_ATOMIC);
>>   		if (!buf)
>>   			return start;
> Thanks!  It occurs to me that another way to detect this bug is that
> one of the allocations in this function already uses GFP_ATOMIC.  It
> doesn't normally make sense to mix GFP_ATOMIC and GFP_KERNEL when there
> isn't any locking in the function.

Yes, this pattern is interesting for bug finding :)
But to fix the bugs of this pattern, we need to decide whether 
GFP_ATOMIC or GFP_KERNEL should be used here.


Best wishes,
Jia-Ju Bai

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

* Re: [PATCH] staging: rtl8723bs: Fix two possible sleep-in-atomic-context bugs in translate_scan()
  2018-06-20 10:05   ` Jia-Ju Bai
@ 2018-06-20 10:22     ` Dan Carpenter
  0 siblings, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2018-06-20 10:22 UTC (permalink / raw)
  To: Jia-Ju Bai
  Cc: cocci, Julia Lawall, gregkh, quytelda, Larry.Finger,
	harshasharmaiitr, arushisinghal19971997, amitoj1606,
	jeremy.lefaure, teo.dacquet, devel, linux-kernel

On Wed, Jun 20, 2018 at 06:05:06PM +0800, Jia-Ju Bai wrote:
> > Thanks!  It occurs to me that another way to detect this bug is that
> > one of the allocations in this function already uses GFP_ATOMIC.  It
> > doesn't normally make sense to mix GFP_ATOMIC and GFP_KERNEL when there
> > isn't any locking in the function.
> 
> Yes, this pattern is interesting for bug finding :)
> But to fix the bugs of this pattern, we need to decide whether GFP_ATOMIC or
> GFP_KERNEL should be used here.
> 

Sure.  But either way it's a bug. Plus this would be the first static
checker warning which warns about using GFP_ATOMIC when it's supposed to
be GFP_KERNEL.  #milestone

regards,
dan carpenter

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

end of thread, other threads:[~2018-06-20 10:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-20  9:50 [PATCH] staging: rtl8723bs: Fix two possible sleep-in-atomic-context bugs in translate_scan() Jia-Ju Bai
2018-06-20  9:56 ` Dan Carpenter
2018-06-20 10:05   ` Jia-Ju Bai
2018-06-20 10:22     ` 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).