Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp5068664pxb; Mon, 15 Feb 2021 08:41:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJzBUOI9ZJNyPaoety6mNVxk48wgldxMCpCBnBEi5AjUHfMWd45zP0LPs397+hdD1e9kCaDo X-Received: by 2002:a05:6402:22ef:: with SMTP id dn15mr10120462edb.259.1613407266999; Mon, 15 Feb 2021 08:41:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613407266; cv=none; d=google.com; s=arc-20160816; b=lsv5dJO3pHr2QfGZBVgha/i0VoFzH5gkB4XXBgoN+WM5cRkbfKGtQOvgKIVfeoKvXS pJGJF52rGR3HTY1TcK0mBcciFXW5L17Pp33CS8W4vazyB7uiK00gi+gtFebaLVJT/FTc NhLCFLiaEfAn3X4xApr6hHbOO9EAFWzWXQ7z8r3RBKFQADHJ6n/+gT3FDIjm9EmED48J EH1jVBwrSR15kSg3p+qtyZA4sO4NHVkKDvnZERIIzZ7/rF0W6FAMI/a2SGfzE58u2dBv foUr6qT/dZfh301QfY6MfxDgHIw73vRDsDzWtb6KZFaVoqU7l9YJD04G7HDWhFunmT+o OfpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=PZNK3oIUJoDar4kd+qQr01voMZxS6/zIuScWcy3GDGM=; b=qUPEnCOxyqYIhOiYvlY4rayzXuZucVJ1NIELu9Q6pdMDf+YcWmiQ7E4rUjABndyfM3 ul3KWk8BR2+BNm43Guum5+f3TgFRV2TrSD3qf2MfKLLRDUNB71tkhw6AMhHHzKxxDMuj i3N1XwVzDQOY8L8lZjdbJYpava3kFoHWzTnU/XPp77CWss3spPhoJt9k0UOkCaZmo1bW XeVhDfvJEu3BYDH1777xTeqpJrRNvLjjsewSMJIvBrBkL2fG/2hSFIZipC77i1nSAaJT WIZmg04pPPJq/nNsnUR6p/x2ELHX4W0wtNWrVxa06BdVCCJFzLBeGlAy+CLfoxAqfmG1 Qgew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="BvpH/4jF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bm18si12701934edb.545.2021.02.15.08.40.43; Mon, 15 Feb 2021 08:41:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="BvpH/4jF"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232257AbhBOQiI (ORCPT + 99 others); Mon, 15 Feb 2021 11:38:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:49602 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231461AbhBOPht (ORCPT ); Mon, 15 Feb 2021 10:37:49 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 2E46164E8D; Mon, 15 Feb 2021 15:33:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1613403183; bh=jzPHzKD2Ed5/n9dms/bMHHJ2mn91e8UVJRhc+VNG0ag=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BvpH/4jFmXIbfFj9H+Fgvm6bSOakBuppn7A5O7Y5+mcySjSVXHIVKorsROZigasqU aC0auMu1XWI8L70aDVOCWt//qmGu7V8Wy48jo45YT0lXPKWG72U+P9V7DncTVXawTn GgQNnVF0CjiO4o6KQML6S9GEm0VmcJI5J+Rr++As= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Florian Westphal , Pablo Neira Ayuso , Sasha Levin Subject: [PATCH 5.10 059/104] netfilter: nftables: fix possible UAF over chains from packet path in netns Date: Mon, 15 Feb 2021 16:27:12 +0100 Message-Id: <20210215152721.383308708@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210215152719.459796636@linuxfoundation.org> References: <20210215152719.459796636@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Pablo Neira Ayuso [ Upstream commit 767d1216bff82507c945e92fe719dff2083bb2f4 ] Although hooks are released via call_rcu(), chain and rule objects are immediately released while packets are still walking over these bits. This patch adds the .pre_exit callback which is invoked before synchronize_rcu() in the netns framework to stay safe. Remove a comment which is not valid anymore since the core does not use synchronize_net() anymore since 8c873e219970 ("netfilter: core: free hooks with call_rcu"). Suggested-by: Florian Westphal Fixes: df05ef874b28 ("netfilter: nf_tables: release objects on netns destruction") Signed-off-by: Pablo Neira Ayuso Signed-off-by: Sasha Levin --- net/netfilter/nf_tables_api.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c index 9a080767667b7..8739ef135156b 100644 --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -8775,6 +8775,17 @@ int __nft_release_basechain(struct nft_ctx *ctx) } EXPORT_SYMBOL_GPL(__nft_release_basechain); +static void __nft_release_hooks(struct net *net) +{ + struct nft_table *table; + struct nft_chain *chain; + + list_for_each_entry(table, &net->nft.tables, list) { + list_for_each_entry(chain, &table->chains, list) + nf_tables_unregister_hook(net, table, chain); + } +} + static void __nft_release_tables(struct net *net) { struct nft_flowtable *flowtable, *nf; @@ -8790,10 +8801,6 @@ static void __nft_release_tables(struct net *net) list_for_each_entry_safe(table, nt, &net->nft.tables, list) { ctx.family = table->family; - - list_for_each_entry(chain, &table->chains, list) - nf_tables_unregister_hook(net, table, chain); - /* No packets are walking on these chains anymore. */ ctx.table = table; list_for_each_entry(chain, &table->chains, list) { ctx.chain = chain; @@ -8842,6 +8849,11 @@ static int __net_init nf_tables_init_net(struct net *net) return 0; } +static void __net_exit nf_tables_pre_exit_net(struct net *net) +{ + __nft_release_hooks(net); +} + static void __net_exit nf_tables_exit_net(struct net *net) { mutex_lock(&net->nft.commit_mutex); @@ -8855,8 +8867,9 @@ static void __net_exit nf_tables_exit_net(struct net *net) } static struct pernet_operations nf_tables_net_ops = { - .init = nf_tables_init_net, - .exit = nf_tables_exit_net, + .init = nf_tables_init_net, + .pre_exit = nf_tables_pre_exit_net, + .exit = nf_tables_exit_net, }; static int __init nf_tables_module_init(void) -- 2.27.0