linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RESEND][PATCH] gen_init_cpio: avoid stack overflow when expanding
@ 2012-10-24 20:57 Kees Cook
  2012-10-24 21:02 ` Andrew Morton
  0 siblings, 1 reply; 6+ messages in thread
From: Kees Cook @ 2012-10-24 20:57 UTC (permalink / raw)
  To: linux-kernel
  Cc: Kees Cook, Andrew Morton, Michal Marek, Brad Spengler, PaX Team

Fix possible overflow of the buffer used for expanding environment
variables when building file list.

$ cat usr/crash.list
file foo ${BIG}${BIG}${BIG}${BIG}${BIG}${BIG} 0755 0 0
$ BIG=$(perl -e 'print "A" x 4096;') ./usr/gen_init_cpio usr/crash.list
*** buffer overflow detected ***: ./usr/gen_init_cpio terminated

This also replaces the space-indenting with tabs.

Patch based on existing fix extracted from grsecurity.

Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Michal Marek <mmarek@suse.cz>
Cc: Brad Spengler <spender@grsecurity.net>
Cc: PaX Team <pageexec@freemail.hu>
Cc: stable@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 usr/gen_init_cpio.c |   43 +++++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 20 deletions(-)

diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c
index af0f22f..aca6edc 100644
--- a/usr/gen_init_cpio.c
+++ b/usr/gen_init_cpio.c
@@ -303,7 +303,7 @@ static int cpio_mkfile(const char *name, const char *location,
 	int retval;
 	int rc = -1;
 	int namesize;
-	int i;
+	unsigned int i;
 
 	mode |= S_IFREG;
 
@@ -381,25 +381,28 @@ error:
 
 static char *cpio_replace_env(char *new_location)
 {
-       char expanded[PATH_MAX + 1];
-       char env_var[PATH_MAX + 1];
-       char *start;
-       char *end;
-
-       for (start = NULL; (start = strstr(new_location, "${")); ) {
-               end = strchr(start, '}');
-               if (start < end) {
-                       *env_var = *expanded = '\0';
-                       strncat(env_var, start + 2, end - start - 2);
-                       strncat(expanded, new_location, start - new_location);
-                       strncat(expanded, getenv(env_var), PATH_MAX);
-                       strncat(expanded, end + 1, PATH_MAX);
-                       strncpy(new_location, expanded, PATH_MAX);
-               } else
-                       break;
-       }
-
-       return new_location;
+	char expanded[PATH_MAX + 1];
+	char env_var[PATH_MAX + 1];
+	char *start;
+	char *end;
+
+	for (start = NULL; (start = strstr(new_location, "${")); ) {
+		end = strchr(start, '}');
+		if (start < end) {
+			*env_var = *expanded = '\0';
+			strncat(env_var, start + 2, end - start - 2);
+			strncat(expanded, new_location, start - new_location);
+			strncat(expanded, getenv(env_var),
+				PATH_MAX - strlen(expanded));
+			strncat(expanded, end + 1,
+				PATH_MAX - strlen(expanded));
+			strncpy(new_location, expanded, PATH_MAX);
+			new_location[PATH_MAX] = 0;
+		} else
+			break;
+	}
+
+	return new_location;
 }
 
 
-- 
1.7.9.5


-- 
Kees Cook
Chrome OS Security

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [RESEND][PATCH] gen_init_cpio: avoid stack overflow when expanding
  2012-10-24 20:57 [RESEND][PATCH] gen_init_cpio: avoid stack overflow when expanding Kees Cook
@ 2012-10-24 21:02 ` Andrew Morton
  2012-10-24 21:33   ` Kees Cook
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2012-10-24 21:02 UTC (permalink / raw)
  To: Kees Cook; +Cc: linux-kernel, Michal Marek, Brad Spengler, PaX Team

On Wed, 24 Oct 2012 13:57:56 -0700
Kees Cook <keescook@chromium.org> wrote:

> Fix possible overflow of the buffer used for expanding environment
> variables when building file list.
> 
> $ cat usr/crash.list
> file foo ${BIG}${BIG}${BIG}${BIG}${BIG}${BIG} 0755 0 0
> $ BIG=$(perl -e 'print "A" x 4096;') ./usr/gen_init_cpio usr/crash.list
> *** buffer overflow detected ***: ./usr/gen_init_cpio terminated
> 
> This also replaces the space-indenting with tabs.
> 
> Patch based on existing fix extracted from grsecurity.
> 
> ...
>
> Cc: stable@vger.kernel.org

Why did you feel we need to backport this to -stable?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RESEND][PATCH] gen_init_cpio: avoid stack overflow when expanding
  2012-10-24 21:02 ` Andrew Morton
@ 2012-10-24 21:33   ` Kees Cook
  2012-10-24 21:44     ` Andrew Morton
  0 siblings, 1 reply; 6+ messages in thread
From: Kees Cook @ 2012-10-24 21:33 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, Michal Marek, Brad Spengler, PaX Team

On Wed, Oct 24, 2012 at 2:02 PM, Andrew Morton
<akpm@linux-foundation.org> wrote:
> On Wed, 24 Oct 2012 13:57:56 -0700
> Kees Cook <keescook@chromium.org> wrote:
>
>> Fix possible overflow of the buffer used for expanding environment
>> variables when building file list.
>>
>> $ cat usr/crash.list
>> file foo ${BIG}${BIG}${BIG}${BIG}${BIG}${BIG} 0755 0 0
>> $ BIG=$(perl -e 'print "A" x 4096;') ./usr/gen_init_cpio usr/crash.list
>> *** buffer overflow detected ***: ./usr/gen_init_cpio terminated
>>
>> This also replaces the space-indenting with tabs.
>>
>> Patch based on existing fix extracted from grsecurity.
>>
>> ...
>>
>> Cc: stable@vger.kernel.org
>
> Why did you feel we need to backport this to -stable?

It's an extremely hard to hit security issue, but it's a security fix
regardless. I won't cry if it doesn't go to stable, but it seems a
trivial fix, so I included it for stable.

-Kees

-- 
Kees Cook
Chrome OS Security

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RESEND][PATCH] gen_init_cpio: avoid stack overflow when expanding
  2012-10-24 21:33   ` Kees Cook
@ 2012-10-24 21:44     ` Andrew Morton
  2012-10-24 21:53       ` Kees Cook
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2012-10-24 21:44 UTC (permalink / raw)
  To: Kees Cook; +Cc: linux-kernel, Michal Marek, Brad Spengler, PaX Team

On Wed, 24 Oct 2012 14:33:02 -0700
Kees Cook <keescook@chromium.org> wrote:

> On Wed, Oct 24, 2012 at 2:02 PM, Andrew Morton
> <akpm@linux-foundation.org> wrote:
> > On Wed, 24 Oct 2012 13:57:56 -0700
> > Kees Cook <keescook@chromium.org> wrote:
> >
> >> Fix possible overflow of the buffer used for expanding environment
> >> variables when building file list.
> >>
> >> $ cat usr/crash.list
> >> file foo ${BIG}${BIG}${BIG}${BIG}${BIG}${BIG} 0755 0 0
> >> $ BIG=$(perl -e 'print "A" x 4096;') ./usr/gen_init_cpio usr/crash.list
> >> *** buffer overflow detected ***: ./usr/gen_init_cpio terminated
> >>
> >> This also replaces the space-indenting with tabs.
> >>
> >> Patch based on existing fix extracted from grsecurity.
> >>
> >> ...
> >>
> >> Cc: stable@vger.kernel.org
> >
> > Why did you feel we need to backport this to -stable?
> 
> It's an extremely hard to hit security issue, but it's a security fix
> regardless. I won't cry if it doesn't go to stable, but it seems a
> trivial fix, so I included it for stable.

Well, I do think that a description of the user impact of the bug
should be included in the changelog so that poor old Greg can work out
why we sent it at him.

If you can suggest some suitable text I can copy-n-slurp that into the
changelog.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RESEND][PATCH] gen_init_cpio: avoid stack overflow when expanding
  2012-10-24 21:44     ` Andrew Morton
@ 2012-10-24 21:53       ` Kees Cook
  2012-10-24 21:56         ` Andrew Morton
  0 siblings, 1 reply; 6+ messages in thread
From: Kees Cook @ 2012-10-24 21:53 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, Michal Marek, Brad Spengler, PaX Team

On Wed, Oct 24, 2012 at 2:44 PM, Andrew Morton
<akpm@linux-foundation.org> wrote:
> On Wed, 24 Oct 2012 14:33:02 -0700
> Kees Cook <keescook@chromium.org> wrote:
>
>> On Wed, Oct 24, 2012 at 2:02 PM, Andrew Morton
>> <akpm@linux-foundation.org> wrote:
>> > On Wed, 24 Oct 2012 13:57:56 -0700
>> > Kees Cook <keescook@chromium.org> wrote:
>> >
>> >> Fix possible overflow of the buffer used for expanding environment
>> >> variables when building file list.
>> >>
>> >> $ cat usr/crash.list
>> >> file foo ${BIG}${BIG}${BIG}${BIG}${BIG}${BIG} 0755 0 0
>> >> $ BIG=$(perl -e 'print "A" x 4096;') ./usr/gen_init_cpio usr/crash.list
>> >> *** buffer overflow detected ***: ./usr/gen_init_cpio terminated
>> >>
>> >> This also replaces the space-indenting with tabs.
>> >>
>> >> Patch based on existing fix extracted from grsecurity.
>> >>
>> >> ...
>> >>
>> >> Cc: stable@vger.kernel.org
>> >
>> > Why did you feel we need to backport this to -stable?
>>
>> It's an extremely hard to hit security issue, but it's a security fix
>> regardless. I won't cry if it doesn't go to stable, but it seems a
>> trivial fix, so I included it for stable.
>
> Well, I do think that a description of the user impact of the bug
> should be included in the changelog so that poor old Greg can work out
> why we sent it at him.
>
> If you can suggest some suitable text I can copy-n-slurp that into the
> changelog.

How about replacing the first paragraph with:

Fix possible overflow of the buffer used for expanding environment
variables when building file list. In the extremely unlikely case of
an attacker having control over the environment variables visible to
gen_init_cpio, control over the contents of the file gen_init_cpio
parses, and gen_init_cpio was built without compiler hardening, the
attacker can gain arbitrary execution control via a stack buffer
overflow.


-Kees

-- 
Kees Cook
Chrome OS Security

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [RESEND][PATCH] gen_init_cpio: avoid stack overflow when expanding
  2012-10-24 21:53       ` Kees Cook
@ 2012-10-24 21:56         ` Andrew Morton
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Morton @ 2012-10-24 21:56 UTC (permalink / raw)
  To: Kees Cook; +Cc: linux-kernel, Michal Marek, Brad Spengler, PaX Team

On Wed, 24 Oct 2012 14:53:33 -0700
Kees Cook <keescook@chromium.org> wrote:

> > Well, I do think that a description of the user impact of the bug
> > should be included in the changelog so that poor old Greg can work out
> > why we sent it at him.
> >
> > If you can suggest some suitable text I can copy-n-slurp that into the
> > changelog.
> 
> How about replacing the first paragraph with:
> 
> Fix possible overflow of the buffer used for expanding environment
> variables when building file list. In the extremely unlikely case of
> an attacker having control over the environment variables visible to
> gen_init_cpio, control over the contents of the file gen_init_cpio
> parses, and gen_init_cpio was built without compiler hardening, the
> attacker can gain arbitrary execution control via a stack buffer
> overflow.

ooh, spiffy - even I understood that!

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-10-24 21:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-24 20:57 [RESEND][PATCH] gen_init_cpio: avoid stack overflow when expanding Kees Cook
2012-10-24 21:02 ` Andrew Morton
2012-10-24 21:33   ` Kees Cook
2012-10-24 21:44     ` Andrew Morton
2012-10-24 21:53       ` Kees Cook
2012-10-24 21:56         ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).