KVM: x86: Fix fault-related bugs in LTR/LLDT emulation
Sean Christopherson
maintainercover KVM: x86: Fix fault-related bugs in LTR/LLDT emulation
Patch 1 fixes a bug found by syzkaller where KVM attempts to set the TSS.busy bit during LTR before checking that the new TSS.base is valid.
Patch 2 fixes a bug found by inspection (when reading the APM to verify the non-canonical logic is correct) where KVM doesn't provide the correct error code if the new TSS.base is non-canonical.
Patch 3 makes the "dangling userspace I/O" WARN_ON two separate WARN_ON_ONCE so that a KVM bug doesn't spam the kernel log (keeping the WARN is desirable specifically to detect these types of bugs).
Sean Christopherson (3):
KVM: x86: Mark TSS busy during LTR emulation _after_ all fault checks
KVM: x86: Set error code to segment selector on LLDT/LTR non-canonical
#GP
KVM: x86: WARN only once if KVM leaves a dangling userspace I/O
request
arch/x86/kvm/emulate.c | 23 +++++++++++------------ arch/x86/kvm/x86.c | 6 ++++-- 2 files changed, 15 insertions(+), 14 deletions(-)
base-commit: b9b71f43683ae9d76b0989249607bbe8c9eb6c5c
Signature · 1 line
2.37.0.144.g8ac04bfd2-goog
Sean Christopherson
maintainerPATCH 1/3 KVM: x86: Mark TSS busy during LTR emulation _after_ all fault checks
Wait to mark the TSS as busy during LTR emulation until after all fault checks for the LTR have passed. Specifically, don't mark the TSS busy if the new TSS base is non-canonical.
Opportunistically drop the one-off !seg_desc.PRESENT check for TR as the only reason for the early check was to avoid marking a !PRESENT TSS as busy, i.e. the common !PRESENT is now done before setting the busy bit.
--- arch/x86/kvm/emulate.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
arch/x86/kvm/emulate.c +9 -10
@@ -1699,16 +1699,6 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,case VCPU_SREG_TR :if (seg_desc.s || (seg_desc.type !=1 && seg_desc.type !=9 )) goto exception; -if (!seg_desc.p) { - err_vec =NP_VECTOR ; - goto exception; - } - old_desc = seg_desc; - seg_desc.type |=2 ;/* busy */ - ret = ctxt->ops->cmpxchg_emulated (ctxt, desc_addr, &old_desc, &seg_desc, -sizeof (seg_desc), &ctxt->exception); -if (ret !=X86EMUL_CONTINUE ) -return ret;break ;case VCPU_SREG_LDTR :if (seg_desc.s || seg_desc.type !=2 )
@@ -1749,6 +1739,15 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, ((u64)base3 << 32), ctxt))return emulate_gp (ctxt,0 ); } + +if (seg ==VCPU_SREG_TR ) { + old_desc = seg_desc; + seg_desc.type |=2 ;/* busy */ + ret = ctxt->ops->cmpxchg_emulated (ctxt, desc_addr, &old_desc, &seg_desc, +sizeof (seg_desc), &ctxt->exception); +if (ret !=X86EMUL_CONTINUE ) +return ret; + } load: ctxt->ops->set_segment (ctxt, selector, &seg_desc, base3, seg);if (desc)
Signature · 1 line
2.37.0.144.g8ac04bfd2-goog
Re: [PATCH 1/3] KVM: x86: Mark TSS busy during LTR emulation _after_ all fault checks
Quoted 3 lines · Sean Christopherson
Wait to mark the TSS as busy during LTR emulation until after all fault checks for the LTR have passed. Specifically, don't mark the TSS busy if the new TSS base is non-canonical.
Took me a while to notice it but I see the canonical check now, so the patch makes sense, and so:
Unrelated, but I do wonder why we use cmpxchg_emulated for setting the busy bit, while we use write_segment_descriptor to set the accessed bit.
Best regards,
Maxim Levitsky
Quoted 52 lines
Opportunistically drop the one-off !seg_desc.PRESENT check for TR as the only reason for the early check was to avoid marking a !PRESENT TSS as busy, i.e. the common !PRESENT is now done before setting the busy bit.
Fixes e37a75a13cda ("KVM: x86: Emulator ignores LDTR/TR extended base on LLDT/LTR")Reported-by [email protected]Cc Tetsuo Handa <[email protected]>Cc Hou Wenlong <[email protected]>Signed-off-by Sean Christopherson <[email protected]>---
arch/x86/kvm/emulate.c | 19 +++++++++----------1 file changed, 9 insertions(+), 10 deletions(-)arch/x86/kvm/emulate.c +0
-0@@ -1699,16 +1699,6 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,case
VCPU_SREG_TR:
if (seg_desc.s || (seg_desc.type != 1 && seg_desc.type != 9)) goto exception;
- if (!seg_desc.p) {
- err_vec =NP_VECTOR;
- goto exception;
- }
- old_desc =seg_desc;
- seg_desc.type |= 2; /* busy */
- ret = ctxt->ops->cmpxchg_emulated(ctxt,desc_addr,&old_desc,&seg_desc, -sizeof(seg_desc),&ctxt->exception); - if (ret !=X86EMUL_CONTINUE)
- return ret;
break;
caseVCPU_SREG_LDTR:
if (seg_desc.s || seg_desc.type != 2)arch/x86/kvm/emulate.c +9
-0@@ -1749,6 +1739,15 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, ((u64)base3 << 32), ctxt)) return emulate_gp(ctxt, 0); } + + if (seg == VCPU_SREG_TR) { + old_desc = seg_desc; + seg_desc.type |= 2; /* busy */ + ret = ctxt->ops->cmpxchg_emulated(ctxt, desc_addr, &old_desc, &seg_desc, + sizeof(seg_desc), &ctxt->exception); + if (ret != X86EMUL_CONTINUE) + return ret; + }load:
ctxt->ops->set_segment(ctxt, selector,&seg_desc, base3, seg); if (desc)
Sean Christopherson
maintainerRe: [PATCH 1/3] KVM: x86: Mark TSS busy during LTR emulation _after_ all fault checks
Quoted 6 lines · Maxim Levitsky
Quoted 3 lines · Sean Christopherson
Wait to mark the TSS as busy during LTR emulation until after all fault checks for the LTR have passed. Specifically, don't mark the TSS busy if the new TSS base is non-canonical.
Quoted 7 lines
Took me a while to notice it but I see the canonical check now, so the patch makes sense, and so:
Reviewed-by Maxim Levitsky <[email protected]>Unrelated, but I do wonder why we use
cmpxchg_emulatedfor setting the busy bit, while we usewrite_segment_descriptorto set the accessed bit.
99% certain it's a historical KVM bug in how it updates the accessed bit.
Sean Christopherson
maintainerPATCH 2/3 KVM: x86: Set error code to segment selector on LLDT/LTR non-canonical #GP
In reply to Sean ChristophersonWhen injecting a #GP on LLDT/LTR due to a non-canonical LDT/TSS base, set the error code to the selector. Intel SDM's says nothing about the #GP, but AMD's APM explicitly states that both LLDT and LTR set the error code to the selector, not zero.
Note, a non-canonical memory operand on LLDT/LTR does generate a #GP(0), but the KVM code in question is specific to the base from the descriptor.
--- arch/x86/kvm/emulate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/emulate.c +2 -2
@@ -1736,8 +1736,8 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt,if (ret !=X86EMUL_CONTINUE )return ret;if (emul_is_noncanonical_address (get_desc_base (&seg_desc) | - ((u64 )base3 <<32 ), ctxt)) -return emulate_gp (ctxt,0 ); + ((u64 )base3 <<32 ), ctxt)) +return emulate_gp (ctxt, err_code); }if (seg ==VCPU_SREG_TR ) {
Signature · 1 line
2.37.0.144.g8ac04bfd2-goog
Re: [PATCH 2/3] KVM: x86: Set error code to segment selector on LLDT/LTR non-canonical #GP
Quoted 22 lines · Sean Christopherson
When injecting a #GP on LLDT/LTR due to a non-canonical LDT/TSS base, set the error code to the selector. Intel SDM's says nothing about the #GP, but AMD's APM explicitly states that both LLDT and LTR set the error code to the selector, not zero.
Note, a non-canonical memory operand on LLDT/LTR does generate a #GP(0), but the KVM code in question is specific to the base from the descriptor.
Fixes e37a75a13cda ("KVM: x86: Emulator ignores LDTR/TR extended base on LLDT/LTR")Signed-off-by Sean Christopherson <[email protected]>---
arch/x86/kvm/emulate.c | 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kvm/emulate.c +0
-0@@ -1736,8 +1736,8 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, if (ret != X86EMUL_CONTINUE) return ret;
Quoted 8 lines
if (emul_is_noncanonical_address(get_desc_base(&seg_desc) | - ((u64)base3 << 32), ctxt))
- returnemulate_gp(ctxt, 0);
+ ((u64)base3 << 32), ctxt)) + returnemulate_gp(ctxt, err_code);
}
if (seg ==VCPU_SREG_TR) {
I guess this is the quote from AMD's manual (might we worth to add to the source?)
"The 64-bit system-segment base address must be in canonical form. Otherwise, a general-protection exception occurs with a selector error-code, #GP(selector), when the system segment is loaded. System-segment limit values are checked by the processor in both 64-bit and compatibility modes, under the control of the granularity (G) bit."
Best regards,
Maxim Levitsky
Sean Christopherson
maintainerRe: [PATCH 2/3] KVM: x86: Set error code to segment selector on LLDT/LTR non-canonical #GP
Quoted 25 lines · Maxim Levitsky
Quoted 24 lines · Sean Christopherson
When injecting a #GP on LLDT/LTR due to a non-canonical LDT/TSS base, set the error code to the selector. Intel SDM's says nothing about the #GP, but AMD's APM explicitly states that both LLDT and LTR set the error code to the selector, not zero.
Note, a non-canonical memory operand on LLDT/LTR does generate a #GP(0), but the KVM code in question is specific to the base from the descriptor.
Fixes e37a75a13cda ("KVM: x86: Emulator ignores LDTR/TR extended base on LLDT/LTR")Signed-off-by Sean Christopherson <[email protected]>---
arch/x86/kvm/emulate.c | 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kvm/emulate.c +0
-0@@ -1736,8 +1736,8 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, if (ret != X86EMUL_CONTINUE) return ret;if (emul_is_noncanonical_address(get_desc_base(&seg_desc) | - ((u64)base3 << 32), ctxt))
Quoted 8 lines
Quoted 6 lines
- return
emulate_gp(ctxt, 0);
+ ((u64)base3 << 32), ctxt)) + returnemulate_gp(ctxt, err_code);
}
if (seg ==VCPU_SREG_TR) {I guess this is the quote from AMD's manual (might we worth to add to the source?)
Eh, probably not worth it. Anyone working on segmentation emulation is already up to their eyeballs in the SDM/APM.
Sean Christopherson
maintainerPATCH 3/3 KVM: x86: WARN only once if KVM leaves a dangling userspace I/O request
In reply to Sean ChristophersonChange a WARN_ON() to separate WARN_ON_ONCE() if KVM has an outstanding PIO or MMIO request without an associated callback, i.e. if KVM queued a userspace I/O exit but didn't actually exit to userspace before moving on to something else. Warning on every KVM_RUN risks spamming the kernel if KVM gets into a bad state. Opportunistically split the WARNs so that it's easier to triage failures when a WARN fires.
Deliberately do not use KVM_BUG_ON(), i.e. don't kill the VM. While the WARN is all but guaranteed to fire if and only if there's a KVM bug, a dangling I/O request does not present a danger to KVM (that flag is truly truly consumed only in a single emulator path), and any such bug is unlikely to be fatal to the VM (KVM essentially failed to do something it shouldn't have tried to do in the first place). In other words, note the bug, but let the VM keep running.
--- arch/x86/kvm/x86.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +4 -2
@@ -10847,8 +10847,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) r =cui (vcpu);if (r <=0 ) goto out; - }else -WARN_ON (vcpu->arch.pio.count || vcpu->mmio_needed); + }else { +WARN_ON_ONCE (vcpu->arch.pio.count); +WARN_ON_ONCE (vcpu->mmio_needed); + }if (kvm_run->immediate_exit) { r = -EINTR ;
Signature · 1 line
2.37.0.144.g8ac04bfd2-goog
Re: [PATCH 3/3] KVM: x86: WARN only once if KVM leaves a dangling userspace I/O request
Quoted 34 lines · Sean Christopherson
Change a
WARN_ON()to separateWARN_ON_ONCE()if KVM has an outstanding PIO or MMIO request without an associated callback, i.e. if KVM queued a userspace I/O exit but didn't actually exit to userspace before moving on to something else. Warning on everyKVM_RUNrisks spamming the kernel if KVM gets into a bad state. Opportunistically split the WARNs so that it's easier to triage failures when a WARN fires.Deliberately do not use
KVM_BUG_ON(), i.e. don't kill the VM. While the WARN is all but guaranteed to fire if and only if there's a KVM bug, a dangling I/O request does not present a danger to KVM (that flag is truly truly consumed only in a single emulator path), and any such bug is unlikely to be fatal to the VM (KVM essentially failed to do something it shouldn't have tried to do in the first place). In other words, note the bug, but let the VM keep running.Signed-off-by Sean Christopherson <[email protected]>---
arch/x86/kvm/x86.c | 6 ++++--1 file changed, 4 insertions(+), 2 deletions(-)arch/x86/kvm/x86.c +4
-2@@ -10847,8 +10847,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) r = cui(vcpu); if (r <= 0) goto out; - } else - WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed); + } else { + WARN_ON_ONCE(vcpu->arch.pio.count); + WARN_ON_ONCE(vcpu->mmio_needed); + }
Quoted 3 lines
if (kvm_run->immediate_exit) {
r =-EINTR;
At some point in the future, the checkpatch.pl should start to WARN the patch submitter if WARN_ON and not WARN_ON_ONCE was used ;-)
It already bugs the user about BUG_ON ;-)
Best regards,
Maxim Levitsky
Quoted 10 lines · Sean Christopherson
Patch 1 fixes a bug found by syzkaller where KVM attempts to set the TSS.busy bit during LTR before checking that the new TSS.base is valid.
Patch 2 fixes a bug found by inspection (when reading the APM to verify the non-canonical logic is correct) where KVM doesn't provide the correct error code if the new TSS.base is non-canonical.
Patch 3 makes the "dangling userspace I/O"
WARN_ONtwo separateWARN_ON_ONCEso that a KVM bug doesn't spam the kernel log (keeping the WARN is desirable specifically to detect these types of bugs).
Hi Sean,
If/when you find that I screwed up, would you be kind enough to cc me?
Very likely I won’t be able to assist too much in fixing the bugs under my current affiliation, but it is always interesting to see the escapees of Intel’s validation tools… ;-)
Only if you can.
Thanks,
Nadav
[ p.s. - please use my gmail account for the matter ]
Sean Christopherson
maintainerQuoted 9 lines · Nadav Amit
Quoted 7 lines · Sean Christopherson
Patch 1 fixes a bug found by syzkaller where KVM attempts to set the TSS.busy bit during LTR before checking that the new TSS.base is valid.
Patch 2 fixes a bug found by inspection (when reading the APM to verify the non-canonical logic is correct) where KVM doesn't provide the correct error code if the new TSS.base is non-canonical.
Quoted 7 lines
Quoted 3 lines
Patch 3 makes the "dangling userspace I/O"
WARN_ONtwo separateWARN_ON_ONCEso that a KVM bug doesn't spam the kernel log (keeping the WARN is desirable specifically to detect these types of bugs).Hi Sean,
If/when you find that I screwed up, would you be kind enough to cc me?
Will do!
Quoted 10 lines
Very likely I won’t be able to assist too much in fixing the bugs under my current affiliation, but it is always interesting to see the escapees of Intel’s validation tools… ;-)
Only if you can.
Thanks,
Nadav[ p.s. - please use my gmail account for the matter ]
Yep, still got an alias for ya :-)