KVM: x86: fix CPUID entries returned by KVM_GET_CPUID2 ioctl
PATCH 1/1 KVM: x86: fix CPUID entries returned by KVM_GET_CPUID2 ioctl
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.
--- arch/x86/kvm/cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
@@ -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 ;
Signature · 1 line
2.25.1
Paolo Bonzini
maintainerQuoted 23 lines · Michael Roth
Recent commit
255cbecfe0modified structkvm_vcpu_archto make 'cpuid_entries' a pointer to an array ofkvm_cpuid_entry2entries rather than embedding the array in the struct.KVM_SET_CPUIDandKVM_SET_CPUID2were updated accordingly, butKVM_GET_CPUID2was missed.As a result,
KVM_GET_CPUID2currently returns random fields from structkvm_vcpu_archto 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 <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/cpuid.c +0
-0
arch/x86/kvm/cpuid.c int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, · line 327
Quoted 8 lines
arch/x86/kvm/cpuid.c +1
-1@@ -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
Quoted 22 lines · Michael Roth
Recent commit
255cbecfe0modified structkvm_vcpu_archto make 'cpuid_entries' a pointer to an array ofkvm_cpuid_entry2entries rather than embedding the array in the struct.KVM_SET_CPUIDandKVM_SET_CPUID2were updated accordingly, butKVM_GET_CPUID2was missed.As a result,
KVM_GET_CPUID2currently returns random fields from structkvm_vcpu_archto 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 <[email protected]>Signed-off-by Michael Roth <[email protected]>--- arch/x86/kvm/cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kvm/cpuid.c +0
-0
arch/x86/kvm/cpuid.c int kvm_vcpu_ioctl_get_cpuid2(struct kvm_vcpu *vcpu, · line 327
Quoted 8 lines
arch/x86/kvm/cpuid.c +1
-1@@ -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!
Signature · 1 line
Vitaly
Paolo Bonzini
maintainerQuoted 3 lines · Vitaly Kuznetsov
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