mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net 1/2] ipv4: route: fix use-after-free in ip_route_output_flow()
  2026-11-27  8:45 [PATCH net 0/2] ipv4/xfrm: fix two dst->dev lifetime races Zhou Qiuyue
@ 2026-11-27  6:38 ` 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
  1 sibling, 1 reply; 5+ messages in thread
From: Zhou Qiuyue @ 2026-11-27  6:38 UTC (permalink / raw)
  To: netdev
  Cc: David Ahern, Ido Schimmel, Kuniyuki Iwashima, Steffen Klassert,
	Herbert Xu, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Willem de Bruijn, Hangbin Liu, ipsec,
	linux-kernel

ip_route_output_flow() drops the RCU read lock taken during route lookup
before reading rt->dst.dev->ifindex.  The rtable remains referenced, but
route teardown can replace dst->dev with blackhole_netdev and drop its
reference to the old net_device.

A concurrent netdev_run_todo() can therefore free the old device after
the pointer is loaded and before its ifindex is read.  KASAN reported the
resulting use-after-free twice while UDP sends raced with dummy-device
teardown.

Load and dereference the device through dst_dev_rcu() in an RCU read-side
critical section.  This requires netdev_run_todo() to wait for RCU readers
after flushing routes; that fix is a prerequisite of this patch.

OpenAI Codex was used to analyze the supplied KASAN traces and reproducer
and to draft this patch and changelog.

Fixes: 95c47f9cf5e0 ("ipv4: call dst_dev_put() properly")
Cc: stable@vger.kernel.org
Signed-off-by: Zhou Qiuyue <n0ps1edzz@gmail.com>
---
 net/ipv4/route.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index cd69242af..b16a9d05f 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -2982,7 +2982,9 @@ struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
 		return rt;
 
 	if (flp4->flowi4_proto) {
-		flp4->flowi4_oif = rt->dst.dev->ifindex;
+		rcu_read_lock();
+		flp4->flowi4_oif = dst_dev_rcu(&rt->dst)->ifindex;
+		rcu_read_unlock();
 		rt = dst_rtable(xfrm_lookup_route(net, &rt->dst,
 						  flowi4_to_flowi(flp4),
 						  sk, 0));
-- 
2.56.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid()
  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-27  6:39 ` Zhou Qiuyue
  2026-11-28  9:36   ` netdev-bot+sashiko
  1 sibling, 1 reply; 5+ messages in thread
From: Zhou Qiuyue @ 2026-11-27  6:39 UTC (permalink / raw)
  To: netdev
  Cc: David Ahern, Ido Schimmel, Kuniyuki Iwashima, Steffen Klassert,
	Herbert Xu, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Willem de Bruijn, Hangbin Liu, ipsec,
	linux-kernel

dst_dev_put() can replace a dst's device with the blackhole device and
drop the reference to the original device.  The device can then be freed
after the unregistering netdevice's todo work runs.

xfrm_lookup_with_ifid() reads dst_orig->dev directly in its no-policy
path.  It can therefore dereference the original device after that device
has been freed.  KASAN reported this race three times while a reproducer
repeatedly deleted and recreated a dummy interface and exercised the XFRM
no-policy path.

Read the device through dst_dev_rcu() in an RCU read-side critical section.
This requires netdev_run_todo() to wait for RCU readers after flushing
routes; that fix is a prerequisite of this patch.

This access was also identified during review of an earlier XFRM device
lifetime fix and left for a follow-up.

OpenAI Codex was used to analyze the supplied KASAN traces and reproducer
and to draft this patch and changelog.

Reported-by: Jakub Kicinski <kuba@kernel.org>
Link: https://lore.kernel.org/netdev/20260908224901.1591334-1-kuba@kernel.org/
Fixes: 2d151d39073a ("xfrm: Add possibility to set the default to block if we have no policy")
Cc: stable@vger.kernel.org
Signed-off-by: Zhou Qiuyue <n0ps1edzz@gmail.com>
---
 net/xfrm/xfrm_policy.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index cea6842e6..eafd090e3 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -3186,8 +3186,10 @@ struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
 	struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
 	struct xfrm_dst *xdst;
 	struct dst_entry *dst, *route;
+	struct net_device *dev;
 	u16 family = dst_orig->ops->family;
 	u8 dir = XFRM_POLICY_OUT;
+	bool is_loopback;
 	int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
 
 	dst = NULL;
@@ -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();
+
+	if (!is_loopback &&
 	    READ_ONCE(net->xfrm.policy_default[dir]) == XFRM_USERPOLICY_BLOCK) {
 		err = -EPERM;
 		goto error;
-- 
2.56.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH net 0/2] ipv4/xfrm: fix two dst->dev lifetime races
@ 2026-11-27  8:45 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-27  6:39 ` [PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid() Zhou Qiuyue
  0 siblings, 2 replies; 5+ messages in thread
From: Zhou Qiuyue @ 2026-11-27  8:45 UTC (permalink / raw)
  To: netdev
  Cc: David Ahern, Ido Schimmel, Kuniyuki Iwashima, Steffen Klassert,
	Herbert Xu, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman, Willem de Bruijn, Hangbin Liu, ipsec,
	linux-kernel

These two dst->dev readers were found after the four-patch v2 series had
already been sent.  This follow-up contains only the new fixes and does
not resend any patch from that series.

Both patches depend on the post-route-flush grace period added by:

  [PATCH net v2 1/4] net: wait for RCU readers after flushing routes
  https://lore.kernel.org/netdev/ae140ad9c69d3403d3596b1187b86fdd232292e8.1795754281.git.n0ps1edzz@gmail.com/

Please apply them after or together with that patch.  The
prerequisite-patch-id below records the same dependency.

dst_dev_put() can replace dst->dev with blackhole_netdev and drop the
reference to the original device.  The route-output and XFRM no-policy
paths still had direct readers that could race with the original device
being freed.  Convert both readers to dst_dev_rcu() under an RCU
read-side critical section.

On a v7.4-rc2 net-next KASAN kernel, the supplied reproducer triggered
the route report in 2 of 2 runs and the XFRM report in 3 of 3 runs while
deleting and recreating a dummy interface.  Fixed-patch runtime reruns
have not been performed.

An x86_64 defconfig W=1 build of net/core/dev.o, net/ipv4/route.o, and
net/xfrm/xfrm_policy.o completed successfully on the exact
prerequisite-plus-two-patch tree.

OpenAI Codex was used to analyze the supplied KASAN traces and
reproducer and to draft these patches and changelogs.

Zhou Qiuyue (2):
  ipv4: route: fix use-after-free in ip_route_output_flow()
  xfrm: fix use-after-free in xfrm_lookup_with_ifid()

 net/ipv4/route.c       | 4 +++-
 net/xfrm/xfrm_policy.c | 9 ++++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)


base-commit: e1055cbfd6eb578417f3ce983cf44462e8465267
prerequisite-patch-id: 0e86b27e6eeb1f9bdcc54de4115415ecd29d4e70
-- 
2.56.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net 1/2] ipv4: route: fix use-after-free in ip_route_output_flow()
  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
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28  9:36 UTC (permalink / raw)
  To: n0ps1edzz
  Cc: netdev, dsahern, idosch, kuniyu, steffen.klassert, herbert,
	davem, edumazet, kuba, pabeni, horms, willemb, liuhangbin, ipsec,
	linux-kernel

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

Critical: 0 · High: 2 · Medium: 0 · 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] The fix does not work as submitted, because the RCU grace period
  it depends on is missing.
- [High] The fix leaves the same use-after-free in the code that runs
  right after it on the same UDP send path.
- [Low] ipv4: route: Fixes trailer cites wrong commit for
  ip_route_output_flow()

--

ipv4: route: fix use-after-free in ip_route_output_flow()

The patch changes how ip_route_output_flow() reads the output device. It
now reads it through dst_dev_rcu() inside an RCU read-side critical
section, not as a plain rt->dst.dev->ifindex load. The commit message
says this depends on a separate fix that makes netdev_run_todo() wait
for RCU readers after flushing routes.

> diff --git a/net/ipv4/route.c b/net/ipv4/route.c
> index cd69242afab07..b16a9d05f2217 100644
> --- a/net/ipv4/route.c
> +++ b/net/ipv4/route.c
> @@ -2982,7 +2982,9 @@ struct rtable *ip_route_output_flow(struct net *net, struct flowi4 *flp4,
>  		return rt;
>  
>  	if (flp4->flowi4_proto) {
> -		flp4->flowi4_oif = rt->dst.dev->ifindex;
> +		rcu_read_lock();
> +		flp4->flowi4_oif = dst_dev_rcu(&rt->dst)->ifindex;
> +		rcu_read_unlock();

[Severity: Low]
Is 95c47f9cf5e0 the right commit for the Fixes tag? When that commit
landed, ip_route_output_flow() did not read rt->dst.dev at all.
ip_route_output_key_hash_rcu() filled in the output interface with
fl4->flowi4_oif = dev_out->ifindex, and ip_route_output_key_hash()
still held rcu_read_lock() at that point.

Commit 2fbc6e89b2f1 ("ipv4: Update exception handling for multipath
routes via same device") removed that assignment. It came back in
commit 874fb9e2ca94 ("ipv4: Restore flowi4_oif update before call to
xfrm_lookup_route"). That commit added the flp4->flowi4_oif =
rt->dst.dev->ifindex load to ip_route_output_flow(), which runs after
__ip_route_output_key() has already dropped the RCU read lock. That is
the line this patch changes.

The Fixes tag decides which stable branches pick this up. Pointing at
the 2017 commit could send the patch to trees where this line does not
exist.

Should the tag name 874fb9e2ca94 instead? If the teardown window also
depends on ca6854799069 and f12d2907992d, should those be listed too?

[Severity: High]
Does this rcu_read_lock() section protect the old net_device in this
tree? It only helps if the code that frees the device waits for a grace
period after rt->dst.dev_rcu is switched to blackhole_netdev. I could
not find such a wait.

unregister_netdevice_many_notify() sets dev->dismantle before the
NETDEV_UNREGISTER notifiers run. So rt_flush_dev(dev), called from
fib_netdev_event(), returns early:

net/ipv4/route.c:rt_flush_dev() {
	if (dev && dev->dismantle)
		return;
	...
}

The flush that does the work happens later, in netdev_run_todo(), after
its only rcu_barrier():

net/core/dev.c:netdev_run_todo() {
	...
	/* Wait for rcu callbacks to finish before next phase */
	if (!list_empty(&list))
		rcu_barrier();
	...
	if (!list_empty(&list)) {
		rt_flush_dev(NULL);
		rt6_uncached_list_flush_dev(NULL);
	}
	...
}

rt_flush_dev(NULL) publishes blackhole_netdev and drops the old device
reference right away:

	rcu_assign_pointer(rt->dst.dev_rcu, blackhole_netdev);
	netdev_ref_replace(rt_dev, blackhole_netdev,
			   &rt->dst.dev_tracker, GFP_ATOMIC);

If that was the last extra reference, netdev_wait_allrefs_any() takes
its fast path without any RCU wait:

	list_for_each_entry(dev, list, todo_list)
		if (netdev_refcnt_read(dev) == 1)
			return dev;

The device then reaches netdev_release() in net/core/net-sysfs.c, which
frees it synchronously:

	/* no need to wait for rcu grace period:
	 * device is dead and about to be freed.
	 */
	kfree(rcu_access_pointer(dev->ifalias));
	kvfree(dev);

Wouldn't this interleaving still produce the KASAN report?

CPU0                                  CPU1
ip_route_output_flow()
  rcu_read_lock()
  dev = dst_dev_rcu(&rt->dst)
                                      netdev_run_todo()
                                        rt_flush_dev(NULL)
                                        netdev_wait_allrefs_any()
                                        ...
                                        netdev_release()
                                          kvfree(dev)
  dev->ifindex   <-- freed
  rcu_read_unlock()

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 does not seem to be in the tree or in this series, and the
message gives no commit ID or link for it. The other patch in the
series, "xfrm: fix use-after-free in xfrm_lookup_with_ifid()", only
touches net/xfrm/xfrm_policy.c and relies on the same missing wait.

Could the prerequisite be added to this series or named explicitly? One
option is a synchronize_net() after the rt_flush_dev(NULL) /
rt6_uncached_list_flush_dev(NULL) block and before
netdev_wait_allrefs_any().

About the Fixes tag: commit ca6854799069 ("ipv4: Batch rt_flush_dev() in
netdev_run_todo().") and commit f12d2907992d ("ipv6: Batch
rt6_uncached_list_flush_dev() in netdev_run_todo().") moved the flush
into netdev_run_todo(), after the rcu_barrier(). Before that, it ran from
the notifier, ahead of a synchronize_net(). Those commits look like the
likely origin of the window, but the Fixes tag only names 95c47f9cf5e0.

With Cc: stable and no dependency note, would a stable backport of this
patch alone change anything?

>  		rt = dst_rtable(xfrm_lookup_route(net, &rt->dst,
>  						  flowi4_to_flowi(flp4),
>  						  sk, 0));

[Severity: High]
If a referenced rtable does not keep rt->dst.dev alive, isn't the same
race still open on this send path right after ip_route_output_flow()
returns?

udp_sendmsg() passes the same rt to ip_make_skb() without
rcu_read_lock(). That path then reads the device through the plain
pointer:

udp_sendmsg()
  ip_route_output_flow()
  ip_make_skb()
    ip_setup_cork()
      cork->fragsize = ip_sk_use_pmtu(sk) ?
		 dst4_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu);
    __ip_append_data()
      hh_len = LL_RESERVED_SPACE(rt->dst.dev);
      ... rt->dst.dev->features ...

__ip_append_data() also reads rt->dst.dev->features in several other
places. raw_send_hdrinc() in net/ipv4/raw.c follows the same pattern:

	if (length > rt->dst.dev->mtu) {
	...
	hlen = LL_RESERVED_SPACE(rt->dst.dev);
	tlen = rt->dst.dev->needed_tailroom;

These loads run after the ifindex read and are not inside any RCU
section. Even with the prerequisite grace period in place, can the
"UDP sends raced with dummy-device teardown" reproducer still reach a
freed net_device here?

Should these sites get the same RCU treatment? Or should teardown keep
the device alive while dst references to it still exist?

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net 2/2] xfrm: fix use-after-free in xfrm_lookup_with_ifid()
  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
  0 siblings, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28  9:36 UTC (permalink / raw)
  To: n0ps1edzz
  Cc: netdev, dsahern, idosch, kuniyu, steffen.klassert, herbert,
	davem, edumazet, kuba, pabeni, horms, willemb, liuhangbin, ipsec,
	linux-kernel

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-28  9:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 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®