netfilter: conntrack: introduce clash resolution on insertion race
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH v2 3/4 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.
---
v2: drop refcount of the old conntrack entry, otherwise we leak this.
Call nf_ct_add_to_dying_list() before clash resolution.
include/net/netfilter/nf_conntrack_l4proto.h | 3 ++ net/netfilter/nf_conntrack_core.c | 46 +++++++++++++++++++++++++++- net/netfilter/nf_conntrack_proto_udp.c | 2 ++ net/netfilter/nf_conntrack_proto_udplite.c | 2 ++ 4 files changed, 52 insertions(+), 1 deletion(-)
include/net/netfilter/nf_conntrack_l4proto.h +3 -0
@@ -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 +45 -1
67 lines not on this page · raw message
net/netfilter/nf_conntrack_proto_udp.c +2 -0
@@ -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
@@ -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
Florian Westphal
maintainerQuoted 25 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.
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.Signed-off-by Pablo Neira Ayuso <[email protected]>---
v2: drop refcount of the old conntrack entry, otherwise we leak this.
Callnf_ct_add_to_dying_list()before clash resolution.net/netfilter/nf_conntrack_core.c +3
-0+/* Resolve race on insertion if this protocol allows this. */ +static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb, + struct nf_conn *old_ct,
net/netfilter/nf_conntrack_core.c static inline void nf_ct_acct_update(struct nf_conn *ct, · line 648
Quoted 10 lines
net/netfilter/nf_conntrack_core.c +10
-0+enum ip_conntrack_info ctinfo, + struct nf_conntrack_tuple_hash *h) +{ + struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); +struct nf_conntrack_l4proto *l4proto; + + l4proto =__nf_ct_l4proto_find (nf_ct_l3num (ct),nf_ct_protonum (ct)); +if (l4proto->allow_clash && + !nf_ct_is_dying (ct) && +atomic_inc_not_zero (&ct->ct_general.use)) {
I found this confusing, perhaps add small one-liner comment that *ct is in fact ct already in the table, not the one that was attached to skb->nfct (perhaps I just need more coffee, sorry).
net/netfilter/nf_conntrack_core.c static inline void nf_ct_acct_update(struct nf_conn *ct, · line 651
Quoted 3 lines
net/netfilter/nf_conntrack_core.c +3
-0+/* Don't modify skb->nfctinfo, we're at POSTROUTING so this +* packet is already leaving our framework, it is too late. +*/
Note that this might be loopback in which case this skb will reappear on PREROUTING.
net/netfilter/nf_conntrack_core.c static inline void nf_ct_acct_update(struct nf_conn *ct, · line 654
Quoted 3 lines
net/netfilter/nf_conntrack_core.c +3
-0+ skb->nfct = &ct->ct_general; +nf_ct_acct_merge (ct, ctinfo, old_ct); +nf_ct_put (old_ct);
Perhaps it would be better to not have old_ct and instead nf_conntrack_put(skb->nfct);skb->nfct = &ct->ct_general;
?
net/netfilter/nf_conntrack_core.c __nf_conntrack_confirm(struct sk_buff *skb) · line 773
Quoted 9 lines
net/netfilter/nf_conntrack_core.c +2
-0+int ret; ct =nf_ct_get (skb, &ctinfo); net =nf_ct_net (ct);@@ -727,10 +770,11 @@ __nf_conntrack_confirm(struct sk_buff *skb) out:nf_ct_add_to_dying_list (ct); + ret =nf_ct_resolve_clash (net, skb, ct, ctinfo, h);
Is this safe?
Seems we jump to out label in other cases as well, not just for clashes.
Pablo Neira Ayuso
maintainerQuoted 32 lines · Florian Westphal
Quoted 31 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.
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.Signed-off-by Pablo Neira Ayuso <[email protected]>---
v2: drop refcount of the old conntrack entry, otherwise we leak this.
Callnf_ct_add_to_dying_list()before clash resolution.net/netfilter/nf_conntrack_core.c +9
-0+/* Resolve race on insertion if this protocol allows this. */ +static int nf_ct_resolve_clash (struct net *net,struct sk_buff *skb, +struct nf_conn *old_ct, +enum ip_conntrack_info ctinfo, +struct nf_conntrack_tuple_hash *h) +{ +struct nf_conn *ct =nf_ct_tuplehash_to_ctrack (h); +struct nf_conntrack_l4proto *l4proto; +
Quoted 8 lines
net/netfilter/nf_conntrack_core.c static inline void nf_ct_acct_update(struct nf_conn *ct, · line 648
Quoted 4 lines
net/netfilter/nf_conntrack_core.c +4
-0+ l4proto =__nf_ct_l4proto_find (nf_ct_l3num (ct),nf_ct_protonum (ct)); +if (l4proto->allow_clash && + !nf_ct_is_dying (ct) && +atomic_inc_not_zero (&ct->ct_general.use)) {I found this confusing, perhaps add small one-liner comment that *ct is in fact ct already in the table, not the one that was attached to
skb->nfct(perhaps I just need more coffee, sorry).
OK, will add this.
Quoted 6 lines
net/netfilter/nf_conntrack_core.c static inline void nf_ct_acct_update(struct nf_conn *ct, · line 651
Quoted 3 lines
net/netfilter/nf_conntrack_core.c +3
-0+/* Don't modify skb->nfctinfo, we're at POSTROUTING so this +* packet is already leaving our framework, it is too late. +*/ Note that this might be loopback in which case this skb will reappear on PREROUTING.
The comment intention is that we probably already applied a filtering decision, so changing the ctinfo here seems awkward to me.
In NFQUEUE, this packet may have spent quite a bit of time so it may even get a different ctinfo if we reevaluate, but as I said, having packets changing its original ctinfo is...
Quoted 9 lines
net/netfilter/nf_conntrack_core.c static inline void nf_ct_acct_update(struct nf_conn *ct, · line 654
Quoted 3 lines
net/netfilter/nf_conntrack_core.c +3
-0+ skb->nfct = &ct->ct_general; +nf_ct_acct_merge (ct, ctinfo, old_ct); +nf_ct_put (old_ct);Perhaps it would be better to not have
old_ctand insteadnf_conntrack_put(skb->nfct);skb->nfct=&ct->ct_general;?
I can use this if you find it more readable, no problem.
Quoted 6 lines
Quoted 6 lines
net/netfilter/nf_conntrack_core.c +1
-0+int ret; ct =nf_ct_get (skb, &ctinfo); net =nf_ct_net (ct);@@ -727,10 +770,11 @@ __nf_conntrack_confirm(struct sk_buff *skb)
Quoted 7 lines
net/netfilter/nf_conntrack_core.c __nf_conntrack_confirm(struct sk_buff *skb) · line 773
Quoted 3 lines
net/netfilter/nf_conntrack_core.c +1
-0out:nf_ct_add_to_dying_list (ct); + ret =nf_ct_resolve_clash (net, skb, ct, ctinfo, h);Is this safe?
Seems we jump to out label in other cases as well, not just for clashes.
We're jumping out for dying conntracks too, and clash is handling this already so I considered not adding more code. I could just run nf_ct_resolve_clash() iff !nf_ct_dying() but I don't see much of a benefit on this.
Florian Westphal
maintainerQuoted 9 lines · Pablo Neira Ayuso
Quoted 6 lines
net/netfilter/nf_conntrack_core.c static inline void nf_ct_acct_update(struct nf_conn *ct, · line 651
Quoted 3 lines
net/netfilter/nf_conntrack_core.c +3
-0+/* Don't modify skb->nfctinfo, we're at POSTROUTING so this +* packet is already leaving our framework, it is too late. +*/ Note that this might be loopback in which case this skb will reappear on PREROUTING.
The comment intention is that we probably already applied a filtering decision, so changing the ctinfo here seems awkward to me.
Hmm, but we did not drop the packet, else we would not have ended up in _confirm().
Quoted 3 lines
In NFQUEUE, this packet may have spent quite a bit of time so it may even get a different ctinfo if we reevaluate, but as I said, having packets changing its original ctinfo is...
OK, fair enough -- I just wanted to point out that for loopback clashes we can end up with ->nfct neing set to the "old" one (already in hash table) while ctinfo is the "new" one (from the ->nfct we tossed since would could not add it to the table).
But I see that there is no good argument to chose one over the other.
So I'm fine with current version, sorry for the confusion.
Quoted 11 lines
Quoted 9 lines
net/netfilter/nf_conntrack_core.c static inline void nf_ct_acct_update(struct nf_conn *ct, · line 654
Quoted 3 lines
net/netfilter/nf_conntrack_core.c +3
-0+ skb->nfct = &ct->ct_general; +nf_ct_acct_merge (ct, ctinfo, old_ct); +nf_ct_put (old_ct);Perhaps it would be better to not have
old_ctand insteadnf_conntrack_put(skb->nfct);skb->nfct=&ct->ct_general;?
I can use this if you find it more readable, no problem.
Thanks!
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
net/netfilter/nf_conntrack_core.c +1
-0+int ret; ct =nf_ct_get (skb, &ctinfo); net =nf_ct_net (ct);@@ -727,10 +770,11 @@ __nf_conntrack_confirm(struct sk_buff *skb)Quoted 7 lines
net/netfilter/nf_conntrack_core.c __nf_conntrack_confirm(struct sk_buff *skb) · line 773
Quoted 3 lines
net/netfilter/nf_conntrack_core.c +1
-0out:nf_ct_add_to_dying_list (ct); + ret =nf_ct_resolve_clash (net, skb, ct, ctinfo, h);Is this safe?
Seems we jump to out label in other cases as well, not just for clashes.
Quoted 4 lines
We're jumping out for dying conntracks too, and clash is handling this already so I considered not adding more code. I could just run
nf_ct_resolve_clash()iff !nf_ct_dying() but I don't see much of a benefit on this.
I missed the nf_ct_dying test, that should indeed avoid operating on *h garbage.