]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
netfilter: nf_conntrack: resolve clash for matching conntracks
authorMartynas Pumputis <martynas@weave.works>
Wed, 17 Jul 2019 00:30:40 +0000 (12:30 +1200)
committerSultan Alsawaf <sultan.alsawaf@canonical.com>
Wed, 24 Jul 2019 15:45:12 +0000 (09:45 -0600)
BugLink: https://bugs.launchpad.net/bugs/1836816
This patch enables the clash resolution for NAT (disabled in
"590b52e10d41") if clashing conntracks match (i.e. both tuples are equal)
and a protocol allows it.

The clash might happen for a connections-less protocol (e.g. UDP) when
two threads in parallel writes to the same socket and consequent calls
to "get_unique_tuple" return the same tuples (incl. reply tuples).

In this case it is safe to perform the resolution, as the losing CT
describes the same mangling as the winning CT, so no modifications to
the packet are needed, and the result of rules traversal for the loser's
packet stays valid.

Signed-off-by: Martynas Pumputis <martynas@weave.works>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
(cherry picked from commit ed07d9a021df6da53456663a76999189badc432a)
Signed-off-by: Matthew Ruffell <matthew.ruffell@canonical.com>
Acked-by: Connor Kuehl <connor.kuehl@canonical.com>
Acked-by: Stefan Bader <stefan.bader@canonical.com>
Signed-off-by: Khalid Elmously <khalid.elmously@canonical.com>
net/netfilter/nf_conntrack_core.c

index 7f23c4c4c6d01e38335c2279f269198bcbaa16f8..49412d2c24fdd2ad0f50798881bb321cfa15f3f5 100644 (file)
@@ -508,6 +508,18 @@ nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
               net_eq(net, nf_ct_net(ct));
 }
 
+static inline bool
+nf_ct_match(const struct nf_conn *ct1, const struct nf_conn *ct2)
+{
+       return nf_ct_tuple_equal(&ct1->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
+                                &ct2->tuplehash[IP_CT_DIR_ORIGINAL].tuple) &&
+              nf_ct_tuple_equal(&ct1->tuplehash[IP_CT_DIR_REPLY].tuple,
+                                &ct2->tuplehash[IP_CT_DIR_REPLY].tuple) &&
+              nf_ct_zone_equal(ct1, nf_ct_zone(ct2), IP_CT_DIR_ORIGINAL) &&
+              nf_ct_zone_equal(ct1, nf_ct_zone(ct2), IP_CT_DIR_REPLY) &&
+              net_eq(nf_ct_net(ct1), nf_ct_net(ct2));
+}
+
 /* caller must hold rcu readlock and none of the nf_conntrack_locks */
 static void nf_ct_gc_expired(struct nf_conn *ct)
 {
@@ -701,19 +713,21 @@ static int nf_ct_resolve_clash(struct net *net, struct sk_buff *skb,
        /* This is the conntrack entry already in hashes that won race. */
        struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
        const struct nf_conntrack_l4proto *l4proto;
+       enum ip_conntrack_info oldinfo;
+       struct nf_conn *loser_ct = nf_ct_get(skb, &oldinfo);
 
        l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
        if (l4proto->allow_clash &&
-           ((ct->status & IPS_NAT_DONE_MASK) == 0) &&
            !nf_ct_is_dying(ct) &&
            atomic_inc_not_zero(&ct->ct_general.use)) {
-               enum ip_conntrack_info oldinfo;
-               struct nf_conn *loser_ct = nf_ct_get(skb, &oldinfo);
-
-               nf_ct_acct_merge(ct, ctinfo, loser_ct);
-               nf_conntrack_put(&loser_ct->ct_general);
-               nf_ct_set(skb, ct, oldinfo);
-               return NF_ACCEPT;
+               if (((ct->status & IPS_NAT_DONE_MASK) == 0) ||
+                   nf_ct_match(ct, loser_ct)) {
+                       nf_ct_acct_merge(ct, ctinfo, loser_ct);
+                       nf_conntrack_put(&loser_ct->ct_general);
+                       nf_ct_set(skb, ct, oldinfo);
+                       return NF_ACCEPT;
+               }
+               nf_ct_put(ct);
        }
        NF_CT_STAT_INC(net, drop);
        return NF_DROP;