Skip to content

Commit 20f1de6

Browse files
keestorvalds
authored andcommitted
gen_init_cpio: avoid stack overflow when expanding
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. $ 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. Signed-off-by: Kees Cook <keescook@chromium.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: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent fee0de7 commit 20f1de6

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

‎usr/gen_init_cpio.c‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ static int cpio_mkfile(const char *name, const char *location,
303303
int retval;
304304
int rc = -1;
305305
int namesize;
306-
int i;
306+
unsigned int i;
307307

308308
mode |= S_IFREG;
309309

@@ -381,25 +381,28 @@ static int cpio_mkfile(const char *name, const char *location,
381381

382382
static char *cpio_replace_env(char *new_location)
383383
{
384-
char expanded[PATH_MAX + 1];
385-
char env_var[PATH_MAX + 1];
386-
char *start;
387-
char *end;
388-
389-
for (start = NULL; (start = strstr(new_location, "${")); ) {
390-
end = strchr(start, '}');
391-
if (start < end) {
392-
*env_var = *expanded = '\0';
393-
strncat(env_var, start + 2, end - start - 2);
394-
strncat(expanded, new_location, start - new_location);
395-
strncat(expanded, getenv(env_var), PATH_MAX);
396-
strncat(expanded, end + 1, PATH_MAX);
397-
strncpy(new_location, expanded, PATH_MAX);
398-
} else
399-
break;
400-
}
401-
402-
return new_location;
384+
char expanded[PATH_MAX + 1];
385+
char env_var[PATH_MAX + 1];
386+
char *start;
387+
char *end;
388+
389+
for (start = NULL; (start = strstr(new_location, "${")); ) {
390+
end = strchr(start, '}');
391+
if (start < end) {
392+
*env_var = *expanded = '\0';
393+
strncat(env_var, start + 2, end - start - 2);
394+
strncat(expanded, new_location, start - new_location);
395+
strncat(expanded, getenv(env_var),
396+
PATH_MAX - strlen(expanded));
397+
strncat(expanded, end + 1,
398+
PATH_MAX - strlen(expanded));
399+
strncpy(new_location, expanded, PATH_MAX);
400+
new_location[PATH_MAX] = 0;
401+
} else
402+
break;
403+
}
404+
405+
return new_location;
403406
}
404407

405408

0 commit comments

Comments
 (0)