public inbox for linux-wireless@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
@ 2026-01-16  7:48 Zilin Guan
  2026-01-16  8:23 ` Zong-Zhe Yang
  2026-01-16 11:04 ` Markus Elfring
  0 siblings, 2 replies; 7+ messages in thread
From: Zilin Guan @ 2026-01-16  7:48 UTC (permalink / raw)
  To: pkshih; +Cc: jianhao.xu, linux-wireless, linux-kernel, Zilin Guan

In __print_txpwr_map(), memory is allocated to bufp via vzalloc().
If max_valid_addr is 0, the function returns -EOPNOTSUPP immediately
without freeing bufp, leading to a memory leak.

Fix this by freeing the temporary buffer bufp in the error path.

Compile tested only. Issue found using a prototype static analysis tool
and code review.

Fixes: 036042e15770 ("wifi: rtw89: debug: txpwr table supports Wi-Fi 7 chips")
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
 drivers/net/wireless/realtek/rtw89/debug.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wireless/realtek/rtw89/debug.c b/drivers/net/wireless/realtek/rtw89/debug.c
index 1264c2f82600..c7bd1d0212b6 100644
--- a/drivers/net/wireless/realtek/rtw89/debug.c
+++ b/drivers/net/wireless/realtek/rtw89/debug.c
@@ -834,8 +834,10 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev, char *buf, size_t buf
 	else
 		max_valid_addr = map->addr_to;
 
-	if (max_valid_addr == 0)
+	if (max_valid_addr == 0) {
+		vfree(bufp);
 		return -EOPNOTSUPP;
+	}
 
 	for (addr = map->addr_from; addr <= max_valid_addr; addr += 4) {
 		ret = rtw89_mac_txpwr_read32(rtwdev, RTW89_PHY_0, addr, &val);
-- 
2.34.1


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

* RE: [PATCH] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-16  7:48 [PATCH] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map() Zilin Guan
@ 2026-01-16  8:23 ` Zong-Zhe Yang
  2026-01-16 12:11   ` Zilin Guan
  2026-01-16 11:04 ` Markus Elfring
  1 sibling, 1 reply; 7+ messages in thread
From: Zong-Zhe Yang @ 2026-01-16  8:23 UTC (permalink / raw)
  To: Zilin Guan, Ping-Ke Shih
  Cc: jianhao.xu@seu.edu.cn, linux-wireless@vger.kernel.org,
	linux-kernel@vger.kernel.org

Zilin Guan <zilin@seu.edu.cn> wrote:
> 
> In __print_txpwr_map(), memory is allocated to bufp via vzalloc().
> If max_valid_addr is 0, the function returns -EOPNOTSUPP immediately
> without freeing bufp, leading to a memory leak.
> 
> Fix this by freeing the temporary buffer bufp in the error path.
> 
> Compile tested only. Issue found using a prototype static analysis tool
> and code review.
> 
> Fixes: 036042e15770 ("wifi: rtw89: debug: txpwr table supports Wi-Fi 7 chips")
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
>  drivers/net/wireless/realtek/rtw89/debug.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/wireless/realtek/rtw89/debug.c
> b/drivers/net/wireless/realtek/rtw89/debug.c
> index 1264c2f82600..c7bd1d0212b6 100644
> --- a/drivers/net/wireless/realtek/rtw89/debug.c
> +++ b/drivers/net/wireless/realtek/rtw89/debug.c
> @@ -834,8 +834,10 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev, char
> *buf, size_t buf
>         else
>                 max_valid_addr = map->addr_to;
> 
> -       if (max_valid_addr == 0)
> +       if (max_valid_addr == 0) {
> +               vfree(bufp);
>                 return -EOPNOTSUPP;
> +       }

Thank you for catching this.
Since the decision for max_valid_addr doesn't depend on bufp,
how about moving vzalloc down here ?

> 
>         for (addr = map->addr_from; addr <= max_valid_addr; addr += 4) {
>                 ret = rtw89_mac_txpwr_read32(rtwdev, RTW89_PHY_0, addr, &val);
> --
> 2.34.1
> 


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

* Re: [PATCH] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-16  7:48 [PATCH] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map() Zilin Guan
  2026-01-16  8:23 ` Zong-Zhe Yang
@ 2026-01-16 11:04 ` Markus Elfring
  2026-01-16 11:41   ` Zong-Zhe Yang
  1 sibling, 1 reply; 7+ messages in thread
From: Markus Elfring @ 2026-01-16 11:04 UTC (permalink / raw)
  To: Zilin Guan, linux-wireless, Ping-Ke Shih
  Cc: LKML, kernel-janitors, Jianhao Xu, Zong-Zhe Yang

…
> Fix this by freeing the temporary buffer bufp in the error path.
…

How do you think about to use an attribute like “__free(vfree)”?
https://elixir.bootlin.com/linux/v6.19-rc5/source/drivers/net/wireless/realtek/rtw89/debug.c#L815-L858

Regards,
Markus

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

* RE: [PATCH] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-16 11:04 ` Markus Elfring
@ 2026-01-16 11:41   ` Zong-Zhe Yang
  2026-01-16 12:38     ` Markus Elfring
  0 siblings, 1 reply; 7+ messages in thread
From: Zong-Zhe Yang @ 2026-01-16 11:41 UTC (permalink / raw)
  To: Markus Elfring, Zilin Guan, linux-wireless@vger.kernel.org,
	Ping-Ke Shih
  Cc: LKML, kernel-janitors@vger.kernel.org, Jianhao Xu

Markus Elfring <Markus.Elfring@web.de> wrote:
> 
> …
> > Fix this by freeing the temporary buffer bufp in the error path.
> …
> 
> How do you think about to use an attribute like “__free(vfree)”?
> https://elixir.bootlin.com/linux/v6.19-rc5/source/drivers/net/wireless/realtek/rtw89/debug.c#
> L815-L858
> 

Using __free(XXX) is fine to me.
But, I don't seem to see DEFINE_FREE(vfree, ...) yet.
So perhaps, need to use __free(kvfree).


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

* RE: [PATCH] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-16  8:23 ` Zong-Zhe Yang
@ 2026-01-16 12:11   ` Zilin Guan
  0 siblings, 0 replies; 7+ messages in thread
From: Zilin Guan @ 2026-01-16 12:11 UTC (permalink / raw)
  To: kevin_yang; +Cc: jianhao.xu, linux-kernel, linux-wireless, pkshih, zilin

On Fri, Jan 16, 2026 at 08:23:57AM +0000, Zong-Zhe Yang wrote:
> > @@ -834,8 +834,10 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev, char
> > *buf, size_t buf
> >         else
> >                 max_valid_addr = map->addr_to;
> > 
> > -       if (max_valid_addr == 0)
> > +       if (max_valid_addr == 0) {
> > +               vfree(bufp);
> >                 return -EOPNOTSUPP;
> > +       }
> 
> Thank you for catching this.
> Since the decision for max_valid_addr doesn't depend on bufp,
> how about moving vzalloc down here ?
> 
> > 
> >         for (addr = map->addr_from; addr <= max_valid_addr; addr += 4) {
> >                 ret = rtw89_mac_txpwr_read32(rtwdev, RTW89_PHY_0, addr, &val);
> > --
> > 2.34.1
> > 

Thanks for your suggestion. I agree that moving vzalloc() after the check 
is a cleaner solution. I will send a v2 patch to address this.

Best Regards,
Zilin Guan

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

* Re: wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-16 11:41   ` Zong-Zhe Yang
@ 2026-01-16 12:38     ` Markus Elfring
  2026-01-16 13:48       ` Johannes Berg
  0 siblings, 1 reply; 7+ messages in thread
From: Markus Elfring @ 2026-01-16 12:38 UTC (permalink / raw)
  To: Zong-Zhe Yang, Zilin Guan, Ping-Ke Shih, linux-wireless,
	kernel-janitors
  Cc: LKML, Jianhao Xu

> But, I don't seem to see DEFINE_FREE(vfree, ...) yet.
…

Would you dare to support the addition of such a macro call anyhow?
https://elixir.bootlin.com/linux/v6.19-rc5/source/include/linux/cleanup.h#L157-L161

Regards,
Markus

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

* Re: wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-16 12:38     ` Markus Elfring
@ 2026-01-16 13:48       ` Johannes Berg
  0 siblings, 0 replies; 7+ messages in thread
From: Johannes Berg @ 2026-01-16 13:48 UTC (permalink / raw)
  To: Markus Elfring, Zong-Zhe Yang, Zilin Guan, Ping-Ke Shih,
	linux-wireless, kernel-janitors
  Cc: LKML, Jianhao Xu

For the record, everyone, please just ignore Markus. I've told him
numerous times to just leave us alone.

johannes

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

end of thread, other threads:[~2026-01-16 13:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-16  7:48 [PATCH] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map() Zilin Guan
2026-01-16  8:23 ` Zong-Zhe Yang
2026-01-16 12:11   ` Zilin Guan
2026-01-16 11:04 ` Markus Elfring
2026-01-16 11:41   ` Zong-Zhe Yang
2026-01-16 12:38     ` Markus Elfring
2026-01-16 13:48       ` Johannes Berg

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