From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2497CC4320A for ; Thu, 12 Aug 2021 12:39:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E799B61019 for ; Thu, 12 Aug 2021 12:39:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237067AbhHLMje (ORCPT ); Thu, 12 Aug 2021 08:39:34 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:33482 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233956AbhHLMja (ORCPT ); Thu, 12 Aug 2021 08:39:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628771945; 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=sbqGKseWRRYhbY2LGrYsEOQPjgQ6ehV+W5zF3jgNGfg=; b=cyMImLTLaArjT4LGx0T0mtmk4mEV59XFkyZlVepb5PTUKQ5Gz+CPnKK/jbEVGrNMNCULJi mwb+8sUz1bUgBg7zwlaQ3Crzv+m+rI+x5UMgxGlByD8YL5/sCeWQh9mN9dNKdTtavREJvA 0rfno5rxB+EptaNxrLi0ttX+qKyisfY= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-377-CJ4tD6-mNEmRviaNOkFuNQ-1; Thu, 12 Aug 2021 08:39:03 -0400 X-MC-Unique: CJ4tD6-mNEmRviaNOkFuNQ-1 Received: by mail-wr1-f69.google.com with SMTP id y12-20020adfee0c0000b0290154e82fef34so1793037wrn.6 for ; Thu, 12 Aug 2021 05:39:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=sbqGKseWRRYhbY2LGrYsEOQPjgQ6ehV+W5zF3jgNGfg=; b=h7t/PRMg7fci2QYp1sIuVLJFK1dHgIAxDkuQaOkBRKvqBo3x329n+irMwAOjH4JIUT ByQUCAnQHkF8AeGq2UqzI/IdJdh4Z39RwtKABsAuHbBJLF9Vk+Vbz/lDO1cQt6XyrL8A +Ntbyrka6wzAtTTS6AVZAK2vH2v6ii49rGcdc/kahLlQgmBQnH4LVnEPd8N8mIBxe+Ss GLGBJ8aLFH7RKtcVli88m7rf0KtaA1WhMMRQigUhltFAXH6ulsypewQV/2rUU7AuXxtn jxlcgxCe7Cto7GWQn6yCLs/muJdv06PSt1YvfUCCh1GTS9buxgcBdZPBjtmFqfAlIQ29 ke4g== X-Gm-Message-State: AOAM532UsTC83TS/5A/fFdb0XZmxoDUWyk0ciQl7YJR9aEd3dfHjM/e1 +10gv3U5V7AYSLW0ckEBp9Czl6Xh9jyjuVJY2Ecv2oFKOKf4KfIzS4RudmE1JOo+rCuVcmwRwjr m2l2wMsYWdildNzBnqKWqaF+szw== X-Received: by 2002:adf:db83:: with SMTP id u3mr3880794wri.363.1628771942743; Thu, 12 Aug 2021 05:39:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxDcYmjDSU043EHzDh1omoow7kdOXdSFpNIKh9fkmzaYfVhQ8MrQo4AHUXle/AZphgQiUXdsA== X-Received: by 2002:adf:db83:: with SMTP id u3mr3880754wri.363.1628771942458; Thu, 12 Aug 2021 05:39:02 -0700 (PDT) Received: from [192.168.3.132] (p4ff23d8b.dip0.t-ipconnect.de. [79.242.61.139]) by smtp.gmail.com with ESMTPSA id d18sm2297940wrb.16.2021.08.12.05.38.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 Aug 2021 05:39:01 -0700 (PDT) Subject: Re: [PATCH v1 3/7] kernel/fork: always deny write access to current MM exe_file To: Christian Brauner Cc: linux-kernel@vger.kernel.org, Linus Torvalds , 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 , 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@vger.kernel.org, x86@kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Andrei Vagin References: <20210812084348.6521-1-david@redhat.com> <20210812084348.6521-4-david@redhat.com> <20210812100544.uhsfp75b4jcrv3qx@wittgenstein> <1b6d27cf-2238-0c1c-c563-b38728fbabc2@redhat.com> <20210812123239.trksnm57owzwzokj@wittgenstein> From: David Hildenbrand Organization: Red Hat Message-ID: <59b6be94-c7d0-0535-1348-4e9c7f188d20@redhat.com> Date: Thu, 12 Aug 2021 14:38:59 +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: <20210812123239.trksnm57owzwzokj@wittgenstein> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-unionfs@vger.kernel.org On 12.08.21 14:32, Christian Brauner wrote: > On Thu, Aug 12, 2021 at 12:13:44PM +0200, David Hildenbrand wrote: >> On 12.08.21 12:05, Christian Brauner wrote: >>> [+Cc Andrei] >>> >>> On Thu, Aug 12, 2021 at 10:43:44AM +0200, David Hildenbrand wrote: >>>> We want to remove VM_DENYWRITE only currently only used when mapping the >>>> executable during exec. During exec, we already deny_write_access() the >>>> executable, however, after exec completes the VMAs mapped >>>> with VM_DENYWRITE effectively keeps write access denied via >>>> deny_write_access(). >>>> >>>> Let's deny write access when setting the MM exe_file. With this change, we >>>> can remove VM_DENYWRITE for mapping executables. >>>> >>>> This represents a minor user space visible change: >>>> sys_prctl(PR_SET_MM_EXE_FILE) can now fail if the file is already >>>> opened writable. Also, after sys_prctl(PR_SET_MM_EXE_FILE), the file >>> >>> Just for completeness, this also affects PR_SET_MM_MAP when exe_fd is >>> set. >> >> Correct. >> >>> >>>> cannot be opened writable. Note that we can already fail with -EACCES if >>>> the file doesn't have execute permissions. >>>> >>>> Signed-off-by: David Hildenbrand >>>> --- >>> >>> The biggest user I know and that I'm involved in is CRIU which heavily >>> uses PR_SET_MM_MAP (with a fallback to PR_SET_MM_EXE_FILE on older >>> kernels) during restore. Afair, criu opens the exe fd as an O_PATH >>> during dump and thus will use the same flag during restore when >>> opening it. So that should be fine. >> >> Yes. >> >>> >>> However, if I understand the consequences of this change correctly, a >>> problem could be restoring workloads that hold a writable fd open to >>> their exe file at dump time which would mean that during restore that fd >>> would be reopened writable causing CRIU to fail when setting the exe >>> file for the task to be restored. >> >> If it's their exe file, then the existing VM_DENYWRITE handling would have >> forbidden these workloads to open the fd of their exe file writable, right? > > Yes. > >> At least before doing any PR_SET_MM_MAP/PR_SET_MM_EXE_FILE. But that should >> rule out quite a lot of cases we might be worried about, right? > > Yes, it rules out the most obvious cases. The problem is really just > that we don't know how common weirder cases are. But that doesn't mean > we shouldn't try and risk it. This is a nice cleanup and playing > /proc/self/exe games isn't super common. > Right, and having the file your executing opened writable isn't something very common as well. If we really run into problems, we could not protect the new file when issuing PR_SET_MM_MAP/PR_SET_MM_EXE_FILE. But I'd like to avoid that, if possible, because it feels like working around something that never should have worked that way and is quite inconsistent. -- Thanks, David / dhildenb