[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAGXu5jLCdWZ1qdTvo_VK0VT8AbGQ44wsKPypRPRzNVed7f1xeg@mail.gmail.com>
Date: Tue, 13 Nov 2018 14:16:29 -0600
From: Kees Cook <keescook@...omium.org>
To: Oleg Nesterov <oleg@...hat.com>
Cc: Andrew Morton <akpm@...ux-foundation.org>,
Ben Woodard <woodard@...hat.com>,
"Eric W. Biederman" <ebiederm@...ssion.com>,
Michal Hocko <mhocko@...e.com>,
LKML <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH 1/2] exec: load_script: don't blindly truncate shebang string
On Mon, Nov 12, 2018 at 10:09 AM, Oleg Nesterov <oleg@...hat.com> wrote:
> 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.
>
> Signed-off-by: Oleg Nesterov <oleg@...hat.com>
Acked-by: Kees Cook <keescook@...omium.org>
-Kees
> ---
> fs/binfmt_script.c | 10 +++++++---
> 1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/fs/binfmt_script.c b/fs/binfmt_script.c
> index 7cde3f4..d0078cb 100644
> --- a/fs/binfmt_script.c
> +++ b/fs/binfmt_script.c
> @@ -42,10 +42,14 @@ 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;
> + for (cp = bprm->buf+2;; cp++) {
> + if (cp >= bprm->buf + BINPRM_BUF_SIZE)
> + return -ENOEXEC;
> + if (!*cp || (*cp == '\n'))
> + break;
> + }
> *cp = '\0';
> +
> while (cp > bprm->buf) {
> cp--;
> if ((*cp == ' ') || (*cp == '\t'))
> --
> 2.5.0
>
>
--
Kees Cook
Powered by blists - more mailing lists