Skip to contentSkip to conversation

netfilter: conntrack: introduce clash resolution on insertion race

PATCH Superseded Pablo Neira Ayuso 2 participants 2 messages May 3, 2016

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v4 1/1 netfilter: conntrack: introduce clash resolution on insertion race

This patch introduces nf_ct_resolve_clash() to resolve race condition on conntrack insertions.

This is particularly a problem for connection-less protocols such as UDP, with no initial handshake. Two or more packets may race to insert the entry resulting in packet drops.

Another problematic scenario are packets enqueued to userspace via NFQUEUE after the raw table, that make it easier to trigger this race.

To resolve this, the idea is to reset the conntrack entry to the one that won race. Packet and bytes counters are also merged.

The 'insert_failed' stats still accounts for this situation, after this patch, the drop counter is bumped whenever we drop packets, so we can watch for unresolved clashes.

---
v4: Explicit initialization of ret = NF_DROP for dying conntracks.

 include/net/netfilter/nf_conntrack_l4proto.h |  3 ++
 net/netfilter/nf_conntrack_core.c            | 55 ++++++++++++++++++++++++++--
 net/netfilter/nf_conntrack_proto_udp.c       |  2 +
 net/netfilter/nf_conntrack_proto_udplite.c   |  2 +
 4 files changed, 59 insertions(+), 3 deletions(-)
include/net/netfilter/nf_conntrack_l4proto.h +3 -0
index 956d8a6..1a5fb36 100644
@@ -23,6 +23,9 @@ struct nf_conntrack_l4proto {
 	/* L4 Protocol number. */
 	u_int8_t l4proto;
 
+	/* Resolve clashes on insertion races. */
+	bool allow_clash;
+
 	/* Try to fill in the third arg: dataoff is offset past network protocol
            hdr.  Return true if possible. */
 	bool (*pkt_to_tuple)(const struct sk_buff *skb, unsigned int dataoff,
net/netfilter/nf_conntrack_core.c +52 -3

82 lines not on this page · raw message

net/netfilter/nf_conntrack_proto_udp.c +2 -0
index 478f92f..4fd0405 100644
@@ -309,6 +309,7 @@ struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
 	.l3proto		= PF_INET,
 	.l4proto		= IPPROTO_UDP,
 	.name			= "udp",
+	.allow_clash		= true,
 	.pkt_to_tuple		= udp_pkt_to_tuple,
 	.invert_tuple		= udp_invert_tuple,
 	.print_tuple		= udp_print_tuple,
@@ -341,6 +342,7 @@ struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
 	.l3proto		= PF_INET6,
 	.l4proto		= IPPROTO_UDP,
 	.name			= "udp",
+	.allow_clash		= true,
 	.pkt_to_tuple		= udp_pkt_to_tuple,
 	.invert_tuple		= udp_invert_tuple,
 	.print_tuple		= udp_print_tuple,
net/netfilter/nf_conntrack_proto_udplite.c +2 -0
index 1ac8ee1..9d692f5 100644
@@ -274,6 +274,7 @@ static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite4 __read_mostly =
 	.l3proto		= PF_INET,
 	.l4proto		= IPPROTO_UDPLITE,
 	.name			= "udplite",
+	.allow_clash		= true,
 	.pkt_to_tuple		= udplite_pkt_to_tuple,
 	.invert_tuple		= udplite_invert_tuple,
 	.print_tuple		= udplite_print_tuple,
@@ -306,6 +307,7 @@ static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
 	.l3proto		= PF_INET6,
 	.l4proto		= IPPROTO_UDPLITE,
 	.name			= "udplite",
+	.allow_clash		= true,
 	.pkt_to_tuple		= udplite_pkt_to_tuple,
 	.invert_tuple		= udplite_invert_tuple,
 	.print_tuple		= udplite_print_tuple,
Signature · 1 line
2.1.4
maintainer
#1 lore raw
Quoted 14 lines · Pablo Neira Ayuso

This patch introduces nf_ct_resolve_clash() to resolve race condition on conntrack insertions.

This is particularly a problem for connection-less protocols such as UDP, with no initial handshake. Two or more packets may race to insert the entry resulting in packet drops.

Another problematic scenario are packets enqueued to userspace via NFQUEUE after the raw table, that make it easier to trigger this race.

To resolve this, the idea is to reset the conntrack entry to the one that won race. Packet and bytes counters are also merged.

Quoted 7 lines

The 'insert_failed' stats still accounts for this situation, after this patch, the drop counter is bumped whenever we drop packets, so we can watch for unresolved clashes.

---
v4: Explicit initialization of ret = NF_DROP for dying conntracks.

Looks good, thanks for working on this.