Skip to contentSkip to conversation

ipv4: Fix device use-after-free in ip_mc_output()

PATCH In review Chengfeng Ye 4 participants 4 messages Sep 28 – Sep 29

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

PATCH v2 1/1 ipv4: Fix device use-after-free in ip_mc_output()

ip_mc_output() reads rt->dst.dev and stores it in skb->dev without RCU protection. dst_dev_put() can concurrently replace the destination device and release the old device after an RCU grace period, leaving the multicast output path with a stale skb->dev.

The stale device can be dereferenced by the post-routing path. In particular, a socket using IP_PMTUDISC_PROBE reaches ip_skb_dst_mtu() from ip_finish_output() and reads the freed device's MTU.

KASAN reported:

  BUG: KASAN: slab-use-after-free in ip_skb_dst_mtu+0x634/0x740
  Read of size 4 at addr ffff88810921c038 by task poc/98
  Call Trace:
   ip_skb_dst_mtu+0x634/0x740
   __ip_finish_output.part.0+0x22/0x2c0
   ip_mc_output+0x287/0x930
   ip_send_skb+0x11d/0x150
   udp_send_skb+0x63e/0xdf0
   udp_sendmsg+0x1235/0x1da0
   __sys_sendto+0x32c/0x3a0

  Freed by task 99:
   kfree+0x131/0x3c0
   device_release+0xc8/0x240
   kobject_put+0x14d/0x280
   netdev_run_todo+0x4cb/0xc70
   rtnl_dellink+0x362/0xa90

Protect the whole multicast output path with an RCU read-side critical section, as ip_output() already does. Load the destination device through skb_dst_dev_rcu() and keep it protected through the post-routing hooks and ip_finish_output().

Fixes 4a6ce2b6f2ec ("net: introduce a new function dst_dev_put()")

---
Changes in v2:
- Protect the whole ip_mc_output() path instead of only the MTU read.
- Load the destination device with skb_dst_dev_rcu(), matching ip_output().
- Use the commit that made dst device replacement RCU-protected as Fixes.

 net/ipv4/ip_output.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
net/ipv4/ip_output.c +13 -6
index a24cc8ee11d3..cf44597896cd 100644
@@ -367,8 +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.
 	 */
@@ -406,7 +409,8 @@ int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 
 		if (ip_hdr(skb)->ttl == 0) {
 			kfree_skb(skb);
-			return 0;
+			ret = 0;
+			goto out;
 		}
 	}
 
@@ -418,10 +422,13 @@ 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));
+out:
+	rcu_read_unlock();
+	return ret;
 }
 
 int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 29 lines · Chengfeng Ye

ip_mc_output() reads rt->dst.dev and stores it in skb->dev without RCU protection. dst_dev_put() can concurrently replace the destination device and release the old device after an RCU grace period, leaving the multicast output path with a stale skb->dev.

The stale device can be dereferenced by the post-routing path. In particular, a socket using IP_PMTUDISC_PROBE reaches ip_skb_dst_mtu() from ip_finish_output() and reads the freed device's MTU.

KASAN reported:

  BUG: KASAN: slab-use-after-free in ip_skb_dst_mtu+0x634/0x740
  Read of size 4 at addr ffff88810921c038 by task poc/98
  Call Trace:
   ip_skb_dst_mtu+0x634/0x740
   __ip_finish_output.part.0+0x22/0x2c0
   ip_mc_output+0x287/0x930
   ip_send_skb+0x11d/0x150
   udp_send_skb+0x63e/0xdf0
   udp_sendmsg+0x1235/0x1da0
   __sys_sendto+0x32c/0x3a0

  Freed by task 99:
   kfree+0x131/0x3c0
   device_release+0xc8/0x240
   kobject_put+0x14d/0x280
   netdev_run_todo+0x4cb/0xc70
   rtnl_dellink+0x362/0xa90
Quoted 8 lines

Protect the whole multicast output path with an RCU read-side critical section, as ip_output() already does. Load the destination device through skb_dst_dev_rcu() and keep it protected through the post-routing hooks and ip_finish_output().

Fixes 4a6ce2b6f2ec ("net: introduce a new function dst_dev_put()")
Quoted 58 lines

---
Changes in v2:
- Protect the whole ip_mc_output() path instead of only the MTU read.
- Load the destination device with skb_dst_dev_rcu(), matching ip_output().
- Use the commit that made dst device replacement RCU-protected as Fixes.

 net/ipv4/ip_output.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
net/ipv4/ip_output.c +13 -6
index a24cc8ee11d3..cf44597896cd 100644
@@ -367,8 +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.
 	 */
@@ -406,7 +409,8 @@ int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 
 		if (ip_hdr(skb)->ttl == 0) {
 			kfree_skb(skb);
-			return 0;
+			ret = 0;
+			goto out;
 		}
 	}
 
@@ -418,10 +422,13 @@ 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));
+out:
+	rcu_read_unlock();
+	return ret;
 }
 
 int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Signature · 1 line
2.43.0
#2 lore raw
In reply to Chengfeng Ye
Quoted 1 line · Chengfeng Ye

ip_mc_output() reads rt->dst.dev and stores it in skb->dev without RCU

Could you take a look at this thread ?

https://lore.kernel.org/netdev/ahveCu-zzFlpeVut@v4bel/

It seems there are still a lot of similar paths to be considered.

Quoted 92 lines

protection. dst_dev_put() can concurrently replace the destination device and release the old device after an RCU grace period, leaving the multicast output path with a stale skb->dev.

The stale device can be dereferenced by the post-routing path. In particular, a socket using IP_PMTUDISC_PROBE reaches ip_skb_dst_mtu() from ip_finish_output() and reads the freed device's MTU.

KASAN reported:

   BUG: KASAN: slab-use-after-free in ip_skb_dst_mtu+0x634/0x740
   Read of size 4 at addr ffff88810921c038 by task poc/98
   Call Trace:
    ip_skb_dst_mtu+0x634/0x740
    __ip_finish_output.part.0+0x22/0x2c0
    ip_mc_output+0x287/0x930
    ip_send_skb+0x11d/0x150
    udp_send_skb+0x63e/0xdf0
    udp_sendmsg+0x1235/0x1da0
    __sys_sendto+0x32c/0x3a0

   Freed by task 99:
    kfree+0x131/0x3c0
    device_release+0xc8/0x240
    kobject_put+0x14d/0x280
    netdev_run_todo+0x4cb/0xc70
    rtnl_dellink+0x362/0xa90

Protect the whole multicast output path with an RCU read-side critical section, as ip_output() already does. Load the destination device through skb_dst_dev_rcu() and keep it protected through the post-routing hooks and ip_finish_output().

Fixes 4a6ce2b6f2ec ("net: introduce a new function dst_dev_put()")

---
Changes in v2:
- Protect the whole ip_mc_output() path instead of only the MTU read.
- Load the destination device with skb_dst_dev_rcu(), matching ip_output().
- Use the commit that made dst device replacement RCU-protected as Fixes.

  net/ipv4/ip_output.c | 19 +++++++++++++------
  1 file changed, 13 insertions(+), 6 deletions(-)
net/ipv4/ip_output.c +13 -6
index a24cc8ee11d3..cf44597896cd 100644
@@ -367,8 +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.
  	 */
@@ -406,7 +409,8 @@ int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 
  		if (ip_hdr(skb)->ttl == 0) {
  			kfree_skb(skb);
-			return 0;
+			ret = 0;
+			goto out;
  		}
  	}
 
@@ -418,10 +422,13 @@ 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));
+out:
+	rcu_read_unlock();
+	return ret;
  }
 
  int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb)
maintainer
#3 lore raw
In reply to Chengfeng Ye
Quoted 2 lines · Chengfeng Ye

ip_mc_output() reads rt->dst.dev and stores it in skb->dev without RCU protection. dst_dev_put() can concurrently replace the destination device

It can also be rt_flush_dev(), if the dst entry is uncached.

Quoted 1 line

and release the old device after an RCU grace period, leaving the multicast

The reference release is immediate, only the free happens after a GP.

Quoted 5 lines

output path with a stale skb->dev.

The stale device can be dereferenced by the post-routing path. In particular, a socket using IP_PMTUDISC_PROBE reaches ip_skb_dst_mtu() from ip_finish_output() and reads the freed device's MTU.

ip_skb_dst_mtu() reads the device from the dst entry, not from skb->dev.

Quoted 35 lines

KASAN reported:

  BUG: KASAN: slab-use-after-free in ip_skb_dst_mtu+0x634/0x740
  Read of size 4 at addr ffff88810921c038 by task poc/98
  Call Trace:
   ip_skb_dst_mtu+0x634/0x740
   __ip_finish_output.part.0+0x22/0x2c0
   ip_mc_output+0x287/0x930
   ip_send_skb+0x11d/0x150
   udp_send_skb+0x63e/0xdf0
   udp_sendmsg+0x1235/0x1da0
   __sys_sendto+0x32c/0x3a0

  Freed by task 99:
   kfree+0x131/0x3c0
   device_release+0xc8/0x240
   kobject_put+0x14d/0x280
   netdev_run_todo+0x4cb/0xc70
   rtnl_dellink+0x362/0xa90

Protect the whole multicast output path with an RCU read-side critical section, as ip_output() already does. Load the destination device through skb_dst_dev_rcu() and keep it protected through the post-routing hooks and ip_finish_output().

Fixes 4a6ce2b6f2ec ("net: introduce a new function dst_dev_put()")

---
Changes in v2:
- Protect the whole ip_mc_output() path instead of only the MTU read.
- Load the destination device with skb_dst_dev_rcu(), matching ip_output().
- Use the commit that made dst device replacement RCU-protected as Fixes.

Quoted 4 lines
 net/ipv4/ip_output.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

The diff itself looks fine.

FTR, I'm aware of the discussion that wants an RCU critical section in ip_local_out(), but:

1. Adding one in ip_mc_output() is consistent with other output
functions: ip_output(), ip6_output(), ip_mr_output(), ip6_mr_output(), etc.

2. This patch doesn't affect the fast path, unlike an RCU critical section in ip_local_out() [1].

3. Patching ip_local_out() doesn't cover all the process context callers of dst_output() either.

And yes, I'm aware that more places suffer from this "problem". I opened an internal ticket after seeing [2]. I will get to it eventually and will target it at net-next since these UAFs are not very realistic as evident from [2].

[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/netdev/[email protected]/