ipv4: ip_output: fix use-after-free in ip_mc_output()
Subsystems (4)
PATCH 1/1 ipv4: ip_output: fix use-after-free in ip_mc_output()
ip_mc_output() loads rt->dst.dev without RCU, stores it in skb->dev, and passes that pointer to the POST_ROUTING netfilter hooks. The stale pointer is carried across the hook call, so the device can be unregistered and freed before the hook dereferences it.
When the interface is unregistered, the route is torn down and the net_device reference held by the dst is dropped. A legacy iptables hook then reads the freed dev->name through ifname_compare_aligned(). The hook is a normal netfilter configuration inside the network namespace.
Use the same pattern as ip_output(): take rcu_read_lock(), load the device with skb_dst_dev_rcu(), and hold the section until the final NF_HOOK_COND() returns. Drop the lock before freeing the skb on the TTL-zero path.
KASAN reports (trimmed to the relevant frames):
BUG: KASAN: slab-use-after-free in ipt_do_table+0xf88/0x1060 Read of size 8 at addr ffff888105348120 by task mc_race/116 Call Trace: <TASK> dump_stack_lvl+0x55/0x70 print_report+0xcb/0x5d0 kasan_report+0xb8/0xf0 ipt_do_table+0xf88/0x1060 nf_hook_slow+0xb8/0x1f0 ip_mc_output+0x71e/0x9d0 ip_send_skb+0x11e/0x150 udp_send_skb+0x637/0xd60 udp_sendmsg+0x12d5/0x1dd0 __x64_sys_sendto+0xdb/0x1b0 do_syscall_64+0xee/0x500 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK> Allocated by task 122: __kasan_kmalloc+0xaa/0xb0 __kvmalloc_node_noprof+0x216/0x5d0 alloc_netdev_mqs+0x7e/0x11c0 rtnl_create_link+0xaa6/0xe30 rtnl_newlink+0xd31/0x1e50 rtnetlink_rcv_msg+0x697/0xa00 Freed by task 122: __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 288 bytes inside of
freed 4096-byte region [ffff888105348000, ffff888105349000)
The faulting address is object offset 288, the dev->name read in ifname_compare_aligned().
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 output path 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.
---
Window amplification: none in the kernel. The KASAN report was produced without any instrumentation; the race is widened entirely from userspace:
- one veth pair veth0/veth1, veth0 10.0.0.1/24, veth1 10.0.0.2/24
- multicast route 239.0.0.0/8 via veth0
- 4 sender threads, each paired with a blocker pinned to the same CPU, both SCHED_IDLE; the blocker stops the sender inside sendto() for a random 30-4000 us
- a deleter thread at SCHED_FIFO issues RTM_DELLINK veth0, waits 45 ms, then recreates the pair
- a legacy iptables mangle table with a POST_ROUTING hook is installed via IPT_SO_SET_REPLACE; the rule reads outdev->name on every entry
Three independent boots hit. The report below is from run 3 (mc-natural-run3.log). Run 2 (mc-natural-run2.log) hit during the first delete cycle with zero successful sends (every send returned ENETUNREACH), which shows that only reaching ip_mc_output() matters.
Report context: 7.4.0-rc2 #38, CONFIG_KASAN=y, PREEMPT(full), kasan.fault=panic. Full log: evidence/dyn-mc/mc-natural-run3.log.
Object detail from the report:
The buggy address belongs to the object at ffff888105348000 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/ip_output.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
net/ipv4/ip_output.c +12 -5
@@ -367,7 +367,11 @@ static int ip_mc_finish_output(struct net *net, struct sock *sk,int ip_mc_output (struct net *net,struct sock *sk,struct sk_buff *skb) {struct rtable *rt =skb_rtable (skb); -struct net_device *dev = rt->dst.dev; +struct net_device *dev; +int ret; + +rcu_read_lock (); + dev =skb_dst_dev_rcu (skb);/* * If the indicated interface is up and running, send the packet.
@@ -405,6 +409,7 @@ int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)/* Multicasts with ttl 0 must not go beyond the host */ if (ip_hdr (skb)->ttl ==0 ) { +rcu_read_unlock ();kfree_skb (skb);return 0 ; }
@@ -418,10 +423,12 @@ int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb) ip_mc_finish_output); } -return NF_HOOK_COND (NFPROTO_IPV4 ,NF_INET_POST_ROUTING , - net, sk, skb,NULL , skb->dev, - ip_finish_output, - !(IPCB (skb)->flags &IPSKB_REROUTED )); + ret =NF_HOOK_COND (NFPROTO_IPV4 ,NF_INET_POST_ROUTING , + net, sk, skb,NULL , skb->dev, + ip_finish_output, + !(IPCB (skb)->flags &IPSKB_REROUTED )); +rcu_read_unlock (); +return ret; }int ip_output (struct net *net,struct sock *sk,struct sk_buff *skb)
base-commit: af6737b9066832a2017c281159950ba92385ce00
Signature · 1 line
2.34.1
[email protected]
AI reviewThis 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.
Please disregard this patch. It is a duplicate of pre-existing work on the same ip_mc_output() path:
https://lore.kernel.org/netdev/ahveCu-zzFlpeVut@v4bel/
https://patchwork.kernel.org/patch/14852717/
Hyunwoo Kim's v1-v3 series already covers ip_mc_output(), and Chengfeng Ye's v2 uses the same skb_dst_dev_rcu() fix. I missed both when checking for prior work before posting; there is nothing new in my patch, so please drop it.
Sorry for the noise.
Thanks,
Zhou Qiuyue