KVM: nVMX: Don't clobber VM-Entry instruction length on soft INTn exits
cover KVM: nVMX: Don't clobber VM-Entry instruction length on soft INTn exits
From: Mahmoud Abumandour <[email protected]>
KVM writes the length of a soft INTn that L2 was vectoring at the time of a nested VM-Exit into vmcs12's VM-Entry instruction length, an L1-owned control, instead of the VM-Exit instruction length. Patch 1 fixes it and patch 2 adds a selftest.
Found with a fuzzer for VMX isolation. The selftest patch was written with the help of an LLM coding assistant (Claude Opus 5.5). kvm-x86/next was run as a nested hypervisor under QEMU. The selftest passes only with patch 1, and other nested VMX selftests are unaffected.
Mahmoud Abumandour (2):
KVM: nVMX: Don't clobber VM-Entry instruction length when saving soft
INTn
KVM: selftests: Add test for nested VM-Exits while vectoring soft INTs
arch/x86/kvm/vmx/nested.c | 2 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../kvm/x86/vmx_soft_int_vectoring_test.c | 105 ++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/kvm/x86/vmx_soft_int_vectoring_test.c
base-commit: dea4f6380f4ec96e8f3b0aaf2343764bd0d25ef4
Signature · 1 line
2.43.0
PATCH 1/2 KVM: nVMX: Don't clobber VM-Entry instruction length when saving soft INTn
From: Mahmoud Abumandour <[email protected]>
When a VM-Exit from L2 occurs while vectoring a software interrupt, write the instruction length to vmcs12's VM-Exit instruction length field, as is done for software exceptions, not to the VM-Entry instruction length field, which belongs to L1.
Found with a differential testing harness for VMX isolation.
--- arch/x86/kvm/vmx/nested.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/nested.c +1 -1
@@ -4016,7 +4016,7 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,if (vcpu->arch.interrupt.soft) { idt_vectoring |=INTR_TYPE_SOFT_INTR ; - vmcs12->vm_entry_instruction_len = + vmcs12->vm_exit_instruction_len = vcpu->arch.event_exit_inst_len; }else idt_vectoring |=INTR_TYPE_EXT_INTR ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 1/2] KVM: nVMX: Don't clobber VM-Entry instruction length when saving soft INTn
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kvm: nVMX: vm_exit_instruction_len overwritten in prepare_vmcs12() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: nVMX: vm_exit_instruction_len overwritten in prepare_vmcs12() --
commit 24bdb8dd4aa6675d8e53850c4d0acf71d5903e75
Author: Mahmoud Abumandour <[email protected]>
KVM: nVMX: Don't clobber VM-Entry instruction length when saving soft INTn
This patch updates vmcs12_save_pending_event() so that when a nested VM-Exit occurs while vectoring a software interrupt, vcpu->arch.event_exit_inst_len is written to vmcs12->vm_exit_instruction_len instead of vmcs12->vm_entry_instruction_len.
arch/x86/kvm/vmx/nested.c static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu, · line 4022
Quoted 13 lines
arch/x86/kvm/vmx/nested.c +1
-1@@ -4016,7 +4016,7 @@ static void vmcs12_save_pending_event(struct kvm_vcpu *vcpu,if (vcpu->arch.interrupt.soft) { idt_vectoring |=INTR_TYPE_SOFT_INTR ; - vmcs12->vm_entry_instruction_len = + vmcs12->vm_exit_instruction_len = vcpu->arch.event_exit_inst_len; }else idt_vectoring |=INTR_TYPE_EXT_INTR ;
[Severity: Medium]
Does writing vmcs12->vm_exit_instruction_len in vmcs12_save_pending_event() actually take effect?
prepare_vmcs12() in arch/x86/kvm/vmx/nested.c is the sole caller of vmcs12_save_pending_event(), and since commit c3634d25fbee ("KVM: nVMX: Leave most VM-Exit info fields unmodified on failed VM-Entry"), it unconditionally overwrites vmcs12->vm_exit_instruction_len with exit_insn_len right after vmcs12_save_pending_event() returns:
arch/x86/kvm/vmx/nested.c:prepare_vmcs12() {
...
vmcs12_save_pending_event(vcpu, vmcs12,
vm_exit_reason, exit_intr_info);
vmcs12->vm_exit_intr_info = exit_intr_info;
vmcs12->vm_exit_instruction_len = exit_insn_len;
vmcs12->vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
...
}This also affects the existing kvm_exception_is_soft(nr) branch in vmcs12_save_pending_event():
arch/x86/kvm/vmx/nested.c:vmcs12_save_pending_event() {
...
if (kvm_exception_is_soft(nr)) {
vmcs12->vm_exit_instruction_len =
vcpu->arch.event_exit_inst_len;
idt_vectoring |= INTR_TYPE_SOFT_EXCEPTION;
} else
...
}When a nested VM-Exit is synthesized while a software interrupt or software exception is being vectored and vcpu->arch.event_exit_inst_len differs from exit_insn_len (for instance, after vmx_cancel_injection() or after state restore via KVM_SET_NESTED_STATE and KVM_SET_VCPU_EVENTS prior to kvm_check_nested_events() -> nested_vmx_inject_exception_vmexit()), won't prepare_vmcs12() clobber vmcs12->vm_exit_instruction_len with a stale or zero exit_insn_len?
Should prepare_vmcs12() assign vmcs12->vm_exit_instruction_len = exit_insn_len before calling vmcs12_save_pending_event() so that soft interrupts and soft exceptions can override it?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/2 KVM: selftests: Add test for nested VM-Exits while vectoring soft INTs
In reply to [email protected]From: Mahmoud Abumandour <[email protected]>
Verify that a VM-Exit from L2 that occurs while vectoring INT3 or INTn reports the instruction length in the VM-Exit instruction length field and leaves L1's VM-Entry instruction length untouched.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../kvm/x86/vmx_soft_int_vectoring_test.c | 105 ++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/vmx_soft_int_vectoring_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -140,6 +140,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_soft_int_vectoring_test TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test TEST_GEN_PROGS_x86 += x86/xapic_ipi_test TEST_GEN_PROGS_x86 += x86/xapic_state_test
tools/testing/selftests/kvm/x86/vmx_soft_int_vectoring_test.c new file +105 -0
105 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 2/2] KVM: selftests: Add test for nested VM-Exits while vectoring soft INTs
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] KVM: selftests: standalone test duplicates nested_exceptions_test setup --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM: selftests: standalone test duplicates nested_exceptions_test setup --
commit 419533e2e5a5342a3cc80d072a4cecd9f800a093
Author: Mahmoud Abumandour <[email protected]>
KVM: selftests: Add test for nested VM-Exits while vectoring soft INTs
This commit adds a standalone KVM x86 selftest to verify that a nested VM-Exit occurring while L2 vectors a software exception or interrupt reports the instruction length in VM_EXIT_INSTRUCTION_LEN without modifying VM_ENTRY_INSTRUCTION_LEN.
Quoted 17 lines
tools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -140,6 +140,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_soft_int_vectoring_test TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test TEST_GEN_PROGS_x86 += x86/xapic_ipi_test TEST_GEN_PROGS_x86 += x86/xapic_state_testtools/testing/selftests/kvm/x86/vmx_soft_int_vectoring_test.c new file +0
-0
[ ... ]
Quoted 15 lines
tools/testing/selftests/kvm/x86/vmx_soft_int_vectoring_test.c new file +15
-0+static void l1_guest_code (struct vmx_pages *vmx_pages) +{ +prepare_for_vmx_operation (vmx_pages); +load_vmcs (vmx_pages); +prepare_vmcs (vmx_pages, l2_guest_code); + +/* +* Put every IDT entry beyond the limit so that vectoring any event in +* L2 generates #GP, and intercept #GP so that the VM-Exit occurs while +* the original event is being vectored. +*/ +vmwrite (GUEST_IDTR_LIMIT ,0 ); +vmwrite (EXCEPTION_BITMAP ,BIT (GP_VECTOR )); +vmwrite (VM_ENTRY_INSTRUCTION_LEN ,ENTRY_INSN_LEN ); +
tools/testing/selftests/kvm/x86/vmx_soft_int_vectoring_test.c line 76
Quoted 10 lines
tools/testing/selftests/kvm/x86/vmx_soft_int_vectoring_test.c new file +10
-0+vmlaunch (); +l1_assert_vectoring_exit ("INT3" ,INTR_TYPE_SOFT_EXCEPTION |BP_VECTOR ,1 ); + +vmresume (); +l1_assert_vectoring_exit ("INTn" ,INTR_TYPE_SOFT_INTR |SOFT_INT_VECTOR ,2 ); + +vmresume (); +GUEST_ASSERT_EQ (vmread (VM_EXIT_REASON ),EXIT_REASON_VMCALL ); +GUEST_DONE (); +}
[Severity: Medium]
This isn't a bug, but l1_guest_code() in tools/testing/selftests/kvm/x86/vmx_soft_int_vectoring_test.c duplicates the nested VMCS setup, empty GUEST_IDTR_LIMIT configuration, and GP_VECTOR interception in EXCEPTION_BITMAP that l1_vmx_code() in tools/testing/selftests/kvm/x86/nested_exceptions_test.c already sets up to trigger nested VM-Exits during L2 event vectoring:
tools/testing/selftests/kvm/x86/nested_exceptions_test.c:l1_vmx_code() {
prepare_for_vmx_operation(vmx);
load_vmcs(vmx);
prepare_vmcs(vmx, NULL);
vmwrite(GUEST_IDTR_LIMIT, 0);
...
vmwrite(EXCEPTION_BITMAP, INTERCEPT_SS_GP_DF);
...
}Could the INT3 and INTn vectoring checks in l2_guest_code() and l1_assert_vectoring_exit() be added to tools/testing/selftests/kvm/x86/nested_exceptions_test.c instead of adding a separate x86/vmx_soft_int_vectoring_test target to TEST_GEN_PROGS_x86 in tools/testing/selftests/kvm/Makefile.kvm?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2