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

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