Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2231030ybi; Thu, 20 Jun 2019 11:16:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqwMT490AyUxucdiTBaSB0S2nQ2Hszar4FgJPxEvxDmyyVeoUS9v6ZDauOpAoTe4nx02kPlU X-Received: by 2002:aa7:90ce:: with SMTP id k14mr15460627pfk.239.1561054560664; Thu, 20 Jun 2019 11:16:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561054560; cv=none; d=google.com; s=arc-20160816; b=pctW5Xr6SuCN54ZOPFRJ+f6rOdeV65mhj8tiKfgdW0NHREe2zfXWped5R9HUstm00y FsQZ8oOrhvzWAeSLSjLMzE2CSm3LNNmxIM0bLlW/f2umP5kZf0fUzzw/ZMLilMyZ4k4X yRMYlofmuqBjQW5LtYSM2OFxXRwmX4le96UG3PvHbMTTGkrARb67S6tbIQFgJv1rU9Ze EVQJxVE2YwImZRzo+ZLvkU1SBv7p7AAsqw0eAw8vXebskCzrGXUr+MbnPmBx9TObeG4I Ebm2KxdtwjEL9+f0RbZqntrDzKW4xSUstqrS6SvrjX/1EJxD4xY1WWwMCBeagbz/UYYN /lNg== 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=DnZ7WjhE6z8OvObAmsAM8KRMjwaKkTLsHO7/vIAH/gg=; b=uS14MuKREP9jwFQzswzTG2YyRVU0qmb3kescK8+J2DyMglvEp2uuymda23IkdFWsd3 PB+EJecLsaZRCjJsemUPC2BTJp5SGakBZn0PsFjLqY0m8hP3ODbx7wLcwPPkAj1ru7SD F7JZMV5ARqJZF85r6d0UfP0Vg1XukO+v6kBXoKRdkoQYyOATyDM7DofUHdP8SOAocCVr ORzgpRAbElB4txIhCqlu2xnSkOAC3UkAgDSarfVJDmvFoBYc9qoU28Sf1FLnK2Bd2ZeQ GTpNRjrbk2BHsF4aHq2gjDz4LTqtIUJxBd1d0mz/VIThumgwaYTsiht3ky2RejyjAU3r tuXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GgtF71D2; 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 s3si384943pji.24.2019.06.20.11.15.45; Thu, 20 Jun 2019 11:16:00 -0700 (PDT) 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=GgtF71D2; 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 S1727690AbfFTSO1 (ORCPT + 99 others); Thu, 20 Jun 2019 14:14:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:42690 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728962AbfFTSOY (ORCPT ); Thu, 20 Jun 2019 14:14:24 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.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 65CEB205F4; Thu, 20 Jun 2019 18:14:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1561054463; bh=VqzwzHHbmfYIgfW2lSoHJXCgE3kN9UfqUcUCgn0i6Ds=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GgtF71D25WrlgNXTD+gBKNe0JL5CPy9Lpas3IBQDxO/Mbk99Kjg3VMU43vHeZQsZj 0uy7VZkeqpYgUDFMtcqV6gUcwucjDkg0sAO65OU9nyxYtZVvS4jDlbhX9KewTESMBB gy5TB+wiboiPo8nvr49m3KJ1MYTqDRVAHY63GGEg= 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.1 37/98] netfilter: nf_tables: fix oops during rule dump Date: Thu, 20 Jun 2019 19:57:04 +0200 Message-Id: <20190620174350.778751666@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190620174349.443386789@linuxfoundation.org> References: <20190620174349.443386789@linuxfoundation.org> User-Agent: quilt/0.66 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 [ Upstream commit 2c82c7e724ff51cab78e1afd5c2aaa31994fe41e ] We can oops in nf_tables_fill_rule_info(). Its not possible to fetch previous element in rcu-protected lists when deletions are not prevented somehow: list_del_rcu poisons the ->prev pointer value. Before rcu-conversion this was safe as dump operations did hold nfnetlink mutex. Pass previous rule as argument, obtained by keeping a pointer to the previous rule during traversal. Fixes: d9adf22a291883 ("netfilter: nf_tables: use call_rcu in netlink dumps") Signed-off-by: Florian Westphal Signed-off-by: Pablo Neira Ayuso Signed-off-by: Sasha Levin --- net/netfilter/nf_tables_api.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c index aa5e7b00a581..101975386547 100644 --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -2261,13 +2261,13 @@ static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, u32 flags, int family, const struct nft_table *table, const struct nft_chain *chain, - const struct nft_rule *rule) + const struct nft_rule *rule, + const struct nft_rule *prule) { struct nlmsghdr *nlh; struct nfgenmsg *nfmsg; const struct nft_expr *expr, *next; struct nlattr *list; - const struct nft_rule *prule; u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags); @@ -2287,8 +2287,7 @@ static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, NFTA_RULE_PAD)) goto nla_put_failure; - if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) { - prule = list_prev_entry(rule, list); + if (event != NFT_MSG_DELRULE && prule) { if (nla_put_be64(skb, NFTA_RULE_POSITION, cpu_to_be64(prule->handle), NFTA_RULE_PAD)) @@ -2335,7 +2334,7 @@ static void nf_tables_rule_notify(const struct nft_ctx *ctx, err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, event, 0, ctx->family, ctx->table, - ctx->chain, rule); + ctx->chain, rule, NULL); if (err < 0) { kfree_skb(skb); goto err; @@ -2360,12 +2359,13 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, const struct nft_chain *chain) { struct net *net = sock_net(skb->sk); + const struct nft_rule *rule, *prule; unsigned int s_idx = cb->args[0]; - const struct nft_rule *rule; + prule = NULL; list_for_each_entry_rcu(rule, &chain->rules, list) { if (!nft_is_active(net, rule)) - goto cont; + goto cont_skip; if (*idx < s_idx) goto cont; if (*idx > s_idx) { @@ -2377,11 +2377,13 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, NFT_MSG_NEWRULE, NLM_F_MULTI | NLM_F_APPEND, table->family, - table, chain, rule) < 0) + table, chain, rule, prule) < 0) return 1; nl_dump_check_consistent(cb, nlmsg_hdr(skb)); cont: + prule = rule; +cont_skip: (*idx)++; } return 0; @@ -2537,7 +2539,7 @@ static int nf_tables_getrule(struct net *net, struct sock *nlsk, err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, - family, table, chain, rule); + family, table, chain, rule, NULL); if (err < 0) goto err; -- 2.20.1