[PATCH 08/10] KVM: selftests: Add nested EPT supervisor shadow-stack test
From: Dennis Reußwig
Date: Thu Dec 03 2026 - 18:51:13 EST
L1 runs L2 with supervisor shadow stacks. L2 does a CALL and RET whose
shadow-stack push and pop hit a page whose L1 EPT permissions vary per
case, with and without EPTP bit 7. Check that the accesses succeed or
cause EPT violations as the SDM defines, and that the violations reach
L1 with exit qualification bits 13 and 14 set as expected.
Signed-off-by: Dennis Reußwig <dennis@reusswig.codes>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../selftests/kvm/x86/vmx_nested_sss_test.c | 213 +++++++++++++++++++++
2 files changed, 214 insertions(+)
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 95867977beb3..dc890dbe48d1 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -146,6 +146,7 @@ TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state
TEST_GEN_PROGS_x86 += x86/vmx_msrs_test
TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state
TEST_GEN_PROGS_x86 += x86/vmx_nested_la57_state_test
+TEST_GEN_PROGS_x86 += x86/vmx_nested_sss_test
TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test
TEST_GEN_PROGS_x86 += x86/apic_id_routing_test
TEST_GEN_PROGS_x86 += x86/xapic_ipi_test
diff --git a/tools/testing/selftests/kvm/x86/vmx_nested_sss_test.c b/tools/testing/selftests/kvm/x86/vmx_nested_sss_test.c
new file mode 100644
index 000000000000..a685fba0af7f
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/vmx_nested_sss_test.c
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Nested EPT supervisor shadow-stack control (EPTP bit 7, EPT bit 60).
+ *
+ * L1 runs L2 with supervisor shadow stacks enabled; L2 does a CALL/RET whose
+ * shadow-stack push and pop hit a page whose L1 EPT permissions vary per test.
+ * With EPTP bit 7 set, supervisor shadow-stack accesses are allowed iff every
+ * EPT entry is readable, every non-leaf entry is writable and the leaf sets
+ * bit 60; the write bit of the leaf does not matter. Without EPTP bit 7 they
+ * follow the normal write rules. Denied accesses must reach L1 as EPT
+ * violations with exit qualification bit 13 (shadow-stack access) and, with
+ * EPTP bit 7, bit 14 (bit 60 of the leaf).
+ */
+#include "test_util.h"
+#include "kvm_util.h"
+#include "processor.h"
+#include "vmx.h"
+
+#define SS_GVA 0x30000000ULL
+/* Alone in its 2M region, so the non-leaf EPT entries above it are its own. */
+#define SS_L2_GPA 0x80000000ULL
+
+#define EPT_SSS_PAGE BIT_ULL(60)
+#define VMX_EPTP_SSS BIT_ULL(7)
+
+/* Paging attributes of L2's linear address, not under test. */
+#define QUAL_IGNORE (BIT_ULL(9) | BIT_ULL(10) | BIT_ULL(11))
+
+#define QUAL_SS_WRITE (EPT_VIOLATION_ACC_WRITE | EPT_VIOLATION_GVA_IS_VALID | \
+ EPT_VIOLATION_GVA_TRANSLATED | EPT_VIOLATION_SHADOW_STACK)
+
+struct sss_test {
+ const char *name;
+ bool eptp_sss;
+ bool leaf_writable;
+ bool leaf_sss;
+ bool pde_writable;
+ /* 0 if L2 must reach its VMCALL. */
+ u64 qual;
+ u64 qual_ignore;
+};
+
+static const struct sss_test tests[] = {
+ {
+ .name = "SSS page, leaf read-only",
+ .eptp_sss = true, .leaf_writable = false, .leaf_sss = true,
+ .pde_writable = true,
+ },
+ {
+ .name = "not an SSS page, leaf read-only",
+ .eptp_sss = true, .leaf_writable = false, .leaf_sss = false,
+ .pde_writable = true,
+ .qual = QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ | EPT_VIOLATION_PROT_EXEC,
+ },
+ {
+ .name = "not an SSS page, leaf writable",
+ .eptp_sss = true, .leaf_writable = true, .leaf_sss = false,
+ .pde_writable = true,
+ .qual = QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ |
+ EPT_VIOLATION_PROT_WRITE | EPT_VIOLATION_PROT_EXEC,
+ },
+ {
+ .name = "SSS page, non-leaf read-only",
+ .eptp_sss = true, .leaf_writable = false, .leaf_sss = true,
+ .pde_writable = false,
+ .qual = QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ | EPT_VIOLATION_PROT_EXEC |
+ EPT_VIOLATION_SSS_PAGE,
+ },
+ {
+ .name = "no EPTP bit 7, leaf read-only",
+ .eptp_sss = false, .leaf_writable = false, .leaf_sss = true,
+ .pde_writable = true,
+ .qual = QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ | EPT_VIOLATION_PROT_EXEC,
+ /* Bit 14 is undefined without EPTP bit 7. */
+ .qual_ignore = EPT_VIOLATION_SSS_PAGE,
+ },
+ {
+ .name = "no EPTP bit 7, leaf writable",
+ .eptp_sss = false, .leaf_writable = true, .leaf_sss = true,
+ .pde_writable = true,
+ },
+};
+
+static void l2_guest_code(void)
+{
+ /* The CALL pushes to and the RET pops from the shadow stack. */
+ asm volatile("call 1f\n\t"
+ "jmp 2f\n\t"
+ "1: ret\n\t"
+ "2:" ::: "memory");
+ vmcall();
+}
+
+static void l1_guest_code(struct vmx_pages *vmx, u64 eptp_sss, u64 qual,
+ u64 qual_ignore)
+{
+ u64 actual;
+
+ prepare_for_vmx_operation(vmx);
+ load_vmcs(vmx);
+ prepare_vmcs(vmx, l2_guest_code);
+
+ if (eptp_sss)
+ vmwrite(EPT_POINTER, vmread(EPT_POINTER) | VMX_EPTP_SSS);
+
+ /* CR4.CET requires CR0.WP. */
+ vmwrite(GUEST_CR0, vmread(GUEST_CR0) | X86_CR0_WP);
+ vmwrite(GUEST_CR4, vmread(GUEST_CR4) | X86_CR4_CET);
+ vmwrite(GUEST_S_CET, CET_SHSTK_EN);
+ vmwrite(GUEST_SSP, SS_GVA + PAGE_SIZE);
+ vmwrite(GUEST_INTR_SSP_TABLE, 0);
+ vmwrite(VM_ENTRY_CONTROLS, vmread(VM_ENTRY_CONTROLS) | VM_ENTRY_LOAD_CET_STATE);
+ vmwrite(HOST_S_CET, 0);
+ vmwrite(HOST_SSP, 0);
+ vmwrite(HOST_INTR_SSP_TABLE, 0);
+ vmwrite(VM_EXIT_CONTROLS, vmread(VM_EXIT_CONTROLS) | VM_EXIT_LOAD_CET_STATE);
+
+ vmlaunch();
+
+ if (!qual) {
+ __GUEST_ASSERT(vmread(VM_EXIT_REASON) == EXIT_REASON_VMCALL,
+ "Expected VMCALL, got exit reason 0x%lx, qualification 0x%lx",
+ vmread(VM_EXIT_REASON), vmread(EXIT_QUALIFICATION));
+ GUEST_DONE();
+ }
+
+ __GUEST_ASSERT(vmread(VM_EXIT_REASON) == EXIT_REASON_EPT_VIOLATION,
+ "Expected EPT violation, got exit reason 0x%lx",
+ vmread(VM_EXIT_REASON));
+
+ /* The CALL pushes the return address just below the top of the page. */
+ __GUEST_ASSERT(vmread(GUEST_PHYSICAL_ADDRESS) == SS_L2_GPA + PAGE_SIZE - 8,
+ "Expected GPA 0x%llx, got 0x%lx", SS_L2_GPA + PAGE_SIZE - 8,
+ vmread(GUEST_PHYSICAL_ADDRESS));
+
+ actual = vmread(EXIT_QUALIFICATION) & ~(QUAL_IGNORE | qual_ignore);
+ __GUEST_ASSERT(actual == qual, "Expected qualification 0x%lx, got 0x%lx",
+ qual, actual);
+ GUEST_DONE();
+}
+
+static void run_test(const struct sss_test *t)
+{
+ struct kvm_mmu *ept;
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ gva_t vmx_gva;
+ struct ucall uc;
+ gpa_t ss_gpa;
+ u64 *pte;
+
+ pr_info("%s\n", t->name);
+
+ vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);
+ vm_enable_tdp(vm);
+ vcpu_alloc_vmx(vm, &vmx_gva);
+ ept = &vm->stage2_mmu;
+
+ /*
+ * Map the shadow-stack page into L2's (and L1's) page tables as a
+ * supervisor shadow-stack page: read-only and dirty.
+ */
+ ss_gpa = vm_phy_page_alloc(vm, MEM_REGION_DATA);
+ virt_pg_map(vm, SS_GVA, SS_L2_GPA);
+ pte = vm_get_pte(vm, SS_GVA);
+ *pte &= ~(PTE_WRITABLE_MASK(&vm->mmu) | PTE_USER_MASK(&vm->mmu));
+ *pte |= PTE_DIRTY_MASK(&vm->mmu);
+
+ tdp_identity_map_default_memslots(vm);
+ tdp_map(vm, SS_L2_GPA, ss_gpa, PAGE_SIZE);
+
+ pte = tdp_get_pte(vm, SS_L2_GPA);
+ if (!t->leaf_writable)
+ *pte &= ~PTE_WRITABLE_MASK(ept);
+ if (t->leaf_sss)
+ *pte |= EPT_SSS_PAGE;
+
+ if (!t->pde_writable) {
+ pte = __tdp_get_pte(vm, SS_L2_GPA, PG_LEVEL_2M);
+ *pte &= ~PTE_WRITABLE_MASK(ept);
+ }
+
+ vcpu_args_set(vcpu, 4, vmx_gva, (u64)t->eptp_sss, t->qual, t->qual_ignore);
+ vcpu_run(vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_DONE:
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ default:
+ TEST_FAIL("Unexpected ucall %lu", uc.cmd);
+ }
+
+ kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+ int i;
+
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SHSTK));
+ TEST_REQUIRE(kvm_cpu_has_ept());
+ TEST_REQUIRE(kvm_get_feature_msr(MSR_IA32_VMX_EPT_VPID_CAP) &
+ VMX_EPT_VPID_CAP_SSS);
+
+ for (i = 0; i < ARRAY_SIZE(tests); i++)
+ run_test(&tests[i]);
+
+ return 0;
+}
--
2.53.0