net: dsa: realtek: fix memory leak in rtl8366rb_setup_led()
PATCH 1/1 net: dsa: realtek: fix memory leak in rtl8366rb_setup_led()
led_classdev_register_ext() only reads init_data.devicename - it never stores the pointer. However, the caller allocated devicename with kasprintf() but never freed it, leaking the string memory.
Fix it with a stack buffer to avoid dynamic buffers completely.
--- drivers/net/dsa/realtek/rtl8366rb-leds.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/dsa/realtek/rtl8366rb-leds.c +4 -4
@@ -89,6 +89,7 @@ static int rtl8366rb_setup_led(struct realtek_priv *priv, struct dsa_port *dp,struct led_init_data init_data = { };enum led_default_state state;struct rtl8366rb_led *led; +char name[64 ];u32 led_group;int ret;
@@ -129,10 +130,9 @@ static int rtl8366rb_setup_led(struct realtek_priv *priv, struct dsa_port *dp, init_data.fwnode = led_fwnode; init_data.devname_mandatory = true; - init_data.devicename =kasprintf (GFP_KERNEL ,"Realtek-%d:0%d:%d" , - dp->ds->index, dp->index, led_group); -if (!init_data.devicename) -return -ENOMEM ; +snprintf (name,sizeof (name),"Realtek-%d:0%d:%d" , + dp->ds->index, dp->index, led_group); + init_data.devicename = name; ret =devm_led_classdev_register_ext (priv->dev, &led->cdev, &init_data);if (ret) {
Signature · 1 line
2.53.0
Luiz Angelo Daros de Luca
maintainerThanks David,
Quoted 25 lines
led_classdev_register_ext()only reads init_data.devicename - it never stores the pointer. However, the caller allocated devicename withkasprintf()but never freed it, leaking the string memory.Fix it with a stack buffer to avoid dynamic buffers completely.
Fixes 32d617005475 ("net: dsa: realtek: add LED drivers for rtl8366rb")Signed-off-by David Yang <[email protected]>--- drivers/net/dsa/realtek/rtl8366rb-leds.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)drivers/net/dsa/realtek/rtl8366rb-leds.c +1
-0@@ -89,6 +89,7 @@ static int rtl8366rb_setup_led(struct realtek_priv *priv, struct dsa_port *dp,struct led_init_data init_data = { };enum led_default_state state;struct rtl8366rb_led *led; +char name[64 ];u32 led_group;int ret;
drivers/net/dsa/realtek/rtl8366rb-leds.c static int rtl8366rb_setup_led(struct realtek_priv *priv, struct dsa_port *dp, · line 133
Quoted 5 lines
drivers/net/dsa/realtek/rtl8366rb-leds.c +0
-2@@ -129,10 +130,9 @@ static int rtl8366rb_setup_led(struct realtek_priv *priv, struct dsa_port *dp, init_data.fwnode = led_fwnode; init_data.devname_mandatory = true; - init_data.devicename =kasprintf (GFP_KERNEL ,"Realtek-%d:0%d:%d" , - dp->ds->index, dp->index, led_group);
Indeed, it will leak. init_data is local and init_data.devicename is read by led_compose_name, not stored. However, stack is a limited space for allocation.
You can alternatively solve the leak using devm_kasprintf() (my choice) or adding a kfree() before leaving the function.
Quoted 11 lines
drivers/net/dsa/realtek/rtl8366rb-leds.c +3
-3-if (!init_data.devicename) -return -ENOMEM ; +snprintf (name,sizeof (name),"Realtek-%d:0%d:%d" , + dp->ds->index, dp->index, led_group); + init_data.devicename = name; ret =devm_led_classdev_register_ext (priv->dev, &led->cdev, &init_data);if (ret) { --2.53.0
Quoted 3 lines · Luiz Angelo Daros de Luca
Indeed, it will leak.
init_datais local and init_data.devicename is read byled_compose_name, not stored. However, stack is a limited space for allocation.
I've checked the buffer is long enough to hold the name string while relatively small (only 64B), so it should be safe for the stack.
Quoted 2 lines
You can alternatively solve the leak using
devm_kasprintf()(my choice) or adding akfree()before leaving the function.
devm_kasprintf() still makes the memory unused (later in the driver) and unusable (since normally you won't unload the switch driver until shutdown), IMO.
Quoted 8 lines · David Yang
led_classdev_register_ext()only reads init_data.devicename - it never stores the pointer. However, the caller allocated devicename withkasprintf()but never freed it, leaking the string memory.Fix it with a stack buffer to avoid dynamic buffers completely.
Fixes 32d617005475 ("net: dsa: realtek: add LED drivers for rtl8366rb")Signed-off-by David Yang <[email protected]>
Good catch!
Yours,
Linus Walleij
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Thu, 18 Jun 2026 22:01:55 +0800 you wrote:
led_classdev_register_ext()only reads init_data.devicename - it never stores the pointer. However, the caller allocated devicename withkasprintf()but never freed it, leaking the string memory.Fix it with a stack buffer to avoid dynamic buffers completely.
Fixes 32d617005475 ("net: dsa: realtek: add LED drivers for rtl8366rb")Signed-off-by David Yang <[email protected]>[...]
Here is the summary with links:
- [net] net: dsa: realtek: fix memory leak in rtl8366rb_setup_led()
https://git.kernel.org/netdev/net/c/056a5087d87e
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html