* Re: [PATCH net v4] mctp i2c: fix double free of midev on netdev register failure
2026-11-19 9:43 [PATCH net v4] mctp i2c: fix double free of midev on netdev register failure Kenny Hsieh
@ 2026-11-23 9:54 ` netdev-bot+sashiko
0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-23 9:54 UTC (permalink / raw)
To: kenny_hsieh
Cc: netdev, jk, matt, andrew+netdev, davem, edumazet, kuba, pabeni,
stable, linux-kernel
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.
Closes: https://lore.kernel.org/netdev/179239912623.3135.11352652156626817288@kernel.org/
> diff --git a/drivers/net/mctp/mctp-i2c.c b/drivers/net/mctp/mctp-i2c.c
> index b02476d338bbf..29a186f714d1a 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,11 +785,46 @@ static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev,
[ ... ]
> +/* 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.
[ ... ]
> @@ -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?
>
> /* Remove the netdev from the parent i2c client. */
[ ... ]
> @@ -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);
> }
[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()?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119094307.3864679-1-kenny_hsieh%40jabil.com
^ permalink raw reply [flat|nested] 2+ messages in thread