Skip to contentSkip to conversation

net: sfp: fix hwmon_name memory leak on hwmon registration failure

PATCH Applied Krishan Singh 3 participants 3 messages Aug 9 – Aug 11

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: sfp: fix hwmon_name memory leak on hwmon registration failure

hwmon_sanitize_name() allocates sfp->hwmon_name before hwmon_device_register_with_info() is called. If the registration fails, sfp->hwmon_dev is left pointing to an error while sfp->hwmon_name remains allocated.

Later, when the SFP module is removed, sfp_hwmon_remove() only frees hwmon_name when hwmon_dev is valid. As a result, hwmon_name is leaked if hwmon_device_register_with_info() fails.

Free hwmon_name independently of hwmon_dev. Continue to unregister the hwmon device only when hwmon_dev was successfully registered.

Fixes 3f118c449c8e ("net: sfp: use hwmon_sanitize_name()")

---
v2:
- Move hwmon_name cleanup to sfp_hwmon_remove().
- Free hwmon_name independently of hwmon_dev.

---
 drivers/net/phy/sfp.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/net/phy/sfp.c +4 -0
index f52020673..bfa2b821f 100644
@@ -1916,7 +1916,11 @@ static void sfp_hwmon_remove(struct sfp *sfp)
 	if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
 		hwmon_device_unregister(sfp->hwmon_dev);
 		sfp->hwmon_dev = NULL;
+	}
+
+	if (!IS_ERR_OR_NULL(sfp->hwmon_name)) {
 		kfree(sfp->hwmon_name);
+		sfp->hwmon_name = NULL;
 	}
 }
 
Signature · 1 line
2.34.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Krishan Singh

hwmon_sanitize_name() allocates sfp->hwmon_name before hwmon_device_register_with_info() is called. If the registration fails, sfp->hwmon_dev is left pointing to an error while sfp->hwmon_name remains allocated.

Later, when the SFP module is removed, sfp_hwmon_remove() only frees hwmon_name when hwmon_dev is valid. As a result, hwmon_name is leaked if hwmon_device_register_with_info() fails.

Quoted 6 lines

Free hwmon_name independently of hwmon_dev. Continue to unregister the hwmon device only when hwmon_dev was successfully registered.

Fixes 3f118c449c8e ("net: sfp: use hwmon_sanitize_name()")

Andrew

#2 lore raw
In reply to Krishan Singh
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Sun, 9 Aug 2026 12:15:04 +0530 you wrote:

hwmon_sanitize_name() allocates sfp->hwmon_name before hwmon_device_register_with_info() is called. If the registration fails, sfp->hwmon_dev is left pointing to an error while sfp->hwmon_name remains allocated.

Later, when the SFP module is removed, sfp_hwmon_remove() only frees hwmon_name when hwmon_dev is valid. As a result, hwmon_name is leaked if hwmon_device_register_with_info() fails.

[...]

Here is the summary with links:
- [net-next,v2] net: sfp: fix hwmon_name memory leak on hwmon registration failure

    https://git.kernel.org/netdev/net-next/c/20232e99e8bf

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html