Skip to contentSkip to conversation

evaluate: fix assertion failure with malformed map definitions

PATCH In review Florian Westphal 2 participants 2 messages Mar 20, 2025 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 evaluate: fix assertion failure with malformed map definitions

Included bogon triggers:
nft: src/evaluate.c:2267: expr_evaluate_mapping: Assertion `set->data != NULL' failed.

After this fix, following errors will be shown:
Error: unqualified type invalid specified in map definition. Try "typeof expression" instead of "type datatype".

map m {
    ^
map m {
    ^

Error: map has no mapping data

Fixes 343a51702656 ("src: store expr, not dtype to track data in sets")
---
 src/evaluate.c                                              | 5 ++++-
 .../bogons/nft-f/malformed_map_expr_evaluate_mapping_assert | 6 ++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 tests/shell/testcases/bogons/nft-f/malformed_map_expr_evaluate_mapping_assert
src/evaluate.c +4 -1
index 5b7a9b863cd5..a22cabb615e9 100644
@@ -2323,7 +2323,10 @@ static int expr_evaluate_mapping(struct eval_ctx *ctx, struct expr **expr)
 				  "Key must be a constant");
 	mapping->flags |= mapping->left->flags & EXPR_F_SINGLETON;
 
-	assert(set->data != NULL);
+	/* This can happen for malformed map definitions */
+	if (!set->data)
+		return set_error(ctx, set, "map has no mapping data");
+
 	if (!set_is_anonymous(set->flags) &&
 	    set->data->flags & EXPR_F_INTERVAL)
 		datalen = set->data->len / 2;
tests/shell/testcases/bogons/nft-f/malformed_map_expr_evaluate_mapping_assert new file +6 -0
new file mode 100644
index 000000000000..c77a9c33e0ad
@@ -0,0 +1,6 @@
+table ip x {
+        map m {
+                typeof ct saddr :ct expectation
+                elements = { * : none}
+        }
+}
Signature · 1 line
2.48.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

TLDR; patch is fine with me.

tests/shell/testcases/bogons/nft-f/malformed_map_expr_evaluate_mapping_assert line 3

Quoted 9 lines · Florian Westphal
tests/shell/testcases/bogons/nft-f/malformed_map_expr_evaluate_mapping_assert new file +3 -0
new file mode 100644
index 000000000000..c77a9c33e0ad
@@ -0,0 +1,6 @@
+table ip x {
+        map m {
+                typeof ct saddr :ct expectation

Longer story: This is declaration is "correct".

Actually, ct saddr is broken because it is ambiguous, it is there in the parser for "backwards compatibility", IIRC it can be only used sanely from rules, ie. ct saddr cannot be used from set/map, ct original ip saddr should be used instead.

The problem with ct saddr is that length depends on the context, and having a key with variable size is problematic, the assumption here is that all keys have fixed size.

tests/shell/testcases/bogons/nft-f/malformed_map_expr_evaluate_mapping_assert line 6

Quoted 3 lines
tests/shell/testcases/bogons/nft-f/malformed_map_expr_evaluate_mapping_assert new file +3 -0
+                elements = { * : none}
+        }
+}

This variant bails out correctly:

table ip x {
        map m {
                typeof ct original ip saddr : ct expectation
                elements = { * : none}
        }
}

/tmp/k:4:30-30: Error: Could not process rule: No such file or directory

                elements = { * : none}

because "none" ct expectation object does not exist.