All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4] exec: load_script: Do not exec truncated interpreter path
@ 2019-02-17  9:09 Kees Cook
  2019-02-17 17:30 ` Linus Torvalds
  0 siblings, 1 reply; 3+ messages in thread
From: Kees Cook @ 2019-02-17  9:09 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Linus Torvalds, Oleg Nesterov, Samuel Dionne-Riel,
	Richard Weinberger, Graham Christensen, Michal Hocko, LKML

Commit 8099b047ecc4 ("exec: load_script: don't blindly truncate
shebang string") was trying to protect against a confused exec of a
truncated interpreter path. However, it was overeager and also refused
to truncate arguments as well, which broke userspace, and it was
reverted. This attempts the protection again, but allows arguments to
remain truncated. In an effort to improve readability, helper functions
and comments have been added.

Co-developed-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 fs/binfmt_script.c | 57 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 48 insertions(+), 9 deletions(-)

diff --git a/fs/binfmt_script.c b/fs/binfmt_script.c
index 7cde3f46ad26..32cdf6ee1f75 100644
--- a/fs/binfmt_script.c
+++ b/fs/binfmt_script.c
@@ -14,13 +14,30 @@
 #include <linux/err.h>
 #include <linux/fs.h>
 
+static inline bool spacetab(char c) { return c == ' ' || c == '\t'; }
+static inline char *next_non_spacetab(char *first, const char *last)
+{
+	for (; first <= last; first++)
+		if (!spacetab(*first))
+			return first;
+	return NULL;
+}
+static inline char *next_spacetab(char *first, const char *last)
+{
+	for (; first <= last; first++)
+		if (spacetab(*first))
+			return first;
+	return NULL;
+}
+
 static int load_script(struct linux_binprm *bprm)
 {
 	const char *i_arg, *i_name;
-	char *cp;
+	char *cp, *buf_end;
 	struct file *file;
 	int retval;
 
+	/* Not ours to exec if we don't start with "#!". */
 	if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!'))
 		return -ENOEXEC;
 
@@ -33,18 +50,40 @@ static int load_script(struct linux_binprm *bprm)
 	if (bprm->interp_flags & BINPRM_FLAGS_PATH_INACCESSIBLE)
 		return -ENOENT;
 
-	/*
-	 * This section does the #! interpretation.
-	 * Sorta complicated, but hopefully it will work.  -TYT
-	 */
-
+	/* Release since we are not mapping a binary into memory. */
 	allow_write_access(bprm->file);
 	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;
+	/*
+	 * This section handles parsing the #! line into separate
+	 * interpreter path and argument strings. We must be careful
+	 * because bprm->buf is not yet guaranteed to be NUL-terminated
+	 * (though the buffer will have trailing NUL padding when the
+	 * file size was smaller than the buffer size).
+	 *
+	 * We do not want to exec a truncated interpreter path, so either
+	 * we find a newline (which indicates nothing is truncated), or
+	 * we find a space/tab/NUL after the interpreter path (which
+	 * itself may be preceded by spaces/tabs). Truncating the
+	 * arguments is fine: the interpreter can re-read the script to
+	 * parse them on its own.
+	 */
+	buf_end = bprm->buf + sizeof(bprm->buf) - 1;
+	cp = strnchr(bprm->buf, sizeof(bprm->buf), '\n');
+	if (!cp) {
+		cp = next_non_spacetab(bprm->buf + 2, buf_end);
+		if (!cp)
+			return -ENOEXEC; /* Entire buf is spaces/tabs */
+		/*
+		 * Without later spaces/tabs and a non-NUL final byte we
+		 * must assume the interpreter path is truncated.
+		 */
+		if (!next_spacetab(cp, buf_end) && *buf_end)
+			return -ENOEXEC;
+		cp = buf_end;
+	}
+	/* NUL-terminate the buffer and any trailing spaces/tabs. */
 	*cp = '\0';
 	while (cp > bprm->buf) {
 		cp--;
-- 
2.17.1


-- 
Kees Cook

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] exec: load_script: Do not exec truncated interpreter path
  2019-02-17  9:09 [PATCH v4] exec: load_script: Do not exec truncated interpreter path Kees Cook
@ 2019-02-17 17:30 ` Linus Torvalds
  2019-02-17 17:41   ` Kees Cook
  0 siblings, 1 reply; 3+ messages in thread
From: Linus Torvalds @ 2019-02-17 17:30 UTC (permalink / raw)
  To: Kees Cook
  Cc: Andrew Morton, Oleg Nesterov, Samuel Dionne-Riel,
	Richard Weinberger, Graham Christensen, Michal Hocko, LKML

On Sun, Feb 17, 2019 at 1:09 AM Kees Cook <keescook@chromium.org> wrote:
>
> +static inline char *next_spacetab(char *first, const char *last)
> +{
> +       for (; first <= last; first++)
> +               if (spacetab(*first))
> +                       return first;
> +       return NULL;
> +}

I think this should be "next_terminator()" and also stop at NUL.

You do check for the NUL case here:

> +               if (!next_spacetab(cp, buf_end) && *buf_end)
> +                       return -ENOEXEC;

but it means that if there's no space ot tab and it's a short file,
you pointlessly walk to the end. No?

                   Linus

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] exec: load_script: Do not exec truncated interpreter path
  2019-02-17 17:30 ` Linus Torvalds
@ 2019-02-17 17:41   ` Kees Cook
  0 siblings, 0 replies; 3+ messages in thread
From: Kees Cook @ 2019-02-17 17:41 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: Andrew Morton, Oleg Nesterov, Samuel Dionne-Riel,
	Richard Weinberger, Graham Christensen, Michal Hocko, LKML

On Sun, Feb 17, 2019 at 9:30 AM Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> On Sun, Feb 17, 2019 at 1:09 AM Kees Cook <keescook@chromium.org> wrote:
> >
> > +static inline char *next_spacetab(char *first, const char *last)
> > +{
> > +       for (; first <= last; first++)
> > +               if (spacetab(*first))
> > +                       return first;
> > +       return NULL;
> > +}
>
> I think this should be "next_terminator()" and also stop at NUL.
>
> You do check for the NUL case here:
>
> > +               if (!next_spacetab(cp, buf_end) && *buf_end)
> > +                       return -ENOEXEC;
>
> but it means that if there's no space ot tab and it's a short file,
> you pointlessly walk to the end. No?

Yeah, that works. I'll send v5.

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-02-17 17:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-17  9:09 [PATCH v4] exec: load_script: Do not exec truncated interpreter path Kees Cook
2019-02-17 17:30 ` Linus Torvalds
2019-02-17 17:41   ` Kees Cook

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.