Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp5979717pxb; Thu, 27 Jan 2022 04:01:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJyYH8G/yI1gZGK+9H98wirO2uzcBtJ1Jk5hmQP8FbLiXmdxNIvLrMu4OZbyMkq1Zla1Lbzu X-Received: by 2002:a17:902:7682:: with SMTP id m2mr2848189pll.71.1643284872538; Thu, 27 Jan 2022 04:01:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643284872; cv=none; d=google.com; s=arc-20160816; b=Qw1aj/yWmYZiut6x8yjIzNmu1DyQaP5W47TIvjTTRgeZnl8MyEQ0VGHodanTYeVsqC 93mFNmR3qtjRIjQ12fqnP7jw6KP7EomlDvgM63IUPhwxIU8Sk3LQriXof8ljH9MgSedn F61APnhMBAAdTpZ3vPwOdatZHBUtSDeRb+ICuzEGWB0dU6lfI1FC5rBupWjEH5y7u89B RDTpiBrSyW9p5GB0ZCIcznrFP/a2EPun6hPZt6EQBqxwMEXiul1AdqeMoPw/cPDb4iqn FC2LnKzrqSsMUw0J8FVfeSV3Hwm3lB4ULkbxYyYABXS7L8Puj5M+EXvnO/DhZT/r1x5q N89A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=beUbO3aZF/2kD/fftJXd1EMY7Ug/jw5Wb5BLQxmuCc0=; b=SoKVJfC784yYJr/N7NBAYTcFzAhcfagK6oVBVO307C1OQU4KgSvOH+6N6ZZ4NUE2ZG yacEMVrgA3N5vRPFpwONQPDdCMSgKwYbvO2oN73qZSJ7N8ylMDTI6/bEKbYNO9olxbLV r1MNOHQruQwYFvryz8FFca7+7dkX5wDWLa7SGHiTJON2YOP1E0QA1LWfexcWM9L6Ivw+ TMV1MBqyC6/n3tcDgDel2H6Z8EJu+4z9bNByPHddobmWDWKvDU/N+BlfWGbLk5hyQh19 ZAxgHI5GBckI3xUw5QUrfkLkS5fRNUDtpPF/rOj3Y1DlpzNGoy0MiQTiHaLUEY/m14Nd WN/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Mkb0P6sW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id gl7si4913440pjb.5.2022.01.27.04.01.00; Thu, 27 Jan 2022 04:01:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Mkb0P6sW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S236188AbiA0F3G (ORCPT + 99 others); Thu, 27 Jan 2022 00:29:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233121AbiA0F3E (ORCPT ); Thu, 27 Jan 2022 00:29:04 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C2B5C061747 for ; Wed, 26 Jan 2022 21:29:04 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id h12so1791282pjq.3 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=Bph9JFyMq3YyoxKMe8yLagDYGHZdi4Iw4pcPUx02va2pyH9i0GpNxr16RgjLWl/jUL lzK17aGgJqE7iV0mh/e4fUpy0GkFI+2n9x/i3DICi+VKpjqlvuRkY834g+zPhhaRdwaH FTQ8DO4zGDvnLFIyiXD1eMuW1oa30Qemd22C7yPWhPh7Sd4FlE+dA7VI0BKn+iq9qFU4 kRvh5hnG7ePogZ8/OjydjOeWuekvDR8NpVwnvyH9cXtT6l1QC4t9lY2qlossob6V/Id2 7kTKlZa+tiYE6P7Rv1nkXbwJ2d98QUbZ16l0NLEHDBKewJatZBDa6q+FfyUdjw8lSFtF LmVg== X-Gm-Message-State: AOAM530H3DZLy4wZQUlA3cnKRsFJzz8wQFWq8SyusosHvVx6PuMmZpzc +UbMxpxU82hAJ0DWhDr2MDF9KQ== 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> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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