Skip to contentSkip to conversation

x86: Fixes to shadowing dom0

PATCH In review Andrew Cooper 2 participants 8 messages Nov 9, 2018 – Nov 12, 2018

lore
Share & tools
lore
#0 lore raw

cover x86: Fixes to shadowing dom0

It turns out there are some real bugs attempting to shadow dom0, and it is because of these that XSA-273 went out with pv-l1tf= defaulting to not shadowing dom0. They aren't security issues themselves.

Patch 1 is a general problem with dom0 and 1G superpages, and wants to be taken onto all releases.

Patches 2 and 3 are various backports of work already upstream, to fix the shadowing of PV guests with 2M superpages.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw

PATCH 1/3 x86/dom0: Avoid using 1G superpages if shadowing may be necessary

The shadow code doesn't support 1G superpages, and will hand #PF[RSVD] back to guests.

For dom0's with 512GB of RAM or more (and subject to the P2M alignment), Xen's domain builder might use 1G superpages.

Avoid using 1G superpages (falling back to 2M superpages instead) if there is a reasonable chance that we may have to shadow dom0. This assumes that there are no circumstances where we will activate logdirty mode on dom0.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/arch/x86/pv/dom0_build.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
xen/arch/x86/pv/dom0_build.c +8 -1
index 812b026..710c8bb 100644
@@ -148,7 +148,14 @@ static __init void setup_pv_physmap(struct domain *d, unsigned long pgtbl_pfn,
         pl3e += l3_table_offset(vphysmap_start);
         if ( !l3e_get_intpte(*pl3e) )
         {
-            if ( cpu_has_page1gb &&
+            /*
+             * 1G superpages aren't supported by the shadow code.  Avoid using
+             * them we are liable to need to start shadowing dom0.  This
+             * assumes that there are no circumstances where we will activate
+             * logdirty mode on dom0.
+             */
+            if ( (!IS_ENABLED(CONFIG_SHADOW_PAGING) ||
+                  !d->arch.pv.check_l1tf) && cpu_has_page1gb &&
                  !(vphysmap_start & ((1UL << L3_PAGETABLE_SHIFT) - 1)) &&
                  vphysmap_end >= vphysmap_start + (1UL << L3_PAGETABLE_SHIFT) &&
                  (page = alloc_domheap_pages(d,
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

PATCH 2/3 x86/dom0: Fix shadowing of PV guests with 2M superpages

In reply to Andrew Cooper

This is a straight backport of c/s 28d9a9a2d41759b9e5163037b759ac557aea767c but with a different justification.

Dom0 may have superpages (e.g. initial P2M), and may be shadowed (e.g. PV-L1TF). Because of this incorrect check, when PV superpages are disallowed (which is the security supported configuration), attempting to shadow the P2M with its superpages still intact will fail. A #PF will be handed back to the kernel, rather than the superpage being splintered and shadowed.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/include/asm-x86/guest_pt.h | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
xen/include/asm-x86/guest_pt.h +7 -5
index 72126d5..08031c8 100644
@@ -205,15 +205,17 @@ static inline guest_l4e_t guest_l4e_from_gfn(gfn_t gfn, u32 flags)
 static inline bool guest_can_use_l2_superpages(const struct vcpu *v)
 {
     /*
+     * PV guests use Xen's paging settings.  Being 4-level, 2M
+     * superpages are unconditionally supported.
+     *
      * The L2 _PAGE_PSE bit must be honoured in HVM guests, whenever
      * CR4.PSE is set or the guest is in PAE or long mode.
      * It's also used in the dummy PT for vcpus with CR0.PG cleared.
      */
-    return (is_pv_vcpu(v)
-            ? opt_allow_superpage
-            : (GUEST_PAGING_LEVELS != 2
-               || !hvm_paging_enabled(v)
-               || (v->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PSE)));
+    return (is_pv_vcpu(v) ||
+            GUEST_PAGING_LEVELS != 2 ||
+            !hvm_paging_enabled(v) ||
+            (v->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PSE));
 }
 
 static inline bool guest_can_use_l3_superpages(const struct domain *d)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

PATCH 3/3 x86/dom0: Fix shadowing of PV guests with 2M superpages

In reply to Andrew Cooper

This is a minimal backport of pieces of:

c/s 28d9a9a2d41759b9e5163037b759ac557aea767c
c/s 4c5d78a10dc89427140a50a1df5a0b8e9f073e82

to fix a PV shadowing problem which I hadn't anticipated at the time these fixes were first accepted.

Having opt_allow_superpage disabled causes guest_supports_superpages() to return false for PV guests. Returning false causes guest_walk_tables() to ignore L2 superpages, and read under them.

This ignoring behaviour is correct for 2-level paging when CR4.PSE is clear, but isn't correct for 3- or 4-level paging.

When opt_allow_superpage is clear, PV domU's can't have superpages, but dom0 will still have its initial P2M constructed with 2M superpages.

The end result is that, if dom0 becomes shadowed (e.g. PV-L1TF), the next memory access touching a P2M superpage will cause the shadow code to read under the P2M superpage and attempt to shadow junk.

---
 xen/arch/x86/mm/guest_walk.c   | 15 ++++++++++++++-
 xen/include/asm-x86/guest_pt.h | 20 ++++++++++++--------
 2 files changed, 26 insertions(+), 9 deletions(-)
xen/arch/x86/mm/guest_walk.c +14 -1
index 868e909..f4c9921 100644
@@ -324,9 +324,20 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
         rc |= _PAGE_PRESENT;
         goto out;
     }
+
+    /*
+     * In 2-level paging without CR0.PSE, there are no reserved bits, and the
+     * PAT/PSE bit is ignored.
+     */
+    if ( GUEST_PAGING_LEVELS == 2 && !guest_supports_superpages(v) )
+    {
+        gw->l2e.l2 &= ~_PAGE_PSE;
+        gflags &= ~_PAGE_PSE;
+    }
+
     rc |= ((gflags & mflags) ^ mflags);
 
-    pse2M = (gflags & _PAGE_PSE) && guest_supports_superpages(v); 
+    pse2M = !!(gflags & _PAGE_PSE);
 
     if ( pse2M )
     {
@@ -348,6 +359,8 @@ guest_walk_tables(struct vcpu *v, struct p2m_domain *p2m,
             /* _PAGE_PSE_PAT not set: remove _PAGE_PAT from flags. */
             flags &= ~_PAGE_PAT;
 
+        if ( !guest_supports_superpages(v) )
+            rc |= _PAGE_PSE | _PAGE_INVALID_BIT;
         if ( gfn_x(start) & GUEST_L2_GFN_MASK & ~0x1 )
             rc |= _PAGE_INVALID_BITS;
 
xen/include/asm-x86/guest_pt.h +12 -8
index 79ed4ff..6bdb7ca 100644
@@ -179,14 +179,18 @@ static inline guest_l4e_t guest_l4e_from_gfn(gfn_t gfn, u32 flags)
 static inline int
 guest_supports_superpages(struct vcpu *v)
 {
-    /* The _PAGE_PSE bit must be honoured in HVM guests, whenever
-     * CR4.PSE is set or the guest is in PAE or long mode. 
-     * It's also used in the dummy PT for vcpus with CR4.PG cleared. */
-    return (is_pv_vcpu(v)
-            ? opt_allow_superpage
-            : (GUEST_PAGING_LEVELS != 2 
-               || !hvm_paging_enabled(v)
-               || (v->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PSE)));
+    /*
+     * PV guests use Xen's paging settings.  Being 4-level, 2M
+     * superpages are unconditionally supported.
+     *
+     * The L2 _PAGE_PSE bit must be honoured in HVM guests, whenever
+     * CR4.PSE is set or the guest is in PAE or long mode.
+     * It's also used in the dummy PT for vcpus with CR0.PG cleared.
+     */
+    return (is_pv_vcpu(v) ||
+            GUEST_PAGING_LEVELS != 2 ||
+            !hvm_paging_enabled(v) ||
+            (v->arch.hvm_vcpu.guest_cr[4] & X86_CR4_PSE));
 }
 
 static inline int
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] x86/dom0: Avoid using 1G superpages if shadowing may be necessary

In reply to Andrew Cooper
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 09.11.18 at 16:26, <[email protected]> wrote:

The shadow code doesn't support 1G superpages, and will hand #PF[RSVD] back to guests.

So this change is then instead of trying to make shadow code cope?

xen/arch/x86/pv/dom0_build.c static __init void setup_pv_physmap(struct domain *d, unsigned long pgtbl_pfn, · line 153

Quoted 10 lines
xen/arch/x86/pv/dom0_build.c +3 -1
@@ -148,7 +148,14 @@ static __init void setup_pv_physmap(struct domain *d, unsigned long pgtbl_pfn,
         pl3e += l3_table_offset(vphysmap_start);
         if ( !l3e_get_intpte(*pl3e) )
         {
-            if ( cpu_has_page1gb &&
+            /*
+             * 1G superpages aren't supported by the shadow code.  Avoid using
+             * them we are liable to need to start shadowing dom0.  This

I think there's a word missing here - "when" perhaps?

xen/arch/x86/pv/dom0_build.c static __init void setup_pv_physmap(struct domain *d, unsigned long pgtbl_pfn, · line 161

Quoted 8 lines
xen/arch/x86/pv/dom0_build.c +5 -0
+             * assumes that there are no circumstances where we will activate
+             * logdirty mode on dom0.
+             */
+            if ( (!IS_ENABLED(CONFIG_SHADOW_PAGING) ||
+                  !d->arch.pv.check_l1tf) && cpu_has_page1gb &&
                  !(vphysmap_start & ((1UL << L3_PAGETABLE_SHIFT) - 1)) &&
                  vphysmap_end >= vphysmap_start + (1UL << L3_PAGETABLE_SHIFT) &&
                  (page = alloc_domheap_pages(d,

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3 Xen-4.9] x86/dom0: Fix shadowing of PV guests with 2M superpages

In reply to Andrew Cooper
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 09.11.18 at 16:26, <[email protected]> wrote:

This is a straight backport of c/s 28d9a9a2d41759b9e5163037b759ac557aea767c but with a different justification.

Quoted 8 lines

Dom0 may have superpages (e.g. initial P2M), and may be shadowed (e.g. PV-L1TF). Because of this incorrect check, when PV superpages are disallowed (which is the security supported configuration), attempting to shadow the P2M with its superpages still intact will fail. A #PF will be handed back to the kernel, rather than the superpage being splintered and shadowed.

It being a straight backport, tags from the original commit should perhaps be retained?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3 Xen-4.8] x86/dom0: Fix shadowing of PV guests with 2M superpages

In reply to Andrew Cooper
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 09.11.18 at 16:26, <[email protected]> wrote:

This is a minimal backport of pieces of:

c/s 28d9a9a2d41759b9e5163037b759ac557aea767c
c/s 4c5d78a10dc89427140a50a1df5a0b8e9f073e82

to fix a PV shadowing problem which I hadn't anticipated at the time these fixes were first accepted.

Having opt_allow_superpage disabled causes guest_supports_superpages() to return false for PV guests. Returning false causes guest_walk_tables() to ignore L2 superpages, and read under them.

This ignoring behaviour is correct for 2-level paging when CR4.PSE is clear, but isn't correct for 3- or 4-level paging.

Quoted 8 lines

When opt_allow_superpage is clear, PV domU's can't have superpages, but dom0 will still have its initial P2M constructed with 2M superpages.

The end result is that, if dom0 becomes shadowed (e.g. PV-L1TF), the next memory access touching a P2M superpage will cause the shadow code to read under the P2M superpage and attempt to shadow junk.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [PATCH 1/3] x86/dom0: Avoid using 1G superpages if shadowing may be necessary

In reply to Jan Beulich
Quoted 4 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 09.11.18 at 16:26, <[email protected]> wrote:

The shadow code doesn't support 1G superpages, and will hand #PF[RSVD] back to guests.

So this change is then instead of trying to make shadow code cope?

At the moment, that is an unknown quantity of work.  Ideally yes we should get 1G superpage support working in the shadow code, but it definitely isn't top of the priority list atm.

Quoted 12 lines

xen/arch/x86/pv/dom0_build.c static __init void setup_pv_physmap(struct domain *d, unsigned long pgtbl_pfn, · line 153

Quoted 11 lines
xen/arch/x86/pv/dom0_build.c +3 -1
@@ -148,7 +148,14 @@ static __init void setup_pv_physmap(struct domain *d, unsigned long pgtbl_pfn,
         pl3e += l3_table_offset(vphysmap_start);
         if ( !l3e_get_intpte(*pl3e) )
         {
-            if ( cpu_has_page1gb &&
+            /*
+             * 1G superpages aren't supported by the shadow code.  Avoid using
+             * them we are liable to need to start shadowing dom0.  This

I think there's a word missing here - "when" perhaps?

Oops yes.

Quoted 10 lines

xen/arch/x86/pv/dom0_build.c static __init void setup_pv_physmap(struct domain *d, unsigned long pgtbl_pfn, · line 161

Quoted 9 lines
xen/arch/x86/pv/dom0_build.c +5 -0
+             * assumes that there are no circumstances where we will activate
+             * logdirty mode on dom0.
+             */
+            if ( (!IS_ENABLED(CONFIG_SHADOW_PAGING) ||
+                  !d->arch.pv.check_l1tf) && cpu_has_page1gb &&
                  !(vphysmap_start & ((1UL << L3_PAGETABLE_SHIFT) - 1)) &&
                  vphysmap_end >= vphysmap_start + (1UL << L3_PAGETABLE_SHIFT) &&
                  (page = alloc_domheap_pages(d,

Thanks,

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel