netfilter: nf_tables: fix chain binding transaction logic
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/6 netfilter: nf_tables: fix chain binding transaction logic
Add bound flag to chain transaction as in 6a0a8d10a366 ("netfilter:nf_tables: use-after-free in failing rule with bound set") to skip it in case the chain is already bound from the abort path.
This patch fixes an imbalance in the chain use refcnt that triggers a WARN_ON on the table and chain destroy path.
This patch also disallows nested chain bindings, which is not supported from userspace.
The logic to deal with chain binding in nft_data_hold() and nft_data_release() is not correct. The NFT_TRANS_PREPARE state needs a special handling in case a chain is bound but next expressions in the same rule fail to initialize as described by 1240eb93f061 ("netfilter:nf_tables: incorrect error path handling with NFT_MSG_NEWRULE").
--- include/net/netfilter/nf_tables.h | 13 +++++++ net/netfilter/nf_tables_api.c | 65 ++++++++++++++++++++----------- net/netfilter/nft_immediate.c | 56 ++++++++++++++++++++++---- 3 files changed, 104 insertions(+), 30 deletions(-)
include/net/netfilter/nf_tables.h +13 -0
@@ -1104,6 +1104,7 @@ int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,const struct nft_set_iter *iter, struct nft_set_elem *elem);int nft_set_catchall_validate (const struct nft_ctx *ctx,struct nft_set *set); +int nf_tables_bind_chain (const struct nft_ctx *ctx,struct nft_chain *chain);enum nft_chain_types {NFT_CHAIN_T_DEFAULT =0 ,
@@ -1140,11 +1141,17 @@ int nft_chain_validate_dependency(const struct nft_chain *chain,int nft_chain_validate_hooks (const struct nft_chain *chain,unsigned int hook_flags); +static inline bool nft_chain_binding (const struct nft_chain *chain) +{ +return chain->flags &NFT_CHAIN_BINDING ; +} +static inline bool nft_chain_is_bound (struct nft_chain *chain) {return (chain->flags &NFT_CHAIN_BINDING ) && chain->bound; } +int nft_chain_add (struct nft_table *table,struct nft_chain *chain);void nft_chain_del (struct nft_chain *chain);void nf_tables_chain_destroy (struct nft_ctx *ctx);
@@ -1607,15 +1614,19 @@ struct nft_trans_set { (((struct nft_trans_set *)trans->data)->gc_int)struct nft_trans_chain { +struct nft_chain *chain;bool update;char *name;struct nft_stats __percpu *stats;u8 policy; +bool bound;u32 chain_id;struct nft_base_chain *basechain;struct list_head hook_list; }; +#define nft_trans_chain (trans) \ + (((struct nft_trans_chain *)trans->data)->chain)#define nft_trans_chain_update (trans) \ (((struct nft_trans_chain *)trans->data)->update)#define nft_trans_chain_name (trans) \
@@ -1624,6 +1635,8 @@ struct nft_trans_chain { (((struct nft_trans_chain *)trans->data)->stats)#define nft_trans_chain_policy (trans) \ (((struct nft_trans_chain *)trans->data)->policy) +#define nft_trans_chain_bound (trans) \ + (((struct nft_trans_chain *)trans->data)->bound)#define nft_trans_chain_id (trans) \ (((struct nft_trans_chain *)trans->data)->chain_id)#define nft_trans_basechain (trans) \
net/netfilter/nf_tables_api.c +42 -23
114 lines not on this page · raw message
net/netfilter/nft_immediate.c +49 -7
81 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/6 netfilter: nf_tables: disallow element updates of bound anonymous sets
Anonymous sets come with NFT_SET_CONSTANT from userspace. Although API allows to create anonymous sets without NFT_SET_CONSTANT, it makes no sense to allow to add and to delete elements for bound anonymous sets.
--- net/netfilter/nf_tables_api.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
net/netfilter/nf_tables_api.c +5 -2
@@ -6644,7 +6644,8 @@ static int nf_tables_newsetelem(struct sk_buff *skb,if (IS_ERR (set))return PTR_ERR (set); -if (!list_empty (&set->bindings) && set->flags &NFT_SET_CONSTANT ) +if (!list_empty (&set->bindings) && + (set->flags & (NFT_SET_CONSTANT |NFT_SET_ANONYMOUS )))return -EBUSY ;nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla);
@@ -6918,7 +6919,9 @@ static int nf_tables_delsetelem(struct sk_buff *skb, set =nft_set_lookup (table, nla[NFTA_SET_ELEM_LIST_SET ], genmask);if (IS_ERR (set))return PTR_ERR (set); -if (!list_empty (&set->bindings) && set->flags &NFT_SET_CONSTANT ) + +if (!list_empty (&set->bindings) && + (set->flags & (NFT_SET_CONSTANT |NFT_SET_ANONYMOUS )))return -EBUSY ;nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/6 netfilter: nf_tables: reject unbound anonymous set before commit phase
In reply to Pablo Neira AyusoAdd a new list to track set transaction and to check for unbound anonymous sets before entering the commit phase.
Bail out at the end of the transaction handling if an anonymous set remains unbound.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 35 ++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -1572,6 +1572,7 @@ static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)*/ struct nft_trans {struct list_head list; +struct list_head binding_list;int msg_type;bool put_net;struct nft_ctx ctx;
@@ -1713,6 +1714,7 @@ static inline int nft_request_module(struct net *net, const char *fmt, ...) { restruct nftables_pernet {struct list_head tables;struct list_head commit_list; +struct list_head binding_list;struct list_head module_list;struct list_head notify_list;struct mutex commit_mutex;
net/netfilter/nf_tables_api.c +32 -3
@@ -151,6 +151,7 @@ static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx,return NULL ;INIT_LIST_HEAD (&trans->list); +INIT_LIST_HEAD (&trans->binding_list); trans->msg_type = msg_type; trans->ctx = *ctx;
@@ -163,9 +164,15 @@ static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx,return nft_trans_alloc_gfp (ctx, msg_type, size,GFP_KERNEL ); } -static void nft_trans_destroy (struct nft_trans *trans) +static void nft_trans_list_del (struct nft_trans *trans) {list_del (&trans->list); +list_del (&trans->binding_list); +} + +static void nft_trans_destroy (struct nft_trans *trans) +{ +nft_trans_list_del (trans);kfree (trans); }
@@ -330,6 +337,14 @@ static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *tr {struct nftables_pernet *nft_net =nft_pernet (net); +switch (trans->msg_type) { +case NFT_MSG_NEWSET : +if (!nft_trans_set_update (trans) && +nft_set_is_anonymous (nft_trans_set (trans))) +list_add_tail (&trans->binding_list, &nft_net->binding_list); +break ; + } +list_add_tail (&trans->list, &nft_net->commit_list); }
@@ -8975,7 +8990,7 @@ static void nf_tables_trans_destroy_work(struct work_struct *w)synchronize_rcu ();list_for_each_entry_safe (trans, next, &head, list) { -list_del (&trans->list); +nft_trans_list_del (trans);nft_commit_release (trans); } }
@@ -9340,6 +9355,19 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)return 0 ; } +list_for_each_entry (trans, &nft_net->binding_list, binding_list) { +switch (trans->msg_type) { +case NFT_MSG_NEWSET : +if (!nft_trans_set_update (trans) && +nft_set_is_anonymous (nft_trans_set (trans)) && + !nft_trans_set_bound (trans)) { +pr_warn_once ("nftables ruleset with unbound set\n" ); +return -EINVAL ; + } +break ; + } + } +/* 0. Validate ruleset, otherwise roll back for error reporting. */ if (nf_tables_validate (net) <0 )return -EAGAIN ;
@@ -9846,7 +9874,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)list_for_each_entry_safe_reverse (trans, next, &nft_net->commit_list, list) { -list_del (&trans->list); +nft_trans_list_del (trans);nf_tables_abort_release (trans); }
@@ -10619,6 +10647,7 @@ static int __net_init nf_tables_init_net(struct net *net)INIT_LIST_HEAD (&nft_net->tables);INIT_LIST_HEAD (&nft_net->commit_list); +INIT_LIST_HEAD (&nft_net->binding_list);INIT_LIST_HEAD (&nft_net->module_list);INIT_LIST_HEAD (&nft_net->notify_list);mutex_init (&nft_net->commit_mutex);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/6 netfilter: nf_tables: reject unbound chain set before commit phase
In reply to Pablo Neira AyusoUse binding list to track set transaction and to check for unbound chains before entering the commit phase.
Bail out if chain binding remain unused before entering the commit step.
--- net/netfilter/nf_tables_api.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
net/netfilter/nf_tables_api.c +13 -0
@@ -343,6 +343,11 @@ static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *tr nft_set_is_anonymous(nft_trans_set(trans)))list_add_tail (&trans->binding_list, &nft_net->binding_list);break ; +case NFT_MSG_NEWCHAIN : +if (!nft_trans_chain_update (trans) && +nft_chain_binding (nft_trans_chain (trans))) +list_add_tail (&trans->binding_list, &nft_net->binding_list); +break ; }list_add_tail (&trans->list, &nft_net->commit_list);
@@ -9365,6 +9370,14 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)return -EINVAL ; }break ; +case NFT_MSG_NEWCHAIN : +if (!nft_trans_chain_update (trans) && +nft_chain_binding (nft_trans_chain (trans)) && + !nft_trans_chain_bound (trans)) { +pr_warn_once ("nftables ruleset with unbound chain\n" ); +return -EINVAL ; + } +break ; } }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/6 netfilter: nf_tables: disallow updates of anonymous sets
In reply to Pablo Neira AyusoDisallow updates of set timeout and garbage collection parameters for anonymous sets.
--- net/netfilter/nf_tables_api.c | 3 +++ 1 file changed, 3 insertions(+)
net/netfilter/nf_tables_api.c +3 -0
@@ -4890,6 +4890,9 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,if (info->nlh->nlmsg_flags &NLM_F_REPLACE )return -EOPNOTSUPP ; +if (nft_set_is_anonymous (set)) +return -EOPNOTSUPP ; + err =nft_set_expr_alloc (&ctx, set, nla, exprs, &num_exprs, flags);if (err <0 )return err;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 6/6 netfilter: nf_tables: disallow timeout for anonymous sets
In reply to Pablo Neira AyusoNever used from userspace, disallow these parameters.
--- net/netfilter/nf_tables_api.c | 7 +++++++ 1 file changed, 7 insertions(+)
net/netfilter/nf_tables_api.c +7 -0
@@ -4836,6 +4836,9 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,if (!(flags &NFT_SET_TIMEOUT ))return -EINVAL ; +if (flags &NFT_SET_ANONYMOUS ) +return -EOPNOTSUPP ; + err =nf_msecs_to_jiffies64 (nla[NFTA_SET_TIMEOUT ], &desc.timeout);if (err)return err;
@@ -4844,6 +4847,10 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,if (nla[NFTA_SET_GC_INTERVAL ] !=NULL ) {if (!(flags &NFT_SET_TIMEOUT ))return -EINVAL ; + +if (flags &NFT_SET_ANONYMOUS ) +return -EOPNOTSUPP ; + desc.gc_int =ntohl (nla_get_be32 (nla[NFTA_SET_GC_INTERVAL ])); }
Signature · 1 line
2.30.2
Florian Westphal
maintainernet/netfilter/nf_tables_api.c void nft_data_hold(const struct nft_data *data, enum nft_data_types type) · line 6656
Quoted 8 lines · Pablo Neira Ayuso
net/netfilter/nf_tables_api.c +0
-8- -if (!nft_chain_is_bound (chain)) -break ; - - chain->table->use++; -list_for_each_entry (rule, &chain->rules, list) - chain->use++; -
Thanks for removing this, I could not find a reason why the use count increment is required.