Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: n0ps1edzz@gmail.com
Cc: netdev@vger.kernel.org, dsahern@kernel.org, idosch@nvidia.com,
	kuniyu@amazon.com, steffen.klassert@secunet.com,
	herbert@gondor.apana.org.au, davem@davemloft.net,
	edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
	horms@kernel.org, willemb@google.com, liuhangbin@gmail.com,
	ipsec@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid()
Date: Sat, 28 Nov 2026 09:36:25 +0000	[thread overview]
Message-ID: <179585858536.3135.14235570619606863005@kernel.org> (raw)
In-Reply-To: <624bf99e815ca1a98d13ef06684c86148166fe9b.1795769101.git.n0ps1edzz@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

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] The patch does not fix the use-after-free it claims to fix.

--

xfrm: fix use-after-free in xfrm_lookup_with_ifid()

This patch changes the no-policy path of xfrm_lookup_with_ifid(). It now
reads the device of dst_orig through dst_dev_rcu() inside an RCU read-side
critical section, instead of dereferencing dst_orig->dev directly. The
commit message says this depends on a netdev_run_todo() change that waits
for RCU readers.

Link: https://lore.kernel.org/netdev/20260908224901.1591334-1-kuba@kernel.org/

> diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
> index cea6842e6454d..eafd090e3aebc 100644
> --- a/net/xfrm/xfrm_policy.c
> +++ b/net/xfrm/xfrm_policy.c

[ ... ]

> @@ -3312,7 +3314,12 @@ struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
>  	return dst;
>  
>  nopol:
> -	if ((!dst_orig->dev || !(dst_orig->dev->flags & IFF_LOOPBACK)) &&
> +	rcu_read_lock();
> +	dev = dst_dev_rcu(dst_orig);
> +	is_loopback = dev && (dev->flags & IFF_LOOPBACK);
> +	rcu_read_unlock();

[Severity: High]
In this tree, does this RCU section close the race?

The commit message says:

  This requires netdev_run_todo() to wait for RCU readers after flushing
  routes; that fix is a prerequisite of this patch.

That change isn't in this tree. Neither this patch nor the earlier
"ipv4: route: fix use-after-free in ip_route_output_flow()" touches
net/core/. The only rcu_barrier() in netdev_run_todo() still runs before
the route flush, and nothing waits for readers after the flush:

net/core/dev.c:netdev_run_todo() {
    ...
	if (!list_empty(&list))
		rcu_barrier();
    ...
	if (!list_empty(&list)) {
		rt_flush_dev(NULL);
		rt6_uncached_list_flush_dev(NULL);
	}

	cnt = 0;
	while (!list_empty(&list)) {
		struct net *net;

		dev = netdev_wait_allrefs_any(&list);
    ...
}

rt_flush_dev() and rt6_uncached_list_flush_dev() call
rcu_assign_pointer(rt->dst.dev_rcu, blackhole_netdev). They then drop the
old device reference at once through netdev_ref_replace().

netdev_wait_allrefs_any() returns immediately when netdev_refcnt_read(dev)
is 1. The device is then released through kobject_put() and
netdev_release(), which calls kvfree(dev) without waiting for an RCU grace
period.

The only grace period on the free path is the synchronize_net() in
netdev_napi_exit(). It is skipped when napi_list is empty, which is the
case for the dummy device used in the reproducer.

If dst_orig is an uncached output route, can this sequence still happen?

CPU A
xfrm_lookup_with_ifid()
    rcu_read_lock();
    dev = dst_dev_rcu(dst_orig);
    <preempted under CONFIG_PREEMPT_RCU>

CPU B
netdev_run_todo()
    rt_flush_dev(NULL)
        rcu_assign_pointer(rt->dst.dev_rcu, blackhole_netdev);
        netdev_ref_replace(...);
    netdev_wait_allrefs_any()        <-- refcnt is 1, returns at once
    kobject_put(&dev->dev.kobj)
        netdev_release()
            kvfree(dev);

CPU A
    is_loopback = dev && (dev->flags & IFF_LOOPBACK);   <-- freed dev

As far as I can tell, this makes the window smaller but doesn't close it.
That would need a synchronize_rcu() or synchronize_net() in
netdev_run_todo() after the flush and before the devices are freed.

The commit message doesn't give a SHA, Link or Depends-on for the
prerequisite, and the patch is tagged Cc: stable with Fixes: 2d151d39073a.
Could the prerequisite be named explicitly, or sent in the same series as
this patch? Otherwise a stable backport could pick up this patch without
it.

The commit message also names dst_dev_put() as the writer:

  dst_dev_put() can replace a dst's device with the blackhole device and
  drop the reference to the original device.

The dst_dev_put() calls made from NETDEV_UNREGISTER notifiers are followed
by synchronize_net() in unregister_netdevice_many_notify(). The pointer
swap with no grace period after it seems to be the rt_flush_dev(NULL) and
rt6_uncached_list_flush_dev(NULL) calls in netdev_run_todo(). Would it be
more accurate to describe those calls instead?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795769101.git.n0ps1edzz%40gmail.com

      reply	other threads:[~2026-11-28  9:36 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  8:45 [PATCH net 0/2] ipv4/xfrm: fix two dst->dev lifetime races Zhou Qiuyue
2026-11-27  6:38 ` [PATCH net 1/2] ipv4: route: fix use-after-free in ip_route_output_flow() Zhou Qiuyue
2026-11-28  9:36   ` netdev-bot+sashiko
2026-11-27  6:39 ` [PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid() Zhou Qiuyue
2026-11-28  9:36   ` netdev-bot+sashiko [this message]

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=179585858536.3135.14235570619606863005@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=ipsec@lists.linux.dev \
    --cc=kuba@kernel.org \
    --cc=kuniyu@amazon.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liuhangbin@gmail.com \
    --cc=n0ps1edzz@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=steffen.klassert@secunet.com \
    --cc=willemb@google.com \
    /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