The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH net] ipv4: route: fix use-after-free in ipv4_confirm_neigh()
@ 2026-11-23  5:30 Zhou Qiuyue
  2026-11-23  7:02 ` netdev-bot+sinfo
  0 siblings, 1 reply; 2+ messages in thread
From: Zhou Qiuyue @ 2026-11-23  5:30 UTC (permalink / raw)
  To: netdev
  Cc: David Ahern, Ido Schimmel, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Simon Horman, Hangbin Liu,
	linux-kernel, stable


ipv4_confirm_neigh() loads dst->dev with dst_dev() and hands the
resulting net_device to the neighbour lookup.  The load is outside any
RCU read-side section: __ipv4_confirm_neigh() does take rcu_read_lock(),
but only after the pointer has been read, which is too late to protect
it.  The load is only a READ_ONCE(); it does not pin the device.
udp_sendmsg(), raw_sendmsg() and ping_v4_sendmsg() reach
dst_confirm_neigh() when sendmsg() is issued with MSG_CONFIRM and
MSG_PROBE, without holding RCU or a net_device reference.

The first stale field read is dev_net(dev) in arp_table(), reached from
the neighbour lookup.  Route teardown drops the reference the dst held on
the device: dst_dev_put() does it for dsts removed from the fib tree, and
rt_flush_dev() does it for uncached routes.  Both replace dst->dev with
blackhole_netdev, so a reader that loaded the old pointer just before the
replacement can dereference a freed net_device.

Take rcu_read_lock(), load the device with dst_dev_rcu(), and keep the
section through the gateway dispatch and __ipv4_confirm_neigh().

KASAN reports (trimmed to the relevant frames):

 BUG: KASAN: slab-use-after-free in ipv4_confirm_neigh+0x89d/0x9c0
 Read of size 8 at addr ffff888104bea108 by task confirm_race4/125

 Call Trace:
  <TASK>
  dump_stack_lvl+0x55/0x70
  print_report+0xcb/0x5d0
  kasan_report+0xb8/0xf0
  ipv4_confirm_neigh+0x89d/0x9c0
  udp_sendmsg+0x11b9/0x1dd0
  ____sys_sendmsg+0x7af/0x9b0
  ___sys_sendmsg+0x145/0x220
  __sys_sendmsg+0xf8/0x190
  do_syscall_64+0xee/0x500
  entry_SYSCALL_64_after_hwframe+0x77/0x7f
  </TASK>

 Allocated by task 117:
  __kasan_kmalloc+0xaa/0xb0
  __kvmalloc_node_noprof+0x216/0x5d0
  alloc_netdev_mqs+0x7e/0x11c0
  rtnl_create_link+0xaa6/0xe30
  rtnl_newlink+0xd22/0x1de0
  rtnetlink_rcv_msg+0x697/0xa00

 Freed by task 117:
  __kasan_slab_free+0x61/0x80
  kfree+0x201/0x440
  device_release+0xc8/0x240
  kobject_put+0x101/0x1e0
  netdev_run_todo+0x611/0xe70
  rtnl_dellink+0x343/0xa50
  rtnetlink_rcv_msg+0x697/0xa00

 The buggy address is located 264 bytes inside of
  freed 4096-byte region [ffff888104bea000, ffff888104beb000)

The faulting address is object offset 264, the dev_net(dev) read of
dev->nd_net in arp_table().

The lifetime bug comes from the v4.13 series that removed the dst gc
list.  Commit 95c47f9cf5e0 ("ipv4: call dst_dev_put() properly") made
route teardown release the net_device reference held by the dst, so
dst->dev no longer pins the device for the lifetime of the dst.
Later commits changed the replacement target to blackhole_netdev and
added dev_rcu, but did not add RCU protection to this reader.

The lifetime change is present from v4.13 onward.  Kernels that reach
this callback are affected, including current mainline, net-next, and
the 5.4.y, 5.10.y, 5.15.y, 6.1.y, 6.6.y and 6.12.y stable branches.
dst_dev_rcu() is available in current 5.15.y and newer; 5.10.y and
older need the equivalent rcu_dereference(dst->dev) backport.

Fixes: 95c47f9cf5e0 ("ipv4: call dst_dev_put() properly")
Cc: stable@vger.kernel.org
Signed-off-by: Zhou Qiuyue <n0ps1edzz@gmail.com>
---
Window amplification for the KASAN run only:

 struct net_device *dev = dst_dev(dst);
+if (in_task())
+	msleep(2000);
 const __be32 *pkey = daddr;

The sleep runs in process context (udp_sendmsg), so RTM_DELLINK can
reach netdev_run_todo() and device_release() before dev_net(dev) is read.
It is a verification aid, not part of the fix.

Report context: 7.4.0-rc2 #27, CONFIG_KASAN=y, PREEMPT(full),
kasan.fault=panic.  Full log: evidence/dyn-confirm/qemu-kasan-confirm4.log.

Object detail from the report:

 The buggy address belongs to the object at ffff888104bea000
  which belongs to the cache kmalloc-4k of size 4096

Base tree: netdev/net.git main at af6737b90668.  The pre-image blob in
this patch matches that tip exactly.  The same hunk also applies to
netdev/net-next.git main at eb6d6c80d354; the target function is
unchanged there.

 net/ipv4/route.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index cd69242..5ceea01 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -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.

base-commit: af6737b9066832a2017c281159950ba92385ce00
-- 
2.34.1



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

end of thread, other threads:[~2026-11-23  7:02 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  5:30 [PATCH net] ipv4: route: fix use-after-free in ipv4_confirm_neigh() Zhou Qiuyue
2026-11-23  7:02 ` netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox