From nobody Thu Apr 2 20:25:32 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2C0EC3A545F for ; Thu, 26 Mar 2026 17:50:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774547406; cv=none; b=uAgZEzt4ve1dfO2QNRr/tRo6Kblfo62EpD2eAv4/k+7MLa3+PNKvvD4RQRcHcVAmN9NrqKHpTU5z76J5ps3K5BWXfcRi6oLwmgiLsDd7KOsNE012hL9vCSRIEBB8qH7nxxgSLMdjfgMM878JX0fZD4mRg7IoLvPSJ46yVitbO1M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774547406; c=relaxed/simple; bh=7SI+MHaHsHfhtgMbQHFaI8EaRKrXuYVzdDzUpM37mAE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IgJI3a5TosF7NPHNBHUKXmHPqZJfouGarkXPqBM4Hko103VmOwh+jbVpoMrxOmTid33BMh2649Tp3ZTjKNbiv/oyZgE88Q8/dKNjlJrArUsw2xKeOsLJQSwWFjkoiEkrFuboFdnrAAJiRa9s8Q2GC+f6O95L9f782PSsRPFJlsw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jmattson.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Njh/rOS/; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jmattson.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Njh/rOS/" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-35c1874336aso3788037a91.3 for ; Thu, 26 Mar 2026 10:50:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774547404; x=1775152204; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dAH8qf9G0XQlUHg8hq3D+h8w7UbFbHHt2UywU7wF7JE=; b=Njh/rOS/DQpG/JBpBRz9hHreITlszVQxp/rdncGTtRmb9fwU2u6PMyXh6KulaEGX2r Ie0CICDKukhmEkKglwgB8ie7VC7Pr4+L5j5iYEx5t0rl0Tb1BAK3QJl0TYwI+BMzbyZm a3VzllUAi0FjlPL/v9XeXC3nwWLk0Z/VFmsxePXIuxmPg2mvFDmBFdzqedt0RZFcxc5L fKjsFeQ2+tC8MSD2eMz48qH5yGu1R3vnTyc7VsfhpkAtA4pvkuYF0DLkt09qJBmS7lHA cfzs0e3eIUpy4NVpHPKBQPhVXXtmcv1xMf4O0zdKMWSJb8gKf+jogcWcxym9QDMaaMZL zT/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774547404; x=1775152204; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dAH8qf9G0XQlUHg8hq3D+h8w7UbFbHHt2UywU7wF7JE=; b=mz8XT73XM6IFIzknPzDg4XlV8OS/LcqJAwpx1i29ieE9O0G32IgULi7FWci0UglAV4 OHwT24R2oMLslDGN0kIQqwVBwqzd3MLRJTRiUTVcCTfiwQ7YFPc9zylp9m1hDjc6N1wD ahvEJgM6NlU8F0Q2vtoB6UkTIf+uLm8yLYVRVvVYqqSvs7FgptAKfM1UHGx7IAVYeCxj NlUbgLJXMrZX++79rlCtBYOjIgFZUqBBrXVoqA8B6dg238BkxsO6ixiAafSEKJCg31Gj R3/bp1Ik/3A6qfoCpJDj8wLXV7RRuGBkGgUqY4HGLnxBjvrpO+UsoUaZum32BVxRuG/h 8ZnA== X-Forwarded-Encrypted: i=1; AJvYcCWRnf8ghjituHXtdzQ2C/8ip7CKyXk6ZEkglaGns4kScA98hj2AYnzItWTWt0FuRkPP3THb8tx9qhuMAfk=@vger.kernel.org X-Gm-Message-State: AOJu0YxLawdV5SfHvckVuVXHVD46qCPx9zOQHGDktNUkRRO0+HgVeuFr 6AV+Fnz9emSPslmeYFdFGcBqiE9WUDOFFyfa9hkueZq3CpKV8p2O4C1iOgBWXnlfD+5nvZ8345p IYA6BFgJN9q8Fwg== X-Received: from pjbie10.prod.google.com ([2002:a17:90b:400a:b0:35a:294:ab56]) (user=jmattson job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:e183:b0:35b:9d0c:a2f5 with SMTP id 98e67ed59e1d1-35c0ddf1215mr8406645a91.33.1774547404300; Thu, 26 Mar 2026 10:50:04 -0700 (PDT) Date: Thu, 26 Mar 2026 10:49:21 -0700 In-Reply-To: <20260326174944.3820245-1-jmattson@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260326174944.3820245-1-jmattson@google.com> X-Mailer: git-send-email 2.53.0.1018.g2bb0e51243-goog Message-ID: <20260326174944.3820245-4-jmattson@google.com> Subject: [PATCH v6 03/10] KVM: x86: nSVM: Cache and validate vmcb12 g_pat From: Jim Mattson To: Paolo Bonzini , Jonathan Corbet , Shuah Khan , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Yosry Ahmed Cc: Jim Mattson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT is disabled and nested paging is enabled in vmcb12, validate g_pat at emulated VMRUN and cause an immediate VMEXIT with exit code VMEXIT_INVALID if it is invalid, as specified in the APM, volume 2: "Nested Paging and VMRUN/VMEXIT." Fixes: 3d6368ef580a ("KVM: SVM: Add VMRUN handler") Signed-off-by: Jim Mattson --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/nested.c | 17 +++++++++++++---- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 0809d8f28208..0b4ab141feae 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2511,7 +2511,7 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, = unsigned long npages); KVM_X86_QUIRK_SLOT_ZAP_ALL | \ KVM_X86_QUIRK_STUFF_FEATURE_MSRS | \ KVM_X86_QUIRK_IGNORE_GUEST_PAT | \ - KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM \ + KVM_X86_QUIRK_VMCS12_ALLOW_FREEZE_IN_SMM | \ KVM_X86_QUIRK_NESTED_SVM_SHARED_PAT) =20 #define KVM_X86_CONDITIONAL_QUIRKS \ diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 32fa8e688c00..0ad1e0348492 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -410,7 +410,8 @@ static bool nested_vmcb_check_controls(struct kvm_vcpu = *vcpu, =20 /* Common checks that apply to both L1 and L2 state. */ static bool nested_vmcb_check_save(struct kvm_vcpu *vcpu, - struct vmcb_save_area_cached *save) + struct vmcb_save_area_cached *save, + bool check_gpat) { if (CC(!(save->efer & EFER_SVME))) return false; @@ -445,6 +446,9 @@ static bool nested_vmcb_check_save(struct kvm_vcpu *vcp= u, if (CC(!kvm_valid_efer(vcpu, save->efer))) return false; =20 + if (check_gpat && CC(!kvm_pat_valid(save->g_pat))) + return false; + return true; } =20 @@ -452,7 +456,8 @@ int nested_svm_check_cached_vmcb12(struct kvm_vcpu *vcp= u) { struct vcpu_svm *svm =3D to_svm(vcpu); =20 - if (!nested_vmcb_check_save(vcpu, &svm->nested.save) || + if (!nested_vmcb_check_save(vcpu, &svm->nested.save, + l2_has_separate_pat(svm)) || !nested_vmcb_check_controls(vcpu, &svm->nested.ctl)) return -EINVAL; =20 @@ -562,6 +567,7 @@ static void __nested_copy_vmcb_save_to_cache(struct vmc= b_save_area_cached *to, =20 to->rax =3D from->rax; to->cr2 =3D from->cr2; + to->g_pat =3D from->g_pat; =20 svm_copy_lbrs(to, from); } @@ -1971,13 +1977,16 @@ static int svm_set_nested_state(struct kvm_vcpu *vc= pu, =20 /* * Validate host state saved from before VMRUN (see - * nested_svm_check_permissions). + * nested_svm_check_permissions). Note that the g_pat field is not + * validated, because (a) it may have been clobbered by SMM before + * KVM_GET_NESTED_STATE, and (b) it is not loaded at emulated + * #VMEXIT. */ __nested_copy_vmcb_save_to_cache(&save_cached, save); if (!(save->cr0 & X86_CR0_PG) || !(save->cr0 & X86_CR0_PE) || (save->rflags & X86_EFLAGS_VM) || - !nested_vmcb_check_save(vcpu, &save_cached)) + !nested_vmcb_check_save(vcpu, &save_cached, false)) goto out_free; =20 =20 diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 0f0c08c5ce0c..3588f6d3fb9b 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -161,6 +161,7 @@ struct vmcb_save_area_cached { u64 isst_addr; u64 rax; u64 cr2; + u64 g_pat; u64 dbgctl; u64 br_from; u64 br_to; --=20 2.53.0.1018.g2bb0e51243-goog