From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB15FC433FE for ; Thu, 27 Jan 2022 05:29:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2F886B0072; Thu, 27 Jan 2022 00:29:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DDE966B0073; Thu, 27 Jan 2022 00:29:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA5786B0075; Thu, 27 Jan 2022 00:29:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0048.hostedemail.com [216.40.44.48]) by kanga.kvack.org (Postfix) with ESMTP id BB9C96B0072 for ; Thu, 27 Jan 2022 00:29:05 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 703898249980 for ; Thu, 27 Jan 2022 05:29:05 +0000 (UTC) X-FDA: 79074938292.28.102B80E Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf18.hostedemail.com (Postfix) with ESMTP id 2195B1C0003 for ; Thu, 27 Jan 2022 05:29:04 +0000 (UTC) Received: by mail-pj1-f46.google.com with SMTP id d5so1784437pjk.5 for ; Wed, 26 Jan 2022 21:29:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=beUbO3aZF/2kD/fftJXd1EMY7Ug/jw5Wb5BLQxmuCc0=; b=Mkb0P6sWcL3VkVCy3yV8rbOVXIel3FZSZaHpdJ8N0lcjYq6zyx4hUi4mBTLMFaZHeJ lQOR0wGzTYdQ3uC9REhaHrWW8kVj+HKmE2/bLDMmx/G+cO0ZRzYvQBxl7fh/1edZVY++ 1c90ytUDBbGfHOe4ubEL896FjJreV4KMOheIs= 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:message-id:references :mime-version:content-disposition:in-reply-to; bh=beUbO3aZF/2kD/fftJXd1EMY7Ug/jw5Wb5BLQxmuCc0=; b=lApbHy/ZZfd/IOtHPjzlog3LFMjffuVegvhqCQobHXeIoPsW7rTFt0N52UqZFzzGc7 h0xOTq5y5ahDkxu8QY/+ZXW5NqAov1j4FMBEk/B7JN2ym7IXmtMo4ZTXzSDxRLfFxu/a 52tCXp6mQDYGVD8ZAMshmGnibGpxlp1n5haOhKH3w/MRxBtF6aN8v7+99k0Squ2A/m56 lMC4m7x1JVSO4vv0gb3OviliBLUo5zQdRMJ8fJXM6IIhBxOCYEs6JQhf46oOFBzE682r 6oCsgxOwNJ2aHNkZTnT7l2ulygevTMwhaNgeZGO7mFMpDCAddf5sqOICDSMPx2Jj1hu6 bGuQ== X-Gm-Message-State: AOAM533hBc/YtjOrSQfcfiqiAuVOJn8/ZTnHraDSQXIEv+ACtvZH2Sn4 NniM0EgV6dEwMWf2PlM8dTzGMQ== X-Google-Smtp-Source: ABdhPJwXGj2Q6bkytWK59hvGMzK64mkTpY3dYCfOP2HGl0XbWy3r/zGyfAjqVAai7Y4ZsX1k9UxMgw== X-Received: by 2002:a17:903:2309:: with SMTP id d9mr1768705plh.149.1643261344044; Wed, 26 Jan 2022 21:29:04 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id n4sm6545740pjf.0.2022.01.26.21.29.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 21:29:03 -0800 (PST) Date: Wed, 26 Jan 2022 21:29:02 -0800 From: Kees Cook To: Ariadne Conill Cc: Andrew Morton , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Eric Biederman , Alexander Viro , Michael Kerrisk , Matthew Wilcox , Christian Brauner , Rich Felker , linux-mm@kvack.org, stable@vger.kernel.org Subject: Re: [PATCH v3] fs/exec: require argv[0] presence in do_execveat_common() Message-ID: <202201262119.105FA8BCA9@keescook> References: <20220127000724.15106-1-ariadne@dereferenced.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220127000724.15106-1-ariadne@dereferenced.org> X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 2195B1C0003 X-Stat-Signature: ucw5tiezzmkfw74o17xnauxsf5jtq3b6 X-Rspam-User: nil Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=Mkb0P6sW; spf=pass (imf18.hostedemail.com: domain of keescook@chromium.org designates 209.85.216.46 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-HE-Tag: 1643261344-788681 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Jan 27, 2022 at 12:07:24AM +0000, Ariadne Conill wrote: > 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. POSIX 2017 also recommends this behaviour, > but it is not an explicit requirement[0]: > > The argument arg0 should point to a filename string that is > associated with the process being started by one of the exec > functions. > > To ensure that execve(2) with argc < 1 is not a useful tool for > shellcode to use, we can validate this in do_execveat_common() and > fail for this scenario, effectively blocking successful exploitation > of CVE-2021-4034 and similar bugs which depend on execve(2) working > with argc < 1. > > We use -EINVAL for this case, mirroring recent changes to FreeBSD and > OpenBSD. -EINVAL is also used by QNX for this, while Solaris uses > -EFAULT. > > In earlier versions of the patch, it was proposed that we create a > fake argv for applications to use when argc < 1, but it was concluded > that it would be better to just fail the execve(2) in these cases, as > launching a process with an empty or NULL argv[0] was likely to just > cause more problems. Let's do it and see what breaks. :) I do see at least tools/testing/selftests/exec/recursion-depth.c will need a fix. And maybe testcases/kernel/syscalls/execveat/execveat.h in LTP. Acked-by: Kees Cook > > Interestingly, Michael Kerrisk opened an issue about this in 2008[1], > but there was no consensus to support fixing this issue then. > Hopefully now that CVE-2021-4034 shows practical exploitative use[2] > of this bug in a shellcode, we can reconsider. > > This issue is being tracked in the KSPP issue tracker[3]. > > There are a few[4][5] minor edge cases (primarily in test suites) that > are caught by this, but we plan to work with the projects to fix those > edge cases. > > [0]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html > [1]: https://bugzilla.kernel.org/show_bug.cgi?id=8408 > [2]: https://www.qualys.com/2022/01/25/cve-2021-4034/pwnkit.txt > [3]: https://github.com/KSPP/linux/issues/176 > [4]: https://codesearch.debian.net/search?q=execve%5C+*%5C%28%5B%5E%2C%5D%2B%2C+*NULL&literal=0 > [5]: https://codesearch.debian.net/search?q=execlp%3F%5Cs*%5C%28%5B%5E%2C%5D%2B%2C%5Cs*NULL&literal=0 > > Changes from v2: > - Switch to using -EINVAL as the error code for this. > - Use pr_warn_once() to warn when an execve(2) is rejected due to NULL > argv. > > Changes from v1: > - Rework commit message significantly. > - Make the argv[0] check explicit rather than hijacking the error-check > for count(). > > Reported-by: Michael Kerrisk > To: Andrew Morton > Cc: Matthew Wilcox > Cc: Christian Brauner > Cc: Rich Felker > Cc: Eric Biederman > Cc: Alexander Viro > Cc: Kees Cook > Cc: linux-fsdevel@vger.kernel.org > Cc: linux-mm@kvack.org > Cc: stable@vger.kernel.org > Signed-off-by: Ariadne Conill > --- > fs/exec.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/fs/exec.c b/fs/exec.c > index 79f2c9483302..982730cfe3b8 100644 > --- a/fs/exec.c > +++ b/fs/exec.c > @@ -1897,6 +1897,10 @@ static int do_execveat_common(int fd, struct filename *filename, > } > > retval = count(argv, MAX_ARG_STRINGS); > + if (retval == 0) { > + pr_warn_once("Attempted to run process '%s' with NULL argv\n", bprm->filename); > + retval = -EINVAL; > + } > if (retval < 0) > goto out_free; > bprm->argc = retval; > -- > 2.34.1 > -- Kees Cook