Skip to contentSkip to conversation

x86/nSVM: Validate the L1 MSRPM physical address range

PATCH In review Lin Liu 2 participants 5 messages Sep 10 – Sep 16

lore
Share & tools
lore

Lin Liu

author
#0 lore raw

PATCH 1/1 x86/nSVM: Validate the L1 MSRPM physical address range

8c36d5a500 ("x86/nSVM: Validate the L1 IOPM physical address range") added this check for the IOPM. The MSRPM requires a check as well.

"The MSR or IOIO intercept tables extend to a physical address that is greater than or equal to the maximum supported physical address" is illegal VMCB state. Refer to APM vol.2 15.5.

Assisted-by Claude:claude-opus-5
---
 xen/arch/x86/hvm/svm/nestedsvm.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +15 -5
index 5adb1bd72c..3614473702 100644
@@ -283,6 +283,12 @@ static int nsvm_vcpu_hostrestore(struct vcpu *v, struct cpu_user_regs *regs)
     return 0;
 }
 
+static bool nsvm_perm_map_valid(const struct vcpu *v, uint64_t base,
+                                unsigned int last)
+{
+    return gfn_valid(v->domain, gfn_add(gaddr_to_gfn(base), PFN_DOWN(last)));
+}
+
 static int nsvm_vmrun_permissionmap(struct vcpu *v)
 {
     struct svm_vcpu *arch_svm = &v->arch.hvm.svm;
@@ -295,18 +301,22 @@ static int nsvm_vmrun_permissionmap(struct vcpu *v)
     enum hvm_translation_result ret;
     unsigned long *ns_viomap;
     bool ioport_80 = true, ioport_ed = true;
-    /* IOPM is structured as a linear array of 64K+3 bits. */
-    gfn_t ns_iopm_end =
-        gfn_add(gaddr_to_gfn(ns_vmcb->_iopm_base_pa),
-                PFN_DOWN((0x10000 + 3) / 8));
 
-    if ( !gfn_valid(v->domain, ns_iopm_end) )
+    /* IOPM is structured as a linear array of 64K+3 bits. */
+    if ( !nsvm_perm_map_valid(v, ns_vmcb->_iopm_base_pa, (0x10000 + 3) / 8) )
     {
         gdprintk(XENLOG_ERR, "%s invalid _iopm_base_pa address (%#"PRIx64")\n",
                  __func__, ns_vmcb->_iopm_base_pa);
         return NSVM_ERROR_VVMCB;
     }
 
+    if ( !nsvm_perm_map_valid(v, ns_vmcb->_msrpm_base_pa, MSRPM_SIZE - 1) )
+    {
+        gdprintk(XENLOG_ERR, "%s invalid _msrpm_base_pa address (%#"PRIx64")\n",
+                 __func__, ns_vmcb->_msrpm_base_pa);
+        return NSVM_ERROR_VVMCB;
+    }
+
     ns_msrpm_ptr = (unsigned long *)svm->ns_cached_msrpm;
 
     ret = hvm_copy_from_guest_phys(svm->ns_cached_msrpm,
Signature · 1 line
2.52.0
author
#2 lore raw
Quoted 7 lines · Jan Beulich
Quoted 2 lines · Lin Liu

8c36d5a500 ("x86/nSVM: Validate the L1 IOPM physical address range") added this check for the IOPM. The MSRPM requires a check as well.

See https://lists.xen.org/archives/html/xen-devel/2026-08/msg00242.html
and Abdelkareem's reply. If you disagree, please go into further detail here.

I had missed that thread; the patch as posted should not go in as it stands.

The one thing I would still raise is that hvm_copy_from_guest_phys() does not only reject an out-of-range MSRPM. It also rejects one which is in range but simply not backed by memory - and hardware accepts that.

I checked on an EPYC 9255: I patched Xen so an ordinary non-nested guest's own VMCB carried _msrpm_base_pa = 1 << 45, in range with nothing behind it. The guest ran a full Linux boot; a rejected VMRUN would have executed no guest instructions at all. On an EPYC 9965 I then counted MSR exits, and the CPU reads such a map as all ones - every one of the ten bits construct_vmcb() clears trapped, against none in a control guest on the same boot. (What an unbacked machine-physical read returns is chipset behaviour, not architectural.)

So what I would like to send as v2 is two patches:

1/2 make the MAXPHYADDR range check explicit, as 8c36d5a500 did for
the IOPM. No functional change on its own.
2/2 stop a failed copy returning NSVM_ERROR_VVMCB; fill the cached
map instead - all ones where L1 set MSR_PROT, zero where it did not.

They have to go together: that failing copy is currently the only thing rejecting an out-of-range MSRPM, so 2/2 alone would leave one accepted.

Does that sound reasonable to you?

Lin

#3 lore raw
Quoted 34 lines · Lin Liu
Quoted 7 lines · Jan Beulich
Quoted 2 lines · Lin Liu

8c36d5a500 ("x86/nSVM: Validate the L1 IOPM physical address range") added this check for the IOPM. The MSRPM requires a check as well.

See https://lists.xen.org/archives/html/xen-devel/2026-08/msg00242.html
and Abdelkareem's reply. If you disagree, please go into further detail here.

I had missed that thread; the patch as posted should not go in as it stands.

The one thing I would still raise is that hvm_copy_from_guest_phys() does not only reject an out-of-range MSRPM. It also rejects one which is in range but simply not backed by memory - and hardware accepts that.

I checked on an EPYC 9255: I patched Xen so an ordinary non-nested guest's own VMCB carried _msrpm_base_pa = 1 << 45, in range with nothing behind it. The guest ran a full Linux boot; a rejected VMRUN would have executed no guest instructions at all. On an EPYC 9965 I then counted MSR exits, and the CPU reads such a map as all ones - every one of the ten bits construct_vmcb() clears trapped, against none in a control guest on the same boot. (What an unbacked machine-physical read returns is chipset behaviour, not architectural.)

So what I would like to send as v2 is two patches:

1/2 make the MAXPHYADDR range check explicit, as 8c36d5a500 did for
the IOPM. No functional change on its own.
2/2 stop a failed copy returning NSVM_ERROR_VVMCB; fill the cached
map instead - all ones where L1 set MSR_PROT, zero where it did not.

Quoted 5 lines

They have to go together: that failing copy is currently the only thing rejecting an out-of-range MSRPM, so 2/2 alone would leave one accepted.

Does that sound reasonable to you?

On the surface it sounds plausible. I'm not sure we'd be doing ourselves a favor, though. Non-RAM is rejected in other situations as well, where in principle the same behavior you describe could apply. Hence imo if we wanted to go that route, I think we'd want to be consistent. That would be quite a bit more work.

Andrew - do you have any thoughts here?

Jan

author
#4 lore raw
Quoted 4 lines · Jan Beulich

Non-RAM is rejected in other situations as well, where in principle the same behavior you describe could apply. Hence imo if we wanted to go that route, I think we'd want to be consistent. That would be quite a bit more work.

Agreed - I'm dropping the series.

My premise was wrong in any case. 15.10.1 (p.537) and 15.11 (p.539) both say the map "should reside in memory that is mapped as writeback (WB)", which I had missed, so an unbacked map is outside the contract rather than legal-but-refused. The range check is redundant too: an out-of-range address cannot be populated, so the copy already rejects it.

And "match what hardware does" would not be a consistent rule even in principle, because hardware is not uniform here. On EPYC 9255 / 9965 / 8324P, with a non-nested guest's own VMCB pointed at each address:

  in-range unpopulated hole       accepted; map reads as all ones
  SMRAM (inside an enabled TSeg)  VMRUN hard-rejected, both maps
  one page above TSeg             accepted

The TSeg boundary is the discriminator, not MMIO-vs-RAM; Andrew predicted that offline. One incidental result: at that last address the bitmap is real firmware data, the EFER read bit happened to be clear, and the guest read the host's real EFER with SVME set before triple-faulting - an argument for rejecting rather than guessing.

Thanks for the help to review.

Lin