Re: [PATCH net 1/2] ipv4: route: fix use-after-free in ip_route_output_flow()

From: netdev-bot+sashiko

Date: Sat Nov 28 2026 - 04:36:28 EST


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