linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosry.ahmed@linux.dev>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Jim Mattson <jmattson@google.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yosry Ahmed <yosry.ahmed@linux.dev>
Subject: [PATCH 02/11] KVM: nSVM: Add missing consistency check for EFER, CR0, CR4, and CS
Date: Tue,  4 Nov 2025 19:59:40 +0000	[thread overview]
Message-ID: <20251104195949.3528411-3-yosry.ahmed@linux.dev> (raw)
In-Reply-To: <20251104195949.3528411-1-yosry.ahmed@linux.dev>

According to the APM Volume #2, 15.5, Canonicalization and Consistency
Checks (24593—Rev. 3.42—March 2024), the following condition (among
others) results in a #VMEXIT with VMEXIT_INVALID (aka SVM_EXIT_ERR):

  EFER.LME, CR0.PG, CR4.PAE, CS.L, and CS.D are all non-zero.

Add the missing consistency check. This is functionally a nop because
the nested VMRUN results in SVM_EXIT_ERR in HW, which is forwarded to
L1, but KVM makes all consistency checks before a VMRUN is actually
attempted.

Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev>
---
 arch/x86/kvm/svm/nested.c | 7 +++++++
 arch/x86/kvm/svm/svm.h    | 1 +
 2 files changed, 8 insertions(+)

diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 9a534f04bdc83..9866b2fd8f32a 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -380,6 +380,11 @@ static bool __nested_vmcb_check_save(struct kvm_vcpu *vcpu,
 		    CC(!(save->cr0 & X86_CR0_PE)) ||
 		    CC(!kvm_vcpu_is_legal_cr3(vcpu, save->cr3)))
 			return false;
+
+		if (CC((save->cr4 & X86_CR4_PAE) &&
+		       (save->cs.attrib & SVM_SELECTOR_L_MASK) &&
+		       (save->cs.attrib & SVM_SELECTOR_DB_MASK)))
+			return false;
 	}
 
 	/* Note, SVM doesn't have any additional restrictions on CR4. */
@@ -473,6 +478,8 @@ static void __nested_copy_vmcb_save_to_cache(struct vmcb_save_area_cached *to,
 	 * Copy only fields that are validated, as we need them
 	 * to avoid TOC/TOU races.
 	 */
+	to->cs = from->cs;
+
 	to->efer = from->efer;
 	to->cr0 = from->cr0;
 	to->cr3 = from->cr3;
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 0a2908e22d746..efcf923264c55 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -142,6 +142,7 @@ struct kvm_vmcb_info {
 };
 
 struct vmcb_save_area_cached {
+	struct vmcb_seg cs;
 	u64 efer;
 	u64 cr4;
 	u64 cr3;
-- 
2.51.2.1026.g39e6a42477-goog


  parent reply	other threads:[~2025-11-04 20:00 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-04 19:59 [PATCH 00/11] Nested SVM fixes, cleanups, and hardening Yosry Ahmed
2025-11-04 19:59 ` [PATCH 01/11] KVM: nSVM: Fix consistency checks for NP_ENABLE Yosry Ahmed
2025-11-05 18:52   ` Yosry Ahmed
2025-11-04 19:59 ` Yosry Ahmed [this message]
2025-11-04 19:59 ` [PATCH 03/11] KVM: nSVM: Add missing consistency check for event_inj Yosry Ahmed
2025-11-05 18:48   ` Sean Christopherson
2025-11-05 19:29     ` Yosry Ahmed
2025-11-06  1:17       ` Sean Christopherson
2025-11-08  2:29         ` Yosry Ahmed
2025-11-04 19:59 ` [PATCH 04/11] KVM: SVM: Rename vmcb->nested_ctl to vmcb->misc_ctl Yosry Ahmed
2025-11-04 19:59 ` [PATCH 05/11] KVM: SVM: Rename vmcb->virt_ext to vmcb->misc_ctl2 Yosry Ahmed
2025-11-04 19:59 ` [PATCH 06/11] KVM: SVM: switch svm_copy_lbrs() to a macro Yosry Ahmed
2025-11-04 19:59 ` [PATCH 07/11] KVM: nSVM: Cache all used fields from VMCB12 Yosry Ahmed
2025-11-04 19:59 ` [PATCH 08/11] KVM: nSVM: Restrict mapping VMCB12 on nested VMRUN Yosry Ahmed
2025-11-04 19:59 ` [PATCH 09/11] KVM: nSVM: Simplify nested_svm_vmrun() Yosry Ahmed
2025-11-04 19:59 ` [PATCH 10/11] KVM: nSVM: Sanitize control fields copied from VMCB12 Yosry Ahmed
2025-11-04 19:59 ` [PATCH 11/11] KVM: nSVM: Only copy NP_ENABLE from VMCB01's nested_ctl Yosry Ahmed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251104195949.3528411-3-yosry.ahmed@linux.dev \
    --to=yosry.ahmed@linux.dev \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).