{"id":809392,"url":"http://patchwork.ozlabs.org/api/patches/809392/?format=json","web_url":"http://patchwork.ozlabs.org/project/netfilter-devel/patch/1504477667-12130-2-git-send-email-pablo@netfilter.org/","project":{"id":26,"url":"http://patchwork.ozlabs.org/api/projects/26/?format=json","name":"Netfilter Development","link_name":"netfilter-devel","list_id":"netfilter-devel.vger.kernel.org","list_email":"netfilter-devel@vger.kernel.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<1504477667-12130-2-git-send-email-pablo@netfilter.org>","list_archive_url":null,"date":"2017-09-03T22:27:11","name":"[11/47] netfilter: nfnetlink_queue: don't queue dying conntracks to userspace","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"b12c5197abb592aa60aa73963f9f319cdce7ca72","submitter":{"id":1315,"url":"http://patchwork.ozlabs.org/api/people/1315/?format=json","name":"Pablo Neira Ayuso","email":"pablo@netfilter.org"},"delegate":{"id":6139,"url":"http://patchwork.ozlabs.org/api/users/6139/?format=json","username":"pablo","first_name":"Pablo","last_name":"Neira","email":"pablo@netfilter.org"},"mbox":"http://patchwork.ozlabs.org/project/netfilter-devel/patch/1504477667-12130-2-git-send-email-pablo@netfilter.org/mbox/","series":[{"id":1280,"url":"http://patchwork.ozlabs.org/api/series/1280/?format=json","web_url":"http://patchwork.ozlabs.org/project/netfilter-devel/list/?series=1280","date":"2017-09-03T22:25:42","name":"[01/47] netfilter: expect: add to hash table after expect init","version":1,"mbox":"http://patchwork.ozlabs.org/series/1280/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/809392/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/809392/checks/","tags":{},"related":[],"headers":{"Return-Path":"<netfilter-devel-owner@vger.kernel.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":"ozlabs.org;\n\tspf=none (mailfrom) smtp.mailfrom=vger.kernel.org\n\t(client-ip=209.132.180.67; helo=vger.kernel.org;\n\tenvelope-from=netfilter-devel-owner@vger.kernel.org;\n\treceiver=<UNKNOWN>)","Received":["from vger.kernel.org (vger.kernel.org [209.132.180.67])\n\tby ozlabs.org (Postfix) with ESMTP id 3xlngB4Xxvz9s06\n\tfor <incoming@patchwork.ozlabs.org>;\n\tMon,  4 Sep 2017 08:28:06 +1000 (AEST)","(majordomo@vger.kernel.org) by vger.kernel.org via listexpand\n\tid S1753242AbdICW2E (ORCPT <rfc822;incoming@patchwork.ozlabs.org>);\n\tSun, 3 Sep 2017 18:28:04 -0400","from mail.us.es ([193.147.175.20]:51126 \"EHLO mail.us.es\"\n\trhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP\n\tid S1753182AbdICW2C (ORCPT <rfc822; netfilter-devel@vger.kernel.org>);\n\tSun, 3 Sep 2017 18:28:02 -0400","from antivirus1-rhel7.int (unknown [192.168.2.11])\n\tby mail.us.es (Postfix) with ESMTP id D3E0C190F63\n\tfor <netfilter-devel@vger.kernel.org>;\n\tMon,  4 Sep 2017 00:27:35 +0200 (CEST)","from antivirus1-rhel7.int (localhost [127.0.0.1])\n\tby antivirus1-rhel7.int (Postfix) with ESMTP id C4561B5026\n\tfor <netfilter-devel@vger.kernel.org>;\n\tMon,  4 Sep 2017 00:27:35 +0200 (CEST)","by antivirus1-rhel7.int (Postfix, from userid 99)\n\tid B9C032DC88; Mon,  4 Sep 2017 00:27:35 +0200 (CEST)","from antivirus1-rhel7.int (localhost [127.0.0.1])\n\tby antivirus1-rhel7.int (Postfix) with ESMTP id A9682B5027;\n\tMon,  4 Sep 2017 00:27:33 +0200 (CEST)","from 192.168.1.97 (192.168.1.97) by antivirus1-rhel7.int\n\t(F-Secure/fsigk_smtp/550/antivirus1-rhel7.int); \n\tMon, 04 Sep 2017 00:27:33 +0200 (CEST)","from salvia.here (unknown [31.4.193.113])\n\t(Authenticated sender: pneira@us.es)\n\tby entrada.int (Postfix) with ESMTPA id 4834A4265A20;\n\tMon,  4 Sep 2017 00:27:33 +0200 (CEST)"],"X-Spam-Checker-Version":"SpamAssassin 3.4.1 (2015-04-28) on\n\tantivirus1-rhel7.int","X-Spam-Level":"","X-Spam-Status":"No, score=-108.2 required=7.5 tests=ALL_TRUSTED,BAYES_50,\n\tSMTPAUTH_US2,USER_IN_WHITELIST autolearn=disabled version=3.4.1","X-Virus-Status":"clean(F-Secure/fsigk_smtp/550/antivirus1-rhel7.int)","X-SMTPAUTHUS":"auth mail.us.es","From":"Pablo Neira Ayuso <pablo@netfilter.org>","To":"netfilter-devel@vger.kernel.org","Cc":"davem@davemloft.net, netdev@vger.kernel.org","Subject":"[PATCH 11/47] netfilter: nfnetlink_queue: don't queue dying\n\tconntracks to userspace","Date":"Mon,  4 Sep 2017 00:27:11 +0200","Message-Id":"<1504477667-12130-2-git-send-email-pablo@netfilter.org>","X-Mailer":"git-send-email 2.1.4","In-Reply-To":"<1504477667-12130-1-git-send-email-pablo@netfilter.org>","References":"<1504477667-12130-1-git-send-email-pablo@netfilter.org>","X-Virus-Scanned":"ClamAV using ClamSMTP","Sender":"netfilter-devel-owner@vger.kernel.org","Precedence":"bulk","List-ID":"<netfilter-devel.vger.kernel.org>","X-Mailing-List":"netfilter-devel@vger.kernel.org"},"content":"From: Florian Westphal <fw@strlen.de>\n\nWhen skb is queued to userspace it leaves softirq/rcu protection.\nskb->nfct (via conntrack extensions such as helper) could then reference\nmodules that no longer exist if the conntrack was not yet confirmed.\n\nnf_ct_iterate_destroy() will set the DYING bit for unconfirmed\nconntracks, we therefore solve this race as follows:\n\n1. take the queue spinlock.\n2. check if the conntrack is unconfirmed and has dying bit set.\n   In this case, we must discard skb while we're still inside\n   rcu read-side section.\n3. If nf_ct_iterate_destroy() is called right after the packet is queued\n   to userspace, it will be removed from the queue via\n   nf_ct_iterate_destroy -> nf_queue_nf_hook_drop.\n\nWhen userspace sends the verdict (nfnetlink takes rcu read lock), there\nare two cases to consider:\n\n1. nf_ct_iterate_destroy() was called while packet was out.\n   In this case, skb will have been removed from the queue already\n   and no reinject takes place as we won't find a matching entry for the\n   packet id.\n\n2. nf_ct_iterate_destroy() gets called right after verdict callback\n   found and removed the skb from queue list.\n\n   In this case, skb->nfct is marked as dying but it is still valid.\n   The skb will be dropped either in nf_conntrack_confirm (we don't\n   insert DYING conntracks into hash table) or when we try to queue\n   the skb again, but either events don't occur before the rcu read lock\n   is dropped.\n\nSigned-off-by: Florian Westphal <fw@strlen.de>\nSigned-off-by: Pablo Neira Ayuso <pablo@netfilter.org>\n---\n net/netfilter/nfnetlink_queue.c | 19 +++++++++++++++++++\n 1 file changed, 19 insertions(+)","diff":"diff --git a/net/netfilter/nfnetlink_queue.c b/net/netfilter/nfnetlink_queue.c\nindex 7c543bfbf624..c9796629858f 100644\n--- a/net/netfilter/nfnetlink_queue.c\n+++ b/net/netfilter/nfnetlink_queue.c\n@@ -41,6 +41,10 @@\n #include \"../bridge/br_private.h\"\n #endif\n \n+#if IS_ENABLED(CONFIG_NF_CONNTRACK)\n+#include <net/netfilter/nf_conntrack.h>\n+#endif\n+\n #define NFQNL_QMAX_DEFAULT 1024\n \n /* We're using struct nlattr which has 16bit nla_len. Note that nla_len\n@@ -612,6 +616,18 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,\n \treturn NULL;\n }\n \n+static bool nf_ct_drop_unconfirmed(const struct nf_queue_entry *entry)\n+{\n+#if IS_ENABLED(CONFIG_NF_CONNTRACK)\n+\tstatic const unsigned long flags = IPS_CONFIRMED | IPS_DYING;\n+\tconst struct nf_conn *ct = (void *)skb_nfct(entry->skb);\n+\n+\tif (ct && ((ct->status & flags) == IPS_DYING))\n+\t\treturn true;\n+#endif\n+\treturn false;\n+}\n+\n static int\n __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,\n \t\t\tstruct nf_queue_entry *entry)\n@@ -628,6 +644,9 @@ __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,\n \t}\n \tspin_lock_bh(&queue->lock);\n \n+\tif (nf_ct_drop_unconfirmed(entry))\n+\t\tgoto err_out_free_nskb;\n+\n \tif (queue->queue_total >= queue->queue_maxlen) {\n \t\tif (queue->flags & NFQA_CFG_F_FAIL_OPEN) {\n \t\t\tfailopen = 1;\n","prefixes":["11/47"]}