According to Intel SDM Vol. 3C, "Checks on Guest Segment Registers": - For DS, ES, FS, and GS, if the segment is usable and the guest is not in virtual-8086 mode, bit 0 of the Type field must be 1 (accessed), and if bit 3 of the Type field is 1 (code segment), then bit 1 must be 1 (readable). - For TR, the S flag (descriptor type) must be 0. - For LDTR, if LDTR is usable, the S flag must be 0. Currently, data_segment_valid() does not check the accessed bit or the readable bit for code segments, and neither tr_valid() nor ldtr_valid() checks that the S flag is 0. When is_unrestricted_guest(vcpu) is false (e.g. unrestricted_guest=0 or running a restricted L2 guest), __vmx_guest_state_valid() falsely reports such segment states as valid and leaves emulation_required as false. As a result, KVM attempts a hardware VM-Entry that fails with EXIT_REASON_INVALID_STATE (causing KVM_EXIT_FAIL_ENTRY for L1, or reflecting a spurious VM-Entry failure to L1 instead of synthesizing EXIT_REASON_TRIPLE_FAULT when L2 state is stuffed via RSM or KVM_SET_NESTED_STATE). Add the missing Type checks in data_segment_valid() and the S flag checks in tr_valid() and ldtr_valid(). Fixes: 648dfaa7df2d ("KVM: VMX: Add Guest State Validity Checks") Signed-off-by: Daolong Zhang --- arch/x86/kvm/vmx/vmx.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index cc75feec05da..f14cb9b6f4db 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -3877,6 +3877,11 @@ static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg) return false; if (!var.present) return false; + if (!(var.type & VMX_AR_TYPE_ACCESSES_MASK)) + return false; + if ((var.type & VMX_AR_TYPE_CODE_MASK) && + !(var.type & VMX_AR_TYPE_READABLE_MASK)) + return false; if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) { if (var.dpl < rpl) /* DPL < RPL */ return false; @@ -3900,6 +3905,8 @@ static bool tr_valid(struct kvm_vcpu *vcpu) return false; if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */ return false; + if (tr.s) + return false; if (!tr.present) return false; @@ -3918,6 +3925,8 @@ static bool ldtr_valid(struct kvm_vcpu *vcpu) return false; if (ldtr.type != 2) return false; + if (ldtr.s) + return false; if (!ldtr.present) return false; -- 2.48.1