mctp i2c: fix double free of midev on netdev register failure
Subsystems (4)
PATCH v4 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 now only drops mcli->sel's reference and purges tx_queue. Both have to happen where the core calls it: after dev_close() and before it waits for the last netdev reference, also when a NETDEV_REGISTER notifier fails. sel's reference would otherwise block that wait, and a queued skb can hold a flow key that pins the netdev. Both steps are 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. 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.
---
v4:
- Left ndo_uninit where it was and only changed its body, so the diff shows what it does now. Shortened the comments. No functional change.
v3:
- Kept ndo_uninit, but it now only drops sel and purges tx_queue. Removing it in v2 made register hang if a NETDEV_REGISTER notifier fails, and moved the purge after the refcount wait, where a queued skb can pin the netdev. Both found by Sashiko on v2. midev is still freed only once, in mctp_i2c_midev_free().
- Rebased on current net.
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 | 69 ++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 17 deletions(-)
drivers/net/mctp/mctp-i2c.c +52 -17
@@ -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,11 +785,46 @@ 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) +{ +unsigned long flags; + +spin_lock_irqsave (&midev->tx_queue.lock, flags); +if (midev->unlock_marker.next) +__skb_unlink (&midev->unlock_marker, &midev->tx_queue); +spin_unlock_irqrestore (&midev->tx_queue.lock, flags); + +skb_queue_purge (&midev->tx_queue); +} +/* 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);
@@ -797,17 +835,9 @@ static void mctp_i2c_midev_free(struct mctp_i2c_dev *midev)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); }
@@ -827,8 +857,13 @@ static void mctp_i2c_unregister(struct mctp_i2c_dev *midev)spin_unlock_irqrestore (&midev->lock, flags);wait_for_completion (&midev->rx_done); +/* Release sel's reference on the netdev, otherwise +* mctp_unregister_netdev() blocks waiting for it. +*/ +mctp_i2c_midev_detach (midev); +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 +872,11 @@ 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. +/* 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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 1 (1) · Medium: 1 (1) · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] mctp_i2c_tx_purge() (drivers/net/mctp/mctp-i2c.c:812-822) does not keep the embedded unlock marker away from kfree_skb(), which the…
- [Medium] mctp i2c: running tx_thread in mctp_i2c_ndo_uninit() can re- select mcli->sel
- [Low] Two comments in the patched code now contradict each other about the bus lock.
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the sibling driver mctp-i3c has the reverse version of the same ndo_uninit…
- [Medium] This problem wasn't introduced by this patch, but mctp_register_netdevice() (net/mctp/device.c) leaks the mctp_dev.
--
mctp i2c: fix double free of midev on netdev register failure
After this patch, mctp_i2c_ndo_uninit() no longer frees midev. midev is now freed only in the add_netdev() error path and in mctp_i2c_unregister(), and ndo_uninit only detaches midev from the client's sel and purges tx_queue, keeping the embedded unlock marker out of the purge. The tx thread now releases the i2c bus lock itself before it exits.
Quoted 18 lines
drivers/net/mctp/mctp-i2c.c +4
-1@@ -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,11 +785,46 @@ static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev,
[ ... ]
drivers/net/mctp/mctp-i2c.c static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev, · line 822
Quoted 12 lines
drivers/net/mctp/mctp-i2c.c +12
-0+/* The unlock marker is embedded in midev, so keep it from kfree_skb() */ +static void mctp_i2c_tx_purge (struct mctp_i2c_dev *midev) +{ +unsigned long flags; + +spin_lock_irqsave (&midev->tx_queue.lock, flags); +if (midev->unlock_marker.next) +__skb_unlink (&midev->unlock_marker, &midev->tx_queue); +spin_unlock_irqrestore (&midev->tx_queue.lock, flags); + +skb_queue_purge (&midev->tx_queue); +}
[Severity: High]
The commit message says the marker "must not reach kfree_skb() through the purge". Can it still get there?
The marker is unlinked under tx_queue.lock. The lock is then dropped, and skb_queue_purge() takes it again to splice the queue:
net/core/skbuff.c:skb_queue_purge_reason() {
...
spin_lock_irqsave(&list->lock, flags);
skb_queue_splice_init(list, &tmp);
spin_unlock_irqrestore(&list->lock, flags);
__skb_queue_purge_reason(&tmp, reason);
}In that gap, mctp_i2c_release_flow() can run on another CPU. It sees the unlock_marker.next == NULL state that __skb_unlink() just left and queues the marker again:
mctp_i2c_release_flow() {
...
spin_lock(&midev->tx_queue.lock);
if (!midev->unlock_marker.next)
__skb_queue_tail(&midev->tx_queue,
&midev->unlock_marker);
spin_unlock(&midev->tx_queue.lock);
...
}This looks reachable from mctp_i2c_ndo_uninit(). It runs inside unregister_netdevice() after NETDEV_UNREGISTER and before the netdev refcount wait.
At that point MCTP keys can still hold the mctp_dev. They are released without RTNL: by the key expiry timer through __mctp_key_remove(), by socket close, or through mctp_key_unref(). Each of these calls mctp_dev_release_key()->ops->release_flow().
mdev->ops is not cleared on unregister. The tx thread has already been stopped by mctp_i2c_unregister(), so nothing else dequeues the marker in this window.
The marker lives in netdev_priv and is never initialised, so its skb->users is 0. kfree_skb()->skb_unref() would then hit a refcount_t underflow / use-after-free warning, which panics with panic_on_warn.
Would it work to unlink the marker and splice the queue onto a local list under a single tx_queue.lock hold, and then purge the local list? A later re-queue would be harmless: the purge in mctp_i2c_midev_free() runs after unregister_netdev() has waited for all references.
[ ... ]
drivers/net/mctp/mctp-i2c.c static void mctp_i2c_midev_free(struct mctp_i2c_dev *midev) · line 835
Quoted 2 lines
drivers/net/mctp/mctp-i2c.c +0
-0@@ -797,17 +835,9 @@ static void mctp_i2c_midev_free(struct mctp_i2c_dev *midev)mctp_i2c_unlock_reset (midev);
[Severity: Low]
This isn't a bug, but is this mctp_i2c_unlock_reset() call still needed? In mctp_i2c_midev_free() it reads:
/* Unconditionally unlock on close */ mctp_i2c_unlock_reset(midev);
Both callers have stopped the tx thread before this point. The thread's only exit is the new kthread_should_stop() branch, which already resets i2c_lock_count. Nothing outside the tx thread increments i2c_lock_count, so this call never does anything.
If it ever did fire, it would release the rt_mutex from a non-owner context. The new "Only the rt_mutex owner may release the bus lock" comment in mctp_i2c_tx_thread() rules that out.
Could the call and the "Unconditionally unlock on close" comment be removed, or replaced with a WARN that the count is already zero?
Quoted 2 lines
drivers/net/mctp/mctp-i2c.c +0
-0/* Remove the netdev from the parent i2c client. */
[ ... ]
Quoted 4 lines
drivers/net/mctp/mctp-i2c.c +0
-0@@ -837,11 +872,11 @@ static void mctp_i2c_ndo_uninit(struct net_device *dev) {struct mctp_i2c_dev *midev =netdev_priv (dev);
drivers/net/mctp/mctp-i2c.c static void mctp_i2c_ndo_uninit(struct net_device *dev) · line 880
Quoted 10 lines
drivers/net/mctp/mctp-i2c.c +4
-4-/* Perform cleanup here to ensure that mcli->sel isn't holding -* a reference that would prevent unregister_netdevice() -* from completing. +/* 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); }
[Severity: Medium]
Can the tx thread put midev back into mcli->sel after this detach has run?
The old ndo_uninit went through mctp_i2c_midev_free(), which stopped midev->tx_thread before touching sel. The new one leaves the tx thread running. That is fine on the mctp_i2c_unregister() path, which calls kthread_stop() first, but not on the register failure path this patch is fixing.
mctp_i2c_midev_init() wakes the tx thread before mctp_i2c_add_netdev() calls mctp_register_netdev(). If a NETDEV_REGISTER notifier fails after a packet has been queued, register_netdevice() unwinds through unregister_netdevice_queue(), which calls mctp_i2c_ndo_uninit(). The rtnl_unlock() in mctp_register_netdev() then runs netdev_run_todo() and waits for the last reference in netdev_wait_allrefs_any().
If the tx thread dequeued an skb just before mctp_i2c_tx_purge() ran, it carries on into mctp_i2c_xmit(), which selects the device without checking whether midev is still attached:
mctp_i2c_xmit() {
...
case MCTP_I2C_TX_FLOW_NONE:
/* no flow: full lock & unlock */
mctp_i2c_lock_nest(midev);
mctp_i2c_device_select(midev->client, midev);
...
}The MCTP_I2C_TX_FLOW_NEW case does the same.
mctp_i2c_device_select() sets mcli->sel = midev and takes dev_hold(midev->ndev), although midev is no longer on mcli->devs. With list_empty(&midev->list) now true, every later mctp_i2c_midev_detach() returns early and never drops that reference. netdev_wait_allrefs_any() then waits forever, and mctp_i2c_add_netdev() never gets to the error path that would call mctp_i2c_midev_free().
Would it work to stop the tx thread in mctp_i2c_ndo_uninit() before the detach, as the old code effectively did? mctp_i2c_midev_free() already skips kthread_stop() when tx_thread is NULL, and the tx thread now releases the bus lock itself on exit. Or should mctp_i2c_device_select() refuse to select a midev that has already been removed from mcli->devs?
[Severity: High]
This isn't a bug introduced by this patch, but mctp-i3c seems to have the reverse form of the same ndo_uninit mismatch:
drivers/net/mctp/mctp-i3c.c:mctp_i3c_bus_add() {
...
rc = mctp_register_netdev(ndev, NULL, MCTP_PHYS_BINDING_I3C);
if (rc < 0) {
dev_warn(&ndev->dev, "netdev register failed: %d\n", rc);
goto err_free_netdev;
}
...
}This skips mctp_i3c_bus_free() because it assumes mctp_i3c_ndo_uninit() already ran. That isn't true for early failures:
- mctp_add_dev() can fail before register_netdevice() is called.
- register_netdevice() can fail in dev_get_valid_name(), for example with -EEXIST, since the name is a fixed "mctpi3c<id>".
- register_netdevice() can fail when allocating the name node.
The register_netdevice() failures take "goto out" and never reach ndo_uninit.
In those cases mbus stays on the global busdevs list, and the tx thread started by kthread_run() keeps sleeping on mbus->tx_wq. Meanwhile free_netdev() frees the netdev, and mbus with it.
Can the tx thread, or later walks of busdevs in mctp_i3c_bus_remove_all() and mctp_i3c_bus_add_new(), then use freed memory?
The approach this patch takes for mctp-i2c might fit mctp-i3c as well:
the error path always frees, and the uninit work is safe to repeat.
[Severity: Medium]
This is a pre-existing issue, and this patch neither claims to fix it nor relies on it. Does mctp_register_netdevice() leak the mctp_dev when register_netdevice() fails?
net/mctp/device.c:mctp_register_netdevice() {
...
mdev = mctp_add_dev(dev);
if (IS_ERR(mdev))
return PTR_ERR(mdev);
mdev->ops = ops;
mdev->binding = binding;
return register_netdevice(dev);
}mctp_add_dev() allocates the mdev, sets refs to 1, takes dev_hold(dev) and publishes dev->mctp_ptr. Only the NETDEV_UNREGISTER notifier (mctp_unregister()->mctp_dev_put()) frees it. register_netdevice() only reaches that notifier when a NETDEV_REGISTER notifier fails.
None of the other failure paths free it. These are dev_get_valid_name(), name node allocation, netdev_check_ops, pcpu stats, dev_index_reserve, NETDEV_POST_INIT and kobject registration. At most they send NETDEV_PRE_UNINIT, which mctp doesn't handle.
The driver's free_netdev() then frees the netdev, and the mdev and its untracked netdev hold are leaked. With CONFIG_NET_DEV_REFCNT_TRACKER, could this also trigger a ref_tracker warning at free_netdev()?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119094307.3864679-1-kenny_hsieh%40jabil.com
The three mctp-i2c ones are right. v5 will:
- take the unlock marker off and splice tx_queue out under one hold of the queue lock, so release_flow() can't requeue it in between
- stop the tx thread at the start of ndo_uninit, so on a failed register it can't select midev again after the detach
- drop the unlock_reset() in mctp_i2c_midev_free(), since the tx thread already drops the bus lock when it stops
The mctp-i3c issue and the mctp_dev leak in mctp_register_netdevice() are outside mctp-i2c, so I'll leave them out of this series.
pw-bot: cr