2006-03-15 14:44:41

by Jiri Benc

[permalink] [raw]
Subject: [PATCH] modpost: fix buffer overflow

I got SIGABRT in modpost when building a module really deeply nested in
a filesystem (path > 100 chars):

> 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]>

--- linux-2.6.16-rc6.orig/scripts/mod/modpost.c
+++ linux-2.6.16-rc6/scripts/mod/modpost.c
@@ -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);
}


--
Jiri Benc
SUSE Labs


2006-03-15 14:55:49

by Bernd Petrovitsch

[permalink] [raw]
Subject: Re: [PATCH] modpost: fix buffer overflow

On Wed, 2006-03-15 at 15:44 +0100, Jiri Benc wrote:
> I got SIGABRT in modpost when building a module really deeply nested in
> a filesystem (path > 100 chars):
>
> > 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]>
>
> --- linux-2.6.16-rc6.orig/scripts/mod/modpost.c
> +++ linux-2.6.16-rc6/scripts/mod/modpost.c
> @@ -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
--
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services

2006-03-15 14:57:38

by Bernd Petrovitsch

[permalink] [raw]
Subject: Re: [PATCH] modpost: fix buffer overflow

On Wed, 2006-03-15 at 15:44 +0100, Jiri Benc wrote:
> I got SIGABRT in modpost when building a module really deeply nested in
> a filesystem (path > 100 chars):
>
> > 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]>
>
> --- linux-2.6.16-rc6.orig/scripts/mod/modpost.c
> +++ linux-2.6.16-rc6/scripts/mod/modpost.c
> @@ -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
--
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services

2006-03-15 15:09:03

by Jiri Benc

[permalink] [raw]
Subject: Re: [PATCH] modpost: fix buffer overflow

On Wed, 15 Mar 2006 15:57:28 +0100, Bernd Petrovitsch wrote:
> 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):

> 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]>

--- linux-2.6.16-rc6.orig/scripts/mod/modpost.c
+++ linux-2.6.16-rc6/scripts/mod/modpost.c
@@ -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);


--
Jiri Benc
SUSE Labs

2006-03-15 15:35:46

by Andreas Schwab

[permalink] [raw]
Subject: Re: [PATCH] modpost: fix buffer overflow

Jiri Benc <[email protected]> writes:

> + while (buf->size - buf->pos < len + 1)
> + buf->size += 128;

What's wrong with

buf->size = buf->pos + len + 1;

Andreas.

--
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."

2006-03-15 22:52:16

by Sam Ravnborg

[permalink] [raw]
Subject: Re: [PATCH] modpost: fix buffer overflow

On Wed, Mar 15, 2006 at 04:08:58PM +0100, Jiri Benc wrote:
> I got SIGABRT in modpost when compiling a module really deeply nested in
> a filesystem (path > 100 chars):
>
> > 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]>
>
> --- linux-2.6.16-rc6.orig/scripts/mod/modpost.c
> +++ linux-2.6.16-rc6/scripts/mod/modpost.c
> @@ -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

2006-03-16 13:21:17

by Jiri Benc

[permalink] [raw]
Subject: Re: [PATCH] modpost: fix buffer overflow

On Wed, 15 Mar 2006 23:51:59 +0100, Sam Ravnborg wrote:
> 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,

--
Jiri Benc
SUSE Labs

2006-03-16 15:46:18

by Sam Ravnborg

[permalink] [raw]
Subject: Re: [PATCH] modpost: fix buffer overflow

On Thu, Mar 16, 2006 at 02:21:14PM +0100, Jiri Benc wrote:
> On Wed, 15 Mar 2006 23:51:59 +0100, Sam Ravnborg wrote:
> > 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

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 30f3ac8..0b92ddf 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -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);

2006-03-16 19:00:17

by Sam Ravnborg

[permalink] [raw]
Subject: [PATCH] kbuild: fix buffer overflow in modpost

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:
> Building modules, stage 2.
> MODPOST
> *** glibc detected *** scripts/mod/modpost: realloc(): invalid next size:
+0x0809f588 ***
> [...]

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]>

Signed-off-by: Sam Ravnborg <[email protected]>
---

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index f70ff13..b8b2a56 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -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);