Skip to contentSkip to conversation

netfilter: tproxy: fix deadlock due to missing BH disable

PATCH Applied Florian Westphal 2 participants 2 messages Mar 3, 2023 – Mar 6, 2023

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: tproxy: fix deadlock due to missing BH disable

The xtables packet traverser performs an unconditional local_bh_disable(), but the nf_tables evaluation loop does not.

Functions that are called from either xtables or nftables must assume that they can be called in process context.

inet_twsk_deschedule_put() assumes that no softirq interrupt can occur. If tproxy is used from nf_tables its possible that we'll deadlock trying to aquire a lock already held in process context.

Add a small helper that takes care of this and use it.

Fixes 4ed8eb6570a4 ("netfilter: nf_tables: Add native tproxy support")
Reported-and-tested-by Major Dávid <[email protected]>
---
 include/net/netfilter/nf_tproxy.h   | 7 +++++++
 net/ipv4/netfilter/nf_tproxy_ipv4.c | 2 +-
 net/ipv6/netfilter/nf_tproxy_ipv6.c | 2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)
include/net/netfilter/nf_tproxy.h +7 -0
index 82d0e41b76f2..faa108b1ba67 100644
@@ -17,6 +17,13 @@ static inline bool nf_tproxy_sk_is_transparent(struct sock *sk)
 	return false;
 }
 
+static inline void nf_tproxy_twsk_deschedule_put(struct inet_timewait_sock *tw)
+{
+	local_bh_disable();
+	inet_twsk_deschedule_put(tw);
+	local_bh_enable();
+}
+
 /* assign a socket to the skb -- consumes sk */
 static inline void nf_tproxy_assign_sock(struct sk_buff *skb, struct sock *sk)
 {
net/ipv4/netfilter/nf_tproxy_ipv4.c +1 -1
index b22b2c745c76..69e331799604 100644
@@ -38,7 +38,7 @@ nf_tproxy_handle_time_wait4(struct net *net, struct sk_buff *skb,
 					    hp->source, lport ? lport : hp->dest,
 					    skb->dev, NF_TPROXY_LOOKUP_LISTENER);
 		if (sk2) {
-			inet_twsk_deschedule_put(inet_twsk(sk));
+			nf_tproxy_twsk_deschedule_put(inet_twsk(sk));
 			sk = sk2;
 		}
 	}
net/ipv6/netfilter/nf_tproxy_ipv6.c +1 -1
index 929502e51203..52f828bb5a83 100644
@@ -63,7 +63,7 @@ nf_tproxy_handle_time_wait6(struct sk_buff *skb, int tproto, int thoff,
 					    lport ? lport : hp->dest,
 					    skb->dev, NF_TPROXY_LOOKUP_LISTENER);
 		if (sk2) {
-			inet_twsk_deschedule_put(inet_twsk(sk));
+			nf_tproxy_twsk_deschedule_put(inet_twsk(sk));
 			sk = sk2;
 		}
 	}
Signature · 1 line
2.39.2
maintainer Applied
#1 lore raw
Quoted 11 lines · Florian Westphal

The xtables packet traverser performs an unconditional local_bh_disable(), but the nf_tables evaluation loop does not.

Functions that are called from either xtables or nftables must assume that they can be called in process context.

inet_twsk_deschedule_put() assumes that no softirq interrupt can occur. If tproxy is used from nf_tables its possible that we'll deadlock trying to aquire a lock already held in process context.

Add a small helper that takes care of this and use it.

Applied, thanks