Skip to contentSkip to conversation

iptables: Fix crash on malformed iptables-restore

PATCH Superseded Oliver Ford 2 participants 4 messages May 17, 2017 – May 18, 2017 netfilter

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 can create an invalid pointer.

Improves the tables option check to find a beginning '-' followed by a 't' anywhere in the parameter.

---
 iptables/ip6tables-restore.c |  3 +--
 iptables/iptables-restore.c  |  3 +--
 iptables/iptables-xml.c      | 10 ++++------
 iptables/xtables-restore.c   |  3 +--
 4 files changed, 7 insertions(+), 12 deletions(-)
iptables/ip6tables-restore.c +1 -2
index 39a881d..1a8cc5e 100644
@@ -165,8 +165,7 @@ 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] == '-' && strchr(param_buffer, 't')) {
 				xtables_error(PARAMETER_PROBLEM,
 				"The -t option (seen in line %u) cannot be "
 				"used in ip6tables-restore.\n", line);
iptables/iptables-restore.c +1 -2
index 876fe06..70dc7eb 100644
@@ -162,8 +162,7 @@ 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] == '-' && strchr(param_buffer, 't')) {
 				xtables_error(PARAMETER_PROBLEM,
 				"The -t option (seen in line %u) cannot be "
 				"used in iptables-restore.\n", line);
iptables/iptables-xml.c +4 -6
index 2e093b5..fe219ae 100644
@@ -819,13 +819,11 @@ 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] == '-' && strchr(param_buffer, 't')) {
 						xtables_error(PARAMETER_PROBLEM,
-							   "Line %u seems to have a "
-							   "-t table option.\n",
-							   line);
+							"Line %u seems to have a "
+							"-t table option.\n",
+							line);
 						exit(1);
 					}
 
iptables/xtables-restore.c +1 -2
index 15824f0..75a64a7 100644
@@ -136,8 +136,7 @@ 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] == '-' && strchr(param_buffer, 't')) {
 				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
maintainer
#1 lore raw
Quoted 12 lines · Oliver Ford

Fixes the crash reported in Bugzilla #1131 where a malformed parameter that specifies the table option can create an invalid pointer.

Improves the tables option check to find a beginning '-' followed by a 't' anywhere in the parameter.

iptables/iptables-restore.c +0 -0
index 876fe06..70dc7eb 100644

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

Quoted 9 lines
iptables/iptables-restore.c +1 -2
@@ -162,8 +162,7 @@ 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] == '-' && strchr(param_buffer, 't')) {
 				xtables_error(PARAMETER_PROBLEM,
 				"The -t option (seen in line %u) cannot be "
 				"used in iptables-restore.\n", line);

strchr() can only called if param_buffer[1] != '-', else this breaks something like:

# Generated by iptables-save v1.6.0 on Thu May 18 00:00:38 2017 *filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
--delete foo
COMMIT

(use '-n' option to iptables).

#2 lore raw
Quoted 34 lines · Florian Westphal
Quoted 12 lines · Oliver Ford

Fixes the crash reported in Bugzilla #1131 where a malformed parameter that specifies the table option can create an invalid pointer.

Improves the tables option check to find a beginning '-' followed by a 't' anywhere in the parameter.

iptables/iptables-restore.c +0 -0
index 876fe06..70dc7eb 100644

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

Quoted 9 lines
iptables/iptables-restore.c +1 -2
@@ -162,8 +162,7 @@ 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] == '-' && strchr(param_buffer, 't')) {
                              xtables_error(PARAMETER_PROBLEM,
                              "The -t option (seen in line %u) cannot be "
                              "used in iptables-restore.\n", line);

strchr() can only called if param_buffer[1] != '-', else this breaks something like:

# Generated by iptables-save v1.6.0 on Thu May 18 00:00:38 2017 *filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
--delete foo
COMMIT

Quoted 1 line

(use '-n' option to iptables).

Good point I'll check for that and send an updated patch.

maintainer
#3 lore raw
Quoted 35 lines · Oliver Ford
Quoted 34 lines · Florian Westphal
Quoted 12 lines · Oliver Ford

Fixes the crash reported in Bugzilla #1131 where a malformed parameter that specifies the table option can create an invalid pointer.

Improves the tables option check to find a beginning '-' followed by a 't' anywhere in the parameter.

iptables/iptables-restore.c +0 -0
index 876fe06..70dc7eb 100644

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

Quoted 9 lines
iptables/iptables-restore.c +1 -2
@@ -162,8 +162,7 @@ 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] == '-' && strchr(param_buffer, 't')) {
                              xtables_error(PARAMETER_PROBLEM,
                              "The -t option (seen in line %u) cannot be "
                              "used in iptables-restore.\n", line);

strchr() can only called if param_buffer[1] != '-', else this breaks something like:

# Generated by iptables-save v1.6.0 on Thu May 18 00:00:38 2017 *filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
--delete foo
COMMIT

Quoted 3 lines
Quoted 1 line

(use '-n' option to iptables).

Good point I'll check for that and send an updated patch.

Forgot to mention: Would be nice to explain in the commit message why this is needed, e.g. -nt, --tab, etc. are not caught by current test.