Skip to contentSkip to conversation

src: Allow for empty set variable definition

PATCH New Pablo Neira Ayuso 1 participant 1 message Jul 3, 2020 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 src: Allow for empty set variable definition

Allow for empty set definition in variables if they are merged to non-empty set definition:

define BASE_ALLOWED_INCOMING_TCP_PORTS = {22, 80, 443} define EXTRA_ALLOWED_INCOMING_TCP_PORTS = {}

 table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;
        tcp dport {$BASE_ALLOWED_INCOMING_TCP_PORTS, $EXTRA_ALLOWED_INCOMING_TCP_PORTS} ct state new counter accept
    }

}

However, disallow this:

define EXTRA_ALLOWED_INCOMING_TCP_PORTS = {}

 table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;
        tcp dport {$EXTRA_ALLOWED_INCOMING_TCP_PORTS} ct state new counter accept
    }

}

 # nft -f x.nft
 /tmp/x.nft:6:18-52: Error: Set is empty
        tcp dport {$EXTRA_ALLOWED_INCOMING_TCP_PORTS} ct state new counter accept
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
---
 src/evaluate.c                                  |  3 +++
 src/parser_bison.y                              |  1 +
 tests/shell/testcases/sets/0049set_define_0     | 16 ++++++++++++++++
 tests/shell/testcases/sets/0050set_define_1     | 17 +++++++++++++++++
 .../shell/testcases/sets/dumps/0049set_define_0 |  6 ++++++
 5 files changed, 43 insertions(+)
 create mode 100755 tests/shell/testcases/sets/0049set_define_0
 create mode 100755 tests/shell/testcases/sets/0050set_define_1
 create mode 100644 tests/shell/testcases/sets/dumps/0049set_define_0
src/evaluate.c +3 -0
index 827ee48a48ed..2bfe55524fac 100644
@@ -1897,6 +1897,9 @@ static int expr_evaluate_relational(struct eval_ctx *ctx, struct expr **expr)
 				return -1;
 			break;
 		case EXPR_SET:
+			if (right->size == 0)
+				return expr_error(ctx->msgs, right, "Set is empty");
+
 			right = rel->right =
 				implicit_set_declaration(ctx, "__set%d",
 							 expr_get(left), NULL,
src/parser_bison.y +1 -0
index 8a04d3b409a5..1676aa33e431 100644
@@ -3845,6 +3845,7 @@ set_rhs_expr		:	concat_rhs_expr
 
 initializer_expr	:	rhs_expr
 			|	list_rhs_expr
+			|	'{' '}'		{ $$ = compound_expr_alloc(&@$, EXPR_SET); }
 			;
 
 counter_config		:	PACKETS		NUM	BYTES	NUM
tests/shell/testcases/sets/0049set_define_0 new file +16 -0
new file mode 100755
index 000000000000..1d512f7b5a54
@@ -0,0 +1,16 @@
+#!/bin/bash
+
+set -e
+
+EXPECTED="define BASE_ALLOWED_INCOMING_TCP_PORTS = {22, 80, 443}
+define EXTRA_ALLOWED_INCOMING_TCP_PORTS = {}
+
+table inet filter {
+	chain input {
+		type filter hook input priority 0; policy drop;
+		tcp dport {\$BASE_ALLOWED_INCOMING_TCP_PORTS, \$EXTRA_ALLOWED_INCOMING_TCP_PORTS} ct state new counter accept
+	}
+}
+"
+
+$NFT -f - <<< "$EXPECTED"
tests/shell/testcases/sets/0050set_define_1 new file +17 -0
new file mode 100755
index 000000000000..c12de177c7ec
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+set -e
+
+EXPECTED="define BASE_ALLOWED_INCOMING_TCP_PORTS = {}
+
+table inet filter {
+	chain input {
+		type filter hook input priority 0; policy drop;
+		tcp dport {\$BASE_ALLOWED_INCOMING_TCP_PORTS} ct state new counter accept
+	}
+}
+"
+
+$NFT -f - <<< "$EXPECTED" &> /dev/null || exit 0
+echo "E: Accepted empty set" 1>&2
+exit 1
tests/shell/testcases/sets/dumps/0049set_define_0 new file +6 -0
new file mode 100644
index 000000000000..998b387a8151
@@ -0,0 +1,6 @@
+table inet filter {
+	chain input {
+		type filter hook input priority filter; policy drop;
+		tcp dport { 22, 80, 443 } ct state new counter packets 0 bytes 0 accept
+	}
+}
Signature · 1 line
2.20.1