git: d431c807f15d - main - amd64/vmm: Validate guest MSRs restored from a snapshot

From: Hayzam Sherif <hayzam_at_FreeBSD.org>
Date: Tue, 24 Nov 2026 08:01:46 UTC
The branch main has been updated by hayzam:

URL: https://cgit.FreeBSD.org/src/commit/?id=d431c807f15d8b0673b7d3ecc5aeec987ae9009b

commit d431c807f15d8b0673b7d3ecc5aeec987ae9009b
Author:     Hayzam Sherif <hayzam@FreeBSD.org>
AuthorDate: 2026-11-24 07:24:06 +0000
Commit:     Hayzam Sherif <hayzam@FreeBSD.org>
CommitDate: 2026-11-24 07:59:14 +0000

    amd64/vmm: Validate guest MSRs restored from a snapshot
    
    vmx_vcpu_snapshot() copies the saved guest MSR array from userspace
    without checking it.  vmx_msr_guest_enter() and
    vmx_msr_guest_enter_tsc_aux() write these values to the host's MSRs
    before guest entry, so a non-canonical LSTAR, CSTAR or KGSBASE, or a
    TSC_AUX with reserved bits set, raises #GP in the host and panics it.
    
    Check the values on restore and fail with EINVAL, leaving the vCPU's
    MSRs unchanged.  STAR and SF_MASK need no check, since the CPU accepts
    any value for them.  PAT is not written to a host MSR, but apply the
    check used for guest writes to it as well.
    
    Reported by:    llm-postcr
    Reviewed by:    markj
    Approved by:    markj
    Fixes:          483d953a86a2 (Initial support for bhyve save and restore.)
    Fixes:          f5f5f1e7d6de (Support guest rdtscp and rdpid instructions on Intel VT-x)
    MFC after:      1 week
    Sponsored by:   Alchemilla Ventures Private Limited
    Differential Revision:  https://reviews.freebsd.org/D61733
---
 sys/amd64/vmm/intel/vmx.c     | 10 ++++++++--
 sys/amd64/vmm/intel/vmx_msr.c | 38 ++++++++++++++++++++++++++++++++++++++
 sys/amd64/vmm/intel/vmx_msr.h |  4 ++++
 3 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/sys/amd64/vmm/intel/vmx.c b/sys/amd64/vmm/intel/vmx.c
index 4ad17861a437..92f37d8f0009 100644
--- a/sys/amd64/vmm/intel/vmx.c
+++ b/sys/amd64/vmm/intel/vmx.c
@@ -4172,6 +4172,7 @@ vmx_vcpu_snapshot(void *vcpui, struct vm_snapshot_meta *meta)
 	struct vmx *vmx;
 	struct vmx_vcpu *vcpu;
 	struct vmxctx *vmxctx;
+	uint64_t guest_msrs[GUEST_MSR_NUM];
 	int err, run, hostcpu;
 
 	err = 0;
@@ -4241,8 +4242,13 @@ vmx_vcpu_snapshot(void *vcpui, struct vm_snapshot_meta *meta)
 	if (err != 0)
 		goto done;
 
-	SNAPSHOT_BUF_OR_LEAVE(vcpu->guest_msrs,
-	    sizeof(vcpu->guest_msrs), meta, err, done);
+	memcpy(guest_msrs, vcpu->guest_msrs, sizeof(guest_msrs));
+	SNAPSHOT_BUF_OR_LEAVE(guest_msrs, sizeof(guest_msrs), meta, err, done);
+	if (meta->op == VM_SNAPSHOT_RESTORE) {
+		err = vmx_msr_guest_restore(vcpu, guest_msrs);
+		if (err != 0)
+			goto done;
+	}
 
 	SNAPSHOT_BUF_OR_LEAVE(vcpu->pir_desc,
 	    sizeof(*vcpu->pir_desc), meta, err, done);
diff --git a/sys/amd64/vmm/intel/vmx_msr.c b/sys/amd64/vmm/intel/vmx_msr.c
index 1f9fbb0e0b0d..0e8e5815f43b 100644
--- a/sys/amd64/vmm/intel/vmx_msr.c
+++ b/sys/amd64/vmm/intel/vmx_msr.c
@@ -26,6 +26,8 @@
  * SUCH DAMAGE.
  */
 
+#include "opt_bhyve_snapshot.h"
+
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/proc.h>
@@ -398,6 +400,42 @@ vmx_msr_guest_exit_tsc_aux(struct vmx *vmx, struct vmx_vcpu *vcpu)
 		wrmsr(MSR_TSC_AUX, host_aux);
 }
 
+#ifdef BHYVE_SNAPSHOT
+static bool
+canonical_addr(uint64_t addr)
+{
+	uint64_t mask;
+
+	/*
+	 * WRMSR checks an address against the widest linear address that the
+	 * CPU supports, whether or not 5-level paging is in use.
+	 */
+	mask = ~0UL <<
+	    ((cpu_stdext_feature2 & CPUID_STDEXT2_LA57) != 0 ? 56 : 47);
+	return ((addr & mask) == 0 || (addr & mask) == mask);
+}
+
+/*
+ * Install guest MSR values supplied by userspace.  The values are later
+ * written to the host's MSRs, so reject any for which WRMSR would raise #GP.
+ * STAR and SF_MASK accept all values.
+ */
+int
+vmx_msr_guest_restore(struct vmx_vcpu *vcpu,
+    const uint64_t msrs[static GUEST_MSR_NUM])
+{
+	if (!canonical_addr(msrs[IDX_MSR_LSTAR]) ||
+	    !canonical_addr(msrs[IDX_MSR_CSTAR]) ||
+	    !canonical_addr(msrs[IDX_MSR_KGSBASE]) ||
+	    !pat_valid(msrs[IDX_MSR_PAT]) ||
+	    (msrs[IDX_MSR_TSC_AUX] >> 32) != 0)
+		return (EINVAL);
+
+	memcpy(vcpu->guest_msrs, msrs, sizeof(vcpu->guest_msrs));
+	return (0);
+}
+#endif
+
 int
 vmx_rdmsr(struct vmx_vcpu *vcpu, u_int num, uint64_t *val, bool *retu)
 {
diff --git a/sys/amd64/vmm/intel/vmx_msr.h b/sys/amd64/vmm/intel/vmx_msr.h
index f88f37bd9163..526a4f6f7e7d 100644
--- a/sys/amd64/vmm/intel/vmx_msr.h
+++ b/sys/amd64/vmm/intel/vmx_msr.h
@@ -37,6 +37,10 @@ void vmx_msr_guest_enter_tsc_aux(struct vmx *vmx, struct vmx_vcpu *vcpu);
 void vmx_msr_guest_enter(struct vmx_vcpu *vcpu);
 void vmx_msr_guest_exit(struct vmx_vcpu *vcpu);
 void vmx_msr_guest_exit_tsc_aux(struct vmx *vmx, struct vmx_vcpu *vcpu);
+#ifdef BHYVE_SNAPSHOT
+int vmx_msr_guest_restore(struct vmx_vcpu *vcpu,
+    const uint64_t msrs[static GUEST_MSR_NUM]);
+#endif
 int vmx_rdmsr(struct vmx_vcpu *vcpu, u_int num, uint64_t *val, bool *retu);
 int vmx_wrmsr(struct vmx_vcpu *vcpu, u_int num, uint64_t val, bool *retu);