* [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure
@ 2026-11-18 9:05 Kenny Hsieh
2026-11-18 9:08 ` netdev-bot+sinfo
2026-11-22 10:08 ` netdev-bot+sashiko
0 siblings, 2 replies; 3+ messages in thread
From: Kenny Hsieh @ 2026-11-18 9:05 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 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.
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
Signed-off-by: Kenny Hsieh <kenny_hsieh@jabil.com>
---
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.
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 | 86 ++++++++++++++++++++++++++-----------
1 file changed, 62 insertions(+), 24 deletions(-)
diff --git a/drivers/net/mctp/mctp-i2c.c b/drivers/net/mctp/mctp-i2c.c
index b02476d338bb..a3d0cae41e13 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,60 @@ static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev,
return midev;
}
+/* Removes midev from the parent i2c client, moving sel off midev.
+ * ndo_uninit and mctp_i2c_unregister() need this before the refcount wait,
+ * as sel's netdev reference would block it. A second call on the same
+ * midev does nothing.
+ */
+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);
+}
+
+/* Drops queued skbs, keeping the embedded unlock marker 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);
+}
+
+/* Runs inside unregister, after dev_close() and before the refcount wait,
+ * including when register_netdevice() fails at NETDEV_REGISTER. Both steps
+ * can repeat, so mctp_i2c_midev_free() still frees midev exactly once.
+ */
+static void mctp_i2c_ndo_uninit(struct net_device *dev)
+{
+ struct mctp_i2c_dev *midev = netdev_priv(dev);
+
+ mctp_i2c_midev_detach(midev);
+ mctp_i2c_tx_purge(midev);
+}
+
/* 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 +849,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,23 +871,17 @@ 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);
}
-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.
- */
- mctp_i2c_midev_free(midev);
-}
-
static int mctp_i2c_ndo_open(struct net_device *dev)
{
struct mctp_i2c_dev *midev = netdev_priv(dev);
--
2.50.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure
2026-11-18 9:05 [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure Kenny Hsieh
@ 2026-11-18 9:08 ` netdev-bot+sinfo
2026-11-22 10:08 ` netdev-bot+sashiko
1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sinfo @ 2026-11-18 9:08 UTC (permalink / raw)
To: Kenny Hsieh
Cc: netdev, Jeremy Kerr, Matt Johnston, Andrew Lunn,
David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
stable, linux-kernel
Hi!
This is an automated message. This series looks like a fix, but its
commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical
(e.g. found by LLM and not proven at runtime). If it was triggered
please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead,
reply to this email with the missing information, so that reviewers
can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include
the relevant info in the cover letter or commit messages.
--
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure
2026-11-18 9:05 [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure Kenny Hsieh
2026-11-18 9:08 ` netdev-bot+sinfo
@ 2026-11-22 10:08 ` netdev-bot+sashiko
1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-22 10:08 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: 0 · Low: 2
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:814-824) is
meant to keep the embedded midev->unlock_marker away from kfree_skb(),…
- [Low] After this patch, mctp_i2c_midev_free()
(drivers/net/mctp/mctp-i2c.c:848-849) still has "/* Unconditionally…
- [Low] The new comment in mctp_i2c_unregister()
(drivers/net/mctp/mctp-i2c.c:874-877) says the explicit…
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the sibling
driver mctp-i3c has the teardown-ownership flaw in the opposite…
--
mctp i2c: fix double free of midev on netdev register failure
This patch stops mctp_i2c_ndo_uninit() from freeing midev. midev is now
freed only in the add_netdev() error path and in mctp_i2c_unregister().
ndo_uninit only detaches midev from mcli->sel and purges tx_queue
(skipping the embedded unlock marker), and the tx thread releases the
bus rt_mutex itself before exiting.
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..a3d0cae41e131 100644
> --- a/drivers/net/mctp/mctp-i2c.c
> +++ b/drivers/net/mctp/mctp-i2c.c
[ ... ]
> @@ -782,11 +785,60 @@ static struct mctp_i2c_dev *mctp_i2c_midev_init(struct net_device *dev,
[ ... ]
> +/* Drops queued skbs, keeping the embedded unlock marker 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]
Can the unlock marker still reach kfree_skb() here? The marker is
unlinked in one tx_queue.lock section, and then skb_queue_purge() takes
the lock again separately.
The unlink leaves unlock_marker.next == NULL. Between the two critical
sections, mctp_i2c_release_flow() can queue the marker again:
drivers/net/mctp/mctp-i2c.c: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);
...
}
One possible sequence during device teardown:
CPU0
mctp_i2c_unregister()
mctp_unregister_netdev()
unregister_netdevice_many_notify()
mctp_i2c_ndo_uninit()
mctp_i2c_tx_purge()
__skb_unlink(&midev->unlock_marker, &midev->tx_queue)
spin_unlock_irqrestore(&midev->tx_queue.lock, flags)
CPU1 (or softirq on CPU0 after the irqrestore)
mctp_sk_expire_keys()
__mctp_key_remove()
mctp_dev_release_key()
mctp_i2c_release_flow()
__skb_queue_tail(&midev->tx_queue, &midev->unlock_marker)
CPU0
skb_queue_purge(&midev->tx_queue)
skb_queue_purge_reason()
kfree_skb_reason(&midev->unlock_marker)
ndo_uninit runs before netdev_wait_allrefs(). At this point flow keys
can still hold a mctp_dev reference, and mdev->ops is never cleared.
Keys can also be released by socket close, by route input through
__mctp_key_done_in(), or by a final mctp_key_unref(). None of these
hold RTNL. The tx thread has already been stopped, so nothing else
drains the queue.
The marker is embedded in netdev_priv and its users count is 0. A
kfree_skb() on it would trigger the refcount_t underflow warning in
skb_unref().
The commit message says the marker "must not reach kfree_skb() through
the purge". Could this be done in a single critical section instead?
For example, splice tx_queue into a local list and unlink the marker
from it under one tx_queue.lock hold, or skip the marker while purging.
> +
> +/* Runs inside unregister, after dev_close() and before the refcount wait,
> + * including when register_netdevice() fails at NETDEV_REGISTER. Both steps
> + * can repeat, so mctp_i2c_midev_free() still frees midev exactly once.
> + */
> +static void mctp_i2c_ndo_uninit(struct net_device *dev)
> +{
> + struct mctp_i2c_dev *midev = netdev_priv(dev);
> +
> + mctp_i2c_midev_detach(midev);
> + mctp_i2c_tx_purge(midev);
> +}
> +
> /* 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 +849,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 call still needed? The existing comment
just above it reads "Unconditionally unlock on close". The new comment
in mctp_i2c_tx_thread() says:
/* Only the rt_mutex owner may release the bus lock */
mctp_i2c_lock_nest() is only reached from mctp_i2c_xmit() in the tx
thread, and the tx thread now resets i2c_lock_count itself when it
stops. So by the time mctp_i2c_midev_free() gets here, i2c_lock_count
should always be 0, and this call does nothing.
Could it be dropped, or replaced with something like
WARN_ON(midev->i2c_lock_count), so it doesn't contradict the new
owner-only rule?
>
> /* Remove the netdev from the parent i2c client. */
[ ... ]
> @@ -827,23 +871,17 @@ 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);
> +
[Severity: Low]
Is this comment accurate? mctp_i2c_ndo_uninit() also calls
mctp_i2c_midev_detach(). unregister_netdevice_many_notify() calls
ndo_uninit before net_set_todo(), and the refcount wait in
netdev_wait_allrefs() only happens later, from netdev_run_todo() at
rtnl_unlock() time.
So mctp_unregister_netdev() would not block on sel's reference even
without this early detach.
If the early detach exists for some other reason, such as moving sel to
a sibling midev before unregister starts, could the comment say that
instead?
> 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);
> }
>
> -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.
> - */
> - mctp_i2c_midev_free(midev);
> -}
[Severity: High]
This is a pre-existing issue and was not introduced by this patch.
mctp-i3c has the same ndo_uninit ownership problem, but in the opposite
direction.
mctp_i3c_ndo_uninit() does the full mctp_i3c_bus_free(), and
mctp_i3c_bus_add() skips mctp_i3c_bus_free() when registration fails:
drivers/net/mctp/mctp-i3c.c:mctp_i3c_bus_add() {
...
list_add(&mbus->list, &busdevs);
...
mbus->tx_thread = kthread_run(mctp_i3c_tx_thread, mbus,
"%s/tx", ndev->name);
...
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;
}
...
}
register_netdevice() can return before it reaches its err_uninit label,
in which case ndo_uninit is never called. Two cases:
- dev_get_valid_name() fails, for example with -EEXIST when a netdev
named mctpi3c<id> already exists.
- netdev_name_node_head_alloc() fails.
free_netdev() then sees NETREG_UNINITIALIZED and calls kvfree() on the
netdev, which contains mbus.
Does this leave a freed mbus on busdevs? That list is later walked by
mctp_i3c_bus_add_new(), mctp_i3c_notify_bus_remove() and
mctp_i3c_bus_remove_all(). And would mbus->tx_thread keep running on
the freed mbus->tx_wq?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118090515.3390034-1-kenny_hsieh%40jabil.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-22 10:08 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 9:05 [PATCH net v3] mctp i2c: fix double free of midev on netdev register failure Kenny Hsieh
2026-11-18 9:08 ` netdev-bot+sinfo
2026-11-22 10:08 ` netdev-bot+sashiko
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®