All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: Dan Williams <dan.j.williams@intel.com>
Cc: dave.hansen@linux.intel.com,
	Nikolay Borisov <nik.borisov@suse.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/3] devmem: Block mmap access when read/write access is restricted
Date: Thu, 10 Apr 2025 19:32:22 -0700	[thread overview]
Message-ID: <202504101926.0F8FB73@keescook> (raw)
In-Reply-To: <174433455090.924142.10859114556652046264.stgit@dwillia2-xfh.jf.intel.com>

On Thu, Apr 10, 2025 at 06:22:30PM -0700, Dan Williams wrote:
> Back in 2022 Kees noted that he is able to mmap System RAM below 1MB
> even with CONFIG_STRICT_DEVMEM=y [1]. That is allowed for x86 legacy
> compatibility reasons for userspace that wants to read BIOS data
> resident at that address. However, the expectation is that when
> devmem_is_allowed() returns 2 that the access is redirected to return
> zeroes.
> 
> That happens for the read()/write() case, but by code inspection for
> mmap(), there is no restriction.
> 
> Now, the confidential x86 VM (CVM) use case wants to depend on
> "devmem_is_allowed() == 2" guaranteeing that no mapping to potentially
> encrypted memory is established [2]. The options to enable that are
> teach mmap_mem() to meet the "zeroed buffer" implication of
> devmem_is_allowed() returning "2", or return -EPERM for that case.
> 
> Return -EPERM on the hope that userspace does not actually depend on the
> legacy behavior of being able to reliably map the first 1MB of memory on
> x86. I.e. that all legacy cases are using read()/write() to safely read
> zeroes. If that turns out not to be true then either a "map zeroes"
> scheme can be added, or the CVM case can return 3 from
> devmem_is_allowed() to hide the CVM restriction from legacy
> environments.
> 
> Link: http://lore.kernel.org/CAPcyv4iVt=peUAk1qx_EfKn7aGJM=XwRUpJftBhkUgQEti2bJA@mail.gmail.com [1]
> Link: http://lore.kernel.org/fd683daa-d953-48ca-8c5d-6f4688ad442c@intel.com [2]
> Suggested-by: Nikolay Borisov <nik.borisov@suse.com>
> Cc: Kees Cook <keescook@chromium.org>
> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
> ---
>  include/linux/io.h |    7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/io.h b/include/linux/io.h
> index 0642c7ee41db..564934f7e70d 100644
> --- a/include/linux/io.h
> +++ b/include/linux/io.h
> @@ -191,7 +191,12 @@ static inline int range_is_allowed(unsigned long pfn, unsigned long size)
>  	u64 cursor = from;
>  
>  	while (cursor < to) {
> -		if (!devmem_is_allowed(pfn))
> +		/*
> +		 * Any restricted access is treated as "no access", i.e.
> +		 * handle devmem_is_allowed() returning "2" to indicate
> +		 * restricted access.
> +		 */
> +		if (devmem_is_allowed(pfn) != 1)
>  			return 0;
>  		cursor += PAGE_SIZE;
>  		pfn++;

Looking through the 16 page of Debian Code Search results for
`open("/dev/mem")`, I find a LOT of mmap() use. Some random examples:

https://sources.debian.org/src/i810switch/0.6.5-7.1/i810switch.c/?hl=413#L402

https://sources.debian.org/src/radeontop/1.4-2/detect.c/?hl=91#L88

https://sources.debian.org/src/libdebian-installer/0.125/src/system/subarch-x86-linux.c/?hl=113#L93
Which includes this gem of a comment, implying that it uses mmap
_specifically to bypass the devmem restrictions_:
	/* Please note that we don't use mmap() for performance reasons here,
	 * but to workaround problems many people encountered when trying
	 * to read from /dev/mem using regular read() calls.
	 */

I don't think we can just fail the mmap. :(

-- 
Kees Cook

  reply	other threads:[~2025-04-11  2:32 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-11  1:22 [PATCH v2 0/3] Restrict devmem for confidential VMs Dan Williams
2025-04-11  1:22 ` [PATCH v2 1/3] x86/devmem: Remove duplicate range_is_allowed() definition Dan Williams
2025-04-14 18:17   ` Naveen N Rao
2025-04-16 21:25     ` Dan Williams
2025-04-17  7:28       ` Naveen N Rao
2025-04-17 18:27         ` Dan Williams
2025-04-19  9:09           ` Naveen N Rao
2025-04-11  1:22 ` [PATCH v2 2/3] devmem: Block mmap access when read/write access is restricted Dan Williams
2025-04-11  2:32   ` Kees Cook [this message]
2025-04-11  4:59     ` Dan Williams
2025-04-11 15:38       ` Dave Hansen
2025-04-11 21:48         ` Dan Williams
2025-04-11  1:22 ` [PATCH v2 3/3] x86/devmem: Restrict /dev/mem access for potentially unaccepted memory by default Dan Williams
2025-04-14 18:22   ` Naveen N Rao
2025-04-16 21:30     ` Dan Williams
2025-04-14 10:56 ` [PATCH v2 0/3] Restrict devmem for confidential VMs Nikolay Borisov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202504101926.0F8FB73@keescook \
    --to=kees@kernel.org \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nik.borisov@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.