Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp5541343pxb; Wed, 26 Jan 2022 14:34:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJwzIL6pPb8C/aqnzBV/FSJr/ukz7fg6w5MMf+tgzWecLB+4UVtKpjPqTaSRGKCkEUAAt6pE X-Received: by 2002:a17:906:974c:: with SMTP id o12mr756144ejy.208.1643236457372; Wed, 26 Jan 2022 14:34:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643236457; cv=none; d=google.com; s=arc-20160816; b=CKqKpgfBpje4Jzf37lPGzSPnOke9aPbaWtGgwHjKN1hpzMul/FwMNrZLJ2CgduNbMI eh7rqhjgmgH/W0L9mbYCSEyxmCbqUK5bPOuJja7UkNQ1Nm0+wl/bs7zc6jrKx64W5Bpn Hj66DRIVPXo77d4l6kROtjU0K70g1BEyK1shqGPDV+utB8dDFeLrerCQaipUl4RN69YN qXcvt/O7XOjRzrVzuci86OBr1z+oWflbsTbUdxI3DX14ZHyL5vvyIDnaOqAK3DvrKkoS sKa/rFtO4ESux82YBCBVPQLKtWW4E9ehXl6fo7wSdxFw7hB/GOGaGXKiNpp4Ew/tMmWh od6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=zuNchwIZAGymNJC1SQWN7Ldl6BhEKsuYVz79xP7hsik=; b=dZ4ZwHhmedCKoYiLg06/fxK5PL+gto7cUp7CwcuGOvfzrlSZ1CSucU9wnGJlN0+PTy bjscLfpCr8PFm77YQ2Oe8gfT9p7He10oQKb8wcLvDbHnWwzI4r7fuOBH+3v1UIwag0dI R+IEE2YR1xO1CdzfurQWylgp4l7j9wShDqO3U7wqDKIHym22KCBR16/XY71G2elF3u6Q WZ4DzQ1jjVDiYEs/UfDfp1VdVnA8s7789XI6yH3gyZUYAyR6aOJPMrpd4R8CGowybMUh uNoJ7aNcSmLm/bZRX1QMq2dbD9Mny3dwlqGEmySi1hvBmr6H4UWgtxsoREECmzU/tkb1 rMVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=O8OLscWg; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z6si280975edc.603.2022.01.26.14.33.52; Wed, 26 Jan 2022 14:34:17 -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=@google.com header.s=20210112 header.b=O8OLscWg; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244630AbiAZTvV (ORCPT + 99 others); Wed, 26 Jan 2022 14:51:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244571AbiAZTvI (ORCPT ); Wed, 26 Jan 2022 14:51:08 -0500 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70A08C061748 for ; Wed, 26 Jan 2022 11:51:08 -0800 (PST) Received: by mail-lf1-x12b.google.com with SMTP id b9so986395lfq.6 for ; Wed, 26 Jan 2022 11:51:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=zuNchwIZAGymNJC1SQWN7Ldl6BhEKsuYVz79xP7hsik=; b=O8OLscWgdo8HIVwwfzb5dza2dJnXOZndHiDf93M0yfSU4tRodlztvMJGdacXImWFvT A4bYsx1Mbprxlx5fVLCI461Tn/UrA6CYFDOhYredwv63xNGXFUShGpNuzDAOw5ahH/wy fku0dNyYzo7gA9deEfIX9Ymwmf9QKMCC+oudZWHsLMyD1zQulwYSPfLrvVR5/6dhtLMv i8Rpx83onnbgfc/fJMHs6Eo3x2Az2PeOWwaGFbSZDbJrXy7zvLbHFcwkKlszP4w5ibDU SdzVZ4DBXl12xuVCuvS3fMEjBBVETtA+hOE3GdrcD8ofv5KDpVKOl0uPkj5L+7ETEBsq 8PvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=zuNchwIZAGymNJC1SQWN7Ldl6BhEKsuYVz79xP7hsik=; b=AVuKK1F7x04RKw1CQWc83a+KgFE6gFZS0nGrU4USgjWqQ1t5BhjJYCooVyBBX5Omjp NbQIxqBl+ydyblszNbvu0PzYgwg9EI4aBip1uz0N0KNwxxAJsOepWZZwcAeJXKG9SLIX QcukqyoVBWiOgM2uKbtKhsYdaq1THGMsxdnffXWg57MG2rFjIIT7hVID8GdJrh0hTcNG VQ84Fmrly5KWzKxQlHfCLMZlLINLqA43T7ZC0zXd/3AQNQWTCJcUILnc35qzfP0LKYbj RaxLVBmeIurOUn9TPNJsqEmjZM0hefHcfPZuWfqXbx0f7c0Gj9wQQ5upDcq+4Tz9k2Qd 5fbQ== X-Gm-Message-State: AOAM531cdC9/oNFZS64UViLFGc6e6+P3MLK5ovvJGN3s3wVuHXqHJa/c LREw+B/scVfDR09Xon7Njp7g1tnIY1r/kbLbQaMb9A== X-Received: by 2002:a19:ee13:: with SMTP id g19mr384157lfb.288.1643226666411; Wed, 26 Jan 2022 11:51:06 -0800 (PST) MIME-Version: 1.0 References: <20220126175747.3270945-1-keescook@chromium.org> In-Reply-To: From: Jann Horn Date: Wed, 26 Jan 2022 20:50:39 +0100 Message-ID: Subject: Re: [PATCH] fs/binfmt_elf: Add padding NULL when argc == 0 To: Ariadne Conill Cc: Kees Cook , Michael Kerrisk , Matthew Wilcox , 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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?)