Skip to content

Commit 8cf9381

Browse files
oleg-nesterovgabrielesvelto
authored andcommitted
exec: load_script: don't blindly truncate shebang string
[ Upstream commit 8099b04 ] 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/[email protected] Signed-off-by: Oleg Nesterov <[email protected]> Acked-by: Kees Cook <[email protected]> Acked-by: Michal Hocko <[email protected]> Cc: Ben Woodard <[email protected]> Cc: "Eric W. Biederman" <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent a9ea411 commit 8cf9381

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

fs/binfmt_script.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ static int load_script(struct linux_binprm *bprm)
3333
fput(bprm->file);
3434
bprm->file = NULL;
3535

36-
bprm->buf[BINPRM_BUF_SIZE - 1] = '\0';
37-
if ((cp = strchr(bprm->buf, '\n')) == NULL)
38-
cp = bprm->buf+BINPRM_BUF_SIZE-1;
36+
for (cp = bprm->buf+2;; cp++) {
37+
if (cp >= bprm->buf + BINPRM_BUF_SIZE)
38+
return -ENOEXEC;
39+
if (!*cp || (*cp == '\n'))
40+
break;
41+
}
3942
*cp = '\0';
43+
4044
while (cp > bprm->buf) {
4145
cp--;
4246
if ((*cp == ' ') || (*cp == '\t'))

0 commit comments

Comments
 (0)