Received: by 2002:aa6:c781:0:b0:139:2837:6657 with SMTP id u1csp1540897lkq; Fri, 3 Sep 2021 09:18:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/qMB+RrfTYekAlkNau2TB7+el9QbEALl9K6XWRafAqu1sJNe7nz0R16LrlqRAAWcn9eMt X-Received: by 2002:a05:6e02:1107:: with SMTP id u7mr2966276ilk.39.1630685883929; Fri, 03 Sep 2021 09:18:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630685883; cv=none; d=google.com; s=arc-20160816; b=jpPELdg/4Ue0mrQKp7eAapl+q5EpCrUoRUqfGpKO9L7E0uF5KXrCn/iPc4E3eC7LCu Wu+nGjJ3WuUJZ6DkLfpZzC2MpZknpaAu3eNvzZ054xXOV5d6qPjWJ21otuoxy6Bz2ftj VlgH7BssFjT5u6WY7jnwcFt3zWVpuvBfEZoaLzAmG3Ml8o3SVwJtNPq6biJTV+12ERZb ZuKVzyx1a2VCnRpzxyHD2PXwP5NSZzN8VW15IyoCl5YmRiDPRmsNLSPvd4RkhQFzYVED 0lNZSWoBzYp9zERxPms40nKMCfYv9u7WWyBFM3ADO/R8thCgLOPHWFX1NNTD6CTfy1wd jpPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=Orf48Qymoz5f+fX7Ah1Z1mFWpxtUTmn8u/iZMkyoKa8=; b=BKAR/ACazW51iPcJZgpeQ5c4O7Naq0vGXd7JidExwpKnwuVPl7oa4SvTfFIzeVbpgT KBbKoT3boznaxzj20ekBYiFe9wCw3gokDk1q3g24+RY4uVCDdlvDciQiOvYfOiXhNuR9 Vif0HgL+/sLcP3WlZAThEXfQkKMv5erH79W3FsTi22umZ6p3BAjaaiqXhje9IzxqQq6e hwfoobu9BIwvCB2+X2kTODuN4mITPYBpjBAAEtXlz6MxJx/RrPafU+n/SE7A5hc2RSSS SfBEzCAfXJQwgMXE1TEYDIrz9olsBEI4qMXgechXBoA3KL5IXq1git3NG4KkJdxyBinb OY7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=muP6k7WV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y4si7637995jas.59.2021.09.03.09.17.49; Fri, 03 Sep 2021 09:18:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=muP6k7WV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234934AbhICPEo (ORCPT + 99 others); Fri, 3 Sep 2021 11:04:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231959AbhICPEn (ORCPT ); Fri, 3 Sep 2021 11:04:43 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7B8BCC061575 for ; Fri, 3 Sep 2021 08:03:42 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id 8so5786130pga.7 for ; Fri, 03 Sep 2021 08:03:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=Orf48Qymoz5f+fX7Ah1Z1mFWpxtUTmn8u/iZMkyoKa8=; b=muP6k7WVk+ZfwM3tx/7+DdpIz9xmUlW4n/hJJnkrKchGopWEJO7QLAbBVE0U8Nkds+ 8FZCaW8U2ekAW/6Ss6smKim4za9a6ycNE92CKdQBCF4n3WFI45TXQHMjiYD0NXTvhshQ 3gmyAPgu0il8Jua3FWlp6kcau+AqlBBYgU/oeyDNk8rOSwjd/KsLnnLyUGZiOy+isKvr swTmcyZ/XwQ5/2S6wuBm5qo4B16waZjJR53R4HluPovZ+H54xcttsDp9q0JqpkzfKqnt rPcrIdZZ9GyDg9H4zuAO78rCWCIjt/wLxWxaXXHCLY5WNtS45ryk8KXqoR55JWZVP5Xp H2+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=Orf48Qymoz5f+fX7Ah1Z1mFWpxtUTmn8u/iZMkyoKa8=; b=t2ggBjokBBuY7EGZYi3TrYgKMrjfrEyYXyF682ujw8KFKEqa/6QhW+Nq7HBZOMb4ST KPH3oY+mI7wIQIMAnR+DSWVFBfx+QOXY9qDfCyXpf/922me7F1LQsy75kWexTN7pt/fB qOL9bYzP3x/T8x30aqrywr/GENip6td7Tc2FQ3jPhUC9Qmf7DVGwgG6cxOyYbmLvtUdz IfaQeg+PJU2aMzChTLDFrAwK/bNcVgm3K1wFX6Effqbx+GpY4W46Vox3XdIh135oyuZX NkOlkM8W4WBcybSXn4Bv6yp8asW7Ee/f4Tzd+uVaZgYTH1eqU3DByNttBvBUKj72ObGx sVNw== X-Gm-Message-State: AOAM531vSLE515CnD9E/PTxBPfIajVYHIrVnPVCknZcwNhV/wO3qUiat hsdpgv0hHCjtx8HDqgZNzCX0hw== X-Received: by 2002:a65:63d0:: with SMTP id n16mr3958589pgv.432.1630681421785; Fri, 03 Sep 2021 08:03:41 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id 70sm5899848pfu.93.2021.09.03.08.03.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 08:03:41 -0700 (PDT) Date: Fri, 3 Sep 2021 15:03:37 +0000 From: Sean Christopherson To: Vitaly Kuznetsov Cc: Jiang Jiasheng , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, pbonzini@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, jarkko@kernel.org, dave.hansen@linux.intel.com Subject: Re: [PATCH 4/4] KVM: X86: Potential 'index out of range' bug Message-ID: References: <1630655700-798374-1-git-send-email-jiasheng@iscas.ac.cn> <87czppnasv.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87czppnasv.fsf@vitty.brq.redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 03, 2021, Vitaly Kuznetsov wrote: > Jiang Jiasheng writes: > > > The kvm_get_vcpu() will call for the array_index_nospec() > > with the value of atomic_read(&(v->kvm)->online_vcpus) as size, > > and the value of constant '0' as index. > > If the size is also '0', it will be unreasonabe > > that the index is no less than the size. > > > > Can this really happen? > > 'online_vcpus' is never decreased, it is increased with every > kvm_vm_ioctl_create_vcpu() call when a new vCPU is created and is set to > 0 when all vCPUs are destroyed (kvm_free_vcpus()). > > kvm_guest_time_update() takes a vcpu as a parameter, this means that at > least 1 vCPU is currently present so 'online_vcpus' just can't be zero. Agreed, but doing kvm_get_vcpu() is ugly and overkill. diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 86539c1686fa..cc1cb9a401cd 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2969,7 +2969,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) offsetof(struct compat_vcpu_info, time)); if (vcpu->xen.vcpu_time_info_set) kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0); - if (v == kvm_get_vcpu(v->kvm, 0)) + if (!kvm_vcpu_get_idx(v)) kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); return 0; }