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

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