Skip to contentSkip to conversation

x86/mm: Make memremap(MEMREMAP_WB) map memory as encrypted by default

PATCH Tom Lendacky 3 participants 3 messages Jan 14, 2025 – Jan 15, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw
Quoted 58 lines · Tom Lendacky
Quoted 57 lines · Kirill A. Shutemov
Quoted 55 lines · Tom Lendacky
Quoted 46 lines · Kirill A. Shutemov
Quoted 30 lines · Tom Lendacky
Quoted 29 lines · Kirill A. Shutemov

Currently memremap(MEMREMAP_WB) can produce decrypted/shared mapping:

memremap(MEMREMAP_WB)
arch_memremap_wb()
ioremap_cache()
__ioremap_caller(.encrytped = false)

In such cases, the IORES_MAP_ENCRYPTED flag on the memory will determine if the resulting mapping is encrypted or decrypted.

Creating a decrypted mapping without explicit request from the caller is risky:

- It can inadvertently expose the guest's data and compromise the guest.

- Accessing private memory via shared/decrypted mapping on TDX will either trigger implicit conversion to shared or #VE (depending on VMM implementation).

Implicit conversion is destructive: subsequent access to the same memory via private mapping will trigger a hard-to-debug #VE crash.

The kernel already provides a way to request decrypted mapping explicitly via the MEMREMAP_DEC flag.

Modify memremap(MEMREMAP_WB) to produce encrypted/private mapping by default unless MEMREMAP_DEC is specified.

Quoted 7 lines
Quoted 1 line

Fix the crash due to #VE on kexec in TDX guests if CONFIG_EISA is enabled.

This patch causes my bare-metal system to crash during boot when using mem_encrypt=on:

[    2.392934] efi: memattr: Entry type should be RuntimeServiceCode/Data
[    2.393731] efi: memattr: ! 0x214c42f01f1162a-0xee70ac7bd1a9c629 [type=2028324321|attr=0x6590648fa4209879]

Could you try if this helps?

drivers/firmware/efi/memattr.c +0 -0
index c38b1a335590..b5051dcb7c1d 100644

drivers/firmware/efi/memattr.c int __init efi_memattr_apply_permissions(struct mm_struct *mm, · line 163

Quoted 5 lines
drivers/firmware/efi/memattr.c +1 -1
@@ -160,7 +160,7 @@ int __init efi_memattr_apply_permissions(struct mm_struct *mm,
 	if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
 		return 0;
 
-	tbl = memremap(efi_mem_attr_table, tbl_size, MEMREMAP_WB);
+	tbl = memremap(efi_mem_attr_table, tbl_size, MEMREMAP_WB | MEMREMAP_DEC);

Well that would work for SME where EFI tables/data are not encrypted, but will break for SEV where EFI tables/data are encrypted.

Quoted 5 lines
Quoted 2 lines

Hm. Why would it break for SEV? It brings the situation back to what it was before the patch.

Ah, true. I can try it and see how much further SME gets. Hopefully it doesn't turn into a whack-a-mole thing.

Unfortunately, it is turning into a whack-a-mole thing.

But it looks the following works for SME:

arch/x86/mm/ioremap.c +1 -1
index 3c36f3f5e688..ff3cd5fc8508 100644
@@ -505,7 +505,7 @@ EXPORT_SYMBOL(iounmap);
 
 void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags)
 {
-	if (flags & MEMREMAP_DEC)
+	if (flags & MEMREMAP_DEC || cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
 		return (void __force *)ioremap_cache(phys_addr, size);
 
 	return (void __force *)ioremap_encrypted(phys_addr, size);

I haven't had a chance to test the series on SEV, yet.

Thanks,
Tom

Quoted 8 lines

Thanks,
Tom

Quoted 4 lines

Note that that __ioremap_caller() would still check io_desc.flags before mapping it as decrypted.

#1 lore raw
Quoted 84 lines · Tom Lendacky
Quoted 58 lines · Tom Lendacky
Quoted 57 lines · Kirill A. Shutemov
Quoted 55 lines · Tom Lendacky
Quoted 46 lines · Kirill A. Shutemov
Quoted 30 lines · Tom Lendacky
Quoted 29 lines · Kirill A. Shutemov

Currently memremap(MEMREMAP_WB) can produce decrypted/shared mapping:

memremap(MEMREMAP_WB)
arch_memremap_wb()
ioremap_cache()
__ioremap_caller(.encrytped = false)

In such cases, the IORES_MAP_ENCRYPTED flag on the memory will determine if the resulting mapping is encrypted or decrypted.

Creating a decrypted mapping without explicit request from the caller is risky:

- It can inadvertently expose the guest's data and compromise the guest.

- Accessing private memory via shared/decrypted mapping on TDX will either trigger implicit conversion to shared or #VE (depending on VMM implementation).

Implicit conversion is destructive: subsequent access to the same memory via private mapping will trigger a hard-to-debug #VE crash.

The kernel already provides a way to request decrypted mapping explicitly via the MEMREMAP_DEC flag.

Modify memremap(MEMREMAP_WB) to produce encrypted/private mapping by default unless MEMREMAP_DEC is specified.

Quoted 7 lines
Quoted 1 line

Fix the crash due to #VE on kexec in TDX guests if CONFIG_EISA is enabled.

This patch causes my bare-metal system to crash during boot when using mem_encrypt=on:

[    2.392934] efi: memattr: Entry type should be RuntimeServiceCode/Data
[    2.393731] efi: memattr: ! 0x214c42f01f1162a-0xee70ac7bd1a9c629 [type=2028324321|attr=0x6590648fa4209879]

Could you try if this helps?

drivers/firmware/efi/memattr.c +0 -0
index c38b1a335590..b5051dcb7c1d 100644

drivers/firmware/efi/memattr.c int __init efi_memattr_apply_permissions(struct mm_struct *mm, · line 163

Quoted 5 lines
drivers/firmware/efi/memattr.c +1 -1
@@ -160,7 +160,7 @@ int __init efi_memattr_apply_permissions(struct mm_struct *mm,
 	if (WARN_ON(!efi_enabled(EFI_MEMMAP)))
 		return 0;
 
-	tbl = memremap(efi_mem_attr_table, tbl_size, MEMREMAP_WB);
+	tbl = memremap(efi_mem_attr_table, tbl_size, MEMREMAP_WB | MEMREMAP_DEC);

Well that would work for SME where EFI tables/data are not encrypted, but will break for SEV where EFI tables/data are encrypted.

Quoted 5 lines
Quoted 2 lines

Hm. Why would it break for SEV? It brings the situation back to what it was before the patch.

Ah, true. I can try it and see how much further SME gets. Hopefully it doesn't turn into a whack-a-mole thing.

Unfortunately, it is turning into a whack-a-mole thing.

But it looks the following works for SME:

arch/x86/mm/ioremap.c +1 -1
index 3c36f3f5e688..ff3cd5fc8508 100644
@@ -505,7 +505,7 @@ EXPORT_SYMBOL(iounmap);
 
 void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags)
 {
-	if (flags & MEMREMAP_DEC)
+	if (flags & MEMREMAP_DEC || cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
 		return (void __force *)ioremap_cache(phys_addr, size);
 
 	return (void __force *)ioremap_encrypted(phys_addr, size);
Quoted 1 line

I haven't had a chance to test the series on SEV, yet.

Please do.

I am okay with the change above. Borislav, is it acceptable direction for you?

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
maintainer
#2 lore raw
Quoted 2 lines · Kirill A. Shutemov

I am okay with the change above. Borislav, is it acceptable direction for you?

Yes, Tom and I have been talking offlist about making the *map code figure out itself what type of encryption setting it should use, based on the platform. It'll need a proper analysis, though, what all the possible usages are to see whether that scheme would be adequeate.

Something to experiment with after the merge window.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette