Received: by 2002:a05:6a10:8a4d:0:0:0:0 with SMTP id dn13csp89054pxb; Thu, 12 Aug 2021 11:26:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGm3f7gFzQh+lhxoqJUB0Nk1VxeD3OJDj6ezDLv1x1C7i1eV36KBztcM518s9WYIIy/dF5 X-Received: by 2002:a02:29c1:: with SMTP id p184mr5028517jap.32.1628792803092; Thu, 12 Aug 2021 11:26:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628792803; cv=none; d=google.com; s=arc-20160816; b=DmvIGw4c0Wnp3SfJcen8qoLSwH8PgJZ+gczDUT/vn7P8XQR/R0OVq09hrntUUW+C8/ Y25cW7nOYb+3dCQb011YAVkt0T+sUi2FqANvH+TYIYxUP3mozGu9b0Vm49ywhfgorIkT FFL07X0TvT3KSoJK5ZBqqwDB1ytTJKPcPtfXpNX6HgX4J16aoTa1eicp8WIZeXzdTDhs 885uNcSX9UrfvlmMUaJYhmd2KzTfnkFd4WwBupT7pEP4bODHmJVab7XRtj2yxQcvk6VG RtCtRC0MA/9ltM+umtifETyAXQKZ9xcgANd8WPrzOkoUBkSo75xew3jUX0CGGGsBgic7 0ziw== 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=nMdbm1Duu6T2o7Pmii6aGZ5IPzKeI0bawgd87K5hISA=; b=Su5xchGk3wig4N54ibRUYpX+v1e5KoBx0IDkohkLEmryVtkRrlw8wGDCNjsbAGmscJ rG5bLFz6XG8EMkBNU1mn3dppheMyRjzvgfPUP1Z0f9G5PbwJLdRP/bBen55f616e5tqZ GB91jbrHS/F4gJQUAKV6jf/wrdfFOxEsPVL9+mQDhaCLOJ+pklS2sGofLdtHutzf61os bWvRj0j6Dj12nzPPFAeLlE8ORte0FYrV/7VJlP4GmC6Ydyftb8ekgR92rOyGgk3WwtTg zQTwB0H8+GHi0QzNqZn5O1Sb74ubD5dXF0++3DNHZJqNhAm5RaAruPgsF2Nyrymn1vCj 9k9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=XKB75bis; 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 z16si3848468jat.29.2021.08.12.11.26.31; Thu, 12 Aug 2021 11:26:43 -0700 (PDT) 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=@linux-foundation.org header.s=google header.b=XKB75bis; 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 S233785AbhHLRAy (ORCPT + 99 others); Thu, 12 Aug 2021 13:00:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231470AbhHLRAx (ORCPT ); Thu, 12 Aug 2021 13:00:53 -0400 Received: from mail-lj1-x231.google.com (mail-lj1-x231.google.com [IPv6:2a00:1450:4864:20::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 241A7C061756 for ; Thu, 12 Aug 2021 10:00:28 -0700 (PDT) Received: by mail-lj1-x231.google.com with SMTP id n7so11617323ljq.0 for ; Thu, 12 Aug 2021 10:00:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=nMdbm1Duu6T2o7Pmii6aGZ5IPzKeI0bawgd87K5hISA=; b=XKB75bis9tKuNhHX3eDTMKeSxlvftJ15dFdW/PqjC89Ur4xgXcznCXav012keKDr9q uVwQApi3YeYfIStj4+gOxYXeF8i11qPieLQ1yiCkx373DM+kh0ueZlzSltpcrquT6StP 3ucAz74g4LfHvAL2yjncpx8WOYjTgEkJKVJzQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=nMdbm1Duu6T2o7Pmii6aGZ5IPzKeI0bawgd87K5hISA=; b=hC2ll0+96yheJ2RiOfkMW4bb1nMMDs6rkCBKMQlS4B6+/WUN6LnvGQi4ihsT8VOxNB D1Lkq2GNa8tc2S/0SZ5f/mxtOHjCCGwJkgClK5LbvLUvScrjUpa/CGL2xwHe+TY9+d8t H0RqEKwotja99FlWq8zSdi+3jmjwTW5gl4Sg0j6j13lyyAB6GaUdVOBRRYnK0P2PLNq+ gUQrHoRPKCpnQagjR7h8KEW6U5DXUW8UMfbzuBc/9nWwvvY2+UhQ1Paqv6S1xZ6I9Ion sLwH7G/19gm8/EIOltssDds2KZy53oTXbMGNG7GvIAne3hpgOxpwUirJvjRDulxSeZcy /xGw== X-Gm-Message-State: AOAM533heGUHWfO0CjUympdNFjSIMD4qxu7WeDBWlMCeZjSmSvQ4YB70 OOVoa3m3Bmql2CHxJi6CcxvTyzO7EKhy+CeiHBQ= X-Received: by 2002:a2e:9998:: with SMTP id w24mr3674480lji.86.1628787625777; Thu, 12 Aug 2021 10:00:25 -0700 (PDT) Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com. [209.85.167.51]) by smtp.gmail.com with ESMTPSA id j1sm368839lji.124.2021.08.12.10.00.25 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 Aug 2021 10:00:25 -0700 (PDT) Received: by mail-lf1-f51.google.com with SMTP id n17so14604453lft.13 for ; Thu, 12 Aug 2021 10:00:25 -0700 (PDT) X-Received: by 2002:a05:6512:1290:: with SMTP id u16mr3010715lfs.487.1628787137615; Thu, 12 Aug 2021 09:52:17 -0700 (PDT) MIME-Version: 1.0 References: <20210812084348.6521-1-david@redhat.com> <20210812084348.6521-4-david@redhat.com> In-Reply-To: <20210812084348.6521-4-david@redhat.com> From: Linus Torvalds Date: Thu, 12 Aug 2021 06:51:59 -1000 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v1 3/7] kernel/fork: always deny write access to current MM exe_file To: David Hildenbrand Cc: Linux Kernel Mailing List , Andrew Morton , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Alexander Viro , Alexey Dobriyan , Steven Rostedt , Peter Zijlstra , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Petr Mladek , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes , Kees Cook , "Eric W. Biederman" , Greg Ungerer , Geert Uytterhoeven , Mike Rapoport , Vlastimil Babka , Vincenzo Frascino , Chinwen Chang , Michel Lespinasse , Catalin Marinas , "Matthew Wilcox (Oracle)" , Huang Ying , Jann Horn , Feng Tang , Kevin Brodsky , Michael Ellerman , Shawn Anastasio , Steven Price , Nicholas Piggin , Christian Brauner , Jens Axboe , Gabriel Krisman Bertazi , Peter Xu , Suren Baghdasaryan , Shakeel Butt , Marco Elver , Daniel Jordan , Nicolas Viennot , Thomas Cedeno , Collin Fijalkovich , Michal Hocko , Miklos Szeredi , Chengguang Xu , =?UTF-8?Q?Christian_K=C3=B6nig?= , linux-unionfs@vger.kernel.org, Linux API , "the arch/x86 maintainers" , linux-fsdevel , Linux-MM Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 11, 2021 at 10:45 PM David Hildenbrand wrote: > > /* No ordering required: file already has been exposed. */ > - RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); > + exe_file = get_mm_exe_file(oldmm); > + RCU_INIT_POINTER(mm->exe_file, exe_file); > + if (exe_file) > + deny_write_access(exe_file); Can we make a helper function for this, since it's done in two different places? > - if (new_exe_file) > + if (new_exe_file) { > get_file(new_exe_file); > + /* > + * exec code is required to deny_write_access() successfully, > + * so this cannot fail > + */ > + deny_write_access(new_exe_file); > + } > rcu_assign_pointer(mm->exe_file, new_exe_file); And the above looks positively wrong. The comment is also nonsensical, in that it basically says "we thought this cannot fail, so we'll just rely on it". If it truly cannot fail, then the comment should give the reason, not the "we depend on this not failing". And honestly, I don't see why it couldn't fail. And if it *does* fail, we cannot then RCU-assign the exe_file pointer with this, because you'll get a counter imbalance when you do the allow_write_access() later. Anyway, do_open_execat() does do deny_write_access() with proper error checking. I think that is the existing reference that you depend on - so that it doesn't fail. So the comment could possibly say that the only caller has done this, but can we not just use the reference deny_write_access() directly, and not do a new one here? IOW, maybe there's an extraneous 'allow_write_access()' somewhere that should be dropped when we do the whole binprm dance in execve()? Linus