Skip to contentSkip to conversation

evaluate: rename recursion counter to recursion.binop

PATCH New Florian Westphal 1 participant 2 messages Jun 6, 2025 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH v2 1/2 evaluate: rename recursion counter to recursion.binop

The existing recursion counter is used by the binop expression to detect if we've completely followed all the binops.

We can only chain up to NFT_MAX_EXPR_RECURSION binops, but the evaluation step can perform constant-folding, so we must recurse until we found the rightmost (last) binop in the chain.

Then we can check the post-eval chain to see if it is something that can be serialized later (i.e., if we are within the NFT_MAX_EXPR_RECURSION after constant folding) or not.

Thus we can't reuse the existing ctx->recursion counter for other expressions; entering the initial expr_evaluate_binop with ctx->recursion > 0 would break things.

Therefore rename this to an embedded structure.
This allows us to add a new recursion counter in a followup patch.

---
v2: I'm resending this as per last discussion.
There are no changes in this patch.

 include/rule.h |  8 ++++++--
 src/evaluate.c | 10 +++++-----
 2 files changed, 11 insertions(+), 7 deletions(-)
include/rule.h +6 -2
index 85a0d9c0b524..e7e80a41506c 100644
@@ -756,6 +756,10 @@ extern void cmd_free(struct cmd *cmd);
 #include <payload.h>
 #include <expression.h>
 
+struct eval_recursion {
+	uint16_t binop;
+};
+
 /**
  * struct eval_ctx - evaluation context
  *
@@ -767,7 +771,7 @@ extern void cmd_free(struct cmd *cmd);
  * @set:	current set
  * @stmt:	current statement
  * @stmt_len:	current statement template length
- * @recursion:  expr evaluation recursion counter
+ * @recursion:  expr evaluation recursion counters
  * @cache:	cache context
  * @debug_mask: debugging bitmask
  * @ectx:	expression context
@@ -783,7 +787,7 @@ struct eval_ctx {
 	struct set		*set;
 	struct stmt		*stmt;
 	uint32_t		stmt_len;
-	uint32_t		recursion;
+	struct eval_recursion	recursion;
 	struct expr_ctx		ectx;
 	struct proto_ctx	_pctx[2];
 	const struct proto_desc	*inner_desc;
src/evaluate.c +5 -5
index e8e4aa2df4ca..2878a23c24ee 100644
@@ -1522,11 +1522,11 @@ static int expr_evaluate_binop(struct eval_ctx *ctx, struct expr **expr)
 	unsigned int max_shift_len = ctx->ectx.len;
 	int ret = -1;
 
-	if (ctx->recursion >= USHRT_MAX)
+	if (ctx->recursion.binop >= USHRT_MAX)
 		return expr_binary_error(ctx->msgs, op, NULL,
 					 "Binary operation limit %u reached ",
-					 ctx->recursion);
-	ctx->recursion++;
+					 ctx->recursion.binop);
+	ctx->recursion.binop++;
 
 	if (expr_evaluate(ctx, &op->left) < 0)
 		return -1;
@@ -1611,7 +1611,7 @@ static int expr_evaluate_binop(struct eval_ctx *ctx, struct expr **expr)
 	}
 
 
-	if (ctx->recursion == 0)
+	if (ctx->recursion.binop == 0)
 		BUG("recursion counter underflow");
 
 	/* can't check earlier: evaluate functions might do constant-merging + expr_free.
@@ -1619,7 +1619,7 @@ static int expr_evaluate_binop(struct eval_ctx *ctx, struct expr **expr)
 	 * So once we've evaluate everything check for remaining length of the
 	 * binop chain.
 	 */
-	if (--ctx->recursion == 0) {
+	if (--ctx->recursion.binop == 0) {
 		unsigned int to_linearize = 0;
 
 		op = *expr;
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH v2 2/2 evaluate: restrict allowed subtypes of concatenations

We need to restrict this, included bogon asserts with:

BUG: unknown expression type prefix

nft: src/netlink_linearize.c:940: netlink_gen_expr: Assertion `0' failed.

Prefix expressions are only allowed if the concatenation is used within a set element, not when specifying the lookup key.

For the former, anything that represents a value is allowed. For the latter, only what will generate data (fill a register) is permitted.

At this time we do not have an annotation that tells if the expression is on the left hand side (lookup key) or right hand side (set element).

Add a new list recursion counter for this. If its 0 then we're building the lookup key, if its the latter the concatenation is the RHS part of a relational expression and prefix, ranges and so on are allowed.

IOW, we don't really need a recursion counter, another type of annotation that would tell if the expression is placed on the left or right hand side of another expression would work too.

v2: explicitly list all 'illegal' expression types instead of using a default label for them.

This will raise a compiler warning to remind us to adjust the case labels in case a new expression type gets added in the future.

---
Resending as per our last discussion.
WRT. to Pablos comment about changing the logic to only explicitly deny whats not allowed and accept the rest, I don't like this and prefer the conservative approach of denying everything that isn't permitted.
As a compromise, I replaced the 'default' label of v1 with the remaining, not-allowed expressions. This will at least spot a warning when a new expression gets added so it can be classified (lhs/rhs safe) accordingly.

I also slightly changed the commit message to say that the counter isn't strictly required and just serves as 'is this lhs or rhs' indicator.

 include/rule.h                                |  1 +
 src/evaluate.c                                | 61 ++++++++++++++++++-
 .../unknown_expression_type_prefix_assert     |  9 +++
 3 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 tests/shell/testcases/bogons/nft-f/unknown_expression_type_prefix_assert
include/rule.h +1 -0
index e7e80a41506c..655d6abaf5fa 100644
@@ -758,6 +758,7 @@ extern void cmd_free(struct cmd *cmd);
 
 struct eval_recursion {
 	uint16_t binop;
+	uint16_t list;
 };
 
 /**
src/evaluate.c +60 -1

80 lines not on this page · raw message

tests/shell/testcases/bogons/nft-f/unknown_expression_type_prefix_assert new file +9 -0
new file mode 100644
index 000000000000..d7f8526092a5
@@ -0,0 +1,9 @@
+table t {
+	set sc {
+		type inet_service . ifname
+	}
+
+	chain c {
+		tcp dport . bla* @sc accept
+	}
+}
Signature · 1 line
2.49.0