Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp5542277pxb; Wed, 26 Jan 2022 14:35:36 -0800 (PST) X-Google-Smtp-Source: ABdhPJyrWt55Yw6rHeLBVLN7Ri+49X44dV7M72dYSVqovwFtBbzO2cDGNwB5/E5d9cVQiGZotJZJ X-Received: by 2002:a17:907:ea2:: with SMTP id ho34mr692632ejc.698.1643236535980; Wed, 26 Jan 2022 14:35:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643236535; cv=none; d=google.com; s=arc-20160816; b=WSZ7S/0uQYkRu73kpKwmRkr6p0YHvLYrp+F6cr9u08eyB4iEt71cayTaUFRSq5D7W/ RCEiuZ7gcKDESGsgPDEOFli/D4ZNxC6ijk7lCU+rGm31KseXjfBFVv85TYu1y0Z7+O94 OOi/CZ33hmlTUXHMCAI22Z5c5YI3PV8v9dPUQbKTpB5lygY+ArI2i8vlZcLf6AQUhQpx FBPxfz9olxIX0vXpDVyyG3mqaPu+tuVkbck+P7CUxVy3xsANSq+M5QB7bFxPe9m141n5 gdccZjqtVzk8YL39MMqI7vuubXh2FQhkNBjr4YyTBmSrvK/RDlvTBfzUlTPkRTY6wjt3 hH5w== 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=epiW7EOJDtCxAnePBLYcB/dsG4ABauHMGQc+aHTZ3nU=; b=CxJcYnzJGIN83LZmDp6Q44mNvmhjsiHC7ti0fBfPqsNbGEpQGFAv3Mkwktev9bIdrV s2A175bxbiT0oYPqKn0QlEaATXmHIgW5ILndh0TbEZ9Zwdj1EZceJGE243xcsnmGkBZm YhgQGGNmuaDyBmnFpwInu6endPYFyTg/xf1CGsjqkzkWyHznOCqZSARERYQ4oNkWHQ8R zs9iMHfHKcq+5FxS6SLvcLVgoFbjeHJnfD19vv91+S0Eau94Jv9DDMRFM+7esktW52jp /1hKkGw9ij3BLmpGSRBilVqJuRbWRhmhscWJ7f3SuLX9y56IsxMMg2R6MBCMrE4IiW+i pEDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=YP4Atk8A; 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 dp20si301970ejc.526.2022.01.26.14.35.09; Wed, 26 Jan 2022 14:35:35 -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=YP4Atk8A; 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 S229437AbiAZT6s (ORCPT + 99 others); Wed, 26 Jan 2022 14:58:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229534AbiAZT6k (ORCPT ); Wed, 26 Jan 2022 14:58:40 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B62BC061747 for ; Wed, 26 Jan 2022 11:58:40 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id y17so544542plg.7 for ; Wed, 26 Jan 2022 11:58:40 -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=epiW7EOJDtCxAnePBLYcB/dsG4ABauHMGQc+aHTZ3nU=; b=YP4Atk8A6QOHE6yKgxHwvv98aqnpitZZJ04nawZm/fg1pufFSNHMRiSou+FwIASga2 mhqGC0T6cDKKo0TKVksBquQg2k4k1a0Jd9Y/z8ZJrfDQoZBA5TE0Nx5TNlEhDMlLLd7o QApbenrmidG6D6VtYMw0VJ+32+MD0iBsapx/4= 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=epiW7EOJDtCxAnePBLYcB/dsG4ABauHMGQc+aHTZ3nU=; b=j7RB4dPgP91inGhh8ooK75B2SkIRa1bUsyBM8YQk9LjDEm9Fv1tBqeiGJB5cXsFhFs i41VAECK43/7N+Uz4YDchC4iDktRjbblUS45kjrpvHgDbRj/mZJm8m3j8GhNPJ9VAFPq MC69FlDwU8Cqwh3ro5/5DMflPeKTQTlkEAJhrLZtx6fnK7/G4Su2EP+jzyjscITsusDy Fnc2cBZwHZMzYAvXGQkUI+WOvcxmUZ1OQ4W9TA8bzIAcVpf5J78UEHakRHMM2heBcnh2 ipKeXAWivj7QteFj0HGMR8Mt8MoW+MoAVo2U58K9vHu/RGbGH2PSKKEpV6ThhjTRqWMr m7Ew== X-Gm-Message-State: AOAM531cempwmxIqrOtAo0Ftc7MLxrD2kR3JW7LEPEbOtV7VM4VjSui4 k591P7ZST/Iu6++mWJX6JwhvMQ== X-Received: by 2002:a17:903:1109:: with SMTP id n9mr158953plh.163.1643227119920; Wed, 26 Jan 2022 11:58:39 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id n4sm5682138pjf.0.2022.01.26.11.58.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 26 Jan 2022 11:58:39 -0800 (PST) Date: Wed, 26 Jan 2022 11:58:39 -0800 From: Kees Cook To: Jann Horn 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 Subject: Re: [PATCH] fs/binfmt_elf: Add padding NULL when argc == 0 Message-ID: <202201261157.9C3D3C36@keescook> References: <20220126175747.3270945-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 -- Kees Cook