Skip to contentSkip to conversation

iptables: Fix crash on malformed iptables-restore

PATCH In review Oliver Ford 2 participants 2 messages May 19, 2017 – May 29, 2017 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 iptables: Fix crash on malformed iptables-restore

Fixes the crash reported in Bugzilla #1131 where a malformed parameter that specifies the table option during a restore can create an invalid pointer. It was discovered during fuzz testing that options like '-ftf' can cause a segfault. A parameter that includes a 't' is not currently filtered correctly.

Improves the filtering to:
Filter a beginning '-' followed by a character other than '-' and then a 't' anywhere in the parameter. This filters parameters like '-ftf'. Filter '--t'.
Filter '--table', stopping when the parameter length is reached. Because the getopt_long function allows abbreviations, any unique abbreviation of '--table' will be treated as '--table'. This filters parameters like '--t', '--ta', but not '--ttl' or '--target'.

---
 iptables/ip6tables-restore.c | 6 ++++--
 iptables/iptables-restore.c  | 6 ++++--
 iptables/iptables-xml.c      | 7 ++++---
 iptables/xtables-restore.c   | 6 ++++--
 4 files changed, 16 insertions(+), 9 deletions(-)
iptables/ip6tables-restore.c +4 -2
index 39a881d..966f189 100644
@@ -165,8 +165,10 @@ static void add_param_to_argv(char *parsestart)
 			param_buffer[param_len] = '\0';
 
 			/* check if table name specified */
-			if (!strncmp(param_buffer, "-t", 2)
-                            || !strncmp(param_buffer, "--table", 8)) {
+			if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&
+				strchr(param_buffer, 't')) ||
+				(!strncmp(param_buffer, "--t", 3)
+				&& !strncmp(param_buffer, "--table", strlen(param_buffer)))) {
 				xtables_error(PARAMETER_PROBLEM,
 				"The -t option (seen in line %u) cannot be "
 				"used in ip6tables-restore.\n", line);
iptables/iptables-restore.c +4 -2
index 876fe06..18ea655 100644
@@ -162,8 +162,10 @@ static void add_param_to_argv(char *parsestart)
 			param_buffer[param_len] = '\0';
 
 			/* check if table name specified */
-			if (!strncmp(param_buffer, "-t", 2)
-			    || !strncmp(param_buffer, "--table", 8)) {
+			if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&
+				strchr(param_buffer, 't')) ||
+				(!strncmp(param_buffer, "--t", 3)
+				&& !strncmp(param_buffer, "--table", strlen(param_buffer)))) {
 				xtables_error(PARAMETER_PROBLEM,
 				"The -t option (seen in line %u) cannot be "
 				"used in iptables-restore.\n", line);
iptables/iptables-xml.c +4 -3
index 2e093b5..05e3bf8 100644
@@ -819,9 +819,10 @@ iptables_xml_main(int argc, char *argv[])
 					*(param_buffer + param_len) = '\0';
 
 					/* check if table name specified */
-					if (!strncmp(param_buffer, "-t", 3)
-					    || !strncmp(param_buffer,
-							"--table", 8)) {
+					if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&
+						strchr(param_buffer, 't')) ||
+						(!strncmp(param_buffer, "--t", 3)
+						&& !strncmp(param_buffer, "--table", strlen(param_buffer)))) {
 						xtables_error(PARAMETER_PROBLEM,
 							   "Line %u seems to have a "
 							   "-t table option.\n",
iptables/xtables-restore.c +4 -2
index 15824f0..e99231a 100644
@@ -136,8 +136,10 @@ static void add_param_to_argv(char *parsestart)
 			param_buffer[param_len] = '\0';
 
 			/* check if table name specified */
-			if (!strncmp(param_buffer, "-t", 2)
-			    || !strncmp(param_buffer, "--table", 8)) {
+			if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&
+				strchr(param_buffer, 't')) ||
+				(!strncmp(param_buffer, "--t", 3)
+				&& !strncmp(param_buffer, "--table", strlen(param_buffer)))) {
 				xtables_error(PARAMETER_PROBLEM,
 				"The -t option (seen in line %u) cannot be "
 				"used in xtables-restore.\n", line);
Signature · 1 line
2.11.0
#1 lore raw
Quoted 6 lines · Oliver Ford

Fixes the crash reported in Bugzilla #1131 where a malformed parameter that specifies the table option during a restore can create an invalid pointer. It was discovered during fuzz testing that options like '-ftf' can cause a segfault. A parameter that includes a 't' is not currently filtered correctly.

Quoted 8 lines

Improves the filtering to:
Filter a beginning '-' followed by a character other than '-' and then a 't' anywhere in the parameter. This filters parameters like '-ftf'. Filter '--t'.
Filter '--table', stopping when the parameter length is reached. Because the getopt_long function allows abbreviations, any unique abbreviation of '--table' will be treated as '--table'. This filters parameters like '--t', '--ta', but not '--ttl' or '--target'.

Applied with minor glitches.

Quoted 13 lines
---
 iptables/ip6tables-restore.c | 6 ++++--
 iptables/iptables-restore.c  | 6 ++++--
 iptables/iptables-xml.c      | 7 ++++---
 iptables/xtables-restore.c   | 6 ++++--
 4 files changed, 16 insertions(+), 9 deletions(-)
iptables/ip6tables-restore.c +0 -0
index 39a881d..966f189 100644

iptables/ip6tables-restore.c static void add_param_to_argv(char *parsestart) · line 171

Quoted 9 lines
iptables/ip6tables-restore.c +4 -2
@@ -165,8 +165,10 @@ static void add_param_to_argv(char *parsestart)
 			param_buffer[param_len] = '\0';
 
 			/* check if table name specified */
-			if (!strncmp(param_buffer, "-t", 2)
-                            || !strncmp(param_buffer, "--table", 8)) {
+			if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&
+				strchr(param_buffer, 't')) ||
+				(!strncmp(param_buffer, "--t", 3)
+				&& !strncmp(param_buffer, "--table", strlen(param_buffer)))) {

No blame. iptables coding style is a bit messy, but we try to make it converge to kernel coding style with this updates so I mangled this to:

			if ((param_buffer[0] == '-' &&
                             param_buffer[1] != '-' &&
                             strchr(param_buffer, 't')) ||
                            (!strncmp(param_buffer, "--t", 3) &&
                             !strncmp(param_buffer, "--table", strlen(param_buffer)))) {