Skip to contentSkip to conversation

evaluate: check XOR RHS operand is a constant value

PATCH In review Florian Westphal 2 participants 4 messages Aug 5, 2025 – Aug 18, 2025 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/2 evaluate: check XOR RHS operand is a constant value

Now that we support non-constant RHS side in binary operations, reject XOR with non-constant key: we cannot transfer the expression.

Fixes 54bfc38c522b ("src: allow binop expressions with variable right-hand operands")

---
I suggest to defer this until after 1.1.4 is out.

 src/evaluate.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/evaluate.c +5 -1
index 81e88d11aecb..1d102f842df0 100644
@@ -2578,16 +2578,20 @@ static int binop_can_transfer(struct eval_ctx *ctx,
 
 	switch (left->op) {
 	case OP_LSHIFT:
+		assert(left->right->etype == EXPR_VALUE);
+		assert(right->etype == EXPR_VALUE);
+
 		if (mpz_scan1(right->value, 0) < mpz_get_uint32(left->right->value))
 			return expr_binary_error(ctx->msgs, right, left,
 						 "Comparison is always false");
 		return 1;
 	case OP_RSHIFT:
+		assert(left->right->etype == EXPR_VALUE);
 		if (ctx->ectx.len < right->len + mpz_get_uint32(left->right->value))
 			ctx->ectx.len += mpz_get_uint32(left->right->value);
 		return 1;
 	case OP_XOR:
-		return 1;
+		return expr_is_constant(left->right);
 	default:
 		return 0;
 	}
Signature · 1 line
2.49.1
author maintainer
#1 lore raw

PATCH 2/2 tests: shell: add parser and packetpath test

One to validate parsing, and one to test that packets match the expected mapping.

omits json file because of:
internal:0:0-0: Error: Expression type payload not allowed in context (RHS, STMT).

i.e. there is more work to be done on json side to support this.

---
I suggest to defer this until after 1.1.4 is out.

.../bitwise/bitwise_in_sets_and_maps | 27 ++++++++

 .../dumps/bitwise_in_sets_and_maps.nft        | 17 ++++++
 .../testcases/packetpath/bitwise_with_map     | 61 +++++++++++++++++++
 .../packetpath/dumps/bitwise_with_map.nft     | 16 +++++
 4 files changed, 121 insertions(+)
 create mode 100755 tests/shell/testcases/bitwise/bitwise_in_sets_and_maps
 create mode 100644 tests/shell/testcases/bitwise/dumps/bitwise_in_sets_and_maps.nft
 create mode 100755 tests/shell/testcases/packetpath/bitwise_with_map
 create mode 100644 tests/shell/testcases/packetpath/dumps/bitwise_with_map.nft
tests/shell/testcases/bitwise/bitwise_in_sets_and_maps new file +27 -0
new file mode 100755
index 000000000000..4f5044f512aa
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+# NFT_TEST_REQUIRES(NFT_TEST_HAVE_bitshift)
+
+set -e
+
+$NFT -f - <<EOF
+table ip t {
+	map m {
+		typeof ip saddr : mark
+		elements = { 1.2.3.4 : 42 }
+	}
+
+	chain c {
+		meta mark set ip saddr map @m
+		meta mark set ip saddr & 255.255.255.0 map @m
+		meta mark set ip saddr ^ 255.255.255.0 map @m
+		meta mark set ip saddr ^ ip daddr map @m
+		meta mark set ip saddr ^ 1 map @m
+
+		meta mark set ip saddr & ip daddr map { 10.1.2.3 : 1, 10.2.3.4 : 2 }
+		meta mark set ip saddr ^ ip daddr map { 10.1.2.3 : 1, 10.2.3.4 : 2 }
+	}
+}
+EOF
+
+$NFT add element "t m { 10.1.2.1 : 23 }"
tests/shell/testcases/bitwise/dumps/bitwise_in_sets_and_maps.nft new file +17 -0
new file mode 100644
index 000000000000..a29d6c011fbb
@@ -0,0 +1,17 @@
+table ip t {
+	map m {
+		typeof ip saddr : meta mark
+		elements = { 1.2.3.4 : 0x0000002a,
+			     10.1.2.1 : 0x00000017 }
+	}
+
+	chain c {
+		meta mark set ip saddr map @m
+		meta mark set ip saddr & 255.255.255.0 map @m
+		meta mark set ip saddr ^ 255.255.255.0 map @m
+		meta mark set ip saddr ^ ip daddr map @m
+		meta mark set ip saddr ^ 0.0.0.1 map @m
+		meta mark set ip saddr & ip daddr map { 10.1.2.3 : 0x00000001, 10.2.3.4 : 0x00000002 }
+		meta mark set ip saddr ^ ip daddr map { 10.1.2.3 : 0x00000001, 10.2.3.4 : 0x00000002 }
+	}
+}
tests/shell/testcases/packetpath/bitwise_with_map new file +61 -0

61 lines not on this page · raw message

tests/shell/testcases/packetpath/dumps/bitwise_with_map.nft new file +16 -0
new file mode 100644
index 000000000000..ba1ef8ac3f1f
@@ -0,0 +1,16 @@
+table ip test-binop {
+	chain in {
+		type filter hook input priority filter; policy accept;
+		icmp type echo-request jump {
+			meta mark 0x00000000 counter packets 0 bytes 0
+			meta mark 0x00000001 counter packets 1 bytes 84
+			meta mark 0x00000002 counter packets 1 bytes 84
+			meta mark 0x00000003 counter packets 0 bytes 0
+		}
+	}
+
+	chain out {
+		type filter hook output priority filter; policy accept;
+		icmp type echo-request meta mark set ip saddr ^ ip daddr map { 0.0.0.0 : 0x00000001, 0.1.2.2 : 0x00000002, 127.0.0.1 : 0x00000003 }
+	}
+}
Signature · 1 line
2.49.1
maintainer Reviewed-by on v1 1/2
#2 lore raw
In reply to Florian Westphal
Quoted 5 lines · Florian Westphal

Now that we support non-constant RHS side in binary operations, reject XOR with non-constant key: we cannot transfer the expression.

Fixes 54bfc38c522b ("src: allow binop expressions with variable right-hand operands")

Maybe a tests/py for this to improve coverage is worth?

Quoted 36 lines

---
I suggest to defer this until after 1.1.4 is out.

 src/evaluate.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/evaluate.c +5 -1
index 81e88d11aecb..1d102f842df0 100644
@@ -2578,16 +2578,20 @@ static int binop_can_transfer(struct eval_ctx *ctx,
 
 	switch (left->op) {
 	case OP_LSHIFT:
+		assert(left->right->etype == EXPR_VALUE);
+		assert(right->etype == EXPR_VALUE);
+
 		if (mpz_scan1(right->value, 0) < mpz_get_uint32(left->right->value))
 			return expr_binary_error(ctx->msgs, right, left,
 						 "Comparison is always false");
 		return 1;
 	case OP_RSHIFT:
+		assert(left->right->etype == EXPR_VALUE);
 		if (ctx->ectx.len < right->len + mpz_get_uint32(left->right->value))
 			ctx->ectx.len += mpz_get_uint32(left->right->value);
 		return 1;
 	case OP_XOR:
-		return 1;
+		return expr_is_constant(left->right);
 	default:
 		return 0;
 	}
Signature · 1 line
2.49.1
#3 lore raw
Quoted 10 lines · Pablo Neira Ayuso
Quoted 5 lines · Florian Westphal

Now that we support non-constant RHS side in binary operations, reject XOR with non-constant key: we cannot transfer the expression.

Fixes 54bfc38c522b ("src: allow binop expressions with variable right-hand operands")

Maybe a tests/py for this to improve coverage is worth?

Sorry, it's coming in 2/2, that's fine. Thanks.