Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5822363imu; Tue, 13 Nov 2018 12:17:15 -0800 (PST) X-Google-Smtp-Source: AJdET5dtgUFKE5o/4Q3seuckaeUmEwbpg3H051GdOkX35WVQDowwZjWumemTXBcR4UYa6m074IeD X-Received: by 2002:a63:68c4:: with SMTP id d187mr5953005pgc.11.1542140235661; Tue, 13 Nov 2018 12:17:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542140235; cv=none; d=google.com; s=arc-20160816; b=xkKeT/1PLd/9FmyYKiL4aDyPkRxGOIQ7aTT0GhH+XjoS4jfFqU6vKA5bTTULPDXDwk 4lQZge1kghHMfZM67/WlsjqigZ6RN+fnmABvgo9vSffcfVZKyPJf4fm2p1LS/ZPaSCBq joP8skLkSqrFmJPt/hEx13mpxYESYjEHuh4Sup7hFPM52kSCjwBJpjG3bmg3kGUPhewm ZT88f1tS606ULlv5RKUxOAjv9x+yipgLHHiB4jd/Ok3fZzizsCUv00jxvrCWPsB8Ed4I cuuFDSN7BVABceRmfcy2fDAhe5bBnq1M2IsujEAdZD5KbZpZXPQ3uA6wva/3v5n0vPLF gTrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature; bh=xDwJKA7a9/1zySW4GCEoPliPcO++fw7YIoSz99wHR8o=; b=q+q2MubYSccAv121AqR2tdNc24WELJdt51/afvuRbpQX3/tHCLN4XraFVTNrgae9Vf EvaAGxMT5UTOhqngiNcUHoBp1to9stpmA93QiEufNpMB54MbdBu9y2Z1m2yBO3q2qPfm icHH9twBTcSmxCOD6a7Upi4V0cEDlCeAoXuss3TMpJ9YNY4Dvp2Tb432zMtRqaY+9I3U QeDvx8twitQxeouinViE4kGqKt8PUlCyW1fobTci4Og7Fe5OviSbs+HgM4Js8p40KpC8 rrUSTYQImfuFkWO+l2mQXXdh8ueXoqYAB8tCMy3Xcq9188nyQAjbjdfJDDcnxId9oLHu 1Q1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=HMXqcKMk; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e1si485710pln.55.2018.11.13.12.17.00; Tue, 13 Nov 2018 12:17:15 -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=@chromium.org header.s=google header.b=HMXqcKMk; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726610AbeKNGQR (ORCPT + 99 others); Wed, 14 Nov 2018 01:16:17 -0500 Received: from mail-yb1-f194.google.com ([209.85.219.194]:34620 "EHLO mail-yb1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725750AbeKNGQR (ORCPT ); Wed, 14 Nov 2018 01:16:17 -0500 Received: by mail-yb1-f194.google.com with SMTP id n140-v6so5964756yba.1 for ; Tue, 13 Nov 2018 12:16:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=xDwJKA7a9/1zySW4GCEoPliPcO++fw7YIoSz99wHR8o=; b=HMXqcKMkoQsOh/jYTRBtTxAj8tQ2j3wn7DxtnqZcEMVWJMxclyNpXFvvFHQktl27o7 pQtb4hMZs9RXsDZ/9jMhZeeFBYG+rHbc8yqMIWAhrud/DTQpWf46Duv0B5dcOuCTFLY8 ETuRdDRdJYMrNOFlwKHrppeppIeAJ4y9+QF98= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=xDwJKA7a9/1zySW4GCEoPliPcO++fw7YIoSz99wHR8o=; b=FQGKVyVjGshLFWlS22cw/f6Rku08yEAckLtgD8VvENzIeEYSHGK1uEqKwbb7EcTFxG UvEveH6aDuY/XkdUFKhi9gAzLvmHr+7grYx06nGZxcwhnYFBXS3yi2Bp1RGvNZS7zoxd fOud4p0rXeG/ra/WcMYQ3dI5A4WHDjhTLiiYIVye9o1uGSBJECu/h+hUuYPwEwkKkMs3 9r+8OHFMHpwI35Mv2rig02sRG2xWcUSvzu8aykCeLYknQ1TYtGeWUdl3d+CTk9EIioGE Guy5epA7IdjNwCrSJwQERUBHt4y0zsE9P4czhorKJ98hH1gJ3H32g9WPzjwGP4vNm5As zjtQ== X-Gm-Message-State: AGRZ1gLe5rfUqXZrICOEud/bN2fVwsfzZneqRgUd0AvxpOii9TRTi1Fe 1FxZVFlGC9l7XwmkkSdAELGvX45zZ0Y= X-Received: by 2002:a25:8041:: with SMTP id a1-v6mr6446287ybn.411.1542140191499; Tue, 13 Nov 2018 12:16:31 -0800 (PST) Received: from mail-yb1-f172.google.com (mail-yb1-f172.google.com. [209.85.219.172]) by smtp.gmail.com with ESMTPSA id 200-v6sm6244113ywq.97.2018.11.13.12.16.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Nov 2018 12:16:30 -0800 (PST) Received: by mail-yb1-f172.google.com with SMTP id g192-v6so5954330ybf.3 for ; Tue, 13 Nov 2018 12:16:30 -0800 (PST) X-Received: by 2002:a25:3588:: with SMTP id c130-v6mr6525056yba.410.1542140189975; Tue, 13 Nov 2018 12:16:29 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a25:b906:0:0:0:0:0 with HTTP; Tue, 13 Nov 2018 12:16:29 -0800 (PST) In-Reply-To: <20181112160931.GA28463@redhat.com> References: <20181112160931.GA28463@redhat.com> From: Kees Cook Date: Tue, 13 Nov 2018 14:16:29 -0600 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 1/2] exec: load_script: don't blindly truncate shebang string To: Oleg Nesterov Cc: Andrew Morton , Ben Woodard , "Eric W. Biederman" , Michal Hocko , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 12, 2018 at 10:09 AM, Oleg Nesterov wrote: > 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. > > Signed-off-by: Oleg Nesterov Acked-by: Kees Cook -Kees > --- > 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 7cde3f4..d0078cb 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.5.0 > > -- Kees Cook