netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: davem@davemloft.net, netdev@vger.kernel.org
Subject: [PATCH 24/29] netfilter: nf_tables: check the result of dereferencing base_chain->stats
Date: Sat,  2 Mar 2019 19:37:15 +0100	[thread overview]
Message-ID: <20190302183720.3220-5-pablo@netfilter.org> (raw)
In-Reply-To: <20190302183720.3220-1-pablo@netfilter.org>

From: Li RongQing <lirongqing@baidu.com>

Check the result of dereferencing base_chain->stats, instead of result
of this_cpu_ptr with NULL.

base_chain->stats maybe be changed to NULL when a chain is updated and a
new NULL counter can be attached.

And we do not need to check returning of this_cpu_ptr since
base_chain->stats is from percpu allocator if it is non-NULL,
this_cpu_ptr returns a valid value.

And fix two sparse error by replacing rcu_access_pointer and
rcu_dereference with READ_ONCE under rcu_read_lock.

Thanks for Eric's help to finish this patch.

Fixes: 009240940e84c1 ("netfilter: nf_tables: don't assume chain stats are set when jumplabel is set")
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: Zhang Yu <zhangyu31@baidu.com>
Signed-off-by: Li RongQing <lirongqing@baidu.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 net/netfilter/nf_tables_core.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/net/netfilter/nf_tables_core.c b/net/netfilter/nf_tables_core.c
index e56a94dff34e..d0f168c2670f 100644
--- a/net/netfilter/nf_tables_core.c
+++ b/net/netfilter/nf_tables_core.c
@@ -98,21 +98,23 @@ static noinline void nft_update_chain_stats(const struct nft_chain *chain,
 					    const struct nft_pktinfo *pkt)
 {
 	struct nft_base_chain *base_chain;
+	struct nft_stats __percpu *pstats;
 	struct nft_stats *stats;
 
 	base_chain = nft_base_chain(chain);
-	if (!rcu_access_pointer(base_chain->stats))
-		return;
 
-	local_bh_disable();
-	stats = this_cpu_ptr(rcu_dereference(base_chain->stats));
-	if (stats) {
+	rcu_read_lock();
+	pstats = READ_ONCE(base_chain->stats);
+	if (pstats) {
+		local_bh_disable();
+		stats = this_cpu_ptr(pstats);
 		u64_stats_update_begin(&stats->syncp);
 		stats->pkts++;
 		stats->bytes += pkt->skb->len;
 		u64_stats_update_end(&stats->syncp);
+		local_bh_enable();
 	}
-	local_bh_enable();
+	rcu_read_unlock();
 }
 
 struct nft_jumpstack {
-- 
2.11.0



  parent reply	other threads:[~2019-03-02 18:37 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-02 18:37 [PATCH 20/29] netfilter: nft_tunnel: Add dst_cache support Pablo Neira Ayuso
2019-03-02 18:37 ` [PATCH 21/29] netfilter: convert the proto argument from u8 to u16 Pablo Neira Ayuso
2019-03-02 18:37 ` [PATCH 22/29] ipvs: get sctphdr by sctphoff in sctp_csum_check Pablo Neira Ayuso
2019-03-02 18:37 ` [PATCH 23/29] netfilter: bridge: Don't sabotage nf_hook calls for an l3mdev slave Pablo Neira Ayuso
2019-03-02 18:37 ` Pablo Neira Ayuso [this message]
2019-03-02 18:37 ` [PATCH 25/29] netfilter: nf_conntrack: ensure that CONNTRACK_LOCKS is power of 2 Pablo Neira Ayuso
2019-03-03  8:27   ` Sergei Shtylyov
2019-03-02 18:37 ` [PATCH 26/29] netfilter: xt_IDLETIMER: fix sysfs callback function type Pablo Neira Ayuso
2019-03-02 18:37 ` [PATCH 27/29] netfilter: nf_tables: nat: merge nft_redir protocol specific modules Pablo Neira Ayuso
2019-03-02 18:37 ` [PATCH 28/29] netfilter: nf_tables: nat: merge nft_masq " Pablo Neira Ayuso
2019-03-02 18:37 ` [PATCH 29/29] netfilter: nf_tables: merge ipv4 and ipv6 nat chain types Pablo Neira Ayuso
2020-07-05 12:38   ` Reindl Harald

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190302183720.3220-5-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).