Skip to contentSkip to conversation

net: dsa: realtek: fix memory leak in rtl8366rb_setup_led()

PATCH Applied David Yang 4 participants 5 messages Jun 18 – Jun 25

Subsystems (4)
lore
Share & tools
lore

David Yang

author
#0 lore raw

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.

Fixes 32d617005475 ("net: dsa: realtek: add LED drivers for rtl8366rb")
---
 drivers/net/dsa/realtek/rtl8366rb-leds.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/dsa/realtek/rtl8366rb-leds.c +4 -4
index 509ffd3f8db5..ba50d311cb15 100644
@@ -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
#1 lore raw

Thanks David,

Quoted 25 lines

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.

Fixes 32d617005475 ("net: dsa: realtek: add LED drivers for rtl8366rb")
---
 drivers/net/dsa/realtek/rtl8366rb-leds.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/dsa/realtek/rtl8366rb-leds.c +1 -0
index 509ffd3f8db5..ba50d311cb15 100644
@@ -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

author
#2 lore raw
Quoted 3 lines · Luiz Angelo Daros de Luca

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.

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 a kfree() 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.

maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to David Yang
Quoted 8 lines · David Yang

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.

Fixes 32d617005475 ("net: dsa: realtek: add LED drivers for rtl8366rb")

Good catch!

Yours,
Linus Walleij

#4 lore raw
In reply to David Yang
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 with kasprintf() 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")

[...]

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