dst_cache: fix possible races
Subsystems (4)
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
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().
--- net/ipv6/ioam6_iptunnel.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/ipv6/ioam6_iptunnel.c +4 -4
@@ -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
PATCH 2/5 net: ipv6: rpl_iptunnel: block BH in rpl_output() and rpl_input()
In reply to Eric DumazetAs 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().
--- net/ipv6/rpl_iptunnel.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
net/ipv6/rpl_iptunnel.c +6 -8
@@ -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
PATCH 3/5 ipv6: sr: block BH in seg6_output_core() and seg6_input_core()
In reply to Eric DumazetAs 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().
--- net/ipv6/seg6_iptunnel.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
net/ipv6/seg6_iptunnel.c +6 -8
@@ -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
PATCH 4/5 ila: block BH in ila_output()
In reply to Eric DumazetAs 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
@@ -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
PATCH 5/5 net: dst_cache: add two DEBUG_NET warnings
In reply to Eric DumazetAfter 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
@@ -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
Quoted 15 lines · Eric Dumazet
This series is inspired by various undisclosed syzbot reports hinting at corruptions in
dst_cachestructures.It seems at least four users of
dst_cacheare racy against BH reentrancy.Last patch is adding a
DEBUG_NETcheck to catch future misuses.Eric Dumazet (5):
ipv6: ioam: block BH fromioam6_output()
net: ipv6:rpl_iptunnel: block BH inrpl_output()andrpl_input()
ipv6: sr: block BH inseg6_output_core()andseg6_input_core()
ila: block BH inila_output()
net:dst_cache: add twoDEBUG_NETwarnings
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!
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_cachestructures.It seems at least four users of
dst_cacheare racy against BH reentrancy.Last patch is adding a
DEBUG_NETcheck 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