Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp705956pxt; Thu, 12 Aug 2021 07:55:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJzf1ANo/eCwfPwsG7ZmnnnFKH7Z1riMEWpWcego56f3KHNDIYU3B6TP21hoPbwI1rp+1Z X-Received: by 2002:a17:906:8a98:: with SMTP id mu24mr4035214ejc.404.1628780118984; Thu, 12 Aug 2021 07:55:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628780118; cv=none; d=google.com; s=arc-20160816; b=jbW9zgYiqG3Ly/JvqQ1K3rD/zkPJ8TNkXtJAHoSDPnAnaZ7e9MnH00N2PcI+jQLyjd lxEKGUKzSw2sJ/8vzX6G01wGlEQMA0WNAJ82aG59/BopdYVBPGV+n+LyMZHL1Ca6wS6q yU3PaVfTtsmcdLT0E2TgXBDRmczgiG5O9Svopmcjr6nnWqAImunzF1wMquo1/W1iHtH8 eblC12wg1zwgQLzoGFm9g8/46RJL4+RZvjRT9UvL00dke1Wl/RhO0VopFQcB//hIYzBg 9bTxTt24b1oVLRB5j6W2qiT3AK9yfw/6wUGWqCINz3OQc06HqKArC9ySrD5+00uTEbrq zgew== 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:organization :from:references:cc:to:subject:dkim-signature; bh=sbqGKseWRRYhbY2LGrYsEOQPjgQ6ehV+W5zF3jgNGfg=; b=ok4YQL9aG4Rnwn38N0OHRpPrwt1y3C6Ff2x+uo/qamVp8Ya4IEY55VqklGfLdr09BB PHEX3K801EMOVNkp9TY87jaYKC8g3iGJFCzNKyIQ3lHo5sWTmR4ExNQdvUsj2tR89bIk UyQs6zYWN78YZ/r2p7fgA6jIWdUxzZNLKvv+cXB19w3FF7f/M84NS0M1DicPrtBeH1F0 8E9RLIhP8vgKwQe7VWBEvN2AXqTP7IBbPvqpIpOIbd1rvWQEgU4s6y1vTmZqzRMqZYhS sFwJ60T3J+x10OpaS0WBlSRqFC0U0tsrf48QAoS2eCjVU397220B1bUmgerXD65Yvtl9 suVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cyMImLTL; 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 gh24si2707782ejb.197.2021.08.12.07.54.53; Thu, 12 Aug 2021 07:55:18 -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=cyMImLTL; 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 S233956AbhHLMje (ORCPT + 99 others); Thu, 12 Aug 2021 08:39:34 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:43704 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236323AbhHLMja (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-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-20-R3j17TpIPfCilmewsBh9pw-1; Thu, 12 Aug 2021 08:39:04 -0400 X-MC-Unique: R3j17TpIPfCilmewsBh9pw-1 Received: by mail-wm1-f70.google.com with SMTP id c2-20020a7bc8420000b0290238db573ab7so3310597wml.5 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=r/Pxrec1VTqMeK2atHGswB+U6rIRg/wu0JD9PSCx66FHJfjS9c7K/Lg+hG1teyL7I6 17ocQA+KfRiVjC30gnMc9jcV9ZTU7TiSbCDUU2gpizaZqzoFcqtCdgfLOR48YUguGGDw TNXUzff1rE3TeX1g1ajuMv7CxZtgQN0cbltdoxEVVelVWWkA7GZS5+3vH+Ej76rywU/z hJCrBIVh0PkEm0fbwFL+K4HFCMAbEN4xrCAkEAzqPB3sDaN/ctxBIxEBqtG9BjozayLm uljTo+hZRXQPR15ZJShMZ+HXkiHCvIYAIZekEVlaDkNtHkCCCtd4TQe8STwSjomKyNQu NvXg== X-Gm-Message-State: AOAM532j6517hObv16QVq38BbAYteMaOPTXUBBD6ZEToSR55pDfV9X36 HuwU1akjnkHdVEC6RQcB1MuolCt/POuGDgyF6Sl5V9tK+AjE++paUIcVLX6Hht3VHUx0wt+81nu RyPOwGz2zGghS8BrXkhFBhdXQ X-Received: by 2002:adf:db83:: with SMTP id u3mr3880791wri.363.1628771942743; Thu, 12 Aug 2021 05:39:02 -0700 (PDT) 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-kernel@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