Skip to content

Commit cee05c0

Browse files
TaeheeYoogregkh
authored andcommitted
netfilter: nf_tables: fix suspicious RCU usage in nft_chain_stats_replace()
[ Upstream commit 4c05ec4 ] basechain->stats is rcu protected data which is updated from nft_chain_stats_replace(). This function is executed from the commit phase which holds the pernet nf_tables commit mutex - not the global nfnetlink subsystem mutex. Test commands to reproduce the problem are: %iptables-nft -I INPUT %iptables-nft -Z %iptables-nft -Z This patch uses RCU calls to handle basechain->stats updates to fix a splat that looks like: [89279.358755] ============================= [89279.363656] WARNING: suspicious RCU usage [89279.368458] 4.20.0-rc2+ #44 Tainted: G W L [89279.374661] ----------------------------- [89279.379542] net/netfilter/nf_tables_api.c:1404 suspicious rcu_dereference_protected() usage! [...] [89279.406556] 1 lock held by iptables-nft/5225: [89279.411728] #0: 00000000bf45a000 (&net->nft.commit_mutex){+.+.}, at: nf_tables_valid_genid+0x1f/0x70 [nf_tables] [89279.424022] stack backtrace: [89279.429236] CPU: 0 PID: 5225 Comm: iptables-nft Tainted: G W L 4.20.0-rc2+ #44 [89279.430135] Call Trace: [89279.430135] dump_stack+0xc9/0x16b [89279.430135] ? show_regs_print_info+0x5/0x5 [89279.430135] ? lockdep_rcu_suspicious+0x117/0x160 [89279.430135] nft_chain_commit_update+0x4ea/0x640 [nf_tables] [89279.430135] ? sched_clock_local+0xd4/0x140 [89279.430135] ? check_flags.part.35+0x440/0x440 [89279.430135] ? __rhashtable_remove_fast.constprop.67+0xec0/0xec0 [nf_tables] [89279.430135] ? sched_clock_cpu+0x126/0x170 [89279.430135] ? find_held_lock+0x39/0x1c0 [89279.430135] ? hlock_class+0x140/0x140 [89279.430135] ? is_bpf_text_address+0x5/0xf0 [89279.430135] ? check_flags.part.35+0x440/0x440 [89279.430135] ? __lock_is_held+0xb4/0x140 [89279.430135] nf_tables_commit+0x2555/0x39c0 [nf_tables] Fixes: f102d66 ("netfilter: nf_tables: use dedicated mutex to guard transactions") Signed-off-by: Taehee Yoo <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 537e9a8 commit cee05c0

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

include/linux/netfilter/nfnetlink.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ static inline bool lockdep_nfnl_is_held(__u8 subsys_id)
6262
}
6363
#endif /* CONFIG_PROVE_LOCKING */
6464

65-
/*
66-
* nfnl_dereference - fetch RCU pointer when updates are prevented by subsys mutex
67-
*
68-
* @p: The pointer to read, prior to dereferencing
69-
* @ss: The nfnetlink subsystem ID
70-
*
71-
* Return the value of the specified RCU-protected pointer, but omit
72-
* the READ_ONCE(), because caller holds the NFNL subsystem mutex.
73-
*/
74-
#define nfnl_dereference(p, ss) \
75-
rcu_dereference_protected(p, lockdep_nfnl_is_held(ss))
76-
7765
#define MODULE_ALIAS_NFNL_SUBSYS(subsys) \
7866
MODULE_ALIAS("nfnetlink-subsys-" __stringify(subsys))
7967

net/netfilter/nf_tables_api.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,8 @@ static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,
11991199
if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name))
12001200
goto nla_put_failure;
12011201

1202-
if (basechain->stats && nft_dump_stats(skb, basechain->stats))
1202+
if (rcu_access_pointer(basechain->stats) &&
1203+
nft_dump_stats(skb, rcu_dereference(basechain->stats)))
12031204
goto nla_put_failure;
12041205
}
12051206

@@ -1375,16 +1376,18 @@ static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr)
13751376
return newstats;
13761377
}
13771378

1378-
static void nft_chain_stats_replace(struct nft_base_chain *chain,
1379+
static void nft_chain_stats_replace(struct net *net,
1380+
struct nft_base_chain *chain,
13791381
struct nft_stats __percpu *newstats)
13801382
{
13811383
struct nft_stats __percpu *oldstats;
13821384

13831385
if (newstats == NULL)
13841386
return;
13851387

1386-
if (chain->stats) {
1387-
oldstats = nfnl_dereference(chain->stats, NFNL_SUBSYS_NFTABLES);
1388+
if (rcu_access_pointer(chain->stats)) {
1389+
oldstats = rcu_dereference_protected(chain->stats,
1390+
lockdep_commit_lock_is_held(net));
13881391
rcu_assign_pointer(chain->stats, newstats);
13891392
synchronize_rcu();
13901393
free_percpu(oldstats);
@@ -1421,9 +1424,10 @@ static void nf_tables_chain_destroy(struct nft_ctx *ctx)
14211424
struct nft_base_chain *basechain = nft_base_chain(chain);
14221425

14231426
module_put(basechain->type->owner);
1424-
free_percpu(basechain->stats);
1425-
if (basechain->stats)
1427+
if (rcu_access_pointer(basechain->stats)) {
14261428
static_branch_dec(&nft_counters_enabled);
1429+
free_percpu(rcu_dereference_raw(basechain->stats));
1430+
}
14271431
kfree(chain->name);
14281432
kfree(basechain);
14291433
} else {
@@ -1572,7 +1576,7 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
15721576
kfree(basechain);
15731577
return PTR_ERR(stats);
15741578
}
1575-
basechain->stats = stats;
1579+
rcu_assign_pointer(basechain->stats, stats);
15761580
static_branch_inc(&nft_counters_enabled);
15771581
}
15781582

@@ -6145,7 +6149,8 @@ static void nft_chain_commit_update(struct nft_trans *trans)
61456149
return;
61466150

61476151
basechain = nft_base_chain(trans->ctx.chain);
6148-
nft_chain_stats_replace(basechain, nft_trans_chain_stats(trans));
6152+
nft_chain_stats_replace(trans->ctx.net, basechain,
6153+
nft_trans_chain_stats(trans));
61496154

61506155
switch (nft_trans_chain_policy(trans)) {
61516156
case NF_DROP:

net/netfilter/nf_tables_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static noinline void nft_update_chain_stats(const struct nft_chain *chain,
101101
struct nft_stats *stats;
102102

103103
base_chain = nft_base_chain(chain);
104-
if (!base_chain->stats)
104+
if (!rcu_access_pointer(base_chain->stats))
105105
return;
106106

107107
local_bh_disable();

0 commit comments

Comments
 (0)