Skip to content

Commit 2991dc3

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 558f503 commit 2991dc3

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
@@ -962,7 +962,6 @@ struct nft_chain {
962962
char *name;
963963
u16 udlen;
964964
u8 *udata;
965-
struct rcu_head rcu_head;
966965

967966
/* Only used during control plane commit phase: */
968967
struct nft_rule **rules_next;
@@ -1101,7 +1100,6 @@ static inline void nft_use_inc_restore(u32 *use)
11011100
* @sets: sets in the table
11021101
* @objects: stateful objects in the table
11031102
* @flowtables: flow tables in the table
1104-
* @net: netnamespace this table belongs to
11051103
* @hgenerator: handle generator state
11061104
* @handle: table handle
11071105
* @use: number of chain references to this table
@@ -1117,7 +1115,6 @@ struct nft_table {
11171115
struct list_head sets;
11181116
struct list_head objects;
11191117
struct list_head flowtables;
1120-
possible_net_t net;
11211118
u64 hgenerator;
11221119
u64 handle;
11231120
u32 use;

net/netfilter/nf_tables_api.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,6 @@ static int nf_tables_newtable(struct net *net, struct sock *nlsk,
12991299
INIT_LIST_HEAD(&table->sets);
13001300
INIT_LIST_HEAD(&table->objects);
13011301
INIT_LIST_HEAD(&table->flowtables);
1302-
write_pnet(&table->net, net);
13031302
table->family = family;
13041303
table->flags = flags;
13051304
table->handle = ++table_handle;
@@ -3345,8 +3344,11 @@ void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule)
33453344
kfree(rule);
33463345
}
33473346

3347+
/* can only be used if rule is no longer visible to dumps */
33483348
static void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule)
33493349
{
3350+
lockdep_commit_lock_is_held(ctx->net);
3351+
33503352
nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE);
33513353
nf_tables_rule_destroy(ctx, rule);
33523354
}
@@ -4858,6 +4860,8 @@ void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
48584860
struct nft_set_binding *binding,
48594861
enum nft_trans_phase phase)
48604862
{
4863+
lockdep_commit_lock_is_held(ctx->net);
4864+
48614865
switch (phase) {
48624866
case NFT_TRANS_PREPARE_ERROR:
48634867
nft_set_trans_unbind(ctx, set);
@@ -9571,19 +9575,6 @@ static void __nft_release_basechain_now(struct nft_ctx *ctx)
95719575
nf_tables_chain_destroy(ctx->chain);
95729576
}
95739577

9574-
static void nft_release_basechain_rcu(struct rcu_head *head)
9575-
{
9576-
struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
9577-
struct nft_ctx ctx = {
9578-
.family = chain->table->family,
9579-
.chain = chain,
9580-
.net = read_pnet(&chain->table->net),
9581-
};
9582-
9583-
__nft_release_basechain_now(&ctx);
9584-
put_net(ctx.net);
9585-
}
9586-
95879578
int __nft_release_basechain(struct nft_ctx *ctx)
95889579
{
95899580
struct nft_rule *rule;
@@ -9598,11 +9589,18 @@ int __nft_release_basechain(struct nft_ctx *ctx)
95989589
nft_chain_del(ctx->chain);
95999590
nft_use_dec(&ctx->table->use);
96009591

9601-
if (maybe_get_net(ctx->net))
9602-
call_rcu(&ctx->chain->rcu_head, nft_release_basechain_rcu);
9603-
else
9592+
if (!maybe_get_net(ctx->net)) {
96049593
__nft_release_basechain_now(ctx);
9594+
return 0;
9595+
}
9596+
9597+
/* wait for ruleset dumps to complete. Owning chain is no longer in
9598+
* lists, so new dumps can't find any of these rules anymore.
9599+
*/
9600+
synchronize_rcu();
96059601

9602+
__nft_release_basechain_now(ctx);
9603+
put_net(ctx->net);
96069604
return 0;
96079605
}
96089606
EXPORT_SYMBOL_GPL(__nft_release_basechain);

0 commit comments

Comments
 (0)