Re: [PATCH] exec: load_script: Allow interpreter argument truncation

From: Oleg Nesterov
Date: Thu Feb 14 2019 - 11:08:29 EST


On 02/13, Kees Cook wrote:
>
> While we want to make sure the kernel doesn't attempt to execute a
> truncated interpreter path, we must allow the interpreter arguments to
> be truncated. Perl, for example, will re-read the script itself to parse
> arguments correctly.

Heh. I still think that 8099b047ecc4 does the right thing.

But I can't argue with the fact that it caused the regression, so it should
be reverted.

> This documents the parsing steps, and will fail to exec if the string was
> truncated with neither an end-of-line nor any trailing whitespace.

You know, I have already spent 3 hours trying to write something simple and
clear, but failed. Still trying...

Nor I can really understand your fix ;) Will try to read it again, just one
question for now,

> for (cp = bprm->buf+2;; cp++) {
> - if (cp >= bprm->buf + BINPRM_BUF_SIZE)
> - return -ENOEXEC;
> - if (!*cp || (*cp == '\n'))
> + if (cp == bprm->buf + BINPRM_BUF_SIZE - 1) {
> + truncated = true;

Off-by-one, no? "bprm->buf + BINPRM_BUF_SIZE - 1" is the very last char, it can
be '\n' or '\0', this should set end_of_interp.

> break;
> + }
> + if (!*cp || (*cp == '\n')) {
> + end_of_interp = true;
> + break;
> + }

so unless I am totally confused you should move this block up before the
"bprm->buf + BINPRM_BUF_SIZE - 1" check or that check should use
"bprm->buf + BINPRM_BUF_SIZE".

No?

Oleg.