Skip to contentSkip to conversation

KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0

PATCH In review Like Xu 4 participants 8 messages Jan 23, 2022 – Jan 26, 2022

Subsystems (3)
lore
Share & tools
lore

Like Xu

author
#0 lore raw

PATCH 1/1 KVM: x86/cpuid: Exclude unpermitted xfeatures for vcpu->arch.guest_supported_xcr0

From: Like Xu <[email protected]>

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")
---
 arch/x86/kvm/cpuid.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +2 -1
index 3902c28fb6cb..1bd4d560cbdd 100644
@@ -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
Signature · 1 line
2.33.1
#1 lore raw
Quoted 8 lines

From: Like Xu <[email protected]>
Sent: Sunday, January 23, 2022 1:50 PM

From: Like Xu <[email protected]>

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()...

Quoted 27 lines
Fixes 445ecdf79be0 ("kvm: x86: Exclude unpermitted xfeatures at

KVM_GET_SUPPORTED_CPUID")

---
 arch/x86/kvm/cpuid.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +2 -1
index 3902c28fb6cb..1bd4d560cbdd 100644
@@ -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());
 
arch/x86/kvm/cpuid.c +0 -0
 	/*
 	 * Bits 127:0 of the allowed SECS.ATTRIBUTES (CPUID.0x12.0x1)

enumerate
--
2.33.1

author
#2 lore raw
Quoted 10 lines · Kevin
Quoted 8 lines

From: Like Xu <[email protected]>
Sent: Sunday, January 23, 2022 1:50 PM

From: Like Xu <[email protected]>

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 ?

Quoted 29 lines
Quoted 28 lines
Fixes 445ecdf79be0 ("kvm: x86: Exclude unpermitted xfeatures at

KVM_GET_SUPPORTED_CPUID")

---
  arch/x86/kvm/cpuid.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +2 -1
index 3902c28fb6cb..1bd4d560cbdd 100644
@@ -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

#3 lore raw
Quoted 10 lines · Like Xu
Quoted 9 lines · Kevin
Quoted 8 lines

From: Like Xu <[email protected]>
Sent: Sunday, January 23, 2022 1:50 PM

From: Like Xu <[email protected]>

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.

Quoted 7 lines
Quoted 1 line

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?

Quoted 1 line

- __do_cpuid_func

Reporting what KVM supports to host userspace.

Quoted 1 line

- kvm_mpx_supported

This is a check on host support.

Quoted 1 line

- kvm_vcpu_ioctl_x86_set_xsave

"write" from host userspace.

Quoted 1 line

Have you identified all their areas of use ?

maintainer
#4 lore raw
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · Like Xu
Quoted 10 lines · Kevin
Quoted 8 lines

From: Like Xu <[email protected]>
Sent: Sunday, January 23, 2022 1:50 PM

From: Like Xu <[email protected]>

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.

Quoted 8 lines
Quoted 3 lines

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

Quoted 14 lines
Quoted 1 line

- __do_cpuid_func

Reporting what KVM supports to host userspace.

Quoted 1 line

- kvm_mpx_supported

This is a check on host support.

Quoted 1 line

- kvm_vcpu_ioctl_x86_set_xsave

"write" from host userspace.

Quoted 1 line

Have you identified all their areas of use ?

#5 lore raw
Quoted 39 lines

From: Paolo Bonzini <[email protected]>
Sent: Tuesday, January 25, 2022 12:30 AM

Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Like Xu
Quoted 6 lines · Kevin
Quoted 6 lines

From: Like Xu <[email protected]>
Sent: Sunday, January 23, 2022 1:50 PM

From: Like Xu <[email protected]>

A malicious user space can bypass xstate_get_guest_group_perm() in

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

KVM_GET_SUPPORTED_CPUID mechanism and obtain unpermitted

xfeatures,

Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
Quoted 1 line

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

Quoted 1 line

are intended to work in host context. guest_supported_xcr0 is about what

the guest

Quoted 1 line

is/isn't allowed to access, it has no bearing on what host userspace

can/can't do.

Quoted 1 line

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.

Quoted 9 lines

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

maintainer
#6 lore raw
Quoted 9 lines · Kevin
Quoted 9 lines

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.

Quoted 8 lines
Quoted 6 lines

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

Quoted 4 lines

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)...

#7 lore raw
Quoted 21 lines

From: Paolo Bonzini <[email protected]>
Sent: Tuesday, January 25, 2022 4:28 PM

Quoted 1 line · Kevin
Quoted 1 line

The extra complication is that

arch_prctl(ARCH_REQ_XCOMP_GUEST_PERM)

Quoted 4 lines
Quoted 4 lines

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.

Quoted 2 lines
Quoted 2 lines

Therefore, KVM_GET_SUPPORTED_CPUID must limit its returned values

to

Quoted 5 lines
Quoted 5 lines

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

Quoted 1 line
Quoted 1 line

probably best realized as a new KVM_CHECK_EXTENSION rather than as

Quoted 9 lines

an

Quoted 4 lines
Quoted 2 lines

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.