Skip to contentSkip to conversation

ncsi: Fix use-after-free in the VLAN id list removal

PATCH Applied Binbin Deng 2 participants 2 messages Oct 10 – Oct 16

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

PATCH v2 1/1 ncsi: Fix use-after-free in the VLAN id list removal

ncsi_vlan_rx_kill_vid() removes an entry from the NCSI VLAN list with:

	list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
		if (vlan->vid == vid) {
			list_del_rcu(&vlan->list);
			found = true;
			kfree(vlan);
		}

The list is published with list_add_rcu(), so readers traverse it under rcu_read_lock(). The writer holds only the RTNL, which does not exclude those readers. list_for_each_entry_safe() protects the writer iteration itself, not concurrent readers.

The reader is set_one_vid(), invoked from the NCSI channel configuration state machine (ndp->work) while sending the "Set VLAN Filter" command:

	rcu_read_lock();
	list_for_each_entry_rcu(vlan, &ndp->vlan_vids, list) {
		vid = vlan->vid;
		...
	}
	rcu_read_unlock();

An RCU read-side critical section only protects objects released via call_rcu()/kfree_rcu(); it does not protect against the immediate kfree() above. If the state machine work has already obtained the list entry pointer and is then scheduled out while another CPU removes the VLAN id, the resumed iteration reads the freed node and follows the ->next pointer of reused memory.

This is reachable on BMC systems using NCSI when a VLAN id is deleted (ip link / rtnetlink) concurrently with an NCSI configuration cycle.

Fix this by adding an rcu_head to struct vlan_vid and releasing the node with kfree_rcu().

Found during a static-analysis-assisted audit: candidate sites were surfaced by a Coccinelle rule, and the reader/writer paths were confirmed by LLM-assisted (GLM-5.3) code inspection. The issue was not observed at runtime; it is identified by code inspection only.

Fixes 21acf63013ed ("net/ncsi: Configure VLAN tag filter")
---
 net/ncsi/internal.h    | 1 +
 net/ncsi/ncsi-manage.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
net/ncsi/internal.h +1 -0
index adee6dcabdc3..2c9d1f22c16a 100644
@@ -312,6 +312,7 @@ struct vlan_vid {
 	struct list_head list;
 	__be16 proto;
 	u16 vid;
+	struct rcu_head rcu_head;
 };
 
 struct ncsi_dev_priv {
net/ncsi/ncsi-manage.c +1 -1
index 54d0df0a9efe..1d63958c4429 100644
@@ -1737,7 +1737,7 @@ int ncsi_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
 			netdev_dbg(dev, "NCSI: vid %u found, removing\n", vid);
 			list_del_rcu(&vlan->list);
 			found = true;
-			kfree(vlan);
+			kfree_rcu(vlan, rcu_head);
 		}
 
 	if (!found) {
Signature · 1 line
2.43.0
#1 lore raw
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 Sat, 10 Oct 2026 11:08:20 +0800 you wrote:

ncsi_vlan_rx_kill_vid() removes an entry from the NCSI VLAN list with:

	list_for_each_entry_safe(vlan, tmp, &ndp->vlan_vids, list)
		if (vlan->vid == vid) {
			list_del_rcu(&vlan->list);
			found = true;
			kfree(vlan);
		}

[...]

Here is the summary with links:
- [net,v2] ncsi: Fix use-after-free in the VLAN id list removal

    https://git.kernel.org/netdev/net-next/c/9c7e89fc1b68

You are awesome, thank you!

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