Skip to contentSkip to conversation

ipv6: route: fix use-after-free in ip6_confirm_neigh()

PATCH New Zhou Qiuyue 3 participants 3 messages Nov 23 – Nov 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ipv6: route: fix use-after-free in ip6_confirm_neigh()

ip6_confirm_neigh() loads dst->dev with dst_dev() and dereferences the resulting net_device without holding RCU. The load is only a READ_ONCE(); it does not pin the device. rawv6_sendmsg() and udpv6_sendmsg() reach dst_confirm_neigh() when sendmsg() is issued with MSG_CONFIRM and MSG_PROBE, without holding RCU or a net_device reference.

Route teardown drops the reference the dst held on the device:
dst_dev_put() does it for dsts removed from the fib tree, and the uncached-route flush does it through netdev_ref_replace(). 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 while testing dev->flags.

Take rcu_read_lock(), load the device with dst_dev_rcu(), and keep the section through the flags test and __ipv6_confirm_neigh().

KASAN reports (trimmed to the relevant frames):

 BUG: KASAN: slab-use-after-free in ip6_confirm_neigh+0x802/0x8c0
 Read of size 4 at addr ffff888104f960b0 by task confirm_race/121

 Call Trace:
  <TASK>
  dump_stack_lvl+0x55/0x70
  print_report+0xcb/0x5d0
  kasan_report+0xb8/0xf0
  ip6_confirm_neigh+0x802/0x8c0
  rawv6_sendmsg+0x116b/0x3780
  ____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 176 bytes inside of
freed 4096-byte region [ffff888104f96000, ffff888104f97000)

The faulting address is object offset 176, the 4-byte dev->flags load.

The lifetime bug comes from the v4.13 series that removed the dst gc list. Commit 9514528d92d4 ("ipv6: 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 9514528d92d4 ("ipv6: call dst_dev_put() properly")

---
Window amplification for the KASAN run only:

struct net_device *dev = dst_dev(dst);
+if (in_task())
+ msleep(500);

daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);

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

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

Object detail from the report:

 The buggy address belongs to the object at ffff888104f96000
  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/ipv6/route.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
net/ipv6/route.c +9 -4
index 1b2dcc0..6bdd442 100644
@@ -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 = {

base-commit: af6737b9066832a2017c281159950ba92385ce00

Signature · 1 line
2.34.1
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#2 lore raw
In reply to Zhou Qiuyue
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 (2) · 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 RCU protection this patch adds to ip6_confirm_neigh() does not cover the uncached-route flush, which is one of the two teardown…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the IPv4 sibling callback ipv4_confirm_neigh() (net/ipv4/route.c:441-457) has…
- [High] This problem wasn't introduced by this patch, but ip6_dst_neigh_lookup(), the IPv6 dst_ops->neigh_lookup callback in the…

--

ipv6: route: fix use-after-free in ip6_confirm_neigh()

This commit takes rcu_read_lock() in ip6_confirm_neigh(), loads the device with dst_dev_rcu() and holds the section through the dev->flags test and __ipv6_confirm_neigh(). It is meant to stop a use-after-free when route teardown swaps dst->dev to blackhole_netdev and the old net_device is freed. That race is reached from rawv6_sendmsg() and udpv6_sendmsg() with MSG_CONFIRM|MSG_PROBE.

net/ipv6/route.c static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, · line 263

Quoted 13 lines
net/ipv6/route.c +4 -1
index 1b2dcc066e198..6bdd4423ce784 100644
@@ -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);

[Severity: High]
Does this RCU section cover the uncached-route flush that the commit message names as one of the two teardown paths?

Since commit f12d2907992db7 ("ipv6: Batch rt6_uncached_list_flush_dev() in netdev_run_todo()."), rt6_uncached_list_flush_dev(dev) returns early when dev->dismantle is set. During unregister, the pointer swap now happens from netdev_run_todo(), after that function's only rcu_barrier():

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)) {
		...
		dev = netdev_wait_allrefs_any(&list);
		...
		kobject_put(&dev->dev.kobj);
    ...
}

rt6_uncached_list_flush_dev(NULL) does:

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

netdev_wait_allrefs_any() returns as soon as netdev_refcnt_read(dev) == 1. kobject_put() then reaches netdev_release(), which frees the device right away:

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

free_netdev() only reaches synchronize_net() through netdev_napi_exit() when the device has NAPI instances. Dummy devices have none.

As far as I can tell, nothing between the swap and the free waits for current RCU readers. Can a task that loaded the old device here with dst_dev_rcu() just before the swap still read dev->flags, or dev_net(dev) in __ipv6_neigh_lookup_noref(), after the device has been freed?

User space can get such a dst. rawv6_sendmsg() sets FLOWI_FLAG_KNOWN_NH for hdrincl sockets, and ip6_pol_route() then puts the route on rt6_uncached_list. MSG_CONFIRM|MSG_PROBE then reaches dst_confirm_neigh(). This is the same rawv6_sendmsg() path as in the KASAN report.

The dst_dev_put() path looks covered, because the NETDEV_UNREGISTER notifiers are followed by synchronize_net() in unregister_netdevice_many_notify().

Would netdev_run_todo() need a synchronize_net() or synchronize_rcu() after the rt_flush_dev(NULL)/rt6_uncached_list_flush_dev(NULL) batch, before the device can be freed, to close the uncached-flush case?

Quoted 5 lines
net/ipv6/route.c +1 -1
 	daddr = choose_neigh_daddr(rt6_nexthop(rt, &in6addr_any), NULL, daddr);
 	if (!daddr)
-		return;
+		goto out;

net/ipv6/route.c static struct neighbour *ip6_dst_neigh_lookup(const struct dst_entry *dst, · line 275

Quoted 10 lines
net/ipv6/route.c +4 -2
 	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();
 }

[Severity: High]
This isn't a bug introduced by this patch, but does the IPv4 callback ipv4_confirm_neigh() in net/ipv4/route.c have the same use-after-free?

net/ipv4/route.c:ipv4_confirm_neigh() {
	const struct rtable *rt = container_of(dst, struct rtable, dst);
	struct net_device *dev = dst_dev(dst);
	...
	} else if (IS_ENABLED(CONFIG_IPV6) && rt->rt_gw_family == AF_INET6) {
		return __ipv6_confirm_neigh(dev, &rt->rt_gw6);
	...
	__ipv4_confirm_neigh(dev, *(__force u32 *)pkey);
}

The device is loaded with a plain dst_dev() outside RCU. __ipv4_confirm_neigh() and __ipv6_confirm_neigh() only take rcu_read_lock() after that load. They then dereference the pointer:
dev_net(dev) and dev->flags in __ipv4_neigh_lookup_noref(), or dev_net(dev) in __ipv6_neigh_lookup_noref().

IPv4 teardown drops the dst's device reference in the same way. rt_flush_dev() does rcu_assign_pointer(rt->dst.dev_rcu, blackhole_netdev) and then netdev_ref_replace(). During unregister this runs from netdev_run_todo() through rt_flush_dev(NULL).

udp_sendmsg(), raw_sendmsg() and ping_v4_sendmsg() all reach dst_confirm_neigh() with MSG_CONFIRM|MSG_PROBE. None of them holds RCU or a device reference, and the UDP path needs no privilege.

The neighbouring ipv4_dst_neigh_lookup() already uses rcu_read_lock() with dst_dev_rcu(). Should ipv4_confirm_neigh() be converted the same way? It would also depend on the missing grace period in netdev_run_todo() described above.

[Severity: High]
This is a pre-existing issue, but ip6_dst_neigh_lookup(), just above this hunk, still uses the unpinned load that this patch removes from ip6_confirm_neigh():

net/ipv6/route.c:ip6_dst_neigh_lookup() {
	const struct rt6_info *rt = dst_rt6_info(dst);

	return __ip6_dst_neigh_lookup(rt6_nexthop(rt, &in6addr_any),
				      dst_dev(dst), skb, daddr);
}

__ip6_dst_neigh_lookup() calls __ipv6_neigh_lookup(dev, daddr), which only takes rcu_read_lock() after the pointer was loaded. On a cache miss it calls ipv6_neigh_create(dev, daddr), which leads to ___neigh_create():

net/core/neighbour.c:___neigh_create() {
    ...
	n->dev = dev;
	netdev_hold(dev, &n->dev_tracker, GFP_ATOMIC);
    ...
}

After that it calls dev->netdev_ops->ndo_neigh_construct.

Suppose dst_dev_put() or the uncached-route flush moves the dst to blackhole_netdev, and the old device is freed between the load and these uses. Could netdev_hold() then increment a refcount in a freed net_device?

Some callers get here holding only a dst reference. For example, irdma_addr_resolve_neigh_ipv6() in drivers/infiniband/hw/irdma/cm.c calls dst_neigh_lookup() after ip6_route_output() without rcu_read_lock().

The IPv4 counterpart ipv4_dst_neigh_lookup() already holds rcu_read_lock() around dst_dev_rcu() and the lookup/create.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123133000.1.n0ps1edzz%40gmail.com