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. More cases check the shadow page tables: - L1 maps one 2M region at two L2 addresses that differ only in bit 60; shadow-stack accesses through the one without bit 60 must fault after the other one was used. - L1 clears bit 60 in a 2M leaf that L2 has used; the next shadow-stack access must fault. - A shadow-stack read (RET) from a page that the host never wrote must complete instead of faulting forever. KVM maps such a page read-only, and the read needs bit 60, which KVM grants only with write access. The same read without EPTP bit 7, and a CPL 3 read with EPTP bit 7 through a leaf without bit 60, need only read access and must complete as well. - Shadow-stack writes show up in the dirty log, also after it was fetched and cleared. Signed-off-by: Dennis Reußwig --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/x86/vmx_nested_sss_test.c | 620 +++++++++++++++++++++ 2 files changed, 621 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..2efca0136ba7 --- /dev/null +++ b/tools/testing/selftests/kvm/x86/vmx_nested_sss_test.c @@ -0,0 +1,620 @@ +// 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). + * + * More cases check KVM's shadow page tables: + * - L1 maps one 2M region at two L2 addresses, with and without bit 60. + * Shadow-stack accesses through the mapping without bit 60 must fault even + * after the other mapping was used, i.e. KVM must not share the 4K shadow + * pages of the two huge pages. + * - L1 clears bit 60 in a 2M leaf that L2 has used for shadow-stack + * accesses. After an ordinary access to the region, shadow-stack accesses + * must fault, i.e. KVM must not reuse the old 4K shadow pages. + * - A shadow-stack read (RET) from a page that the host has never written + * must complete; it finds a zero entry and causes a #CP in L2. KVM maps + * a never-written page read-only (the host's zero page), so this checks + * that a read that needs bit 60 is faulted in like a write. The same + * read without EPTP bit 7, and a user-mode read with EPTP bit 7 and a leaf + * without bit 60, need only read access and must complete as well. + * - Shadow-stack writes show up in the dirty log, also after the dirty log + * was fetched and cleared, i.e. KVM write-protects bit 60 for dirty + * logging and marks the page dirty when it grants bit 60 again. + */ +#include + +#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "vmx.h" + +/* The single-page cases: L2's shadow stack at SS_GVA, mapped to SS_L2_GPA. */ +#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 + +/* L1 memory that only L2 uses, in its own memslot that the host never touches. */ +#define L2_ONLY_SLOT 10 +#define L2_ONLY_GPA 0x40000000ULL +#define SIZE_2M (2ULL << 20) + +/* + * The 2M cases (alias, revoke) use their own VMs and L2 layout: two L2 + * mappings of the same 2M region, with and without bit 60. + */ +#define HUGE_SSS_L2_GPA 0x80000000ULL +#define HUGE_NO_SSS_L2_GPA 0x80200000ULL +#define SS_OFFSET 0x10000ULL +#define READ_OFFSET 0x20000ULL +#define SS_SSS_GVA 0x30000000ULL +#define SS_NO_SSS_GVA 0x30001000ULL +#define READ_GVA 0x30002000ULL +/* L1's view of its own EPT page that holds a 2M leaf. */ +#define EPT_PAGE_GVA 0x30004000ULL + +#define CP_VECTOR 21 + +#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 l2_read(void) +{ + READ_ONCE(*(u64 *)READ_GVA); + vmcall(); +} + +/* A RET without CALL: the shadow-stack read returns 0, which causes a #CP. */ +static void l2_ret_only(void) +{ + asm volatile("push $1\n\t" + "ret" ::: "memory"); + vmcall(); +} + +static void l1_enable_l2_cet(bool eptp_sss, u64 ssp) +{ + 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, ssp); + 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); +} + +static void l1_assert_vmcall(void) +{ + __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)); +} + +static void l1_assert_ept_violation(u64 gpa, u64 qual, u64 qual_ignore) +{ + u64 actual; + + __GUEST_ASSERT(vmread(VM_EXIT_REASON) == EXIT_REASON_EPT_VIOLATION, + "Expected EPT violation, got exit reason 0x%lx", + vmread(VM_EXIT_REASON)); + __GUEST_ASSERT(vmread(GUEST_PHYSICAL_ADDRESS) == gpa, + "Expected GPA 0x%lx, got 0x%lx", gpa, + 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); +} + +static void l1_guest_code(struct vmx_pages *vmx, u64 eptp_sss, u64 qual, + u64 qual_ignore) +{ + prepare_for_vmx_operation(vmx); + load_vmcs(vmx); + prepare_vmcs(vmx, l2_guest_code); + l1_enable_l2_cet(eptp_sss, SS_GVA + PAGE_SIZE); + + vmlaunch(); + + if (!qual) { + l1_assert_vmcall(); + GUEST_DONE(); + } + + /* The CALL pushes the return address just below the top of the page. */ + l1_assert_ept_violation(SS_L2_GPA + PAGE_SIZE - 8, qual, qual_ignore); + GUEST_DONE(); +} + +static void l1_alias_code(struct vmx_pages *vmx) +{ + prepare_for_vmx_operation(vmx); + load_vmcs(vmx); + prepare_vmcs(vmx, l2_guest_code); + l1_enable_l2_cet(true, SS_SSS_GVA + PAGE_SIZE); + + /* Shadow-stack accesses through the mapping with bit 60 succeed. */ + vmlaunch(); + l1_assert_vmcall(); + + /* An ordinary read of another page through the mapping without bit 60. */ + vmwrite(GUEST_RIP, (u64)l2_read); + vmresume(); + l1_assert_vmcall(); + + /* Shadow-stack accesses to the first page through that mapping fault. */ + vmwrite(GUEST_SSP, SS_NO_SSS_GVA + PAGE_SIZE); + vmwrite(GUEST_RIP, (u64)l2_guest_code); + vmresume(); + l1_assert_ept_violation(HUGE_NO_SSS_L2_GPA + SS_OFFSET + PAGE_SIZE - 8, + QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ | + EPT_VIOLATION_PROT_EXEC, + EPT_VIOLATION_PROT_USER_EXEC); + GUEST_DONE(); +} + +static void l1_invept_single_context(u64 eptp) +{ + struct { + u64 eptp; + u64 reserved; + } desc = { eptp, 0 }; + + asm volatile("invept %0, %1" : : "m"(desc), "r"(1UL) : "memory"); +} + +static void l1_revoke_code(struct vmx_pages *vmx, u64 *leaf) +{ + prepare_for_vmx_operation(vmx); + load_vmcs(vmx); + prepare_vmcs(vmx, l2_guest_code); + l1_enable_l2_cet(true, SS_SSS_GVA + PAGE_SIZE); + + /* Shadow-stack accesses through the 2M leaf with bit 60 succeed. */ + vmlaunch(); + l1_assert_vmcall(); + + /* Take the supervisor shadow-stack permission back. */ + *leaf &= ~EPT_SSS_PAGE; + l1_invept_single_context(vmread(EPT_POINTER)); + + /* An ordinary read of another page of the region. */ + vmwrite(GUEST_RIP, (u64)l2_read); + vmresume(); + l1_assert_vmcall(); + + /* Shadow-stack accesses to the first page now fault. */ + vmwrite(GUEST_SSP, SS_SSS_GVA + PAGE_SIZE); + vmwrite(GUEST_RIP, (u64)l2_guest_code); + vmresume(); + l1_assert_ept_violation(HUGE_SSS_L2_GPA + SS_OFFSET + PAGE_SIZE - 8, + QUAL_SS_WRITE | EPT_VIOLATION_PROT_READ | + EPT_VIOLATION_PROT_EXEC, + EPT_VIOLATION_PROT_USER_EXEC); + GUEST_DONE(); +} + +/* Run L2 at CPL 3 with user shadow stacks; @rsp is a user-mode data stack. */ +static void l1_enter_l2_at_cpl3(u64 rsp) +{ + wrmsr(MSR_IA32_U_CET, CET_SHSTK_EN); + vmwrite(GUEST_CS_SELECTOR, vmread(GUEST_CS_SELECTOR) | 3); + vmwrite(GUEST_CS_AR_BYTES, vmread(GUEST_CS_AR_BYTES) | (3 << 5)); + vmwrite(GUEST_SS_SELECTOR, vmread(GUEST_SS_SELECTOR) | 3); + vmwrite(GUEST_SS_AR_BYTES, vmread(GUEST_SS_AR_BYTES) | (3 << 5)); + vmwrite(GUEST_RSP, rsp); +} + +static void l1_zero_page_code(struct vmx_pages *vmx, u64 eptp_sss, u64 user_rsp) +{ + prepare_for_vmx_operation(vmx); + load_vmcs(vmx); + prepare_vmcs(vmx, l2_ret_only); + l1_enable_l2_cet(eptp_sss, SS_GVA + PAGE_SIZE - 8); + if (user_rsp) + l1_enter_l2_at_cpl3(user_rsp); + vmwrite(EXCEPTION_BITMAP, BIT(CP_VECTOR)); + + vmlaunch(); + + __GUEST_ASSERT(vmread(VM_EXIT_REASON) == EXIT_REASON_EXCEPTION_NMI && + (vmread(VM_EXIT_INTR_INFO) & 0xff) == CP_VECTOR, + "Expected #CP, got exit reason 0x%lx, interruption info 0x%lx, qualification 0x%lx", + vmread(VM_EXIT_REASON), vmread(VM_EXIT_INTR_INFO), + vmread(EXIT_QUALIFICATION)); + GUEST_DONE(); +} + +static void l1_dirty_log_code(struct vmx_pages *vmx) +{ + prepare_for_vmx_operation(vmx); + load_vmcs(vmx); + prepare_vmcs(vmx, l2_guest_code); + l1_enable_l2_cet(true, SS_GVA + PAGE_SIZE); + + vmlaunch(); + l1_assert_vmcall(); + GUEST_SYNC(1); + + vmwrite(GUEST_SSP, SS_GVA + PAGE_SIZE); + vmwrite(GUEST_RIP, (u64)l2_guest_code); + vmresume(); + l1_assert_vmcall(); + GUEST_DONE(); +} + +/* Map @gva to @l2_gpa in L2's page tables as a supervisor shadow-stack page. */ +static void map_ss_page(struct kvm_vm *vm, u64 gva, u64 l2_gpa) +{ + u64 *pte; + + virt_pg_map(vm, gva, l2_gpa); + pte = vm_get_pte(vm, gva); + *pte &= ~(PTE_WRITABLE_MASK(&vm->mmu) | PTE_USER_MASK(&vm->mmu)); + *pte |= PTE_DIRTY_MASK(&vm->mmu); +} + +static void run_l1(struct kvm_vcpu *vcpu) +{ + struct ucall uc; + + 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); + } +} + +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; + gpa_t ss_gpa; + u64 *pte; + + vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code); + vm_enable_tdp(vm); + vcpu_alloc_vmx(vm, &vmx_gva); + ept = &vm->stage2_mmu; + + ss_gpa = vm_phy_page_alloc(vm, MEM_REGION_DATA); + map_ss_page(vm, SS_GVA, SS_L2_GPA); + + 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); + run_l1(vcpu); + + kvm_vm_free(vm); +} + +static void test_huge_page_alias(void) +{ + struct kvm_mmu *ept; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + gva_t vmx_gva; + u64 *pte; + + vm = vm_create_with_one_vcpu(&vcpu, l1_alias_code); + vm_enable_tdp(vm); + vcpu_alloc_vmx(vm, &vmx_gva); + ept = &vm->stage2_mmu; + + /* 4K host pages, so that KVM shadows the 2M EPT leaves with 4K pages. */ + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, L2_ONLY_GPA, + L2_ONLY_SLOT, SIZE_2M / PAGE_SIZE, 0); + + map_ss_page(vm, SS_SSS_GVA, HUGE_SSS_L2_GPA + SS_OFFSET); + map_ss_page(vm, SS_NO_SSS_GVA, HUGE_NO_SSS_L2_GPA + SS_OFFSET); + virt_pg_map(vm, READ_GVA, HUGE_NO_SSS_L2_GPA + READ_OFFSET); + + tdp_identity_map_default_memslots(vm); + __tdp_map(vm, HUGE_SSS_L2_GPA, L2_ONLY_GPA, SIZE_2M, PG_LEVEL_2M); + __tdp_map(vm, HUGE_NO_SSS_L2_GPA, L2_ONLY_GPA, SIZE_2M, PG_LEVEL_2M); + + /* Both leaves read-only and dirty; they differ only in bit 60. */ + pte = __tdp_get_pte(vm, HUGE_SSS_L2_GPA, PG_LEVEL_2M); + *pte &= ~PTE_WRITABLE_MASK(ept); + *pte |= PTE_DIRTY_MASK(ept) | EPT_SSS_PAGE; + pte = __tdp_get_pte(vm, HUGE_NO_SSS_L2_GPA, PG_LEVEL_2M); + *pte &= ~PTE_WRITABLE_MASK(ept); + *pte |= PTE_DIRTY_MASK(ept); + + vcpu_args_set(vcpu, 1, vmx_gva); + run_l1(vcpu); + + kvm_vm_free(vm); +} + +static void test_huge_page_revoke(void) +{ + struct kvm_mmu *ept; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + gva_t vmx_gva; + gpa_t leaf_gpa; + u64 *pte; + + vm = vm_create_with_one_vcpu(&vcpu, l1_revoke_code); + vm_enable_tdp(vm); + vcpu_alloc_vmx(vm, &vmx_gva); + ept = &vm->stage2_mmu; + + /* 4K host pages, so that KVM shadows the 2M EPT leaf with 4K pages. */ + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, L2_ONLY_GPA, + L2_ONLY_SLOT, SIZE_2M / PAGE_SIZE, 0); + + map_ss_page(vm, SS_SSS_GVA, HUGE_SSS_L2_GPA + SS_OFFSET); + virt_pg_map(vm, READ_GVA, HUGE_SSS_L2_GPA + READ_OFFSET); + + tdp_identity_map_default_memslots(vm); + __tdp_map(vm, HUGE_SSS_L2_GPA, L2_ONLY_GPA, SIZE_2M, PG_LEVEL_2M); + + pte = __tdp_get_pte(vm, HUGE_SSS_L2_GPA, PG_LEVEL_2M); + *pte &= ~PTE_WRITABLE_MASK(ept); + *pte |= PTE_DIRTY_MASK(ept) | EPT_SSS_PAGE; + + /* Let L1 change the leaf itself. */ + leaf_gpa = addr_hva2gpa(vm, pte); + virt_pg_map(vm, EPT_PAGE_GVA, leaf_gpa & ~(PAGE_SIZE - 1)); + + vcpu_args_set(vcpu, 2, vmx_gva, EPT_PAGE_GVA + (leaf_gpa & (PAGE_SIZE - 1))); + run_l1(vcpu); + + kvm_vm_free(vm); +} + +static void sigalrm_handler(int sig) +{ + TEST_FAIL("L2's shadow-stack read did not complete, KVM keeps faulting it in"); +} + +/* A user-mode linear address needs U/S set at every level of L2's paging. */ +static void make_user(struct kvm_vm *vm, gva_t gva) +{ + int level; + + for (level = PG_LEVEL_4K; level <= vm->mmu.pgtable_levels; level++) + *__vm_get_pte(vm, gva, level) |= PTE_USER_MASK(&vm->mmu); +} + +/* + * @eptp_sss: run L2 with EPTP bit 7. The L1 EPT leaf sets bit 60 and is + * read-only in the supervisor case with EPTP bit 7, is writable without + * EPTP bit 7, and has neither in the user case. + * @user: run the RET at CPL 3 on a user shadow stack. + */ +static void test_shadow_stack_read_zero_page(bool eptp_sss, bool user) +{ + gva_t vmx_gva, user_stack = 0; + struct kvm_mmu *ept; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + u64 *pte; + + vm = vm_create_with_one_vcpu(&vcpu, l1_zero_page_code); + vm_enable_tdp(vm); + vcpu_alloc_vmx(vm, &vmx_gva); + ept = &vm->stage2_mmu; + + /* Never written, so a read fault maps the host's zero page. */ + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, L2_ONLY_GPA, + L2_ONLY_SLOT, 1, 0); + map_ss_page(vm, SS_GVA, SS_L2_GPA); + if (user) { + user_stack = vm_alloc_page(vm); + make_user(vm, SS_GVA); + make_user(vm, user_stack); + make_user(vm, (gva_t)l2_ret_only); + make_user(vm, (gva_t)l2_ret_only + 32); + } + + tdp_identity_map_default_memslots(vm); + tdp_map(vm, SS_L2_GPA, L2_ONLY_GPA, PAGE_SIZE); + pte = tdp_get_pte(vm, SS_L2_GPA); + *pte |= PTE_DIRTY_MASK(ept); + if (eptp_sss) + *pte &= ~PTE_WRITABLE_MASK(ept); + if (eptp_sss && !user) + *pte |= EPT_SSS_PAGE; + + vcpu_args_set(vcpu, 3, vmx_gva, (u64)eptp_sss, + user ? user_stack + PAGE_SIZE : 0); + signal(SIGALRM, sigalrm_handler); + alarm(10); + run_l1(vcpu); + alarm(0); + + kvm_vm_free(vm); +} + +static bool ss_page_dirty(struct kvm_vm *vm) +{ + unsigned long bitmap = 0; + + kvm_vm_get_dirty_log(vm, L2_ONLY_SLOT, &bitmap); + return bitmap & 1; +} + +static void test_shadow_stack_dirty_log(void) +{ + struct kvm_mmu *ept; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + struct ucall uc; + gva_t vmx_gva; + u64 *pte; + + vm = vm_create_with_one_vcpu(&vcpu, l1_dirty_log_code); + vm_enable_tdp(vm); + vcpu_alloc_vmx(vm, &vmx_gva); + ept = &vm->stage2_mmu; + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, L2_ONLY_GPA, + L2_ONLY_SLOT, 1, KVM_MEM_LOG_DIRTY_PAGES); + map_ss_page(vm, SS_GVA, SS_L2_GPA); + + tdp_identity_map_default_memslots(vm); + tdp_map(vm, SS_L2_GPA, L2_ONLY_GPA, PAGE_SIZE); + pte = tdp_get_pte(vm, SS_L2_GPA); + *pte &= ~PTE_WRITABLE_MASK(ept); + *pte |= PTE_DIRTY_MASK(ept) | EPT_SSS_PAGE; + + vcpu_args_set(vcpu, 1, vmx_gva); + vcpu_run(vcpu); + TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); + if (get_ucall(vcpu, &uc) == UCALL_ABORT) + REPORT_GUEST_ASSERT(uc); + TEST_ASSERT_EQ(uc.cmd, UCALL_SYNC); + + TEST_ASSERT(ss_page_dirty(vm), "First shadow-stack write is not in the dirty log"); + TEST_ASSERT(!ss_page_dirty(vm), "Dirty log was not cleared"); + + run_l1(vcpu); + TEST_ASSERT(ss_page_dirty(vm), + "Shadow-stack write after clearing the dirty log is not in the dirty log"); + + 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); + + ksft_print_header(); + ksft_set_plan(ARRAY_SIZE(tests) + 6); + + for (i = 0; i < ARRAY_SIZE(tests); i++) { + run_test(&tests[i]); + ksft_test_result_pass("%s\n", tests[i].name); + } + + test_huge_page_alias(); + ksft_test_result_pass("2M leaves with and without bit 60 do not share shadow pages\n"); + + test_huge_page_revoke(); + ksft_test_result_pass("clearing bit 60 in a used 2M leaf takes effect\n"); + + test_shadow_stack_read_zero_page(true, false); + ksft_test_result_pass("shadow-stack read from a never-written page completes\n"); + + test_shadow_stack_read_zero_page(false, false); + ksft_test_result_pass("same without EPTP bit 7\n"); + + test_shadow_stack_read_zero_page(true, true); + ksft_test_result_pass("same at CPL 3, leaf without bit 60\n"); + + test_shadow_stack_dirty_log(); + ksft_test_result_pass("shadow-stack writes show up in the dirty log\n"); + + ksft_finished(); +} -- 2.53.0