KVM: nVMX: Synthesize SHUTDOWN on RSM with bad state
Sean Christopherson
maintainerv5 cover KVM: nVMX: Synthesize SHUTDOWN on RSM with bad state
Synthesize SHUTDOWN if invalid guest state is detected a RSM, i.e. if SMRAM is clobbered by L1 (or host userspace) while handling an SMI that occurred while L2 was active. This fixes yet another case of syzkaller tripping KVM's sanity check that KVM doesn't cancel a pending nested VM-Enter.
v5:
- Fix goofs in patch 1. [Sashiko]
- Print out correct target L1 vs. L2. [Sashiko]
v4:
- https://lore.kernel.org/all/[email protected]
- Synthesize SHUTDOWN instead of trying to suppress the KVM_BUG_ON().
- Add a selftest.
v3:
- https://lore.kernel.org/all/[email protected]
- Retain KVM_NESTED_RUN_PENDING_UNTRUSTED until after sync_vmcs02_to_vmcs12(), to avoid saving VMCS12 fields that are valid only after L2 has actually run.
- Clear the untrusted pending state before restoring L1 state to avoid leaking nested_run_pending into L1 and blocking event injection.
- Clarify the VMX pending-run BUG comment and update the changelog.
- Tested with the syzkaller repro on the fixed kernel, no WARNING/KVM_BUG in the repro log
v2:
- https://lore.kernel.org/all/[email protected]
- Mark nested state restored by RSM from SMRAM as KVM_NESTED_RUN_PENDING_UNTRUSTED.
- Keep the BUG check in __vmx_handle_exit(), but make it apply only to KVM_NESTED_RUN_PENDING.
v1: https://lore.kernel.org/all/[email protected]
Hao Zhang (1):
KVM: selftests: Extend the invalid nVMX guest state test to cover RSM
Sean Christopherson (5):
KVM: x86: Extract VMX's unhandleable emulation check to common x86
KVM: nVMX: Synthesize SHUTDOWN on RSM if L2 requires emulation
KVM: x86: Rework kvm_x86_ops.vcpu_pre_run() into
.vcpu_needs_initialization()
KVM: selftests: Use port 0x80 in invalid nVMX guest state test
KVM: selftests: Refactor invalid nVMX state test to prepare for RSM
testcase
arch/x86/include/asm/kvm-x86-ops.h | 3 +- arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/smm.c | 4 + arch/x86/kvm/svm/sev.c | 5 + arch/x86/kvm/svm/svm.c | 12 +- arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/vmx/main.c | 21 +++- arch/x86/kvm/vmx/tdx.c | 9 +- arch/x86/kvm/vmx/vmx.c | 12 +- arch/x86/kvm/vmx/x86_ops.h | 4 +- arch/x86/kvm/x86.c | 10 +- .../kvm/x86/vmx_invalid_nested_guest_state.c | 118 ++++++++++++++---- 12 files changed, 143 insertions(+), 60 deletions(-)
base-commit: 3c7d7f908d574277a845423ec32250a8d8df44c8
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH v5 1/6 KVM: x86: Extract VMX's unhandleable emulation check to common x86
Expose VMX's check for unhandleable emulation as its own kvm_x86_ops hook, and move the actual pre-KVM_RUN check into common x86. This will allow sharing the core logic with KVM's RSM emulation without needed to add a post-RSM hook, and is a step towards removing the .vcpu_pre_run() hook entirely.
Alternatively, KVM could provide a post-RSM hook as mentioned, but pre/post hooks tend to be unwieldy as the exact "timing" of the call often matters greatly. E.g. in this case, the call must slot in exactly between loading guest state from SMRAM and the hack to force the vCPU out of L2 on SHUTDOWN.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/vmx/main.c | 11 +++++++++++ arch/x86/kvm/vmx/vmx.c | 7 +------ arch/x86/kvm/vmx/x86_ops.h | 1 + arch/x86/kvm/x86.c | 5 +++++ 6 files changed, 21 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -68,6 +68,7 @@ KVM_X86_OP(vcpu_run) KVM_X86_OP(handle_exit) KVM_X86_OP(skip_emulated_instruction) KVM_X86_OP_OPTIONAL(update_emulated_instruction) +KVM_X86_OP_OPTIONAL_RET0(unhandleable_emulation_required) KVM_X86_OP(set_interrupt_shadow) KVM_X86_OP(get_interrupt_shadow) KVM_X86_OP(patch_hypercall)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1593,6 +1593,8 @@ struct kvm_x86_ops { enum exit_fastpath_completion exit_fastpath);int (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);void (*update_emulated_instruction)(struct kvm_vcpu *vcpu); +bool (*unhandleable_emulation_required)(struct kvm_vcpu *vcpu); +void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu,int mask);u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu); void (*patch_hypercall)(struct kvm_vcpu *vcpu,
arch/x86/kvm/vmx/main.c +11 -0
@@ -165,6 +165,16 @@ static int vt_handle_exit(struct kvm_vcpu *vcpu,return vmx_handle_exit (vcpu, fastpath); } +static bool vt_unhandleable_emulation_required (struct kvm_vcpu *vcpu) +{ +if (is_td_vcpu (vcpu)) { +WARN_ON_ONCE (to_vt (vcpu)->emulation_required); +return false; + } + +return vmx_unhandleable_emulation_required (vcpu); +} +static int vt_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info) {if (unlikely (is_td_vcpu (vcpu)))
@@ -944,6 +954,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .handle_exit = vt_op(handle_exit), .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction, + .unhandleable_emulation_required = vt_op(unhandleable_emulation_required), .set_interrupt_shadow = vt_op(set_interrupt_shadow), .get_interrupt_shadow = vt_op(get_interrupt_shadow), .patch_hypercall = vt_op(patch_hypercall),
arch/x86/kvm/vmx/vmx.c +1 -6
@@ -6035,7 +6035,7 @@ static int handle_nmi_window(struct kvm_vcpu *vcpu)* with unsrestricted guest mode disabled) and KVM can't faithfully emulate the * current vCPU state. */ -static bool vmx_unhandleable_emulation_required (struct kvm_vcpu *vcpu) +bool vmx_unhandleable_emulation_required (struct kvm_vcpu *vcpu) {struct vcpu_vmx *vmx =to_vmx (vcpu);
@@ -6112,11 +6112,6 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)int vmx_vcpu_pre_run (struct kvm_vcpu *vcpu) { -if (vmx_unhandleable_emulation_required (vcpu)) { -kvm_prepare_emulation_failure_exit (vcpu); -return 0 ; - } -return 1 ; }
arch/x86/kvm/vmx/x86_ops.h +1 -0
@@ -31,6 +31,7 @@ int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath);void vmx_handle_exit_irqoff (struct kvm_vcpu *vcpu);int vmx_skip_emulated_instruction (struct kvm_vcpu *vcpu);void vmx_update_emulated_instruction (struct kvm_vcpu *vcpu); +bool vmx_unhandleable_emulation_required (struct kvm_vcpu *vcpu);int vmx_set_msr (struct kvm_vcpu *vcpu,struct msr_data *msr_info);#ifdef CONFIG_KVM_SMM int vmx_smi_allowed (struct kvm_vcpu *vcpu,bool for_injection);
arch/x86/kvm/x86.c +5 -0
@@ -8867,6 +8867,11 @@ static int kvm_x86_vcpu_pre_run(struct kvm_vcpu *vcpu) !kvm_apic_init_sipi_allowed(vcpu))return -EINVAL ; +if (kvm_x86_call (unhandleable_emulation_required)(vcpu)) { +kvm_prepare_emulation_failure_exit (vcpu); +return 0 ; + } +return kvm_x86_call (vcpu_pre_run)(vcpu); }
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH v5 2/6 KVM: nVMX: Synthesize SHUTDOWN on RSM if L2 requires emulation
In reply to Sean ChristophersonSynthesize SHUTDOWN (for L1) if L2 requires unhandleable emulation after loading guest state from SMRAM during RSM to prevent a misbehaving L1 (or userspace via L1) from tripping the sanity check that KVM doesn't try to cancel a pending nested VM-Enter. If SMRAM is modified such that RSM will load what should be impossible state for L2, then KVM will detect that it needs to emulate the current code stream and will abort VM-Entry to L2. And because KVM (rightly) expects such a scenario to be impossible, KVM WARNs and bugs the VM.
__ret && !(vcpu->kvm)->vm_bugged
WARNING: arch/x86/kvm/vmx/vmx.c:6741 at vmx_handle_exit+0x65/0x790 [kvm_intel], CPU#13: vmx_invalid_nes/2902 Modules linked in: kvm_intel kvm irqbypass [last unloaded: kvm] CPU: 13 UID: 1000 PID: 2902 Comm: vmx_invalid_nes Tainted: G W 7.2.0-rc2 #124 PREEMPT Tainted: [W]=WARN Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:vmx_handle_exit+0x65/0x790 [kvm_intel] Call Trace: <TASK> kvm_arch_vcpu_ioctl_run+0xdf8/0x1d00 [kvm] kvm_vcpu_ioctl+0x2d5/0x960 [kvm] __x64_sys_ioctl+0x8a/0xd0 do_syscall_64+0xb7/0x570 entry_SYSCALL_64_after_hwframe+0x4b/0x53 </TASK>
Alternatively, KVM could suppress the WARN for the RSM case, but that would still leave the vCPU in a "bad" state that KVM doesn't know how to handle (which is also why KVM rejects attempts to do KVM_RUN when the vCPU is loaded with invalid state). And architecturally, the Intel SDM explicitly states that RSM leads to shutdown if the CPU detects invalid state.
--- arch/x86/kvm/smm.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kvm/smm.c +4 -0
@@ -649,6 +649,10 @@ int emulator_leave_smm(struct x86_emulate_ctxt *ctxt)#endif ret =rsm_load_state_32 (ctxt, &smram.smram32); +if (ret ==X86EMUL_CONTINUE && +kvm_x86_call (unhandleable_emulation_required)(vcpu)) + ret =X86EMUL_UNHANDLEABLE ; +/* * If RSM fails and triggers shutdown, architecturally the shutdown * occurs *before* the transition to guest mode. But due to KVM's
Signature · 1 line
2.55.0.508.g3f0d502094-goog
[email protected]
AI reviewRe: [PATCH v5 2/6] KVM: nVMX: Synthesize SHUTDOWN on RSM if L2 requires emulation
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The early return for leave_smm failure in emulator_leave_smm bypasses kvm_leave_nested(), leaving the vCPU in an inconsistent nested guest mode state. --
--- Patch [2]: [PATCH v5 2/6] KVM: nVMX: Synthesize SHUTDOWN on RSM if L2 requires emulation --- commit 4200f9610608af5b1ad7221b2b5cf164faad2906
Author: Sean Christopherson <[email protected]>
KVM: nVMX: Synthesize SHUTDOWN on RSM if L2 requires emulation
This commit synthesizes SHUTDOWN for L1 if L2 requires unhandleable emulation after loading guest state from SMRAM during RSM. This prevents a misbehaving L1 from triggering a KVM warning and bugging the VM.
Quoted 5 lines
arch/x86/kvm/smm.c +0
-0
arch/x86/kvm/smm.c int emulator_leave_smm(struct x86_emulate_ctxt *ctxt) · line 658
Quoted 10 lines
arch/x86/kvm/smm.c +4
-0@@ -649,6 +649,10 @@ int emulator_leave_smm(struct x86_emulate_ctxt *ctxt)#endif ret =rsm_load_state_32 (ctxt, &smram.smram32); +if (ret ==X86EMUL_CONTINUE && +kvm_x86_call (unhandleable_emulation_required)(vcpu)) + ret =X86EMUL_UNHANDLEABLE ; +/* * If RSM fails and triggers shutdown, architecturally the shutdown * occurs *before* the transition to guest mode. But due to KVM's
[Severity: High]
This isn't a bug introduced by this patch, but looking at the earlier call to kvm_x86_call(leave_smm)() in emulator_leave_smm() in arch/x86/kvm/smm.c:
if (kvm_x86_call(leave_smm)(vcpu, &smram))
return X86EMUL_UNHANDLEABLE;If this fails, it returns directly and bypasses the kvm_leave_nested() cleanup at the bottom of the function.
Can this leave the vCPU in an inconsistent nested guest mode state?
For example, if an unprivileged L1 hypervisor maliciously modifies the SMRAM state (like an invalid CR3) for an active nested VM, could emulator_leave_smm() call svm_leave_smm() -> enter_svm_guest_mode() -> enter_guest_mode(vcpu) and fail in nested_svm_load_cr3()?
If so, would it return an error without leaving guest mode, bypass the kvm_leave_nested() cleanup, and synthesize a nested VM-Exit to L1 instead of properly transitioning L1 to SHUTDOWN?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Sean Christopherson
maintainerPATCH v5 3/6 KVM: x86: Rework kvm_x86_ops.vcpu_pre_run() into .vcpu_needs_initialization()
In reply to Sean ChristophersonRework .vcpu_pre_run() into a more specific .vcpu_needs_initialization() to consolidate the SNP and TDX control flows, and to eliminate the potentially confusing almost-collision between svm_vcpu_pre_run() and pre_sev_run(): the former is SEV specific, but is pre-KVM_RUN, whereas the latter is pre-VMRUN.
--- arch/x86/include/asm/kvm-x86-ops.h | 2 +- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/sev.c | 5 +++++ arch/x86/kvm/svm/svm.c | 12 +----------- arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/vmx/main.c | 10 ++++------ arch/x86/kvm/vmx/tdx.c | 9 +++------ arch/x86/kvm/vmx/vmx.c | 5 ----- arch/x86/kvm/vmx/x86_ops.h | 3 +-- arch/x86/kvm/x86.c | 5 ++++- 10 files changed, 21 insertions(+), 33 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -1
@@ -63,7 +63,7 @@ KVM_X86_OP_OPTIONAL(flush_remote_tlbs_range)#endif KVM_X86_OP(flush_tlb_gva) KVM_X86_OP(flush_tlb_guest) -KVM_X86_OP(vcpu_pre_run) +KVM_X86_OP_OPTIONAL_RET0(vcpu_needs_initialization) KVM_X86_OP(vcpu_run) KVM_X86_OP(handle_exit) KVM_X86_OP(skip_emulated_instruction)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1586,7 +1586,7 @@ struct kvm_x86_ops {*/ void (*flush_tlb_guest)(struct kvm_vcpu *vcpu); -int (*vcpu_pre_run)(struct kvm_vcpu *vcpu); +bool (*vcpu_needs_initialization)(struct kvm_vcpu *vcpu);enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu,u64 run_flags); int (*handle_exit)(struct kvm_vcpu *vcpu,
arch/x86/kvm/svm/sev.c +5 -0
@@ -3556,6 +3556,11 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)__sev_es_unmap_ghcb (svm); } +bool sev_vcpu_needs_initialization (struct kvm_vcpu *vcpu) +{ +return to_kvm_sev_info (vcpu->kvm)->need_init; +} +int pre_sev_run (struct vcpu_svm *svm,int cpu) {struct svm_cpu_data *sd =per_cpu_ptr (&svm_data, cpu);
arch/x86/kvm/svm/svm.c +1 -11
@@ -4400,16 +4400,6 @@ static void svm_cancel_injection(struct kvm_vcpu *vcpu)svm_complete_interrupts (vcpu); } -static int svm_vcpu_pre_run (struct kvm_vcpu *vcpu) -{ -#ifdef CONFIG_KVM_AMD_SEV -if (to_kvm_sev_info (vcpu->kvm)->need_init) -return -EINVAL ; -#endif - -return 1 ; -} -static fastpath_t svm_exit_handlers_fastpath (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu);
@@ -5376,7 +5366,6 @@ struct kvm_x86_ops svm_x86_ops __initdata = { .flush_tlb_gva = svm_flush_tlb_gva, .flush_tlb_guest = svm_flush_tlb_guest, - .vcpu_pre_run = svm_vcpu_pre_run, .vcpu_run = svm_vcpu_run, .handle_exit = svm_handle_exit, .skip_emulated_instruction = svm_skip_emulated_instruction,
@@ -5434,6 +5423,7 @@ struct kvm_x86_ops svm_x86_ops __initdata = {#endif #ifdef CONFIG_KVM_AMD_SEV + .vcpu_needs_initialization = sev_vcpu_needs_initialization, .dev_get_attr = sev_dev_get_attr, .mem_enc_ioctl = sev_mem_enc_ioctl, .mem_enc_register_region = sev_mem_enc_register_region,
arch/x86/kvm/svm/svm.h +1 -0
@@ -979,6 +979,7 @@ void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_arevoid sev_es_unmap_ghcb (struct vcpu_svm *svm);#ifdef CONFIG_KVM_AMD_SEV +bool sev_vcpu_needs_initialization (struct kvm_vcpu *vcpu);int sev_mem_enc_ioctl (struct kvm *kvm,void __user *argp);int sev_mem_enc_register_region (struct kvm *kvm,struct kvm_enc_region *range);
arch/x86/kvm/vmx/main.c +4 -6
@@ -140,12 +140,10 @@ static void vt_vcpu_put(struct kvm_vcpu *vcpu)vmx_vcpu_put (vcpu); } -static int vt_vcpu_pre_run (struct kvm_vcpu *vcpu) +static bool vt_vcpu_needs_initialization (struct kvm_vcpu *vcpu) { -if (is_td_vcpu (vcpu)) -return tdx_vcpu_pre_run (vcpu); - -return vmx_vcpu_pre_run (vcpu); +return is_td_vcpu (vcpu) && +tdx_vcpu_needs_initialization (vcpu); }static fastpath_t vt_vcpu_run (struct kvm_vcpu *vcpu,u64 run_flags)
@@ -949,7 +947,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .flush_tlb_gva = vt_op(flush_tlb_gva), .flush_tlb_guest = vt_op(flush_tlb_guest), - .vcpu_pre_run = vt_op(vcpu_pre_run), + .vcpu_needs_initialization = vt_op_tdx_only(vcpu_needs_initialization), .vcpu_run = vt_op(vcpu_run), .handle_exit = vt_op(handle_exit), .skip_emulated_instruction = vmx_skip_emulated_instruction,
arch/x86/kvm/vmx/tdx.c +3 -6
@@ -897,13 +897,10 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu) tdx->state =VCPU_TD_STATE_UNINITIALIZED ; } -int tdx_vcpu_pre_run (struct kvm_vcpu *vcpu) +bool tdx_vcpu_needs_initialization (struct kvm_vcpu *vcpu) { -if (unlikely (to_tdx (vcpu)->state !=VCPU_TD_STATE_INITIALIZED || -to_kvm_tdx (vcpu->kvm)->state !=TD_STATE_RUNNABLE )) -return -EINVAL ; - -return 1 ; +return to_tdx (vcpu)->state !=VCPU_TD_STATE_INITIALIZED || +to_kvm_tdx (vcpu->kvm)->state !=TD_STATE_RUNNABLE ; } static __always_inline u32 tdcall_to_vmx_exit_reason(struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/vmx.c +0 -5
@@ -6110,11 +6110,6 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)return 1 ; } -int vmx_vcpu_pre_run (struct kvm_vcpu *vcpu) -{ -return 1 ; -} -/* * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
arch/x86/kvm/vmx/x86_ops.h +1 -2
@@ -21,7 +21,6 @@ int vmx_vm_init(struct kvm *kvm);void vmx_vm_destroy (struct kvm *kvm);int vmx_vcpu_precreate (struct kvm *kvm);int vmx_vcpu_create (struct kvm_vcpu *vcpu); -int vmx_vcpu_pre_run (struct kvm_vcpu *vcpu);fastpath_t vmx_vcpu_run (struct kvm_vcpu *vcpu,u64 run_flags);void vmx_vcpu_free (struct kvm_vcpu *vcpu);void vmx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);
@@ -138,7 +137,7 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu);void tdx_vcpu_reset (struct kvm_vcpu *vcpu,bool init_event);void tdx_vcpu_free (struct kvm_vcpu *vcpu);void tdx_vcpu_load (struct kvm_vcpu *vcpu,int cpu); -int tdx_vcpu_pre_run (struct kvm_vcpu *vcpu); +bool tdx_vcpu_needs_initialization (struct kvm_vcpu *vcpu);fastpath_t tdx_vcpu_run (struct kvm_vcpu *vcpu,u64 run_flags);void tdx_prepare_switch_to_guest (struct kvm_vcpu *vcpu);void tdx_vcpu_put (struct kvm_vcpu *vcpu);
arch/x86/kvm/x86.c +4 -1
@@ -8867,12 +8867,15 @@ static int kvm_x86_vcpu_pre_run(struct kvm_vcpu *vcpu) !kvm_apic_init_sipi_allowed(vcpu))return -EINVAL ; +if (kvm_x86_call (vcpu_needs_initialization)(vcpu)) +return -EINVAL ; +if (kvm_x86_call (unhandleable_emulation_required)(vcpu)) {kvm_prepare_emulation_failure_exit (vcpu);return 0 ; } -return kvm_x86_call (vcpu_pre_run)(vcpu); +return 1 ; }int kvm_arch_vcpu_ioctl_run (struct kvm_vcpu *vcpu)
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH v5 4/6 KVM: selftests: Use port 0x80 in invalid nVMX guest state test
In reply to Sean ChristophersonUse port 0x80 instead of 0x2000 to trigger an L2 => L0 VM-Exit in the nVMX invalid guest state test, so that the test can be expanded to cover RSM in the L2 => SMI => L2 path without having to manually encode a MOV to DX (the immediate form of IN only supports an imm8).
Use port 0x80 as it's a well-known port and is used by other tests, e.g. in the coalesced I/O test.
--- .../selftests/kvm/x86/vmx_invalid_nested_guest_state.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c +3 -3
@@ -9,7 +9,7 @@#include "kselftest.h" -#define ARBITRARY_IO_PORT 0x2000 +#define ARBITRARY_IO_PORT 0x80static struct kvm_vm *vm;
@@ -19,8 +19,8 @@ static void l2_guest_code(void)* Generate an exit to L0 userspace, i.e. main(), via I/O to an * arbitrary port. */ - asmvolatile ("inb %%dx, %%al" - : : [port]"d" (ARBITRARY_IO_PORT ) :"rax" ); + asm volatile("inb $" __stringify(ARBITRARY_IO_PORT) ", %%al" + ::: "rax"); }static void l1_guest_code (struct vmx_pages *vmx_pages)
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH v5 5/6 KVM: selftests: Refactor invalid nVMX state test to prepare for RSM testcase
In reply to Sean ChristophersonIn the invalid nVMX guest state test, extract the creation of the VM and initial running of the vCPU to get to L2 into helpers so that the common code can be reused to extend the test to also cover RSM.
Eliminate the unnecessary global "vm", and opportunistically free the VM after the testcase as there's zero reason not to.
Opportunistically assert that L2 is never resumed after the I/O exit to L1, e.g. to guard against false passes.
--- .../kvm/x86/vmx_invalid_nested_guest_state.c | 61 +++++++++++++------ 1 file changed, 42 insertions(+), 19 deletions(-)
tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c +42 -19
@@ -11,8 +11,6 @@#define ARBITRARY_IO_PORT 0x80 -static struct kvm_vm *vm; -static void l2_guest_code (void ) {/*
@@ -21,6 +19,7 @@ static void l2_guest_code(void)*/ asm volatile("inb $" __stringify(ARBITRARY_IO_PORT) ", %%al" ::: "rax"); +GUEST_FAIL ("L2 resumed after stuffing invalid guest state" ); }static void l1_guest_code (struct vmx_pages *vmx_pages)
@@ -46,35 +45,50 @@ static void l1_guest_code(struct vmx_pages *vmx_pages)GUEST_DONE (); } -int main (int argc,char *argv[]) +static void vcpu_run_to_io (struct kvm_vcpu *vcpu,bool want_l2) +{ +struct kvm_run *run = vcpu->run; + +vcpu_run (vcpu); + +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_IO ); + +TEST_ASSERT (run->io.port ==ARBITRARY_IO_PORT && + (!!(run->flags &KVM_RUN_X86_GUEST_MODE ) == want_l2 || + !kvm_has_cap (KVM_CAP_X86_GUEST_MODE )), +"Expected IN from port 0x%x from L%u, got port 0x%x from L%u" , +ARBITRARY_IO_PORT ,1 + want_l2, run->io.port, +1 + !!(run->flags &KVM_RUN_X86_GUEST_MODE )); +} + +static struct kvm_vm *vm_create_and_run_l2 (struct kvm_vcpu **vcpu) {gva_t vmx_pages_gva; -struct kvm_sregs sregs; -struct kvm_vcpu *vcpu; -struct kvm_run *run; -struct ucall uc; +struct kvm_vm *vm; -TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX )); - - vm =vm_create_with_one_vcpu (&vcpu, l1_guest_code); + vm =vm_create_with_one_vcpu (vcpu, l1_guest_code);/* Allocate VMX pages and shared descriptors (vmx_pages). */ vcpu_alloc_vmx (vm, &vmx_pages_gva); -vcpu_args_set (vcpu,1 , vmx_pages_gva); - -vcpu_run (vcpu); - - run = vcpu->run; +vcpu_args_set (*vcpu,1 , vmx_pages_gva);/* * The first exit to L0 userspace should be an I/O access from L2. * Running L1 should launch L2 without triggering an exit to userspace. */ -TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_IO ); +vcpu_run_to_io (*vcpu, true); -TEST_ASSERT (run->io.port ==ARBITRARY_IO_PORT , -"Expected IN from port %d from L2, got port %d" , -ARBITRARY_IO_PORT , run->io.port); +return vm; +} + +static void test_invalid_l2_guest_state (void ) +{ +struct kvm_sregs sregs; +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; + + vm =vm_create_and_run_l2 (&vcpu);/* * Stuff invalid guest state for L2 by making TR unusable. The next
@@ -96,4 +110,13 @@ int main(int argc, char *argv[])default :TEST_FAIL ("Unexpected ucall: %lu" , uc.cmd); } + +kvm_vm_free (vm); +} + +int main (int argc,char *argv[]) +{ +TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX )); + +test_invalid_l2_guest_state (); }
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Sean Christopherson
maintainerPATCH v5 6/6 KVM: selftests: Extend the invalid nVMX guest state test to cover RSM
In reply to Sean ChristophersonFrom: Hao Zhang <[email protected]>
Extend the invalid nVMX guest state to cover RSM, i.e. to validate that KVM synthesizes SHUTDOWN for L1 if SMRAM is clobbered with invalid guest state during an L2 => SMI => RSM => L2 sequence.
Note, unlike the existing testcase, clobbering SMRAM should result in L1, not L2, getting SHUTDOWN / TRIPLE_FAULT, as RSM is architecturally defined to trigger shutdown if the CPU detects invalid state.
--- .../kvm/x86/vmx_invalid_nested_guest_state.c | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+)
tools/testing/selftests/kvm/x86/vmx_invalid_nested_guest_state.c +53 -0
@@ -2,6 +2,7 @@#include "test_util.h" #include "kvm_util.h" #include "processor.h" +#include "smm.h" #include "vmx.h" #include <string.h>
@@ -11,6 +12,22 @@#define ARBITRARY_IO_PORT 0x80 +/* +* The 64-bit SMRAM state-save area starts at SMBASE + 0xfe00. TR starts at +* offset 0xfe90, and attributes is the second 16-bit field in the descriptor. +*/ +#define SMRAM64_TR_ATTRIBUTES_OFFSET 0xfe92 +#define SMRAM_GPA 0x1000000 + +/* +* SMI handler that runs in 16-bit Real Mode. Syncs with L0 via port I/O, then +* executes RSM to trigger the consumption of invalid guest state. +*/ +static u8 smi_handler[] = { +0xe4 ,ARBITRARY_IO_PORT ,/* IN $ARBITRARY_IO_PORT, %al */ +0x0f ,0xaa ,/* RSM */ +}; +static void l2_guest_code (void ) {/*
@@ -114,9 +131,45 @@ static void test_invalid_l2_guest_state(void)kvm_vm_free (vm); } +static void test_invalid_l2_guest_state_rsm (void ) +{ +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +u16 *tr_attrs; + +if (!kvm_has_cap (KVM_CAP_X86_SMM )) +return ; + + vm =vm_create_and_run_l2 (&vcpu); + +/* +* Inject SMI while L2 is active, run the vCPU to get I/O exit from L1, +* then stuff TR in the SMRAM state-save area so that RSM restores +* invalid L2 state. +*/ +setup_smram (vm, vcpu,SMRAM_GPA , smi_handler,sizeof (smi_handler)); +inject_smi (vcpu); + +vcpu_run_to_io (vcpu, false); + +/* Clear the present bit in SMRAM to make TR unusable. */ + tr_attrs =addr_gpa2hva (vm,SMRAM_GPA +SMRAM64_TR_ATTRIBUTES_OFFSET ); + *tr_attrs &= ~BIT (7 ); + +vcpu_run (vcpu); + +/* +* For RSM, L1 gets the SHUTDOWN because RSM is architecturally defined +* to result in shutdown if the CPU detects invalid state in SMRAM. +*/ +TEST_ASSERT_KVM_EXIT_REASON (vcpu,KVM_EXIT_SHUTDOWN ); +kvm_vm_free (vm); +} +int main (int argc,char *argv[]) {TEST_REQUIRE (kvm_cpu_has (X86_FEATURE_VMX ));test_invalid_l2_guest_state (); +test_invalid_l2_guest_state_rsm (); }
Signature · 1 line
2.55.0.508.g3f0d502094-goog
Quoted 10 lines · Sean Christopherson
Synthesize SHUTDOWN if invalid guest state is detected a RSM, i.e. if SMRAM is clobbered by L1 (or host userspace) while handling an SMI that occurred while L2 was active. This fixes yet another case of syzkaller tripping KVM's sanity check that KVM doesn't cancel a pending nested VM-Enter.
v5:
- Fix goofs in patch 1. [Sashiko]
- Print out correct target L1 vs. L2. [Sashiko][...]
Applied to kvm-x86 misc, thanks!
[1/6] KVM: x86: Extract VMX's unhandleable emulation check to common x86
https://github.com/kvm-x86/linux/commit/a40764607eab [2/6] KVM: nVMX: Synthesize SHUTDOWN on RSM if L2 requires emulation https://github.com/kvm-x86/linux/commit/281a4e5f1086 [3/6] KVM: x86: Rework kvm_x86_ops.vcpu_pre_run() into .vcpu_needs_initialization() https://github.com/kvm-x86/linux/commit/9fd7a4433d74 [4/6] KVM: selftests: Use port 0x80 in invalid nVMX guest state test https://github.com/kvm-x86/linux/commit/6fc510e1f101 [5/6] KVM: selftests: Refactor invalid nVMX state test to prepare for RSM testcase https://github.com/kvm-x86/linux/commit/2cb49770e28f [6/6] KVM: selftests: Extend the invalid nVMX guest state test to cover RSM https://github.com/kvm-x86/linux/commit/bc44a9ce73cd