Blame SOURCES/0010-xtables-translate-Fix-for-interface-name-corner-case.patch

fc8f74
From da36213a48f6114ab998a5fb37bae61d2a02d5f6 Mon Sep 17 00:00:00 2001
fc8f74
From: Phil Sutter <phil@nwl.cc>
fc8f74
Date: Thu, 6 Feb 2020 15:08:41 +0100
fc8f74
Subject: [PATCH] xtables-translate: Fix for interface name corner-cases
fc8f74
fc8f74
There are two special situations xlate_ifname() didn't cover for:
fc8f74
fc8f74
* Interface name containing '*': This went unchanged, creating a command
fc8f74
  nft wouldn't accept. Instead translate into '\*' which doesn't change
fc8f74
  semantics.
fc8f74
fc8f74
* Interface name being '+': Can't translate into nft wildcard character
fc8f74
  as nft doesn't accept asterisk-only interface names. Instead decide
fc8f74
  what to do based on 'invert' value: Skip match creation if false,
fc8f74
  match against an invalid interface name if true.
fc8f74
fc8f74
Also add a test to make sure future changes to this behaviour are
fc8f74
noticed.
fc8f74
fc8f74
Signed-off-by: Phil Sutter <phil@nwl.cc>
fc8f74
(cherry picked from commit e179e87a1179e272a9bdabb0220b17d61d099ee3)
fc8f74
Signed-off-by: Phil Sutter <psutter@redhat.com>
fc8f74
---
fc8f74
 extensions/generic.txlate    | 12 ++++++++++++
fc8f74
 iptables/xtables-translate.c | 33 ++++++++++++++++++++++++++++-----
fc8f74
 2 files changed, 40 insertions(+), 5 deletions(-)
fc8f74
fc8f74
diff --git a/extensions/generic.txlate b/extensions/generic.txlate
fc8f74
index b38fbd1fe113b..c92d082abea78 100644
fc8f74
--- a/extensions/generic.txlate
fc8f74
+++ b/extensions/generic.txlate
fc8f74
@@ -18,3 +18,15 @@ nft add rule bridge filter FORWARD iifname != "iname" meta ibrname "ilogname" oi
fc8f74
 
fc8f74
 ebtables-translate -I INPUT -p ip -d 1:2:3:4:5:6/ff:ff:ff:ff:00:00
fc8f74
 nft insert rule bridge filter INPUT ether type 0x800 ether daddr 01:02:03:04:00:00 and ff:ff:ff:ff:00:00 == 01:02:03:04:00:00 counter
fc8f74
+
fc8f74
+# asterisk is not special in iptables and it is even a valid interface name
fc8f74
+iptables-translate -A FORWARD -i '*' -o 'eth*foo'
fc8f74
+nft add rule ip filter FORWARD iifname "\*" oifname "eth\*foo" counter
fc8f74
+
fc8f74
+# skip for always matching interface names
fc8f74
+iptables-translate -A FORWARD -i '+'
fc8f74
+nft add rule ip filter FORWARD counter
fc8f74
+
fc8f74
+# match against invalid interface name to simulate never matching rule
fc8f74
+iptables-translate -A FORWARD ! -i '+'
fc8f74
+nft add rule ip filter FORWARD iifname "INVAL/D" counter
fc8f74
diff --git a/iptables/xtables-translate.c b/iptables/xtables-translate.c
fc8f74
index 77a186b905d73..c4e177c0d63ba 100644
fc8f74
--- a/iptables/xtables-translate.c
fc8f74
+++ b/iptables/xtables-translate.c
fc8f74
@@ -32,15 +32,38 @@
fc8f74
 void xlate_ifname(struct xt_xlate *xl, const char *nftmeta, const char *ifname,
fc8f74
 		  bool invert)
fc8f74
 {
fc8f74
-	int ifaclen = strlen(ifname);
fc8f74
-	char iface[IFNAMSIZ];
fc8f74
+	int ifaclen = strlen(ifname), i, j;
fc8f74
+	char iface[IFNAMSIZ * 2];
fc8f74
 
fc8f74
 	if (ifaclen < 1 || ifaclen >= IFNAMSIZ)
fc8f74
 		return;
fc8f74
 
fc8f74
-	strcpy(iface, ifname);
fc8f74
-	if (iface[ifaclen - 1] == '+')
fc8f74
-		iface[ifaclen - 1] = '*';
fc8f74
+	for (i = 0, j = 0; i < ifaclen + 1; i++, j++) {
fc8f74
+		switch (ifname[i]) {
fc8f74
+		case '+':
fc8f74
+			iface[j] = '*';
fc8f74
+			break;
fc8f74
+		case '*':
fc8f74
+			iface[j++] = '\\';
fc8f74
+			/* fall through */
fc8f74
+		default:
fc8f74
+			iface[j] = ifname[i];
fc8f74
+			break;
fc8f74
+		}
fc8f74
+	}
fc8f74
+
fc8f74
+	if (ifaclen == 1 && ifname[0] == '+') {
fc8f74
+		/* Nftables does not support wildcard only string. Workaround
fc8f74
+		 * is easy, given that this will match always or never
fc8f74
+		 * depending on 'invert' value. To match always, simply don't
fc8f74
+		 * generate an expression. To match never, use an invalid
fc8f74
+		 * interface name (kernel doesn't accept '/' in names) to match
fc8f74
+		 * against. */
fc8f74
+		if (!invert)
fc8f74
+			return;
fc8f74
+		strcpy(iface, "INVAL/D");
fc8f74
+		invert = false;
fc8f74
+	}
fc8f74
 
fc8f74
 	xt_xlate_add(xl, "%s %s\"%s\" ", nftmeta, invert ? "!= " : "", iface);
fc8f74
 }
fc8f74
-- 
fc8f74
2.24.1
fc8f74