Skip to contentSkip to conversation

netlink_delinearize: only remove protocol if equal cmp is used

PATCH In review Florian Westphal 2 participants 2 messages Jan 27, 2016 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netlink_delinearize: only remove protocol if equal cmp is used

Check for OP_EQ before removing a dependency, else we may zap wrong one, changing the meaning of the rule.

Listing without patch:
ip protocol udp udp dport ssh
ip protocol udp udp dport ssh
counter packets 1 bytes 308 ip protocol udp udp dport ssh

With patch:
ip protocol != tcp udp dport ssh
ip protocol != udp udp dport ssh
ip protocol != tcp counter packets 1 bytes 308 udp dport ssh

---
NB: ip protocol != udp udp dport ... is nonsensical, not sure if its worth the hassle to try to reject stuff like this.

 src/netlink_delinearize.c |  1 +
 tests/py/ip/tcp.t         |  6 ++++++
 tests/py/ip/tcp.t.payload | 18 ++++++++++++++++++
 3 files changed, 25 insertions(+)
 create mode 100644 tests/py/ip/tcp.t
 create mode 100644 tests/py/ip/tcp.t.payload
src/netlink_delinearize.c +1 -0
index 76d598c..2732c96 100644
@@ -1042,6 +1042,7 @@ static void payload_match_expand(struct rule_pp_ctx *ctx,
 		 * payload expression.
 		 */
 		if (ctx->pbase == PROTO_BASE_INVALID &&
+		    expr->op == OP_EQ &&
 		    left->flags & EXPR_F_PROTOCOL) {
 			unsigned int proto = mpz_get_be16(tmp->value);
 			const struct proto_desc *desc, *next;
tests/py/ip/tcp.t new file +6 -0
new file mode 100644
index 0000000..4dcfcb6
@@ -0,0 +1,6 @@
+:input;type filter hook input priority 0
+
+*ip;test-ip;input
+
+ip protocol tcp tcp dport ssh accept;ok;tcp dport 22 accept
+ip protocol ne tcp udp dport ssh accept;ok;ip protocol != 6 udp dport 22 accept
tests/py/ip/tcp.t.payload new file +18 -0
new file mode 100644
index 0000000..ed0dcc8
@@ -0,0 +1,18 @@
+# ip protocol tcp tcp dport ssh accept
+ip test-ip input
+  [ payload load 1b @ network header + 9 => reg 1 ]
+  [ cmp eq reg 1 0x00000006 ]
+  [ payload load 2b @ transport header + 2 => reg 1 ]
+  [ cmp eq reg 1 0x00001600 ]
+  [ immediate reg 0 accept ]
+
+# ip protocol ne tcp udp dport ssh accept
+ip test-ip input
+  [ payload load 1b @ network header + 9 => reg 1 ]
+  [ cmp neq reg 1 0x00000006 ]
+  [ payload load 1b @ network header + 9 => reg 1 ]
+  [ cmp eq reg 1 0x00000011 ]
+  [ payload load 2b @ transport header + 2 => reg 1 ]
+  [ cmp eq reg 1 0x00001600 ]
+  [ immediate reg 0 accept ]
+
Signature · 1 line
2.4.10
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Florian Westphal

Check for OP_EQ before removing a dependency, else we may zap wrong one, changing the meaning of the rule.

Listing without patch:
ip protocol udp udp dport ssh
ip protocol udp udp dport ssh
counter packets 1 bytes 308 ip protocol udp udp dport ssh

Quoted 4 lines

With patch:
ip protocol != tcp udp dport ssh
ip protocol != udp udp dport ssh
ip protocol != tcp counter packets 1 bytes 308 udp dport ssh

Quoted 4 lines

---
NB: ip protocol != udp udp dport ... is nonsensical, not sure if its worth the hassle to try to reject stuff like this.

I agree this is not worth. We'll have more advanced tools to perform transformations and more in-depth semantic evaluation of the ruleset at some point, but not now ;-)