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=-19.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham 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 2DC0BC433EF for ; Tue, 14 Sep 2021 11:57:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C695B610D1 for ; Tue, 14 Sep 2021 11:57:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C695B610D1 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 53C0E6B006C; Tue, 14 Sep 2021 07:57:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E93D6B0071; Tue, 14 Sep 2021 07:57:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3895C900002; Tue, 14 Sep 2021 07:57:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0191.hostedemail.com [216.40.44.191]) by kanga.kvack.org (Postfix) with ESMTP id 25AB36B006C for ; Tue, 14 Sep 2021 07:57:31 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id DBA65181200F7 for ; Tue, 14 Sep 2021 11:57:30 +0000 (UTC) X-FDA: 78586029060.12.D818A20 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf12.hostedemail.com (Postfix) with ESMTP id 7A74210000A3 for ; Tue, 14 Sep 2021 11:57:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631620649; 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=kjBNsUJZhb+CoTbkZeanK9icxsVOilejMTC0HAi65SM=; b=in4qvABgwsEWaCZieQsxIN72obCeVdZfwb3lyUThFiobU3nwFsvZwjz6BxZ5cNhopbMzbU rRlYMAO3sadBibwjY/JvgYdiKg9bxo2RtlhkV2Z32P2pj3N8AgeSVlUSieifSwQXB5ZTFt KVw6vXKpQtdVFTWhNM1pdp7F+oRu68k= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-377-AmI_QlSWMKKpQT4Avf3CKQ-1; Tue, 14 Sep 2021 07:57:28 -0400 X-MC-Unique: AmI_QlSWMKKpQT4Avf3CKQ-1 Received: by mail-wm1-f69.google.com with SMTP id b139-20020a1c8091000000b002fb33c467c8so6349120wmd.5 for ; Tue, 14 Sep 2021 04:57:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=kjBNsUJZhb+CoTbkZeanK9icxsVOilejMTC0HAi65SM=; b=2qzI9JC/AqFhK0gVY+hCKGbkRdidHUQRCBJu8qn/sIMGmoCL377S7kFsNnJItk5Qvm 4NZM5LXX1paMa/2Qjp35BENEhakqO5Htc0MwLz3CubVPTZ1Y/wtbwYLszTGnSB1uxwRj OlNdxW76AmD8UEHbBeHGP5Ze3UVOZmZAfPpW3E9hIxAYTYYlv7dFzxA5zGs0lR+zbrsj A/Yn/NPW8zrP3/a2P5nvxZc1TI9DeUSQikUEkZg7eXgVN44VLBuweJO9SbiqhyCn3EDS xspn4UZfp69F5yCgLtNIhl/ZAAI5vSHQ9Kl4ZewoydlCA4trjZ1mKupeQbKd+vaQpv6c E63A== X-Gm-Message-State: AOAM533IKZAi3RJ/tfaqd/SXrqRjK0RBdD1G2vovlauHp7NgcMVFZwbf YqWoLkzgbOkhS8MKNTOEYFTQtc2I7IrY6TGreGYM4PLe/KCQYRLCceGNn5/f3o0rVm+WcpTTON7 WoUGAc9KGlOBCeXI68Z0aQFlvJlA7lyk7/DHufn5/kzX6zHJqGLcZs1S47GU= X-Received: by 2002:adf:b785:: with SMTP id s5mr18234022wre.30.1631620647126; Tue, 14 Sep 2021 04:57:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+Cp51cW71Hx/HXa3ft0tf4MRVU/E3ocxIO1LloVlrNFZuxeHhOo+RIIhwRu78LdbPvR4dbQ== X-Received: by 2002:adf:b785:: with SMTP id s5mr18233980wre.30.1631620646792; Tue, 14 Sep 2021 04:57:26 -0700 (PDT) Received: from [192.168.3.132] (p5b0c6041.dip0.t-ipconnect.de. [91.12.96.65]) by smtp.gmail.com with ESMTPSA id i5sm10058563wrc.86.2021.09.14.04.57.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Sep 2021 04:57:26 -0700 (PDT) Subject: Re: [PATCH v4 3/3] virtio-mem: disallow mapping virtio-mem memory via /dev/mem To: "Michael S. Tsirkin" Cc: linux-kernel@vger.kernel.org, Arnd Bergmann , Greg Kroah-Hartman , Jason Wang , "Rafael J. Wysocki" , Andrew Morton , Dan Williams , Hanjun Guo , Andy Shevchenko , virtualization@lists.linux-foundation.org, linux-mm@kvack.org References: <20210902160919.25683-1-david@redhat.com> <20210902160919.25683-4-david@redhat.com> <20210903025630-mutt-send-email-mst@kernel.org> <20210914075046-mutt-send-email-mst@kernel.org> From: David Hildenbrand Organization: Red Hat Message-ID: Date: Tue, 14 Sep 2021 13:57:25 +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: <20210914075046-mutt-send-email-mst@kernel.org> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 7A74210000A3 X-Stat-Signature: cm3fpfs6xhimd3ud9kes7f6cretytrdk Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=in4qvABg; spf=none (imf12.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1631620650-937163 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 14.09.21 13:52, Michael S. Tsirkin wrote: > On Tue, Sep 14, 2021 at 11:45:25AM +0200, David Hildenbrand wrote: >> On 03.09.21 09:02, Michael S. Tsirkin wrote: >>> On Thu, Sep 02, 2021 at 06:09:19PM +0200, David Hildenbrand wrote: >>>> We don't want user space to be able to map virtio-mem device memory >>>> directly (e.g., via /dev/mem) in order to have guarantees that in a sane >>>> setup we'll never accidentially access unplugged memory within the >>>> device-managed region of a virtio-mem device, just as required by the >>>> virtio-spec. >>>> >>>> As soon as the virtio-mem driver is loaded, the device region is visible >>>> in /proc/iomem via the parent device region. From that point on user space >>>> is aware of the device region and we want to disallow mapping anything >>>> inside that region (where we will dynamically (un)plug memory) until >>>> the driver has been unloaded cleanly and e.g., another driver might take >>>> over. >>>> >>>> By creating our parent IORESOURCE_SYSTEM_RAM resource with >>>> IORESOURCE_EXCLUSIVE, we will disallow any /dev/mem access to our >>>> device region until the driver was unloaded cleanly and removed the >>>> parent region. This will work even though only some memory blocks are >>>> actually currently added to Linux and appear as busy in the resource tree. >>>> >>>> So access to the region from user space is only possible >>>> a) if we don't load the virtio-mem driver. >>>> b) after unloading the virtio-mem driver cleanly. >>>> >>>> Don't build virtio-mem if access to /dev/mem cannot be restricticted -- >>>> if we have CONFIG_DEVMEM=y but CONFIG_STRICT_DEVMEM is not set. >>>> >>>> Reviewed-by: Dan Williams >>>> Signed-off-by: David Hildenbrand >>> >>> >>>> --- >>>> drivers/virtio/Kconfig | 1 + >>>> drivers/virtio/virtio_mem.c | 4 +++- >>>> 2 files changed, 4 insertions(+), 1 deletion(-) >>>> >>>> diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig >>>> index ce1b3f6ec325..ff80cd03f1d1 100644 >>>> --- a/drivers/virtio/Kconfig >>>> +++ b/drivers/virtio/Kconfig >>>> @@ -101,6 +101,7 @@ config VIRTIO_MEM >>>> depends on MEMORY_HOTPLUG_SPARSE >>>> depends on MEMORY_HOTREMOVE >>>> depends on CONTIG_ALLOC >>>> + depends on !DEVMEM || STRICT_DEVMEM >>>> help >>>> This driver provides access to virtio-mem paravirtualized memory >>>> devices, allowing to hotplug and hotunplug memory. >>> >>> It would be nicer if there was a symbol in the MEMORY_ namespace >>> we can depend on exported by mm and depending on !DEVMEM || >>> STRICT_DEVMEM. >>> >>> E.g. >>> >>> config MEMORY_EXCLUSIVE >>> def_bool y >>> depends on !DEVMEM || STRICT_DEVMEM >>> >>> and then in virtio >>> depends on MEMORY_EXCLUSIVE >>> >>> >> >> Yes, but I'm not able to come up with an expressive name. MEMORY_EXCLUSIVE >> can be highly misleading ... > > > I mean ... it enables IORESOURCE_EXCLUSIVE ... but ok ... > MEMORY_EXCLUSIVE_KERNEL_MAP ? It enables IORESOURCE_EXCLUSIVE for IORESOURCE_SYSTEM_RAM ... and notably not for IORESOURCE_MEM. MEMORY_EXCLUSIVE_SYSTEM_RAM ? > >> >>> the virtio change itself is ok though: >>> >>> Acked-by: Michael S. Tsirkin >> >> Thanks! >> >> >> -- >> Thanks, >> >> David / dhildenb > -- Thanks, David / dhildenb