mctp i2c: fix double free of midev on netdev register failure
Subsystems (4)
PATCH v5 1/1 mctp i2c: fix double free of midev on netdev register failure
mctp_i2c_add_netdev() frees midev if mctp_register_netdev() fails, but register_netdevice() may already have freed it through ndo_uninit, mctp_i2c_ndo_uninit(). midev is then freed twice: list_del() runs on a poisoned entry, and the adapter and client lose a second reference.
Free midev only where it was set up: in the add_netdev() error path, and in mctp_i2c_unregister() before free_netdev().
ndo_uninit no longer frees midev. It still stops the tx thread, drops mcli->sel's reference and purges tx_queue before the core waits for the last netdev reference, also when a NETDEV_REGISTER notifier fails, and each step is safe to repeat.
Freeing after unregister exposes two more things the old order hid. Flows released while the netdev unregisters queue the unlock marker, which is embedded in midev and must not reach kfree_skb() through the purge, so the purge takes the marker off and splices the queue out under one hold of the queue lock. And the bus lock, an rt_mutex taken by the tx thread, was released by whoever called kthread_stop(); the tx thread now drops it itself before exiting.
The double free was pointed out by the Sashiko AI review of a pending mctp-i2c change, as a problem that predates it. None of this was hit on hardware: it takes a late register_netdevice() failure, or a flow key race during unregister.
---
v5:
- Take the unlock marker off and splice tx_queue out under one hold of the queue lock. With two holds, release_flow() could queue the marker again in between, and the purge would kfree_skb() it. Found by Sashiko on v3.
- Dropped the unlock_reset() in mctp_i2c_midev_free(). The tx thread already drops the bus lock when it stops, so it did nothing.
- Dropped the early detach in mctp_i2c_unregister(), added in v2. ndo_uninit does it before the refcount wait, so sel is now moved at the same point as upstream.
- Stop the tx thread at the start of ndo_uninit. On a failed register it was still running, and could select midev again after the detach, keeping a netdev reference that blocks the unwind. Found by Sashiko on v4.
v4: https://lore.kernel.org/netdev/[email protected]/
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
drivers/net/mctp/mctp-i2c.c | 76 +++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 20 deletions(-)
drivers/net/mctp/mctp-i2c.c +56 -20
@@ -628,8 +628,11 @@ static int mctp_i2c_tx_thread(void *data)unsigned long flags;for (;;) { -if (kthread_should_stop ()) +if (kthread_should_stop ()) { +/* Only the rt_mutex owner may release the bus lock */ +mctp_i2c_unlock_reset (midev);break ; + }spin_lock_irqsave (&midev->tx_queue.lock, flags); skb =__skb_dequeue (&midev->tx_queue);
@@ -782,32 +785,59 @@ static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev,return midev; } +/* Removes midev from the parent i2c client and moves sel off it. +* Safe to call more than once. +*/ +static void mctp_i2c_midev_detach (struct mctp_i2c_dev *midev) +{ +struct mctp_i2c_client *mcli = midev->client; +unsigned long flags; + +spin_lock_irqsave (&mcli->sel_lock, flags); +if (list_empty (&midev->list)) { +spin_unlock_irqrestore (&mcli->sel_lock, flags); +return ; + } +list_del_init (&midev->list); +if (mcli->sel == midev) { +struct mctp_i2c_dev *first; + + first = list_first_entry_or_null(&mcli->devs, struct mctp_i2c_dev, list); +__mctp_i2c_device_select (mcli, first); + } +spin_unlock_irqrestore (&mcli->sel_lock, flags); +} + +/* The unlock marker is embedded in midev, so keep it from kfree_skb() */ +static void mctp_i2c_tx_purge (struct mctp_i2c_dev *midev) +{ +struct sk_buff_head tmp; +unsigned long flags; + +__skb_queue_head_init (&tmp); +spin_lock_irqsave (&midev->tx_queue.lock, flags); +if (midev->unlock_marker.next) +__skb_unlink (&midev->unlock_marker, &midev->tx_queue); +skb_queue_splice_init (&midev->tx_queue, &tmp); +spin_unlock_irqrestore (&midev->tx_queue.lock, flags); + +__skb_queue_purge (&tmp); +} +/* Counterpart of mctp_i2c_midev_init */ static void mctp_i2c_midev_free (struct mctp_i2c_dev *midev) {struct mctp_i2c_client *mcli = midev->client; -unsigned long flags;if (midev->tx_thread) {kthread_stop (midev->tx_thread); midev->tx_thread =NULL ; } -/* Unconditionally unlock on close */ -mctp_i2c_unlock_reset (midev); -/* Remove the netdev from the parent i2c client. */ -spin_lock_irqsave (&mcli->sel_lock, flags); -list_del (&midev->list); -if (mcli->sel == midev) { -struct mctp_i2c_dev *first; +mctp_i2c_midev_detach (midev); - first = list_first_entry_or_null(&mcli->devs, struct mctp_i2c_dev, list); -__mctp_i2c_device_select (mcli, first); - } -spin_unlock_irqrestore (&mcli->sel_lock, flags); - -skb_queue_purge (&midev->tx_queue); +mctp_i2c_tx_purge (midev);put_device (&midev->adapter->dev);put_device (&mcli->client->dev); }
@@ -828,7 +858,7 @@ static void mctp_i2c_unregister(struct mctp_i2c_dev *midev)wait_for_completion (&midev->rx_done);mctp_unregister_netdev (midev->ndev); -/* midev has been freed now by mctp_i2c_ndo_uninit callback */ +mctp_i2c_midev_free (midev);free_netdev (midev->ndev); }
@@ -837,11 +867,17 @@ static void mctp_i2c_ndo_uninit(struct net_device *dev) {struct mctp_i2c_dev *midev =netdev_priv (dev); -/* Perform cleanup here to ensure that mcli->sel isn't holding -* a reference that would prevent unregister_netdevice() -* from completing. +/* Stop tx first, or it can select midev again after the detach */ +if (midev->tx_thread) { +kthread_stop (midev->tx_thread); + midev->tx_thread =NULL ; + } + +/* Drop sel's reference and queued skbs before unregister waits +* for the last netdev reference. midev is freed elsewhere. */ -mctp_i2c_midev_free (midev); +mctp_i2c_midev_detach (midev); +mctp_i2c_tx_purge (midev); }static int mctp_i2c_ndo_open (struct net_device *dev)
Signature · 1 line
2.50.1
Paolo Abeni
maintainerQuoted 26 lines · Kenny Hsieh
mctp_i2c_add_netdev()frees midev ifmctp_register_netdev()fails, butregister_netdevice()may already have freed it throughndo_uninit,mctp_i2c_ndo_uninit(). midev is then freed twice:list_del()runs on a poisoned entry, and the adapter and client lose a second reference.Free midev only where it was set up: in the
add_netdev()error path, and inmctp_i2c_unregister()beforefree_netdev().
ndo_uninitno longer frees midev. It still stops the tx thread, drops mcli->sel's reference and purgestx_queuebefore the core waits for the last netdev reference, also when aNETDEV_REGISTERnotifier fails, and each step is safe to repeat.Freeing after unregister exposes two more things the old order hid. Flows released while the netdev unregisters queue the unlock marker, which is embedded in midev and must not reach
kfree_skb()through the purge, so the purge takes the marker off and splices the queue out under one hold of the queue lock. And the bus lock, anrt_mutextaken by the tx thread, was released by whoever calledkthread_stop(); the tx thread now drops it itself before exiting.The double free was pointed out by the Sashiko AI review of a pending mctp-i2c change, as a problem that predates it. None of this was hit on hardware: it takes a late
register_netdevice()failure, or a flow key race during unregister.
Quoted 6 lines
Suggested-by Matt Johnston <[email protected]>Fixes f5b8abf9fc3d ("mctp i2c: MCTP I2C binding driver")Assisted-by Claude:claude-opus-5-5Signed-off-by Kenny Hsieh <[email protected]>
FYI, day-1, sashiko driven issues, at device reg/unreg time are not net material.
/P
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:
Quoted 9 lines · On Tue, 24 Nov 2026 18:12:37 +0800 you wrote:
mctp_i2c_add_netdev()frees midev ifmctp_register_netdev()fails, butregister_netdevice()may already have freed it throughndo_uninit,mctp_i2c_ndo_uninit(). midev is then freed twice:list_del()runs on a poisoned entry, and the adapter and client lose a second reference.Free midev only where it was set up: in the
add_netdev()error path, and inmctp_i2c_unregister()beforefree_netdev().[...]
Here is the summary with links:
- [net,v5] mctp i2c: fix double free of midev on netdev register failure
https://git.kernel.org/netdev/net-next/c/c502311f8d57
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html