Blame SOURCES/0067-nft-Reject-standard-targets-as-chain-names-when-rest.patch

aca4c4
From 257c18bf4fbcc7e5f4fb3c9cadab699986a9bd41 Mon Sep 17 00:00:00 2001
aca4c4
From: Phil Sutter <phil@nwl.cc>
aca4c4
Date: Wed, 16 Mar 2022 17:14:07 +0100
aca4c4
Subject: [PATCH] nft: Reject standard targets as chain names when restoring
aca4c4
aca4c4
Reuse parse_chain() called from do_parse() for '-N' and rename it for a
aca4c4
better description of what it does.
aca4c4
aca4c4
Note that by itself, this patch will likely kill iptables-restore
aca4c4
performance for big rulesets due to the extra extension lookup for chain
aca4c4
lines. A following patch announcing those chains to libxtables will
aca4c4
alleviate that.
aca4c4
aca4c4
Signed-off-by: Phil Sutter <phil@nwl.cc>
aca4c4
Reviewed-by: Florian Westphal <fw@strlen.de>
aca4c4
(cherry picked from commit b1aee6b2238794446feba41778f88703784560f7)
aca4c4
aca4c4
Conflicts:
aca4c4
	iptables/xshared.c
aca4c4
-> Parts manually applied due to unmerged do_parse() function.
aca4c4
---
aca4c4
 iptables/ip6tables.c       | 2 +-
aca4c4
 iptables/iptables.c        | 2 +-
aca4c4
 iptables/xshared.c         | 2 +-
aca4c4
 iptables/xshared.h         | 2 +-
aca4c4
 iptables/xtables-restore.c | 5 +----
aca4c4
 iptables/xtables.c         | 2 +-
aca4c4
 6 files changed, 6 insertions(+), 9 deletions(-)
aca4c4
aca4c4
diff --git a/iptables/ip6tables.c b/iptables/ip6tables.c
aca4c4
index 614d1e249c06d..b96dc033e7ebb 100644
aca4c4
--- a/iptables/ip6tables.c
aca4c4
+++ b/iptables/ip6tables.c
aca4c4
@@ -1247,7 +1247,7 @@ int do_command6(int argc, char *argv[], char **table,
aca4c4
 			break;
aca4c4
 
aca4c4
 		case 'N':
aca4c4
-			parse_chain(optarg);
aca4c4
+			assert_valid_chain_name(optarg);
aca4c4
 			add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
aca4c4
 				    cs.invert);
aca4c4
 			chain = optarg;
aca4c4
diff --git a/iptables/iptables.c b/iptables/iptables.c
aca4c4
index 3b395981cc8ea..6e2946f5660de 100644
aca4c4
--- a/iptables/iptables.c
aca4c4
+++ b/iptables/iptables.c
aca4c4
@@ -1243,7 +1243,7 @@ int do_command4(int argc, char *argv[], char **table,
aca4c4
 			break;
aca4c4
 
aca4c4
 		case 'N':
aca4c4
-			parse_chain(optarg);
aca4c4
+			assert_valid_chain_name(optarg);
aca4c4
 			add_command(&command, CMD_NEW_CHAIN, CMD_NONE,
aca4c4
 				    cs.invert);
aca4c4
 			chain = optarg;
aca4c4
diff --git a/iptables/xshared.c b/iptables/xshared.c
aca4c4
index de8326b6c7b05..0c232ca2ae8d5 100644
aca4c4
--- a/iptables/xshared.c
aca4c4
+++ b/iptables/xshared.c
aca4c4
@@ -776,7 +776,7 @@ int parse_rulenumber(const char *rule)
aca4c4
 	return rulenum;
aca4c4
 }
aca4c4
 
aca4c4
-void parse_chain(const char *chainname)
aca4c4
+void assert_valid_chain_name(const char *chainname)
aca4c4
 {
aca4c4
 	const char *ptr;
aca4c4
 
aca4c4
diff --git a/iptables/xshared.h b/iptables/xshared.h
aca4c4
index f5d2f8d0a2bc5..095a574d85879 100644
aca4c4
--- a/iptables/xshared.h
aca4c4
+++ b/iptables/xshared.h
aca4c4
@@ -217,6 +217,6 @@ char cmd2char(int option);
aca4c4
 void add_command(unsigned int *cmd, const int newcmd,
aca4c4
 		 const int othercmds, int invert);
aca4c4
 int parse_rulenumber(const char *rule);
aca4c4
-void parse_chain(const char *chainname);
aca4c4
+void assert_valid_chain_name(const char *chainname);
aca4c4
 
aca4c4
 #endif /* IPTABLES_XSHARED_H */
aca4c4
diff --git a/iptables/xtables-restore.c b/iptables/xtables-restore.c
aca4c4
index c472ac9bf651b..a078da32045dc 100644
aca4c4
--- a/iptables/xtables-restore.c
aca4c4
+++ b/iptables/xtables-restore.c
aca4c4
@@ -150,10 +150,7 @@ static void xtables_restore_parse_line(struct nft_handle *h,
aca4c4
 				   "%s: line %u chain name invalid\n",
aca4c4
 				   xt_params->program_name, line);
aca4c4
 
aca4c4
-		if (strlen(chain) >= XT_EXTENSION_MAXNAMELEN)
aca4c4
-			xtables_error(PARAMETER_PROBLEM,
aca4c4
-				   "Invalid chain name `%s' (%u chars max)",
aca4c4
-				   chain, XT_EXTENSION_MAXNAMELEN - 1);
aca4c4
+		assert_valid_chain_name(chain);
aca4c4
 
aca4c4
 		policy = strtok(NULL, " \t\n");
aca4c4
 		DEBUGP("line %u, policy '%s'\n", line, policy);
aca4c4
diff --git a/iptables/xtables.c b/iptables/xtables.c
aca4c4
index 3ea293ee7c411..9006962472c58 100644
aca4c4
--- a/iptables/xtables.c
aca4c4
+++ b/iptables/xtables.c
aca4c4
@@ -668,7 +668,7 @@ void do_parse(struct nft_handle *h, int argc, char *argv[],
aca4c4
 			break;
aca4c4
 
aca4c4
 		case 'N':
aca4c4
-			parse_chain(optarg);
aca4c4
+			assert_valid_chain_name(optarg);
aca4c4
 			add_command(&p->command, CMD_NEW_CHAIN, CMD_NONE,
aca4c4
 				    cs->invert);
aca4c4
 			p->chain = optarg;
aca4c4
-- 
aca4c4
2.34.1
aca4c4