Zero memory at the point required by each arm64 sharing implementation. For pKVM, clear and clean the private contents to the point of coherency before the host gains access
For CCA, clear after successful conversion because the RMM can modify the contents while returning the pages to the host.
Clear the range on the no-encryption path as well.
Cc: Catalin Marinas catalin.marinas@arm.com Cc: Will Deacon will@kernel.org Signed-off-by: Aneesh Kumar K.V (Arm) aneesh.kumar@kernel.org --- arch/arm64/mm/mem_encrypt.c | 8 +++++++- drivers/firmware/arm_rmm/rsi.c | 9 +++++++++ drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 11 +++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/mm/mem_encrypt.c b/arch/arm64/mm/mem_encrypt.c index ee3c0ab04384..25eb67138b28 100644 --- a/arch/arm64/mm/mem_encrypt.c +++ b/arch/arm64/mm/mem_encrypt.c @@ -17,6 +17,7 @@ #include <linux/compiler.h> #include <linux/err.h> #include <linux/mm.h> +#include <linux/string.h>
#include <asm/mem_encrypt.h>
@@ -42,8 +43,13 @@ EXPORT_SYMBOL_GPL(set_memory_encrypted);
int set_memory_decrypted(unsigned long addr, int numpages) { - if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr))) + if (likely(!crypt_ops)) { + memset((void *)addr, 0, (size_t)numpages << PAGE_SHIFT); return 0; + } + + if (WARN_ON(!PAGE_ALIGNED(addr))) + return -EINVAL;
return crypt_ops->decrypt(addr, numpages); } diff --git a/drivers/firmware/arm_rmm/rsi.c b/drivers/firmware/arm_rmm/rsi.c index 0fcc4e33bd92..2c1591028b0e 100644 --- a/drivers/firmware/arm_rmm/rsi.c +++ b/drivers/firmware/arm_rmm/rsi.c @@ -6,6 +6,7 @@ #include <linux/jump_label.h> #include <linux/memblock.h> #include <linux/psci.h> +#include <linux/string.h> #include <linux/swiotlb.h> #include <linux/arm-rsi-cmds.h> #include <linux/kobject.h> @@ -146,6 +147,14 @@ static int realm_set_memory_decrypted(unsigned long addr, int numpages) { int ret = __set_memory_enc_dec(addr, numpages, false);
+ /* + * The RMM may scrub pages with nonzero values before returning them to + * the host. Zeroing before conversion would not guarantee zeroed shared + * memory, so clear the final shared mapping after the transition. + */ + if (!ret) + memset((void *)addr, 0, (size_t)numpages << PAGE_SHIFT); + WARN(ret, "Failed to decrypt memory, %d pages will be leaked", numpages);
diff --git a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c index 26fe9c3f22e3..819c26004c99 100644 --- a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c +++ b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c @@ -9,10 +9,12 @@
#include <linux/arm-smccc.h> #include <linux/array_size.h> +#include <linux/cacheflush.h> #include <linux/io.h> #include <linux/mem_encrypt.h> #include <linux/mm.h> #include <linux/pgtable.h> +#include <linux/string.h>
#include <asm/hypervisor.h>
@@ -61,6 +63,15 @@ static int pkvm_set_memory_encrypted(unsigned long addr, int numpages)
static int pkvm_set_memory_decrypted(unsigned long addr, int numpages) { + size_t size = numpages * PAGE_SIZE; + + /* + * The host can read the pages as soon as they are shared, possibly + * through a Non-cacheable mapping. Clear them first. + */ + memset((void *)addr, 0, size); + dcache_clean_poc(addr, addr + size); + return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID, addr, numpages); }