Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2658323imj; Mon, 11 Feb 2019 06:35:42 -0800 (PST) X-Google-Smtp-Source: AHgI3IacgtQ/f0yK7PT2yI006+V27ekFXzNY/GN5RcX43ve3G6rUSP6o/D9Z0HnCTciS6TEzBU9S X-Received: by 2002:a17:902:8203:: with SMTP id x3mr38121269pln.285.1549895742188; Mon, 11 Feb 2019 06:35:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549895742; cv=none; d=google.com; s=arc-20160816; b=0QflhVR+mChH6lB+5jN3NidGZ50Fwzi/DOpNqu3q263cu8sDs4IdwwRgLaqtSGtL6J r+eUmJlj0kQZJIvMB/VdCufF/BLzPg10WiF/CNJv54cMwc+SdrrbwM/LvqWpcj8riimY AOYHrT8ReAJDe6k4JRQDWMVVPvK/W3cozOy1Vqi8SRkIuwNgTeBqxMfIZmhBfAr6xwpg UBC9hUuW0HEZWGJ1myTz1GigXTLoWMNunuO2v2sdu5bLpoSI/iG5fGEp4B57mvH5RNsQ 4eg54gfUnyFuE6SYYi+FZ+xN5rLdPp5vEAAKijbqNnZlVlfb8+cexuU/++001nMLOWRq luCQ== 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=sJeVVbMvPUaRlPvs/LzxF5i+b2LvdsjE+ITvZbD5ej4=; b=BFL15LmZLGBgKDzbjH9Z3KrKEhMPh3Oc/hxffptW9HJGbQp2Ih9uGP14OomHoqN1HJ HXdcLDp4SJ/4o+AMfMwKmMt5c+PohCqozNkxruOJMX/01jlF4omJ5zU6LKx/aF7VjSHC uUUBP1zxqCpz0crqkQxuhl7e9bCAY1tGs3TAQWMftBAkXhByxsLVg5IOrKwORk5I6/GF kVBaZnTntrShSg2ts/Y4JRWbQC8Y6m1oEgEqOAbOd3NxGRcLtRpzjqV4KjZsRV4SimeR wdIumyQgWcAis5wR6AveQbiyAGC0UfU/halFlqMyTGX4oEkVPg/NLaU0vuMYb/pGUnoR OBwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=DDkJcUM1; 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 c13si3564496pgk.253.2019.02.11.06.35.26; Mon, 11 Feb 2019 06:35:42 -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=DDkJcUM1; 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 S1730919AbfBKOer (ORCPT + 99 others); Mon, 11 Feb 2019 09:34:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:43060 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730875AbfBKOel (ORCPT ); Mon, 11 Feb 2019 09:34:41 -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 239192080F; Mon, 11 Feb 2019 14:34:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549895680; bh=w3TLt+K56Z4iGUpbc/OcUIsAyo++BHd1Faes/DxPcEw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DDkJcUM1c5rH0meBEj4VcOJ/lefxwMjHJlKyUCGiO/if2MosPfIu0PrT+/CUvE+3t JYe4Xjdpfy0LzxCFOCKvN0soRpRxdnBCSiIoZPOJ2CtgfMSf7GiSpl2lUbHUEZaJK7 DSrnp+ppX5DcpD9uvKeM4YyQn2YXkVWkH8Hz5SJc= 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.20 287/352] exec: load_script: dont blindly truncate shebang string Date: Mon, 11 Feb 2019 15:18:34 +0100 Message-Id: <20190211141905.178818500@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141846.543045703@linuxfoundation.org> References: <20190211141846.543045703@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.20-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