Skip to contentSkip to conversation

KVM: nSVM: Restore L1's debug registers on nested VM-Exit

PATCH In review Hao Zhang 3 participants 5 messages Aug 27 – Aug 28

Subsystems (4)
lore
Share & tools
lore

Hao Zhang

author
#0 lore raw

PATCH 1/2 KVM: nSVM: Restore L1's debug registers on nested VM-Exit

From: Hao Zhang <[email protected]>

Restore L1's DR6 and DR7 when emulating a nested VM-Exit from L2 to L1. KVM loads L2's debug register state from vmcb12 on nested VMRUN, but currently restores DR7 to its reset value and leaves DR6 untouched when switching back to vmcb01. As a result, L1 can observe stale L2 DR6 state and lose its DR7 state after an L2 VM-Exit.

Restore both registers from vmcb01, matching the rest of the L1 processor state restored on nested VM-Exit.

Fixes 4995a3685f1b ("KVM: SVM: Use a separate vmcb for the nested L2 guest")
---
 arch/x86/kvm/svm/nested.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +2 -1
index 73f37b050d0a..e7975ac7bfca 100644
@@ -1427,7 +1427,8 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
 	kvm_rsp_write(vcpu, vmcb01->save.rsp);
 	kvm_rip_write(vcpu, vmcb01->save.rip);
 
-	svm->vcpu.arch.dr7 = DR7_FIXED_1;
+	svm->vcpu.arch.dr6 = vmcb01->save.dr6;
+	svm->vcpu.arch.dr7 = vmcb01->save.dr7;
 	kvm_update_dr7(&svm->vcpu);
 
 	nested_svm_transition_tlb_flush(vcpu);

base-commit: 45c13f3f9e3bb15fd89ff2864c6f627a3b4b4229

Signature · 1 line
2.15.0
author
#1 lore raw

PATCH 2/2 selftests: kvm: Add regression test for nSVM debug register state

From: Hao Zhang <[email protected]>

Add a regression test for debug register state restoration when KVM emulates a nested SVM VM-Exit from L2 to L1.

Give L1 and L2 distinct DR6 and DR7 values, run L2 until a VMMCALL VM-Exit, and then verify that KVM saved L2's debug register state to vmcb12 while restoring L1's architectural DR6 and DR7 values. Without the fix, L1 observes stale L2 DR6 state and loses its DR7 state after the nested VM-Exit.

---
 tools/testing/selftests/kvm/Makefile.kvm           |   1 +
 .../selftests/kvm/x86/svm_nested_debug_regs_test.c | 104 +++++++++++++++++++++
 2 files changed, 105 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_nested_debug_regs_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 1bde5ab306cf..65ad842eecd4 100644
@@ -122,6 +122,7 @@ TEST_GEN_PROGS_x86 += x86/svm_nested_clear_efer_svme
 TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_vmcb12_gpa
+TEST_GEN_PROGS_x86 += x86/svm_nested_debug_regs_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_pat_test
 TEST_GEN_PROGS_x86 += x86/svm_lbr_nested_state
 TEST_GEN_PROGS_x86 += x86/svm_pmu_host_guest_test
tools/testing/selftests/kvm/x86/svm_nested_debug_regs_test.c new file +104 -0

104 lines not on this page · raw message

Signature · 1 line
2.15.0
#2 lore raw
In reply to Hao Zhang
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Restoring L1's architectural DR6 and DR7 from the hardware `vmcb01->save` state on nested VM-Exit…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Restoring L1's architectural DR6 and DR7 from the hardware vmcb01->save state on nested VM-Exit corrupts L1's debug state and leaks host debugging information. --

commit cba4fc9e5c8bde0943c94c7835fb0e3712e3df8e
Author: Hao Zhang <[email protected]>

KVM: nSVM: Restore L1's debug registers on nested VM-Exit

This commit restores L1's DR6 and DR7 when emulating a nested VM-Exit from L2 to L1. It copies the debug states from vmcb01 to prevent L1 from observing stale L2 state or losing its DR7 state.

arch/x86/kvm/svm/nested.c void nested_svm_vmexit(struct vcpu_svm *svm) · line 1431

Quoted 11 lines
arch/x86/kvm/svm/nested.c +2 -1
index 73f37b050d0a0..e7975ac7bfca2 100644
@@ -1427,7 +1427,8 @@ void nested_svm_vmexit(struct vcpu_svm *svm)
 	kvm_rsp_write(vcpu, vmcb01->save.rsp);
 	kvm_rip_write(vcpu, vmcb01->save.rip);
 
-	svm->vcpu.arch.dr7 = DR7_FIXED_1;
+	svm->vcpu.arch.dr6 = vmcb01->save.dr6;
+	svm->vcpu.arch.dr7 = vmcb01->save.dr7;

[Severity: High]
Does copying the hardware state from vmcb01->save back into the software structures here in nested_svm_vmexit() corrupt L1's architectural debug state?

If KVM intercepts debug registers (which is the default) or if the host is actively debugging the guest, vmcb01->save contains host-controlled values like DR6_ACTIVE_LOW or vcpu->arch.guest_debug_dr7.

Overwriting L1's software-tracked DR6 destroys pending L1 debug exceptions, while pulling the host-polluted DR7 permanently leaks host-level breakpoints to the unprivileged L1 guest.

Quoted 3 lines
arch/x86/kvm/svm/nested.c +0 -0
 	kvm_update_dr7(&svm->vcpu);
 
 	nested_svm_transition_tlb_flush(vcpu);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw
In reply to Hao Zhang
Quoted 7 lines · Hao Zhang

From: Hao Zhang <[email protected]>

Restore L1's DR6 and DR7 when emulating a nested VM-Exit from L2 to L1. KVM loads L2's debug register state from vmcb12 on nested VMRUN, but currently restores DR7 to its reset value and leaves DR6 untouched when switching back to vmcb01. As a result, L1 can observe stale L2 DR6 state and lose its DR7 state after an L2 VM-Exit.

Well, yeah, because the APM says so. The APM very clearly says:

Disables all breakpoints in the host DR7 register.

In typical APM fashion, it says nothing about DR6, so it's reasonable to assume it's preserved as-is across #VMEXIT, i.e. is L1's responsibility to ignore since there's no #DB.

Quoted 2 lines

Restore both registers from vmcb01, matching the rest of the L1 processor state restored on nested VM-Exit.

What happens to other state is irrelevant, what matters is what the architecture says happens.

author
#4 lore raw
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Hao Zhang

From: Hao Zhang <[email protected]>

Restore L1's DR6 and DR7 when emulating a nested VM-Exit from L2 to L1. KVM loads L2's debug register state from vmcb12 on nested VMRUN, but currently restores DR7 to its reset value and leaves DR6 untouched when switching back to vmcb01. As a result, L1 can observe stale L2 DR6 state and lose its DR7 state after an L2 VM-Exit.

Quoted 8 lines

Well, yeah, because the APM says so. The APM very clearly says:

Disables all breakpoints in the host DR7 register.

In typical APM fashion, it says nothing about DR6, so it's reasonable to assume it's preserved as-is across #VMEXIT, i.e. is L1's responsibility to ignore since there's no #DB.

Thanks. I checked the APM which requires #VMEXIT disables all breakpoints in the host DR7 register, which means KVM's DR7_FIXED_1 restore on nested VM-Exit is intentional architectural behavior for L1-as-host, not a lost-state bug.

I'll drop this patch as-is.

Thanks,
Hao

Quoted 5 lines
Quoted 2 lines

Restore both registers from vmcb01, matching the rest of the L1 processor state restored on nested VM-Exit.

What happens to other state is irrelevant, what matters is what the architecture says happens.