Skip to contentSkip to conversation

evaluate: tolerate empty concatenation

PATCH In review Florian Westphal 2 participants 6 messages Mar 24, 2025 – Mar 27, 2025 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 evaluate: tolerate empty concatenation

Don't rely on a successful evaluation of set->key. With this input, set->key fails validation but subsequent element evaluation asserts because the context points at the set key -- an empty concatenation.

Causes:
nft: src/evaluate.c:1681: expr_evaluate_concat: Assertion `!list_empty(&ctx->ectx.key->expressions)' failed.

After patch:
internal:0:0-0: Error: unqualified type specified in set definition. Try "typeof expression" instead of "type datatype". internal:0:0-0: Error: Could not parse symbolic invalid expression

---
 src/evaluate.c                                | 10 +++++--
 ...pr_evaluate_concat_empty_concat_key_assert | 27 +++++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)
 create mode 100644 tests/shell/testcases/bogons/nft-j-f/expr_evaluate_concat_empty_concat_key_assert
src/evaluate.c +8 -2
index 1e7f6f53542b..a6b08cf3b1b5 100644
@@ -1645,6 +1645,13 @@ static int list_member_evaluate(struct eval_ctx *ctx, struct expr **expr)
 	return err;
 }
 
+static bool ctx_has_concat_key(const struct eval_ctx *ctx)
+{
+	/* Ignore empty concatenation key, set eval queued an error */
+	return ctx->ectx.key && ctx->ectx.key->etype == EXPR_CONCAT &&
+	       !list_empty(&ctx->ectx.key->expressions);
+}
+
 static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
 {
 	const struct datatype *dtype = ctx->ectx.dtype, *tmp;
@@ -1657,9 +1664,8 @@ static int expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)
 	bool runaway = false;
 	uint32_t size = 0;
 
-	if (ctx->ectx.key && ctx->ectx.key->etype == EXPR_CONCAT) {
+	if (ctx_has_concat_key(ctx)) {
 		key_ctx = ctx->ectx.key;
-		assert(!list_empty(&ctx->ectx.key->expressions));
 		key = list_first_entry(&ctx->ectx.key->expressions, struct expr, list);
 		expressions = &ctx->ectx.key->expressions;
 	}
tests/shell/testcases/bogons/nft-j-f/expr_evaluate_concat_empty_concat_key_assert new file +27 -0
new file mode 100644
index 000000000000..956ecdc99721
@@ -0,0 +1,27 @@
+{
+  "nftables": [
+    {
+      "table": { "family": "ip",
+        "name": "t",
+        "handle": 0
+      }
+    },
+    {
+      "set": {
+        "family": "ip",
+        "name": "s",
+        "table": "t",
+        "type": [
+             ],
+        "elem": [
+          {
+            "concat": [
+              "foo", "bar"
+            ]
+          }
+        ]
+      }
+    }
+  ]
+}
+
Signature · 1 line
2.48.1
#1 lore raw

Hi Florian,

Quoted 11 lines · Florian Westphal

Don't rely on a successful evaluation of set->key. With this input, set->key fails validation but subsequent element evaluation asserts because the context points at the set key -- an empty concatenation.

Causes:
nft: src/evaluate.c:1681: expr_evaluate_concat: Assertion `!list_empty(&ctx->ectx.key->expressions)' failed.

After patch:
internal:0:0-0: Error: unqualified type specified in set definition. Try "typeof expression" instead of "type datatype". internal:0:0-0: Error: Could not parse symbolic invalid expression

Maybe block this from the json parser itself?

src/parser_json.c +8 -0
index 17bc38b565ae..8d5aa480ae04 100644
@@ -3395,6 +3395,14 @@ static struct cmd *json_parse_cmd_add_set(struct json_ctx *ctx, json_t *root,
 		return NULL;
 	}
 
+	if (set->key->etype == EXPR_CONCAT &&
+	    list_empty(&set->key->expressions)) {
+		json_error(ctx, "Empty set type.");
+		set_free(set);
+		handle_free(&h);
+		return NULL;
+	}
+
 	if (!json_unpack(root, "{s:o}", "map", &tmp)) {
 		if (json_is_string(tmp)) {
 			const char *s = json_string_value(tmp);
#2 lore raw
Quoted 8 lines · Pablo Neira Ayuso

Hi Florian,

Quoted 5 lines · Florian Westphal

Don't rely on a successful evaluation of set->key. With this input, set->key fails validation but subsequent element evaluation asserts because the context points at the set key -- an empty concatenation.

Quoted 8 lines
Quoted 6 lines

Causes:
nft: src/evaluate.c:1681: expr_evaluate_concat: Assertion `!list_empty(&ctx->ectx.key->expressions)' failed.

After patch:
internal:0:0-0: Error: unqualified type specified in set definition. Try "typeof expression" instead of "type datatype". internal:0:0-0: Error: Could not parse symbolic invalid expression

Maybe block this from the json parser itself?

Maybe this instead? This covers for empty concatenation in both set key and set data.

commit e0123be7a908d1a4b7c43d0817b9ecccf2bd1416

Author: Pablo Neira Ayuso <[email protected]>
Date:   Thu Mar 27 16:32:16 2025 +0100

parser_json: reject empty concatention in set key and data

src/parser_json.c +7 -0
index 17bc38b565ae..513e0b10f028 100644
@@ -1729,6 +1729,13 @@ static struct expr *json_parse_dtype_expr(struct json_ctx *ctx, json_t *root)
 			}
 			compound_expr_add(expr, i);
 		}
+
+		if (list_empty(&expr->expressions)) {
+			json_error(ctx, "Empty concatenation");
+			expr_free(expr);
+			return NULL;
+		}
+
 		return expr;
 	} else if (json_is_object(root)) {
 		const char *key;
author maintainer
#3 lore raw
Quoted 9 lines · Pablo Neira Ayuso
Quoted 9 lines · Florian Westphal
Quoted 8 lines

Don't rely on a successful evaluation of set->key. With this input, set->key fails validation but subsequent element evaluation asserts because the context points at the set key -- an empty concatenation.

Causes:
nft: src/evaluate.c:1681: expr_evaluate_concat: Assertion `!list_empty(&ctx->ectx.key->expressions)' failed.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

After patch:
internal:0:0-0: Error: unqualified type specified in set definition. Try "typeof expression" instead of "type datatype". internal:0:0-0: Error: Could not parse symbolic invalid expression

Maybe block this from the json parser itself?

Maybe this instead? This covers for empty concatenation in both set key and set data.

I don't like the idea of having to keep double-error-checks in both json and bison frontends.

I would prefer a generic solution where possible, unless there is some other advantage such as better error reporting.

#4 lore raw
Quoted 16 lines · Florian Westphal
Quoted 15 lines · Pablo Neira Ayuso
Quoted 14 lines · Florian Westphal
Quoted 11 lines

Don't rely on a successful evaluation of set->key. With this input, set->key fails validation but subsequent element evaluation asserts because the context points at the set key -- an empty concatenation.

Causes:
nft: src/evaluate.c:1681: expr_evaluate_concat: Assertion `!list_empty(&ctx->ectx.key->expressions)' failed.

After patch:
internal:0:0-0: Error: unqualified type specified in set definition. Try "typeof expression" instead of "type datatype". internal:0:0-0: Error: Could not parse symbolic invalid expression

Maybe block this from the json parser itself?

Quoted 8 lines
Quoted 2 lines

Maybe this instead? This covers for empty concatenation in both set key and set data.

I don't like the idea of having to keep double-error-checks in both json and bison frontends.

I would prefer a generic solution where possible, unless there is some other advantage such as better error reporting.

I have been trying to trigger this from the bison parser, but I could not.

I think assertion are still useful to denote something is very broken if we get to the evaluation step with an empty concatenation.

author maintainer
#5 lore raw
Quoted 2 lines · Pablo Neira Ayuso

I think assertion are still useful to denote something is very broken if we get to the evaluation step with an empty concatenation.

OK, then please push put your fix then.