Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp5543125pxb; Wed, 26 Jan 2022 14:36:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJyoQCv84nonWekuH+FCKi2pji3F5/KhfgnqhSXca6pLTue5qhh9PY1EI9e//iQyBoN8USoM X-Received: by 2002:a17:906:478b:: with SMTP id cw11mr715288ejc.35.1643236610093; Wed, 26 Jan 2022 14:36:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643236610; cv=none; d=google.com; s=arc-20160816; b=pg361n63I3hPbV0zwQOvYE72rUnJ05lGR+OHwCgG7qD29UOByk7yaI03GT7hyL7JJO EbMuxC2tEvn5nfZnltdUUlY+bzLkQ/UpqrzXLXo2WRvYJ+tb3eztbRN0ExHdjH1K4X/C HaiVz7pP9dZkO952+KJgnm4Ivj+I+YReG9DZMrFtRgBtJe9tRGZG+DESwf2Q2EVE5hC1 GaRHIg44Ca5Ac1Ox8KM+VHR8tnBuBQT377JCo+TREZdQio0sTALeMJyMvHUzL8T6SsXs Vs1ULsnWxKBDU1k/8qq9iHzZcEF8wAblYNz3pkiUeQZb+Ds9kLk6k0ZaNbxYlvNF2dX3 pkng== 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=pBGfaiXr2odCJuv3v+l1X6S49qybF/E8gAqwCt/alyg=; b=zmEqJGxC+3/iAxF5CcLga+LLoeKvDLXUzD/lf/nQFzotgKMCu77mpX8nO2m12ZZLr7 9/ECgsXSsmBZMytJRjklgBee4Y0CbVtz/6f3vTg48Vq+NAdzwLjdPP1ABW8DEPpcrbz8 c0vvM5/3iI7TSFdkaN30viiK7W6HGPe6MX2uR6fm5UvBW2piawt2ebshJdM3t5sS+qDu zDQ3xbKB6WTtkf/Sa/knrDSln3IVHp+vWZie+CKg+Y5/25lpzKiYyzH/s2ZoCJOx8yEO RtoxHHbn7rvfY/dMKeA+9JgBgS+Gq0k6dVPwwRJf9/562K2Za3EE/IqaBtxh3/y2PDZJ rfqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=bNRb4JYQ; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id do18si241410ejc.934.2022.01.26.14.36.22; Wed, 26 Jan 2022 14:36:50 -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=@infradead.org header.s=casper.20170209 header.b=bNRb4JYQ; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229939AbiAZUIq (ORCPT + 99 others); Wed, 26 Jan 2022 15:08:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229601AbiAZUIp (ORCPT ); Wed, 26 Jan 2022 15:08:45 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6D89C06161C; Wed, 26 Jan 2022 12:08:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=pBGfaiXr2odCJuv3v+l1X6S49qybF/E8gAqwCt/alyg=; b=bNRb4JYQwKcMtQW4Tp/TU3w8I3 ly428vCW5JXET+V4u8AZVpNtDZjpRwugtd+nAEOdPmuqUFnVWqY1CPYdfc2qsGiDhRG0SjZDO7TXK Cjp/6ymK1ZruwOxLunpKfJhpV6nBgy791QPhu1OEJttaSO0kneif6k0bKzjAKHIYy7fYOIMwu0e2T D6ZRShV6NoPQdis8j8oZ+X9d4yGQ98iRcCsBrQ3/NkD4wfXwxsvf6b8+V0BjSZVhe+XAaOCCqS/KT 347nnhtJbKi1omhIJ/dgkzXcy36MhVCMaYNF+2NK1vIpBbZLk32/qxevmbbwH9/jH6RCCAE3knupR M0BxGzmg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nCoaU-004PLG-HV; Wed, 26 Jan 2022 20:08:14 +0000 Date: Wed, 26 Jan 2022 20:08:14 +0000 From: Matthew Wilcox To: Kees Cook Cc: Jann Horn , Ariadne Conill , Michael Kerrisk , Christian Brauner , Rich Felker , 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] fs/binfmt_elf: Add padding NULL when argc == 0 Message-ID: References: <20220126175747.3270945-1-keescook@chromium.org> <202201261157.9C3D3C36@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202201261157.9C3D3C36@keescook> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 26, 2022 at 11:58:39AM -0800, Kees Cook wrote: > On Wed, Jan 26, 2022 at 08:50:39PM +0100, Jann Horn wrote: > > On Wed, Jan 26, 2022 at 7:42 PM Ariadne Conill wrote: > > > On Wed, 26 Jan 2022, Jann Horn wrote: > > > > On Wed, Jan 26, 2022 at 6:58 PM Kees Cook wrote: > > > >> Quoting Ariadne Conill: > > > >> > > > >> "In several other operating systems, it is a hard requirement that the > > > >> first 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[1]: > > > >> > > > >> The argument arg0 should point to a filename string that is > > > >> associated with the process being started by one of the exec > > > >> functions. > > > >> ... > > > >> Interestingly, Michael Kerrisk opened an issue about this in 2008[2], > > > >> but there was no consensus to support fixing this issue then. > > > >> Hopefully now that CVE-2021-4034 shows practical exploitative use[3] > > > >> of this bug in a shellcode, we can reconsider." > > > >> > > > >> An examination of existing[4] users of execve(..., NULL, NULL) shows > > > >> mostly test code, or example rootkit code. While rejecting a NULL argv > > > >> would be preferred, it looks like the main cause of userspace confusion > > > >> is an assumption that argc >= 1, and buggy programs may skip argv[0] > > > >> when iterating. To protect against userspace bugs of this nature, insert > > > >> an extra NULL pointer in argv when argc == 0, so that argv[1] != envp[0]. > > > >> > > > >> Note that this is only done in the argc == 0 case because some userspace > > > >> programs expect to find envp at exactly argv[argc]. The overlap of these > > > >> two misguided assumptions is believed to be zero. > > > > > > > > Will this result in the executed program being told that argc==0 but > > > > having an extra NULL pointer on the stack? > > > > If so, I believe this breaks the x86-64 ABI documented at > > > > https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf - page 29, > > > > figure 3.9 describes the layout of the initial process stack. > > > > > > I'm presently compiling a kernel with the patch to see if it works or not. > > > > > > > Actually, does this even work? Can a program still properly access its > > > > environment variables when invoked with argc==0 with this patch > > > > applied? AFAIU the way userspace locates envv on x86-64 is by > > > > calculating 8*(argc+1)? > > > > > > In the other thread, it was suggested that perhaps we should set up an > > > argv of {"", NULL}. In that case, it seems like it would be safe to claim > > > argc == 1. > > > > > > What do you think? > > > > Sounds good to me, since that's something that could also happen > > normally if userspace calls execve(..., {"", NULL}, ...). > > > > (I'd like it even better if we could just bail out with an error code, > > but I guess the risk of breakage might be too high with that > > approach?) > > We can't mutate argc; it'll turn at least some userspace into an > infinite loop: > https://sources.debian.org/src/valgrind/1:3.18.1-1/none/tests/execve.c/?hl=22#L22 How does that become an infinite loop? We obviously wouldn't mutate argc in the caller, just the callee. Also, there's a version of this where we only mutate argc if we're executing a setuid program, which would remove the privilege escalation part of things.