From: "Dennis Reußwig" <dennis@reusswig.codes>
To: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Thomas Gleixner <tglx@kernel.org>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Jon Kohler <jon@nutanix.com>, Jonathan Corbet <corbet@lwn.net>,
Shuah Khan <skhan@linuxfoundation.org>,
Randy Dunlap <rdunlap@infradead.org>,
Shuah Khan <shuah@kernel.org>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org,
"Chao Gao" <chao.gao@intel.com>,
"Maxim Levitsky" <mlevitsk@redhat.com>,
"Vitaly Kuznetsov" <vkuznets@redhat.com>,
"Panayotis Katsaloulis" <panayotis@panayotis.com>,
"Dennis Reußwig" <dennis@reusswig.codes>
Subject: [PATCH 08/10] KVM: selftests: Add nested EPT supervisor shadow-stack test
Date: Fri, 04 Dec 2026 00:49:21 +0100 [thread overview]
Message-ID: <20261204-nested-sss-v1-8-57de06f58b2b@reusswig.codes> (raw)
In-Reply-To: <20261204-nested-sss-v1-0-57de06f58b2b@reusswig.codes>
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
next prev parent reply other threads:[~2026-12-03 23:49 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
2026-12-03 23:49 ` [PATCH 01/10] KVM: nVMX: Allow CET entry/exit controls with enlightened VMCS Dennis Reußwig
2026-12-03 23:49 ` [PATCH 02/10] KVM: x86/mmu: Move TDP A/D tracking type to SPTE bits 56-57 Dennis Reußwig
2026-12-03 23:49 ` [PATCH 03/10] KVM: VMX: Report shadow-stack accesses in EPT violations as PFERR_SS Dennis Reußwig
2026-12-03 23:49 ` [PATCH 04/10] KVM: x86/mmu: Handle the EPT supervisor shadow-stack bit like a write bit Dennis Reußwig
2026-12-03 23:49 ` [PATCH 05/10] KVM: nVMX: Shadow supervisor shadow-stack control of nested EPT Dennis Reußwig
2026-12-03 23:49 ` [PATCH 06/10] KVM: nVMX: Advertise supervisor shadow-stack control for " Dennis Reußwig
2026-12-03 23:49 ` [PATCH 07/10] KVM: selftests: Add helpers for nested EPT shadow-stack and MBEC tests Dennis Reußwig
2026-12-03 23:49 ` Dennis Reußwig [this message]
2026-12-03 23:49 ` [PATCH 09/10] KVM: selftests: Add nested EPT mode-based execute control test Dennis Reußwig
2026-12-03 23:49 ` [PATCH 10/10] KVM: selftests: Check VMX capabilities for CET in L2 with MBEC Dennis Reußwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204-nested-sss-v1-8-57de06f58b2b@reusswig.codes \
--to=dennis@reusswig.codes \
--cc=bp@alien8.de \
--cc=chao.gao@intel.com \
--cc=corbet@lwn.net \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=jon@nutanix.com \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=mlevitsk@redhat.com \
--cc=panayotis@panayotis.com \
--cc=pbonzini@redhat.com \
--cc=rdunlap@infradead.org \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=tglx@kernel.org \
--cc=vkuznets@redhat.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®