Skip to contentSkip to conversation

modpost: fix buffer overflow

PATCH In review Jiri Benc 4 participants 9 messages Mar 15, 2006 – Mar 16, 2006 Kbuild

lore
Share & tools
lore

Jiri Benc

author
#0 lore raw

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
#1 lore raw
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.

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 Services
#2 lore raw
In reply to Jiri Benc
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.

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 Services
author
#3 lore raw
Quoted 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
#4 lore raw

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."
#5 lore raw
In reply to Jiri Benc
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.

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

author
#6 lore raw
Quoted 3 lines · Sam Ravnborg

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.

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
#7 lore raw
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 use buf_write. And then change buf_write to 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
index 30f3ac8..0b92ddf 100644
@@ -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);
#8 lore raw

PATCH 1/1 kbuild: fix buffer overflow in modpost

In reply to Jiri Benc

Hi 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
index f70ff13..b8b2a56 100644
@@ -508,12 +508,7 @@ buf_printf(struct buffer *buf, const cha
 	
 	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);
 }
 
@@ -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);