From: Vitaly Kuznetsov <vkuznets@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>, kvm@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
Wanpeng Li <wanpengli@tencent.com>,
Jim Mattson <jmattson@google.com>,
Igor Mammedov <imammedo@redhat.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] KVM: x86: Use memcmp in kvm_cpuid_check_equal()
Date: Mon, 24 Jan 2022 16:10:22 +0100 [thread overview]
Message-ID: <87bl01i2zl.fsf@redhat.com> (raw)
In-Reply-To: <95f63ed6-743b-3547-dda1-4fe83bc39070@redhat.com>
Paolo Bonzini <pbonzini@redhat.com> writes:
> On 1/24/22 11:36, Vitaly Kuznetsov wrote:
>> kvm_cpuid_check_equal() should also check .flags equality but instead
>> of adding it to the existing check, just switch to using memcmp() for
>> the whole 'struct kvm_cpuid_entry2'.
>>
>> When .flags are not checked, kvm_cpuid_check_equal() may allow an update
>> which it shouldn't but kvm_set_cpuid() does not actually update anything
>> and just returns success.
>>
>> Suggested-by: Sean Christopherson <seanjc@google.com>
>> Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
>> ---
>> arch/x86/kvm/cpuid.c | 13 ++-----------
>> 1 file changed, 2 insertions(+), 11 deletions(-)
>>
>> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
>> index 89d7822a8f5b..7dd9c8f4f46e 100644
>> --- a/arch/x86/kvm/cpuid.c
>> +++ b/arch/x86/kvm/cpuid.c
>> @@ -123,20 +123,11 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu,
>> static int kvm_cpuid_check_equal(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2,
>> int nent)
>> {
>> - struct kvm_cpuid_entry2 *orig;
>> - int i;
>> -
>> if (nent != vcpu->arch.cpuid_nent)
>> return -EINVAL;
>>
>> - for (i = 0; i < nent; i++) {
>> - orig = &vcpu->arch.cpuid_entries[i];
>> - if (e2[i].function != orig->function ||
>> - e2[i].index != orig->index ||
>> - e2[i].eax != orig->eax || e2[i].ebx != orig->ebx ||
>> - e2[i].ecx != orig->ecx || e2[i].edx != orig->edx)
>> - return -EINVAL;
>> - }
>> + if (memcmp(e2, vcpu->arch.cpuid_entries, nent * sizeof(*e2)))
>> + return -EINVAL;
>
> Hmm, not sure about that due to the padding in struct kvm_cpuid_entry2.
> It might break userspace that isn't too careful about zeroing it.
FWIW, QEMU zeroes the whole thing before setting individual CPUID
entries. Legacy KVM_SET_CPUID call is also not afffected as it copies
entries to a newly allocated "struct kvm_cpuid_entry2[]" and explicitly
zeroes padding.
Do we need to at least add a check for ".flags"?
>
> Queued patch 1 though.
>
> Paolo
>
--
Vitaly
next prev parent reply other threads:[~2022-01-24 15:10 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-24 10:36 [PATCH 0/2] KVM: x86: Partially allow KVM_SET_CPUID{,2} follow-up Vitaly Kuznetsov
2022-01-24 10:36 ` [PATCH 1/2] KVM: x86: Move CPUID.(EAX=0x12,ECX=1) mangling to __kvm_update_cpuid_runtime() Vitaly Kuznetsov
2022-01-24 10:36 ` [PATCH 2/2] KVM: x86: Use memcmp in kvm_cpuid_check_equal() Vitaly Kuznetsov
2022-01-24 14:02 ` Paolo Bonzini
2022-01-24 15:10 ` Vitaly Kuznetsov [this message]
2022-01-24 16:52 ` Sean Christopherson
2022-01-24 17:07 ` Paolo Bonzini
2022-01-24 19:08 ` Sean Christopherson
2022-01-24 21:03 ` Joe Perches
2022-01-26 10:03 ` Vitaly Kuznetsov
2022-01-26 16:02 ` Sean Christopherson
2022-01-26 16:25 ` Vitaly Kuznetsov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87bl01i2zl.fsf@redhat.com \
--to=vkuznets@redhat.com \
--cc=imammedo@redhat.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=wanpengli@tencent.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).