Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp2040303pxb; Fri, 24 Sep 2021 18:49:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxIT6SbIYOi3CGDmG82/4lb4mwaXAyrlfHDXcpUMWVqXej4p58FHLl7FETt8uSS4lE+fDif X-Received: by 2002:a17:906:4ed2:: with SMTP id i18mr14529233ejv.528.1632534543748; Fri, 24 Sep 2021 18:49:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632534543; cv=none; d=google.com; s=arc-20160816; b=FmoZcHm9QZ7Dd+YTxVTNRb8au2U2fdl2qJfuvhe1FaXSmaPJy4yCMF5LDpSV/FdExB wJsHhahePCLOGWg9a1iNPwaGZsco20NcM6vCfYcQW7ulsUOd1VVa7Zs235SI+y9Z20ef 3gU/zjvSJJ9EL8SwF0AUrroervLWW0OCNuod5MhoZtLdtjzD+ewKTXL/jdPS/lDB6A8Y tKfTfBvkQ+0XsMOzq8/0BOjRuDW/QcPT82d9bXz0L4i1C4hpifi+MisIIN4VSmmwJIQA IOPRoqNFeMhgEO5Yx13PwULDJUShaIWy1lP46VcJMkFY22dsJSh6tYUUPsLwrAGHjPzf uxYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=pvBapdQ+zIzAmyxrrOIhvPAnOMqQp2FLAZOjpWmFNDo=; b=UpD+A4eJZVgu5UAP/G0tmnW9BNnirqCDLdSBBqqCS4n6Qf0dVcWntVCmGrhiM7aNg7 FGCWfy0yb1ocQJ+LbwoHY9Z4bsBSIe8cVGSVceZfsy9dNISKe/2Lh/aSecy4HRw7i/rO 6Kj8Xm7yQ2tV18HwVAb70Fv+5qAx5oAwpmQ6faPaxg7x/yoep/jFXbYaa34iLAB33IXQ Eyle6sQHfbVHKnN6H0QnpB6/IWwuVPQwvmUP6WD/kDorisevi3/jHL92NJ2WsgUOHVe1 6gbgqtETdKs9hzNDMCZQ/dNRphw10qgosUu8nM09r+NjxKtH5YtoLGfG8fgzz1arYuCU WeJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VsSwpDDI; 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 v2si10772765ejk.783.2021.09.24.18.48.25; Fri, 24 Sep 2021 18:49: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=@redhat.com header.s=mimecast20190719 header.b=VsSwpDDI; 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 S1347735AbhIXQeF (ORCPT + 99 others); Fri, 24 Sep 2021 12:34:05 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:53522 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347483AbhIXQdj (ORCPT ); Fri, 24 Sep 2021 12:33:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1632501125; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pvBapdQ+zIzAmyxrrOIhvPAnOMqQp2FLAZOjpWmFNDo=; b=VsSwpDDIy7YFs3CH8dEBNEiyXfuJg0Lseyr4cxxGwa3x6IqdPCuUrYIQfSeQMN8nHuEyYi DTRNaeo2hOT0PncBN1eZ0to7dC/KYsEat3Megdo9w5e6cWy1ycR5M1kUjXjykpAzWGUMUa rJ44LzEEp4xFj3pc250K8ZrvB0oIbAI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-65-zQLAOaFBMIa6AFkmt1myaw-1; Fri, 24 Sep 2021 12:32:04 -0400 X-MC-Unique: zQLAOaFBMIa6AFkmt1myaw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id AF0FC10168D0; Fri, 24 Sep 2021 16:32:02 +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 24C5E60E1C; Fri, 24 Sep 2021 16:32:02 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: dmatlack@google.com, seanjc@google.com, Lai Jiangshan , Lai Jiangshan Subject: [PATCH v3 16/31] KVM: x86/mmu: Verify shadow walk doesn't terminate early in page faults Date: Fri, 24 Sep 2021 12:31:37 -0400 Message-Id: <20210924163152.289027-17-pbonzini@redhat.com> In-Reply-To: <20210924163152.289027-1-pbonzini@redhat.com> References: <20210924163152.289027-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson WARN and bail if the shadow walk for faulting in a SPTE terminates early, i.e. doesn't reach the expected level because the walk encountered a terminal SPTE. The shadow walks for page faults are subtle in that they install non-leaf SPTEs (zapping leaf SPTEs if necessary!) in the loop body, and consume the newly created non-leaf SPTE in the loop control, e.g. __shadow_walk_next(). In other words, the walks guarantee that the walk will stop if and only if the target level is reached by installing non-leaf SPTEs to guarantee the walk remains valid. Opportunistically use fault->goal-level instead of it.level in FNAME(fetch) to further clarify that KVM always installs the leaf SPTE at the target level. Reviewed-by: Lai Jiangshan Signed-off-by: Sean Christopherson Signed-off-by: Lai Jiangshan Message-Id: <20210906122547.263316-1-jiangshanlai@gmail.com> Signed-off-by: Paolo Bonzini --- arch/x86/kvm/mmu/mmu.c | 3 +++ arch/x86/kvm/mmu/paging_tmpl.h | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 5ba0a844f576..2ddbabad5bd2 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3012,6 +3012,9 @@ static int __direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) account_huge_nx_page(vcpu->kvm, sp); } + if (WARN_ON_ONCE(it.level != fault->goal_level)) + return -EFAULT; + ret = mmu_set_spte(vcpu, it.sptep, ACC_ALL, fault->write, fault->goal_level, base_gfn, fault->pfn, fault->prefault, fault->map_writable); diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index 6bc0dbc0baff..7a8a2d14a3c7 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -760,9 +760,12 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, } } + if (WARN_ON_ONCE(it.level != fault->goal_level)) + return -EFAULT; + ret = mmu_set_spte(vcpu, it.sptep, gw->pte_access, fault->write, - it.level, base_gfn, fault->pfn, fault->prefault, - fault->map_writable); + fault->goal_level, base_gfn, fault->pfn, + fault->prefault, fault->map_writable); if (ret == RET_PF_SPURIOUS) return ret; -- 2.27.0