Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4466624ybx; Mon, 4 Nov 2019 13:54:54 -0800 (PST) X-Google-Smtp-Source: APXvYqx0MO0lqjrrmJ+E/iEfSeUaYQfrmTg3/hlYNyvZdb4hEK5sUf+058vPPZFGSJMwJO9EF1rp X-Received: by 2002:a17:906:1b0c:: with SMTP id o12mr26001139ejg.312.1572904494181; Mon, 04 Nov 2019 13:54:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572904494; cv=none; d=google.com; s=arc-20160816; b=CJFbG1ajx0CppWe6RzgvU9IJA4TvZRarcKlVQlzzHh2q8+KvORRArxnqR0HyyTLHqf ji7QcQUjOIGOuQQq48mmLgY/nE7Yn5/SK7WhaTKZqdcGubwFr4aPmyBsYDzWfJcTtizK XO6z7cN92s4C1SPpc0uxC2Hid9ikfSQUwwBnHk3D4fX70toZeaWn5vyRu9DZ6dD0zSgB NFdr1z3YK72hpM6u3vhlnfjUs89oNHlKrORFkXpG8w9/oxIJlaaxjd4IH2iWzIHkI1EN gkfV1cPFKenxchR5uBzR3JTI5E0WoZHpj4h3OBUciOw/OeOjXjQmuUrqlbbtRiaduqWX Zz/A== 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=Ym0ed/AZR5DtQ8v2VpvOB8MwcYR1+0AYzDETe1vBSQ0=; b=yyKE/rr0rKFFBJUnFhTHWBu444Wf1madPKBJSNDS9TlHh+xxlTEjgRrA8n2+FgX5V/ 6gjQDkCzuuL9pTg61A0Vft8yCj+AiwlD/UsuDykN8zpJqok7y3feWuU9RtXerRtoWBZH cOt77WY+6jp4di9GAq4Y7vlxnM6WN1MPlWXBICF6nSw16Q1VcLrofghNOZYQ/tyTpBk/ DxNI4cwVBx0bP4UwbEBr63XILntKvCg6idiqAxTbAA5ScoSFaNscscTFGtc5VkkFK8VR blMl55DdXjqjcsI/JlNxjDtn47sGYErghmsRauGfaL4dGgluB4Sa2b6bE2Z+c/WC7/Y7 sICQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=oB8NTEzb; 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 b10si7503093eda.160.2019.11.04.13.54.30; Mon, 04 Nov 2019 13:54:54 -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=oB8NTEzb; 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 S1729949AbfKDVwp (ORCPT + 99 others); Mon, 4 Nov 2019 16:52:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:46302 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730809AbfKDVwm (ORCPT ); Mon, 4 Nov 2019 16:52:42 -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 7416F21850; Mon, 4 Nov 2019 21:52:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572904361; bh=ne9QAjeb1S8nb93Ake9Lq8Wna3eB+iMjKjixdd5/Lj0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oB8NTEzbFvz+XkKmpnNFLXdP1BCO/pPoORNxx+cnqTfl2AtioHGkbDFyBbfpBzeX4 tcr6IrxojXM/QAXNmeYwkpjTChe8rt6BHkKSBzSnjuG5G6ZHD3WA175lpiZXJf5//j od+Ij36pFTeIkTV76cV/fiJN91qK1o7XoquLiyBU= 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.14 21/95] exec: load_script: Do not exec truncated interpreter path Date: Mon, 4 Nov 2019 22:44:19 +0100 Message-Id: <20191104212049.961020644@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212038.056365853@linuxfoundation.org> References: <20191104212038.056365853@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 7cde3f46ad263..e996174cbfc02 100644 --- a/fs/binfmt_script.c +++ b/fs/binfmt_script.c @@ -14,13 +14,30 @@ #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; 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 */ + /* + * 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