Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3637019imu; Mon, 28 Jan 2019 08:13:11 -0800 (PST) X-Google-Smtp-Source: ALg8bN4WlQleIy1khBjxs+62RK7dwKe8gg9PgKld2T1d9lG+wqAYT+7Gu6q+4/6ZUob5cuA7/Uxx X-Received: by 2002:a62:e044:: with SMTP id f65mr22264258pfh.208.1548691991523; Mon, 28 Jan 2019 08:13:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548691991; cv=none; d=google.com; s=arc-20160816; b=qef3exrDADpPcIJzN8eFikaZ4H8Y9qgJA+c5JpH6dFgVbrAZMhtJ/ouf0aGlJY9y11 jIm6Cn/h5LwfbLGNtrCq2zZeYG/UthPz9tDZY19wx71Pevi7fVTazFmJ2oab9aFYkkDX EyIfN3azUCuHBqDiijhy97G8tVICgThQ7+S1jBuw5ngMuJjdC2YoW2x3AneZZx+lLiJJ tRWldN011BTAjdVoJG17R137eEqE17kPOtzd1CJuuMDjFMtAn4N79OLYcrcifX4+IP9D HM43bbcwR2XWeZrHww1woetn8QzTDmT6swRDTV4A9+YjeY4ZYeDma9owyLr0QJ7CBAFH cfVw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=f5Yhjz4ZHSBkS8De0B0Lbn2rHH+Ji+LRVkLdaqiXO2k=; b=D72TZ6+EsyMqu6AtNzfq4TK1NDgr1+ce9QBz8kYtH1ZNjtFxK4CcN2lskbz02uq2ST C+yNQxNZ6Ouk8x/pfl+EjzsqaIS7t7hbgSaVYHxHsm8kG+zajhdscaidTU3x2t+rZBuM pbZweAR5YLF5JRJPEENq4kpH2CkW5udwzxt11fkb55Xxv9fIeLCNpet81kgmP4P7e9pB +6UkBJ63bdyvk9rnFOgA+2KdNrcPvKLEIgYXlTONkQs+iafC/6h+jve04a34zVetcj7q kp7AWEYgh1lAoQeL1mPe/1gi8hwxy00y78xVeVinXvkwNWcO5xlwpcS3GWzCiINMcROV +5hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JUk4+1Fm; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v25si8171025pgk.341.2019.01.28.08.12.55; Mon, 28 Jan 2019 08:13:11 -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=JUk4+1Fm; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732627AbfA1QL4 (ORCPT + 99 others); Mon, 28 Jan 2019 11:11:56 -0500 Received: from mail.kernel.org ([198.145.29.99]:39342 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733043AbfA1QLx (ORCPT ); Mon, 28 Jan 2019 11:11:53 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 04C6421852; Mon, 28 Jan 2019 16:11:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548691912; bh=INfw3x0PEw8wjZIATFrl5hYnvIK3kjLWHAWkKeZkh2M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JUk4+1FmOGzSLLHcCrwXxqQ+5afPTwNlJOKgLrWZexj9e586KnG26HyYdoE1R02GI P3FaNOE4kTHVaIg9GB1SeQrmD/6/Y7hMNlW8LuN2ibKzRg4c36dRYNjVysiJC5RlcJ URXbfdnWh8N6DxddWm3X/igAt7Jy5jHwPA7YDL4Q= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Oleg Nesterov , Ben Woodard , "Eric W. Biederman" , Andrew Morton , Linus Torvalds , Sasha Levin , linux-fsdevel@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 256/258] exec: load_script: don't blindly truncate shebang string Date: Mon, 28 Jan 2019 10:59:22 -0500 Message-Id: <20190128155924.51521-256-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190128155924.51521-1-sashal@kernel.org> References: <20190128155924.51521-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Oleg Nesterov [ 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