iptables-restore: move code to add_param_to_argv, cleanup (fix gcc-4.7)
[email protected]
maintainerPATCH 1/1 iptables-restore: move code to add_param_to_argv, cleanup (fix gcc-4.7)
From: Pablo Neira Ayuso <[email protected]>
This patch seems to be a mere cleanup that moves the parameter parsing code to add_param_to_argv.
But, in reality, it also fixes iptables whe compiled with gcc-4.7.
Moving param_buffer declaration out of the loop seems to resolve the issue. gcc-4.7 seems to be generating bad code regarding param_buffer.
@@ -380,9 +380,9 @@
quote_open = 0;
escaped = 0;
param_len = 0;+ char param_buffer[1024];
for (curchar = parsestart; *curchar; curchar++) {- char param_buffer[1024];
if (quote_open) {
if (escaped) {But I have hard time to apply this patch in such a way. Instead, I came up with the idea of this cleanup, which does not harm after all (and fixes the issue for us).
Sorry, I didn't have the time to further debug this issue, but it would be worth to investigate what's going wrong and ping gcc people.
--- iptables/ip6tables-restore.c | 133 +++++++++++++++++++++--------------------- iptables/iptables-restore.c | 133 +++++++++++++++++++++--------------------- 2 files changed, 130 insertions(+), 136 deletions(-)
iptables/ip6tables-restore.c +65 -68
@@ -114,6 +114,70 @@ static void free_argv(void) {free (newargv[i]); } +static void add_param_to_argv (char *parsestart) +{ +int quote_open =0 , escaped =0 , param_len =0 ; +char param_buffer[1024 ], *curchar; + +/* After fighting with strtok enough, here's now +* a 'real' parser. According to Rusty I'm now no +* longer a real hacker, but I can live with that */ + +for (curchar = parsestart; *curchar; curchar++) { +if (quote_open) { +if (escaped) { + param_buffer[param_len++] = *curchar; + escaped =0 ; +continue ; + }else if (*curchar =='\\' ) { + escaped =1 ; +continue ; + }else if (*curchar =='"' ) { + quote_open =0 ; + *curchar =' ' ; + }else { + param_buffer[param_len++] = *curchar; +continue ; + } + }else { +if (*curchar =='"' ) { + quote_open =1 ; +continue ; + } + } + +if (*curchar ==' ' + || *curchar =='\t' + || * curchar =='\n' ) { +if (!param_len) { +/* two spaces? */ +continue ; + } + + param_buffer[param_len] ='\0' ; + +/* check if table name specified */ +if (!strncmp (param_buffer,"-t" ,2 ) + || !strncmp (param_buffer,"--table" ,8 )) { +xtables_error (PARAMETER_PROBLEM , +"Line %u seems to have a " +"-t table option.\n" , line); +exit (1 ); + } + +add_argv (param_buffer); + param_len =0 ; + }else { +/* regular character, copy to buffer */ + param_buffer[param_len++] = *curchar; + +if (param_len >=sizeof (param_buffer)) +xtables_error (PARAMETER_PROBLEM , +"Parameter too long!" ); + } + } +} +int ip6tables_restore_main (int argc,char *argv[]) {struct xtc_handle *handle =NULL ;
@@ -325,11 +389,6 @@ int ip6tables_restore_main(int argc, char *argv[])char *bcnt =NULL ;char *parsestart; -/* the parser */ -char *curchar; -int quote_open, escaped; -size_t param_len; -/* reset the newargv */ newargc =0 ;
@@ -370,69 +429,7 @@ int ip6tables_restore_main(int argc, char *argv[])add_argv ((char *) bcnt); } -/* After fighting with strtok enough, here's now -* a 'real' parser. According to Rusty I'm now no -* longer a real hacker, but I can live with that */ - - quote_open =0 ; - escaped =0 ; - param_len =0 ; - -for (curchar = parsestart; *curchar; curchar++) { -char param_buffer[1024 ]; - -if (quote_open) { -if (escaped) { - param_buffer[param_len++] = *curchar; - escaped =0 ; -continue ; - }else if (*curchar =='\\' ) { - escaped =1 ; -continue ; - }else if (*curchar =='"' ) { - quote_open =0 ; - *curchar =' ' ; - }else { - param_buffer[param_len++] = *curchar; -continue ; - } - }else { -if (*curchar =='"' ) { - quote_open =1 ; -continue ; - } - } - -if (*curchar ==' ' - || *curchar =='\t' - || * curchar =='\n' ) { -if (!param_len) { -/* two spaces? */ -continue ; - } - - param_buffer[param_len] ='\0' ; - -/* check if table name specified */ -if (!strncmp (param_buffer,"-t" ,2 ) - || !strncmp (param_buffer,"--table" ,8 )) { -xtables_error (PARAMETER_PROBLEM , -"Line %u seems to have a " -"-t table option.\n" , line); -exit (1 ); - } - -add_argv (param_buffer); - param_len =0 ; - }else { -/* regular character, copy to buffer */ - param_buffer[param_len++] = *curchar; - -if (param_len >=sizeof (param_buffer)) -xtables_error (PARAMETER_PROBLEM , -"Parameter too long!" ); - } - } +add_param_to_argv (parsestart);DEBUGP ("calling do_command6(%u, argv, &%s, handle):\n" , newargc, curtable);
iptables/iptables-restore.c +65 -68
151 lines not on this page · raw message
Signature · 1 line
1.7.10.4
Quoted 22 lines · On Mon, 2012-07-23 at 12:38 +0200, [email protected] wrote:
From: Pablo Neira Ayuso <[email protected]>
This patch seems to be a mere cleanup that moves the parameter parsing code to
add_param_to_argv.But, in reality, it also fixes iptables whe compiled with gcc-4.7.
Moving
param_bufferdeclaration out of the loop seems to resolve the issue. gcc-4.7 seems to be generating bad code regardingparam_buffer.(quoted diff) +1
-1@@ -380,9 +380,9 @@ quote_open = 0; escaped = 0; param_len = 0; + char param_buffer[1024]; for (curchar = parsestart; *curchar; curchar++) { - char param_buffer[1024]; if (quote_open) { if (escaped) {
Quoted 6 lines
But I have hard time to apply this patch in such a way. Instead, I came up with the idea of this cleanup, which does not harm after all (and fixes the issue for us).
Sorry, I didn't have the time to further debug this issue, but it would be worth to investigate what's going wrong and ping gcc people.
Bug seems that iptables forgot that "char param_buffer[1024];" can disappear at the end of the block :
for (curchar = parsestart; *curchar; curchar++) {
char param_buffer[1024];
...
}// here param_buffer[1024] is lost, so any var pointing // to it can mess stack
previous gcc were probably not so aggressive.
Quoted 38 lines · Eric Dumazet
Quoted 22 lines · On Mon, 2012-07-23 at 12:38 +0200, [email protected] wrote:
From: Pablo Neira Ayuso <[email protected]>
This patch seems to be a mere cleanup that moves the parameter parsing code to
add_param_to_argv.But, in reality, it also fixes iptables whe compiled with gcc-4.7.
Moving
param_bufferdeclaration out of the loop seems to resolve the issue. gcc-4.7 seems to be generating bad code regardingparam_buffer.(quoted diff) +1
-1@@ -380,9 +380,9 @@ quote_open = 0; escaped = 0; param_len = 0; + char param_buffer[1024]; for (curchar = parsestart; *curchar; curchar++) { - char param_buffer[1024]; if (quote_open) { if (escaped) {Quoted 6 lines
But I have hard time to apply this patch in such a way. Instead, I came up with the idea of this cleanup, which does not harm after all (and fixes the issue for us).
Sorry, I didn't have the time to further debug this issue, but it would be worth to investigate what's going wrong and ping gcc people.
Bug seems that iptables forgot that "char param_buffer[1024];" can disappear at the end of the block :
for (curchar = parsestart; *curchar; curchar++) { char param_buffer[1024]; ... }
Quoted 6 lines
// here param_buffer[1024] is lost, so any var pointing // to it can mess stack
previous gcc were probably not so aggressive.
Oh well, add_argv() does a strdup(), so iptables code seems fine.
Pablo Neira Ayuso
maintainerQuoted 39 lines · Eric Dumazet
Quoted 38 lines · Eric Dumazet
Quoted 22 lines · On Mon, 2012-07-23 at 12:38 +0200, [email protected] wrote:
From: Pablo Neira Ayuso <[email protected]>
This patch seems to be a mere cleanup that moves the parameter parsing code to
add_param_to_argv.But, in reality, it also fixes iptables whe compiled with gcc-4.7.
Moving
param_bufferdeclaration out of the loop seems to resolve the issue. gcc-4.7 seems to be generating bad code regardingparam_buffer.(quoted diff) +1
-1@@ -380,9 +380,9 @@ quote_open = 0; escaped = 0; param_len = 0; + char param_buffer[1024]; for (curchar = parsestart; *curchar; curchar++) { - char param_buffer[1024]; if (quote_open) { if (escaped) {Quoted 6 lines
But I have hard time to apply this patch in such a way. Instead, I came up with the idea of this cleanup, which does not harm after all (and fixes the issue for us).
Sorry, I didn't have the time to further debug this issue, but it would be worth to investigate what's going wrong and ping gcc people.
Bug seems that iptables forgot that "char param_buffer[1024];" can disappear at the end of the block :
for (curchar = parsestart; *curchar; curchar++) { char param_buffer[1024]; ... }
Quoted 6 lines
Quoted 4 lines
// here param_buffer[1024] is lost, so any var pointing // to it can mess stack
previous gcc were probably not so aggressive.
Oh well,
add_argv()does astrdup(), so iptables code seems fine.
I think so, I don't find any possible dereference to param_buffer out of that loop and it does strdup accordingly.
Pablo Neira Ayuso
maintainerQuoted 39 lines · Eric Dumazet
Quoted 38 lines · Eric Dumazet
Quoted 22 lines · On Mon, 2012-07-23 at 12:38 +0200, [email protected] wrote:
From: Pablo Neira Ayuso <[email protected]>
This patch seems to be a mere cleanup that moves the parameter parsing code to
add_param_to_argv.But, in reality, it also fixes iptables whe compiled with gcc-4.7.
Moving
param_bufferdeclaration out of the loop seems to resolve the issue. gcc-4.7 seems to be generating bad code regardingparam_buffer.(quoted diff) +1
-1@@ -380,9 +380,9 @@ quote_open = 0; escaped = 0; param_len = 0; + char param_buffer[1024]; for (curchar = parsestart; *curchar; curchar++) { - char param_buffer[1024]; if (quote_open) { if (escaped) {Quoted 6 lines
But I have hard time to apply this patch in such a way. Instead, I came up with the idea of this cleanup, which does not harm after all (and fixes the issue for us).
Sorry, I didn't have the time to further debug this issue, but it would be worth to investigate what's going wrong and ping gcc people.
Bug seems that iptables forgot that "char param_buffer[1024];" can disappear at the end of the block :
for (curchar = parsestart; *curchar; curchar++) { char param_buffer[1024]; ... }
Quoted 6 lines
Quoted 4 lines
// here param_buffer[1024] is lost, so any var pointing // to it can mess stack
previous gcc were probably not so aggressive.
Oh well,
add_argv()does astrdup(), so iptables code seems fine.
I thought the same, but one contributor has put some on light on this.
I'm going to revert the patch that I applied to fix this and apply the one that comes with this email instead.
It contains a simple description of the problem, I think it's good for the record (distro maintainers will likely google for this).
From 8cfcb0137f4aee880ec749cd2356148325f6085d Mon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Mon, 30 Jul 2012 03:08:51 +0200
Subject: [PATCH] iptables-restore: fix memory corruption in parameter parsing
with gcc-4.7
This patch fixes a memory corruption that has been in iptables-restore since time ago. The problem has shown up with gcc-4.7. This version of gcc seem to perform more agressive memory management than previous.
Peter Lekensteyn provided the following sample code similar to the one in iptables-restore:
int i = 0;
for (;;) {
char x[5];
x[i] = '0' + i;
if (++i == 4) {
x[i] = '\0'; /* terminate string with null byte */
printf("%s\n", x);
break;
}
}Many may expect 0123 as output. But GCC 4.7 does not do that when compiling with optimization enabled (-O1 and higher). It instead puts random data in the first bytes of the character array, which becomes:
| 0 | 1 | 2 | 3 | 4 | | RANDOM | '3' | '\0' |
Since the array is declared inside the scope of loop's body, you can think of it as of a new array being allocated in the automatic storage area for each loop iteration.
The correct code should be:
char x[5];
for (;;) {
x[i] = '0' + i;
if (++i == 4) {
x[i] = '\0'; /* terminate string with null byte */
printf("%s\n", x);
break;
}
}--- iptables/ip6tables-restore.c | 3 +-- iptables/iptables-restore.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-)
iptables/ip6tables-restore.c +1 -2
@@ -329,6 +329,7 @@ int ip6tables_restore_main(int argc, char *argv[])char *curchar;int quote_open, escaped;size_t param_len; +char param_buffer[1024 ];/* reset the newargv */ newargc =0 ;
@@ -379,8 +380,6 @@ int ip6tables_restore_main(int argc, char *argv[]) param_len =0 ;for (curchar = parsestart; *curchar; curchar++) { -char param_buffer[1024 ]; -if (quote_open) {if (escaped) { param_buffer[param_len++] = *curchar;
iptables/iptables-restore.c +1 -2
@@ -329,6 +329,7 @@ iptables_restore_main(int argc, char *argv[])char *curchar;int quote_open, escaped;size_t param_len; +char param_buffer[1024 ];/* reset the newargv */ newargc =0 ;
@@ -379,8 +380,6 @@ iptables_restore_main(int argc, char *argv[]) param_len =0 ;for (curchar = parsestart; *curchar; curchar++) { -char param_buffer[1024 ]; -if (quote_open) {if (escaped) { param_buffer[param_len++] = *curchar;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerQuoted 5 lines · Pablo Neira Ayuso
From
8cfcb0137f4aee880ec749cd2356148325f6085dMon Sep 17 00:00:00 2001
From: Pablo Neira Ayuso <[email protected]>
Date: Mon, 30 Jul 2012 03:08:51 +0200
Subject: [PATCH] iptables-restore: fix memory corruption in parameter parsing
with gcc-4.7
I'm going to change the title of this patch, memory corruption does not seem appropriate. Instead, the problem seems to be related to wrong assumptions on uninitialized memory area.
Quoted 7 lines · Pablo Neira Ayuso
Quoted 6 lines
Quoted 4 lines
// here param_buffer[1024] is lost, so any var pointing // to it can mess stack
previous gcc were probably not so aggressive.
Oh well,
add_argv()does astrdup(), so iptables code seems fine.
Quoted 7 lines
I thought the same, but one contributor has put some on light on this.
I'm going to revert the patch that I applied to fix this and apply the one that comes with this email instead.
It contains a simple description of the problem, I think it's good for the record (distro maintainers will likely google for this).
Your code cleanup, by moving the code into a separate function, is however still desired :)
Pablo Neira Ayuso
maintainerQuoted 11 lines · Jan Engelhardt
Quoted 9 lines · Pablo Neira Ayuso
Quoted 6 lines
Quoted 4 lines
// here param_buffer[1024] is lost, so any var pointing // to it can mess stack
previous gcc were probably not so aggressive.
Oh well,
add_argv()does astrdup(), so iptables code seems fine.I thought the same, but one contributor has put some on light on this.
Quoted 8 lines
Quoted 5 lines
I'm going to revert the patch that I applied to fix this and apply the one that comes with this email instead.
It contains a simple description of the problem, I think it's good for the record (distro maintainers will likely google for this).
Your code cleanup, by moving the code into a separate function, is however still desired :)
OK, it's back:
Quoted 6 lines · On Monday 2012-07-23 12:38, [email protected] wrote:
From: Pablo Neira Ayuso <[email protected]>
This patch seems to be a mere cleanup that moves the parameter parsing code to
add_param_to_argv.But, in reality, it also fixes iptables whe compiled with gcc-4.7.
How does the problem manifest?
Pablo Neira Ayuso
maintainerQuoted 10 lines · Jan Engelhardt
Quoted 6 lines · On Monday 2012-07-23 12:38, [email protected] wrote:
From: Pablo Neira Ayuso <[email protected]>
This patch seems to be a mere cleanup that moves the parameter parsing code to
add_param_to_argv.But, in reality, it also fixes iptables whe compiled with gcc-4.7.
How does the problem manifest?
You can check:
http://bugzilla.netfilter.org/show_bug.cgi?id=774
It tastes like a memory corruption.