Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6249533imu; Mon, 21 Jan 2019 05:50:22 -0800 (PST) X-Google-Smtp-Source: ALg8bN6z2j5FRYkbjU/t+k5VDu7pY9GwhHGnAF5pycnwsDmFodBfrGinwRaj9cmeesWhHGU5ysYs X-Received: by 2002:a17:902:a586:: with SMTP id az6mr13427019plb.298.1548078622433; Mon, 21 Jan 2019 05:50:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548078622; cv=none; d=google.com; s=arc-20160816; b=Miewplm9zifCTZ/q08WUacxUiR/TXjwMkQUoaiPKJGl/tdXgXa9AJ1r8yTnwdZ3F4c xCmEQs0DCv1GtkohmmaWtaNpj5D+tanjvWrNZvjoNohuhmpOiw3Ob364deivNt1TbLcd yYRpr7kTsi/vUrPzkGa+I3f3XOK/2WAFgVp+o7i0Nj3iR+n8oRuDFbxS6Vuio6evQ+ex oalZWpLsxo4YCrJcSFi+y0dAVm0rpPqhpdzdViZ5k61ObGMqMiqoIpM2lUXxNEsRrQCe 8Be7tPtlZAQNEIQBg+wcIybHtcO2LJlhkVy4zsh7Op2ipJHkiwMCLWe4ZvOySmXhV16M XbGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LOUOf5p1sqN0ITKVR862USBUMhMjmsB7IaTeTztlr84=; b=AaZhS8hgc0hSB5KliY5jcaEDwpQFLvXhjM0sWZbT8ShgRhAENODHGR6TeEjhhRtS4t ZY75MvD9jfUMSw+uz8IzKybpRk2I3McUWDCd24YKO4lE+6OOqMXiOmxBNETt8dKNxLET 9gbMULKYbHK7G0kv/iPKL8HPs65NXy2gRotd5x2vRNJMFn3xlvCtgtchFUoWasQ/uoOz JNzL5YamE2iFPrJFnP6SeA6/Zt9Tiyw1yx+luhNQbnc2P9UYV3/Z1urmXZg5SyS74GSn K1BC0OuG+b+Bdt1x8D/zgVTSxX5ZcZ6yMz8h0UdiHDLYfkJc/J6lHEkYL7yCNL13ELfl +mtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=W7V+gKDb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ay4si13255090plb.235.2019.01.21.05.49.59; Mon, 21 Jan 2019 05:50:22 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=W7V+gKDb; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729847AbfAUNsM (ORCPT + 99 others); Mon, 21 Jan 2019 08:48:12 -0500 Received: from mail.kernel.org ([198.145.29.99]:57858 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729830AbfAUNsJ (ORCPT ); Mon, 21 Jan 2019 08:48:09 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7129E20861; Mon, 21 Jan 2019 13:48:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548078487; bh=HuRMaG1AEMTThaJ1oJNVOz61KDV3PCiZzmmF6yMNd9s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W7V+gKDbOxpvsn0i36g06wHaHIByW9sAZai4BJ2Wq3DUV5dHzFMEgj1pVA2klevz+ L8SxIojBVMVVyrKumKQSLAYEKiCIzelhtlKzIvdafCTqFYhPDZJr0GQkhGcDBqENJ4 VgBJkjDEbDI+9MTewsjMP8GSF5m5hCH4sM2TX3mY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Shawn Bohrer , Florian Westphal , Pablo Neira Ayuso Subject: [PATCH 4.20 011/111] netfilter: nf_conncount: move all list iterations under spinlock Date: Mon, 21 Jan 2019 14:42:05 +0100 Message-Id: <20190121122457.004832613@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190121122455.819406896@linuxfoundation.org> References: <20190121122455.819406896@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Pablo Neira Ayuso commit 2f971a8f425545da52ca0e6bee81f5b1ea0ccc5f upstream. Two CPUs may race to remove a connection from the list, the existing conn->dead will result in a use-after-free. Use the per-list spinlock to protect list iterations. As all accesses to the list now happen while holding the per-list lock, we no longer need to delay free operations with rcu. Joint work with Florian. Fixes: 5c789e131cbb9 ("netfilter: nf_conncount: Add list lock and gc worker, and RCU for init tree search") Reviewed-by: Shawn Bohrer Signed-off-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Greg Kroah-Hartman --- net/netfilter/nf_conncount.c | 46 ++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) --- a/net/netfilter/nf_conncount.c +++ b/net/netfilter/nf_conncount.c @@ -43,8 +43,6 @@ struct nf_conncount_tuple { struct nf_conntrack_zone zone; int cpu; u32 jiffies32; - bool dead; - struct rcu_head rcu_head; }; struct nf_conncount_rb { @@ -83,36 +81,21 @@ static int key_diff(const u32 *a, const return memcmp(a, b, klen * sizeof(u32)); } -static void __conn_free(struct rcu_head *h) -{ - struct nf_conncount_tuple *conn; - - conn = container_of(h, struct nf_conncount_tuple, rcu_head); - kmem_cache_free(conncount_conn_cachep, conn); -} - static bool conn_free(struct nf_conncount_list *list, struct nf_conncount_tuple *conn) { bool free_entry = false; - spin_lock_bh(&list->list_lock); - - if (conn->dead) { - spin_unlock_bh(&list->list_lock); - return free_entry; - } + lockdep_assert_held(&list->list_lock); list->count--; - conn->dead = true; - list_del_rcu(&conn->node); + list_del(&conn->node); if (list->count == 0) { list->dead = true; free_entry = true; } - spin_unlock_bh(&list->list_lock); - call_rcu(&conn->rcu_head, __conn_free); + kmem_cache_free(conncount_conn_cachep, conn); return free_entry; } @@ -242,7 +225,7 @@ void nf_conncount_list_init(struct nf_co } EXPORT_SYMBOL_GPL(nf_conncount_list_init); -/* Return true if the list is empty */ +/* Return true if the list is empty. Must be called with BH disabled. */ bool nf_conncount_gc_list(struct net *net, struct nf_conncount_list *list) { @@ -253,12 +236,18 @@ bool nf_conncount_gc_list(struct net *ne bool free_entry = false; bool ret = false; + /* don't bother if other cpu is already doing GC */ + if (!spin_trylock(&list->list_lock)) + return false; + list_for_each_entry_safe(conn, conn_n, &list->head, node) { found = find_or_evict(net, list, conn, &free_entry); if (IS_ERR(found)) { if (PTR_ERR(found) == -ENOENT) { - if (free_entry) + if (free_entry) { + spin_unlock(&list->list_lock); return true; + } collected++; } continue; @@ -271,23 +260,24 @@ bool nf_conncount_gc_list(struct net *ne * closed already -> ditch it */ nf_ct_put(found_ct); - if (conn_free(list, conn)) + if (conn_free(list, conn)) { + spin_unlock(&list->list_lock); return true; + } collected++; continue; } nf_ct_put(found_ct); if (collected > CONNCOUNT_GC_MAX_NODES) - return false; + break; } - spin_lock_bh(&list->list_lock); if (!list->count) { list->dead = true; ret = true; } - spin_unlock_bh(&list->list_lock); + spin_unlock(&list->list_lock); return ret; } @@ -478,6 +468,7 @@ static void tree_gc_worker(struct work_s tree = data->gc_tree % CONNCOUNT_SLOTS; root = &data->root[tree]; + local_bh_disable(); rcu_read_lock(); for (node = rb_first(root); node != NULL; node = rb_next(node)) { rbconn = rb_entry(node, struct nf_conncount_rb, node); @@ -485,6 +476,9 @@ static void tree_gc_worker(struct work_s gc_count++; } rcu_read_unlock(); + local_bh_enable(); + + cond_resched(); spin_lock_bh(&nf_conncount_locks[tree]); if (gc_count < ARRAY_SIZE(gc_nodes))