Skip to content

Commit b0f013b

Browse files
Florian Westphalgregkh
authored andcommitted
netfilter: nf_tables: do not defer rule destruction via call_rcu
commit b04df3da1b5c6f6dc7cdccc37941740c078c4043 upstream. 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: c03d278fdf35 ("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: Greg Kroah-Hartman <[email protected]>
1 parent e6c32a6 commit b0f013b

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,6 @@ struct nft_chain {
10621062
char *name;
10631063
u16 udlen;
10641064
u8 *udata;
1065-
struct rcu_head rcu_head;
10661065

10671066
/* Only used during control plane commit phase: */
10681067
struct nft_rule_blob *blob_next;
@@ -1205,7 +1204,6 @@ static inline void nft_use_inc_restore(u32 *use)
12051204
* @sets: sets in the table
12061205
* @objects: stateful objects in the table
12071206
* @flowtables: flow tables in the table
1208-
* @net: netnamespace this table belongs to
12091207
* @hgenerator: handle generator state
12101208
* @handle: table handle
12111209
* @use: number of chain references to this table
@@ -1221,7 +1219,6 @@ struct nft_table {
12211219
struct list_head sets;
12221220
struct list_head objects;
12231221
struct list_head flowtables;
1224-
possible_net_t net;
12251222
u64 hgenerator;
12261223
u64 handle;
12271224
u32 use;

net/netfilter/nf_tables_api.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,6 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,
14131413
INIT_LIST_HEAD(&table->sets);
14141414
INIT_LIST_HEAD(&table->objects);
14151415
INIT_LIST_HEAD(&table->flowtables);
1416-
write_pnet(&table->net, net);
14171416
table->family = family;
14181417
table->flags = flags;
14191418
table->handle = ++nft_net->table_handle;
@@ -3511,8 +3510,11 @@ void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule)
35113510
kfree(rule);
35123511
}
35133512

3513+
/* can only be used if rule is no longer visible to dumps */
35143514
static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule)
35153515
{
3516+
lockdep_commit_lock_is_held(ctx->net);
3517+
35163518
nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
35173519
nf_tables_rule_destroy(ctx, rule);
35183520
}
@@ -5248,6 +5250,8 @@ void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
52485250
struct nft_set_binding *binding,
52495251
enum nft_trans_phase phase)
52505252
{
5253+
lockdep_commit_lock_is_held(ctx->net);
5254+
52515255
switch (phase) {
52525256
case NFT_TRANS_PREPARE_ERROR:
52535257
nft_set_trans_unbind(ctx, set);
@@ -10674,19 +10678,6 @@ static void __nft_release_basechain_now(struct nft_ctx *ctx)
1067410678
nf_tables_chain_destroy(ctx->chain);
1067510679
}
1067610680

10677-
static void nft_release_basechain_rcu(struct rcu_head *head)
10678-
{
10679-
struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
10680-
struct nft_ctx ctx = {
10681-
.family = chain->table->family,
10682-
.chain = chain,
10683-
.net = read_pnet(&chain->table->net),
10684-
};
10685-
10686-
__nft_release_basechain_now(&ctx);
10687-
put_net(ctx.net);
10688-
}
10689-
1069010681
int __nft_release_basechain(struct nft_ctx *ctx)
1069110682
{
1069210683
struct nft_rule *rule;
@@ -10701,11 +10692,18 @@ int __nft_release_basechain(struct nft_ctx *ctx)
1070110692
nft_chain_del(ctx->chain);
1070210693
nft_use_dec(&ctx->table->use);
1070310694

10704-
if (maybe_get_net(ctx->net))
10705-
call_rcu(&ctx->chain->rcu_head, nft_release_basechain_rcu);
10706-
else
10695+
if (!maybe_get_net(ctx->net)) {
1070710696
__nft_release_basechain_now(ctx);
10697+
return 0;
10698+
}
10699+
10700+
/* wait for ruleset dumps to complete. Owning chain is no longer in
10701+
* lists, so new dumps can't find any of these rules anymore.
10702+
*/
10703+
synchronize_rcu();
1070810704

10705+
__nft_release_basechain_now(ctx);
10706+
put_net(ctx->net);
1070910707
return 0;
1071010708
}
1071110709
EXPORT_SYMBOL_GPL(__nft_release_basechain);

0 commit comments

Comments
 (0)