KVM: x86: Fix DR6.BLD regression in guests
Subsystems (2)
cover KVM: x86: Fix DR6.BLD regression in guests
Hi,
a58917a271cb ("KVM: x86: Preserve DR6.BLD (Bus Lock Detect) when delivering #DB payload"), upstream 719a1d58bf43, went into 6.18.61 without the rest of its upstream series. Since then kvm-unit-tests x86/debug fails on hosts with Bus Lock Detect when the guest CPU model doesn't have it, e.g. the default qemu64:
FAIL: Wanted #DB on 0x400da7 w/ DR6 = 0xffff0ff4, got 1 #DBs, addr[0] = 0x400da7, DR6 = 0xffff07f4
...
SUMMARY: 25 tests, 17 unexpected failures
Once KVM stops intercepting DR accesses, the guest's write of 0 to DR6 goes to hardware and clears BLD. sync_dirty_debug_regs() copies that into vcpu->arch.dr6, and since a58917a271cb the #DB payload path no longer sets BLD back, so the guest sees it cleared.
Later patches in the same series fix this upstream (v7.4-rc1). This backports those two fixes and the two cleanups they depend on. Patch 3 needed a small change: 6.18 has no arch/x86/kvm/regs.h, so the new declaration goes in asm/kvm_host.h.
Tested on an Intel host with bus_lock_detect with a 6.18.64-based kernel whose DR6 handling matches v6.18.64: x86/debug goes from 17 failures to all 25 passing.
7.3.y, 6.12.y, 6.6.y and 6.1.y picked up the same commit (in 7.3.2, 6.12.117, 6.6.163 and 6.1.193) and probably need these too. I haven't tested those, but can send backports if wanted.
Thanks,
Baoli
Sean Christopherson (4):
KVM: x86: Rename kvm_dr6_fixed() => kvm_get_dr6_fixed_1()
KVM: Return an "unsigned long", not "u64" for the fixed-1 DR6 bits
KVM: x86: Force fixed-1 bits in DR6 after synchronizing with hardware
KVM: x86: Set guest's fixed-1 DR6 bits when delivering #DB payload
arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/regs.c | 7 ++++--- arch/x86/kvm/x86.c | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/4 KVM: x86: Rename kvm_dr6_fixed() => kvm_get_dr6_fixed_1()
From: Sean Christopherson <[email protected]>
commit cc551e8d4f77c278fd0bb0ed45d1103cb5a90889 upstream.
Rename kvm_dr6_fixed() to kvm_get_dr6_fixed_1() to make it more clear that the helper retrieves the fixed-1 DR6 bits for a given vCPU.
No functional change intended.
--- arch/x86/kvm/regs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/regs.c +2 -2
@@ -679,7 +679,7 @@ void kvm_update_dr7(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_update_dr7); -static u64 kvm_dr6_fixed (struct kvm_vcpu *vcpu) +static u64 kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) {u64 fixed =DR6_FIXED_1 ;
@@ -705,7 +705,7 @@ int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)case 6 :if (!kvm_dr6_valid (val))return 1 ;/* #GP */ - vcpu->arch.dr6 = (val &DR6_VOLATILE ) |kvm_dr6_fixed (vcpu); + vcpu->arch.dr6 = (val &DR6_VOLATILE ) |kvm_get_dr6_fixed_1 (vcpu);break ;case 5 :default :/* 7 */
Signature · 1 line
2.43.0
PATCH 2/4 KVM: Return an "unsigned long", not "u64" for the fixed-1 DR6 bits
In reply to Baoli ZhangFrom: Sean Christopherson <[email protected]>
commit 28bb033664eef3c18c6acb2c075fc01d7ada9296 upstream.
Return an "unsigned long" instead of a "u64" from the helper to get the fixed-1 DR6 bits, as debug registers are natural width registers, not 64-bit registers.
No functional change intended (the fixed-1 bits only set bits in 31:0, and the sole caller truncates the value to an "unsigned long").
--- arch/x86/kvm/regs.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/regs.c +3 -2
@@ -679,15 +679,16 @@ void kvm_update_dr7(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_update_dr7); -static u64 kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) +static unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) { -u64 fixed =DR6_FIXED_1 ; +unsigned long fixed =DR6_FIXED_1 ;if (!guest_cpu_cap_has (vcpu,X86_FEATURE_RTM )) fixed |=DR6_RTM ;if (!guest_cpu_cap_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT )) fixed |=DR6_BUS_LOCK ; +return fixed; }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 6.18.y 2/4] KVM: Return an "unsigned long", not "u64" for the fixed-1 DR6 bits
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] KVM: x86: missing DR6 fixed-1 bit…
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] KVM: x86: missing DR6 fixed-1 bit enforcement in KVM_SET_DEBUGREGS [1]
--
commit 6ea58ce45be6bc8f3686d6726ca7358f4937978d
Author: Sean Christopherson <[email protected]>
KVM: Return an "unsigned long", not "u64" for the fixed-1 DR6 bits
This commit changes the return type and local variable of kvm_get_dr6_fixed_1() from u64 to unsigned long to match the natural width of debug registers.
Quoted 12 lines
arch/x86/kvm/regs.c +1
-2@@ -679,15 +679,16 @@ void kvm_update_dr7(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_update_dr7); -static u64 kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) +static unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) { -u64 fixed =DR6_FIXED_1 ;
arch/x86/kvm/regs.c void kvm_update_dr7(struct kvm_vcpu *vcpu) · line 693
Quoted 10 lines
arch/x86/kvm/regs.c +2
-0+unsigned long fixed =DR6_FIXED_1 ;if (!guest_cpu_cap_has (vcpu,X86_FEATURE_RTM )) fixed |=DR6_RTM ;if (!guest_cpu_cap_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT )) fixed |=DR6_BUS_LOCK ; +return fixed; }
[Severity: Critical]
This problem wasn't introduced by this patch, but can kvm_vcpu_ioctl_x86_set_debugregs() and kvm_deliver_exception_payload() leave dynamically fixed-1 bits cleared in vcpu->arch.dr6 [1]?
kvm_vcpu_ioctl_x86_set_debugregs() assigns dbgregs->dr6 directly to vcpu->arch.dr6 without ORing in kvm_get_dr6_fixed_1(vcpu), and kvm_deliver_exception_payload() applies static DR6 masks when delivering a DB_VECTOR payload rather than enforcing the dynamic fixed-1 bits (DR6_RTM and DR6_BUS_LOCK when the guest lacks those CPUID features):
arch/x86/kvm/regs.c:kvm_vcpu_ioctl_x86_set_debugregs() {
...
kvm_update_dr0123(vcpu);
vcpu->arch.dr6 = dbgregs->dr6;
vcpu->arch.dr7 = dbgregs->dr7;
...
}If userspace clears these bits via KVM_SET_DEBUGREGS or injects a #DB payload that clears them, vcpu->arch.dr6 violates the fixed-1 invariant and can trigger a host kernel crash or denial of service when subsequent sanity checks evaluate DR6.
[1] https://sashiko.dev/#/bug/linux-aad38d43-73ef-489e-897a-07392877a8fb
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/4 KVM: x86: Force fixed-1 bits in DR6 after synchronizing with hardware
In reply to Baoli ZhangFrom: Sean Christopherson <[email protected]>
commit 7b88577a0983af221a78fc66c56532eb97d8fd2f upstream.
Set all fixed-1 bits in KVM's version of DR6 after synchronizing with hardware to paper over as much of the virtualization hole as possible. Because KVM dynamically disables DR interception, a guest can write any DR6 bit that isn't fixed in bare metal, even if the bit is supposed to be fixed from the guest's perspective.
In addition to providing some amount of consistency in KVM, this will allow adding sanity checks that KVM never ends up with fixed-1 bits clear in its version of DR6.
[ Baoli: 6.18 has no arch/x86/kvm/regs.h, declare kvm_get_dr6_fixed_1()
in arch/x86/include/asm/kvm_host.h next to kvm_get_dr() instead. ]
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/regs.c | 2 +- arch/x86/kvm/x86.c | 1 + 3 files changed, 3 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -2249,6 +2249,7 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);int kvm_set_cr8 (struct kvm_vcpu *vcpu,unsigned long cr8);int kvm_set_dr (struct kvm_vcpu *vcpu,int dr,unsigned long val);unsigned long kvm_get_dr (struct kvm_vcpu *vcpu,int dr); +unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu);unsigned long kvm_get_cr8 (struct kvm_vcpu *vcpu);void kvm_lmsw (struct kvm_vcpu *vcpu,unsigned long msw);int __kvm_set_xcr (struct kvm_vcpu *vcpu,u32 index,u64 xcr);
arch/x86/kvm/regs.c +1 -1
@@ -679,7 +679,7 @@ void kvm_update_dr7(struct kvm_vcpu *vcpu) }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_update_dr7); -static unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) +unsigned long kvm_get_dr6_fixed_1 (struct kvm_vcpu *vcpu) {unsigned long fixed =DR6_FIXED_1 ;
arch/x86/kvm/x86.c +1 -0
@@ -11042,6 +11042,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)WARN_ON (vcpu->arch.switch_db_regs &KVM_DEBUGREG_AUTO_SWITCH );kvm_x86_call (sync_dirty_debug_regs)(vcpu);kvm_update_dr0123 (vcpu); + vcpu->arch.dr6 |=kvm_get_dr6_fixed_1 (vcpu);kvm_update_dr7 (vcpu); }
Signature · 1 line
2.43.0
PATCH 4/4 KVM: x86: Set guest's fixed-1 DR6 bits when delivering #DB payload
In reply to Baoli ZhangFrom: Sean Christopherson <[email protected]>
commit c62ffee78db8837a6251af83798907af37b53ed4 upstream.
When delivering a #DB payload, set all DR6 bits that are fixed-1 from the guest's perspective, not just the host's perspective. Don't WARN on a "bad" payload (or existing DR6) even though KVM itself shouldn't set DR6 or synthesize a payload without the fixed-1 bits set, as userspace can provide a completely arbitrary payload via KVM_SET_VCPU_EVENTS (and "hardening" that flow is not worth the effort as there's no benefit in practice).
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -809,7 +809,7 @@ void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu,* breakpoint), it is reserved and must be zero in DR6. */ vcpu->arch.dr6 &= ~BIT (12 ); - vcpu->arch.dr6 |=DR6_FIXED_1 ; + vcpu->arch.dr6 |=kvm_get_dr6_fixed_1 (vcpu);break ;case PF_VECTOR : vcpu->arch.cr2 = ex->payload;
Signature · 1 line
2.43.0