Skip to contentSkip to conversation

netfilter: nf_tables: reject requests exceeding NF_FLOW_RULE_ACTION_MAX actions

PATCH In review Florian Westphal 3 participants 3 messages Mar 25 – Mar 26

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_tables: reject requests exceeding NF_FLOW_RULE_ACTION_MAX actions

nf_flow_offload_rule_alloc() allocates space for NF_FLOW_RULE_ACTION_MAX entries. Make sure userspace passes more entries to us.

---
Can also route via nf-next if thats deemed the better tree.

 include/net/netfilter/nf_flow_table.h | 2 ++
 net/netfilter/nf_flow_table_offload.c | 2 --
 net/netfilter/nf_tables_offload.c     | 5 +++--
 3 files changed, 5 insertions(+), 4 deletions(-)
include/net/netfilter/nf_flow_table.h +2 -0
index b09c11c048d5..0b2fb1467b3f 100644
@@ -13,6 +13,8 @@
 #include <linux/if_pppox.h>
 #include <linux/ppp_defs.h>
 
+#define NF_FLOW_RULE_ACTION_MAX	16
+
 struct nf_flowtable;
 struct nf_flow_rule;
 struct flow_offload;
net/netfilter/nf_flow_table_offload.c +0 -2
index 9b677e116487..11463682bbfa 100644
@@ -727,8 +727,6 @@ int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow,
 }
 EXPORT_SYMBOL_GPL(nf_flow_rule_route_ipv6);
 
-#define NF_FLOW_RULE_ACTION_MAX	16
-
 static struct nf_flow_rule *
 nf_flow_offload_rule_alloc(struct net *net,
 			   const struct flow_offload_work *offload,
net/netfilter/nf_tables_offload.c +3 -2
index 9101b1703b52..a2f7966bc201 100644
@@ -88,10 +88,11 @@ static void nft_flow_rule_transfer_vlan(struct nft_offload_ctx *ctx,
 struct nft_flow_rule *nft_flow_rule_create(struct net *net,
 					   const struct nft_rule *rule)
 {
+	unsigned int num_actions = 0;
 	struct nft_offload_ctx *ctx;
 	struct nft_flow_rule *flow;
-	int num_actions = 0, err;
 	struct nft_expr *expr;
+	int err;
 
 	expr = nft_expr_first(rule);
 	while (nft_expr_more(rule, expr)) {
@@ -102,7 +103,7 @@ struct nft_flow_rule *nft_flow_rule_create(struct net *net,
 		expr = nft_expr_next(expr);
 	}
 
-	if (num_actions == 0)
+	if (num_actions == 0 || num_actions > NF_FLOW_RULE_ACTION_MAX)
 		return ERR_PTR(-EOPNOTSUPP);
 
 	flow = nft_flow_rule_alloc(num_actions);
Signature · 1 line
2.52.0
#1 lore raw
Quoted 3 lines · Florian Westphal

nf_flow_offload_rule_alloc() allocates space for NF_FLOW_RULE_ACTION_MAX entries. Make sure userspace passes more entries to us.

nit: shouldn't this be "Make sure userspace does not pass more entries to us"?

Other than that, LGTM.

Thanks.

Quoted 62 lines

---
Can also route via nf-next if thats deemed the better tree.

  include/net/netfilter/nf_flow_table.h | 2 ++
  net/netfilter/nf_flow_table_offload.c | 2 --
  net/netfilter/nf_tables_offload.c     | 5 +++--
  3 files changed, 5 insertions(+), 4 deletions(-)
include/net/netfilter/nf_flow_table.h +2 -0
index b09c11c048d5..0b2fb1467b3f 100644
@@ -13,6 +13,8 @@
  #include <linux/if_pppox.h>
  #include <linux/ppp_defs.h>
 
+#define NF_FLOW_RULE_ACTION_MAX	16
+
  struct nf_flowtable;
  struct nf_flow_rule;
  struct flow_offload;
net/netfilter/nf_flow_table_offload.c +0 -2
index 9b677e116487..11463682bbfa 100644
@@ -727,8 +727,6 @@ int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow,
  }
  EXPORT_SYMBOL_GPL(nf_flow_rule_route_ipv6);
 
-#define NF_FLOW_RULE_ACTION_MAX	16
-
  static struct nf_flow_rule *
  nf_flow_offload_rule_alloc(struct net *net,
  			   const struct flow_offload_work *offload,
net/netfilter/nf_tables_offload.c +3 -2
index 9101b1703b52..a2f7966bc201 100644
@@ -88,10 +88,11 @@ static void nft_flow_rule_transfer_vlan(struct nft_offload_ctx *ctx,
  struct nft_flow_rule *nft_flow_rule_create(struct net *net,
  					   const struct nft_rule *rule)
  {
+	unsigned int num_actions = 0;
  	struct nft_offload_ctx *ctx;
  	struct nft_flow_rule *flow;
-	int num_actions = 0, err;
  	struct nft_expr *expr;
+	int err;
 
  	expr = nft_expr_first(rule);
  	while (nft_expr_more(rule, expr)) {
@@ -102,7 +103,7 @@ struct nft_flow_rule *nft_flow_rule_create(struct net *net,
  		expr = nft_expr_next(expr);
  	}
 
-	if (num_actions == 0)
+	if (num_actions == 0 || num_actions > NF_FLOW_RULE_ACTION_MAX)
  		return ERR_PTR(-EOPNOTSUPP);
 
  	flow = nft_flow_rule_alloc(num_actions);
#2 lore raw
In reply to Florian Westphal
Quoted 2 lines · Florian Westphal

nf_flow_offload_rule_alloc() allocates space for NF_FLOW_RULE_ACTION_MAX entries. Make sure userspace passes more entries to us.

While the flowtable hardware offload uses a fixed maximum number of actions NF_FLOW_RULE_ACTION_MAX for simplicity.

But nf_tables hardware offload allocates the number of actions dynamically from nft_flow_rule_create(), such function iterates to check if there is .offload_action is true, the increments the array of actions by one for each.

Possible actions (note payload mangling is currently not supported in nf_tables hardware offload).

This is fragile, because advancing the action array is opencoded:

        entry = &flow->rule->action.entries[ctx->num_actions++];

I can make a patch for nf-next to add a helper, but I don't see any issue on nf_tables_offload at this stage. There are three actions only and they add one single entry to the array.

As for the flowtable hardware offload (different infrastructure) I proposed a different approach:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/