* [PATCH net v5] mctp i2c: fix double free of midev on netdev register failure
@ 2026-11-24 10:12 Kenny Hsieh
2026-12-03 10:22 ` Paolo Abeni
2026-12-03 10:30 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Kenny Hsieh @ 2026-11-24 10:12 UTC (permalink / raw)
To: netdev, Jeremy Kerr, Matt Johnston
Cc: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, stable, linux-kernel
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@codeconstruct.com.au>
Fixes: f5b8abf9fc3d ("mctp i2c: MCTP I2C binding driver")
Closes: https://lore.kernel.org/netdev/179239912623.3135.11352652156626817288@kernel.org/
Cc: stable@vger.kernel.org
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Kenny Hsieh <kenny_hsieh@jabil.com>
---
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@jabil.com/
v3: https://lore.kernel.org/netdev/20261118090515.3390034-1-kenny_hsieh@jabil.com/
v2: https://lore.kernel.org/netdev/20261027053715.1623087-1-kenny_hsieh@jabil.com/
v1: https://lore.kernel.org/netdev/20261022031704.2295541-1-kenny_hsieh@jabil.com/
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
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net v5] mctp i2c: fix double free of midev on netdev register failure
2026-11-24 10:12 [PATCH net v5] mctp i2c: fix double free of midev on netdev register failure Kenny Hsieh
@ 2026-12-03 10:22 ` Paolo Abeni
2026-12-03 10:30 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Paolo Abeni @ 2026-12-03 10:22 UTC (permalink / raw)
To: Kenny Hsieh, netdev, Jeremy Kerr, Matt Johnston
Cc: Andrew Lunn, David S . Miller, Eric Dumazet, Jakub Kicinski,
stable, linux-kernel
On 11/24/26 11:12, Kenny Hsieh wrote:
> 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@codeconstruct.com.au>
> Fixes: f5b8abf9fc3d ("mctp i2c: MCTP I2C binding driver")
> Closes: https://lore.kernel.org/netdev/179239912623.3135.11352652156626817288@kernel.org/
> Cc: stable@vger.kernel.org
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Kenny Hsieh <kenny_hsieh@jabil.com>
FYI, day-1, sashiko driven issues, at device reg/unreg time are not
net material.
/P
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH net v5] mctp i2c: fix double free of midev on netdev register failure
2026-11-24 10:12 [PATCH net v5] mctp i2c: fix double free of midev on netdev register failure Kenny Hsieh
2026-12-03 10:22 ` Paolo Abeni
@ 2026-12-03 10:30 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-12-03 10:30 UTC (permalink / raw)
To: Kenny Hsieh
Cc: netdev, jk, matt, andrew+netdev, davem, edumazet, kuba, pabeni,
stable, linux-kernel
Hello:
This patch was applied to netdev/net-next.git (main)
by Paolo Abeni <pabeni@redhat.com>:
On Tue, 24 Nov 2026 18:12:37 +0800 you wrote:
> 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().
>
> [...]
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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 10:30 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 10:12 [PATCH net v5] mctp i2c: fix double free of midev on netdev register failure Kenny Hsieh
2026-12-03 10:22 ` Paolo Abeni
2026-12-03 10:30 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®