Commit 8099b047 authored by Oleg Nesterov's avatar Oleg Nesterov Committed by Linus Torvalds

exec: load_script: don't blindly truncate shebang string

load_script() simply truncates bprm->buf and this is very wrong if the
length of shebang string exceeds BINPRM_BUF_SIZE-2.  This can silently
truncate i_arg or (worse) we can execute the wrong binary if buf[2:126]
happens to be the valid executable path.

Change load_script() to return ENOEXEC if it can't find '\n' or zero in
bprm->buf.  Note that '\0' can come from either
prepare_binprm()->memset() or from kernel_read(), we do not care.

Link: http://lkml.kernel.org/r/20181112160931.GA28463@redhat.comSigned-off-by: default avatarOleg Nesterov <oleg@redhat.com>
Acked-by: default avatarKees Cook <keescook@chromium.org>
Acked-by: default avatarMichal Hocko <mhocko@suse.com>
Cc: Ben Woodard <woodard@redhat.com>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent fb5bf317
...@@ -42,10 +42,14 @@ static int load_script(struct linux_binprm *bprm) ...@@ -42,10 +42,14 @@ static int load_script(struct linux_binprm *bprm)
fput(bprm->file); fput(bprm->file);
bprm->file = NULL; bprm->file = NULL;
bprm->buf[BINPRM_BUF_SIZE - 1] = '\0'; for (cp = bprm->buf+2;; cp++) {
if ((cp = strchr(bprm->buf, '\n')) == NULL) if (cp >= bprm->buf + BINPRM_BUF_SIZE)
cp = bprm->buf+BINPRM_BUF_SIZE-1; return -ENOEXEC;
if (!*cp || (*cp == '\n'))
break;
}
*cp = '\0'; *cp = '\0';
while (cp > bprm->buf) { while (cp > bprm->buf) {
cp--; cp--;
if ((*cp == ' ') || (*cp == '\t')) if ((*cp == ' ') || (*cp == '\t'))
......
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