Netdev List
 help / color / mirror / Atom feed
From: Jianlin Shi <shijianlin11@foxmail.com>
To: netdev@vger.kernel.org
Cc: mst@redhat.com, jasowang@kernel.org, eperezma@redhat.com,
	xuanzhuo@tsinghua.org.cn, andrew+netdev@amiga.org.uk,
	davem@davemloft.net, edumazet@gmail.com, kuba@kernel.org,
	pabeni@redhat.com, talgi@efi.com, hengqi@loongson.cn,
	horms@kernel.org, virtualization@vger.kernel.org
Subject: [PATCH net v3 1/2] dim: clear irq_moder on init failure
Date: Sun, 22 Nov 2026 17:05:56 +0800	[thread overview]
Message-ID: <20261122090557.19766-2-shijianlin11@foxmail.com> (raw)
In-Reply-To: <20261122090557.19766-1-shijianlin11@foxmail.com>

net_dim_init_irq_moder() publishes the allocated struct via
dev->irq_moder before copying the moderation profiles.  If a
kmemdup() fails, the error path frees the object but leaves
dev->irq_moder dangling.  net_dim_free_irq_moder() treats a
non-NULL irq_moder as live: it dereferences
dev->irq_moder->rx_profile / tx_profile under RTNL and frees the
object a second time.

On current mainline the only in-tree caller of
net_dim_init_irq_moder() is virtnet_probe(), and its failure path
today does "goto free", which only reaches free_netdev(); nothing
calls net_dim_free_irq_moder() there, so the dangling pointer is
never dereferenced.  The bug becomes reachable once the caller
unwinds through the existing free_irq_moder label, as virtio-net
starts doing in the follow-up fix:

    virtnet_probe()
      virtnet_init_irq_moder()
        net_dim_init_irq_moder()    /* kmemdup fails:
                                       kfree(moder),
                                       dev->irq_moder stale */
      goto free_irq_moder
      virtnet_free_irq_moder()
        net_dim_free_irq_moder()    /* non-NULL irq_moder =>
                                       rtnl_dereference(
                                         dev->irq_moder->rx_profile)
                                       use-after-free,
                                       kfree() a second time */

This patch is the prerequisite that makes such an unwind safe;
without it the follow-up would trade the virtqueue leak for a
use-after-free and a double kfree on this error path.

NULL the pointer after freeing it, so a later
net_dim_free_irq_moder() is a no-op and the device is left as if
initialization never happened.

Fixes: f750dfe825b90 ("ethtool: provide customized dim profile management")
Suggested-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Jianlin Shi <shijianlin11@foxmail.com>
---
 lib/dim/net_dim.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/lib/dim/net_dim.c b/lib/dim/net_dim.c
index d8d4f6553559..bf59deae6a29 100644
--- a/lib/dim/net_dim.c
+++ b/lib/dim/net_dim.c
@@ -141,6 +141,7 @@ int net_dim_init_irq_moder(struct net_device *dev, u8 profile_flags,
 	kfree(rxp);
 free_moder:
 	kfree(moder);
+	dev->irq_moder = NULL;
 	return -ENOMEM;
 }
 EXPORT_SYMBOL(net_dim_init_irq_moder);
-- 
2.43.0

  reply	other threads:[~2026-11-22  9:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22  9:05 [PATCH net v3 0/2] virtio-net/dim: fix irq moderation init failure unwind Jianlin Shi
2026-11-22  9:05 ` Jianlin Shi [this message]
2026-11-22  9:05 ` [PATCH net v3 2/2] virtio-net: unwind VQs when irq moderation init fails Jianlin Shi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261122090557.19766-2-shijianlin11@foxmail.com \
    --to=shijianlin11@foxmail.com \
    --cc=andrew+netdev@amiga.org.uk \
    --cc=davem@davemloft.net \
    --cc=edumazet@gmail.com \
    --cc=eperezma@redhat.com \
    --cc=hengqi@loongson.cn \
    --cc=horms@kernel.org \
    --cc=jasowang@kernel.org \
    --cc=kuba@kernel.org \
    --cc=mst@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=talgi@efi.com \
    --cc=virtualization@vger.kernel.org \
    --cc=xuanzhuo@tsinghua.org.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox