Skip to contentSkip to conversation

xen/pvh: prevent Dom0 from accessing reserved MMIO regions

PATCH In review Roger Pau Monne 2 participants 5 messages Jan 23, 2015

lore
Share & tools
lore
#0 lore raw

v4 cover xen/pvh: prevent Dom0 from accessing reserved MMIO regions

This series aims to prevent PVH Dom0 from accessing MMIO regions of devices that are used by Xen.

I know there are concerns with the way we build a PVH Dom0 right now, but addressing those requires a major rework of how construct_dom0 works. IMHO I don't think this should block this series, but I would understand otherwise.

Thanks for the review, Roger.

#2 lore raw

PATCH v4 2/3 xen/pvh: check permissions when adding MMIO regions

Check that MMIO regions added to PVH Dom0 are allowed. Previously a PVH Dom0 would have access to the full MMIO range.

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>

---
Changes since v3:
- Set execute access for MMIO regions.

Changes since v2:
- Fix coding style.

Changes since v1:
- Use the newly introduced p2m_access_t to set the access type.
- Don't add a next label.

---
 xen/arch/x86/domain_build.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
xen/arch/x86/domain_build.c +14 -1
index 01cfa58..2f9aaf9 100644
@@ -320,11 +320,24 @@ static __init void pvh_add_mem_mapping(struct domain *d, unsigned long gfn,
 {
     unsigned long i;
     p2m_access_t a;
+    mfn_t omfn;
+    p2m_type_t t;
     int rc;
 
-    a = p2m_get_hostp2m(d)->default_access;
     for ( i = 0; i < nr_mfns; i++ )
     {
+        if ( !iomem_access_permitted(d, mfn + i, mfn + i) )
+        {
+            omfn = get_gfn_query_unlocked(d, gfn + i, &t);
+            guest_physmap_remove_page(d, gfn + i, mfn_x(omfn), PAGE_ORDER_4K);
+            continue;
+        }
+
+        if ( rangeset_contains_singleton(mmio_ro_ranges, mfn + i) )
+            a = p2m_access_rx;
+        else
+            a = p2m_access_rwx;
+
         if ( (rc = set_mmio_p2m_entry(d, gfn + i, _mfn(mfn + i), a)) )
             panic("pvh_add_mem_mapping: gfn:%lx mfn:%lx i:%ld rc:%d\n",
                   gfn, mfn, i, rc);
Signature · 7 lines
1.9.3 (Apple Git-50)


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#3 lore raw

PATCH v4 3/3 xen: prevent access to HPET from Dom0

In reply to Roger Pau Monne

Prevent Dom0 from accessing HPET MMIO region by adding the HPET mfn to the list of forbiden memory regions (if ACPI_HPET_PAGE_PROTECT4 or ACPI_HPET_PAGE_PROTECT64 flag is set) or to the list of read-only regions.

Also provide an option that prevents adding the HPET to the read-only memory regions called ro-hpet, in case there are systems that put other stuff in the HPET page.

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>

---
Changes since v3:
- Correctly check for page protect values.
- Remove stale comment in include.

Changes since v2:
- Don't map the HPET page at all if ACPI_HPET_PAGE_PROTECT4 is found.
- Provide an option (ro-hpet) that prevents adding the HPET page to the list of read-only memory regions.

Changes since v1:
- Instead of completely blocking access to the HPET mfn, set it as read-only.

---
 docs/misc/xen-command-line.markdown |  8 ++++++++
 xen/arch/x86/acpi/boot.c            |  1 +
 xen/arch/x86/domain_build.c         | 18 ++++++++++++++++++
 xen/arch/x86/hpet.c                 |  1 +
 xen/include/asm-x86/hpet.h          |  1 +
 5 files changed, 29 insertions(+)
docs/misc/xen-command-line.markdown +8 -0
index a061aa4..e87eef4 100644
@@ -1380,3 +1380,11 @@ Use the x2apic physical apic driver.  The alternative is the x2apic cluster driv
 > Default: `true`
 
 Permit use of the `xsave/xrstor` instructions.
+
+### ro-hpet
+> `= <boolean>`
+
+> Default: `true`
+
+Map the HPET page as read only in Dom0. If disabled the page will be mapped
+with read and write permissions.
xen/arch/x86/acpi/boot.c +1 -0
index 903830b..9a8904b 100644
@@ -309,6 +309,7 @@ static int __init acpi_parse_hpet(struct acpi_table_header *table)
 
 	hpet_address = hpet_tbl->address.address;
 	hpet_blockid = hpet_tbl->sequence;
+	hpet_flags = hpet_tbl->flags;
 	printk(KERN_INFO PREFIX "HPET id: %#x base: %#lx\n",
 	       hpet_tbl->id, hpet_address);
 
xen/arch/x86/domain_build.c +18 -0
index 2f9aaf9..8d767a4 100644
@@ -36,6 +36,7 @@
 #include <asm/bzimage.h> /* for bzimage_parse */
 #include <asm/io_apic.h>
 #include <asm/hap.h>
+#include <asm/hpet.h>
 
 #include <public/version.h>
 
@@ -134,6 +135,9 @@ boolean_param("dom0_shadow", opt_dom0_shadow);
 static char __initdata opt_dom0_ioports_disable[200] = "";
 string_param("dom0_ioports_disable", opt_dom0_ioports_disable);
 
+static bool_t __initdata ro_hpet = 1;
+boolean_param("ro-hpet", ro_hpet);
+
 /* Allow ring-3 access in long mode as guest cannot use ring 1 ... */
 #define BASE_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER)
 #define L1_PROT (BASE_PROT|_PAGE_GUEST_KERNEL)
@@ -1495,6 +1499,20 @@ int __init construct_dom0(
             rc |= iomem_deny_access(d, sfn, efn);
     }
 
+    /* Prevent access to HPET */
+    if ( hpet_address != 0 )
+    {
+        u8 prot_flags = hpet_flags & ACPI_HPET_PAGE_PROTECT_MASK;
+        mfn = paddr_to_pfn(hpet_address);
+        if ( prot_flags == ACPI_HPET_PAGE_PROTECT4 )
+            rc |= iomem_deny_access(d, mfn, mfn);
+        else if ( prot_flags == ACPI_HPET_PAGE_PROTECT64 )
+            for ( i = 0; i < 16; i++ )
+                rc |= iomem_deny_access(d, mfn + i, mfn + i);
+        else if ( ro_hpet )
+            rc |= rangeset_add_singleton(mmio_ro_ranges, mfn);
+    }
+
     BUG_ON(rc != 0);
 
     if ( elf_check_broken(&elf) )
xen/arch/x86/hpet.c +1 -0
index 0b13f52..7aa740f 100644
@@ -52,6 +52,7 @@ DEFINE_PER_CPU(struct hpet_event_channel *, cpu_bc_channel);
 
 unsigned long __initdata hpet_address;
 u8 __initdata hpet_blockid;
+u8 __initdata hpet_flags;
 
 /*
  * force_hpet_broadcast: by default legacy hpet broadcast will be stopped
xen/include/asm-x86/hpet.h +1 -0
index 875f1de..10c4a56 100644
@@ -52,6 +52,7 @@
 
 extern unsigned long hpet_address;
 extern u8 hpet_blockid;
+extern u8 hpet_flags;
 
 /*
  * Detect and initialise HPET hardware: return counter update frequency.
Signature · 7 lines
1.9.3 (Apple Git-50)


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#1 lore raw

PATCH v4 1/3 xen: allow set_mmio_p2m_entry to specify access type

In reply to Roger Pau Monne

Preparatory change that allows setting the access type to set_mmio_p2m_entry.

Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Jun Nakajima <[email protected]>
Cc Eddie Dong <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Tim Deegan <[email protected]>

---
Changes since v2:
- Latch default access type prior to entering the loop.

---
 xen/arch/x86/domain_build.c |  4 +++-
 xen/arch/x86/hvm/vmx/vmx.c  |  2 +-
 xen/arch/x86/mm/p2m.c       | 15 +++++++++------
 xen/include/asm-x86/p2m.h   |  3 ++-
 4 files changed, 15 insertions(+), 9 deletions(-)
xen/arch/x86/domain_build.c +3 -1
index 7a912e9..01cfa58 100644
@@ -319,11 +319,13 @@ static __init void pvh_add_mem_mapping(struct domain *d, unsigned long gfn,
                                        unsigned long mfn, unsigned long nr_mfns)
 {
     unsigned long i;
+    p2m_access_t a;
     int rc;
 
+    a = p2m_get_hostp2m(d)->default_access;
     for ( i = 0; i < nr_mfns; i++ )
     {
-        if ( (rc = set_mmio_p2m_entry(d, gfn + i, _mfn(mfn + i))) )
+        if ( (rc = set_mmio_p2m_entry(d, gfn + i, _mfn(mfn + i), a)) )
             panic("pvh_add_mem_mapping: gfn:%lx mfn:%lx i:%ld rc:%d\n",
                   gfn, mfn, i, rc);
         if ( !(i & 0xfffff) )
xen/arch/x86/hvm/vmx/vmx.c +1 -1
index 522892f..ab4b1e5 100644
@@ -2173,7 +2173,7 @@ static int vmx_alloc_vlapic_mapping(struct domain *d)
     share_xen_page_with_guest(virt_to_page(apic_va), d, XENSHARE_writable);
     d->arch.hvm_domain.vmx.apic_access_mfn = virt_to_mfn(apic_va);
     set_mmio_p2m_entry(d, paddr_to_pfn(APIC_DEFAULT_PHYS_BASE),
-        _mfn(virt_to_mfn(apic_va)));
+        _mfn(virt_to_mfn(apic_va)), p2m_get_hostp2m(d)->default_access);
 
     return 0;
 }
xen/arch/x86/mm/p2m.c +9 -6
index efa49dd..c1b7545 100644
@@ -810,7 +810,7 @@ void p2m_change_type_range(struct domain *d,
 
 /* Returns: 0 for success, -errno for failure */
 static int set_typed_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,
-                               p2m_type_t gfn_p2mt)
+                               p2m_type_t gfn_p2mt, p2m_access_t access)
 {
     int rc = 0;
     p2m_access_t a;
@@ -837,7 +837,7 @@ static int set_typed_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,
 
     P2M_DEBUG("set %d %lx %lx\n", gfn_p2mt, gfn, mfn_x(mfn));
     rc = p2m_set_entry(p2m, gfn, mfn, PAGE_ORDER_4K, gfn_p2mt,
-                       p2m->default_access);
+                       access);
     gfn_unlock(p2m, gfn, 0);
     if ( rc )
         gdprintk(XENLOG_ERR,
@@ -850,12 +850,14 @@ static int set_typed_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,
 static int set_foreign_p2m_entry(struct domain *d, unsigned long gfn,
                                  mfn_t mfn)
 {
-    return set_typed_p2m_entry(d, gfn, mfn, p2m_map_foreign);
+    return set_typed_p2m_entry(d, gfn, mfn, p2m_map_foreign,
+                               p2m_get_hostp2m(d)->default_access);
 }
 
-int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn)
+int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,
+                       p2m_access_t access)
 {
-    return set_typed_p2m_entry(d, gfn, mfn, p2m_mmio_direct);
+    return set_typed_p2m_entry(d, gfn, mfn, p2m_mmio_direct, access);
 }
 
 /* Returns: 0 for success, -errno for failure */
@@ -1858,7 +1860,8 @@ int map_mmio_regions(struct domain *d,
 
     for ( i = 0; !ret && i < nr; i++ )
     {
-        ret = set_mmio_p2m_entry(d, start_gfn + i, _mfn(mfn + i));
+        ret = set_mmio_p2m_entry(d, start_gfn + i, _mfn(mfn + i),
+                                 p2m_get_hostp2m(d)->default_access);
         if ( ret )
         {
             unmap_mmio_regions(d, start_gfn, i, mfn);
xen/include/asm-x86/p2m.h +2 -1
index 2cf73ca..e86e26f 100644
@@ -513,7 +513,8 @@ int p2m_is_logdirty_range(struct p2m_domain *, unsigned long start,
                           unsigned long end);
 
 /* Set mmio addresses in the p2m table (for pass-through) */
-int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn);
+int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,
+                       p2m_access_t access);
 int clear_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn);
 
 /* Add foreign mapping to the guest's p2m table. */
Signature · 7 lines
1.9.3 (Apple Git-50)


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

Re: [PATCH v4 3/3] xen: prevent access to HPET from Dom0

In reply to Roger Pau Monne
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 23.01.15 at 13:10, <[email protected]> wrote:

Prevent Dom0 from accessing HPET MMIO region by adding the HPET mfn to the list of forbiden memory regions (if ACPI_HPET_PAGE_PROTECT4 or ACPI_HPET_PAGE_PROTECT64 flag is set) or to the list of read-only regions.

Also provide an option that prevents adding the HPET to the read-only memory regions called ro-hpet, in case there are systems that put other stuff in the HPET page.

I had to fiddle with this a little before committing - please double check.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel