From: Sean Christopherson <seanjc@google.com>
To: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Cc: kvm@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
Jonathan Corbet <corbet@lwn.net>,
Vitaly Kuznetsov <vkuznets@redhat.com>,
Jim Mattson <jmattson@google.com>, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>, "H. Peter Anvin" <hpa@zytor.com>,
Shuah Khan <shuah@kernel.org>, Alexander Graf <graf@amazon.com>,
Andrew Jones <drjones@redhat.com>,
linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: Re: [PATCH v3 1/4] KVM: x86: Fix a spurious -E2BIG in KVM_GET_EMULATED_CPUID
Date: Tue, 6 Apr 2021 16:07:41 +0000 [thread overview]
Message-ID: <YGyHTc0uJtrJY0gh@google.com> (raw)
In-Reply-To: <20210406082642.20115-2-eesposit@redhat.com>
On Tue, Apr 06, 2021, Emanuele Giuseppe Esposito wrote:
> When retrieving emulated CPUID entries, check for an insufficient array
> size if and only if KVM is actually inserting an entry.
> If userspace has a priori knowledge of the exact array size,
> KVM_GET_EMULATED_CPUID will incorrectly fail due to effectively requiring
> an extra, unused entry.
>
> Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Don't think it needs stable@, but I think it's worthwhile to add:
Fixes: 433f4ba19041 ("KVM: x86: fix out-of-bounds write in KVM_GET_EMULATED_CPUID (CVE-2019-19332)")
> ---
> arch/x86/kvm/cpuid.c | 33 ++++++++++++++++-----------------
> 1 file changed, 16 insertions(+), 17 deletions(-)
>
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 6bd2f8b830e4..27059ddf9f0a 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -567,34 +567,33 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
>
> static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
> {
> - struct kvm_cpuid_entry2 *entry;
> -
> - if (array->nent >= array->maxnent)
> - return -E2BIG;
> + struct kvm_cpuid_entry2 entry;
>
> - entry = &array->entries[array->nent];
> - entry->function = func;
> - entry->index = 0;
> - entry->flags = 0;
> + memset(&entry, 0, sizeof(entry));
> + entry.function = func;
Deep into nitpick territory... I think it makes sense to set entry.function only
after the switch statement, that way it'll be a bit more obvious that the default
case doesn't actually consume "entry".
>
> switch (func) {
> case 0:
> - entry->eax = 7;
> - ++array->nent;
> + entry.eax = 7;
> break;
> case 1:
> - entry->ecx = F(MOVBE);
> - ++array->nent;
> + entry.ecx = F(MOVBE);
> break;
> case 7:
> - entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
> - entry->eax = 0;
> - entry->ecx = F(RDPID);
> - ++array->nent;
> - default:
> + entry.flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
> + entry.eax = 0;
> + entry.ecx = F(RDPID);
> break;
> + default:
> + goto out;
> }
Maybe add a comment here to call out that the check is done if and only if there
is a valid entry?
> + if (array->nent >= array->maxnent)
> + return -E2BIG;
> +
> + memcpy(&array->entries[array->nent++], &entry, sizeof(entry));
> +
> +out:
> return 0;
> }
>
> --
> 2.30.2
>
next prev parent reply other threads:[~2021-04-06 16:07 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-06 8:26 [PATCH v3 0/4] KVM: cpuid: fix KVM_GET_EMULATED_CPUID implementation Emanuele Giuseppe Esposito
2021-04-06 8:26 ` [PATCH v3 1/4] KVM: x86: Fix a spurious -E2BIG in KVM_GET_EMULATED_CPUID Emanuele Giuseppe Esposito
2021-04-06 14:21 ` Vitaly Kuznetsov
2021-04-06 16:09 ` Sean Christopherson
2021-04-06 16:07 ` Sean Christopherson [this message]
2021-04-06 8:26 ` [PATCH v3 2/4] Documentation: KVM: update KVM_GET_EMULATED_CPUID ioctl description Emanuele Giuseppe Esposito
2021-04-06 8:26 ` [PATCH v3 3/4] selftests: add kvm_get_emulated_cpuid to processor.h Emanuele Giuseppe Esposito
2021-04-06 8:26 ` [PATCH v3 4/4] selftests: KVM: extend get_cpuid_test to include KVM_GET_EMULATED_CPUID Emanuele Giuseppe Esposito
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=YGyHTc0uJtrJY0gh@google.com \
--to=seanjc@google.com \
--cc=bp@alien8.de \
--cc=corbet@lwn.net \
--cc=drjones@redhat.com \
--cc=eesposit@redhat.com \
--cc=graf@amazon.com \
--cc=hpa@zytor.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=shuah@kernel.org \
--cc=vkuznets@redhat.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).