Skip to contentSkip to conversation

x86/devmem: Drop /dev/mem access for confidential guests

PATCH RESEND In review Nikolay Borisov 5 participants 8 messages Nov 24 – Nov 25

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 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 arrange for /dev/mem to always behave as if lockdown is enabled for confidential guests. 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.

---

Resending this with the hope of either getting it accepted or arriving at a consensus what exactly is missing so that I can take it from there and eventually get something merged. As previously mentioned this results in real problems for confidential guests both on TDX as well as SEV-SNP.

 arch/x86/Kconfig |  4 ++++
 mm/char-mem.c    | 10 ++++++++++
 2 files changed, 14 insertions(+)
arch/x86/Kconfig +4 -0
index 3159852fd6b3..1490e0019c71 100644
@@ -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
mm/char-mem.c +10 -0
index c5761ddedbd7..5d383befd87d 100644
@@ -30,6 +30,7 @@
 #include <linux/uio.h>
 #include <linux/uaccess.h>
 #include <linux/security.h>
+#include <linux/cc_platform.h>
 
 #include "internal.h"
 
@@ -609,6 +610,15 @@ static int open_port(struct inode *inode, struct file *filp)
 	if (rc)
 		return rc;
 
+	/*
+	 * 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 -EPERM;
+
 	if (iminor(inode) != DEVMEM_MINOR)
 		return 0;
 

--
2.56.0

Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Nikolay Borisov

From: Dan Williams <[email protected]>

Resending this with the hope of either getting it accepted or arriving at a consensus
what exactly is missing so that I can take it from there and eventually get something
merged. As previously mentioned this results in real problems for confidential
guests both on TDX as well as SEV-SNP.

I do think we should go further than this, but the patch solves a real problem and it doesn't stop us from doing more later, so

#2 lore raw
In reply to Nikolay Borisov
Quoted 8 lines · Nikolay Borisov

Resending this with the hope of either getting it accepted or arriving at a consensus what exactly is missing so that I can take it from there and eventually get something merged. As previously mentioned this results in real problems for confidential guests both on TDX as well as SEV-SNP.>

 arch/x86/Kconfig |  4 ++++
 mm/char-mem.c    | 10 ++++++++++
 2 files changed, 14 insertions(+)

I'm still OK with doing this.

But I didn't want to sneak an mm/ change in via the tip tree. Any mm acks or naks on this?

reviewer
#3 lore raw

Holy cow the CC list is stale.
Fixing some up.

Quoted 10 lines · Dave Hansen
Quoted 8 lines · Nikolay Borisov

Resending this with the hope of either getting it accepted or arriving at a consensus what exactly is missing so that I can take it from there and eventually get something merged. As previously mentioned this results in real problems for confidential guests both on TDX as well as SEV-SNP.>

 arch/x86/Kconfig |  4 ++++
 mm/char-mem.c    | 10 ++++++++++
 2 files changed, 14 insertions(+)
Quoted 4 lines

I'm still OK with doing this.

But I didn't want to sneak an mm/ change in via the tip tree. Any mm acks or naks on this?

For what it's worth, I don't love this fix. Yes, it fixes issues in /dev/mem, but it:
1) skips any other potential problem spots (I don't know which)
2) open-codes random CC checks in mm, which is kind-of regrettable

OTOH, it's unclear to me why that CC check is needed. If configs select STRICT_DEVMEM and IO_STRICT_DEVMEM, shouldn't it Just Do The Right thing? in iomem_is_exclusive, etc. If not, could the CC awareness sit there instead?

/me doesn't know much about CC other than *if you touch something you shouldn't, it goes boom*

Signature · 1 line
Pedro
#4 lore raw
Quoted 24 lines · Pedro Falcato

Holy cow the CC list is stale.
Fixing some up.

Quoted 10 lines · Dave Hansen
Quoted 8 lines · Nikolay Borisov

Resending this with the hope of either getting it accepted or arriving at a consensus what exactly is missing so that I can take it from there and eventually get something merged. As previously mentioned this results in real problems for confidential guests both on TDX as well as SEV-SNP.>

 arch/x86/Kconfig |  4 ++++
 mm/char-mem.c    | 10 ++++++++++
 2 files changed, 14 insertions(+)
Quoted 4 lines

I'm still OK with doing this.

But I didn't want to sneak an mm/ change in via the tip tree. Any mm acks or naks on this?

For what it's worth, I don't love this fix. Yes, it fixes issues in /dev/mem, but it:
1) skips any other potential problem spots (I don't know which)
2) open-codes random CC checks in mm, which is kind-of regrettable

Quoted 4 lines

OTOH, it's unclear to me why that CC check is needed. If configs select STRICT_DEVMEM and IO_STRICT_DEVMEM, shouldn't it Just Do The Right thing? in iomem_is_exclusive, etc. If not, could the CC awareness sit there instead?

Yeah, why is anyone letting a CC system access /dev/mem at all? That's ripe for abuse lots of different ways, so I really don't like this change either.

thanks,

greg k-h

#5 lore raw
Quoted 25 lines · Greg Kroah-Hartman
Quoted 24 lines · Pedro Falcato

Holy cow the CC list is stale.
Fixing some up.

Quoted 10 lines · Dave Hansen
Quoted 8 lines · Nikolay Borisov

Resending this with the hope of either getting it accepted or arriving at a consensus what exactly is missing so that I can take it from there and eventually get something merged. As previously mentioned this results in real problems for confidential guests both on TDX as well as SEV-SNP.>

  arch/x86/Kconfig |  4 ++++
  mm/char-mem.c    | 10 ++++++++++
  2 files changed, 14 insertions(+)
Quoted 4 lines

I'm still OK with doing this.

But I didn't want to sneak an mm/ change in via the tip tree. Any mm acks or naks on this?

For what it's worth, I don't love this fix. Yes, it fixes issues in /dev/mem, but it:
1) skips any other potential problem spots (I don't know which)
2) open-codes random CC checks in mm, which is kind-of regrettable

Quoted 8 lines
Quoted 4 lines

OTOH, it's unclear to me why that CC check is needed. If configs select STRICT_DEVMEM and IO_STRICT_DEVMEM, shouldn't it Just Do The Right thing? in iomem_is_exclusive, etc. If not, could the CC awareness sit there instead?

Yeah, why is anyone letting a CC system access /dev/mem at all? That's ripe for abuse lots of different ways, so I really don't like this change either.

You are missing the point, this pertains to /dev/mem in the guest. There are a bunch of libraries (libhd for example) which reads the first meg of memory. This causes the CC guest to shutdown in both AMD-SEVSNP and TDX world.

Quoted 4 lines

thanks,

greg k-h

#6 lore raw
Quoted 33 lines · Nikolay Borisov
Quoted 30 lines · Greg Kroah-Hartman
Quoted 28 lines · Pedro Falcato

Holy cow the CC list is stale.
Fixing some up.

Quoted 10 lines · Dave Hansen
Quoted 8 lines · Nikolay Borisov

Resending this with the hope of either getting it accepted or arriving at a consensus what exactly is missing so that I can take it from there and eventually get something merged. As previously mentioned this results in real problems for confidential guests both on TDX as well as SEV-SNP.>

  arch/x86/Kconfig |  4 ++++
  mm/char-mem.c    | 10 ++++++++++
  2 files changed, 14 insertions(+)
Quoted 4 lines

I'm still OK with doing this.

But I didn't want to sneak an mm/ change in via the tip tree. Any mm acks or naks on this?

For what it's worth, I don't love this fix. Yes, it fixes issues in /dev/mem, but it:
1) skips any other potential problem spots (I don't know which)
2) open-codes random CC checks in mm, which is kind-of regrettable

OTOH, it's unclear to me why that CC check is needed. If configs select STRICT_DEVMEM and IO_STRICT_DEVMEM, shouldn't it Just Do The Right thing? in iomem_is_exclusive, etc. If not, could the CC awareness sit there instead?

Quoted 8 lines
Quoted 3 lines

Yeah, why is anyone letting a CC system access /dev/mem at all? That's ripe for abuse lots of different ways, so I really don't like this change either.

You are missing the point, this pertains to /dev/mem in the guest. There are a bunch of libraries (libhd for example) which reads the first meg of memory. This causes the CC guest to shutdown in both AMD-SEVSNP and TDX world.

Then don't allow those libraries to work in cc guests :)

Again, why would you want anyone to access /dev/mem/ at all in those types of situations?

greg k-h

#7 lore raw
In reply to Pedro Falcato
Quoted 14 lines · Pedro Falcato

Holy cow the CC list is stale.
Fixing some up.

Quoted 10 lines · Dave Hansen
Quoted 8 lines · Nikolay Borisov

Resending this with the hope of either getting it accepted or arriving at a consensus what exactly is missing so that I can take it from there and eventually get something merged. As previously mentioned this results in real problems for confidential guests both on TDX as well as SEV-SNP.>

  arch/x86/Kconfig |  4 ++++
  mm/char-mem.c    | 10 ++++++++++
  2 files changed, 14 insertions(+)
Quoted 8 lines
Quoted 4 lines

I'm still OK with doing this.

But I didn't want to sneak an mm/ change in via the tip tree. Any mm acks or naks on this?

For what it's worth, I don't love this fix. Yes, it fixes issues in /dev/mem, but it:
1) skips any other potential problem spots (I don't know which)

Well that's not saying much. Of course there are thousands of bugs lurking around the kernel and not any one patch fixes them all.

Quoted 6 lines

2) open-codes random CC checks in mm, which is kind-of regrettable

OTOH, it's unclear to me why that CC check is needed. If configs select STRICT_DEVMEM and IO_STRICT_DEVMEM, shouldn't it Just Do The Right thing? in iomem_is_exclusive, etc. If not, could the CC awareness sit there instead?

Well, you are right that if we retain just the select STRICT_DEVMEM/IO_STRICT_DEVMEM it might fix the immediate issue. I don't know why eventually Dan decided to also add this check, I guess it's an instance of "fail fast".

Quoted 4 lines

/me doesn't know much about CC other than *if you touch something you shouldn't, it goes boom*