VMX: Invalid guest state detection enhancements and bug fixes
PATCH 1/1 VMX: Invalid guest state detection enhancements and bug fixes
- Correct unusable flag check on SS, DS, ES, FS, GS, and LDTR
- Add rflags checks
- Report failed instruction on emulation failure
--- arch/x86/kvm/vmx.c | 31 +++++++++++++++++++++++++++---- 1 files changed, 27 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx.c +27 -4
@@ -2122,7 +2122,7 @@ static bool stack_segment_valid(struct kvm_vcpu *vcpu) ss_rpl = ss.selector &SELECTOR_RPL_MASK ;if (ss.unusable) -return true; +return false;if (ss.type !=3 && ss.type !=7 )return false;if (!ss.s)
@@ -2144,7 +2144,7 @@ static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) rpl = var.selector &SELECTOR_RPL_MASK ;if (var.unusable) -return true; +return false;if (!var.s)return false;if (!var.present)
@@ -2185,7 +2185,7 @@ static bool ldtr_valid(struct kvm_vcpu *vcpu)vmx_get_segment (vcpu, &ldtr,VCPU_SREG_LDTR );if (ldtr.unusable) -return true; +return false;if (ldtr.selector &SELECTOR_TI_MASK )/* TI = 1 */ return false;if (ldtr.type !=2 )
@@ -2207,6 +2207,27 @@ static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) (ss.selector & SELECTOR_RPL_MASK)); } +static bool rflags_valid (struct kvm_vcpu *vcpu) +{ +unsigned long rflags; +u32 entry_intr_info; + + rflags =vmcs_readl (GUEST_RFLAGS ); + entry_intr_info =vmcs_read32 (VM_ENTRY_INTR_INFO_FIELD ); +#ifdef CONFIG_X86_64 +if (is_long_mode (vcpu)) +if (rflags &X86_EFLAGS_VM ) +return false; +#endif +if ((entry_intr_info &INTR_INFO_INTR_TYPE_MASK ) ==INTR_TYPE_EXT_INTR + && (entry_intr_info &INTR_INFO_VALID_MASK )) { +if (!(rflags &X86_EFLAGS_IF )) +return false; + } + +return true; +} +/* * Check if guest state is valid. Returns true if valid, false if * not.
@@ -2251,8 +2272,9 @@ static bool guest_state_valid(struct kvm_vcpu *vcpu) }/* TODO: * - Add checks on RIP -* - Add checks on RFLAGS */ +if (!rflags_valid (vcpu)) +return false;return true; }
@@ -3559,6 +3581,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) }if (err !=EMULATE_DONE ) { +kvm_report_emulation_failure (vcpu,"invalid guest state handler" ); vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; vcpu->run->internal.ndata =0 ;
Signature · 1 line
1.7.0.4
Quoted 5 lines · Mohammed Gamal
- Correct unusable flag check on SS, DS, ES, FS, GS, and LDTR
- Add rflags checks
- Report failed instruction on emulation failure
Please post as separate patches.
arch/x86/kvm/vmx.c static bool stack_segment_valid(struct kvm_vcpu *vcpu) · line 2126
Quoted 11 lines
arch/x86/kvm/vmx.c +1
-1@@ -2122,7 +2122,7 @@ static bool stack_segment_valid(struct kvm_vcpu *vcpu) ss_rpl = ss.selector&SELECTOR_RPL_MASK ;if (ss.unusable) -return true; +return false;
Where does it say that ss must be usable?
arch/x86/kvm/vmx.c static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) · line 2150
Quoted 13 lines
arch/x86/kvm/vmx.c +0
-0if (ss.type !=3 && ss.type !=7 )return false;if (!ss.s)
arch/x86/kvm/vmx.c +1
-1@@ -2144,7 +2144,7 @@ static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) rpl = var.selector&SELECTOR_RPL_MASK ;if (var.unusable) -return true; +return false;if (!var.s)return false;if (!var.present)
Ditto.
arch/x86/kvm/vmx.c static bool ldtr_valid(struct kvm_vcpu *vcpu) · line 2191
Quoted 10 lines
arch/x86/kvm/vmx.c +1
-1@@ -2185,7 +2185,7 @@ static bool ldtr_valid(struct kvm_vcpu *vcpu)vmx_get_segment (vcpu,&ldtr,VCPU_SREG_LDTR );if (ldtr.unusable) -return true; +return false;if (ldtr.selector&SELECTOR_TI_MASK )/* TI = 1 */ return false;if (ldtr.type !=2 )
Ditto.
Quoted 7 lines
arch/x86/kvm/vmx.c +3
-0@@ -2207,6 +2207,27 @@ static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) (ss.selector& SELECTOR_RPL_MASK)); } +static bool rflags_valid (struct kvm_vcpu *vcpu) +{ +unsigned long rflags;
arch/x86/kvm/vmx.c static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) · line 2222
Quoted 10 lines
arch/x86/kvm/vmx.c +9
-0+u32 entry_intr_info; + + rflags =vmcs_readl (GUEST_RFLAGS ); + entry_intr_info =vmcs_read32 (VM_ENTRY_INTR_INFO_FIELD ); +#ifdef CONFIG_X86_64 +if (is_long_mode (vcpu)) +if (rflags&X86_EFLAGS_VM ) +return false; +#endif
This is architecturally illegal, not just for vmx entries. The check should be made when emulating setting rflags and in KVM_SET_REGS.
These checks should assume the state is architecturally legal, and only check if they are legal for vmx entries.
arch/x86/kvm/vmx.c static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) · line 2229
Quoted 7 lines
arch/x86/kvm/vmx.c +6
-0+if ((entry_intr_info&INTR_INFO_INTR_TYPE_MASK ) ==INTR_TYPE_EXT_INTR + && (entry_intr_info&INTR_INFO_VALID_MASK )) { +if (!(rflags&X86_EFLAGS_IF )) +return false; + } +
Ditto.
arch/x86/kvm/vmx.c static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) · line 3587
Quoted 9 lines
arch/x86/kvm/vmx.c +1
-0@@ -3559,6 +3581,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) }if (err !=EMULATE_DONE ) { +kvm_report_emulation_failure (vcpu,"invalid guest state handler" ); vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; vcpu->run->internal.ndata =0 ;
Uneeded, userspace can report it.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 18 lines · Avi Kivity
Quoted 6 lines · Mohammed Gamal
- Correct unusable flag check on SS, DS, ES, FS, GS, and LDTR
- Add rflags checks
- Report failed instruction on emulation failurePlease post as separate patches.
Quoted 8 lines
arch/x86/kvm/vmx.c +0
-0@@ -2122,7 +2122,7 @@ static bool stack_segment_valid(struct kvm_vcpu *vcpu) ss_rpl = ss.selector& SELECTOR_RPL_MASK;
Quoted 6 lines
arch/x86/kvm/vmx.c static bool stack_segment_valid(struct kvm_vcpu *vcpu)
Quoted 4 lines
arch/x86/kvm/vmx.c +1
-1if (ss.unusable) - return true; + return false;Where does it say that ss must be usable?
Oh, I must have misread it. The unusable bit check is for virtual 8086 mode. However, with this check returning true when the segment is unusable we still return prematurely as we don't do the other checks.
Quoted 71 lines
Quoted 13 lines
arch/x86/kvm/vmx.c +0
-0if (ss.type != 3&& ss.type != 7)return false;
if (!ss.s)arch/x86/kvm/vmx.c +1
-1@@ -2144,7 +2144,7 @@ static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) rpl = var.selector& SELECTOR_RPL_MASK; if (var.unusable) - return true; + return false; if (!var.s) return false;Quoted 2 lines
if (!var.present)
Ditto.
Quoted 10 lines
arch/x86/kvm/vmx.c +1
-1@@ -2185,7 +2185,7 @@ static bool ldtr_valid(struct kvm_vcpu *vcpu) vmx_get_segment(vcpu,&ldtr, VCPU_SREG_LDTR); if (ldtr.unusable) - return true; + return false;if (ldtr.selector& SELECTOR_TI_MASK) /* TI = 1 */ return false;
if (ldtr.type != 2)Ditto.
Quoted 7 lines
arch/x86/kvm/vmx.c +3
-0@@ -2207,6 +2207,27 @@ static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) (ss.selector& SELECTOR_RPL_MASK)); } +static bool rflags_valid (struct kvm_vcpu *vcpu) +{ + unsigned long rflags;arch/x86/kvm/vmx.c static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu) · line 2222
Quoted 10 lines
arch/x86/kvm/vmx.c +9
-0+ u32 entry_intr_info; + + rflags = vmcs_readl(GUEST_RFLAGS); + entry_intr_info = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD); +#ifdef CONFIG_X86_64 + if (is_long_mode(vcpu)) + if (rflags& X86_EFLAGS_VM) + return false; +#endif This is architecturally illegal, not just for vmx entries. The check should be made when emulating setting rflags and in
KVM_SET_REGS.These checks should assume the state is architecturally legal, and only check if they are legal for vmx entries.
Quoted 8 lines
arch/x86/kvm/vmx.c +1
-0+ if ((entry_intr_info& INTR_INFO_INTR_TYPE_MASK) ==
INTR_TYPE_EXT_INTR
+ && (entry_intr_info& INTR_INFO_VALID_MASK)) {
+ if (!(rflags& X86_EFLAGS_IF))
+ return false;
+ }
+Ditto.
Quoted 4 lines
arch/x86/kvm/vmx.c +0
-0@@ -3559,6 +3581,7 @@ static int handle_invalid_guest_state(struct kvm_vcpu *vcpu) }
Quoted 10 lines
Quoted 8 lines
arch/x86/kvm/vmx.c +1
-0if (err != EMULATE_DONE) { + kvm_report_emulation_failure(vcpu, "invalid guest state handler"); vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;vcpu->run->internal.suberror =
KVM_INTERNAL_ERROR_EMULATION;vcpu->run->internal.ndata= 0;Uneeded, userspace can report it.
Userspace does report the address at which a VM fails, it doesn't for instance show the contents of RIP which is necessary for knowing which instruction failed, this is at least needed for testing purposes. IIRC I've seen some trace_kvm_failed_insn() functions somewhere. But I don't know how to use tracing utilities so I'd be delighted if someone could point me to some relevant documentation or something.
Quoted 5 lines
--
error compilingcommittee.c: too many arguments to function
Quoted 9 lines · Mohammed Gamal
Quoted 9 lines
Quoted 8 lines
if (err !=
EMULATE_DONE) {
+kvm_report_emulation_failure(vcpu, "invalid guest state handler");
vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR;vcpu->run->internal.suberror=KVM_INTERNAL_ERROR_EMULATION;
vcpu->run->internal.ndata = 0;
Quoted 8 lines
Quoted 1 line
Uneeded, userspace can report it.
Userspace does report the address at which a VM fails, it doesn't for instance show the contents of RIP which is necessary for knowing which instruction failed, this is at least needed for testing purposes. IIRC I've seen some
trace_kvm_failed_insn()functions somewhere. But I don't know how to use tracing utilities so I'd be delighted if someone could point me to some relevant documentation or something.
Userspace can be extended to print instruction at RIP on emulation failure. Furthermore userspace can even disassemble it for you. IIRC kvm_report_emulation_failure() was once removed from here and I've actually sent patch to remove kvm_report_emulation_failure() at all.
--
Gleb.