Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2717807imj; Mon, 11 Feb 2019 07:26:53 -0800 (PST) X-Google-Smtp-Source: AHgI3IZbaZ2TvUTM3kyJqLzqzCY0ixSX1uz2KSa21wIxCOobGrlY/UK6JAIoFC5n8g4f8eE3d1yU X-Received: by 2002:a17:902:6948:: with SMTP id k8mr37614972plt.2.1549898813562; Mon, 11 Feb 2019 07:26:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549898813; cv=none; d=google.com; s=arc-20160816; b=pS9Of8QvFF4JIqH1jgTAT9mnRQ2oiD2vd60FidAvVeEWsC2RgSgOwmGcQn7O5OPrzd rbsCp4+4QqVi38EWZfPfLJAV5UvnedoPj6EIpvcWWNA5dwFYhCz94Kk/t1TK+zGgLayQ vUM5HuTfUbBZzaBHrIicyG/0OuT+YvVxglIqfo/7W8BxD5+D+AUJiCdGxHyAkNTgjQWj oDS7bTE0P3yLuGx2aGcC5PqNRvYQvhomz0t92SXBQTmp8610hJBdKlsFksz+wEUeBRG1 qMym5sJ0vwCEM6FNlWfO6VAJ9T1oH6CyOjCwjyXr/xAOzhvohBUU9lOGV160CoMu4Thz i3xw== 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=1kBRD9OnMWG5++71XW+Je/eNTcWXQWH2xvE595Bk8xc=; b=atwV3IhJn9Zs6Hz0tVmJzSd9rb3G0licIYJJH4nR7sC2MsPpz1xrIXIf/YEidKVU+K DjrEew2L8sjKlb+Jai8jtLYnFlZfxKs59D8e4CpTldNpiQgG/t/gsFz6sa6dpl5o3+SG 0Jjd6MDRBtMG0bhvo+YPpz8Werl+HxzpTIlCH6TyqD3NoDRVShf7Fj80Ala54rd7txta 3d9f8xRyEcqfIu3aI9hvC45/DFlmV3B8Y26A9BeAhELYGzf13/Dh5CCsm+1Ri8BxVE7K P94Yagmhrvz74cIOf4UUOClV6hYpJnbNU23Uhe9GUYg+biO7hcMlDu8bTMJq4EtmPsPD jPIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=MQmOSEPX; 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 g26si10061843pfi.184.2019.02.11.07.26.38; Mon, 11 Feb 2019 07:26:53 -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=MQmOSEPX; 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 S2390319AbfBKPCB (ORCPT + 99 others); Mon, 11 Feb 2019 10:02:01 -0500 Received: from mail.kernel.org ([198.145.29.99]:50154 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387943AbfBKPCA (ORCPT ); Mon, 11 Feb 2019 10:02:00 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (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 4C165222AE; Mon, 11 Feb 2019 15:01:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549897319; bh=c4EZ/8TL20oHt7zCb9VNih/AjHoQpyucw0ZBw6jV0TQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MQmOSEPXMhuRRQQCx2Utd0s+FdPqgYt/YOIfYMCFeTVS0wSwTWxnStF/BGTxQWAYD dlK5N0HABzBYzZFTiShvRySkbyy1zsuh4JFfBxeZndGvNsJ0hT4LWi1fmIgiHm/nAl XktFotGhuoZE7pXM/DcKh9gH2C1YcborML3P/boE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Oleg Nesterov , Kees Cook , Michal Hocko , Ben Woodard , "Eric W. Biederman" , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 4.14 163/205] exec: load_script: dont blindly truncate shebang string Date: Mon, 11 Feb 2019 15:19:21 +0100 Message-Id: <20190211141839.018790871@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141827.214852402@linuxfoundation.org> References: <20190211141827.214852402@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit 8099b047ecc431518b9bb6bdbba3549bbecdc343 ] 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. Link: http://lkml.kernel.org/r/20181112160931.GA28463@redhat.com Signed-off-by: Oleg Nesterov Acked-by: Kees Cook Acked-by: Michal Hocko Cc: Ben Woodard Cc: "Eric W. Biederman" Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- 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 7cde3f46ad26..d0078cbb718b 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.19.1