Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4472607ybx; Mon, 4 Nov 2019 14:01:16 -0800 (PST) X-Google-Smtp-Source: APXvYqxV3UkPxOr5x2NIzY2CtU++AM8wQAhgMpMT9fd86LHTK0BldqMEIngXIGqfOkWu3/mLcmV4 X-Received: by 2002:aa7:d54e:: with SMTP id u14mr939701edr.49.1572904876023; Mon, 04 Nov 2019 14:01:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572904876; cv=none; d=google.com; s=arc-20160816; b=lTUisJY87qjlgwKrrKfR9GSenZAixc+NQt0gSKFDwwp6At9HHfcYq7eq+fhaklv0yQ EKp0RVZ2zWSW6I1tVw0unG2C10UQUUet9kMK5oV7k64yIX99MDRwbhh4EaaAj1vc4ADl bnVLFuWJq5PczBUx6hw1iuaVR5RVmhjkRo6tBLNstDN4902RMNPiNvr6F/I3QHZVh8/W Qz4rkxI6O6Jiib894Y8PXH/q7pjyyiCRYrrcnVviBTYmI2pJZVRrLL1DdJzNmyebvNkg xjTv3QFAXu49rwFgFGRdimGZXBqlpYYB5mug/KyY+hxrxry43WmhZu+ud8jqbYCcr5ak IhzA== 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=h9/4o7Nb+ziPUoczNfoQzql91zo9fr2NQ0paKC4YFghgoi8yYkxsdlaYxgBplEjthf GqwRF1JCbsN5EbekeL+fGul2b4+Z6m1Etycvtc6khP2a5p/jMbznftIBwUF7PUFmD+2Q 1FjB4r6fry3R3zPmfzGcQgfdlHDhFVaZS/jgbppoE1v9mYJZWXsOVvykxGGvfObGm+bG gtiZf1quaq3r2hvqbwLvU14h59aTpDj6taWBLFRGrGSZfnWiZvoOuJK4UD2NcksdRrUE rmEV2ndylzPJxjoGcTeq4LNNR84wOh5ts6SdYdgAIkv/RvSuoK4Fjubou7brlR4CepDq SxKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Fm1+L4zo; 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 p4si12360030ejo.21.2019.11.04.14.00.52; Mon, 04 Nov 2019 14:01:16 -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=Fm1+L4zo; 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 S1731035AbfKDV7J (ORCPT + 99 others); Mon, 4 Nov 2019 16:59:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:56072 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730893AbfKDV7A (ORCPT ); Mon, 4 Nov 2019 16:59:00 -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 71FFC214E0; Mon, 4 Nov 2019 21:58:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572904738; bh=ne9QAjeb1S8nb93Ake9Lq8Wna3eB+iMjKjixdd5/Lj0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Fm1+L4zo5W+UED7Wg/StclxZscoJwFk2F9GPt4smLml7zbnx4gSfQNmJ9kIIurpja O7CGKgaEctWA2y3s84KC7nquboPe5Vgv0zUPZ7pw8fcyc8C8b1aECrqx1ImkQyN08W S8Olo2hqzmGCubZj/hLCBXXlWrTqvSzQBMBhqDaw= 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.19 053/149] exec: load_script: Do not exec truncated interpreter path Date: Mon, 4 Nov 2019 22:44:06 +0100 Message-Id: <20191104212139.993310316@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212126.090054740@linuxfoundation.org> References: <20191104212126.090054740@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