Re: [PATCH v3] exec: load_script: Do not exec truncated interpreter path

Oleg Nesterov
2019-02-15T16:18:58Z

On 02/14, Kees Cook wrote:
>
> --- a/fs/binfmt_script.c
> +++ b/fs/binfmt_script.c
> @@ -42,9 +42,18 @@ static int load_script(struct linux_binprm *bprm)
> fput(bprm->file);
> bprm->file = NULL;
>
> - bprm->buf[BINPRM_BUF_SIZE - 1] = '\0';
> - if ((cp = strchr(bprm->buf, '\n')) == NULL)
> - cp = bprm->buf+BINPRM_BUF_SIZE-1;
> + if ((cp = strnchr(bprm->buf, BINPRM_BUF_SIZE, '\n')) == NULL) {
> + bool truncated = true;
> +
> + for (cp = bprm->buf+2; cp < bprm->buf+BINPRM_BUF_SIZE-1 &&
> + ((*cp == ' ') || (*cp == '\t')); cp++);
> + for (; cp < bprm->buf+BINPRM_BUF_SIZE-1; cp++) {
> + if ((*cp == ' ') || (*cp == '\t'))
> + truncated = false;
> + }
> + if (truncated)
> + return -ENOEXEC; /* Interpreter truncated */

Not sure. Consider a script file which has a single line

#!/path/to/interpreter

WITHOUT '\n' at the end. If I read load_script() correctly it should work,
so I think the 2nd for() loop should also reset "truncated" if *cp == '\0'.

Hmm. And cp < bprm->buf+BINPRM_BUF_SIZE-1 is off-by-one again...

Well. Probably nobody does this... but after regression caused by my patch
I am not 100% sure ;)

Oleg.