Commit c2315c18 authored by Oleg Nesterov's avatar Oleg Nesterov Committed by Linus Torvalds

exec: load_script: kill the onstack interp[BINPRM_BUF_SIZE] array

Patch series "exec: binfmt_misc: fix use-after-free, kill
iname[BINPRM_BUF_SIZE]".

It looks like this code was always wrong, then commit 948b701a
("binfmt_misc: add persistent opened binary handler for containers")
added more problems.

This patch (of 6):

load_script() can simply use i_name instead, it points into bprm->buf[]
and nobody can change this memory until we call prepare_binprm().

The only complication is that we need to also change the signature of
bprm_change_interp() but this change looks good too.

While at it, do whitespace/style cleanups.

NOTE: the real motivation for this change is that people want to
increase BINPRM_BUF_SIZE, we need to change load_misc_binary() too but
this looks more complicated because afaics it is very buggy.

Link: http://lkml.kernel.org/r/20170918163446.GA26793@redhat.comSigned-off-by: default avatarOleg Nesterov <oleg@redhat.com>
Acked-by: default avatarKees Cook <keescook@chromium.org>
Cc: Travis Gummels <tgummels@redhat.com>
Cc: Ben Woodard <woodard@redhat.com>
Cc: Jim Foraker <foraker1@llnl.gov>
Cc: <tdhooge@llnl.gov>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: James Bottomley <James.Bottomley@HansenPartnership.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 384632e6
...@@ -19,7 +19,6 @@ static int load_script(struct linux_binprm *bprm) ...@@ -19,7 +19,6 @@ static int load_script(struct linux_binprm *bprm)
const char *i_arg, *i_name; const char *i_arg, *i_name;
char *cp; char *cp;
struct file *file; struct file *file;
char interp[BINPRM_BUF_SIZE];
int retval; int retval;
if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!')) if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!'))
...@@ -55,7 +54,7 @@ static int load_script(struct linux_binprm *bprm) ...@@ -55,7 +54,7 @@ static int load_script(struct linux_binprm *bprm)
break; break;
} }
for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++); for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
if (*cp == '\0') if (*cp == '\0')
return -ENOEXEC; /* No interpreter name found */ return -ENOEXEC; /* No interpreter name found */
i_name = cp; i_name = cp;
i_arg = NULL; i_arg = NULL;
...@@ -65,7 +64,6 @@ static int load_script(struct linux_binprm *bprm) ...@@ -65,7 +64,6 @@ static int load_script(struct linux_binprm *bprm)
*cp++ = '\0'; *cp++ = '\0';
if (*cp) if (*cp)
i_arg = cp; i_arg = cp;
strcpy (interp, i_name);
/* /*
* OK, we've parsed out the interpreter name and * OK, we've parsed out the interpreter name and
* (optional) argument. * (optional) argument.
...@@ -80,24 +78,27 @@ static int load_script(struct linux_binprm *bprm) ...@@ -80,24 +78,27 @@ static int load_script(struct linux_binprm *bprm)
if (retval) if (retval)
return retval; return retval;
retval = copy_strings_kernel(1, &bprm->interp, bprm); retval = copy_strings_kernel(1, &bprm->interp, bprm);
if (retval < 0) return retval; if (retval < 0)
return retval;
bprm->argc++; bprm->argc++;
if (i_arg) { if (i_arg) {
retval = copy_strings_kernel(1, &i_arg, bprm); retval = copy_strings_kernel(1, &i_arg, bprm);
if (retval < 0) return retval; if (retval < 0)
return retval;
bprm->argc++; bprm->argc++;
} }
retval = copy_strings_kernel(1, &i_name, bprm); retval = copy_strings_kernel(1, &i_name, bprm);
if (retval) return retval; if (retval)
return retval;
bprm->argc++; bprm->argc++;
retval = bprm_change_interp(interp, bprm); retval = bprm_change_interp(i_name, bprm);
if (retval < 0) if (retval < 0)
return retval; return retval;
/* /*
* OK, now restart the process with the interpreter's dentry. * OK, now restart the process with the interpreter's dentry.
*/ */
file = open_exec(interp); file = open_exec(i_name);
if (IS_ERR(file)) if (IS_ERR(file))
return PTR_ERR(file); return PTR_ERR(file);
......
...@@ -1410,7 +1410,7 @@ static void free_bprm(struct linux_binprm *bprm) ...@@ -1410,7 +1410,7 @@ static void free_bprm(struct linux_binprm *bprm)
kfree(bprm); kfree(bprm);
} }
int bprm_change_interp(char *interp, struct linux_binprm *bprm) int bprm_change_interp(const char *interp, struct linux_binprm *bprm)
{ {
/* If a binfmt changed the interp, free it first. */ /* If a binfmt changed the interp, free it first. */
if (bprm->interp != bprm->filename) if (bprm->interp != bprm->filename)
......
...@@ -131,7 +131,7 @@ extern int setup_arg_pages(struct linux_binprm * bprm, ...@@ -131,7 +131,7 @@ extern int setup_arg_pages(struct linux_binprm * bprm,
int executable_stack); int executable_stack);
extern int transfer_args_to_stack(struct linux_binprm *bprm, extern int transfer_args_to_stack(struct linux_binprm *bprm,
unsigned long *sp_location); unsigned long *sp_location);
extern int bprm_change_interp(char *interp, struct linux_binprm *bprm); extern int bprm_change_interp(const char *interp, struct linux_binprm *bprm);
extern int copy_strings_kernel(int argc, const char *const *argv, extern int copy_strings_kernel(int argc, const char *const *argv,
struct linux_binprm *bprm); struct linux_binprm *bprm);
extern int prepare_bprm_creds(struct linux_binprm *bprm); extern int prepare_bprm_creds(struct linux_binprm *bprm);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment