Skip to content

Commit 27f0574

Browse files
Florian Westphalgregkh
authored andcommitted
netfilter: nf_tables: do not defer rule destruction via call_rcu
[ Upstream commit b04df3d ] nf_tables_chain_destroy can sleep, it can't be used from call_rcu callbacks. Moreover, nf_tables_rule_release() is only safe for error unwinding, while transaction mutex is held and the to-be-desroyed rule was not exposed to either dataplane or dumps, as it deactives+frees without the required synchronize_rcu() in-between. nft_rule_expr_deactivate() callbacks will change ->use counters of other chains/sets, see e.g. nft_lookup .deactivate callback, these must be serialized via transaction mutex. Also add a few lockdep asserts to make this more explicit. Calling synchronize_rcu() isn't ideal, but fixing this without is hard and way more intrusive. As-is, we can get: WARNING: .. net/netfilter/nf_tables_api.c:5515 nft_set_destroy+0x.. Workqueue: events nf_tables_trans_destroy_work RIP: 0010:nft_set_destroy+0x3fe/0x5c0 Call Trace: <TASK> nf_tables_trans_destroy_work+0x6b7/0xad0 process_one_work+0x64a/0xce0 worker_thread+0x613/0x10d0 In case the synchronize_rcu becomes an issue, we can explore alternatives. One way would be to allocate nft_trans_rule objects + one nft_trans_chain object, deactivate the rules + the chain and then defer the freeing to the nft destroy workqueue. We'd still need to keep the synchronize_rcu path as a fallback to handle -ENOMEM corner cases though. Reported-by: [email protected] Closes: https://lore.kernel.org/all/[email protected]/T/ Fixes: c03d278 ("netfilter: nf_tables: wait for rcu grace period on net_device removal") Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 8c2c844 commit 27f0574

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,6 @@ struct nft_rule_blob {
10801080
* @name: name of the chain
10811081
* @udlen: user data length
10821082
* @udata: user data in the chain
1083-
* @rcu_head: rcu head for deferred release
10841083
* @blob_next: rule blob pointer to the next in the chain
10851084
*/
10861085
struct nft_chain {
@@ -1098,7 +1097,6 @@ struct nft_chain {
10981097
char *name;
10991098
u16 udlen;
11001099
u8 *udata;
1101-
struct rcu_head rcu_head;
11021100

11031101
/* Only used during control plane commit phase: */
11041102
struct nft_rule_blob *blob_next;
@@ -1242,7 +1240,6 @@ static inline void nft_use_inc_restore(u32 *use)
12421240
* @sets: sets in the table
12431241
* @objects: stateful objects in the table
12441242
* @flowtables: flow tables in the table
1245-
* @net: netnamespace this table belongs to
12461243
* @hgenerator: handle generator state
12471244
* @handle: table handle
12481245
* @use: number of chain references to this table
@@ -1259,7 +1256,6 @@ struct nft_table {
12591256
struct list_head sets;
12601257
struct list_head objects;
12611258
struct list_head flowtables;
1262-
possible_net_t net;
12631259
u64 hgenerator;
12641260
u64 handle;
12651261
u32 use;

net/netfilter/nf_tables_api.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,6 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,
14311431
INIT_LIST_HEAD(&table->sets);
14321432
INIT_LIST_HEAD(&table->objects);
14331433
INIT_LIST_HEAD(&table->flowtables);
1434-
write_pnet(&table->net, net);
14351434
table->family = family;
14361435
table->flags = flags;
14371436
table->handle = ++nft_net->table_handle;
@@ -3784,8 +3783,11 @@ void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule)
37843783
kfree(rule);
37853784
}
37863785

3786+
/* can only be used if rule is no longer visible to dumps */
37873787
static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule)
37883788
{
3789+
lockdep_commit_lock_is_held(ctx->net);
3790+
37893791
nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
37903792
nf_tables_rule_destroy(ctx, rule);
37913793
}
@@ -5561,6 +5563,8 @@ void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
55615563
struct nft_set_binding *binding,
55625564
enum nft_trans_phase phase)
55635565
{
5566+
lockdep_commit_lock_is_held(ctx->net);
5567+
55645568
switch (phase) {
55655569
case NFT_TRANS_PREPARE_ERROR:
55665570
nft_set_trans_unbind(ctx, set);
@@ -11182,19 +11186,6 @@ static void __nft_release_basechain_now(struct nft_ctx *ctx)
1118211186
nf_tables_chain_destroy(ctx->chain);
1118311187
}
1118411188

11185-
static void nft_release_basechain_rcu(struct rcu_head *head)
11186-
{
11187-
struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
11188-
struct nft_ctx ctx = {
11189-
.family = chain->table->family,
11190-
.chain = chain,
11191-
.net = read_pnet(&chain->table->net),
11192-
};
11193-
11194-
__nft_release_basechain_now(&ctx);
11195-
put_net(ctx.net);
11196-
}
11197-
1119811189
int __nft_release_basechain(struct nft_ctx *ctx)
1119911190
{
1120011191
struct nft_rule *rule;
@@ -11209,11 +11200,18 @@ int __nft_release_basechain(struct nft_ctx *ctx)
1120911200
nft_chain_del(ctx->chain);
1121011201
nft_use_dec(&ctx->table->use);
1121111202

11212-
if (maybe_get_net(ctx->net))
11213-
call_rcu(&ctx->chain->rcu_head, nft_release_basechain_rcu);
11214-
else
11203+
if (!maybe_get_net(ctx->net)) {
1121511204
__nft_release_basechain_now(ctx);
11205+
return 0;
11206+
}
11207+
11208+
/* wait for ruleset dumps to complete. Owning chain is no longer in
11209+
* lists, so new dumps can't find any of these rules anymore.
11210+
*/
11211+
synchronize_rcu();
1121611212

11213+
__nft_release_basechain_now(ctx);
11214+
put_net(ctx->net);
1121711215
return 0;
1121811216
}
1121911217
EXPORT_SYMBOL_GPL(__nft_release_basechain);

0 commit comments

Comments
 (0)