evaluate: tolerate empty concatenation
Florian Westphal
maintainerPATCH 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
@@ -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
@@ -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
Pablo Neira Ayuso
maintainerHi Florian,
Quoted 11 lines · Florian Westphal
Don't rely on a successful evaluation of
set->key. With this input,set->keyfails 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
@@ -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);
Pablo Neira Ayuso
maintainerQuoted 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->keyfails 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 expressionMaybe 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
@@ -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;
Florian Westphal
maintainerQuoted 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->keyfails 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 expressionMaybe 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.
Pablo Neira Ayuso
maintainerQuoted 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->keyfails 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 expressionMaybe 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.
Florian Westphal
maintainerQuoted 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.