Netdev List
 help / color / mirror / Atom feed
From: Xuanqiang Luo <xuanqiang.luo@linux.dev>
To: netdev@vger.kernel.org, andrew@lunn.ch,
	maxime.chevallier@bootlin.com, kuba@kernel.org
Cc: hkallweit1@gmail.com, qingfang.deng@siflower.com.cn,
	hao.guan@siflower.com.cn, linux@armlinux.org.uk,
	davem@davemloft.net, edumazet@google.com, pabeni@redhat.com,
	linux-kernel@vger.kernel.org,
	Xuanqiang Luo <luoxuanqiang@kylinos.cn>
Subject: [PATCH net v4 6/6] net: phy: avoid double-free after LED trigger registration failure
Date: Fri, 21 Aug 2026 15:03:27 +0800	[thread overview]
Message-ID: <20260821070327.16147-7-xuanqiang.luo@linux.dev> (raw)
In-Reply-To: <20260821070327.16147-1-xuanqiang.luo@linux.dev>

From: Xuanqiang Luo <luoxuanqiang@kylinos.cn>

When a speed trigger registration fails, phy_led_triggers_register() frees
phy_led_triggers but leaves the pointer set to the freed allocation. It
then clears phy_num_led_triggers.

phy_probe() ignores this error. If device-tree LED setup then fails, its
error path calls phy_led_triggers_unregister(). The zero trigger count
skips the per-trigger unregister loop, but the helper still frees the
dangling pointer.

Clear the pointer after partial registration cleanup and make
phy_led_triggers_unregister() walk and free the array only while it is
present.

Fixes: b7f0ee992adf ("net: phy: leds: fix memory leak")
Signed-off-by: Xuanqiang Luo <luoxuanqiang@kylinos.cn>
---
 drivers/net/phy/phy_led_triggers.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/net/phy/phy_led_triggers.c b/drivers/net/phy/phy_led_triggers.c
index 4eb7716bb9d6c..ff6e518395be0 100644
--- a/drivers/net/phy/phy_led_triggers.c
+++ b/drivers/net/phy/phy_led_triggers.c
@@ -126,6 +126,7 @@ int phy_led_triggers_register(struct phy_device *phy)
 	while (i--)
 		phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
 	kfree(phy->phy_led_triggers);
+	phy->phy_led_triggers = NULL;
 out_unreg_link:
 	phy_led_trigger_unregister(phy->led_link_trigger);
 out_free_link:
@@ -141,10 +142,12 @@ void phy_led_triggers_unregister(struct phy_device *phy)
 {
 	int i;
 
-	for (i = 0; i < phy->phy_num_led_triggers; i++)
-		phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
-	kfree(phy->phy_led_triggers);
-	phy->phy_led_triggers = NULL;
+	if (phy->phy_led_triggers) {
+		for (i = 0; i < phy->phy_num_led_triggers; i++)
+			phy_led_trigger_unregister(&phy->phy_led_triggers[i]);
+		kfree(phy->phy_led_triggers);
+		phy->phy_led_triggers = NULL;
+	}
 
 	if (phy->led_link_trigger) {
 		phy_led_trigger_unregister(phy->led_link_trigger);
-- 
2.43.0


      parent reply	other threads:[~2026-08-21  7:04 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-21  7:03 [PATCH net v4 0/6] net: phy: fix cleanup after probe failure Xuanqiang Luo
2026-08-21  7:03 ` [PATCH net v4 1/6] net: phy: split phy_probe() error paths Xuanqiang Luo
2026-08-21  7:03 ` [PATCH net v4 2/6] net: phy: unregister SFP upstream before port cleanup Xuanqiang Luo
2026-08-21  7:03 ` [PATCH net v4 3/6] net: phy: set PHY_READY after LED setup Xuanqiang Luo
2026-08-21  7:03 ` [PATCH net v4 4/6] net: phy: call driver remove when core initialization fails Xuanqiang Luo
2026-08-21  7:03 ` [PATCH net v4 5/6] net: phy: propagate errors from default port setup Xuanqiang Luo
2026-08-21  9:42   ` Xuanqiang Luo
2026-08-21  7:03 ` Xuanqiang Luo [this message]

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=20260821070327.16147-7-xuanqiang.luo@linux.dev \
    --to=xuanqiang.luo@linux.dev \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=hao.guan@siflower.com.cn \
    --cc=hkallweit1@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=luoxuanqiang@kylinos.cn \
    --cc=maxime.chevallier@bootlin.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=qingfang.deng@siflower.com.cn \
    /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