KVM: x86: prevent setting unsupported XSAVE states
Paolo Bonzini
maintainerPATCH 1/1 KVM: x86: prevent setting unsupported XSAVE states
A guest can still attempt to save and restore XSAVE states even if they have been masked in CPUID leaf 0Dh. This usually is not visible to the guest, but is still wrong: "Any attempt to set a reserved bit (as determined by the contents of EAX and EDX after executing CPUID with EAX=0DH, ECX= 0H) in XCR0 for a given processor will result in a #GP exception".
The patch also performs the same checks as __kvm_set_xcr in KVM_SET_XSAVE. This catches migration from newer to older kernel/processor before the guest starts running.
--- arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/x86.c | 10 ++++++++-- arch/x86/kvm/x86.h | 1 + 3 files changed, 10 insertions(+), 3 deletions(-)
arch/x86/kvm/cpuid.c +1 -1
@@ -182,7 +182,7 @@ static bool supported_xcr0_bit(unsigned bit) {u64 mask = ((u64 )1 << bit); -return mask & (XSTATE_FP |XSTATE_SSE |XSTATE_YMM ) & host_xcr0; +return mask &KVM_SUPPORTED_XCR0 & host_xcr0; }#define F (x) bit(X86_FEATURE_##x)
arch/x86/kvm/x86.c +8 -2
@@ -586,6 +586,8 @@ int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)return 1 ;if ((xcr0 &XSTATE_YMM ) && !(xcr0 &XSTATE_SSE ))return 1 ; +if (xcr0 & ~KVM_SUPPORTED_XCR0 ) +return 1 ;if (xcr0 & ~host_xcr0)return 1 ;kvm_put_guest_xcr0 (vcpu);
@@ -2980,10 +2982,14 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,u64 xstate_bv = *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET /sizeof (u32)]; -if (cpu_has_xsave) +if (cpu_has_xsave) { +if (xstate_bv & ~KVM_SUPPORTED_XCR0 ) +return -EINVAL ; +if (xstate_bv & ~host_xcr0) +return -EINVAL ;memcpy (&vcpu->arch.guest_fpu.state->xsave, guest_xsave->region, xstate_size); -else { + }else {if (xstate_bv & ~XSTATE_FPSSE )return -EINVAL ; memcpy(&vcpu->arch.guest_fpu.state->fxsave,
arch/x86/kvm/x86.h +1 -0
@@ -122,6 +122,7 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val, unsigned int bytes, struct x86_exception *exception); +#define KVM_SUPPORTED_XCR0 (XSTATE_FP | XSTATE_SSE | XSTATE_YMM)extern u64 host_xcr0;extern struct static_key kvm_no_apic_vcpu;
Signature · 1 line
1.8.3.1
Quoted 38 lines · Paolo Bonzini
A guest can still attempt to save and restore XSAVE states even if they have been masked in CPUID leaf 0Dh. This usually is not visible to the guest, but is still wrong: "Any attempt to set a reserved bit (as determined by the contents of EAX and EDX after executing CPUID with EAX=0DH, ECX= 0H) in XCR0 for a given processor will result in a #GP exception".
The patch also performs the same checks as
__kvm_set_xcrinKVM_SET_XSAVE. This catches migration from newer to older kernel/processor before the guest starts running.Cc Gleb Natapov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/x86.c | 10 ++++++++-- arch/x86/kvm/x86.h | 1 + 3 files changed, 10 insertions(+), 3 deletions(-)arch/x86/kvm/cpuid.c +1
-1@@ -182,7 +182,7 @@ static bool supported_xcr0_bit(unsigned bit) {u64 mask = ((u64 )1 << bit); -return mask & (XSTATE_FP |XSTATE_SSE |XSTATE_YMM ) & host_xcr0; +return mask &KVM_SUPPORTED_XCR0 & host_xcr0; }#define F (x) bit(X86_FEATURE_##x)arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) · line 590
Quoted 5 lines
arch/x86/kvm/x86.c +2
-0@@ -586,6 +586,8 @@ int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)return 1 ;if ((xcr0 &XSTATE_YMM ) && !(xcr0 &XSTATE_SSE ))return 1 ; +if (xcr0 & ~KVM_SUPPORTED_XCR0 ) +return 1 ;
Shouldn't we check guest's cpuid here?
arch/x86/kvm/x86.c static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, · line 2989
Quoted 13 lines
arch/x86/kvm/x86.c +5
-1if (xcr0 & ~host_xcr0)return 1 ;kvm_put_guest_xcr0 (vcpu);@@ -2980,10 +2982,14 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,u64 xstate_bv = *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET /sizeof (u32)]; -if (cpu_has_xsave) +if (cpu_has_xsave) { +if (xstate_bv & ~KVM_SUPPORTED_XCR0 ) +return -EINVAL ; +if (xstate_bv & ~host_xcr0) +return -EINVAL ;
And here?
Quoted 21 lines
arch/x86/kvm/x86.c +1
-1memcpy (&vcpu->arch.guest_fpu.state->xsave, guest_xsave->region, xstate_size); - else { + }else {if (xstate_bv & ~XSTATE_FPSSE )return -EINVAL ; memcpy(&vcpu->arch.guest_fpu.state->fxsave,arch/x86/kvm/x86.h +1
-0@@ -122,6 +122,7 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val, unsigned int bytes, struct x86_exception *exception); +#define KVM_SUPPORTED_XCR0 (XSTATE_FP | XSTATE_SSE | XSTATE_YMM)extern u64 host_xcr0;extern struct static_key kvm_no_apic_vcpu;Signature · 1 line
1.8.3.1
--
Gleb.
Paolo Bonzini
maintainerIl 08/09/2013 11:54, Gleb Natapov ha scritto:
Quoted 33 lines · Paolo Bonzini
Quoted 32 lines
A guest can still attempt to save and restore XSAVE states even if they have been masked in CPUID leaf 0Dh. This usually is not visible to the guest, but is still wrong: "Any attempt to set a reserved bit (as determined by the contents of EAX and EDX after executing CPUID with EAX=0DH, ECX= 0H) in XCR0 for a given processor will result in a #GP exception".
The patch also performs the same checks as
__kvm_set_xcrinKVM_SET_XSAVE. This catches migration from newer to older kernel/processor before the guest starts running.Cc Gleb Natapov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/x86.c | 10 ++++++++-- arch/x86/kvm/x86.h | 1 + 3 files changed, 10 insertions(+), 3 deletions(-)arch/x86/kvm/cpuid.c +1
-1@@ -182,7 +182,7 @@ static bool supported_xcr0_bit(unsigned bit) {u64 mask = ((u64 )1 << bit); -return mask & (XSTATE_FP |XSTATE_SSE |XSTATE_YMM ) & host_xcr0; +return mask &KVM_SUPPORTED_XCR0 & host_xcr0; }
Quoted 12 lines
arch/x86/kvm/x86.c int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) · line 590
Quoted 11 lines
arch/x86/kvm/cpuid.c +0
-0#define F (x) bit(X86_FEATURE_##x)arch/x86/kvm/x86.c +2
-0@@ -586,6 +586,8 @@ int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)return 1 ;if ((xcr0 &XSTATE_YMM ) && !(xcr0 &XSTATE_SSE ))return 1 ; +if (xcr0 & ~KVM_SUPPORTED_XCR0 ) +return 1 ;Shouldn't we check guest's cpuid here?
Yes.
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/x86.c +0
-0if (xcr0 & ~host_xcr0)return 1 ;kvm_put_guest_xcr0 (vcpu);@@ -2980,10 +2982,14 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,u64 xstate_bv = *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET /sizeof (u32)];
Quoted 7 lines
arch/x86/kvm/x86.c static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, · line 2989
Quoted 6 lines
arch/x86/kvm/x86.c +5
-1-if (cpu_has_xsave) +if (cpu_has_xsave) { +if (xstate_bv & ~KVM_SUPPORTED_XCR0 ) +return -EINVAL ; +if (xstate_bv & ~host_xcr0) +return -EINVAL ;And here?
Here it'd be nice, but we cannot due to backwards compatibility. We agreed to change KVM_GET_XSAVE and make it only report states that are included in CPUID, but we still need to do migration from old kernels that report all states.
If we change KVM_SET_XSAVE to look at CPUID, and the CPUID does not include AVX, migration will fail from old kernel (KVM_GET_XSAVE reports all states) to new kernel (KVM_SET_XSAVES checks against CPUID).
Paolo
Quoted 29 lines
Quoted 21 lines
arch/x86/kvm/x86.c +1
-1memcpy (&vcpu->arch.guest_fpu.state->xsave, guest_xsave->region, xstate_size); - else { + }else {if (xstate_bv & ~XSTATE_FPSSE )return -EINVAL ; memcpy(&vcpu->arch.guest_fpu.state->fxsave,arch/x86/kvm/x86.h +1
-0@@ -122,6 +122,7 @@ int kvm_write_guest_virt_system(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val, unsigned int bytes, struct x86_exception *exception); +#define KVM_SUPPORTED_XCR0 (XSTATE_FP | XSTATE_SSE | XSTATE_YMM)extern u64 host_xcr0;extern struct static_key kvm_no_apic_vcpu;Signature · 1 line
1.8.3.1--
Gleb.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 64 lines · Paolo Bonzini
Il 08/09/2013 11:54, Gleb Natapov ha scritto:
Quoted 33 lines · Paolo Bonzini
Quoted 32 lines
A guest can still attempt to save and restore XSAVE states even if they have been masked in CPUID leaf 0Dh. This usually is not visible to the guest, but is still wrong: "Any attempt to set a reserved bit (as determined by the contents of EAX and EDX after executing CPUID with EAX=0DH, ECX= 0H) in XCR0 for a given processor will result in a #GP exception".
The patch also performs the same checks as
__kvm_set_xcrinKVM_SET_XSAVE. This catches migration from newer to older kernel/processor before the guest starts running.Cc Gleb Natapov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/x86.c | 10 ++++++++-- arch/x86/kvm/x86.h | 1 + 3 files changed, 10 insertions(+), 3 deletions(-)arch/x86/kvm/cpuid.c +1
-1@@ -182,7 +182,7 @@ static bool supported_xcr0_bit(unsigned bit) {u64 mask = ((u64 )1 << bit); -return mask & (XSTATE_FP |XSTATE_SSE |XSTATE_YMM ) & host_xcr0; +return mask &KVM_SUPPORTED_XCR0 & host_xcr0; }Quoted 12 lines
arch/x86/kvm/x86.c int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) · line 590
Quoted 11 lines
arch/x86/kvm/cpuid.c +0
-0#define F (x) bit(X86_FEATURE_##x)arch/x86/kvm/x86.c +2
-0@@ -586,6 +586,8 @@ int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)return 1 ;if ((xcr0 &XSTATE_YMM ) && !(xcr0 &XSTATE_SSE ))return 1 ; +if (xcr0 & ~KVM_SUPPORTED_XCR0 ) +return 1 ;Shouldn't we check guest's cpuid here?
Yes.
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/x86.c +0
-0if (xcr0 & ~host_xcr0)return 1 ;kvm_put_guest_xcr0 (vcpu);@@ -2980,10 +2982,14 @@ static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,u64 xstate_bv = *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET /sizeof (u32)];Quoted 7 lines
arch/x86/kvm/x86.c static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, · line 2989
Quoted 6 lines
arch/x86/kvm/x86.c +5
-1-if (cpu_has_xsave) +if (cpu_has_xsave) { +if (xstate_bv & ~KVM_SUPPORTED_XCR0 ) +return -EINVAL ; +if (xstate_bv & ~host_xcr0) +return -EINVAL ;And here?
Quoted 9 lines
Here it'd be nice, but we cannot due to backwards compatibility. We agreed to change
KVM_GET_XSAVEand make it only report states that are included in CPUID, but we still need to do migration from old kernels that report all states.If we change
KVM_SET_XSAVEto look at CPUID, and the CPUID does not include AVX, migration will fail from old kernel (KVM_GET_XSAVEreports all states) to new kernel (KVM_SET_XSAVESchecks against CPUID).
Yeah :(
--
Gleb.