Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4504776ybx; Mon, 4 Nov 2019 14:30:17 -0800 (PST) X-Google-Smtp-Source: APXvYqypZwmGBTE9RlcU3zqYCKSncGexnggnqGwtYjmRnC04mcgc10s2m5h/I8nLkoQj4YV77Vc8 X-Received: by 2002:aa7:c6c9:: with SMTP id b9mr22149225eds.1.1572906617657; Mon, 04 Nov 2019 14:30:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572906617; cv=none; d=google.com; s=arc-20160816; b=00yIy+BFjLJb11jCu+ajc5u16koUSt+qPue57Kr6nMbMgoQLwRUKwKjRzxX15alShf yoDHW/0kMADgyKQ8fYqBucNxZNyAHpwfM8T0jL2kqIR/6TbJJ3eGcZXdbU6gZ99XtsOq 5TJVc3txWZtExirbE5whjn5CqEP+9rYMWx9RV07veJ5eaMKhacDXA0Vk1yumtpdTrX+b b0sW0lqx4banYunF0fD/8fMlbmRiMmtuzr2QW6k+jD8gPKjyDv8Ggevgl2AhWYtCrcpL EpXevoYl8J9ed1YinhOjh/5rFom4KGPu84NfdOviExr42mwuRIF9nqGq7OnDRPvmeRJI 6VfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=zQdu+pFdEjG+z+4gnjGaZxx954Irtm1mBlwAcwTBeUs=; b=fnB17W4OiukkD/vFJxMd6mw/jsLGlflZHTXkWYmRkzxJXO/X8L0Zaj+JpcwCDFXEA8 YyttE9BEof45M213OBA2mSDhbdhqmPzwH5HuasyIjB1pj1m33k40JwnFkvbf3xbqgOYQ pWnoBFJ3UFibKQKe5KaeLogQkjeH9GZuX3WuvoVOCXPKMmNMkZWx+QO4QX+aTXZGIIUG ThalN2P1RHHXVSdqHFXhGRc7M9/JDftPNUuc019hwgyq7sPeulQYBjr8QMB+IuZOquAI jogIQqS5JxgumdWZW3DQt+Gr9e8vTwm5HApMkNIG9rTY9YdDmpIzdNIz2wgu3lmrINcu RNkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zPRqKzo7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c8si8014320ede.323.2019.11.04.14.29.54; Mon, 04 Nov 2019 14:30:17 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=zPRqKzo7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388497AbfKDW2e (ORCPT + 99 others); Mon, 4 Nov 2019 17:28:34 -0500 Received: from mail.kernel.org ([198.145.29.99]:39832 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730182AbfKDVs4 (ORCPT ); Mon, 4 Nov 2019 16:48:56 -0500 Received: from localhost (6.204-14-84.ripe.coltfrance.com [84.14.204.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5758221929; Mon, 4 Nov 2019 21:48:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572904134; bh=3fIb/XpE5v/FmqlNuybYCPwnbwJAl3gVKkv+KO2OZGw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zPRqKzo7yk8v2QgW61CclCOyM28xzgLJ7UXl2ndddFEopIWx5dt2ykdOg2kuUSDCE DpcZuVQ77zfTxpBjHXXu+kQzj5T67wzo1+bZxgX4LsVjO/IchhyR0LtgrJW/L8JlsO bAT/atPL9mwL/XOTfRn73inUtN/Mx+QuGbggEmos= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kees Cook , Andrew Morton , Oleg Nesterov , Samuel Dionne-Riel , Richard Weinberger , Graham Christensen , Michal Hocko , Linus Torvalds , Sasha Levin Subject: [PATCH 4.4 09/46] exec: load_script: Do not exec truncated interpreter path Date: Mon, 4 Nov 2019 22:44:40 +0100 Message-Id: <20191104211839.848088730@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104211830.912265604@linuxfoundation.org> References: <20191104211830.912265604@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook [ Upstream commit b5372fe5dc84235dbe04998efdede3c4daa866a9 ] 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 Signed-off-by: Kees Cook Cc: Andrew Morton Cc: Oleg Nesterov Cc: Samuel Dionne-Riel Cc: Richard Weinberger Cc: Graham Christensen Cc: Michal Hocko Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- 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 afdf4e3cafc2a..37c2093a24d3c 100644 --- a/fs/binfmt_script.c +++ b/fs/binfmt_script.c @@ -14,14 +14,31 @@ #include #include +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_terminator(char *first, const char *last) +{ + for (; first <= last; first++) + if (spacetab(*first) || !*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; char interp[BINPRM_BUF_SIZE]; int retval; + /* Not ours to exec if we don't start with "#!". */ if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!')) return -ENOEXEC; @@ -34,18 +51,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 */ + /* + * If there is no later space/tab/NUL we must assume the + * interpreter path is truncated. + */ + if (!next_terminator(cp, buf_end)) + return -ENOEXEC; + cp = buf_end; + } + /* NUL-terminate the buffer and any trailing spaces/tabs. */ *cp = '\0'; while (cp > bprm->buf) { cp--; -- 2.20.1