kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: x86: fix CPUID entries returned by KVM_GET_CPUID2 ioctl
@ 2021-01-28  2:44 Michael Roth
  2021-01-28  7:40 ` Paolo Bonzini
  2021-01-28  8:36 ` Vitaly Kuznetsov
  0 siblings, 2 replies; 4+ messages in thread
From: Michael Roth @ 2021-01-28  2:44 UTC (permalink / raw)
  To: kvm
  Cc: Paolo Bonzini, Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li,
	Jim Mattson, Joerg Roedel, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, x86, H . Peter Anvin, linux-kernel,
	Michael Roth

Recent commit 255cbecfe0 modified struct kvm_vcpu_arch to make
'cpuid_entries' a pointer to an array of kvm_cpuid_entry2 entries
rather than embedding the array in the struct. KVM_SET_CPUID and
KVM_SET_CPUID2 were updated accordingly, but KVM_GET_CPUID2 was missed.

As a result, KVM_GET_CPUID2 currently returns random fields from struct
kvm_vcpu_arch to userspace rather than the expected CPUID values. Fix
this by treating 'cpuid_entries' as a pointer when copying its
contents to userspace buffer.

Fixes: 255cbecfe0c9 ("KVM: x86: allocate vcpu->arch.cpuid_entries dynamically")
Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: Michael Roth <michael.roth@amd.com.com>
---
 arch/x86/kvm/cpuid.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 13036cf0b912..38172ca627d3 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -321,7 +321,7 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
 	if (cpuid->nent < vcpu->arch.cpuid_nent)
 		goto out;
 	r = -EFAULT;
-	if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
+	if (copy_to_user(entries, vcpu->arch.cpuid_entries,
 			 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
 		goto out;
 	return 0;
-- 
2.25.1


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

* Re: [PATCH] KVM: x86: fix CPUID entries returned by KVM_GET_CPUID2 ioctl
  2021-01-28  2:44 [PATCH] KVM: x86: fix CPUID entries returned by KVM_GET_CPUID2 ioctl Michael Roth
@ 2021-01-28  7:40 ` Paolo Bonzini
  2021-01-28  8:36 ` Vitaly Kuznetsov
  1 sibling, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2021-01-28  7:40 UTC (permalink / raw)
  To: Michael Roth, kvm
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H . Peter Anvin, linux-kernel, Michael Roth

On 28/01/21 03:44, Michael Roth wrote:
> Recent commit 255cbecfe0 modified struct kvm_vcpu_arch to make
> 'cpuid_entries' a pointer to an array of kvm_cpuid_entry2 entries
> rather than embedding the array in the struct. KVM_SET_CPUID and
> KVM_SET_CPUID2 were updated accordingly, but KVM_GET_CPUID2 was missed.
> 
> As a result, KVM_GET_CPUID2 currently returns random fields from struct
> kvm_vcpu_arch to userspace rather than the expected CPUID values. Fix
> this by treating 'cpuid_entries' as a pointer when copying its
> contents to userspace buffer.
> 
> Fixes: 255cbecfe0c9 ("KVM: x86: allocate vcpu->arch.cpuid_entries dynamically")
> Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
> Signed-off-by: Michael Roth <michael.roth@amd.com.com>
> ---
>   arch/x86/kvm/cpuid.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 13036cf0b912..38172ca627d3 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -321,7 +321,7 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
>   	if (cpuid->nent < vcpu->arch.cpuid_nent)
>   		goto out;
>   	r = -EFAULT;
> -	if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
> +	if (copy_to_user(entries, vcpu->arch.cpuid_entries,
>   			 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
>   		goto out;
>   	return 0;
> 

Queued, thanks.  I'll also write a testcase.

Paolo


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

* Re: [PATCH] KVM: x86: fix CPUID entries returned by KVM_GET_CPUID2 ioctl
  2021-01-28  2:44 [PATCH] KVM: x86: fix CPUID entries returned by KVM_GET_CPUID2 ioctl Michael Roth
  2021-01-28  7:40 ` Paolo Bonzini
@ 2021-01-28  8:36 ` Vitaly Kuznetsov
  2021-01-28  8:43   ` Paolo Bonzini
  1 sibling, 1 reply; 4+ messages in thread
From: Vitaly Kuznetsov @ 2021-01-28  8:36 UTC (permalink / raw)
  To: Michael Roth, kvm
  Cc: Paolo Bonzini, Sean Christopherson, Wanpeng Li, Jim Mattson,
	Joerg Roedel, Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H . Peter Anvin, linux-kernel, Michael Roth

Michael Roth <michael.roth@amd.com> writes:

> Recent commit 255cbecfe0 modified struct kvm_vcpu_arch to make
> 'cpuid_entries' a pointer to an array of kvm_cpuid_entry2 entries
> rather than embedding the array in the struct. KVM_SET_CPUID and
> KVM_SET_CPUID2 were updated accordingly, but KVM_GET_CPUID2 was missed.
>
> As a result, KVM_GET_CPUID2 currently returns random fields from struct
> kvm_vcpu_arch to userspace rather than the expected CPUID values. Fix
> this by treating 'cpuid_entries' as a pointer when copying its
> contents to userspace buffer.
>
> Fixes: 255cbecfe0c9 ("KVM: x86: allocate vcpu->arch.cpuid_entries dynamically")
> Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
> Signed-off-by: Michael Roth <michael.roth@amd.com.com>
> ---
>  arch/x86/kvm/cpuid.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 13036cf0b912..38172ca627d3 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -321,7 +321,7 @@ int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu,
>  	if (cpuid->nent < vcpu->arch.cpuid_nent)
>  		goto out;
>  	r = -EFAULT;
> -	if (copy_to_user(entries, &vcpu->arch.cpuid_entries,
> +	if (copy_to_user(entries, vcpu->arch.cpuid_entries,
>  			 vcpu->arch.cpuid_nent * sizeof(struct kvm_cpuid_entry2)))
>  		goto out;
>  	return 0;

This is embarrassing but I have a (possible) excuse: copy_to_user's
argument is 'void *' so no warning was produced. Surprisingly, no test
caught the breakage. Thanks for debugging and fixing!

Reviewed-by: Vitaly Kuznetsov <vkuznets@redhat.com>

-- 
Vitaly


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

* Re: [PATCH] KVM: x86: fix CPUID entries returned by KVM_GET_CPUID2 ioctl
  2021-01-28  8:36 ` Vitaly Kuznetsov
@ 2021-01-28  8:43   ` Paolo Bonzini
  0 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2021-01-28  8:43 UTC (permalink / raw)
  To: Vitaly Kuznetsov, Michael Roth, kvm
  Cc: Sean Christopherson, Wanpeng Li, Jim Mattson, Joerg Roedel,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, x86,
	H . Peter Anvin, linux-kernel, Michael Roth

On 28/01/21 09:36, Vitaly Kuznetsov wrote:
> This is embarrassing but I have a (possible) excuse: copy_to_user's
> argument is 'void *' so no warning was produced. Surprisingly, no test
> caught the breakage. Thanks for debugging and fixing!

So who writes the test:

- the author of the buggy patch

- the maintainer who failed to spot it

- the poor sod who fixed the issue

?

(Just kidding, it will of course be either me or a fourth person :)).

Paolo


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

end of thread, other threads:[~2021-01-28  8:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-28  2:44 [PATCH] KVM: x86: fix CPUID entries returned by KVM_GET_CPUID2 ioctl Michael Roth
2021-01-28  7:40 ` Paolo Bonzini
2021-01-28  8:36 ` Vitaly Kuznetsov
2021-01-28  8:43   ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).