osf: fixed /proc reading bug
PATCH 2/2 osf: fixed /proc reading bug
Fixed buffer overflow when reading rules from /proc file.
Evgeniy Polyakov ( s0mbre )
Only failure makes us experts. -- Theo de Raadt
Quoted 2 lines · Evgeniy Polyakov
Fixed buffer overflow when reading rules from /proc file.
How is this supposed to fix it ?
log("%s [%s]", f->genre, f->details);- count += sprintf(buf+count, "%s - %s[%s] : %s",
+ err = snprintf(buf+count, __count-count, "%s - %s[%s] : %s",
f->genre, f->version,
f->subtype, f->details);-
+ if (err < 0)
+ break;
+ else
+ count += err;
if (f->opt_num)
{
loga(" OPT: ");snprintf returns the number of characters written if n <= limit, otherwise the number of characters that would have been generated for the given input, but never < 0. You can also use vscnprintf to get the real number of bytes written.
Regards
Patrick
Quoted 1 line · Patrick McHardy
How is this supposed to fix it ?
The switch from sprintf to snprintf is a good start.
Review
Quoted 8 lines
log("%s [%s]", f->genre, f->details);(quoted diff) +2
-2- count += sprintf(buf+count, "%s - %s[%s] : %s", + err = snprintf(buf+count, __count-count, "%s - %s[%s] : %s", f->genre, f->version, f->subtype, f->details); - + if (err < 0)
This should read something like follows I think:
if (err < 0 || err > __count-count)
Quoted 11 lines
(quoted diff) +3
-0+ break; + else + count += err; if (f->opt_num) { loga(" OPT: ");snprintf returns the number of characters written if n <= limit, otherwise the number of characters that would have been generated for the given input, but never < 0. You can also use vscnprintf to get the real number of bytes written.
There may still be systems around with old snprintf implementations returning -1 on overflow unless this is kernel code.
Regards
Henrik
Quoted 13 lines · Henrik Nordstrom
Review
Quoted 9 lines · Patrick McHardy
log("%s [%s]", f->genre, f->details);(quoted diff) +1
-1- count += sprintf(buf+count, "%s - %s[%s] : %s", + err = snprintf(buf+count, __count-count, "%s - %s[%s]: %s",
f->genre, f->version, f->subtype, f->details);(quoted diff) +1
-1- + if (err < 0)
Quoted 11 lines
This should read something like follows I think:
if (err < 0 || err > __count-count)Quoted 6 lines
(quoted diff) +3
-0+ break; + else + count += err; if (f->opt_num) { loga(" OPT: ");
Yes, but the check for < 0 is not needed and it should read >= __count - count to avoid useless zero-byte snprintfs. Evgeniy, can you please send a new patch ?
Quoted 8 lines
Quoted 5 lines
snprintf returns the number of characters written if n <= limit, otherwise the number of characters that would have been generated for the given input, but never < 0. You can also use vscnprintf to get the real number of bytes written.
There may still be systems around with old snprintf implementations returning -1 on overflow unless this is kernel code.
It is. The *nprintf behaviour really is surprising (and annoying for chaining multiple snprintfs), given the behaviour of the *printf functions. OTOH, there is no other way to distinguish "n bytes written" with "truncated to n bytes" and returning -1 is even worse.
Regards
Patrick
Quoted 27 lines · Patrick McHardy
Quoted 13 lines · Henrik Nordstrom
Review
Quoted 9 lines · Patrick McHardy
log("%s [%s]", f->genre, f->details);(quoted diff) +1
-1- count += sprintf(buf+count, "%s - %s[%s] : %s", + err = snprintf(buf+count, __count-count, "%s - %s[%s]: %s",
f->genre, f->version, f->subtype, f->details);(quoted diff) +1
-1- + if (err < 0)Quoted 11 lines
This should read something like follows I think:
if (err < 0 || err > __count-count)Quoted 6 lines
(quoted diff) +3
-0+ break; + else + count += err; if (f->opt_num) { loga(" OPT: ");
Quoted 4 lines
Yes, but the check for < 0 is not needed and it should read >=
__count- count
to avoid useless zero-byte snprintfs. Evgeniy, can you please send a new patch ?
Sure.
It simply checks if return value from snprintf is 0 and breaks, otherwise it proceeds.
ipt_osf.diff.1 - patch for 2.6
ipt_osf.diff.1.24 - patch for 2.4
Quoted 2 lines
Regards
Patrick
Signature · 3 lines
Evgeniy Polyakov ( s0mbre ) Crash is better than data corruption. -- Art Grabowski
patch-o-matic-ng/osf/linux-2.4/net/ipv4/netfilter/ipt_osf.c +12 -5
@@ -182,7 +185,6 @@ optsize = tcp->doff*4 -sizeof (struct tcphdr ); } -/* Actually we can create hash/table of all genres and search * only in appropriate part, but here is initial variant, * so will use slow path.
@@ -601,9 +603,10 @@ {struct list_head *ent;struct osf_finger *f =NULL ; -int i; +int i, __count, err; *eof =1 ; + __count = count; count =0 ;read_lock_bh (&osf_lock);
@@ -613,10 +616,13 @@log ("%s [%s]" , f->genre, f->details); - count +=sprintf (buf+count,"%s - %s[%s] : %s" , + err =snprintf (buf+count, __count-count,"%s - %s[%s] : %s" , f->genre, f->version, f->subtype, f->details); - +if (err ==0 ) +break ; +else + count += err;if (f->opt_num) {loga (" OPT: " );
@@ -630,7 +636,11 @@ } }loga ("\n" ); - count +=sprintf (buf+count,"\n" ); + err =snprintf (buf+count, __count-count,"\n" ); +if (err ==0 ) +break ; +else + count += err; }read_unlock_bh (&osf_lock);
patch-o-matic-ng/osf/linux-2.4/net/ipv4/netfilter/ipt_osf.c +12 -5
@@ -182,7 +185,6 @@ optsize = tcp->doff*4 -sizeof (struct tcphdr ); } -/* Actually we can create hash/table of all genres and search * only in appropriate part, but here is initial variant, * so will use slow path.
@@ -601,9 +603,10 @@ {struct list_head *ent;struct osf_finger *f =NULL ; -int i; +int i, __count, err; *eof =1 ; + __count = count; count =0 ;read_lock_bh (&osf_lock);
@@ -613,10 +616,13 @@log ("%s [%s]" , f->genre, f->details); - count +=sprintf (buf+count,"%s - %s[%s] : %s" , + err =snprintf (buf+count, __count-count,"%s - %s[%s] : %s" , f->genre, f->version, f->subtype, f->details); - +if (err ==0 ) +break ; +else + count += err;if (f->opt_num) {loga (" OPT: " );
@@ -630,7 +636,11 @@ } }loga ("\n" ); - count +=sprintf (buf+count,"\n" ); + err =snprintf (buf+count, __count-count,"\n" ); +if (err ==0 ) +break ; +else + count += err; }read_unlock_bh (&osf_lock);
Quoted 3 lines · Evgeniy Polyakov
It simply checks if return value from snprintf is 0 and breaks, otherwise it proceeds.
Still broken. snprintf returns a value > n if it truncated to n bytes. See my last mail again. BTW, did the overflow actually cause problems ? proc has an extra k of space just for overflows ..
Regards
Patrick
Quoted 121 lines
ipt_osf.diff.1 - patch for 2.6
ipt_osf.diff.1.24 - patch for 2.4Signed-off-by Evgeniy Polyakov <[email protected]>Quoted 114 lines
Regards
Patrick------------------------------------------------------------------------
patch-o-matic-ng/osf/linux-2.4/net/ipv4/netfilter/ipt_osf.c +12
-6@@ -182,7 +185,6 @@ optsize = tcp->doff*4 -sizeof (struct tcphdr ); } -/* Actually we can create hash/table of all genres and search * only in appropriate part, but here is initial variant, * so will use slow path. @@ -601,9 +603,10 @@ {struct list_head *ent;struct osf_finger *f =NULL ; -int i; +int i, __count, err; *eof =1 ; + __count = count; count =0 ;read_lock_bh (&osf_lock);@@ -613,10 +616,13 @@log ("%s [%s]" , f->genre, f->details); - count +=sprintf (buf+count,"%s - %s[%s] : %s" , + err =snprintf (buf+count, __count-count,"%s - %s[%s] : %s" , f->genre, f->version, f->subtype, f->details); - +if (err ==0 ) +break ; +else + count += err;if (f->opt_num) {loga (" OPT: " );@@ -630,7 +636,11 @@ } }loga ("\n" ); - count +=sprintf (buf+count,"\n" ); + err =snprintf (buf+count, __count-count,"\n" ); +if (err ==0 ) +break ; +else + count += err; }read_unlock_bh (&osf_lock); ------------------------------------------------------------------------patch-o-matic-ng/osf/linux-2.4/net/ipv4/netfilter/ipt_osf.c +12
-5@@ -182,7 +185,6 @@ optsize = tcp->doff*4 -sizeof (struct tcphdr ); } -/* Actually we can create hash/table of all genres and search * only in appropriate part, but here is initial variant, * so will use slow path. @@ -601,9 +603,10 @@ {struct list_head *ent;struct osf_finger *f =NULL ; -int i; +int i, __count, err; *eof =1 ; + __count = count; count =0 ;read_lock_bh (&osf_lock);@@ -613,10 +616,13 @@log ("%s [%s]" , f->genre, f->details); - count +=sprintf (buf+count,"%s - %s[%s] : %s" , + err =snprintf (buf+count, __count-count,"%s - %s[%s] : %s" , f->genre, f->version, f->subtype, f->details); - +if (err ==0 ) +break ; +else + count += err;if (f->opt_num) {loga (" OPT: " );@@ -630,7 +636,11 @@ } }loga ("\n" ); - count +=sprintf (buf+count,"\n" ); + err =snprintf (buf+count, __count-count,"\n" ); +if (err ==0 ) +break ; +else + count += err; }read_unlock_bh (&osf_lock);
Quoted 8 lines · Patrick McHardy
Quoted 3 lines · Evgeniy Polyakov
It simply checks if return value from snprintf is 0 and breaks, otherwise it proceeds.
Still broken. snprintf returns a value > n if it truncated to n bytes. See my last mail again. BTW, did the overflow actually cause problems ? proc has an extra k of space just for overflows ..
If it truncates than we have [avoided] overflow and definetely will not write anything after it(except zero-lengh snprintf) since
__count-count == 0 there.
Do you mean following:
list_for_each()
{
snprintf();
if (count > __count)
break;
}Quoted 125 lines
Regards
PatrickQuoted 121 lines
ipt_osf.diff.1 - patch for 2.6
ipt_osf.diff.1.24 - patch for 2.4Signed-off-by Evgeniy Polyakov <[email protected]>Quoted 114 lines
Regards
Patrick------------------------------------------------------------------------
patch-o-matic-ng/osf/linux-2.4/net/ipv4/netfilter/ipt_osf.c +12
-6@@ -182,7 +185,6 @@ optsize = tcp->doff*4 -sizeof (struct tcphdr ); } -/* Actually we can create hash/table of all genres and search * only in appropriate part, but here is initial variant, * so will use slow path. @@ -601,9 +603,10 @@ {struct list_head *ent;struct osf_finger *f =NULL ; -int i; +int i, __count, err; *eof =1 ; + __count = count; count =0 ;read_lock_bh (&osf_lock);@@ -613,10 +616,13 @@log ("%s [%s]" , f->genre, f->details); - count +=sprintf (buf+count,"%s - %s[%s] : %s" , + err =snprintf (buf+count, __count-count,"%s - %s[%s] : %s" , f->genre, f->version, f->subtype, f->details); - +if (err ==0 ) +break ; +else + count += err;if (f->opt_num) {loga (" OPT: " );@@ -630,7 +636,11 @@ } }loga ("\n" ); - count +=sprintf (buf+count,"\n" ); + err =snprintf (buf+count, __count-count,"\n" ); +if (err ==0 ) +break ; +else + count += err; }read_unlock_bh (&osf_lock); ------------------------------------------------------------------------patch-o-matic-ng/osf/linux-2.4/net/ipv4/netfilter/ipt_osf.c +12
-5@@ -182,7 +185,6 @@ optsize = tcp->doff*4 -sizeof (struct tcphdr ); } -/* Actually we can create hash/table of all genres and search * only in appropriate part, but here is initial variant, * so will use slow path. @@ -601,9 +603,10 @@ {struct list_head *ent;struct osf_finger *f =NULL ; -int i; +int i, __count, err; *eof =1 ; + __count = count; count =0 ;read_lock_bh (&osf_lock);@@ -613,10 +616,13 @@log ("%s [%s]" , f->genre, f->details); - count +=sprintf (buf+count,"%s - %s[%s] : %s" , + err =snprintf (buf+count, __count-count,"%s - %s[%s] : %s" , f->genre, f->version, f->subtype, f->details); - +if (err ==0 ) +break ; +else + count += err;if (f->opt_num) {loga (" OPT: " );@@ -630,7 +636,11 @@ } }loga ("\n" ); - count +=sprintf (buf+count,"\n" ); + err =snprintf (buf+count, __count-count,"\n" ); +if (err ==0 ) +break ; +else + count += err; }read_unlock_bh (&osf_lock);
Signature · 3 lines
Evgeniy Polyakov ( s0mbre ) Crash is better than data corruption. -- Art Grabowski
Quoted 3 lines · Evgeniy Polyakov
If it truncates than we have [avoided] overflow and definetely will not write anything after it(except zero-lengh snprintf) since __count-count == 0 there.
Not quite.. __count-count <= 0 there..
char buf[4];
n = snprintf(buf, sizeof(buf), "xxxxxxxx");
returns 8, indicating that buf needs to be at least 9 octets large for this call to succeed.
Regards
Henrik
Quoted 6 lines · Evgeniy Polyakov
Quoted 5 lines · Patrick McHardy
Quoted 3 lines · Evgeniy Polyakov
It simply checks if return value from snprintf is 0 and breaks, otherwise it proceeds.
Quoted 7 lines
Quoted 3 lines
Still broken. snprintf returns a value > n if it truncated to n bytes. See my last mail again. BTW, did the overflow actually cause problems ? proc has an extra k of space just for overflows ..
If it truncates than we have [avoided] overflow and definetely will not write anything after it(except zero-lengh snprintf) since
__count-count == 0 there.
Actually <= 0 which is not good but avoids overflows.
I can trigger overflow without patch(actually it was hard lockup without any messages).
Quoted 7 lines
Do you mean following:
list_for_each() { snprintf(); if (count > __count) break; }
Attached with check
__count >= count + err;
Quoted 4 lines
Quoted 4 lines
Regards
Patrick
Signature · 3 lines
Evgeniy Polyakov ( s0mbre ) Crash is better than data corruption. -- Art Grabowski
patch-o-matic-ng/osf/linux-2.6/net/ipv4/netfilter/ipt_osf.c +12 -5
@@ -182,7 +185,6 @@ optsize = tcp->doff*4 -sizeof (struct tcphdr ); } -/* Actually we can create hash/table of all genres and search * only in appropriate part, but here is initial variant, * so will use slow path.
@@ -601,9 +603,10 @@ {struct list_head *ent;struct osf_finger *f =NULL ; -int i; +int i, __count, err; *eof =1 ; + __count = count; count =0 ;read_lock_bh (&osf_lock);
@@ -613,10 +616,13 @@log ("%s [%s]" , f->genre, f->details); - count +=sprintf (buf+count,"%s - %s[%s] : %s" , + err =snprintf (buf+count, __count-count,"%s - %s[%s] : %s" , f->genre, f->version, f->subtype, f->details); - +if (err ==0 || __count >= count + err) +break ; +else + count += err;if (f->opt_num) {loga (" OPT: " );
@@ -630,7 +636,11 @@ } }loga ("\n" ); - count +=sprintf (buf+count,"\n" ); + err =snprintf (buf+count, __count-count,"\n" ); +if (err ==0 || __count >= count + err) +break ; +else + count += err; }read_unlock_bh (&osf_lock);
patch-o-matic-ng/osf/linux-2.4/net/ipv4/netfilter/ipt_osf.c +12 -5
@@ -182,7 +185,6 @@ optsize = tcp->doff*4 -sizeof (struct tcphdr ); } -/* Actually we can create hash/table of all genres and search * only in appropriate part, but here is initial variant, * so will use slow path.
@@ -601,9 +603,10 @@ {struct list_head *ent;struct osf_finger *f =NULL ; -int i; +int i, __count, err; *eof =1 ; + __count = count; count =0 ;read_lock_bh (&osf_lock);
@@ -613,10 +616,13 @@log ("%s [%s]" , f->genre, f->details); - count +=sprintf (buf+count,"%s - %s[%s] : %s" , + err =snprintf (buf+count, __count-count,"%s - %s[%s] : %s" , f->genre, f->version, f->subtype, f->details); - +if (err ==0 || __count >= count + err) +break ; +else + count += err;if (f->opt_num) {loga (" OPT: " );
@@ -630,7 +636,11 @@ } }loga ("\n" ); - count +=sprintf (buf+count,"\n" ); + err =snprintf (buf+count, __count-count,"\n" ); +if (err ==0 || __count >= count + err) +break ; +else + count += err; }read_unlock_bh (&osf_lock);
Quoted 27 lines · Evgeniy Polyakov
Quoted 6 lines · Evgeniy Polyakov
Quoted 5 lines · Patrick McHardy
Quoted 3 lines · Evgeniy Polyakov
It simply checks if return value from snprintf is 0 and breaks, otherwise it proceeds.
Quoted 7 lines
Quoted 3 lines
Still broken. snprintf returns a value > n if it truncated to n bytes. See my last mail again. BTW, did the overflow actually cause problems ? proc has an extra k of space just for overflows ..
If it truncates than we have [avoided] overflow and definetely will not write anything after it(except zero-lengh snprintf) since
__count-count == 0 there.Actually <= 0 which is not good but avoids overflows.
I can trigger overflow without patch(actually it was hard lockup without any messages).Quoted 7 lines
Do you mean following:
list_for_each() { snprintf(); if (count > __count) break; }
Quoted 2 lines
Attached with check
__count >= count + err;
I'm not smoking bad crack, but it needs to be __count <= count + err; Attached.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Regards
Patrick
Signature · 3 lines
Evgeniy Polyakov ( s0mbre ) Crash is better than data corruption. -- Art Grabowski
patch-o-matic-ng/osf/linux-2.6/net/ipv4/netfilter/ipt_osf.c +4 -1
@@ -411,8 +413,11 @@ } } -read_unlock (&osf_lock); +if (fcount) + fmatch =FMATCH_OK ; +read_unlock (&osf_lock); +return (fmatch ==FMATCH_OK )?1 :0 ; }
patch-o-matic-ng/osf/linux-2.6/net/ipv4/netfilter/ipt_osf.c +12 -5
@@ -182,7 +185,6 @@ optsize = tcp->doff*4 -sizeof (struct tcphdr ); } -/* Actually we can create hash/table of all genres and search * only in appropriate part, but here is initial variant, * so will use slow path.
@@ -601,9 +603,10 @@ {struct list_head *ent;struct osf_finger *f =NULL ; -int i; +int i, __count, err; *eof =1 ; + __count = count; count =0 ;read_lock_bh (&osf_lock);
@@ -613,10 +616,13 @@log ("%s [%s]" , f->genre, f->details); - count +=sprintf (buf+count,"%s - %s[%s] : %s" , + err =snprintf (buf+count, __count-count,"%s - %s[%s] : %s" , f->genre, f->version, f->subtype, f->details); - +if (err ==0 || __count <= count + err) +break ; +else + count += err;if (f->opt_num) {loga (" OPT: " );
@@ -630,7 +636,11 @@ } }loga ("\n" ); - count +=sprintf (buf+count,"\n" ); + err =snprintf (buf+count, __count-count,"\n" ); +if (err ==0 || __count <= count + err) +break ; +else + count += err; }read_unlock_bh (&osf_lock);
Quoted 4 lines · Evgeniy Polyakov
I'm not smoking bad crack, but it needs to be
__count<= count + err; Attached.
This one looks fine, applied.