Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp411033pxb; Fri, 3 Sep 2021 05:09:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzIEiYvwQeXUTpqrum2B9gjYRx4gIVJQE8VGIHQGaEbagD6Tumyf1giNaaGHdx0BeQ3PO9C X-Received: by 2002:a1c:7c0e:: with SMTP id x14mr8155901wmc.30.1630670974076; Fri, 03 Sep 2021 05:09:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630670974; cv=none; d=google.com; s=arc-20160816; b=XVo1MOpbo25howKM2Ejyqi1cCwaPsELjhBGpscsHA62C0eOlmby8i1ua9k/rbF8WIJ j2nA+ke9a/sVlf4vFcN2RY4Zq4n124Kjdjca6UpE8hxy8yYGIPTck8FHxxLOThSe3qGm ZC5qPbLAFh0Fkj/JYx6QDRAhIV/fffHkqVv50eK9Bm07uDyAOz05ojyoaN5ZsZbJfyYQ JdyoRwN1uiWEomW8PnAB9if3Ww/jch45XtqAcO7sw2ff5JHquGjkzhdExW00zFAfwb76 OWQdpp8gq4Fo23al4V0wmSpVtNumQiEYLoO+qbvJxeUoBetPS/0TcFSglPFerhErNXlz 8TMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=HpnKnHLGzPAJzdSBbsC3pLsaD4HC3lArEEuQ/lWlcJQ=; b=bWNonbhw+3ESkl+NAL3M2JHwrKbipGFZJV32cUth/hBRsLWKAY1fEMTQ20KpdgymQd +fcPWAsY0vGHb1AZU23qStLCPHc6nDmVFvBW4A5fc9mqP+F7XNwuKMGELWIhWq3zm6zX bBhvAImv9OXkcc0farCchqwVPU8as2HWJ9ugez6tfNdvhS0OV7AwvYoSGkBCJdU7Sqos VJBjGl+qfjVzXuUevuaNUTWsXh3vN9bgkJjZFLugJWvzz1ogVoSv4or0tBUJW6rMg7Qz d2y0O6d2pFihhWhKpIddU2Gcst8puoehja/NxvRPWXGoOukOkHXApW106o9v9c9Ilrpy BYRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IeG+L9JM; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l12si5363790edb.8.2021.09.03.05.09.07; Fri, 03 Sep 2021 05:09:34 -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=@redhat.com header.s=mimecast20190719 header.b=IeG+L9JM; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348247AbhICLpY (ORCPT + 99 others); Fri, 3 Sep 2021 07:45:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:38514 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348664AbhICLpU (ORCPT ); Fri, 3 Sep 2021 07:45:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630669460; 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: in-reply-to:in-reply-to:references:references; bh=HpnKnHLGzPAJzdSBbsC3pLsaD4HC3lArEEuQ/lWlcJQ=; b=IeG+L9JMi1VdrHVbbN/ZiuERp+0L09BPOLAprxjOzlF8sP23qEWo7StkM4ZK0AEzVm+NHk 2IOYxM8rrUiXBIQkP/sHdtUchji63tds/1skb/ezI1AJiE07NL1eVfqlQxz9s6qCxHqHNX TY32j7mnGCYYjkepsR4yJc38gMYDUOo= 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-206-CcZhGcYyP7WHqLXOaiu_wQ-1; Fri, 03 Sep 2021 07:44:19 -0400 X-MC-Unique: CcZhGcYyP7WHqLXOaiu_wQ-1 Received: by mail-wm1-f72.google.com with SMTP id a201-20020a1c7fd2000000b002e748bf0544so2564705wmd.2 for ; Fri, 03 Sep 2021 04:44:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=HpnKnHLGzPAJzdSBbsC3pLsaD4HC3lArEEuQ/lWlcJQ=; b=Ir/73RCjeuuPmtRIot4t88bkPq6ftbkbhWPLRk2NlYzmlOZgqVPSx4/L11f5JKZkl+ GoORzbJujr7l6Oy5eFM8s8lDAs+JJLyUnzmfv59/4FgeshVOfdS7coK2c4x/TaEAVS3y XQLBIBB5maM6zxUg0qaEXdfsEGOdHSJ7Fzw2F0MZN+Yeh6sSBeerEHFAN7nJT9reAZZH fQ+uGVcgUYRP28E8fzqoEtUo1dpKP1hlDlu8vQoyxDoZ2tSxf5y/9faJHlJ3/EjjvGjN Zq3nfG7f0p5ipjwlhdprbIqRTCtcxfzADH0/NENIGF73M6ECc22Ltra0OGdhOHjBNTbd 6VOA== X-Gm-Message-State: AOAM531N601dC1yii0zb+n7xkKLHZzAIIgsgtYzOmoC5G/pFvh9JaYBx K6NKlqejBYoNCRPW+PwuEuGX7wNKPuM8jALBYVdccsRLd0Ub2HDQrV8QeudZauA0N4AFQV2x3mM +UoMbXBMX8+3iRLJcooczKXe1 X-Received: by 2002:a1c:cc05:: with SMTP id h5mr7972770wmb.5.1630669458564; Fri, 03 Sep 2021 04:44:18 -0700 (PDT) X-Received: by 2002:a1c:cc05:: with SMTP id h5mr7972738wmb.5.1630669458289; Fri, 03 Sep 2021 04:44:18 -0700 (PDT) Received: from vitty.brq.redhat.com (g-server-2.ign.cz. [91.219.240.2]) by smtp.gmail.com with ESMTPSA id u27sm4682490wru.2.2021.09.03.04.44.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 04:44:17 -0700 (PDT) From: Vitaly Kuznetsov To: Jiang Jiasheng Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, Jiang Jiasheng , pbonzini@redhat.com, seanjc@google.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 In-Reply-To: <1630655700-798374-1-git-send-email-jiasheng@iscas.ac.cn> References: <1630655700-798374-1-git-send-email-jiasheng@iscas.ac.cn> Date: Fri, 03 Sep 2021 13:44:16 +0200 Message-ID: <87czppnasv.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > Signed-off-by: Jiang Jiasheng > --- > arch/x86/kvm/x86.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index e0f4a46..c59013c 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -2871,7 +2871,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 (atomic_read(&(v->kvm)->online_vcpus) > 0 && v == kvm_get_vcpu(v->kvm, 0)) > kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); > return 0; > } -- Vitaly