Skip to content

Commit d432f7b

Browse files
committed
Merge tag 'nf-24-04-04' of git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf
Pablo Neira Ayuso says: ==================== Netfilter fixes for net The following patchset contains Netfilter fixes for net: Patch #1 unlike early commit path stage which triggers a call to abort, an explicit release of the batch is required on abort, otherwise mutex is released and commit_list remains in place. Patch #2 release mutex after nft_gc_seq_end() in commit path, otherwise async GC worker could collect expired objects. Patch #3 flush pending destroy work in module removal path, otherwise UaF is possible. Patch #4 and #6 restrict the table dormant flag with basechain updates to fix state inconsistency in the hook registration. Patch #5 adds missing RCU read side lock to flowtable type to avoid races with module removal. * tag 'nf-24-04-04' of git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf: netfilter: nf_tables: discard table flag update with pending basechain deletion netfilter: nf_tables: Fix potential data-race in __nft_flowtable_type_get() netfilter: nf_tables: reject new basechain after table flag update netfilter: nf_tables: flush pending destroy work before exit_net release netfilter: nf_tables: release mutex after nft_gc_seq_end from abort path netfilter: nf_tables: release batch on table validation from abort path ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents a66323e + 1bc83a0 commit d432f7b

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

net/netfilter/nf_tables_api.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,11 @@ static bool nft_table_pending_update(const struct nft_ctx *ctx)
12091209
return true;
12101210

12111211
list_for_each_entry(trans, &nft_net->commit_list, list) {
1212-
if ((trans->msg_type == NFT_MSG_NEWCHAIN ||
1213-
trans->msg_type == NFT_MSG_DELCHAIN) &&
1214-
trans->ctx.table == ctx->table &&
1215-
nft_trans_chain_update(trans))
1212+
if (trans->ctx.table == ctx->table &&
1213+
((trans->msg_type == NFT_MSG_NEWCHAIN &&
1214+
nft_trans_chain_update(trans)) ||
1215+
(trans->msg_type == NFT_MSG_DELCHAIN &&
1216+
nft_is_base_chain(trans->ctx.chain))))
12161217
return true;
12171218
}
12181219

@@ -2449,6 +2450,9 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
24492450
struct nft_stats __percpu *stats = NULL;
24502451
struct nft_chain_hook hook = {};
24512452

2453+
if (table->flags & __NFT_TABLE_F_UPDATE)
2454+
return -EINVAL;
2455+
24522456
if (flags & NFT_CHAIN_BINDING)
24532457
return -EOPNOTSUPP;
24542458

@@ -8293,11 +8297,12 @@ static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,
82938297
return err;
82948298
}
82958299

8300+
/* call under rcu_read_lock */
82968301
static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family)
82978302
{
82988303
const struct nf_flowtable_type *type;
82998304

8300-
list_for_each_entry(type, &nf_tables_flowtables, list) {
8305+
list_for_each_entry_rcu(type, &nf_tables_flowtables, list) {
83018306
if (family == type->family)
83028307
return type;
83038308
}
@@ -8309,9 +8314,13 @@ nft_flowtable_type_get(struct net *net, u8 family)
83098314
{
83108315
const struct nf_flowtable_type *type;
83118316

8317+
rcu_read_lock();
83128318
type = __nft_flowtable_type_get(family);
8313-
if (type != NULL && try_module_get(type->owner))
8319+
if (type != NULL && try_module_get(type->owner)) {
8320+
rcu_read_unlock();
83148321
return type;
8322+
}
8323+
rcu_read_unlock();
83158324

83168325
lockdep_nfnl_nft_mutex_not_held();
83178326
#ifdef CONFIG_MODULES
@@ -10455,10 +10464,11 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
1045510464
struct nft_trans *trans, *next;
1045610465
LIST_HEAD(set_update_list);
1045710466
struct nft_trans_elem *te;
10467+
int err = 0;
1045810468

1045910469
if (action == NFNL_ABORT_VALIDATE &&
1046010470
nf_tables_validate(net) < 0)
10461-
return -EAGAIN;
10471+
err = -EAGAIN;
1046210472

1046310473
list_for_each_entry_safe_reverse(trans, next, &nft_net->commit_list,
1046410474
list) {
@@ -10650,12 +10660,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
1065010660
nf_tables_abort_release(trans);
1065110661
}
1065210662

10653-
if (action == NFNL_ABORT_AUTOLOAD)
10654-
nf_tables_module_autoload(net);
10655-
else
10656-
nf_tables_module_autoload_cleanup(net);
10657-
10658-
return 0;
10663+
return err;
1065910664
}
1066010665

1066110666
static int nf_tables_abort(struct net *net, struct sk_buff *skb,
@@ -10668,6 +10673,17 @@ static int nf_tables_abort(struct net *net, struct sk_buff *skb,
1066810673
gc_seq = nft_gc_seq_begin(nft_net);
1066910674
ret = __nf_tables_abort(net, action);
1067010675
nft_gc_seq_end(nft_net, gc_seq);
10676+
10677+
WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
10678+
10679+
/* module autoload needs to happen after GC sequence update because it
10680+
* temporarily releases and grabs mutex again.
10681+
*/
10682+
if (action == NFNL_ABORT_AUTOLOAD)
10683+
nf_tables_module_autoload(net);
10684+
else
10685+
nf_tables_module_autoload_cleanup(net);
10686+
1067110687
mutex_unlock(&nft_net->commit_mutex);
1067210688

1067310689
return ret;
@@ -11473,9 +11489,10 @@ static void __net_exit nf_tables_exit_net(struct net *net)
1147311489

1147411490
gc_seq = nft_gc_seq_begin(nft_net);
1147511491

11476-
if (!list_empty(&nft_net->commit_list) ||
11477-
!list_empty(&nft_net->module_list))
11478-
__nf_tables_abort(net, NFNL_ABORT_NONE);
11492+
WARN_ON_ONCE(!list_empty(&nft_net->commit_list));
11493+
11494+
if (!list_empty(&nft_net->module_list))
11495+
nf_tables_module_autoload_cleanup(net);
1147911496

1148011497
__nft_release_tables(net);
1148111498

@@ -11567,6 +11584,7 @@ static void __exit nf_tables_module_exit(void)
1156711584
unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
1156811585
nft_chain_filter_fini();
1156911586
nft_chain_route_fini();
11587+
nf_tables_trans_destroy_flush_work();
1157011588
unregister_pernet_subsys(&nf_tables_net_ops);
1157111589
cancel_work_sync(&trans_gc_work);
1157211590
cancel_work_sync(&trans_destroy_work);

0 commit comments

Comments
 (0)