From patchwork Wed Jul 17 00:30:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Ruffell X-Patchwork-Id: 1133020 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45pJ8k087sz9sNr; Wed, 17 Jul 2019 10:31:01 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1hnXqO-0003NP-V9; Wed, 17 Jul 2019 00:30:52 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1hnXqN-0003NH-Bg for kernel-team@lists.ubuntu.com; Wed, 17 Jul 2019 00:30:51 +0000 Received: from mail-pf1-f197.google.com ([209.85.210.197]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1hnXqM-0005uw-Sa for kernel-team@lists.ubuntu.com; Wed, 17 Jul 2019 00:30:51 +0000 Received: by mail-pf1-f197.google.com with SMTP id i27so13370780pfk.12 for ; Tue, 16 Jul 2019 17:30:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=Z4nM4AsscXzUaDxmczvrbFhbqTV4O308Z3GhHFb0o/0=; b=EleMYOEz3nAKVYj4ODLzWYubak+wl8YsFZYwuF0xzf2F+LgECTGRK5U/AmY/SickJb 7bZ9cgtEBEkrZ6AXZR2x3kaHwBsEZod7OHsnick81O7/lfhCpwErOo5OHnPpj3rbyg2l 0yL5TVCF6fK6eq4ImmS0LU9P+Pd/F6l/uqMJ8pQupAmFuUFOaCqcnGZAF13aIgB68Iay RzcrfRqyns//Gh6c8bibmXRzp/Sxh/NUiA6JWfkTU2TdLv/D+Key4c6beqcGglC2H4wi Z1CIK2UmW+VGJBcXof41m4CqI8SJY3xV07e6fLJuDytgExW8j2/OUCn3DAreBw/pZKbQ hBBQ== X-Gm-Message-State: APjAAAXjZ627RA19ROJxPEoWvtriiAbWlx3uQAzx3DeUnWiqbiEAaWFZ hJv+Mp/EVNQBqb2pjuhJ/HFICDvRfEv0j1CpAKCx9sBVa+qqU7RArUtja0SoczHP1qVR6z/adCM fB+tJs+hK9DZWRHiHVpVTqEdMW0NCklCLBfem99/X4g== X-Received: by 2002:a17:90a:25c8:: with SMTP id k66mr40288611pje.129.1563323449405; Tue, 16 Jul 2019 17:30:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqxqPaY1NbBnSMYWvWoB1gXTTYd4AzCnIrNyVwjsKNdMP/t4RakVnHDyCPInBwJVUTNec6MyEQ== X-Received: by 2002:a17:90a:25c8:: with SMTP id k66mr40288583pje.129.1563323449100; Tue, 16 Jul 2019 17:30:49 -0700 (PDT) Received: from localhost.localdomain (125-238-115-161-fibre.sparkbb.co.nz. [125.238.115.161]) by smtp.gmail.com with ESMTPSA id i74sm23814103pje.16.2019.07.16.17.30.47 for (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 16 Jul 2019 17:30:48 -0700 (PDT) From: Matthew Ruffell To: kernel-team@lists.ubuntu.com Subject: [SRU][Bionic][PATCH 0/2] Fix nf_conntrack races when dealing with same origin requests in NAT environments Date: Wed, 17 Jul 2019 12:30:39 +1200 Message-Id: <20190717003041.9775-1-matthew.ruffell@canonical.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" BugLink: https://bugs.launchpad.net/bugs/1836816 [Impact] There are a number of races in nf_conntrack when multiple packets are sent from the same origin to the same destination at the same time, where there is no pre-existing confirmed conntrack entry, in a NAT environment. This is most frequently seen in users with kubernetes workloads, where dns requests are sent from the same socket at the same time, from different threads. One request is for a A record, the other AAAA. A conntrack race happens, which leads to one request being dropped, which leads to 5 second dns timeouts. This problem is not specific to kubernetes, as any multi-threaded process which sends UDP packets from the same socket at the same time from different threads is affected. Note that since UDP is connectionless, no packet is sent when connect() is called, so no conntrack entries are created until a packet is first sent. In the scenario where two UDP packets are sent at the same time from the same socket on different threads, there are the following possible races: 1.) Neither of the packets find a confirmed conntrack entry. This leads to two conntrack entries being created with the same tuples. 2.) Same as 1), but a conntrack entry is confirmed for one of the packets before the other calls get_unique_tuple(). The other packet gets a different reply tuple with the source port changed. The outcomes of the races are the same, one of the packets is dropped when it comes time to confirm conntrack entries with __nf_conntrack_confirm(). For more information, read the technical blog post: https://www.weave.works/blog/racy-conntrack-and-dns-lookup-timeouts [Fix] The fix to race 1) was included in 4.19 upstream by this commit: netfilter: nf_conntrack: resolve clash for matching conntracks commit ed07d9a021df6da53456663a76999189badc432a Author: Martynas Pumputis Date: Mon Jul 2 16:52:14 2018 +0200 This commit adds an extra check to see if the two entries are the same and to merge the entries if they are. The fix to race 2) was included in 5.0 upstream by this commit: netfilter: nf_nat: skip nat clash resolution for same-origin entries commit 4e35c1cb9460240e983a01745b5f29fe3a4d8e39 Author: Martynas Pumputis Date: Tue Jan 29 15:51:42 2019 +0100 This commit ensures that a new source port is not allocated to a duplicated entry, and forwards the duplicated entries to be resolved later on. Note that 4e35c1cb9460240e983a01745b5f29fe3a4d8e39 was also backported to stable releases 4.9.163, 4.14.106, 4.19.29, 4.20.16. Both commits cherry-pick to 4.15 bionic cleanly. Please cherry-pick both commits to all bionic kernels. [Testcase] The reproducer has been provided by the author of the fixes, and is best viewed here: https://github.com/brb/conntrack-race The dmesg logs and packet traces in the above github repo are best viewed while reading upstream discussion: https://patchwork.ozlabs.org/patch/937963/ I have built a test kernel for xenial HWE, which can be found here: https://launchpad.net/~mruffell/+archive/ubuntu/sf00227747-test-kernel The test kernel has been tested with a kubernetes workload and aided with resolution of kubernetes problems. [Regression Potential] As noted previously, 4e35c1cb9460240e983a01745b5f29fe3a4d8e39 has been backported to stable releases 4.9.163, 4.14.106, 4.19.29, 4.20.16 and has no changes to the primary commit. This commit is well tested and considered stable by the community. Both commits have also been present in the -azure kernels since 4.15.0-1030.31 and 4.18.0-1004.4, as per LP #1795493, http://bugs.launchpad.net/bugs/1795493 They have received wide testing, and no problems have been reported. The changes are specifically limited to resolving clashes between duplicate conntrack entries, and any regressions will be limited to that scenario. The overall risk of regression is very low. [Notes] The commits in the -azure kernels will need to be reverted before importing the generic bionic updates, since they are the pre-upstream commits, and have had several revisions since they were imported. Replacing them with the actual upstream commits will make things easier in the future to maintain. http://bugs.launchpad.net/bugs/1795493 For race 1): Azure: https://patchwork.ozlabs.org/patch/937963/ Upstream: ed07d9a021df6da53456663a76999189badc432a Commits are the same. For race 2): Azure: https://patchwork.ozlabs.org/patch/952939/ Upstream: 4e35c1cb9460240e983a01745b5f29fe3a4d8e39 Note the difference, old commit must be reverted first. Martynas Pumputis (2): netfilter: nf_conntrack: resolve clash for matching conntracks netfilter: nf_nat: skip nat clash resolution for same-origin entries net/netfilter/nf_conntrack_core.c | 46 +++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) Acked-by: Stefan Bader Acked-by: Connor Kuehl