From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFF36C43387 for ; Wed, 2 Jan 2019 20:42:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B472520874 for ; Wed, 2 Jan 2019 20:42:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727160AbfABUmx (ORCPT ); Wed, 2 Jan 2019 15:42:53 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:43791 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727193AbfABUmx (ORCPT ); Wed, 2 Jan 2019 15:42:53 -0500 Received: from mail-qk1-f198.google.com ([209.85.222.198]) by youngberry.canonical.com with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.76) (envelope-from ) id 1genLn-0002ah-GW for stable@vger.kernel.org; Wed, 02 Jan 2019 20:42:51 +0000 Received: by mail-qk1-f198.google.com with SMTP id w185so38377165qka.9 for ; Wed, 02 Jan 2019 12:42:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Ork+B04QGZEk602Qi88An+6xPzB+iyFzNpJhzB2qOao=; b=EDBvo5RKmHoo1hIzTyE/KF7iFIDS59XNrElX/IE23OtkjRVtfzPL72Ry/gEem/y2N1 E12f/+6G1Cc4RnTJrSy39EfGgQYgIwnLo9T3Gin/HZ/VY9b2YMuHP7HRXTFAaXi9+N9d YgZGg3E+BpH65Yw2FcsMJ87VhhkfhD6JD5fZlr3v+llQ4labHu9c+T1/U02l3I5oRLCK WO9QReWCXIYh2Gc4Xl80YJTWitHNpwL24PSe8ensgl9t9V8jKHyyqyPoohzAnMeZ1nZq S/NYd09A8jMrJYSPgUa1gNz7/V/CLgm96U5ZYVKjrNfwscHyZv/ouRqLoPO54Ydsc9kh CjVA== X-Gm-Message-State: AJcUukfef1dIoJqIBmndz006FFp9f2bbOnvAwNbEHJxe/FNwHfsKFOai c2jfseEHBVLxXYuU1zhUyOnla/vu0rwRS2TyYwA5w8s8zW9zMRNffJ0AYT4U/6AKVJ7a84BqlSP uVm+0x2OC82Xg+36G/Sx5+SK5j1V+JUwQ4A== X-Received: by 2002:a0c:b3c2:: with SMTP id b2mr44221164qvf.138.1546461770438; Wed, 02 Jan 2019 12:42:50 -0800 (PST) X-Google-Smtp-Source: ALg8bN7Hcey4hXvmSOb7Rjnaa41RFLUE08waHqTggS9WmOZZRbFD0Y7sMlt4UAFAQSu1VR/3un603Q== X-Received: by 2002:a0c:b3c2:: with SMTP id b2mr44221153qvf.138.1546461770206; Wed, 02 Jan 2019 12:42:50 -0800 (PST) Received: from localhost.localdomain ([179.159.56.118]) by smtp.gmail.com with ESMTPSA id e17sm26679381qte.12.2019.01.02.12.42.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Jan 2019 12:42:49 -0800 (PST) From: Mauricio Faria de Oliveira To: stable@vger.kernel.org, netdev@vger.kernel.org, Florian Westphal Cc: Alakesh Haloi , nivedita.singhvi@canonical.com, Pablo Neira Ayuso , Jozsef Kadlecsik , "David S. Miller" , Yi-Hung Wei Subject: [PATCH v2 4.14 0/5] netfilter: xt_connlimit: backport upstream fixes for race in connection counting Date: Wed, 2 Jan 2019 18:41:59 -0200 Message-Id: <20190102204204.12389-1-mfo@canonical.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Ubuntu [changelog] - v2: include patch 5/5 (a very recent fix to patch 4/5) which is not yet in Linus's tree but it's in nf.git + linux-next.git, thus should make it shortly. Test results still consistent. Thanks Florian Westphal for reviewing and pointing that out. Recently, Alakesh Haloi reported the following issue [1] with stable/4.14: """ An iptable rule like the following on a multicore systems will result in accepting more connections than set in the rule. iptables -A INPUT -p tcp -m tcp --syn --dport 7777 -m connlimit \ --connlimit-above 2000 --connlimit-mask 0 -j DROP """ And proposed a fix that is not in Linus's tree. The discussion went on to confirm whether the issue was still reproducible with mainline/nf.git tip, and to either identify the upstream fix or re-submit the non-upstream fix. Alakesh eventually was able to test with upstream, and reported that issue was still reproducible [2]. On that, our findinds diverge, at least in my test environment: First, I verified that the suggested mainline fix for the issue [3] indeed fixes it, by testing with it applied and reverted on v4.18, a clean revert. (The issue is reproducible with the commit reverted). Then, with a consistent reproducer, I moved to nf.git, with HEAD on commit a007232 ("netfilter: nf_conncount: fix argument order to find_next_bit"), and the issues was not reproducible (even with 20+ threads on client side, the number Alakesh reported to achieve 2150+ connections [4], and I tried spreading the network interface IRQ affinity over more and more CPUs too.) Either way, the suggested mainline fix does actually fix the issue in 4.14 for at least one environment. So, it might well be the case that Alakesh's test environment has differences/subtleties that leads to more connections accepted, and more commits are needed for that particular environment type. (v2 update: see Florian's reply to v1 thread [1]; these different results are probably explained by very recent fixes still missing back then.) But for now, with one bare-metal environment (24-core server, 4-core client) verified, I thought of submitting the patches for review/comments/testing, then looking for additional fixes for that environment separately. The fix is PATCH 4 (needs fix in PATCH 5), and PATCHes 1-3 are helpers for a cleaner backport. All backports are simple, and essentially consist of refresh context lines and use older struct/file names. Reviews from netfilter maintainers are very appreciated, as I've no previous experience in this area, and although the backports look simple and build/run correctly, there's usually stuff that only more experienced people may notice. Thanks, Mauricio Links: ===== [1] https://www.spinics.net/lists/stable/msg270040.html [2] https://www.spinics.net/lists/stable/msg273669.html [3] https://www.spinics.net/lists/stable/msg271300.html [4] https://www.spinics.net/lists/stable/msg273669.html [5] https://www.spinics.net/lists/stable/msg276883.html Test-case: ========= - v4.14.91 (original): client achieves 2000+ connections (6000 target) with 3 threads. server # iptables -F server # iptables -A INPUT -p tcp -m tcp --syn --dport 7777 -m connlimit --connlimit-above 2000 --connlimit-mask 0 -j DROP server # iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination DROP tcp -- anywhere anywhere tcp dpt:7777 flags:FIN,SYN,RST,ACK/SYN #conn src/0 > 2000 Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination server # ulimit -SHn 65000 server # ruby server.rb <... listening ...> client # ulimit -SHn 65000 client # ruby client.rb 10.230.56.100 7777 6000 3 Connecting to ["10.230.56.100"]:7777 6000 times with 3 1 2 3 <...> 2000 <...> 6000 Target reached. Thread finishing 6001 Target reached. Thread finishing 6002 Target reached. Thread finishing Threads done. 6002 connections press enter to exit - v4.14.91 + patches: client only achieved 2000 connections. server # (same procedure) client # (same procedure) Connecting to ["10.230.56.100"]:7777 6000 times with 3 1 2 3 <...> 2000 <... blocked for a while...> failed to create connection: Connection timed out - connect(2) for "10.230.56.100" port 7777 failed to create connection: Connection timed out - connect(2) for "10.230.56.100" port 7777 failed to create connection: Connection timed out - connect(2) for "10.230.56.100" port 7777 Threads done. 2000 connections press enter to exit Florian Westphal (3): netfilter: xt_connlimit: don't store address in the conn nodes netfilter: nf_conncount: fix garbage collection confirm race netfilter: nf_conncount: don't skip eviction when age is negative Pablo Neira Ayuso (1): netfilter: nf_conncount: expose connection list interface Yi-Hung Wei (1): netfilter: nf_conncount: Fix garbage collection with zones include/net/netfilter/nf_conntrack_count.h | 15 +++++ net/netfilter/xt_connlimit.c | 99 +++++++++++++++++++++++------- 2 files changed, 91 insertions(+), 23 deletions(-) create mode 100644 include/net/netfilter/nf_conntrack_count.h -- 2.7.4