net: fix dst->dev lifetime races
Subsystems (3)
v2 cover net: fix dst->dev lifetime races
The conversion of dst->dev to an RCU-protected pointer left two route confirmation callbacks and the IPv6 neighbour lookup callback loading it without RCU protection. Also, netdev_run_todo() did not wait for readers after flushing routes that can release the last dst-held device reference.
Patch 1 adds the teardown-side grace period after the route flushes. Patches 2 and 3 protect the IPv4 and IPv6 confirmation callbacks. Patch 4 protects the IPv6 neighbour lookup callback, matching its IPv4 counterpart.
Finding and validation:
- Patches 2 and 3 came from an AI-assisted source audit. Delay-assisted KASAN runs on the exact v7.4-rc3 commit reproduced both bugs:
BUG: KASAN: slab-use-after-free in ipv4_confirm_neigh+0xcc8/0xce0
BUG: KASAN: slab-use-after-free in ip6_confirm_neigh+0x6a7/0xbd0The delay was debug-only window amplification and is not in this series.
- Ido identified the missing grace period in patch 1 and the unprotected lookup in patch 4 during review. Both were validated by source inspection; those two windows have not been triggered independently at runtime.
Testing:
- KASAN reruns on v7.4-rc2 with the callback fixes corresponding to patches 2 and 3 completed without a report. The IPv4 run made 392065 callback calls across 110 dst_dev_put() calls and 48 device frees. The IPv6 run made 13144 callback calls across 36 dst_dev_put() calls and 48 device frees.
- On the current net tree base below, these objects built with W=1:
net/core/dev.o, net/ipv4/route.o, and net/ipv6/route.o.
- A full allmodconfig or allyesconfig build and networking selftests were not run.
This is treated as a regular bug: the demonstrated teardown needs privileged network-device administration, and no privilege-boundary crossing or exploit has been demonstrated.
Changes since v1:
- Combine the separate IPv4 and IPv6 submissions into one series, as requested by Ido and Kuniyuki.
- Add the post-flush synchronize_net() patch suggested by Ido.
- Add the ip6_dst_neigh_lookup() patch suggested by Ido.
- Distinguish the dynamically reproduced findings from the source-inspection findings and document the validation limits.
v1 IPv6:
https://lore.kernel.org/netdev/[email protected]/
v1 IPv4:
https://lore.kernel.org/netdev/[email protected]/
Review:
https://lore.kernel.org/netdev/20261125190113.GA421469@shredder/
AI assistance disclosure: Codex was used for the source audit, test-harness automation, patch drafting, and changelog drafting. The runtime results above come from the described QEMU/KASAN runs. The Signed-off-by tags were added at the submitter's explicit request.
Zhou Qiuyue (4):
net: wait for RCU readers after flushing routes
ipv4: route: fix use-after-free in ipv4_confirm_neigh()
ipv6: route: fix use-after-free in ip6_confirm_neigh()
ipv6: route: fix use-after-free in ip6_dst_neigh_lookup()
net/core/dev.c | 1 + net/ipv4/route.c | 12 +++++++++--- net/ipv6/route.c | 24 ++++++++++++++++++------ 3 files changed, 28 insertions(+), 9 deletions(-)
base-commit: e1055cbfd6eb578417f3ce983cf44462e8465267
Signature · 1 line
2.56.1.windows.1
PATCH v2 1/4 net: wait for RCU readers after flushing routes
netdev_run_todo() waits for outstanding RCU callbacks before flushing routes. However, rt_flush_dev() and rt6_uncached_list_flush_dev() can release the last reference held by a dst to an unregistering net_device.
A reader can load the old device from dst->dev under RCU before dst_dev_put() replaces it with blackhole_netdev. A route release from an RCU callback can then drop the last reference while that newer reader is still active. The earlier rcu_barrier() does not wait for such a read-side critical section, so netdev_run_todo() can proceed to free the device underneath the reader.
Wait for pre-existing networking RCU read-side critical sections after both route flushes and before freeing the devices.
Ido Schimmel identified this missing grace period during review. The lifetime was validated by source inspection; this specific window has not been triggered independently at runtime.
--- net/core/dev.c | 1 + 1 file changed, 1 insertion(+)
net/core/dev.c +1 -0
@@ -12039,6 +12039,7 @@ void netdev_run_todo(void)if (!list_empty (&list)) {rt_flush_dev (NULL );rt6_uncached_list_flush_dev (NULL ); +synchronize_net (); } cnt =0 ;
Signature · 1 line
2.56.1.windows.1
[email protected]
AI reviewRe: [PATCH net v2 1/4] net: wait for RCU readers after flushing routes
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.
Critical: 0 · High: 1 · 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 new grace period runs too early, so the fix is incomplete.
- [Low] The Fixes: tag only names 4a6ce2b6f2ec ("net: introduce a new function dst_dev_put()", 2017).
--
net: wait for RCU readers after flushing routes
This patch adds a synchronize_net() call to netdev_run_todo(), after rt_flush_dev(NULL) and rt6_uncached_list_flush_dev(NULL). The goal is to wait for RCU readers that loaded the old device from dst->dev before dst_dev_put() dropped the last device reference, so the device is not freed underneath them.
Quoted 1 line
Fixes 4a6ce2b6f2ec ("net: introduce a new function dst_dev_put()")
[Severity: Low]
Should this also have Fixes tags for these two commits?
ca6854799069 ("ipv4: Batch rt_flush_dev() in netdev_run_todo().") f12d2907992d ("ipv6: Batch rt6_uncached_list_flush_dev() in netdev_run_todo().")
Those commits added the block in netdev_run_todo() that this hunk changes:
rt_flush_dev(NULL); rt6_uncached_list_flush_dev(NULL);
They also added the dev->dismantle early return. That return makes the NETDEV_UNREGISTER-time flushes in fib_netdev_event() and rt6_disable_ip() into no-ops.
Before those commits, both flushes ran from the NETDEV_UNREGISTER notifier. The synchronize_net() in unregister_netdevice_many_notify() always followed them, before net_set_todo():
net/core/dev.c:unregister_netdevice_many_notify() {
...
synchronize_net();
...
}So the window in the first paragraph of the commit message seems to exist only on top of the batching commits. The 2017 tag seems to apply only to the RCU-callback case in the second paragraph.
With Cc: stable, the hunk will not apply to trees that lack the batching commits. Those trees would also need the wait in a different place.
Could the tags and the commit message keep these two cases apart?
net/core/dev.c void netdev_run_todo(void) · line 12045
Quoted 12 lines
net/core/dev.c +1
-0@@ -12039,6 +12039,7 @@ void netdev_run_todo(void)if (!list_empty (&list)) {rt_flush_dev (NULL );rt6_uncached_list_flush_dev (NULL ); +synchronize_net (); } cnt =0 ;
[Severity: High]
Is this grace period late enough to cover every final device reference drop?
It only covers references dropped before this point. Some RCU callbacks can still call dst_dev_put() after synchronize_net() has returned. dst_dev_put() drops the device reference right after the pointer swap:
net/core/dst.c:dst_dev_put() {
...
rcu_assign_pointer(dst->dev_rcu, blackhole_netdev);
netdev_ref_replace(dev, blackhole_netdev, &dst->dev_tracker,
GFP_ATOMIC);
...
}The first path starts in the flush this patch targets. rt6_uncached_list_flush_dev() calls fib6_info_release() on entries that have fib6_destroying set:
net/ipv6/route.c:rt6_uncached_list_flush_dev() {
...
from = unrcu_pointer(xchg(&rt->from, NULL));
fib6_info_release(from);
...
}If that drops the last fib6_ref, it queues call_rcu_hurry(&f6i->rcu, fib6_info_destroy_rcu). This happens after the first rcu_barrier() in netdev_run_todo(). The callback then runs:
fib6_info_destroy_rcu()
fib6_nh_release()
fib6_nh_release_dsts()
dst_dev_put(&pcpu_rt->dst)It does this on the per-CPU routes of a fib6_nh on the dying device. Those routes hold their real device reference until then, because fib6_purge_rt() only drops their from reference.
The second path is a chained nexthop callback. fib6_info_destroy_rcu() or free_fib_info_rcu() can run during the first rcu_barrier() and call nexthop_put(). nexthop_put() then queues call_rcu_hurry(&nh->rcu, nexthop_free_rcu) from inside that callback. rcu_barrier() does not wait for the chained callback, which later runs:
nexthop_free_rcu()
nexthop_free_single()
fib6_nh_release()
fib6_nh_release_dsts()
dst_dev_put()IPv4 has the same pattern through rt_fibinfo_free_cpus().
In both paths, a reader can enter RCU after synchronize_net() has started and before the callback runs. That reader can still load the old device. One example is ip6_dst_mtu_maybe_forward(), reached through dst_mtu() on a socket's cached per-CPU dst:
include/net/ip6_route.h:ip6_dst_mtu_maybe_forward() {
...
rcu_read_lock();
idev = __in6_dev_get(dst_dev_rcu(dst));
...
}netdev_wait_allrefs_any() returns as soon as the refcount reaches 1. On the first pass wait is 0, so msleep() is skipped. The device can be returned right after rcu_barrier() has run the callback:
net/core/dev.c:netdev_wait_allrefs_any() {
...
rcu_barrier();
if (!wait) {
wait = WAIT_REFS_MIN_MSECS;
} else {
msleep(wait);
...
}netdev_run_todo() then moves straight to freeing the device, with no grace period after the last reference drop:
netdev_do_free_pcpu_stats() priv_destructor()
free_netdev()
kobject_put() -> netdev_release() -> kvfree(dev)
Can the reader still be running when kvfree(dev) is called?
Two later patches in this series depend on this grace period:
"ipv4: route: fix use-after-free in ipv4_confirm_neigh()" "ipv6: route: fix use-after-free in ip6_confirm_neigh()"
Both say "The grace period added to netdev_run_todo() ensures that the device remains alive until the read-side critical section finishes". No later patch in the series changes net/core/.
Would it be better to wait for a grace period after netdev_wait_allrefs_any() sees refcnt == 1 and before the free? That could be a batched synchronize_net() at that point, or an RCU-deferred netdev free. Either would replace the placement in this patch rather than add to it.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795754281.git.n0ps1edzz%40gmail.com
PATCH v2 2/4 ipv4: route: fix use-after-free in ipv4_confirm_neigh()
In reply to Zhou Qiuyuedst_confirm_neigh() is reachable from paths that do not hold an RCU read lock, including udp_sendmsg() with MSG_CONFIRM and MSG_PROBE.
ipv4_confirm_neigh() loads the egress device with dst_dev(). Concurrent route teardown can replace dst->dev with blackhole_netdev and drop the route's reference to the old net_device. The old device can then be freed before ipv4_confirm_neigh() finishes using the pointer.
An AI-assisted source audit identified the unsafe load. A delay inserted after it to widen the race window reproduced the issue on v7.4-rc3 with KASAN:
BUG: KASAN: slab-use-after-free in ipv4_confirm_neigh+0xcc8/0xce0 Read of size 8 ... by task confirm_race4/126
Hold the RCU read lock across the callback and use dst_dev_rcu() to load the device. The grace period added to netdev_run_todo() ensures that the device remains alive until the read-side critical section finishes.
--- net/ipv4/route.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
net/ipv4/route.c +9 -3
@@ -441,19 +441,25 @@ static struct neighbour *ipv4_dst_neigh_lookup(const struct dst_entry *dst,static void ipv4_confirm_neigh (const struct dst_entry *dst,const void *daddr) { const struct rtable *rt = container_of(dst, struct rtable, dst); -struct net_device *dev =dst_dev (dst); +struct net_device *dev;const __be32 *pkey = daddr; +rcu_read_lock (); + dev =dst_dev_rcu (dst); +if (rt->rt_gw_family ==AF_INET ) { pkey = (const __be32 *)&rt->rt_gw4; }else if (IS_ENABLED (CONFIG_IPV6 ) && rt->rt_gw_family ==AF_INET6 ) { -return __ipv6_confirm_neigh (dev, &rt->rt_gw6); +__ipv6_confirm_neigh (dev, &rt->rt_gw6); + goto out; }else if (!daddr || (rt->rt_flags & (RTCF_MULTICAST |RTCF_BROADCAST |RTCF_LOCAL ))) { -return ; + goto out; }__ipv4_confirm_neigh (dev, *(__force u32 *)pkey); +out: +rcu_read_unlock (); }/* Hash tables of size 2048..262144 depending on RAM size.
Signature · 1 line
2.56.1.windows.1
[email protected]
AI reviewRe: [PATCH net v2 2/4] ipv4: route: fix use-after-free in ipv4_confirm_neigh()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
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 fix makes ipv4_confirm_neigh() (net/ipv4/route.c) load the device with dst_dev_rcu() inside rcu_read_lock().
--
ipv4: route: fix use-after-free in ipv4_confirm_neigh()
This patch makes ipv4_confirm_neigh() hold rcu_read_lock() and load the egress device with dst_dev_rcu() instead of dst_dev(). It relies on the grace period in netdev_run_todo() to keep the old net_device alive after a concurrent dst_dev_put() swaps dst->dev to blackhole_netdev.
Quoted 5 lines
net/ipv4/route.c +0
-0
net/ipv4/route.c static struct neighbour *ipv4_dst_neigh_lookup(const struct dst_entry *dst, · line 448
Quoted 9 lines
net/ipv4/route.c +3
-1@@ -441,19 +441,25 @@ static struct neighbour *ipv4_dst_neigh_lookup(const struct dst_entry *dst,static void ipv4_confirm_neigh (const struct dst_entry *dst,const void *daddr) { const struct rtable *rt = container_of(dst, struct rtable, dst); -struct net_device *dev =dst_dev (dst); +struct net_device *dev;const __be32 *pkey = daddr; +rcu_read_lock (); + dev =dst_dev_rcu (dst);
[Severity: High]
The commit message says:
The grace period added to netdev_run_todo() ensures that the device remains alive until the read-side critical section finishes.
Does that hold for dst->dev swaps done from RCU callbacks?
The synchronize_net() in netdev_run_todo() only covers dst_dev_put() calls that finish before it starts. The commit in the Fixes tag also added dst_dev_put() to rt_fibinfo_free_cpus() in net/ipv4/fib_semantics.c, which works on per-CPU cached output routes. Those routes are not on rt_uncached_list, so rt_flush_dev() never touches them.
For routes that use nexthop objects (ip nexthop add id N dev X; ip route add ... nhid N), it looks like the teardown can be pushed past that grace period:
unregister (RTNL held)
nexthop_flush_dev()->remove_nexthop()->nexthop_put()
/* not the last ref, fi->nh holds one from fib_create_info() */
fib_flush()->fib_release_info()->fib_info_put()->free_fib_info()
call_rcu_hurry(free_fib_info_rcu) netdev_run_todo()
rcu_barrier()
free_fib_info_rcu()
nexthop_put(fi->nh)
call_rcu_hurry(&nh->rcu, nexthop_free_rcu) /* chained */
rt_flush_dev(NULL)
rt6_uncached_list_flush_dev(NULL)
synchronize_net() later, from the chained callback:
nexthop_free_rcu()->nexthop_free_single()->fib_nh_release()
->fib_nh_common_release()->rt_fibinfo_free_cpus()
->dst_dev_put()
rcu_assign_pointer(dst->dev_rcu, blackhole_netdev);
netdev_ref_replace(dev, blackhole_netdev, ...);rcu_barrier() does not wait for callbacks queued by the callbacks it waited for. That means nexthop_free_rcu() can run during or after the new synchronize_net().
After that, netdev_wait_allrefs_any() hands the device back as soon as netdev_refcnt_read(dev) == 1. This can happen on its first check or right after an rcu_barrier(), and neither of those implies a grace period after the dst_dev_put() above. netdev_run_todo() then frees the device.
Here is the sequence I have in mind. A udp_sendmsg() caller using MSG_CONFIRM|MSG_PROBE holds that per-CPU route. It enters rcu_read_lock() here after the synchronize_net() has started. It loads the old device through dst_dev_rcu() just before the swap. It is still inside the read-side section when the device is freed.
Wouldn't __ipv4_confirm_neigh()->__ipv4_neigh_lookup_noref() then read dev->flags from the freed net_device?
The same window seems to exist when a switchdev or offload driver (mlxsw, prestera, rocker, mlx5 lag, netdevsim) holds a fib_info reference with fib_info_hold() and drops it from a work item after netdev_run_todo() has gone past its rcu_barrier() and synchronize_net().
Is another grace period needed between the last dst_dev_put() reference drop and the free? One option would be a synchronize_net() after netdev_wait_allrefs_any() sees the refcount reach 1.
Quoted 3 lines
net/ipv4/route.c +1
-0+if (rt->rt_gw_family ==AF_INET ) { pkey = (const __be32 *)&rt->rt_gw4;
[ ... ]
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1795754281.git.n0ps1edzz%40gmail.com
PATCH v2 3/4 ipv6: route: fix use-after-free in ip6_confirm_neigh()
In reply to Zhou Qiuyuedst_confirm_neigh() is reachable from paths that do not hold an RCU read lock, including rawv6_sendmsg() with MSG_CONFIRM and MSG_PROBE.
ip6_confirm_neigh() loads the egress device with dst_dev(). Concurrent route teardown can replace dst->dev with blackhole_netdev and drop the route's reference to the old net_device. The old device can then be freed before ip6_confirm_neigh() finishes using the pointer.
An AI-assisted source audit identified the unsafe load. A delay inserted after it to widen the race window reproduced the issue on v7.4-rc3 with KASAN:
BUG: KASAN: slab-use-after-free in ip6_confirm_neigh+0x6a7/0xbd0 Read of size 4 ... by task confirm_race/130
Hold the RCU read lock across the callback and use dst_dev_rcu() to load the device. The grace period added to netdev_run_todo() ensures that the device remains alive until the read-side critical section finishes.
--- net/ipv6/route.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
net/ipv6/route.c +9 -4
@@ -257,16 +257,21 @@ static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst,static void ip6_confirm_neigh (const struct dst_entry *dst,const void *daddr) {const struct rt6_info *rt =dst_rt6_info (dst); -struct net_device *dev =dst_dev (dst); +struct net_device *dev; + +rcu_read_lock (); + dev =dst_dev_rcu (dst); daddr =choose_neigh_daddr (rt6_nexthop (rt, &in6addr_any),NULL , daddr);if (!daddr) -return ; + goto out;if (dev->flags & (IFF_NOARP |IFF_LOOPBACK )) -return ; + goto out;if (ipv6_addr_is_multicast ((const struct in6_addr *)daddr)) -return ; + goto out;__ipv6_confirm_neigh (dev, daddr); +out: +rcu_read_unlock (); }static struct dst_ops ip6_dst_ops_template = {
Signature · 1 line
2.56.1.windows.1
PATCH v2 4/4 ipv6: route: fix use-after-free in ip6_dst_neigh_lookup()
In reply to Zhou Qiuyueip6_dst_neigh_lookup() can be called without an RCU read lock. One such caller is irdma_addr_resolve_neigh_ipv6(), after its route lookup.
The callback loads the egress device with dst_dev() and passes the pointer to __ip6_dst_neigh_lookup(). On a neighbour-cache miss, that helper calls ipv6_neigh_create(), which dereferences the device and can invoke its operations. Concurrent route teardown can replace dst->dev with blackhole_netdev, drop the route's reference to the old net_device, and free it while the lookup is still using the pointer.
Hold the RCU read lock across the lookup and use dst_dev_rcu() to load the device, matching the protection in ipv4_dst_neigh_lookup().
Ido Schimmel identified this unprotected callback during review. The lifetime and non-RCU caller were validated by source inspection; this path has not been triggered independently at runtime.
--- net/ipv6/route.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
net/ipv6/route.c +9 -2
@@ -249,9 +249,16 @@ static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, const void *daddr) {const struct rt6_info *rt =dst_rt6_info (dst); +struct net_device *dev; +struct neighbour *n; + +rcu_read_lock (); + dev =dst_dev_rcu (dst); + n =__ip6_dst_neigh_lookup (rt6_nexthop (rt, &in6addr_any), + dev, skb, daddr); +rcu_read_unlock (); -return __ip6_dst_neigh_lookup (rt6_nexthop (rt, &in6addr_any), -dst_dev (dst), skb, daddr); +return n; }static void ip6_confirm_neigh (const struct dst_entry *dst,const void *daddr)
Signature · 1 line
2.56.1.windows.1