On Thu, Feb 14, 2019 at 8:08 AM Oleg Nesterov <[email protected]> wrote:
>
> 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...

That's why I added comments too. It's kind of a weird bit of parsing,
and has to protect itself from lack of initial NUL-termination. :P

> 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.

Ah yeah, this bails out one byte too early. As you suggestion, I
should move that test to after the !*cp || *cp == '\n' test, like so:

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

> >                       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?

Moving the block is right, dropping the -1 would be lead to the
post-loop *cp = '\0' writing past the end of the buffer.

With this change, my tests (after gaining an extra byte of available
interp path name) still pass. I'll send a v2...

Thanks!

-- 
Kees Cook

Reply via email to