Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp5538251pxb; Wed, 26 Jan 2022 14:28:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJwI6l5jiUaCEaMO5fmoXbmqD2UfIIrxilE+ivIjcznHKOBKbiVG3iUPtzSx9hDk3ODgqpZf X-Received: by 2002:a17:907:2d8d:: with SMTP id gt13mr632420ejc.583.1643236129760; Wed, 26 Jan 2022 14:28:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643236129; cv=none; d=google.com; s=arc-20160816; b=O797Fj/OkiNXPZGY/uMG+t0Z1GlLB7EfYL7QSL44wCPXiIGy7QZ81mqYZ84o2omeg/ ZzRkDx56/Q1Jyad7nClqFq9uUPyWD3PzshBD1xr0Px174kvo6itHahCgaBVAMx9mIanT O9U82IDb3sB8w+xQYJy4hD5soaX6ok6cL6OGNHOhkiJcC5DBrJedSqyf1JSNMUoE37qG AMGQv/s2xwQ0r3v6CHYJA5R620Hm90FEfAWrAAGZbshlP0s36PolrcKwDrKN7FnjKZOu 8OaifR9VNjawMuG8f6Fbl1oXNj8RLyZsKyv6GI8AblLO+qxxTG84Gwhh7r8zxKpmmG2R X9aQ== 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=e6GlIwf2gQF3ITti5q/SlwCwbOwNxQ4Vmm6ghQbKw6g=; b=Q+PqLmixJ0N4jLZVLO1GktgfIOsXDKI5wNaPhzQMnWKaJbKR2P0DG+yxX9Nw2/xFNP jIeZCOVo8dvo4n833ilPQ8lsiPeNm6wCTvL20Nenv+A+MP0C8H9K76Shl/4z1qlDF33x 6VCDlTl5Nn0bltROobrHi6/oWqtaoH87ZdafgyVtXHdbTBLYvAMJMgT4q+8qUCHorJ59 BJL9DEeYDtYzuBqVwTe5LSJxZbHQN9bpxKsdPh30iGNg9W+r3LwQYbeUsUHJRAPL9vfY u6cLwSgad8PVW8rO//yTgah7zqHtuqSxr9sZNIl0a2UxGKwQ7YUfN3BpI4DgrTXM+WJ3 IEBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Ua1tzA6B; 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 j11si275397edq.3.2022.01.26.14.28.24; Wed, 26 Jan 2022 14:28:49 -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=Ua1tzA6B; 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 S244122AbiAZSHw (ORCPT + 99 others); Wed, 26 Jan 2022 13:07:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244144AbiAZSHt (ORCPT ); Wed, 26 Jan 2022 13:07:49 -0500 Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6207C06161C for ; Wed, 26 Jan 2022 10:07:48 -0800 (PST) Received: by mail-lj1-x22c.google.com with SMTP id z7so774003ljj.4 for ; Wed, 26 Jan 2022 10:07:48 -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=e6GlIwf2gQF3ITti5q/SlwCwbOwNxQ4Vmm6ghQbKw6g=; b=Ua1tzA6BRz4Kicy3wWXE4baxIRed4b9N+oLT8SW2I9QdF1CllcC4jq2nAn5zWd10IO NRGiqajhZhjSigz6UU0a1JEOJou2zOPJ12aECIZ9bjc8mfmO0BauwNMsuq04vVIodApe IHoHDZhgNwNhnqgs3I9PsX7XKNZH1bfLcGbAjDEEmBsdWnRJLrk392k4up2kfQWxGBGK ZE4p0xGXNI2SGwMhgAwQMHa42mmsoHGdVIudHFz4d01ODNhzl2n8r9F9WWgY8V6Vrxgp HvvR2P09koQ/4fm1aRPs7B8PGp9XZEqJKfuuPaWHYVph0twQVUqPMaLvRxCGSEexEdlU tOWg== 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=e6GlIwf2gQF3ITti5q/SlwCwbOwNxQ4Vmm6ghQbKw6g=; b=FjJDEuITRT5gWQKDGLqecXorYCXSshjY95pmdemab4b/adXbzo8WL4nMsVaySzS1hs raaSzvmZncUdgHVPlr2A4tjgHgDzGT1QHu1qcgRcmgUuESBDaM6SVK29AmPF+6cfpfgA pra2H1VvXzP3MRQZyfiTrcbs+iEaWiKQM0txXeYvv+Ix+nCrsPacPiltaur+tGPsXDgw V+fSedyioKp7BBmsmN3tMMUhd3OvbH+PrO5U+bPxIlFHF/Kz5MjdatKtzxDqRbUN2BzW 0/+mJbYi0D4IKJpRvMLO6rUf9t442Y8Cib9rw0mQUzbwOkoVg/UQLfREw39a1csUtIsy Zf3Q== X-Gm-Message-State: AOAM531Qf47yp0OO2OcKIBeFuVwrsr2P2QrXlArynqQGG2SnkqnL9jvD cn/ITVWQ0OL+OV7218RMjO9IpU7dKXUXFSbUPhNc5CQsxIs= X-Received: by 2002:a05:651c:984:: with SMTP id b4mr153763ljq.235.1643220466896; Wed, 26 Jan 2022 10:07:46 -0800 (PST) MIME-Version: 1.0 References: <20220126175747.3270945-1-keescook@chromium.org> In-Reply-To: <20220126175747.3270945-1-keescook@chromium.org> From: Jann Horn Date: Wed, 26 Jan 2022 19:07:20 +0100 Message-ID: Subject: Re: [PATCH] fs/binfmt_elf: Add padding NULL when argc == 0 To: Kees Cook Cc: Ariadne Conill , 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 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. 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)?