Blame SOURCES/0034-evaluate-missing-datatype-definition-in-implicit_set.patch

fdae68
From 3193f74613b16a42d7784452ebf4d53ccd33b887 Mon Sep 17 00:00:00 2001
fdae68
From: Phil Sutter <psutter@redhat.com>
fdae68
Date: Tue, 12 Jan 2021 10:34:35 +0100
fdae68
Subject: [PATCH] evaluate: missing datatype definition in
fdae68
 implicit_set_declaration()
fdae68
fdae68
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1877022
fdae68
Upstream Status: nftables commit 54eb1e16cc478
fdae68
fdae68
commit 54eb1e16cc4787906fe8206858f0ea0bfb9c1209
fdae68
Author: Pablo Neira Ayuso <pablo@netfilter.org>
fdae68
Date:   Sun Jun 7 15:23:21 2020 +0200
fdae68
fdae68
    evaluate: missing datatype definition in implicit_set_declaration()
fdae68
fdae68
    set->data from implicit_set_declaration(), otherwise, set_evaluation()
fdae68
    bails out with:
fdae68
fdae68
     # nft -f /etc/nftables/inet-filter.nft
fdae68
     /etc/nftables/inet-filter.nft:8:32-54: Error: map definition does not specify
fdae68
     mapping data type
fdae68
                    tcp dport vmap { 22 : jump ssh_input }
fdae68
                                   ^^^^^^^^^^^^^^^^^^^^^^^
fdae68
     /etc/nftables/inet-filter.nft:13:26-52: Error: map definition does not specify
fdae68
     mapping data type
fdae68
                     iif vmap { "eth0" : jump wan_input }
fdae68
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
fdae68
fdae68
    Add a test to cover this case.
fdae68
fdae68
    Fixes: 7aa08d45031e ("evaluate: Perform set evaluation on implicitly declared (anonymous) sets")
fdae68
    Closes: https://bugzilla.kernel.org/show_bug.cgi?id=208093
fdae68
    Reviewed-by: Stefano Brivio <sbrivio@redhat.com>
fdae68
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
fdae68
---
fdae68
 src/evaluate.c                              | 22 ++++++++++++----------
fdae68
 tests/shell/testcases/maps/0009vmap_0       | 19 +++++++++++++++++++
fdae68
 tests/shell/testcases/maps/dumps/0009vmap_0 | 13 +++++++++++++
fdae68
 3 files changed, 44 insertions(+), 10 deletions(-)
fdae68
 create mode 100755 tests/shell/testcases/maps/0009vmap_0
fdae68
 create mode 100644 tests/shell/testcases/maps/dumps/0009vmap_0
fdae68
fdae68
diff --git a/src/evaluate.c b/src/evaluate.c
fdae68
index fc45cef..a966ed4 100644
fdae68
--- a/src/evaluate.c
fdae68
+++ b/src/evaluate.c
fdae68
@@ -79,6 +79,7 @@ static int set_evaluate(struct eval_ctx *ctx, struct set *set);
fdae68
 static struct expr *implicit_set_declaration(struct eval_ctx *ctx,
fdae68
 					     const char *name,
fdae68
 					     struct expr *key,
fdae68
+					     struct expr *data,
fdae68
 					     struct expr *expr)
fdae68
 {
fdae68
 	struct cmd *cmd;
fdae68
@@ -92,6 +93,7 @@ static struct expr *implicit_set_declaration(struct eval_ctx *ctx,
fdae68
 	set->flags	= NFT_SET_ANONYMOUS | expr->set_flags;
fdae68
 	set->handle.set.name = xstrdup(name);
fdae68
 	set->key	= key;
fdae68
+	set->data	= data;
fdae68
 	set->init	= expr;
fdae68
 	set->automerge	= set->flags & NFT_SET_INTERVAL;
fdae68
 
fdae68
@@ -1387,7 +1389,7 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)
fdae68
 	struct expr_ctx ectx = ctx->ectx;
fdae68
 	struct expr *map = *expr, *mappings;
fdae68
 	const struct datatype *dtype;
fdae68
-	struct expr *key;
fdae68
+	struct expr *key, *data;
fdae68
 
fdae68
 	expr_set_context(&ctx->ectx, NULL, 0);
fdae68
 	if (expr_evaluate(ctx, &map->map) < 0)
fdae68
@@ -1406,15 +1408,14 @@ static int expr_evaluate_map(struct eval_ctx *ctx, struct expr **expr)
fdae68
 					  ctx->ectx.byteorder,
fdae68
 					  ctx->ectx.len, NULL);
fdae68
 
fdae68
+		dtype = set_datatype_alloc(ectx.dtype, ectx.byteorder);
fdae68
+		data = constant_expr_alloc(&netlink_location, dtype,
fdae68
+					   dtype->byteorder, ectx.len, NULL);
fdae68
+
fdae68
 		mappings = implicit_set_declaration(ctx, "__map%d",
fdae68
-						    key,
fdae68
+						    key, data,
fdae68
 						    mappings);
fdae68
 
fdae68
-		dtype = set_datatype_alloc(ectx.dtype, ectx.byteorder);
fdae68
-
fdae68
-		mappings->set->data = constant_expr_alloc(&netlink_location,
fdae68
-							  dtype, dtype->byteorder,
fdae68
-							  ectx.len, NULL);
fdae68
 		if (ectx.len && mappings->set->data->len != ectx.len)
fdae68
 			BUG("%d vs %d\n", mappings->set->data->len, ectx.len);
fdae68
 
fdae68
@@ -1857,7 +1858,8 @@ static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr)
fdae68
 		case EXPR_SET:
fdae68
 			right = rel->right =
fdae68
 				implicit_set_declaration(ctx, "__set%d",
fdae68
-							 expr_get(left), right);
fdae68
+							 expr_get(left), NULL,
fdae68
+							 right);
fdae68
 			/* fall through */
fdae68
 		case EXPR_SET_REF:
fdae68
 			/* Data for range lookups needs to be in big endian order */
fdae68
@@ -2335,7 +2337,7 @@ static int stmt_evaluate_meter(struct eval_ctx *ctx, struct stmt *stmt)
fdae68
 		set->set_flags |= NFT_SET_TIMEOUT;
fdae68
 
fdae68
 	setref = implicit_set_declaration(ctx, stmt->meter.name,
fdae68
-					  expr_get(key), set);
fdae68
+					  expr_get(key), NULL, set);
fdae68
 
fdae68
 	setref->set->desc.size = stmt->meter.size;
fdae68
 	stmt->meter.set = setref;
fdae68
@@ -3173,7 +3175,7 @@ static int stmt_evaluate_objref_map(struct eval_ctx *ctx, struct stmt *stmt)
fdae68
 					  ctx->ectx.len, NULL);
fdae68
 
fdae68
 		mappings = implicit_set_declaration(ctx, "__objmap%d",
fdae68
-						    key, mappings);
fdae68
+						    key, NULL, mappings);
fdae68
 		mappings->set->objtype  = stmt->objref.type;
fdae68
 
fdae68
 		map->mappings = mappings;
fdae68
diff --git a/tests/shell/testcases/maps/0009vmap_0 b/tests/shell/testcases/maps/0009vmap_0
fdae68
new file mode 100755
fdae68
index 0000000..7627c81
fdae68
--- /dev/null
fdae68
+++ b/tests/shell/testcases/maps/0009vmap_0
fdae68
@@ -0,0 +1,19 @@
fdae68
+#!/bin/bash
fdae68
+
fdae68
+set -e
fdae68
+
fdae68
+EXPECTED="table inet filter {
fdae68
+        chain ssh_input {
fdae68
+        }
fdae68
+
fdae68
+        chain wan_input {
fdae68
+                tcp dport vmap { 22 : jump ssh_input }
fdae68
+        }
fdae68
+
fdae68
+        chain prerouting {
fdae68
+                type filter hook prerouting priority -300; policy accept;
fdae68
+                iif vmap { "lo" : jump wan_input }
fdae68
+        }
fdae68
+}"
fdae68
+
fdae68
+$NFT -f - <<< "$EXPECTED"
fdae68
diff --git a/tests/shell/testcases/maps/dumps/0009vmap_0 b/tests/shell/testcases/maps/dumps/0009vmap_0
fdae68
new file mode 100644
fdae68
index 0000000..540a8af
fdae68
--- /dev/null
fdae68
+++ b/tests/shell/testcases/maps/dumps/0009vmap_0
fdae68
@@ -0,0 +1,13 @@
fdae68
+table inet filter {
fdae68
+	chain ssh_input {
fdae68
+	}
fdae68
+
fdae68
+	chain wan_input {
fdae68
+		tcp dport vmap { 22 : jump ssh_input }
fdae68
+	}
fdae68
+
fdae68
+	chain prerouting {
fdae68
+		type filter hook prerouting priority -300; policy accept;
fdae68
+		iif vmap { "lo" : jump wan_input }
fdae68
+	}
fdae68
+}
fdae68
-- 
fdae68
1.8.3.1
fdae68