[PATCH Part1 RFC v2 17/20] x86/mm: Add support to validate memory when changing C-bit

From: Brijesh Singh
Date: Fri Apr 30 2021 - 08:18:05 EST


The set_memory_{encrypt,decrypt}() are used for changing the pages
from decrypted (shared) to encrypted (private) and vice versa.
When SEV-SNP is active, the page state transition needs to go through
additional steps.

If the page is transitioned from shared to private, then perform the
following after the encryption attribute is set in the page table:

1. Issue the page state change VMGEXIT to add the memory region in
the RMP table.
2. Validate the memory region after the RMP entry is added.

To maintain the security guarantees, if the page is transitioned from
private to shared, then perform the following before encryption attribute
is removed from the page table:

1. Invalidate the page.
2. Issue the page state change VMGEXIT to remove the page from RMP table.

To change the page state in the RMP table, use the Page State Change
VMGEXIT defined in the GHCB specification.

Signed-off-by: Brijesh Singh <brijesh.singh@xxxxxxx>
---
arch/x86/include/asm/sev.h | 4 ++
arch/x86/kernel/sev.c | 114 +++++++++++++++++++++++++++++++++++
arch/x86/mm/pat/set_memory.c | 15 +++++
3 files changed, 133 insertions(+)

diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
index 62aba82acfb8..1b505061d9f7 100644
--- a/arch/x86/include/asm/sev.h
+++ b/arch/x86/include/asm/sev.h
@@ -111,6 +111,8 @@ void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long padd
unsigned int npages);
void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr,
unsigned int npages);
+void snp_set_memory_shared(unsigned long vaddr, unsigned int npages);
+void snp_set_memory_private(unsigned long vaddr, unsigned int npages);
#else
static inline void sev_es_ist_enter(struct pt_regs *regs) { }
static inline void sev_es_ist_exit(void) { }
@@ -126,6 +128,8 @@ static inline void __init
early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages)
{
}
+static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { }
+static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { }
#endif

#endif
diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c
index 33420f6da030..f28fd8605e63 100644
--- a/arch/x86/kernel/sev.c
+++ b/arch/x86/kernel/sev.c
@@ -612,6 +612,120 @@ void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr
early_snp_set_page_state(paddr, npages, SNP_PAGE_STATE_SHARED);
}

+static int snp_page_state_vmgexit(struct ghcb *ghcb, struct snp_page_state_change *data)
+{
+ struct snp_page_state_header *hdr;
+ int ret = 0;
+
+ hdr = &data->header;
+
+ /*
+ * As per the GHCB specification, the hypervisor can resume the guest before
+ * processing all the entries. The loop checks whether all the entries are
+ * processed. If not, then keep retrying.
+ */
+ while (hdr->cur_entry <= hdr->end_entry) {
+
+ ghcb_set_sw_scratch(ghcb, (u64)__pa(data));
+
+ ret = sev_es_ghcb_hv_call(ghcb, NULL, SVM_VMGEXIT_SNP_PAGE_STATE_CHANGE, 0, 0);
+
+ /* Page State Change VMGEXIT can pass error code through exit_info_2. */
+ if (ret || ghcb->save.sw_exit_info_2) {
+ WARN(1, "SEV-SNP: page state change failed ret=%d exit_info_2=%llx\n",
+ ret, ghcb->save.sw_exit_info_2);
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * The function uses the NAE event to batch the page state change request.
+ */
+static void snp_set_page_state(unsigned long vaddr, unsigned int npages, int op)
+{
+ struct snp_page_state_change *data;
+ struct snp_page_state_header *hdr;
+ struct snp_page_state_entry *e;
+ unsigned long vaddr_end;
+ struct ghcb_state state;
+ struct ghcb *ghcb;
+ int idx;
+
+ vaddr = vaddr & PAGE_MASK;
+ vaddr_end = vaddr + (npages << PAGE_SHIFT);
+
+ ghcb = sev_es_get_ghcb(&state);
+ if (unlikely(!ghcb))
+ panic("SEV-SNP: Failed to get GHCB\n");
+
+ data = (struct snp_page_state_change *)ghcb->shared_buffer;
+ hdr = &data->header;
+
+ while (vaddr < vaddr_end) {
+ e = data->entry;
+ memset(data, 0, sizeof (*data));
+
+ for (idx = 0; idx < VMGEXIT_PSC_MAX_ENTRY; idx++, e++) {
+ unsigned long pfn;
+
+ if (is_vmalloc_addr((void *)vaddr))
+ pfn = vmalloc_to_pfn((void *)vaddr);
+ else
+ pfn = __pa(vaddr) >> PAGE_SHIFT;
+
+ e->gfn = pfn;
+ e->operation = op;
+ hdr->end_entry = idx;
+
+ /*
+ * The GHCB specification provides the flexibility to use
+ * either 4K or 2MB page size in the RMP table. The curent
+ * SNP support does not keep track of the page size used
+ * in the RMP table. To avoid the overlap request, use the
+ * 4K page size in the RMP table.
+ */
+ e->pagesize = RMP_PG_SIZE_4K;
+ vaddr = vaddr + PAGE_SIZE;
+
+ if (vaddr >= vaddr_end)
+ break;
+ }
+
+ /* Terminate the guest on page state change failure. */
+ if (snp_page_state_vmgexit(ghcb, data))
+ sev_es_terminate(1, GHCB_TERM_PSC);
+ }
+
+ sev_es_put_ghcb(&state);
+}
+
+void snp_set_memory_shared(unsigned long vaddr, unsigned int npages)
+{
+ if (!sev_snp_active())
+ return;
+
+ /* Invalidate the memory before changing the page state in the RMP table. */
+ pvalidate_pages(vaddr, npages, 0);
+
+ /* Change the page state in the RMP table. */
+ snp_set_page_state(vaddr, npages, SNP_PAGE_STATE_SHARED);
+}
+
+void snp_set_memory_private(unsigned long vaddr, unsigned int npages)
+{
+ if (!sev_snp_active())
+ return;
+
+ /* Change the page state in the RMP table. */
+ snp_set_page_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE);
+
+ /* Validate the memory after the memory is made private in the RMP table. */
+ pvalidate_pages(vaddr, npages, 1);
+}
+
int sev_es_setup_ap_jump_table(struct real_mode_header *rmh)
{
u16 startup_cs, startup_ip;
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index 427980617557..34cd13671d5c 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -27,6 +27,7 @@
#include <asm/proto.h>
#include <asm/memtype.h>
#include <asm/set_memory.h>
+#include <asm/sev.h>

#include "../mm_internal.h"

@@ -2001,8 +2002,22 @@ static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)
*/
cpa_flush(&cpa, !this_cpu_has(X86_FEATURE_SME_COHERENT));

+ /*
+ * To maintain the security gurantees of SEV-SNP guest invalidate the memory
+ * before clearing the encryption attribute.
+ */
+ if (!enc)
+ snp_set_memory_shared(addr, numpages);
+
ret = __change_page_attr_set_clr(&cpa, 1);

+ /*
+ * Now that memory is mapped encrypted in the page table, validate the memory
+ * range before the return.
+ */
+ if (!ret && enc)
+ snp_set_memory_private(addr, numpages);
+
/*
* After changing the encryption attribute, we need to flush TLBs again
* in case any speculative TLB caching occurred (but no need to flush
--
2.17.1