[PATCH net v5] mctp i2c: fix double free of midev on netdev register failure

From: Kenny Hsieh

Date: Tue Nov 24 2026 - 05:12:59 EST


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.

Suggested-by: Matt Johnston <matt@xxxxxxxxxxxxxxxxxxxx>
Fixes: f5b8abf9fc3d ("mctp i2c: MCTP I2C binding driver")
Closes: https://lore.kernel.org/netdev/179239912623.3135.11352652156626817288@xxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Kenny Hsieh <kenny_hsieh@xxxxxxxxx>
---
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/20261119094307.3864679-1-kenny_hsieh@xxxxxxxxx/
v3: https://lore.kernel.org/netdev/20261118090515.3390034-1-kenny_hsieh@xxxxxxxxx/
v2: https://lore.kernel.org/netdev/20261027053715.1623087-1-kenny_hsieh@xxxxxxxxx/
v1: https://lore.kernel.org/netdev/20261022031704.2295541-1-kenny_hsieh@xxxxxxxxx/

drivers/net/mctp/mctp-i2c.c | 76 +++++++++++++++++++++++++++----------
1 file changed, 56 insertions(+), 20 deletions(-)

diff --git a/drivers/net/mctp/mctp-i2c.c b/drivers/net/mctp/mctp-i2c.c
index b02476d338bbf..ef095028143a8 100644
--- a/drivers/net/mctp/mctp-i2c.c
+++ b/drivers/net/mctp/mctp-i2c.c
@@ -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)
--
2.50.1