From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7012C433ED for ; Fri, 9 Apr 2021 12:34:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BE8B4610FC for ; Fri, 9 Apr 2021 12:34:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233028AbhDIMfA (ORCPT ); Fri, 9 Apr 2021 08:35:00 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:36791 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232855AbhDIMe4 (ORCPT ); Fri, 9 Apr 2021 08:34:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617971681; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WrEm6vZHVri6hjV+VlEozAzJGJuNVgmg6MRBLGVidsk=; b=O3zTGjnJIVAR7QMEFSiU8/2Nhp2BeXtq+dPKSWmseCY42xccjRjOjbw3oN2/sTeJRCORnQ 5QIWNFfn1UNiJWod5945YLFQAKO4W0Qpz2l85gwKCLSAVZZoHnl/DsvLiB4JCHEaxRfTIH p8cPPyMKYW/KmCBTbwoTxc9kQDLxKJ8= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-222-0x-WCCAhPiOy7j02Jglp1A-1; Fri, 09 Apr 2021 08:34:39 -0400 X-MC-Unique: 0x-WCCAhPiOy7j02Jglp1A-1 Received: by mail-wm1-f72.google.com with SMTP id h10-20020a05600c350ab029010cff8d09edso506493wmq.4 for ; Fri, 09 Apr 2021 05:34:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=WrEm6vZHVri6hjV+VlEozAzJGJuNVgmg6MRBLGVidsk=; b=EB17s5FW+lx1te/A16vluaOWy86v5LX7OiVHmYHrqdiA2GOP1CYrxqIAq3MRZ0kxv4 mdV/2NNVvcNiaoC9Hn5Sm8uSBJeEKt8HQ0KliPMkG+8puLDnzk0yX6f9oBcKV3imlqLT 22sU1PuSoJpR3/FENTXXj107CNG2YcfQNATo81KyY5GDRO6otZ4NkxlGZ7xGqxft5CUo W4KjqFjxG1F/3XY3+dUWmbCz8J1HbXoC9OJqlAjc7YDizU532DSg9/0hnYRnQtmLemEP JLmHl1asiSjfehh1/yPTUyRj3FC7MGpB95Lu2P/2e2IrXpPp6wtFe+vBCRGKr9XH7+5w ZUSg== X-Gm-Message-State: AOAM530D1R4bnV/O9iKF1L4JbSSKkSusVoItoiye/A26sa1FphLJD+vf eHY9H5loeFfpFBIyVLe1VkREUnk4SOCCbTPnK9SDyAXX/WHpMWZQ50/YQdZxjCs/9b/FZKcjwNO A1Wc7gO+Mq12tfto83Q4ppISG X-Received: by 2002:a5d:644e:: with SMTP id d14mr17123592wrw.339.1617971678239; Fri, 09 Apr 2021 05:34:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxUGpD7HjO5daDt1oGsFMpgGK4lWP5dsiPMLmwvcZD85K+7Mp5kdBf3eYobr4sxrvRT5Q9mQ== X-Received: by 2002:a5d:644e:: with SMTP id d14mr17123579wrw.339.1617971678102; Fri, 09 Apr 2021 05:34:38 -0700 (PDT) Received: from localhost.localdomain ([194.230.155.173]) by smtp.gmail.com with ESMTPSA id p17sm3774470wmg.5.2021.04.09.05.34.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 09 Apr 2021 05:34:36 -0700 (PDT) Subject: Re: [PATCH v4 1/4] KVM: x86: Fix a spurious -E2BIG in KVM_GET_EMULATED_CPUID To: Sean Christopherson Cc: kvm@vger.kernel.org, Paolo Bonzini , Jonathan Corbet , Vitaly Kuznetsov , Jim Mattson , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Shuah Khan , Alexander Graf , Andrew Jones , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org References: <20210408114303.30310-1-eesposit@redhat.com> <20210408114303.30310-2-eesposit@redhat.com> From: Emanuele Giuseppe Esposito Message-ID: <74b017e4-5a44-e20f-3435-ec48c4927ec4@redhat.com> Date: Fri, 9 Apr 2021 14:34:35 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 08/04/2021 22:29, Sean Christopherson wrote: > On Thu, Apr 08, 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. >> >> Fixes: 433f4ba19041 ("KVM: x86: fix out-of-bounds write in KVM_GET_EMULATED_CPUID (CVE-2019-19332)") >> Signed-off-by: Emanuele Giuseppe Esposito >> --- >> 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..d30194081892 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)); >> >> 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.ecx = F(RDPID); >> break; >> + default: >> + goto out; >> } >> >> + /* This check is performed only when func is valid */ > > Sorry to keep nitpicking and bikeshedding. No problem at all. Any comment is very welcome :) Funcs aren't really "invalid", KVM > just doesn't have any features it emulates in other leafs. Maybe be more literal > in describing what triggers the check? > > /* Check the array capacity iff the entry is being copied over. */ What I mean here is that a func is "valid" if it matches one of the cases of the switch statement. If it is not valid, it ends up in the default case. But I agree, will change the comment your suggestion and resend. Thank you, Emanuele > > Not a sticking point, so either way: > > Reviewed-by: Sean Christopherson > >> + if (array->nent >= array->maxnent) >> + return -E2BIG; >> + >> + entry.function = func; >> + memcpy(&array->entries[array->nent++], &entry, sizeof(entry)); >> + >> +out: >> return 0; >> } >> >> -- >> 2.30.2 >> >