Netdev List
 help / color / mirror / Atom feed
From: Krishan Singh <krishanmohan298@gmail.com>
To: linux@armlinux.org.uk
Cc: andrew@lunn.ch, hkallweit1@gmail.com, davem@davemloft.net,
	edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Krishan Singh <krishanmohan298@gmail.com>
Subject: [PATCH] net: sfp: fix hwmon_name leak on hwmon registration failure
Date: Sat,  1 Aug 2026 12:27:03 +0530	[thread overview]
Message-ID: <20260801065703.43896-1-krishanmohan298@gmail.com> (raw)

When hwmon_device_register_with_info() fails in sfp_hwmon_probe(),
the string allocated by hwmon_sanitize_name() is not freed, leaking
memory on repeated hotplug/probe attempts.

Also, sfp_hwmon_remove() only freed hwmon_name inside the
IS_ERR_OR_NULL(hwmon_dev) block, so a partial-init failure could
leave the name allocation behind.

Fix by freeing hwmon_name and clearing both pointers on registration
failure, and by freeing hwmon_name unconditionally in remove while
still only unregistering hwmon_dev when it is valid.

Fixes: 3f118c449c8e ("net: sfp: use hwmon_sanitize_name()")
Signed-off-by: Krishan Singh <krishanmohan298@gmail.com>
---
 drivers/net/phy/sfp.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/sfp.c b/drivers/net/phy/sfp.c
index f52020673..f605fb399 100644
--- a/drivers/net/phy/sfp.c
+++ b/drivers/net/phy/sfp.c
@@ -1895,9 +1895,13 @@ static void sfp_hwmon_probe(struct work_struct *work)
 							 sfp->hwmon_name, sfp,
 							 &sfp_hwmon_chip_info,
 							 NULL);
-	if (IS_ERR(sfp->hwmon_dev))
+	if (IS_ERR(sfp->hwmon_dev)) {
 		dev_err(sfp->dev, "failed to register hwmon device: %ld\n",
 			PTR_ERR(sfp->hwmon_dev));
+		kfree(sfp->hwmon_name);
+		sfp->hwmon_name = NULL;
+		sfp->hwmon_dev  = NULL;
+	 }
 }
 
 static int sfp_hwmon_insert(struct sfp *sfp)
-- 
2.34.1


             reply	other threads:[~2026-08-01  6:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-01  6:57 Krishan Singh [this message]
2026-08-01 19:56 ` [PATCH] net: sfp: fix hwmon_name leak on hwmon registration failure Andrew Lunn
2026-08-01 21:33   ` Janis Edvarts Lacis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260801065703.43896-1-krishanmohan298@gmail.com \
    --to=krishanmohan298@gmail.com \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=hkallweit1@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox