Skip to contentSkip to conversation

dst_cache: fix possible races

PATCH Applied Eric Dumazet 3 participants 8 messages May 31, 2024 – Jun 4, 2024

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

cover dst_cache: fix possible races

This series is inspired by various undisclosed syzbot reports hinting at corruptions in dst_cache structures.

It seems at least four users of dst_cache are racy against BH reentrancy.

Last patch is adding a DEBUG_NET check to catch future misuses.

Eric Dumazet (5):
ipv6: ioam: block BH from ioam6_output()
net: ipv6: rpl_iptunnel: block BH in rpl_output() and rpl_input()
ipv6: sr: block BH in seg6_output_core() and seg6_input_core()
ila: block BH in ila_output()
net: dst_cache: add two DEBUG_NET warnings

 net/core/dst_cache.c      |  2 ++
 net/ipv6/ila/ila_lwt.c    |  7 ++++++-
 net/ipv6/ioam6_iptunnel.c |  8 ++++----
 net/ipv6/rpl_iptunnel.c   | 14 ++++++--------
 net/ipv6/seg6_iptunnel.c  | 14 ++++++--------
 5 files changed, 24 insertions(+), 21 deletions(-)
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
#1 lore raw

PATCH 1/5 ipv6: ioam: block BH from ioam6_output()

As explained in commit 1378817486d6 ("tipc: block BH before using dst_cache"), net/core/dst_cache.c helpers need to be called with BH disabled.

Disabling preemption in ioam6_output() is not good enough, because ioam6_output() is called from process context, lwtunnel_output() only uses rcu_read_lock().

We might be interrupted by a softirq, re-enter ioam6_output() and corrupt dst_cache data structures.

Fix the race by using local_bh_disable() instead of preempt_disable().

Fixes 8cb3bf8bff3c ("ipv6: ioam: Add support for the ip6ip6 encapsulation")
Cc Justin Iurman <[email protected]>
---
 net/ipv6/ioam6_iptunnel.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/ipv6/ioam6_iptunnel.c +4 -4
index 7563f8c6aa87cf9f7841ee78dcea2a16f60ac344..bf7120ecea1ebe834e70073710be0c1692d7ad1d 100644
@@ -351,9 +351,9 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 		goto drop;
 
 	if (!ipv6_addr_equal(&orig_daddr, &ipv6_hdr(skb)->daddr)) {
-		preempt_disable();
+		local_bh_disable();
 		dst = dst_cache_get(&ilwt->cache);
-		preempt_enable();
+		local_bh_enable();
 
 		if (unlikely(!dst)) {
 			struct ipv6hdr *hdr = ipv6_hdr(skb);
@@ -373,9 +373,9 @@ static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 				goto drop;
 			}
 
-			preempt_disable();
+			local_bh_disable();
 			dst_cache_set_ip6(&ilwt->cache, dst, &fl6.saddr);
-			preempt_enable();
+			local_bh_enable();
 		}
 
 		skb_dst_drop(skb);
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
#2 lore raw

PATCH 2/5 net: ipv6: rpl_iptunnel: block BH in rpl_output() and rpl_input()

In reply to Eric Dumazet

As explained in commit 1378817486d6 ("tipc: block BH before using dst_cache"), net/core/dst_cache.c helpers need to be called with BH disabled.

Disabling preemption in rpl_output() is not good enough, because rpl_output() is called from process context, lwtunnel_output() only uses rcu_read_lock().

We might be interrupted by a softirq, re-enter rpl_output() and corrupt dst_cache data structures.

Fix the race by using local_bh_disable() instead of preempt_disable().

Apply a similar change in rpl_input().

Cc Alexander Aring <[email protected]>
---
 net/ipv6/rpl_iptunnel.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
net/ipv6/rpl_iptunnel.c +6 -8
index a013b92cbb860aa36a23f50d3d5c5963857d601c..2c83b7586422ddd2ae877f98e47698410e47b233 100644
@@ -212,9 +212,9 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 	if (unlikely(err))
 		goto drop;
 
-	preempt_disable();
+	local_bh_disable();
 	dst = dst_cache_get(&rlwt->cache);
-	preempt_enable();
+	local_bh_enable();
 
 	if (unlikely(!dst)) {
 		struct ipv6hdr *hdr = ipv6_hdr(skb);
@@ -234,9 +234,9 @@ static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 			goto drop;
 		}
 
-		preempt_disable();
+		local_bh_disable();
 		dst_cache_set_ip6(&rlwt->cache, dst, &fl6.saddr);
-		preempt_enable();
+		local_bh_enable();
 	}
 
 	skb_dst_drop(skb);
@@ -268,23 +268,21 @@ static int rpl_input(struct sk_buff *skb)
 		return err;
 	}
 
-	preempt_disable();
+	local_bh_disable();
 	dst = dst_cache_get(&rlwt->cache);
-	preempt_enable();
 
 	if (!dst) {
 		ip6_route_input(skb);
 		dst = skb_dst(skb);
 		if (!dst->error) {
-			preempt_disable();
 			dst_cache_set_ip6(&rlwt->cache, dst,
 					  &ipv6_hdr(skb)->saddr);
-			preempt_enable();
 		}
 	} else {
 		skb_dst_drop(skb);
 		skb_dst_set(skb, dst);
 	}
+	local_bh_enable();
 
 	err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
 	if (unlikely(err))
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
#3 lore raw

PATCH 3/5 ipv6: sr: block BH in seg6_output_core() and seg6_input_core()

In reply to Eric Dumazet

As explained in commit 1378817486d6 ("tipc: block BH before using dst_cache"), net/core/dst_cache.c helpers need to be called with BH disabled.

Disabling preemption in seg6_output_core() is not good enough, because seg6_output_core() is called from process context, lwtunnel_output() only uses rcu_read_lock().

We might be interrupted by a softirq, re-enter seg6_output_core() and corrupt dst_cache data structures.

Fix the race by using local_bh_disable() instead of preempt_disable().

Apply a similar change in seg6_input_core().

Fixes fa79581ea66c ("ipv6: sr: fix several BUGs when preemption is enabled")
Fixes 6c8702c60b88 ("ipv6: sr: add support for SRH encapsulation and injection with lwtunnels")
Cc David Lebrun <[email protected]>
---
 net/ipv6/seg6_iptunnel.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
net/ipv6/seg6_iptunnel.c +6 -8
index a75df2ec8db0d369a4e3481576fc09f511a4dd36..098632adc9b5afa69e4b65439ee54c3fc0a8d668 100644
@@ -464,23 +464,21 @@ static int seg6_input_core(struct net *net, struct sock *sk,
 
 	slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
 
-	preempt_disable();
+	local_bh_disable();
 	dst = dst_cache_get(&slwt->cache);
-	preempt_enable();
 
 	if (!dst) {
 		ip6_route_input(skb);
 		dst = skb_dst(skb);
 		if (!dst->error) {
-			preempt_disable();
 			dst_cache_set_ip6(&slwt->cache, dst,
 					  &ipv6_hdr(skb)->saddr);
-			preempt_enable();
 		}
 	} else {
 		skb_dst_drop(skb);
 		skb_dst_set(skb, dst);
 	}
+	local_bh_enable();
 
 	err = skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev));
 	if (unlikely(err))
@@ -536,9 +534,9 @@ static int seg6_output_core(struct net *net, struct sock *sk,
 
 	slwt = seg6_lwt_lwtunnel(orig_dst->lwtstate);
 
-	preempt_disable();
+	local_bh_disable();
 	dst = dst_cache_get(&slwt->cache);
-	preempt_enable();
+	local_bh_enable();
 
 	if (unlikely(!dst)) {
 		struct ipv6hdr *hdr = ipv6_hdr(skb);
@@ -558,9 +556,9 @@ static int seg6_output_core(struct net *net, struct sock *sk,
 			goto drop;
 		}
 
-		preempt_disable();
+		local_bh_disable();
 		dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr);
-		preempt_enable();
+		local_bh_enable();
 	}
 
 	skb_dst_drop(skb);
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
#4 lore raw

PATCH 4/5 ila: block BH in ila_output()

In reply to Eric Dumazet

As explained in commit 1378817486d6 ("tipc: block BH before using dst_cache"), net/core/dst_cache.c helpers need to be called with BH disabled.

ila_output() is called from lwtunnel_output() possibly from process context, and under rcu_read_lock().

We might be interrupted by a softirq, re-enter ila_output() and corrupt dst_cache data structures.

Fix the race by using local_bh_disable().

---
 net/ipv6/ila/ila_lwt.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
net/ipv6/ila/ila_lwt.c +6 -1
index 0601bad798221389fe83318fbb17f192cec880d4..ff7e734e335b06f03c4c8815163a706136e42a3e 100644
@@ -58,7 +58,9 @@ static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 		return orig_dst->lwtstate->orig_output(net, sk, skb);
 	}
 
+	local_bh_disable();
 	dst = dst_cache_get(&ilwt->dst_cache);
+	local_bh_enable();
 	if (unlikely(!dst)) {
 		struct ipv6hdr *ip6h = ipv6_hdr(skb);
 		struct flowi6 fl6;
@@ -86,8 +88,11 @@ static int ila_output(struct net *net, struct sock *sk, struct sk_buff *skb)
 			goto drop;
 		}
 
-		if (ilwt->connected)
+		if (ilwt->connected) {
+			local_bh_disable();
 			dst_cache_set_ip6(&ilwt->dst_cache, dst, &fl6.saddr);
+			local_bh_enable();
+		}
 	}
 
 	skb_dst_set(skb, dst);
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
#5 lore raw

PATCH 5/5 net: dst_cache: add two DEBUG_NET warnings

In reply to Eric Dumazet

After fixing four different bugs involving dst_cache users, it might be worth adding a check about BH being blocked by dst_cache callers.

DEBUG_NET_WARN_ON_ONCE(!in_softirq());

It is not fatal, if we missed valid case where no BH deadlock is to be feared, we might change this.

---
 net/core/dst_cache.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/dst_cache.c +2 -0
index 6a0482e676d379f1f9bffdda51c7535243b3ec38..70c634b9e7b02300188582a1634d5977838db132 100644
@@ -27,6 +27,7 @@ struct dst_cache_pcpu {
 static void dst_cache_per_cpu_dst_set(struct dst_cache_pcpu *dst_cache,
 				      struct dst_entry *dst, u32 cookie)
 {
+	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
 	dst_release(dst_cache->dst);
 	if (dst)
 		dst_hold(dst);
@@ -40,6 +41,7 @@ static struct dst_entry *dst_cache_per_cpu_get(struct dst_cache *dst_cache,
 {
 	struct dst_entry *dst;
 
+	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
 	dst = idst->dst;
 	if (!dst)
 		goto fail;
Signature · 1 line
2.45.1.288.g0e0cd299f1-goog
maintainer Acked-by on the v1 series
#6 lore raw
In reply to Eric Dumazet
Quoted 15 lines · Eric Dumazet

This series is inspired by various undisclosed syzbot reports hinting at corruptions in dst_cache structures.

It seems at least four users of dst_cache are racy against BH reentrancy.

Last patch is adding a DEBUG_NET check to catch future misuses.

Eric Dumazet (5):
ipv6: ioam: block BH from ioam6_output()
net: ipv6: rpl_iptunnel: block BH in rpl_output() and rpl_input()
ipv6: sr: block BH in seg6_output_core() and seg6_input_core()
ila: block BH in ila_output()
net: dst_cache: add two DEBUG_NET warnings

Quoted 7 lines
 net/core/dst_cache.c      |  2 ++
 net/ipv6/ila/ila_lwt.c    |  7 ++++++-
 net/ipv6/ioam6_iptunnel.c |  8 ++++----
 net/ipv6/rpl_iptunnel.c   | 14 ++++++--------
 net/ipv6/seg6_iptunnel.c  | 14 ++++++--------
 5 files changed, 24 insertions(+), 21 deletions(-)

LGTM, thanks!

#7 lore raw
In reply to Eric Dumazet
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Fri, 31 May 2024 13:26:31 +0000 you wrote:

This series is inspired by various undisclosed syzbot reports hinting at corruptions in dst_cache structures.

It seems at least four users of dst_cache are racy against BH reentrancy.

Last patch is adding a DEBUG_NET check to catch future misuses.

[...]

Here is the summary with links:
- [net,1/5] ipv6: ioam: block BH from ioam6_output()

    https://git.kernel.org/netdev/net/c/2fe40483ec25
  - [net,2/5] net: ipv6: rpl_iptunnel: block BH in rpl_output() and rpl_input()
    https://git.kernel.org/netdev/net/c/db0090c6eb12
  - [net,3/5] ipv6: sr: block BH in seg6_output_core() and seg6_input_core()
    https://git.kernel.org/netdev/net/c/c0b98ac1cc10
  - [net,4/5] ila: block BH in ila_output()
    https://git.kernel.org/netdev/net/c/cf28ff8e4c02
  - [net,5/5] net: dst_cache: add two DEBUG_NET warnings
    https://git.kernel.org/netdev/net/c/2fe6fb36c781

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html