Skip to contentSkip to conversation

netfilter: nf_tables: fix chain binding transaction logic

PATCH New Pablo Neira Ayuso 1 participant 3 messages Jun 9, 2023

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/3 netfilter: nf_tables: fix chain binding transaction logic

In case of error from the preparation phase, the deactivate function marks this chain as inactive in the next generation, so it is not reachable anymore from the transaction list. In this case, immediate destroy skips releasing the chain object because the transaction deals with this. From the commit phase, deactivate detaches the chain from the rule, then the immediate destroy function releases now this (unbound) chain.

While at it, set chain->bound flag for NFT_CHAIN_BINDING only by checking for the new nft_chain_binding() helper function.

Fixes d0e2c7de92c7 ("netfilter: nf_tables: add NFT_CHAIN_BINDING")
---
 include/net/netfilter/nf_tables.h |  5 +++
 net/netfilter/nft_immediate.c     | 52 ++++++++++++++++++++++++++++---
 2 files changed, 52 insertions(+), 5 deletions(-)
include/net/netfilter/nf_tables.h +5 -0
index 83db182decc8..66e5c7a8ec21 100644
@@ -1140,6 +1140,11 @@ 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;
net/netfilter/nft_immediate.c +47 -5

81 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/3 netfilter: nf_tables: disallow unbound anonymous set from commit step

Bail out if userspace creates an anonymous set which remains unbound after this transaction.

Add a list of pending objects, this list is never used in iterations as it stores object of different type.

---
 include/net/netfilter/nf_tables.h |  3 +++
 net/netfilter/nf_tables_api.c     | 17 +++++++++++++++++
 2 files changed, 20 insertions(+)
include/net/netfilter/nf_tables.h +3 -0
index 66e5c7a8ec21..d24146b526a1 100644
@@ -511,6 +511,7 @@ struct nft_set_elem_expr {
  *
  *	@list: table set list node
  *	@bindings: list of set bindings
+ *	@pending_list: list of pending anonymous set not yet bound to rule
  *	@table: table this set belongs to
  *	@net: netnamespace this set belongs to
  * 	@name: name of the set
@@ -540,6 +541,7 @@ struct nft_set_elem_expr {
 struct nft_set {
 	struct list_head		list;
 	struct list_head		bindings;
+	struct list_head		pending_list;
 	struct nft_table		*table;
 	possible_net_t			net;
 	char				*name;
@@ -1707,6 +1709,7 @@ struct nftables_pernet {
 	struct list_head	commit_list;
 	struct list_head	module_list;
 	struct list_head	notify_list;
+	struct list_head	pending_list;
 	struct mutex		commit_mutex;
 	u64			table_handle;
 	unsigned int		base_seq;
net/netfilter/nf_tables_api.c +17 -0
index 69bceefaa5c8..11beb6750531 100644
@@ -4926,6 +4926,12 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info,
 	if (err < 0)
 		goto err_set_expr_alloc;
 
+	if (nft_set_is_anonymous(set)) {
+		struct nftables_pernet *nft_net = nft_pernet(net);
+
+		list_add_tail(&set->pending_list, &nft_net->pending_list);
+	}
+
 	list_add_tail_rcu(&set->list, &table->sets);
 	table->use++;
 	return 0;
@@ -5108,6 +5114,9 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
 	}
 bind:
 	binding->chain = ctx->chain;
+	if (nft_set_is_anonymous(set))
+		list_del(&set->pending_list);
+
 	list_add_tail_rcu(&binding->list, &set->bindings);
 	nft_set_trans_bind(ctx, set);
 	set->use++;
@@ -8792,6 +8801,11 @@ static int nf_tables_validate(struct net *net)
 	struct nftables_pernet *nft_net = nft_pernet(net);
 	struct nft_table *table;
 
+	if (!list_empty(&nft_net->pending_list)) {
+		pr_warn_once("rejecting nftables ruleset with pending objects\n");
+		return -EINVAL;
+	}
+
 	list_for_each_entry(table, &nft_net->tables, list) {
 		switch (table->validate_state) {
 		case NFT_VALIDATE_SKIP:
@@ -9727,6 +9741,8 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
 			if (nft_trans_set_bound(trans)) {
 				nft_trans_destroy(trans);
 				break;
+			} else if (nft_set_is_anonymous(nft_trans_set(trans))) {
+				list_del(&nft_trans_set(trans)->pending_list);
 			}
 			list_del_rcu(&nft_trans_set(trans)->list);
 			break;
@@ -10599,6 +10615,7 @@ static int __net_init nf_tables_init_net(struct net *net)
 	INIT_LIST_HEAD(&nft_net->commit_list);
 	INIT_LIST_HEAD(&nft_net->module_list);
 	INIT_LIST_HEAD(&nft_net->notify_list);
+	INIT_LIST_HEAD(&nft_net->pending_list);
 	mutex_init(&nft_net->commit_mutex);
 	nft_net->base_seq = 1;
 
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH 3/3 netfilter: nf_tables: disallow unbound chain from commit step

In reply to Pablo Neira Ayuso

Bail out if userspace creates a chain binding which remains unbound after this transaction.

Use list of pending objects, which is already used by pending anonymous set which are not yet bound to rule.

---
 include/net/netfilter/nf_tables.h | 1 +
 net/netfilter/nf_tables_api.c     | 8 ++++++++
 net/netfilter/nft_immediate.c     | 1 +
 3 files changed, 10 insertions(+)
include/net/netfilter/nf_tables.h +1 -0
index d24146b526a1..161913fc098b 100644
@@ -1085,6 +1085,7 @@ struct nft_chain {
 	struct nft_rule_blob		__rcu *blob_gen_0;
 	struct nft_rule_blob		__rcu *blob_gen_1;
 	struct list_head		rules;
+	struct list_head		pending_list;
 	struct list_head		list;
 	struct rhlist_head		rhlhead;
 	struct nft_table		*table;
net/netfilter/nf_tables_api.c +8 -0
index 11beb6750531..f7917f678719 100644
@@ -2370,6 +2370,12 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,
 		goto err_unregister_hook;
 	}
 
+	if (nft_chain_binding(chain)) {
+		struct nftables_pernet *nft_net = nft_pernet(ctx->net);
+
+		list_add_tail(&chain->pending_list, &nft_net->pending_list);
+	}
+
 	table->use++;
 
 	return 0;
@@ -9694,6 +9700,8 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)
 				if (nft_chain_is_bound(trans->ctx.chain)) {
 					nft_trans_destroy(trans);
 					break;
+				} else if (nft_chain_binding(trans->ctx.chain)) {
+					list_del(&trans->ctx.chain->pending_list);
 				}
 				trans->ctx.table->use--;
 				nft_chain_del(trans->ctx.chain);
net/netfilter/nft_immediate.c +1 -0
index 054243b9b89e..929879d1c048 100644
@@ -82,6 +82,7 @@ static int nft_immediate_init(const struct nft_ctx *ctx,
 					goto err1;
 				}
 				chain->bound = true;
+				list_del(&chain->pending_list);
 			}
 			break;
 		default:
Signature · 1 line
2.30.2