Received: by 2002:a05:6a10:8a4d:0:0:0:0 with SMTP id dn13csp174419pxb; Thu, 12 Aug 2021 13:37:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwpv6hd426mf84dX4WoeVx9w9O4TySdTL3QoX9YQumwzc4b5SVQWb6W3g1Qiz6klivlLlZA X-Received: by 2002:a92:7d08:: with SMTP id y8mr310776ilc.111.1628800630372; Thu, 12 Aug 2021 13:37:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628800630; cv=none; d=google.com; s=arc-20160816; b=PWncR8tg2xUzj8PTF6IzSTwI8kkn1qx2YEiKWcy4tOJBbDtFNErA17Z1mZUHxnohZO Z8HCuPJKnkypSCiRMfy37oN8V6y35pvhMpKqufIWzrJ+vjleH28Vr9185j4ZHu6SW/3o KBGKgOLJ+ZegEE+EpmfL2OfDbodLz90Lqw9EP737Wb3Xutqi4Bxd40leedfk1cYajwsN t7Zr7zKBgl6a+zH8EehMEmH6FbXrFATckHr2ojmgCU/mXlk3fPR2DsV9MW2PRKytaDW6 jaxALO6yGldQ3vbpsVXpsPShdshaKApMJp++JOO+2t/iVwCznHgjSKqp1VJJpxijf2lK 5FeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject :organization:from:references:cc:to:dkim-signature; bh=tGJibS4yiaglb2J5rU+JWBqNKg1kdcrr0OdS+mMkHrI=; b=A8bG2EQFl3p0kMj61yw2YhIcvQcDmkW127e0IcUdOiHDrxJm9cyAk38Qsa9tUW8ltk KWH6Hz0fLHeDE3aANKIiwz+xMmQ0x8QY97ODfE8fwBcvyZoRYk1ZeItEPEtE9n/EwXsv pv/RWBioHRELh0h2Xwcu4zBe9cFhS9ffC4O+jfI2vRUIbsbqQfltiP+n6KJkzEdZ7LT6 5bUMe5PGOBrf9YVSi496Jd1FDyHjRhf+8k31L8DJgfQ/yRAfIOxTf0AbDLFvVK/cGwfv t1g4TivgZisyZhcs9A7kvbEJZDYgTM4OJrb4j+2cgt92cEI5Lg6SVntBFUegRdXY4lsS /0yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=YsZPejHE; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d20si3870143ioo.61.2021.08.12.13.36.57; Thu, 12 Aug 2021 13:37:10 -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=@redhat.com header.s=mimecast20190719 header.b=YsZPejHE; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234059AbhHLTjB (ORCPT + 99 others); Thu, 12 Aug 2021 15:39:01 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:46018 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232684AbhHLTjA (ORCPT ); Thu, 12 Aug 2021 15:39:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628797113; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=tGJibS4yiaglb2J5rU+JWBqNKg1kdcrr0OdS+mMkHrI=; b=YsZPejHEDNuP59u5DgBvk4kho1KqLMikTMva8fQFqGcUB5Zv/X5np47jKoEAhQC2o1/3fw XN1GGdkS/lNaYiTeY0hEC4I1CJpgS5MlUQCo/bXS7Tot3QgIFzp+/Wbu7B36XlIjkIkYI3 7taUgRmjsZlLW+RzVkCA0VgaR/0r4K4= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-497-4sJWHDKnOEChEuUHJ4k3Bg-1; Thu, 12 Aug 2021 15:38:32 -0400 X-MC-Unique: 4sJWHDKnOEChEuUHJ4k3Bg-1 Received: by mail-wr1-f72.google.com with SMTP id m5-20020a5d6a050000b0290154e83dce73so2142748wru.19 for ; Thu, 12 Aug 2021 12:38:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:references:from:organization:subject :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=tGJibS4yiaglb2J5rU+JWBqNKg1kdcrr0OdS+mMkHrI=; b=VUJyVIKl9ETYk6Y5HIwjbpu25suWeXnXaGaIeXZc3gtF9PTRA1gE+reeKFLNROSgxv 4ltdSUhuqyEjUnjGuzEA7Sqrr1pBnBhfax1cAt7cNvzvp8ndT9S0s0SmFRDAOjeYczpJ 56DQPGutQCWxOomN9uHRouda1aJXnlrIzjjUkDYlAzON06jCA+M/u5YhPdlvE4k2u0fh WYi7NRlksyCC+Lk7hFJAYCPhwAbYvtD7gRRhv0cMM9icJwbdaTjiRAKbR2hNKieV13ka LEPFXn88XUumVUWm7gFou0iV433UAuet6ibjFtVH8A1Himi4yS4VXmSN/gyLyWLwybWk GT/w== X-Gm-Message-State: AOAM530ujc8AKrkLkGUMkdBkEdOFFyQIu9w6kvVkhNOrcZf8dfPB5SG7 6pwBEosxO6gQqufAnNGkLcunjmyilwDp3He9e0gZn3NF6tZSXZ+c81ZB8jrGaJZOC6kbT0AKR3M 3kYkeZAj+EqW/h5T2OWnKyqbs X-Received: by 2002:a5d:45c2:: with SMTP id b2mr5697484wrs.188.1628797111135; Thu, 12 Aug 2021 12:38:31 -0700 (PDT) X-Received: by 2002:a5d:45c2:: with SMTP id b2mr5697441wrs.188.1628797110959; Thu, 12 Aug 2021 12:38:30 -0700 (PDT) Received: from [192.168.3.132] (p4ff23d8b.dip0.t-ipconnect.de. [79.242.61.139]) by smtp.gmail.com with ESMTPSA id i9sm4899610wre.36.2021.08.12.12.38.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 Aug 2021 12:38:29 -0700 (PDT) To: Linus Torvalds 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 References: <20210812084348.6521-1-david@redhat.com> <20210812084348.6521-4-david@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v1 3/7] kernel/fork: always deny write access to current MM exe_file Message-ID: <15628c8a-9c71-5611-2edf-07087ad662b7@redhat.com> Date: Thu, 12 Aug 2021 21:38:26 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 12.08.21 18:51, Linus Torvalds wrote: > 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? Sure, no compelling reason not to (except finding a suitable name, but I'll think about that tomorrow). > >> - 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". Well, it documents the expectation towards the caller, but in a suboptimal way, I agree. > > If it truly cannot fail, then the comment should give the reason, not > the "we depend on this not failing". Right, "We depend on the caller already have done a deny_write_access() successfully first such that this call cannot fail." combined with if (deny_write_access(new_exe_file)) pr_warn("Unexpected failure of deny_write_access() in %s", __func__); suggestions welcome. > > 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. Anyone calling set_mm_exe_file() (-> begin_new_exec()) is expected to successfully triggered a deny_write_access() upfront such that we won't fail at that point. Further, on the dup_mmap() path we are sure the previous oldmm exe_file properly saw a successful deny_write_access() already, because that's now guaranteed for any exe_file. > > 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? I think that might over-complicate the exec code where we would see a allow_write_access() on error paths, but not on success paths. This here looks cleaner to me, agreeing that the comment and the error check has to be improved. We handle all allow_write_access()/deny_write_access() regarding exe_file completely in kernel/fork.c, which is IMHO quite nice. > > IOW, maybe there's an extraneous 'allow_write_access()' somewhere that > should be dropped when we do the whole binprm dance in execve()? fs/exec.c: free_bprm() and exec_binprm() to be precise. Thanks! -- Thanks, David / dhildenb