Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1658118pxb; Wed, 2 Feb 2022 09:35:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJzJN8WEhW5mR4dhpQiNl6TeT3ZEYVGZLXdiAElCfIdwBEtfn6nIv7N/GRwJpbXRSnEffDPL X-Received: by 2002:a17:906:5d12:: with SMTP id g18mr25201654ejt.745.1643823332457; Wed, 02 Feb 2022 09:35:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643823332; cv=none; d=google.com; s=arc-20160816; b=bI0HuDOvxWsGxKRCwIsnqdQ8XV4YjlBy8gzneuV9QrsHm7o3ZYigZAgSxgfaLoqtjk b+q8X/AYBbLTNcriXp5m6OJki7zl8g9UUZKScmL6HIVPpxOEcvjRNsMfCSmLDvUrFfWA 01FtEoYEU2HKDUSIUcegj/qlvXlGgYRP4ayfIAiDsqtS/mcGQRGt6eKx8rGfc4mNolWk 3+bdK6ZIPpYtPd5v+SoI38EIDYNpIisLZf2h6nVXcrYHIPBQ73wVPDE4I7YAlh+8YVlo ANjko1NYU8ysvEMwmI8iTTmHslnuS8HZ0ruxmmuRoBMJidnYx8C61vTcflUXBtfRsatB sYLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:references:in-reply-to:user-agent:subject:cc:to:from :date:dkim-signature; bh=n2Bdj/sV7I9YYST/a6Ee+uQKpWStxnhqS6FCuPodXGI=; b=pqJBSx2qbhoPHk00rEjznI6rMTDYuBJ5L35ceU6eaVWwNx1JUaI+eQV2xHUjKzVFDC k8MeGu8nQjjEGC8NRB3dn6oEUzo7o1xqkaf1Gw/cszN6ji92JrrQoY/tuX0J+EmxT8/t IclcG/eUAjGmQFqsmfi1AZvAJggXVkq+R0JLpvgO8ApUqeFvr6Q8TMq8IdKsq25Woc38 mMXezlbMYzIr6f7UTR1fPx47HWSlNOWILDhwwAuuemiXY1Scyz99QA1Psn9v1nIYav/u EM8VBgCrz1f0oE+GympUMgpFHRwH3qbMvGYPR8YKYJWr17ZSLOdVX/IHDLBqe1oBV1GR lz/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=F3ZMKUgp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hg5si13484257ejc.279.2022.02.02.09.35.06; Wed, 02 Feb 2022 09:35:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=F3ZMKUgp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S1345581AbiBBPur (ORCPT + 99 others); Wed, 2 Feb 2022 10:50:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345566AbiBBPuo (ORCPT ); Wed, 2 Feb 2022 10:50:44 -0500 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DC41C061714 for ; Wed, 2 Feb 2022 07:50:44 -0800 (PST) Received: by mail-pj1-x102c.google.com with SMTP id o16-20020a17090aac1000b001b62f629953so7216940pjq.3 for ; Wed, 02 Feb 2022 07:50:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:user-agent:in-reply-to:references :message-id:mime-version:content-transfer-encoding; bh=n2Bdj/sV7I9YYST/a6Ee+uQKpWStxnhqS6FCuPodXGI=; b=F3ZMKUgp1tN25A8goPns7uiIg6+q4JCdBIfHNcLOjskWhHLztZ3IOsMjBPb9EYpP41 NlKix/zibD64RhL0/Gff9y2zuBiSFX5dVLWUOokhFGNetNeRL8JA3E8Ch2D5samJ+RdJ MQaWo7gjXWDOd3F40H32FV3yLyI+ehlw4QRy0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:user-agent:in-reply-to :references:message-id:mime-version:content-transfer-encoding; bh=n2Bdj/sV7I9YYST/a6Ee+uQKpWStxnhqS6FCuPodXGI=; b=NMDEPv1byygTZ1QkwsKEuebsHa+Wkmj1nxAbCH+kFLHPOCox9sTJDY3G6+a8gYzscf cU+kTXhjwQXRr5jWGJ4WuCRJeouqdIrseU2kKED+sHPyLoidmAP4cBUuYPUtY0ZOuN7j 4R16dALZRSmQUVk03bQOFh2YbNg34hWJsjbnlA1iLZHAy69FWjW5pnseXhUYdV+UiHW6 foVRdRc8NVKirwwm3tlIoHefqNp1MlHZVg5jqW06gXfoOvPJ/KrRxzO7//4sGb+ZBygv dEHInmiK0Gw/LDEXeDV3TwKC1PtUBqQl/idIxZTS7APN6KCqOaJYWrGZ6UPyUwwnAaly VRrA== X-Gm-Message-State: AOAM5300smdVkf6klZ//l0v5AxpUudpnr6em/GAVvHKwGYVbmMzI090q wb0jlCYwxJzg0/grGt+gpOaOtQ== X-Received: by 2002:a17:902:eacc:: with SMTP id p12mr31311692pld.123.1643817043774; Wed, 02 Feb 2022 07:50:43 -0800 (PST) Received: from [127.0.0.1] (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id h11sm14933953pfe.214.2022.02.02.07.50.43 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 02 Feb 2022 07:50:43 -0800 (PST) Date: Wed, 02 Feb 2022 07:50:42 -0800 From: Kees Cook To: Rich Felker CC: Andrew Morton , Ariadne Conill , Michael Kerrisk , Matthew Wilcox , Christian Brauner , Eric Biederman , Alexander Viro , linux-fsdevel@vger.kernel.org, stable@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] exec: Force single empty string when argv is empty User-Agent: K-9 Mail for Android In-Reply-To: <20220201145324.GA29634@brightrain.aerifal.cx> References: <20220201000947.2453721-1-keescook@chromium.org> <20220201145324.GA29634@brightrain.aerifal.cx> Message-ID: <1A24DA4E-2B15-4A95-B2A1-F5F963E0CD6F@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On February 1, 2022 6:53:25 AM PST, Rich Felker wrote: >On Mon, Jan 31, 2022 at 04:09:47PM -0800, Kees Cook wrote: >> Quoting[1] Ariadne Conill: >>=20 >> "In several other operating systems, it is a hard requirement that the >> second argument to execve(2) be the name of a program, thus prohibiting >> a scenario where argc < 1=2E POSIX 2017 also recommends this behaviour, >> but it is not an explicit requirement[2]: >>=20 >> The argument arg0 should point to a filename string that is >> associated with the process being started by one of the exec >> functions=2E >> =2E=2E=2E=2E >> Interestingly, Michael Kerrisk opened an issue about this in 2008[3], >> but there was no consensus to support fixing this issue then=2E >> Hopefully now that CVE-2021-4034 shows practical exploitative use[4] >> of this bug in a shellcode, we can reconsider=2E >>=20 >> This issue is being tracked in the KSPP issue tracker[5]=2E" >>=20 >> While the initial code searches[6][7] turned up what appeared to be >> mostly corner case tests, trying to that just reject argv =3D=3D NULL >> (or an immediately terminated pointer list) quickly started tripping[8] >> existing userspace programs=2E >>=20 >> The next best approach is forcing a single empty string into argv and >> adjusting argc to match=2E The number of programs depending on argc =3D= =3D 0 >> seems a smaller set than those calling execve with a NULL argv=2E >>=20 >> Account for the additional stack space in bprm_stack_limits()=2E Inject= an >> empty string when argc =3D=3D 0 (and set argc =3D 1)=2E Warn about the = case so >> userspace has some notice about the change: >>=20 >> process '=2E/argc0' launched '=2E/argc0' with NULL argv: empty stri= ng added >>=20 >> Additionally WARN() and reject NULL argv usage for kernel threads=2E >>=20 >> [1] https://lore=2Ekernel=2Eorg/lkml/20220127000724=2E15106-1-ariadne@d= ereferenced=2Eorg/ >> [2] https://pubs=2Eopengroup=2Eorg/onlinepubs/9699919799/functions/exec= =2Ehtml >> [3] https://bugzilla=2Ekernel=2Eorg/show_bug=2Ecgi?id=3D8408 >> [4] https://www=2Equalys=2Ecom/2022/01/25/cve-2021-4034/pwnkit=2Etxt >> [5] https://github=2Ecom/KSPP/linux/issues/176 >> [6] https://codesearch=2Edebian=2Enet/search?q=3Dexecve%5C+*%5C%28%5B%5= E%2C%5D%2B%2C+*NULL&literal=3D0 >> [7] https://codesearch=2Edebian=2Enet/search?q=3Dexeclp%3F%5Cs*%5C%28%5= B%5E%2C%5D%2B%2C%5Cs*NULL&literal=3D0 >> [8] https://lore=2Ekernel=2Eorg/lkml/20220131144352=2EGE16385@xsang-Opt= iPlex-9020/ >>=20 >> Reported-by: Ariadne Conill >> Reported-by: Michael Kerrisk >> Cc: Matthew Wilcox >> Cc: Christian Brauner >> Cc: Rich Felker >> Cc: Eric Biederman >> Cc: Alexander Viro >> Cc: linux-fsdevel@vger=2Ekernel=2Eorg >> Cc: stable@vger=2Ekernel=2Eorg >> Signed-off-by: Kees Cook >> --- >> fs/exec=2Ec | 26 +++++++++++++++++++++++++- >> 1 file changed, 25 insertions(+), 1 deletion(-) >>=20 >> diff --git a/fs/exec=2Ec b/fs/exec=2Ec >> index 79f2c9483302=2E=2Ebbf3aadf7ce1 100644 >> --- a/fs/exec=2Ec >> +++ b/fs/exec=2Ec >> @@ -495,8 +495,14 @@ static int bprm_stack_limits(struct linux_binprm *= bprm) >> * the stack=2E They aren't stored until much later when we can't >> * signal to the parent that the child has run out of stack space=2E >> * Instead, calculate it here so it's possible to fail gracefully=2E >> + * >> + * In the case of argc =3D 0, make sure there is space for adding a >> + * empty string (which will bump argc to 1), to ensure confused >> + * userspace programs don't start processing from argv[1], thinking >> + * argc can never be 0, to keep them from walking envp by accident=2E >> + * See do_execveat_common()=2E >> */ >> - ptr_size =3D (bprm->argc + bprm->envc) * sizeof(void *); >> + ptr_size =3D (min(bprm->argc, 1) + bprm->envc) * sizeof(void *); > >From #musl: > > kees: shouldn't the min(bprm->argc, 1) be max(=2E=2E=2E) in your p= atch? Fix has already been sent, yup=2E >I'm pretty sure without fixing that, you're introducing a giant vuln >here=2E I wouldn't say "giant", but yes, it weakened a defense in depth for avoidi= ng high stack utilization=2E > I believe this is the second time a patch attempting to fix this >non-vuln has proposed adding a new vuln=2E=2E=2E Mistakes happen, and that's why there is review and testing=2E Thank you f= or being part of the review process! :) -Kees --=20 Kees Cook