Skip to contentSkip to conversation

netconsole: fix target enable race and configfs read locking

PATCH Applied Gustavo Luiz Duarte 3 participants 10 messages Nov 2 – Nov 6

Subsystems (3)
lore
Share & tools
lore

Gustavo Luiz Duarte

author reviewer
#0 lore raw

v5 cover netconsole: fix target enable race and configfs read locking

While looking into the sashiko report about local_ip_show() and remote_ip_show() missing proper locking [1], I noticed a couple other places that need locking:

Patch 1 fixes a null-ptr-deref when enabling a target races against a NETDEV_UNREGISTER event.

Patch 2 fixes a use-after-free when the local_mac_show() callback races against a NETDEV_UNREGISTER event.

Patch 3 fixes yet another race, in netcons_netpoll_setup(), where netconsole could miss the NETDEV_UNREGISTER notification and keep holding a reference to an unregistered device.

[1] https://netdev-ai.bots.linux.dev/sashiko/#/patchset/[email protected]

---
Changes in v5:
- Add patch 4: netconsole: stop waiting for carrier once the device is unregistered.
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- Add patch 3: netconsole: fix target enable race and configfs read locking.
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Use target_list_lock instead of RTNL in local_mac_show.
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Resending for the 'net' tree.
- Link to v1: https://patch.msgid.link/[email protected]

To: Breno Leitao <[email protected]>
To: Andrew Lunn <[email protected]>
To: "David S. Miller" <[email protected]>
To: Eric Dumazet <[email protected]>
To: Jakub Kicinski <[email protected]>
To: Paolo Abeni <[email protected]>
To: Stephen Hemminger <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
Gustavo Luiz Duarte (4):
netconsole: finish enabling the target before releasing RTNL
netconsole: read np.dev under target_list_lock in local_mac_show()
netconsole: recheck device registration after waiting for carrier
netconsole: stop waiting for carrier once the device is unregistered

 drivers/net/netconsole.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)

---
base-commit: 7308ef8b4a6779a39f68d053c19b53a5c584a663
change-id: 20260925-netcons-fixes-93a3e14326b5

Best regards,

Signature · 1 line
Gustavo Luiz Duarte <[email protected]>
author reviewer
#1 lore raw

PATCH v5 1/4 netconsole: finish enabling the target before releasing RTNL

netcons_netpoll_setup() publishes nt->np.dev inside __netpoll_setup(), then releases the RTNL and waits for an RCU grace period before returning. Only after that does its caller store STATE_ENABLED.

A NETDEV_UNREGISTER event landing in that window tears the target down and we end up storing STATE_ENABLED with a NULL np->dev, leading to a null-ptr-deref in netconsole_write():

  BUG: KASAN: null-ptr-deref in netconsole_write+0xb9/0x7f0
  Read of size 8 at addr 00000000000000a8 by task pr/netcon_ext0/135
   netconsole_write+0xb9/0x7f0
   nbcon_emit_next_record+0x501/0x550
   nbcon_emit_one+0x10e/0x170
   nbcon_kthread_func+0x2ff/0x3b0
   kthread+0x199/0x1e0

Store STATE_ENABLED before dropping the RTNL lock to avoid racing with netconsole_netdev_event().

Fixes 2382b15bcc39 ("netconsole: take care of NETDEV_UNREGISTER event")
---
 drivers/net/netconsole.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/net/netconsole.c +5 -6
index 267254f046de..7dde00eb8e98 100644
@@ -552,13 +552,16 @@ static int netcons_netpoll_setup(struct netconsole_target *nt)
 	err = __netpoll_setup(np, ndev);
 	if (err)
 		goto put;
-	rtnl_unlock();
 
 	/* Make sure all NAPI polls which started before dev->npinfo
 	 * was visible have exited before we start calling NAPI poll.
 	 * NAPI skips locking if dev->npinfo is NULL.
+	 * Hold RTNL until enable is finished so we don't race with
+	 * netconsole_netdev_event()
 	 */
-	synchronize_rcu();
+	synchronize_net();
+	nt->state = STATE_ENABLED;
+	rtnl_unlock();
 
 	return 0;
 
@@ -589,7 +592,6 @@ static void resume_target(struct netconsole_target *nt)
 		return;
 	}
 
-	nt->state = STATE_ENABLED;
 	pr_info("network logging resumed on interface %s\n", nt->np.dev_name);
 }
 
@@ -1079,7 +1081,6 @@ static ssize_t enabled_store(struct config_item *item,
 			goto out_unlock;
 		}
 
-		nt->state = STATE_ENABLED;
 		pr_info("network logging started\n");
 	} else {	/* false */
 		/* We need to disable the netconsole before cleaning it up
@@ -2667,8 +2668,6 @@ static struct netconsole_target *alloc_param_target(char *target_config,
 			 * otherwise, keep the target in the list, but disabled.
 			 */
 			goto fail;
-	} else {
-		nt->state = STATE_ENABLED;
 	}
 	populate_configfs_item(nt, cmdline_count);
 
Signature · 1 line
2.56.0
author reviewer
#3 lore raw

PATCH v5 2/4 netconsole: read np.dev under target_list_lock in local_mac_show()

In reply to Gustavo Luiz Duarte

local_mac_show() loads nt->np.dev and dereferences it with no lock. It can race with the interface going away and netconsole_netdev_event() eventually calling netdev_put() and freeing the device before we dereference it, leading to a UAF.

The window is only a few instructions wide, so I could only reproduce it with an artificially widened window between the load and the dereference:

  BUG: KASAN: slab-use-after-free in local_mac_show+0x4e/0x80
  Read of size 8 at addr ffff88800e990458 by task uaf/130
   local_mac_show+0x4e/0x80
   configfs_read_iter+0x187/0x260
   vfs_read+0x453/0x5a0

  Allocated by task 116:
   alloc_netdev_mqs+0x78/0x830
   rtnl_create_link+0x53b/0x5c0

  Freed by task 121:
   kfree+0x159/0x420
   device_release+0x77/0x120
   kobject_put+0xb5/0x160
   netdev_run_todo+0x420/0x850
   rtnl_dellink+0x259/0x5b0

Two places can free np.dev: (1) netconsole_netdev_event() moves a target out of STATE_ENABLED under target_list_lock before netdev_put(), and (2) process_resume_target() releases np.dev under dynamic_netconsole_mutex.

Fixes 0953864160bd ("[NETPOLL]: no need to store local_mac")
---
 drivers/net/netconsole.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
drivers/net/netconsole.c +16 -3
index 7dde00eb8e98..1680581c595e 100644
@@ -900,10 +900,23 @@ static ssize_t remote_ip_show(struct config_item *item, char *buf)
 
 static ssize_t local_mac_show(struct config_item *item, char *buf)
 {
-	struct net_device *dev = to_target(item)->np.dev;
-	static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
+	struct netconsole_target *nt = to_target(item);
+	unsigned long flags;
+	u8 mac[ETH_ALEN];
+
+	eth_broadcast_addr(mac);
+
+	dynamic_netconsole_mutex_lock();
+	/* Hold target_list_lock here so netconsole_netdev_event() doesn't tear
+	 * down np.dev from under us.
+	 */
+	spin_lock_irqsave(&target_list_lock, flags);
+	if (nt->state == STATE_ENABLED && nt->np.dev)
+		memcpy(mac, nt->np.dev->dev_addr, ETH_ALEN);
+	spin_unlock_irqrestore(&target_list_lock, flags);
+	dynamic_netconsole_mutex_unlock();
 
-	return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
+	return sysfs_emit(buf, "%pM\n", mac);
 }
 
 static ssize_t remote_mac_show(struct config_item *item, char *buf)
Signature · 1 line
2.56.0
author reviewer
#5 lore raw

PATCH v5 3/4 netconsole: recheck device registration after waiting for carrier

In reply to Gustavo Luiz Duarte

netcons_netpoll_setup() drops RTNL while it waits for carrier when the device is not yet up. If the device is unregistered in that window, the NETDEV_UNREGISTER notifier runs before np->dev is set and has nothing to match.

We might then end up setting up netpoll on a device that is being unregistered while holding a reference to it, stalling unregister_netdevice() in netdev_wait_allrefs_any().

Add a check to confirm the device is still registered once we retake RTNL. Release the device reference and return -ENODEV otherwise.

---
 drivers/net/netconsole.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/net/netconsole.c +10 -0
index 1680581c595e..238acf300d23 100644
@@ -537,6 +537,16 @@ static int netcons_netpoll_setup(struct netconsole_target *nt)
 		rtnl_unlock();
 		netcons_wait_carrier(np, ndev);
 		rtnl_lock();
+
+		/* Check that ndev hasn't been unregistered when we released
+		 * the RTNL above.
+		 */
+		if (ndev->reg_state != NETREG_REGISTERED) {
+			np_err(np, "%s was unregistered while waiting for carrier, aborting\n",
+			       netcons_egress_dev(np, buf, sizeof(buf)));
+			err = -ENODEV;
+			goto put;
+		}
 	}
 
 	if (nt->local_ip.family == AF_UNSPEC) {
Signature · 1 line
2.56.0
author reviewer
#7 lore raw

PATCH v5 4/4 netconsole: stop waiting for carrier once the device is unregistered

In reply to Gustavo Luiz Duarte

netcons_netpoll_setup() releases RTNL and waits in netcons_wait_carrier() for up to carrier_timeout seconds for the device to report carrier. If the device is unregistered in the meantime, the loop keeps polling until the timeout expires.

Stop waiting as soon as the device is no longer NETREG_REGISTERED.

---
 drivers/net/netconsole.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/netconsole.c +2 -0
index 238acf300d23..895d33b179e7 100644
@@ -383,6 +383,8 @@ static void netcons_wait_carrier(struct netpoll *np, struct net_device *ndev)
 
 	atmost = jiffies + netpoll_get_carrier_timeout() * HZ;
 	while (!netif_carrier_ok(ndev)) {
+		if (READ_ONCE(ndev->reg_state) != NETREG_REGISTERED)
+			break;
 		if (time_after(jiffies, atmost)) {
 			np_notice(np, "timeout waiting for carrier\n");
 			break;
Signature · 1 line
2.56.0
maintainer Reviewed-by on v5 1/4
#2 lore raw

Re: [PATCH net v5 1/4] netconsole: finish enabling the target before releasing RTNL

In reply to Gustavo Luiz Duarte
Quoted 16 lines · Gustavo Luiz Duarte

netcons_netpoll_setup() publishes nt->np.dev inside __netpoll_setup(), then releases the RTNL and waits for an RCU grace period before returning. Only after that does its caller store STATE_ENABLED.

A NETDEV_UNREGISTER event landing in that window tears the target down and we end up storing STATE_ENABLED with a NULL np->dev, leading to a null-ptr-deref in netconsole_write():

  BUG: KASAN: null-ptr-deref in netconsole_write+0xb9/0x7f0
  Read of size 8 at addr 00000000000000a8 by task pr/netcon_ext0/135
   netconsole_write+0xb9/0x7f0
   nbcon_emit_next_record+0x501/0x550
   nbcon_emit_one+0x10e/0x170
   nbcon_kthread_func+0x2ff/0x3b0
   kthread+0x199/0x1e0
Quoted 5 lines

Store STATE_ENABLED before dropping the RTNL lock to avoid racing with netconsole_netdev_event().

Fixes 2382b15bcc39 ("netconsole: take care of NETDEV_UNREGISTER event")
maintainer Reviewed-by on v5 2/4
#4 lore raw

Re: [PATCH net v5 2/4] netconsole: read np.dev under target_list_lock in local_mac_show()

In reply to Gustavo Luiz Duarte
Quoted 26 lines · Gustavo Luiz Duarte

local_mac_show() loads nt->np.dev and dereferences it with no lock. It can race with the interface going away and netconsole_netdev_event() eventually calling netdev_put() and freeing the device before we dereference it, leading to a UAF.

The window is only a few instructions wide, so I could only reproduce it with an artificially widened window between the load and the dereference:

  BUG: KASAN: slab-use-after-free in local_mac_show+0x4e/0x80
  Read of size 8 at addr ffff88800e990458 by task uaf/130
   local_mac_show+0x4e/0x80
   configfs_read_iter+0x187/0x260
   vfs_read+0x453/0x5a0

  Allocated by task 116:
   alloc_netdev_mqs+0x78/0x830
   rtnl_create_link+0x53b/0x5c0

  Freed by task 121:
   kfree+0x159/0x420
   device_release+0x77/0x120
   kobject_put+0xb5/0x160
   netdev_run_todo+0x420/0x850
   rtnl_dellink+0x259/0x5b0
Quoted 6 lines

Two places can free np.dev: (1) netconsole_netdev_event() moves a target out of STATE_ENABLED under target_list_lock before netdev_put(), and (2) process_resume_target() releases np.dev under dynamic_netconsole_mutex.

Fixes 0953864160bd ("[NETPOLL]: no need to store local_mac")
maintainer Reviewed-by on v5 3/4
#6 lore raw

Re: [PATCH net v5 3/4] netconsole: recheck device registration after waiting for carrier

In reply to Gustavo Luiz Duarte
Quoted 9 lines · Gustavo Luiz Duarte

netcons_netpoll_setup() drops RTNL while it waits for carrier when the device is not yet up. If the device is unregistered in that window, the NETDEV_UNREGISTER notifier runs before np->dev is set and has nothing to match.

We might then end up setting up netpoll on a device that is being unregistered while holding a reference to it, stalling unregister_netdevice() in netdev_wait_allrefs_any().

Quoted 5 lines

Add a check to confirm the device is still registered once we retake RTNL. Release the device reference and return -ENODEV otherwise.

maintainer Reviewed-by on v5 4/4
#8 lore raw

Re: [PATCH net v5 4/4] netconsole: stop waiting for carrier once the device is unregistered

In reply to Gustavo Luiz Duarte
Quoted 9 lines · Gustavo Luiz Duarte

netcons_netpoll_setup() releases RTNL and waits in netcons_wait_carrier() for up to carrier_timeout seconds for the device to report carrier. If the device is unregistered in the meantime, the loop keeps polling until the timeout expires.

Stop waiting as soon as the device is no longer NETREG_REGISTERED.

#9 lore raw
In reply to Gustavo Luiz Duarte
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 8 lines · On Mon, 02 Nov 2026 19:54:48 +0000 you wrote:

While looking into the sashiko report about local_ip_show() and remote_ip_show() missing proper locking [1], I noticed a couple other places that need locking:

Patch 1 fixes a null-ptr-deref when enabling a target races against a NETDEV_UNREGISTER event.

[...]

Here is the summary with links:
- [net,v5,1/4] netconsole: finish enabling the target before releasing RTNL

    https://git.kernel.org/netdev/net-next/c/f94b9640bcf2
  - [net,v5,2/4] netconsole: read np.dev under target_list_lock in local_mac_show()
    https://git.kernel.org/netdev/net-next/c/3733db02b88e
  - [net,v5,3/4] netconsole: recheck device registration after waiting for carrier
    https://git.kernel.org/netdev/net-next/c/6891cd198752
  - [net,v5,4/4] netconsole: stop waiting for carrier once the device is unregistered
    https://git.kernel.org/netdev/net-next/c/a2b4d25e4ddf

You are awesome, thank you!

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