All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0
@ 2022-01-23  5:50 Like Xu
  2022-01-24  7:06 ` Tian, Kevin
  0 siblings, 1 reply; 8+ messages in thread
From: Like Xu @ 2022-01-23  5:50 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Jing Liu, Wanpeng Li, Sean Christopherson, Jim Mattson,
	Vitaly Kuznetsov, Joerg Roedel, kvm, linux-kernel

From: Like Xu <likexu@tencent.com>

A malicious user space can bypass xstate_get_guest_group_perm() in the
KVM_GET_SUPPORTED_CPUID mechanism and obtain unpermitted xfeatures,
since the validity check of xcr0 depends only on guest_supported_xcr0.

Fixes: 445ecdf79be0 ("kvm: x86: Exclude unpermitted xfeatures at KVM_GET_SUPPORTED_CPUID")
Signed-off-by: Like Xu <likexu@tencent.com>
---
 arch/x86/kvm/cpuid.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 3902c28fb6cb..1bd4d560cbdd 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -266,7 +266,8 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
 		vcpu->arch.guest_supported_xcr0 = 0;
 	else
 		vcpu->arch.guest_supported_xcr0 =
-			(best->eax | ((u64)best->edx << 32)) & supported_xcr0;
+			(best->eax | ((u64)best->edx << 32)) &
+			(supported_xcr0 & xstate_get_guest_group_perm());
 
 	/*
 	 * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1) enumerate
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* RE: [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0
  2022-01-23  5:50 [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0 Like Xu
@ 2022-01-24  7:06 ` Tian, Kevin
  2022-01-24  7:17   ` Like Xu
  0 siblings, 1 reply; 8+ messages in thread
From: Tian, Kevin @ 2022-01-24  7:06 UTC (permalink / raw)
  To: Like Xu, Paolo Bonzini
  Cc: Liu, Jing2, Wanpeng Li, Christopherson,,
	Sean, Jim Mattson, Vitaly Kuznetsov, Joerg Roedel, kvm,
	linux-kernel

> From: Like Xu <like.xu.linux@gmail.com>
> Sent: Sunday, January 23, 2022 1:50 PM
> 
> From: Like Xu <likexu@tencent.com>
> 
> A malicious user space can bypass xstate_get_guest_group_perm() in the
> KVM_GET_SUPPORTED_CPUID mechanism and obtain unpermitted xfeatures,
> since the validity check of xcr0 depends only on guest_supported_xcr0.

Unpermitted xfeatures cannot pass kvm_check_cpuid()...

> 
> Fixes: 445ecdf79be0 ("kvm: x86: Exclude unpermitted xfeatures at
> KVM_GET_SUPPORTED_CPUID")
> Signed-off-by: Like Xu <likexu@tencent.com>
> ---
>  arch/x86/kvm/cpuid.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 3902c28fb6cb..1bd4d560cbdd 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -266,7 +266,8 @@ static void kvm_vcpu_after_set_cpuid(struct
> kvm_vcpu *vcpu)
>  		vcpu->arch.guest_supported_xcr0 = 0;
>  	else
>  		vcpu->arch.guest_supported_xcr0 =
> -			(best->eax | ((u64)best->edx << 32)) &
> supported_xcr0;
> +			(best->eax | ((u64)best->edx << 32)) &
> +			(supported_xcr0 & xstate_get_guest_group_perm());
> 
>  	/*
>  	 * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1)
> enumerate
> --
> 2.33.1


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0
  2022-01-24  7:06 ` Tian, Kevin
@ 2022-01-24  7:17   ` Like Xu
  2022-01-24 16:23     ` Sean Christopherson
  0 siblings, 1 reply; 8+ messages in thread
From: Like Xu @ 2022-01-24  7:17 UTC (permalink / raw)
  To: Tian, Kevin, Paolo Bonzini
  Cc: Liu, Jing2, Wanpeng Li, Christopherson,,
	Sean, Jim Mattson, Vitaly Kuznetsov, Joerg Roedel, kvm,
	linux-kernel

On 24/1/2022 3:06 pm, Tian, Kevin wrote:
>> From: Like Xu <like.xu.linux@gmail.com>
>> Sent: Sunday, January 23, 2022 1:50 PM
>>
>> From: Like Xu <likexu@tencent.com>
>>
>> A malicious user space can bypass xstate_get_guest_group_perm() in the
>> KVM_GET_SUPPORTED_CPUID mechanism and obtain unpermitted xfeatures,
>> since the validity check of xcr0 depends only on guest_supported_xcr0.
> 
> Unpermitted xfeatures cannot pass kvm_check_cpuid()...

Indeed, 5ab2f45bba4894a0db4af8567da3efd6228dd010.

This part of logic is pretty fragile and fragmented due to semantic
inconsistencies between supported_xcr0 and guest_supported_xcr0
in other three places:

- __do_cpuid_func
- kvm_mpx_supported
- kvm_vcpu_ioctl_x86_set_xsave

Have you identified all their areas of use ?

> 
>>
>> Fixes: 445ecdf79be0 ("kvm: x86: Exclude unpermitted xfeatures at
>> KVM_GET_SUPPORTED_CPUID")
>> Signed-off-by: Like Xu <likexu@tencent.com>
>> ---
>>   arch/x86/kvm/cpuid.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
>> index 3902c28fb6cb..1bd4d560cbdd 100644
>> --- a/arch/x86/kvm/cpuid.c
>> +++ b/arch/x86/kvm/cpuid.c
>> @@ -266,7 +266,8 @@ static void kvm_vcpu_after_set_cpuid(struct
>> kvm_vcpu *vcpu)
>>   		vcpu->arch.guest_supported_xcr0 = 0;
>>   	else
>>   		vcpu->arch.guest_supported_xcr0 =
>> -			(best->eax | ((u64)best->edx << 32)) &
>> supported_xcr0;
>> +			(best->eax | ((u64)best->edx << 32)) &
>> +			(supported_xcr0 & xstate_get_guest_group_perm());
>>
>>   	/*
>>   	 * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1)
>> enumerate
>> --
>> 2.33.1
> 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0
  2022-01-24  7:17   ` Like Xu
@ 2022-01-24 16:23     ` Sean Christopherson
  2022-01-24 16:29       ` Paolo Bonzini
  0 siblings, 1 reply; 8+ messages in thread
From: Sean Christopherson @ 2022-01-24 16:23 UTC (permalink / raw)
  To: Like Xu
  Cc: Tian, Kevin, Paolo Bonzini, Liu, Jing2, Wanpeng Li, Jim Mattson,
	Vitaly Kuznetsov, Joerg Roedel, kvm, linux-kernel

On Mon, Jan 24, 2022, Like Xu wrote:
> On 24/1/2022 3:06 pm, Tian, Kevin wrote:
> > > From: Like Xu <like.xu.linux@gmail.com>
> > > Sent: Sunday, January 23, 2022 1:50 PM
> > > 
> > > From: Like Xu <likexu@tencent.com>
> > > 
> > > A malicious user space can bypass xstate_get_guest_group_perm() in the
> > > KVM_GET_SUPPORTED_CPUID mechanism and obtain unpermitted xfeatures,
> > > since the validity check of xcr0 depends only on guest_supported_xcr0.
> > 
> > Unpermitted xfeatures cannot pass kvm_check_cpuid()...
> 
> Indeed, 5ab2f45bba4894a0db4af8567da3efd6228dd010.
> 
> This part of logic is pretty fragile and fragmented due to semantic
> inconsistencies between supported_xcr0 and guest_supported_xcr0
> in other three places:

There are no inconsistencies, at least not in the examples below, as the examples
are intended to work in host context.  guest_supported_xcr0 is about what the guest
is/isn't allowed to access, it has no bearing on what host userspace can/can't do.
Or are you talking about a different type of inconsistency?

> - __do_cpuid_func

Reporting what KVM supports to host userspace.

> - kvm_mpx_supported

This is a check on host support.

> - kvm_vcpu_ioctl_x86_set_xsave

"write" from host userspace.

> Have you identified all their areas of use ?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0
  2022-01-24 16:23     ` Sean Christopherson
@ 2022-01-24 16:29       ` Paolo Bonzini
  2022-01-25  1:54         ` Tian, Kevin
  0 siblings, 1 reply; 8+ messages in thread
From: Paolo Bonzini @ 2022-01-24 16:29 UTC (permalink / raw)
  To: Sean Christopherson, Like Xu
  Cc: Tian, Kevin, Liu, Jing2, Wanpeng Li, Jim Mattson,
	Vitaly Kuznetsov, Joerg Roedel, kvm, linux-kernel, Zhong, Yang

On 1/24/22 17:23, Sean Christopherson wrote:
> On Mon, Jan 24, 2022, Like Xu wrote:
>> On 24/1/2022 3:06 pm, Tian, Kevin wrote:
>>>> From: Like Xu <like.xu.linux@gmail.com>
>>>> Sent: Sunday, January 23, 2022 1:50 PM
>>>>
>>>> From: Like Xu <likexu@tencent.com>
>>>>
>>>> A malicious user space can bypass xstate_get_guest_group_perm() in the
>>>> KVM_GET_SUPPORTED_CPUID mechanism and obtain unpermitted xfeatures,
>>>> since the validity check of xcr0 depends only on guest_supported_xcr0.
>>>
>>> Unpermitted xfeatures cannot pass kvm_check_cpuid()...
>>
>> Indeed, 5ab2f45bba4894a0db4af8567da3efd6228dd010.
>>
>> This part of logic is pretty fragile and fragmented due to semantic
>> inconsistencies between supported_xcr0 and guest_supported_xcr0
>> in other three places:
> 
> There are no inconsistencies, at least not in the examples below, as the examples
> are intended to work in host context.  guest_supported_xcr0 is about what the guest
> is/isn't allowed to access, it has no bearing on what host userspace can/can't do.
> Or are you talking about a different type of inconsistency?

The extra complication is that arch_prctl(ARCH_REQ_XCOMP_GUEST_PERM) 
changes what host userspace can/can't do.  It would be easier if we 
could just say that KVM_GET_SUPPORTED_CPUID returns "the most" that 
userspace can do, but we already have the contract that userspace can
take KVM_GET_SUPPORTED_CPUID and pass it straight to KVM_SET_CPUID2.

Therefore,  KVM_GET_SUPPORTED_CPUID must limit its returned values to 
what has already been enabled.

While reviewing the QEMU part of AMX support (this morning), I also 
noticed that there is no equivalent for guest permissions of 
ARCH_GET_XCOMP_SUPP.  This needs to know KVM's supported_xcr0, so it's 
probably best realized as a new KVM_CHECK_EXTENSION rather than as an 
arch_prctl.

Paolo

>> - __do_cpuid_func
> 
> Reporting what KVM supports to host userspace.
> 
>> - kvm_mpx_supported
> 
> This is a check on host support.
> 
>> - kvm_vcpu_ioctl_x86_set_xsave
> 
> "write" from host userspace.
> 
>> Have you identified all their areas of use ?
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0
  2022-01-24 16:29       ` Paolo Bonzini
@ 2022-01-25  1:54         ` Tian, Kevin
  2022-01-25  8:27           ` Paolo Bonzini
  0 siblings, 1 reply; 8+ messages in thread
From: Tian, Kevin @ 2022-01-25  1:54 UTC (permalink / raw)
  To: Paolo Bonzini, Christopherson,, Sean, Like Xu
  Cc: Liu, Jing2, Wanpeng Li, Jim Mattson, Vitaly Kuznetsov,
	Joerg Roedel, kvm, linux-kernel, Zhong, Yang

> From: Paolo Bonzini <pbonzini@redhat.com>
> Sent: Tuesday, January 25, 2022 12:30 AM
> 
> On 1/24/22 17:23, Sean Christopherson wrote:
> > On Mon, Jan 24, 2022, Like Xu wrote:
> >> On 24/1/2022 3:06 pm, Tian, Kevin wrote:
> >>>> From: Like Xu <like.xu.linux@gmail.com>
> >>>> Sent: Sunday, January 23, 2022 1:50 PM
> >>>>
> >>>> From: Like Xu <likexu@tencent.com>
> >>>>
> >>>> A malicious user space can bypass xstate_get_guest_group_perm() in
> the
> >>>> KVM_GET_SUPPORTED_CPUID mechanism and obtain unpermitted
> xfeatures,
> >>>> since the validity check of xcr0 depends only on guest_supported_xcr0.
> >>>
> >>> Unpermitted xfeatures cannot pass kvm_check_cpuid()...
> >>
> >> Indeed, 5ab2f45bba4894a0db4af8567da3efd6228dd010.
> >>
> >> This part of logic is pretty fragile and fragmented due to semantic
> >> inconsistencies between supported_xcr0 and guest_supported_xcr0
> >> in other three places:
> >
> > There are no inconsistencies, at least not in the examples below, as the
> examples
> > are intended to work in host context.  guest_supported_xcr0 is about what
> the guest
> > is/isn't allowed to access, it has no bearing on what host userspace
> can/can't do.
> > Or are you talking about a different type of inconsistency?
> 
> The extra complication is that arch_prctl(ARCH_REQ_XCOMP_GUEST_PERM)
> changes what host userspace can/can't do.  It would be easier if we
> could just say that KVM_GET_SUPPORTED_CPUID returns "the most" that
> userspace can do, but we already have the contract that userspace can
> take KVM_GET_SUPPORTED_CPUID and pass it straight to KVM_SET_CPUID2.
> 
> Therefore,  KVM_GET_SUPPORTED_CPUID must limit its returned values to
> what has already been enabled.
> 
> While reviewing the QEMU part of AMX support (this morning), I also
> noticed that there is no equivalent for guest permissions of
> ARCH_GET_XCOMP_SUPP.  This needs to know KVM's supported_xcr0, so it's
> probably best realized as a new KVM_CHECK_EXTENSION rather than as an
> arch_prctl.
> 

Would that lead to a weird situation where although KVM says no support
of guest permissions while the user can still request them via prctl()?

I wonder whether it's cleaner to do it still via prctl() if we really want to
enhance this part. But as you said then it needs a mechanism to know 
KVM's supported_xcr0 (and if KVM is not loaded then no guest permission
support at all)...

Thanks
Kevin

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0
  2022-01-25  1:54         ` Tian, Kevin
@ 2022-01-25  8:27           ` Paolo Bonzini
  2022-01-26  1:23             ` Tian, Kevin
  0 siblings, 1 reply; 8+ messages in thread
From: Paolo Bonzini @ 2022-01-25  8:27 UTC (permalink / raw)
  To: Tian, Kevin, Christopherson,, Sean, Like Xu
  Cc: Liu, Jing2, Wanpeng Li, Jim Mattson, Vitaly Kuznetsov,
	Joerg Roedel, kvm, linux-kernel, Zhong, Yang

On 1/25/22 02:54, Tian, Kevin wrote:
>> The extra complication is that arch_prctl(ARCH_REQ_XCOMP_GUEST_PERM)
>> changes what host userspace can/can't do.  It would be easier if we
>> could just say that KVM_GET_SUPPORTED_CPUID returns "the most" that
>> userspace can do, but we already have the contract that userspace can
>> take KVM_GET_SUPPORTED_CPUID and pass it straight to KVM_SET_CPUID2.
>>
>> Therefore,  KVM_GET_SUPPORTED_CPUID must limit its returned values to
>> what has already been enabled.
>>
>> While reviewing the QEMU part of AMX support (this morning), I also
>> noticed that there is no equivalent for guest permissions of
>> ARCH_GET_XCOMP_SUPP.  This needs to know KVM's supported_xcr0, so it's
>> probably best realized as a new KVM_CHECK_EXTENSION rather than as an
>> arch_prctl.
>>
> Would that lead to a weird situation where although KVM says no support
> of guest permissions while the user can still request them via prctl()?

This is already the case for the current implementation of 
KVM_GET_SUPPORTED_CPUID.

Paolo

> I wonder whether it's cleaner to do it still via prctl() if we really want to
> enhance this part. But as you said then it needs a mechanism to know
> KVM's supported_xcr0 (and if KVM is not loaded then no guest permission
> support at all)...


^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0
  2022-01-25  8:27           ` Paolo Bonzini
@ 2022-01-26  1:23             ` Tian, Kevin
  0 siblings, 0 replies; 8+ messages in thread
From: Tian, Kevin @ 2022-01-26  1:23 UTC (permalink / raw)
  To: Paolo Bonzini, Christopherson,, Sean, Like Xu
  Cc: Liu, Jing2, Wanpeng Li, Jim Mattson, Vitaly Kuznetsov,
	Joerg Roedel, kvm, linux-kernel, Zhong, Yang

> From: Paolo Bonzini <pbonzini@redhat.com>
> Sent: Tuesday, January 25, 2022 4:28 PM
> 
> On 1/25/22 02:54, Tian, Kevin wrote:
> >> The extra complication is that
> arch_prctl(ARCH_REQ_XCOMP_GUEST_PERM)
> >> changes what host userspace can/can't do.  It would be easier if we
> >> could just say that KVM_GET_SUPPORTED_CPUID returns "the most" that
> >> userspace can do, but we already have the contract that userspace can
> >> take KVM_GET_SUPPORTED_CPUID and pass it straight to
> KVM_SET_CPUID2.
> >>
> >> Therefore,  KVM_GET_SUPPORTED_CPUID must limit its returned values
> to
> >> what has already been enabled.
> >>
> >> While reviewing the QEMU part of AMX support (this morning), I also
> >> noticed that there is no equivalent for guest permissions of
> >> ARCH_GET_XCOMP_SUPP.  This needs to know KVM's supported_xcr0, so
> it's
> >> probably best realized as a new KVM_CHECK_EXTENSION rather than as
> an
> >> arch_prctl.
> >>
> > Would that lead to a weird situation where although KVM says no support
> > of guest permissions while the user can still request them via prctl()?
> 
> This is already the case for the current implementation of
> KVM_GET_SUPPORTED_CPUID.
> 

fair enough.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-01-26  1:24 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-23  5:50 [PATCH] KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0 Like Xu
2022-01-24  7:06 ` Tian, Kevin
2022-01-24  7:17   ` Like Xu
2022-01-24 16:23     ` Sean Christopherson
2022-01-24 16:29       ` Paolo Bonzini
2022-01-25  1:54         ` Tian, Kevin
2022-01-25  8:27           ` Paolo Bonzini
2022-01-26  1:23             ` Tian, Kevin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.