From nobody Wed Apr 8 17:33:30 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EAFEFFA3740 for ; Thu, 27 Oct 2022 16:20:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236633AbiJ0QUE (ORCPT ); Thu, 27 Oct 2022 12:20:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236572AbiJ0QTB (ORCPT ); Thu, 27 Oct 2022 12:19:01 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6375D1958FA for ; Thu, 27 Oct 2022 09:18:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666887536; 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=W4jlslF5VNxKyOc4aJjoGknzZszIqLvBjQKfttjSBRM=; b=DDTtOA5w+u/8MpcA8VWe7aN83cHq7kqmTEtC+ZTd5ZmMJD4/nWVSnxY4S1RQ9/8oNvnmDV EctpbIsmYsNjfaiCNuWq/PUbce4hTR7gHZ//s5nbHIxrTtgXh0BArA/jCFNebLWb5FKhom Pu4QOAwA7vEyGjPKwHHv3rJ7X6qE5WA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-306-tkL1K6RmOhWJ2BYpozAGpg-1; Thu, 27 Oct 2022 12:18:52 -0400 X-MC-Unique: tkL1K6RmOhWJ2BYpozAGpg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8F6F9811E67; Thu, 27 Oct 2022 16:18:52 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6E6BE17593; Thu, 27 Oct 2022 16:18:52 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: mhal@rbox.co, seanjc@google.com Subject: [PATCH 12/16] KVM: Do not partially reinitialize gfn=>pfn cache during activation Date: Thu, 27 Oct 2022 12:18:45 -0400 Message-Id: <20221027161849.2989332-13-pbonzini@redhat.com> In-Reply-To: <20221027161849.2989332-1-pbonzini@redhat.com> References: <20221027161849.2989332-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Sean Christopherson Don't partially reinitialize a gfn=3D>pfn cache when activating the cache, and instead assert that the cache is not valid during activation. Bug the VM if the assertion fails, as use-after-free and/or data corruption is all but guaranteed if KVM ends up with a valid-but-inactive cache. Signed-off-by: Sean Christopherson Message-Id: <20221013211234.1318131-13-seanjc@google.com> Signed-off-by: Paolo Bonzini --- virt/kvm/pfncache.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 62429b2a6389..06fcf03c2da6 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -347,6 +347,8 @@ void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct = kvm *kvm, gpc->kvm =3D kvm; gpc->vcpu =3D vcpu; gpc->usage =3D usage; + gpc->pfn =3D KVM_PFN_ERR_FAULT; + gpc->uhva =3D KVM_HVA_ERR_BAD; } EXPORT_SYMBOL_GPL(kvm_gpc_init); =20 @@ -355,10 +357,8 @@ int kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa= _t gpa, unsigned long len) struct kvm *kvm =3D gpc->kvm; =20 if (!gpc->active) { - gpc->khva =3D NULL; - gpc->pfn =3D KVM_PFN_ERR_FAULT; - gpc->uhva =3D KVM_HVA_ERR_BAD; - gpc->valid =3D false; + if (KVM_BUG_ON(gpc->valid, kvm)) + return -EIO; =20 spin_lock(&kvm->gpc_lock); list_add(&gpc->list, &kvm->gpc_list); --=20 2.31.1