public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] staging: rtl8723bs: fix error handling and memory leaks
@ 2026-01-08 18:16 Samasth Norway Ananda
  2026-01-08 18:16 ` [PATCH v2 1/3] staging: rtl8723bs: fix firmware memory leak on error Samasth Norway Ananda
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Samasth Norway Ananda @ 2026-01-08 18:16 UTC (permalink / raw)
  To: dan.carpenter, gregkh; +Cc: linux-staging, linux-kernel

This series fixes memory leaks and missing error checks in rtl8723bs:

1. Firmware not released on error paths in rtl8723b_FirmwareDownload().
2. Buffer not freed when cfg80211_inform_bss_frame() fails.
3. Missing IS_ERR() check for kthread_run() in rtl8723b_start_thread().

Changes in v2:
-> Patch 1: Call release_firmware() directly in error paths instead of
using intermediate goto label.
-> Dropped patch 4 (rtw_wdev_alloc) to study cleanup chain further.

Samasth Norway Ananda (3):
  staging: rtl8723bs: fix firmware memory leak on error
  staging: rtl8723bs: fix memory leak in rtw_cfg80211_inform_bss()
  staging: rtl8723bs: add IS_ERR() check for kthread_run()

 drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c | 4 ++++
 drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 4 +++-
 2 files changed, 7 insertions(+), 1 deletion(-)

-- 
2.50.1


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

* [PATCH v2 1/3] staging: rtl8723bs: fix firmware memory leak on error
  2026-01-08 18:16 [PATCH v2 0/3] staging: rtl8723bs: fix error handling and memory leaks Samasth Norway Ananda
@ 2026-01-08 18:16 ` Samasth Norway Ananda
  2026-01-09  6:14   ` Dan Carpenter
  2026-01-08 18:16 ` [PATCH v2 2/3] staging: rtl8723bs: fix memory leak in rtw_cfg80211_inform_bss() Samasth Norway Ananda
  2026-01-08 18:16 ` [PATCH v2 3/3] staging: rtl8723bs: add IS_ERR() check for kthread_run() Samasth Norway Ananda
  2 siblings, 1 reply; 8+ messages in thread
From: Samasth Norway Ananda @ 2026-01-08 18:16 UTC (permalink / raw)
  To: dan.carpenter, gregkh; +Cc: linux-staging, linux-kernel

After successfully calling request_firmware(), if the firmware size
check fails or if kmemdup() fails, the code jumps to the exit label
without calling release_firmware(), causing a memory leak. Call
release_firmware() directly in each error path before jumping to cleanup
label.

Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>
---
 drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c b/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
index 57c83f332e74..56ceedd5a26a 100644
--- a/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
+++ b/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
@@ -346,12 +346,14 @@ s32 rtl8723b_FirmwareDownload(struct adapter *padapter, bool  bUsedWoWLANFw)
 
 	if (fw->size > FW_8723B_SIZE) {
 		rtStatus = _FAIL;
+		release_firmware(fw);
 		goto exit;
 	}
 
 	pFirmware->fw_buffer_sz = kmemdup(fw->data, fw->size, GFP_KERNEL);
 	if (!pFirmware->fw_buffer_sz) {
 		rtStatus = _FAIL;
+		release_firmware(fw);
 		goto exit;
 	}
 
-- 
2.50.1


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

* [PATCH v2 2/3] staging: rtl8723bs: fix memory leak in rtw_cfg80211_inform_bss()
  2026-01-08 18:16 [PATCH v2 0/3] staging: rtl8723bs: fix error handling and memory leaks Samasth Norway Ananda
  2026-01-08 18:16 ` [PATCH v2 1/3] staging: rtl8723bs: fix firmware memory leak on error Samasth Norway Ananda
@ 2026-01-08 18:16 ` Samasth Norway Ananda
  2026-01-09  6:16   ` Dan Carpenter
  2026-01-08 18:16 ` [PATCH v2 3/3] staging: rtl8723bs: add IS_ERR() check for kthread_run() Samasth Norway Ananda
  2 siblings, 1 reply; 8+ messages in thread
From: Samasth Norway Ananda @ 2026-01-08 18:16 UTC (permalink / raw)
  To: dan.carpenter, gregkh; +Cc: linux-staging, linux-kernel

After successfully allocating buf with kzalloc(), if
cfg80211_inform_bss_frame() returns NULL, the code jumps to the exit
label without freeing buf, causing a memory leak. Add kfree(buf) before
the goto to properly free the buffer in this error case.

Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>
---
 drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
index 60edeae1cffe..d80e23cfdf8d 100644
--- a/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
+++ b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
@@ -314,8 +314,10 @@ struct cfg80211_bss *rtw_cfg80211_inform_bss(struct adapter *padapter, struct wl
 	bss = cfg80211_inform_bss_frame(wiphy, notify_channel, (struct ieee80211_mgmt *)buf,
 					len, notify_signal, GFP_ATOMIC);
 
-	if (unlikely(!bss))
+	if (unlikely(!bss)) {
+		kfree(buf);
 		goto exit;
+	}
 
 	cfg80211_put_bss(wiphy, bss);
 	kfree(buf);
-- 
2.50.1


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

* [PATCH v2 3/3] staging: rtl8723bs: add IS_ERR() check for kthread_run()
  2026-01-08 18:16 [PATCH v2 0/3] staging: rtl8723bs: fix error handling and memory leaks Samasth Norway Ananda
  2026-01-08 18:16 ` [PATCH v2 1/3] staging: rtl8723bs: fix firmware memory leak on error Samasth Norway Ananda
  2026-01-08 18:16 ` [PATCH v2 2/3] staging: rtl8723bs: fix memory leak in rtw_cfg80211_inform_bss() Samasth Norway Ananda
@ 2026-01-08 18:16 ` Samasth Norway Ananda
  2026-01-09  4:50   ` Greg KH
  2 siblings, 1 reply; 8+ messages in thread
From: Samasth Norway Ananda @ 2026-01-08 18:16 UTC (permalink / raw)
  To: dan.carpenter, gregkh; +Cc: linux-staging, linux-kernel

kthread_run() returns an ERR_PTR on failure, not NULL. Without this
check, rtl8723b_stop_thread() would later check "if
(xmitpriv->SdioXmitThread)" which evaluates to true for error pointers,
potentially causing issues when trying to complete or wait on an invalid
thread. Set the pointer to NULL on failure to prevent later code from
attempting to use an invalid thread pointer.

Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>
---
 drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c b/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
index 56ceedd5a26a..27d490204fcc 100644
--- a/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
+++ b/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
@@ -2922,6 +2922,8 @@ void rtl8723b_start_thread(struct adapter *padapter)
 	struct xmit_priv *xmitpriv = &padapter->xmitpriv;
 
 	xmitpriv->SdioXmitThread = kthread_run(rtl8723bs_xmit_thread, padapter, "RTWHALXT");
+	if (IS_ERR(xmitpriv->SdioXmitThread))
+		xmitpriv->SdioXmitThread = NULL
 }
 
 void rtl8723b_stop_thread(struct adapter *padapter)
-- 
2.50.1


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

* Re: [PATCH v2 3/3] staging: rtl8723bs: add IS_ERR() check for kthread_run()
  2026-01-08 18:16 ` [PATCH v2 3/3] staging: rtl8723bs: add IS_ERR() check for kthread_run() Samasth Norway Ananda
@ 2026-01-09  4:50   ` Greg KH
  2026-01-09  6:36     ` [External] : " samasth.norway.ananda
  0 siblings, 1 reply; 8+ messages in thread
From: Greg KH @ 2026-01-09  4:50 UTC (permalink / raw)
  To: Samasth Norway Ananda; +Cc: dan.carpenter, linux-staging, linux-kernel

On Thu, Jan 08, 2026 at 10:16:11AM -0800, Samasth Norway Ananda wrote:
> kthread_run() returns an ERR_PTR on failure, not NULL. Without this
> check, rtl8723b_stop_thread() would later check "if
> (xmitpriv->SdioXmitThread)" which evaluates to true for error pointers,
> potentially causing issues when trying to complete or wait on an invalid
> thread. Set the pointer to NULL on failure to prevent later code from
> attempting to use an invalid thread pointer.
> 
> Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>
> ---
>  drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c b/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
> index 56ceedd5a26a..27d490204fcc 100644
> --- a/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
> +++ b/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
> @@ -2922,6 +2922,8 @@ void rtl8723b_start_thread(struct adapter *padapter)
>  	struct xmit_priv *xmitpriv = &padapter->xmitpriv;
>  
>  	xmitpriv->SdioXmitThread = kthread_run(rtl8723bs_xmit_thread, padapter, "RTWHALXT");
> +	if (IS_ERR(xmitpriv->SdioXmitThread))
> +		xmitpriv->SdioXmitThread = NULL

Shouldn't the function be returning an errror if this fails instead of
relying on the pointer to be NULL?

And this is a "wrapper function" and only called in one place, why not
just get rid of it entirely?  Same for rtl8723b_stop_thread()?

thanks,

greg k-h

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

* Re: [PATCH v2 1/3] staging: rtl8723bs: fix firmware memory leak on error
  2026-01-08 18:16 ` [PATCH v2 1/3] staging: rtl8723bs: fix firmware memory leak on error Samasth Norway Ananda
@ 2026-01-09  6:14   ` Dan Carpenter
  0 siblings, 0 replies; 8+ messages in thread
From: Dan Carpenter @ 2026-01-09  6:14 UTC (permalink / raw)
  To: Samasth Norway Ananda; +Cc: gregkh, linux-staging, linux-kernel

On Thu, Jan 08, 2026 at 10:16:09AM -0800, Samasth Norway Ananda wrote:
> After successfully calling request_firmware(), if the firmware size
> check fails or if kmemdup() fails, the code jumps to the exit label
> without calling release_firmware(), causing a memory leak. Call
> release_firmware() directly in each error path before jumping to cleanup
> label.
> 
> Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>

Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org>

regards,
dan carpenter


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

* Re: [PATCH v2 2/3] staging: rtl8723bs: fix memory leak in rtw_cfg80211_inform_bss()
  2026-01-08 18:16 ` [PATCH v2 2/3] staging: rtl8723bs: fix memory leak in rtw_cfg80211_inform_bss() Samasth Norway Ananda
@ 2026-01-09  6:16   ` Dan Carpenter
  0 siblings, 0 replies; 8+ messages in thread
From: Dan Carpenter @ 2026-01-09  6:16 UTC (permalink / raw)
  To: Samasth Norway Ananda; +Cc: gregkh, linux-staging, linux-kernel

On Thu, Jan 08, 2026 at 10:16:10AM -0800, Samasth Norway Ananda wrote:
> After successfully allocating buf with kzalloc(), if
> cfg80211_inform_bss_frame() returns NULL, the code jumps to the exit
> label without freeing buf, causing a memory leak. Add kfree(buf) before
> the goto to properly free the buffer in this error case.
> 
> Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>
> ---
>  drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
> index 60edeae1cffe..d80e23cfdf8d 100644
> --- a/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
> +++ b/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c
> @@ -314,8 +314,10 @@ struct cfg80211_bss *rtw_cfg80211_inform_bss(struct adapter *padapter, struct wl
>  	bss = cfg80211_inform_bss_frame(wiphy, notify_channel, (struct ieee80211_mgmt *)buf,
>  					len, notify_signal, GFP_ATOMIC);
>  
> -	if (unlikely(!bss))
> +	if (unlikely(!bss)) {
> +		kfree(buf);
>  		goto exit;
> +	}
>  
>  	cfg80211_put_bss(wiphy, bss);
>  	kfree(buf);

This code is so ugly but that's not really related to your patch...

Reviewed-by: Dan Carpenter <dan.carpenter@linaro.org>

regards,
dan carpenter

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

* Re: [External] : Re: [PATCH v2 3/3] staging: rtl8723bs: add IS_ERR() check for kthread_run()
  2026-01-09  4:50   ` Greg KH
@ 2026-01-09  6:36     ` samasth.norway.ananda
  0 siblings, 0 replies; 8+ messages in thread
From: samasth.norway.ananda @ 2026-01-09  6:36 UTC (permalink / raw)
  To: Greg KH; +Cc: dan.carpenter, linux-staging, linux-kernel



On 1/8/26 8:50 PM, Greg KH wrote:
> On Thu, Jan 08, 2026 at 10:16:11AM -0800, Samasth Norway Ananda wrote:
>> kthread_run() returns an ERR_PTR on failure, not NULL. Without this
>> check, rtl8723b_stop_thread() would later check "if
>> (xmitpriv->SdioXmitThread)" which evaluates to true for error pointers,
>> potentially causing issues when trying to complete or wait on an invalid
>> thread. Set the pointer to NULL on failure to prevent later code from
>> attempting to use an invalid thread pointer.
>>
>> Signed-off-by: Samasth Norway Ananda <samasth.norway.ananda@oracle.com>
>> ---
>>   drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c b/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
>> index 56ceedd5a26a..27d490204fcc 100644
>> --- a/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
>> +++ b/drivers/staging/rtl8723bs/hal/rtl8723b_hal_init.c
>> @@ -2922,6 +2922,8 @@ void rtl8723b_start_thread(struct adapter *padapter)
>>   	struct xmit_priv *xmitpriv = &padapter->xmitpriv;
>>   
>>   	xmitpriv->SdioXmitThread = kthread_run(rtl8723bs_xmit_thread, padapter, "RTWHALXT");
>> +	if (IS_ERR(xmitpriv->SdioXmitThread))
>> +		xmitpriv->SdioXmitThread = NULL
> 
> Shouldn't the function be returning an errror if this fails instead of
> relying on the pointer to be NULL?
> 
> And this is a "wrapper function" and only called in one place, why not
> just get rid of it entirely?  Same for rtl8723b_stop_thread()?

Hi Greg,

Thanks for the feedback. You're right, just setting the pointer to NULL 
just hides the error. The wrapper functions are unnecessary as well.

I will send a v3 that removes rtl8723b_start_thread(), 
rtl8723b_stop_thread(), rtw_hal_start_thread() and rtw_hal_stop_thread() 
entirely, inlining the kthread handling directly to 
rtw_start_drv_threads() and rtw_stop_drv_threads() with proper IS_ERR() 
checking.

Thanks,
Samasth.

> 
> thanks,
> 
> greg k-h


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

end of thread, other threads:[~2026-01-09  6:36 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-08 18:16 [PATCH v2 0/3] staging: rtl8723bs: fix error handling and memory leaks Samasth Norway Ananda
2026-01-08 18:16 ` [PATCH v2 1/3] staging: rtl8723bs: fix firmware memory leak on error Samasth Norway Ananda
2026-01-09  6:14   ` Dan Carpenter
2026-01-08 18:16 ` [PATCH v2 2/3] staging: rtl8723bs: fix memory leak in rtw_cfg80211_inform_bss() Samasth Norway Ananda
2026-01-09  6:16   ` Dan Carpenter
2026-01-08 18:16 ` [PATCH v2 3/3] staging: rtl8723bs: add IS_ERR() check for kthread_run() Samasth Norway Ananda
2026-01-09  4:50   ` Greg KH
2026-01-09  6:36     ` [External] : " samasth.norway.ananda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox