modpost: fix buffer overflow
PATCH 1/1 modpost: fix buffer overflow
I got SIGABRT in modpost when building a module really deeply nested in a filesystem (path > 100 chars):
Quoted 4 lines
Building modules, stage 2.
MODPOST
*** glibc detected ***scripts/mod/modpost:realloc(): invalid next size:0x0809f588*** [...]
This patch fixes that problem.
scripts/mod/modpost.c +1 -1
@@ -552,7 +552,7 @@ void __attribute__((format(printf, 2, 3)va_start (ap, fmt); len =vsnprintf (tmp,SZ , fmt, ap); -if (buf->size - buf->pos < len +1 ) { +while (buf->size - buf->pos < len +1 ) { buf->size +=128 ; buf->p =realloc (buf->p, buf->size); }
Signature · 2 lines
Jiri Benc SUSE Labs
Quoted 15 lines · Jiri Benc
I got SIGABRT in modpost when building a module really deeply nested in a filesystem (path > 100 chars):
Quoted 4 lines
Building modules, stage 2.
MODPOST
*** glibc detected ***scripts/mod/modpost:realloc(): invalid next size:0x0809f588*** [...]This patch fixes that problem.
Signed-off-by Jiri Benc <[email protected]>
scripts/mod/modpost.c void __attribute__((format(printf, 2, 3) · line 558
Quoted 8 lines
scripts/mod/modpost.c +1
-1@@ -552,7 +552,7 @@ void __attribute__((format(printf, 2, 3)va_start (ap, fmt); len =vsnprintf (tmp,SZ , fmt, ap); -if (buf->size - buf->pos < len +1 ) { +while (buf->size - buf->pos < len +1 ) { buf->size +=128 ; buf->p =realloc (buf->p, buf->size); }
Are you sure you won't pull the line with realloc() out of the loop and do it once afterwards?
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and ServicesQuoted 15 lines · Jiri Benc
I got SIGABRT in modpost when building a module really deeply nested in a filesystem (path > 100 chars):
Quoted 4 lines
Building modules, stage 2.
MODPOST
*** glibc detected ***scripts/mod/modpost:realloc(): invalid next size:0x0809f588*** [...]This patch fixes that problem.
Signed-off-by Jiri Benc <[email protected]>
scripts/mod/modpost.c void __attribute__((format(printf, 2, 3) · line 558
Quoted 8 lines
scripts/mod/modpost.c +1
-1@@ -552,7 +552,7 @@ void __attribute__((format(printf, 2, 3)va_start (ap, fmt); len =vsnprintf (tmp,SZ , fmt, ap); -if (buf->size - buf->pos < len +1 ) { +while (buf->size - buf->pos < len +1 ) { buf->size +=128 ; buf->p =realloc (buf->p, buf->size); }
Silly me. To make it more obvious whatz I really meant was:
---- snip ----
if (buf->size - buf->pos < len + 1) {
while (buf->size - buf->pos < len + 1)
buf->size += 128;
buf->p = realloc(buf->p, buf->size);
}---- snip ----
Bernd
Signature · 3 lines
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and ServicesQuoted 8 lines · Bernd Petrovitsch
Silly me. To make it more obvious whatz I really meant was:
---- snip ----if (buf->size - buf->pos < len + 1) { while (buf->size - buf->pos < len + 1) buf->size += 128; buf->p = realloc(buf->p, buf->size); }---- snip ----
Yes, this is probably better. New version of the patch follows.
---->8----
I got SIGABRT in modpost when compiling a module really deeply nested in a filesystem (path > 100 chars):
Quoted 4 lines
Building modules, stage 2.
MODPOST
*** glibc detected ***scripts/mod/modpost:realloc(): invalid next size:0x0809f588*** [...]
This patch fixes that problem.
scripts/mod/modpost.c +2 -1
@@ -553,7 +553,8 @@ void __attribute__((format(printf, 2, 3)va_start (ap, fmt); len =vsnprintf (tmp,SZ , fmt, ap);if (buf->size - buf->pos < len +1 ) { - buf->size +=128 ; +while (buf->size - buf->pos < len +1 ) + buf->size +=128 ; buf->p =realloc (buf->p, buf->size); }strncpy (buf->p + buf->pos, tmp, len +1 );
Signature · 2 lines
Jiri Benc SUSE Labs
scripts/mod/modpost.c void __attribute__((format(printf, 2, 3) · line 557
Quoted 2 lines · Jiri Benc
scripts/mod/modpost.c +2
-0+while (buf->size - buf->pos < len +1 ) + buf->size +=128 ;
What's wrong with
buf->size = buf->pos + len + 1;
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Quoted 15 lines · Jiri Benc
I got SIGABRT in modpost when compiling a module really deeply nested in a filesystem (path > 100 chars):
Quoted 4 lines
Building modules, stage 2.
MODPOST
*** glibc detected ***scripts/mod/modpost:realloc(): invalid next size:0x0809f588*** [...]This patch fixes that problem.
Signed-off-by Jiri Benc <[email protected]>
scripts/mod/modpost.c void __attribute__((format(printf, 2, 3) · line 560
Quoted 9 lines
scripts/mod/modpost.c +2
-1@@ -553,7 +553,8 @@ void __attribute__((format(printf, 2, 3)va_start (ap, fmt); len =vsnprintf (tmp,SZ , fmt, ap);if (buf->size - buf->pos < len +1 ) { - buf->size +=128 ; +while (buf->size - buf->pos < len +1 ) + buf->size +=128 ; buf->p =realloc (buf->p, buf->size); }strncpy (buf->p + buf->pos, tmp, len +1 );
Hi Jiri.
Can I ask you to make a new patch where you change buf_printf() to use buf_write. And then change buf_write to allocate in chunks also. This would be cleanest solution.
Sam
Quoted 3 lines · Sam Ravnborg
Can I ask you to make a new patch where you change
buf_printf()to usebuf_write. And then changebuf_writeto allocate in chunks also. This would be cleanest solution.
This probably will be the cleanest solution, but I doubt it would be acceptable for 2.6.16. And I think the fix should go into 2.6.16.
Thanks,
Signature · 2 lines
Jiri Benc SUSE Labs
Quoted 7 lines · Jiri Benc
Quoted 3 lines · Sam Ravnborg
Can I ask you to make a new patch where you change
buf_printf()to usebuf_write. And then changebuf_writeto allocate in chunks also. This would be cleanest solution.This probably will be the cleanest solution, but I doubt it would be acceptable for 2.6.16. And I think the fix should go into 2.6.16.
Like this...
Sam
scripts/mod/modpost.c +2 -7
@@ -923,19 +923,14 @@ void __attribute__((format(printf, 2, 3)va_start (ap, fmt); len =vsnprintf (tmp,SZ , fmt, ap); -if (buf->size - buf->pos < len +1 ) { - buf->size +=128 ; - buf->p =realloc (buf->p, buf->size); - } -strncpy (buf->p + buf->pos, tmp, len +1 ); - buf->pos += len; +buf_write (buf, tmp, len);va_end (ap); }void buf_write (struct buffer *buf,const char *s,int len) {if (buf->size - buf->pos < len) { - buf->size += len; + buf->size += len +SZ ; buf->p =realloc (buf->p, buf->size); }strncpy (buf->p + buf->pos, s, len);
PATCH 1/1 kbuild: fix buffer overflow in modpost
In reply to Jiri BencHi Linus - please apply to 2.6.16-rc
Jiri Benc <[email protected]> reported that modpost would stop with SIGABRT if used with long filepaths.
The error looked like:
Quoted 3 lines
Building modules, stage 2.
MODPOST
*** glibc detected ***scripts/mod/modpost:realloc(): invalid next size:
+0x0809f588 ***
Quoted 1 line
[...]
Following patch fixes this by allocating at least the required memory + SZ bytes each time. Before we sometimes ended up allocating too little memory resuting in the glibc detected bug above. Based on patch originally submitted by: Jiri Benc <[email protected]>
---
scripts/mod/modpost.c +2 -7
@@ -508,12 +508,7 @@ buf_printf(struct buffer *buf, const chava_start (ap, fmt); len =vsnprintf (tmp,SZ , fmt, ap); -if (buf->size - buf->pos < len +1 ) { - buf->size +=128 ; - buf->p =realloc (buf->p, buf->size); - } -strncpy (buf->p + buf->pos, tmp, len +1 ); - buf->pos += len; +buf_write (buf, tmp, len);va_end (ap); }
@@ -521,7 +516,7 @@ void buf_write(struct buffer *buf, const char *s, int len) { if (buf->size - buf->pos < len) { - buf->size += len; + buf->size += len + SZ; buf->p =realloc (buf->p, buf->size); }strncpy (buf->p + buf->pos, s, len);