Skip to contentSkip to conversation

iptables: Fix crash on malformed iptables-restore

PATCH Superseded Oliver Ford 2 participants 8 messages May 19, 2017 netfilter

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 a beginning '--t'. Because the getopt_long function allows abbreviations, any parameter beginning with '--t' will be treated as '--table'. As no other iptables option begins with a 't' this won't filter out other options.

---
 iptables/ip6tables-restore.c |  9 +++++----
 iptables/iptables-restore.c  |  9 +++++----
 iptables/iptables-xml.c      | 11 +++++------
 iptables/xtables-restore.c   |  9 +++++----
 4 files changed, 20 insertions(+), 18 deletions(-)
iptables/ip6tables-restore.c +5 -4
index 39a881d..46e9fae 100644
@@ -165,11 +165,12 @@ 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)) {
 				xtables_error(PARAMETER_PROBLEM,
-				"The -t option (seen in line %u) cannot be "
-				"used in ip6tables-restore.\n", line);
+					"The -t option (seen in line %u) cannot be "
+					"used in ip6tables-restore.\n", line);
 				exit(1);
 			}
 
iptables/iptables-restore.c +5 -4
index 876fe06..9b06395 100644
@@ -162,11 +162,12 @@ 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)) {
 				xtables_error(PARAMETER_PROBLEM,
-				"The -t option (seen in line %u) cannot be "
-				"used in iptables-restore.\n", line);
+					"The -t option (seen in line %u) cannot be "
+					"used in iptables-restore.\n", line);
 				exit(1);
 			}
 
iptables/iptables-xml.c +5 -6
index 2e093b5..d836ff4 100644
@@ -819,13 +819,12 @@ 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)) {
 						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 +5 -4
index 15824f0..994e04e 100644
@@ -136,11 +136,12 @@ 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)) {
 				xtables_error(PARAMETER_PROBLEM,
-				"The -t option (seen in line %u) cannot be "
-				"used in xtables-restore.\n", line);
+					"The -t option (seen in line %u) cannot be "
+					"used in xtables-restore.\n", line);
 				exit(1);
 			}
 
Signature · 1 line
2.11.0
maintainer
#1 lore raw
Quoted 2 lines · Oliver Ford

Filter a beginning '--t'. Because the getopt_long function allows abbreviations, any parameter beginning with '--t' will be treated as '--table'.

No, thats not correct:
--t is treated as --table.
--tfoo is an invalid option.
--ttl is ttl.

So this:

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

Quoted 4 lines
iptables/ip6tables-restore.c +3 -0
+				|| !strncmp(param_buffer, "--t", 3)) {
 				xtables_error(PARAMETER_PROBLEM,
+					"The -t option (seen in line %u) cannot be "
+					"used in ip6tables-restore.\n", line);

.. rejects rules like

-A INPUT -m ttl --ttl 32

#2 lore raw
Quoted 11 lines · Florian Westphal
Quoted 2 lines · Oliver Ford

Filter a beginning '--t'. Because the getopt_long function allows abbreviations, any parameter beginning with '--t' will be treated as '--table'.

No, thats not correct:
--t is treated as --table.
--tfoo is an invalid option.
--ttl is ttl.

So this:

Quoted 8 lines

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

Quoted 4 lines
iptables/ip6tables-restore.c +3 -0
+                             || !strncmp(param_buffer, "--t", 3)) {
                              xtables_error(PARAMETER_PROBLEM,
+                                     "The -t option (seen in line %u) cannot be "
+                                     "used in ip6tables-restore.\n", line);

.. rejects rules like

-A INPUT -m ttl --ttl 32

Would strncmp(param_buffer, "--ta", 4) work? I don't think there are any options that begin with --ta other than --table.

maintainer
#3 lore raw
Quoted 17 lines · Oliver Ford
Quoted 16 lines · Florian Westphal
Quoted 2 lines · Oliver Ford

Filter a beginning '--t'. Because the getopt_long function allows abbreviations, any parameter beginning with '--t' will be treated as '--table'.

No, thats not correct:
--t is treated as --table.
--tfoo is an invalid option.
--ttl is ttl.

So this:

Quoted 4 lines
iptables/ip6tables-restore.c +3 -0
+                             || !strncmp(param_buffer, "--t", 3)) {
                              xtables_error(PARAMETER_PROBLEM,
+                                     "The -t option (seen in line %u) cannot be "
+                                     "used in ip6tables-restore.\n", line);
Quoted 6 lines
Quoted 3 lines

.. rejects rules like

-A INPUT -m ttl --ttl 32

Would strncmp(param_buffer, "--ta", 4) work? I don't think there are any options that begin with --ta other than --table.

That won't catch '--t'.

It will also add trouble later if any module adds an option like --tap, --tail, --target, etc.

Whats wrong with:

if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&

     strchr(param_buffer, 't') ||
     (!strncmp(param_buffer, "--t", 3) &&
      !strncmp(param_buffer, "--table", strlen(param_buffer)))) {

?

#4 lore raw
Quoted 30 lines · Florian Westphal
Quoted 17 lines · Oliver Ford
Quoted 16 lines · Florian Westphal
Quoted 2 lines · Oliver Ford

Filter a beginning '--t'. Because the getopt_long function allows abbreviations, any parameter beginning with '--t' will be treated as '--table'.

No, thats not correct:
--t is treated as --table.
--tfoo is an invalid option.
--ttl is ttl.

So this:

Quoted 4 lines
iptables/ip6tables-restore.c +3 -0
+                             || !strncmp(param_buffer, "--t", 3)) {
                              xtables_error(PARAMETER_PROBLEM,
+                                     "The -t option (seen in line %u) cannot be "
+                                     "used in ip6tables-restore.\n", line);
Quoted 6 lines
Quoted 3 lines

.. rejects rules like

-A INPUT -m ttl --ttl 32

Would strncmp(param_buffer, "--ta", 4) work? I don't think there are any options that begin with --ta other than --table.

That won't catch '--t'.

It will also add trouble later if any module adds an option like --tap, --tail, --target, etc.

Quoted 8 lines

Whats wrong with:

if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&

     strchr(param_buffer, 't') ||
     (!strncmp(param_buffer, "--t", 3) &&
      !strncmp(param_buffer, "--table", strlen(param_buffer)))) {

?

Yes I think that will work. Will test and resend.

#5 lore raw
In reply to Florian Westphal
Quoted 30 lines · Florian Westphal
Quoted 17 lines · Oliver Ford
Quoted 16 lines · Florian Westphal
Quoted 2 lines · Oliver Ford

Filter a beginning '--t'. Because the getopt_long function allows abbreviations, any parameter beginning with '--t' will be treated as '--table'.

No, thats not correct:
--t is treated as --table.
--tfoo is an invalid option.
--ttl is ttl.

So this:

Quoted 4 lines
iptables/ip6tables-restore.c +3 -0
+                             || !strncmp(param_buffer, "--t", 3)) {
                              xtables_error(PARAMETER_PROBLEM,
+                                     "The -t option (seen in line %u) cannot be "
+                                     "used in ip6tables-restore.\n", line);
Quoted 6 lines
Quoted 3 lines

.. rejects rules like

-A INPUT -m ttl --ttl 32

Would strncmp(param_buffer, "--ta", 4) work? I don't think there are any options that begin with --ta other than --table.

That won't catch '--t'.

It will also add trouble later if any module adds an option like --tap, --tail, --target, etc.

Quoted 8 lines

Whats wrong with:

if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&

     strchr(param_buffer, 't') ||
     (!strncmp(param_buffer, "--t", 3) &&
      !strncmp(param_buffer, "--table", strlen(param_buffer)))) {

?

I've just sent v4 that definitely works now. If you've got "!strncmp(param_buffer, "--table", strlen(param_buffer))" you don't also need "!strncmp(param_buffer, "--t", 3)" as --t will get filtered.

I've tested --t --ta --tab --tabl --table gets filtered but not --ttl, --target, --type.

Also -ftf still gets filtered. Thanks for bearing with this.

maintainer
#6 lore raw
Quoted 38 lines · Oliver Ford
Quoted 37 lines · Florian Westphal
Quoted 17 lines · Oliver Ford
Quoted 16 lines · Florian Westphal
Quoted 2 lines · Oliver Ford

Filter a beginning '--t'. Because the getopt_long function allows abbreviations, any parameter beginning with '--t' will be treated as '--table'.

No, thats not correct:
--t is treated as --table.
--tfoo is an invalid option.
--ttl is ttl.

So this:

Quoted 4 lines
iptables/ip6tables-restore.c +3 -0
+                             || !strncmp(param_buffer, "--t", 3)) {
                              xtables_error(PARAMETER_PROBLEM,
+                                     "The -t option (seen in line %u) cannot be "
+                                     "used in ip6tables-restore.\n", line);
Quoted 6 lines
Quoted 3 lines

.. rejects rules like

-A INPUT -m ttl --ttl 32

Would strncmp(param_buffer, "--ta", 4) work? I don't think there are any options that begin with --ta other than --table.

That won't catch '--t'.

It will also add trouble later if any module adds an option like --tap, --tail, --target, etc.

Whats wrong with:

if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&

     strchr(param_buffer, 't') ||
     (!strncmp(param_buffer, "--t", 3) &&
      !strncmp(param_buffer, "--table", strlen(param_buffer)))) {
Quoted 5 lines
Quoted 1 line

?

I've just sent v4 that definitely works now. If you've got "!strncmp(param_buffer, "--table", strlen(param_buffer))" you don't also need "!strncmp(param_buffer, "--t", 3)" as --t will get filtered.

Its needed, else '--' marker gets detected as 'table', e.g.:
-A INPUT -m ttl --ttl 32 -j ACCEPT --

#7 lore raw
Quoted 39 lines · Florian Westphal
Quoted 38 lines · Oliver Ford
Quoted 37 lines · Florian Westphal
Quoted 17 lines · Oliver Ford
Quoted 16 lines · Florian Westphal
Quoted 2 lines · Oliver Ford

Filter a beginning '--t'. Because the getopt_long function allows abbreviations, any parameter beginning with '--t' will be treated as '--table'.

No, thats not correct:
--t is treated as --table.
--tfoo is an invalid option.
--ttl is ttl.

So this:

Quoted 4 lines
iptables/ip6tables-restore.c +3 -0
+                             || !strncmp(param_buffer, "--t", 3)) {
                              xtables_error(PARAMETER_PROBLEM,
+                                     "The -t option (seen in line %u) cannot be "
+                                     "used in ip6tables-restore.\n", line);
Quoted 6 lines
Quoted 3 lines

.. rejects rules like

-A INPUT -m ttl --ttl 32

Would strncmp(param_buffer, "--ta", 4) work? I don't think there are any options that begin with --ta other than --table.

That won't catch '--t'.

It will also add trouble later if any module adds an option like --tap, --tail, --target, etc.

Whats wrong with:

if ((param_buffer[0] == '-' && param_buffer[1] != '-' &&

     strchr(param_buffer, 't') ||
     (!strncmp(param_buffer, "--t", 3) &&
      !strncmp(param_buffer, "--table", strlen(param_buffer)))) {
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

?

I've just sent v4 that definitely works now. If you've got "!strncmp(param_buffer, "--table", strlen(param_buffer))" you don't also need "!strncmp(param_buffer, "--t", 3)" as --t will get filtered.

Its needed, else '--' marker gets detected as 'table', e.g.:
-A INPUT -m ttl --ttl 32 -j ACCEPT --

Ok I've put that back in in v5 and just sent it.