x86/devmem: Drop /dev/mem access for confidential guests
Subsystems (3)
PATCH v2 1/1 x86/devmem: Drop /dev/mem access for confidential guests
From: Dan Williams <[email protected]>
Nikolay reports that accessing BIOS data (first 1MB of the physical address space) via /dev/mem results in a "crash" / terminated VM (unhandled SEPT violation). See report [1] for details.
The cause is ioremap() (via xlate_dev_mem_ptr()) establishes an unencrypted mapping where the kernel had established an encrypted mapping previously. The CPU enforces mapping consistency with a fault upon detecting a mismatch. A similar situation arises with devmem access to "unaccepted" confidential memory. In summary, it is fraught to allow uncoordinated userspace mapping of confidential memory.
While there is an existing mitigation to simulate and redirect access to the BIOS data area with STRICT_DEVMEM=y, it is insufficient. Specifically, STRICT_DEVMEM=y traps read(2) access to the BIOS data area, and returns a zeroed buffer. However, it turns out the kernel fails to enforce the same via mmap(2), and a direct mapping is established. This is a hole, and unfortunately userspace has learned to exploit it [2].
This means the kernel either needs: a mechanism to ensure consistent plus accepted "encrypted" mappings of this /dev/mem mmap() hole, close the hole by mapping the zero page in the mmap(2) path, block only BIOS data access and let typical STRICT_DEVMEM protect the rest, or disable /dev/mem altogether.
The simplest option for now is to disallow access to /dev/mem via devmem_is_allowed. Require confidential guest userspace to jettison legacy dependencies on /dev/mem similar to how other legacy mechanisms are jettisoned for confidential operation. Recall that modern methods for BIOS data access are available like /sys/firmware/dmi/tables.
Now, this begs the question what to do with PCI sysfs which allows userspace mappings of confidential MMIO with similar mapping consistency and acceptance expectations? Here, the existing mitigation of IO_STRICT_DEVMEM is likely sufficient. The kernel is expected to use request_mem_region() when toggling the state of MMIO. With IO_STRICT_DEVMEM that enforces kernel-exclusive access until release_mem_region(), i.e. mapping conflicts are prevented.
---
V2:
* Move the check to x86's devmem_is_allowed implementation to avoid touching generic code
* Very slightly reword the 1st sentence of the 5th paragraph to not mention "lockdown",
arch/x86/Kconfig | 4 ++++ arch/x86/mm/init.c | 10 ++++++++++ 2 files changed, 14 insertions(+)
arch/x86/Kconfig +4 -0
@@ -901,6 +901,8 @@ config INTEL_TDX_GUEST depends on X86_X2APIC depends on EFI_STUB depends on PARAVIRT + select STRICT_DEVMEM + select IO_STRICT_DEVMEM select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select X86_MCE
@@ -1490,6 +1492,8 @@ config AMD_MEM_ENCRYPT bool "AMD Secure Memory Encryption (SME) support" depends on X86_64 && CPU_SUP_AMD depends on EFI_STUB + select STRICT_DEVMEM + select IO_STRICT_DEVMEM select DMA_COHERENT_POOL select ARCH_USE_MEMREMAP_PROT select INSTRUCTION_DECODER
arch/x86/mm/init.c +10 -0
@@ -8,6 +8,7 @@#include <linux/kmemleak.h> #include <linux/sched/task.h> #include <linux/execmem.h> +#include <linux/cc_platform.h> #include <asm/set_memory.h> #include <asm/cpu_device_id.h>
@@ -866,6 +867,15 @@ void __init poking_init(void)*/ int devmem_is_allowed (unsigned long pagenr) { +/* +* Enforce encrypted mapping consistency and avoid unaccepted +* memory conflicts, "lockdown" /dev/mem for confidential +* guests. +*/ +if (IS_ENABLED (CONFIG_STRICT_DEVMEM ) && +cc_platform_has (CC_ATTR_GUEST_MEM_ENCRYPT )) +return 0 ; +if (region_intersects (PFN_PHYS (pagenr),PAGE_SIZE ,IORESOURCE_SYSTEM_RAM ,IORES_DESC_NONE ) !=REGION_DISJOINT ) {
--
2.56.0
Quoted 15 lines · Nikolay Borisov
arch/x86/Kconfig +2
-0@@ -901,6 +901,8 @@ config INTEL_TDX_GUEST depends on X86_X2APIC depends on EFI_STUB depends on PARAVIRT + select STRICT_DEVMEM + select IO_STRICT_DEVMEM select ARCH_HAS_CC_PLATFORM select X86_MEM_ENCRYPT select X86_MCE
arch/x86/Kconfig config AMD_MEM_ENCRYPT · line 1499
Quoted 8 lines
arch/x86/Kconfig +2
-0@@ -1490,6 +1492,8 @@ config AMD_MEM_ENCRYPT bool "AMD Secure Memory Encryption (SME) support" depends on X86_64 && CPU_SUP_AMD depends on EFI_STUB + select STRICT_DEVMEM + select IO_STRICT_DEVMEM select DMA_COHERENT_POOL select ARCH_USE_MEMREMAP_PROT select INSTRUCTION_DECODER
Sashiko had a comment on the previous posting about this, which I agree with:
http://lore.kernel.org/r/[email protected]
With the selects above, it will not be possible to disable CONFIG_DEVMEM completely if also selecting AMD_MEM_ENCRYPT/INTEL_TDX_GUEST.
In addition, I don't see the need for IO_STRICT_DEVMEM, at least in terms of the issue being addressed and the fix below in devmem_is_allowed(). Dan's commit message refers to confidential MMIO, and I think that series can add the select if necessary. Note that adding this select will force-enable IO_STRICT_DEVMEM on baremetal as well.
So, a simple 'select STRICT_DEVMEM if DEVMEM' instead?
- Naveen
Quoted 15 lines · Nikolay Borisov
arch/x86/mm/init.c +1
-0@@ -8,6 +8,7 @@#include <linux/kmemleak.h> #include <linux/sched/task.h> #include <linux/execmem.h> +#include <linux/cc_platform.h> #include <asm/set_memory.h> #include <asm/cpu_device_id.h> @@ -866,6 +867,15 @@ void __init poking_init(void)*/ int devmem_is_allowed (unsigned long pagenr)
arch/x86/mm/init.c void __init poking_init(void) · line 878
Quoted 10 lines
arch/x86/mm/init.c +9
-0{ +/* +* Enforce encrypted mapping consistency and avoid unaccepted +* memory conflicts, "lockdown" /dev/mem for confidential +* guests. +*/ +if (IS_ENABLED (CONFIG_STRICT_DEVMEM ) && +cc_platform_has (CC_ATTR_GUEST_MEM_ENCRYPT )) +return 0 ; +
You can drop IS_ENABLED() check there -- devmem_is_allowed() is only called if STRICT_DEVMEM is enabled.
- Naveen