Skip to contentSkip to conversation

x86/mm: Add array_index_nospec to guest provided index values

PATCH Superseded Alexandru Stefan ISAILA 2 participants 10 messages Dec 19, 2019 – Dec 20, 2019

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/4 x86/mm: Add array_index_nospec to guest provided index values

This patch aims to sanitize indexes, potentially guest provided values, for altp2m_eptp[] and altp2m_p2m[] arrays.

---
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
---
Changes since V4:
- Change bounds check from MAX_EPTP to MAX_ALTP2M
- Move array_index_nospec() closer to the bounds check.

---
 xen/arch/x86/mm/mem_access.c | 15 +++++++++------
 xen/arch/x86/mm/p2m.c        | 20 ++++++++++++++------
 2 files changed, 23 insertions(+), 12 deletions(-)
xen/arch/x86/mm/mem_access.c +9 -6
index 320b9fe621..33e379db8f 100644
@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,
     if ( altp2m_idx )
     {
         if ( altp2m_idx >= MAX_ALTP2M ||
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==
+             mfn_x(INVALID_MFN) )
             return -EINVAL;
 
-        ap2m = d->arch.altp2m_p2m[altp2m_idx];
+        ap2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx, MAX_ALTP2M)];
     }
 #else
     ASSERT(!altp2m_idx);
@@ -426,10 +427,11 @@ long p2m_set_mem_access_multi(struct domain *d,
     if ( altp2m_idx )
     {
         if ( altp2m_idx >= MAX_ALTP2M ||
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==
+             mfn_x(INVALID_MFN) )
             return -EINVAL;
 
-        ap2m = d->arch.altp2m_p2m[altp2m_idx];
+        ap2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx, MAX_ALTP2M)];
     }
 #else
     ASSERT(!altp2m_idx);
@@ -492,10 +494,11 @@ int p2m_get_mem_access(struct domain *d, gfn_t gfn, xenmem_access_t *access,
     else if ( altp2m_idx ) /* altp2m view 0 is treated as the hostp2m */
     {
         if ( altp2m_idx >= MAX_ALTP2M ||
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==
+             mfn_x(INVALID_MFN) )
             return -EINVAL;
 
-        p2m = d->arch.altp2m_p2m[altp2m_idx];
+        p2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx, MAX_ALTP2M)];
     }
 #else
     ASSERT(!altp2m_idx);
xen/arch/x86/mm/p2m.c +14 -6
index ba126f790a..16039c7a57 100644
@@ -2574,6 +2574,7 @@ int p2m_init_altp2m_by_id(struct domain *d, unsigned int idx)
     if ( idx >= MAX_ALTP2M )
         return rc;
 
+    idx = array_index_nospec(idx, MAX_ALTP2M);
     altp2m_list_lock(d);
 
     if ( d->arch.altp2m_eptp[idx] == mfn_x(INVALID_MFN) )
@@ -2615,6 +2616,7 @@ int p2m_destroy_altp2m_by_id(struct domain *d, unsigned int idx)
     if ( !idx || idx >= MAX_ALTP2M )
         return rc;
 
+    idx = array_index_nospec(idx, MAX_ALTP2M);
     rc = domain_pause_except_self(d);
     if ( rc )
         return rc;
@@ -2686,11 +2688,13 @@ int p2m_change_altp2m_gfn(struct domain *d, unsigned int idx,
     mfn_t mfn;
     int rc = -EINVAL;
 
-    if ( idx >= MAX_ALTP2M || d->arch.altp2m_eptp[idx] == mfn_x(INVALID_MFN) )
+    if ( idx >= MAX_ALTP2M ||
+         d->arch.altp2m_eptp[array_index_nospec(idx, MAX_ALTP2M)] ==
+         mfn_x(INVALID_MFN) )
         return rc;
 
     hp2m = p2m_get_hostp2m(d);
-    ap2m = d->arch.altp2m_p2m[idx];
+    ap2m = d->arch.altp2m_p2m[array_index_nospec(idx, MAX_ALTP2M)];
 
     p2m_lock(hp2m);
     p2m_lock(ap2m);
@@ -3030,10 +3034,12 @@ int p2m_set_suppress_ve(struct domain *d, gfn_t gfn, bool suppress_ve,
     if ( altp2m_idx > 0 )
     {
         if ( altp2m_idx >= MAX_ALTP2M ||
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==
+             mfn_x(INVALID_MFN) )
             return -EINVAL;
 
-        p2m = ap2m = d->arch.altp2m_p2m[altp2m_idx];
+        p2m = ap2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx,
+                                                           MAX_ALTP2M)];
     }
     else
         p2m = host_p2m;
@@ -3073,10 +3079,12 @@ int p2m_get_suppress_ve(struct domain *d, gfn_t gfn, bool *suppress_ve,
     if ( altp2m_idx > 0 )
     {
         if ( altp2m_idx >= MAX_ALTP2M ||
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==
+             mfn_x(INVALID_MFN) )
             return -EINVAL;
 
-        p2m = ap2m = d->arch.altp2m_p2m[altp2m_idx];
+        p2m = ap2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx,
+                                                           MAX_ALTP2M)];
     }
     else
         p2m = host_p2m;
Signature · 6 lines
2.17.1

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

PATCH v5 2/4 x86/altp2m: Add hypercall to set a range of sve bits

By default the sve bits are not set.
This patch adds a new hypercall, xc_altp2m_set_supress_ve_multi(), to set a range of sve bits.
The core function, p2m_set_suppress_ve_multi(), does not brake in case of a error and it is doing a best effort for setting the bits in the given range. A check for continuation is made in order to have preemption on big ranges.
The gfn of the first error is stored in xen_hvm_altp2m_suppress_ve_multi.first_error and the error code is stored in xen_hvm_altp2m_suppress_ve_multi.first_error_code. If no error occurred the values will be 0.

---
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Julien Grall <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: George Dunlap <[email protected]>
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
---
Changes since V4:
- Remove ->first_error and first_error_code from
HVMOP_altp2m_set_suppress_ve_multi check
- Check ->first_error_code so that first_error on gfn 0 can be
saved
- Chage type of first_error_code to int32_t
- Clip ->last_gfn before sanity check.

---
 tools/libxc/include/xenctrl.h   |  4 +++
 tools/libxc/xc_altp2m.c         | 33 +++++++++++++++++
 xen/arch/x86/hvm/hvm.c          | 20 +++++++++++
 xen/arch/x86/mm/p2m.c           | 64 +++++++++++++++++++++++++++++++++
 xen/include/public/hvm/hvm_op.h | 13 +++++++
 xen/include/xen/mem_access.h    |  3 ++
 6 files changed, 137 insertions(+)
tools/libxc/include/xenctrl.h +4 -0
index f4431687b3..21a333f2c4 100644
@@ -1923,6 +1923,10 @@ int xc_altp2m_switch_to_view(xc_interface *handle, uint32_t domid,
                              uint16_t view_id);
 int xc_altp2m_set_suppress_ve(xc_interface *handle, uint32_t domid,
                               uint16_t view_id, xen_pfn_t gfn, bool sve);
+int xc_altp2m_set_supress_ve_multi(xc_interface *handle, uint32_t domid,
+                                   uint16_t view_id, xen_pfn_t first_gfn,
+                                   xen_pfn_t last_gfn, bool sve,
+                                   xen_pfn_t *error_gfn, int32_t *error_code);
 int xc_altp2m_get_suppress_ve(xc_interface *handle, uint32_t domid,
                               uint16_t view_id, xen_pfn_t gfn, bool *sve);
 int xc_altp2m_set_mem_access(xc_interface *handle, uint32_t domid,
tools/libxc/xc_altp2m.c +33 -0
index 09dad0355e..4ba930666a 100644
@@ -234,6 +234,39 @@ int xc_altp2m_set_suppress_ve(xc_interface *handle, uint32_t domid,
     return rc;
 }
 
+int xc_altp2m_set_supress_ve_multi(xc_interface *handle, uint32_t domid,
+                                   uint16_t view_id, xen_pfn_t first_gfn,
+                                   xen_pfn_t last_gfn, bool sve,
+                                   xen_pfn_t *error_gfn, int32_t *error_code)
+{
+    int rc;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_altp2m_op_t, arg);
+
+    arg = xc_hypercall_buffer_alloc(handle, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    arg->version = HVMOP_ALTP2M_INTERFACE_VERSION;
+    arg->cmd = HVMOP_altp2m_set_suppress_ve_multi;
+    arg->domain = domid;
+    arg->u.suppress_ve_multi.view = view_id;
+    arg->u.suppress_ve_multi.first_gfn = first_gfn;
+    arg->u.suppress_ve_multi.last_gfn = last_gfn;
+    arg->u.suppress_ve_multi.suppress_ve = sve;
+
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
+
+    if ( arg->u.suppress_ve_multi.first_error )
+    {
+        *error_gfn = arg->u.suppress_ve_multi.first_error;
+        *error_code = arg->u.suppress_ve_multi.first_error_code;
+    }
+
+    xc_hypercall_buffer_free(handle, arg);
+    return rc;
+}
+
 int xc_altp2m_set_mem_access(xc_interface *handle, uint32_t domid,
                              uint16_t view_id, xen_pfn_t gfn,
                              xenmem_access_t access)
xen/arch/x86/hvm/hvm.c +20 -0
index 47573f71b8..98d1d9788b 100644
@@ -4553,6 +4553,7 @@ static int do_altp2m_op(
     case HVMOP_altp2m_destroy_p2m:
     case HVMOP_altp2m_switch_p2m:
     case HVMOP_altp2m_set_suppress_ve:
+    case HVMOP_altp2m_set_suppress_ve_multi:
     case HVMOP_altp2m_get_suppress_ve:
     case HVMOP_altp2m_set_mem_access:
     case HVMOP_altp2m_set_mem_access_multi:
@@ -4711,6 +4712,25 @@ static int do_altp2m_op(
         }
         break;
 
+    case HVMOP_altp2m_set_suppress_ve_multi:
+    {
+        uint64_t max_phys_addr = (1UL << d->arch.cpuid->extd.maxphysaddr) - 1;
+
+        a.u.suppress_ve_multi.last_gfn = min(a.u.suppress_ve_multi.last_gfn,
+                                             max_phys_addr);
+
+        if ( a.u.suppress_ve_multi.pad1 ||
+             a.u.suppress_ve_multi.first_gfn > a.u.suppress_ve_multi.last_gfn )
+            rc = -EINVAL;
+        else
+        {
+            rc = p2m_set_suppress_ve_multi(d, &a.u.suppress_ve_multi);
+            if ( (!rc || rc == -ERESTART) && __copy_to_guest(arg, &a, 1) )
+                rc = -EFAULT;
+        }
+        break;
+    }
+
     case HVMOP_altp2m_get_suppress_ve:
         if ( a.u.suppress_ve.pad1 || a.u.suppress_ve.pad2 )
             rc = -EINVAL;
xen/arch/x86/mm/p2m.c +64 -0

70 lines not on this page · raw message

xen/include/public/hvm/hvm_op.h +13 -0
index 353f8034d9..5446d634d8 100644
@@ -46,6 +46,16 @@ struct xen_hvm_altp2m_suppress_ve {
     uint64_t gfn;
 };
 
+struct xen_hvm_altp2m_suppress_ve_multi {
+    uint16_t view;
+    uint8_t suppress_ve; /* Boolean type. */
+    uint8_t pad1;
+    int32_t first_error_code; /* Must be set to 0 . */
+    uint64_t first_gfn; /* Value will be updated */
+    uint64_t last_gfn;
+    uint64_t first_error; /* Gfn of the first error. Must be set to 0. */
+};
+
 #if __XEN_INTERFACE_VERSION__ < 0x00040900
 
 /* Set the logical level of one of a domain's PCI INTx wires. */
@@ -339,6 +349,8 @@ struct xen_hvm_altp2m_op {
 #define HVMOP_altp2m_vcpu_disable_notify  13
 /* Get the active vcpu p2m index */
 #define HVMOP_altp2m_get_p2m_idx          14
+/* Set the "Supress #VE" bit for a range of pages */
+#define HVMOP_altp2m_set_suppress_ve_multi 15
     domid_t domain;
     uint16_t pad1;
     uint32_t pad2;
@@ -353,6 +365,7 @@ struct xen_hvm_altp2m_op {
         struct xen_hvm_altp2m_change_gfn           change_gfn;
         struct xen_hvm_altp2m_set_mem_access_multi set_mem_access_multi;
         struct xen_hvm_altp2m_suppress_ve          suppress_ve;
+        struct xen_hvm_altp2m_suppress_ve_multi    suppress_ve_multi;
         struct xen_hvm_altp2m_vcpu_disable_notify  disable_notify;
         struct xen_hvm_altp2m_get_vcpu_p2m_idx     get_vcpu_p2m_idx;
         uint8_t pad[64];
xen/include/xen/mem_access.h +3 -0
index e4d24502e0..00e594a0ad 100644
@@ -75,6 +75,9 @@ long p2m_set_mem_access_multi(struct domain *d,
 int p2m_set_suppress_ve(struct domain *d, gfn_t gfn, bool suppress_ve,
                         unsigned int altp2m_idx);
 
+int p2m_set_suppress_ve_multi(struct domain *d,
+                              struct xen_hvm_altp2m_suppress_ve_multi *suppress_ve);
+
 int p2m_get_suppress_ve(struct domain *d, gfn_t gfn, bool *suppress_ve,
                         unsigned int altp2m_idx);
 
Signature · 6 lines
2.17.1

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

Re: [Xen-devel] [PATCH V5 2/4] x86/altp2m: Add hypercall to set a range of sve bits

xen/include/public/hvm/hvm_op.h struct xen_hvm_altp2m_suppress_ve { · line 54

Quoted 12 lines · Alexandru Stefan ISAILA
xen/include/public/hvm/hvm_op.h +6 -0
@@ -46,6 +46,16 @@ struct xen_hvm_altp2m_suppress_ve {
     uint64_t gfn;
 };
 
+struct xen_hvm_altp2m_suppress_ve_multi {
+    uint16_t view;
+    uint8_t suppress_ve; /* Boolean type. */
+    uint8_t pad1;
+    int32_t first_error_code; /* Must be set to 0 . */
+    uint64_t first_gfn; /* Value will be updated */

s/will/may/

xen/include/public/hvm/hvm_op.h struct xen_hvm_altp2m_suppress_ve { · line 56

Quoted 2 lines
xen/include/public/hvm/hvm_op.h +2 -0
+    uint64_t last_gfn;
+    uint64_t first_error; /* Gfn of the first error. Must be set to 0. */

There's no real "must" here. Please at most say "should", but I'd even consider dropping that part of the comment altogether. The consumer logic needs to key off of the error code anyway. Even for the error code field I'd suggest saying just "should": You don't check it (because you can't), and the caller only shoots itself in the foot if it doesn't do so.

Also looking at this yet again - I think field names would better be "first_error" for the error code and "first_error_gfn" for the GFN.

Anyway, preferably with the suggested adjustments, applicable hypervisor parts

Jan

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

#3 lore raw

PATCH v5 3/4 x86/mm: Pull out the p2m specifics from p2m_init_altp2m_ept

In reply to Alexandru Stefan ISAILA

---
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

---
 xen/arch/x86/mm/p2m-ept.c | 6 ------
 xen/arch/x86/mm/p2m.c     | 6 ++++++
 2 files changed, 6 insertions(+), 6 deletions(-)
xen/arch/x86/mm/p2m-ept.c +0 -6
index b5517769c9..d861cd7b51 100644
@@ -1357,13 +1357,7 @@ void p2m_init_altp2m_ept(struct domain *d, unsigned int i)
     struct p2m_domain *hostp2m = p2m_get_hostp2m(d);
     struct ept_data *ept;
 
-    p2m->default_access = hostp2m->default_access;
-    p2m->domain = hostp2m->domain;
-
-    p2m->global_logdirty = hostp2m->global_logdirty;
     p2m->ept.ad = hostp2m->ept.ad;
-    p2m->min_remapped_gfn = gfn_x(INVALID_GFN);
-    p2m->max_mapped_pfn = p2m->max_remapped_gfn = 0;
     ept = &p2m->ept;
     ept->mfn = pagetable_get_pfn(p2m_get_pagetable(p2m));
     d->arch.altp2m_eptp[i] = ept->eptp;
xen/arch/x86/mm/p2m.c +6 -0
index d92613ebe4..cb5b8d67d1 100644
@@ -2559,6 +2559,12 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)
         goto out;
     }
 
+    p2m->default_access = hostp2m->default_access;
+    p2m->domain = hostp2m->domain;
+    p2m->global_logdirty = hostp2m->global_logdirty;
+    p2m->min_remapped_gfn = gfn_x(INVALID_GFN);
+    p2m->max_mapped_pfn = p2m->max_remapped_gfn = 0;
+
     p2m_init_altp2m_ept(d, idx);
 
  out:
Signature · 6 lines
2.17.1

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

PATCH v5 4/4 x86/mm: Make use of the default access param from xc_altp2m_create_view

In reply to Alexandru Stefan ISAILA

At this moment the default_access param from xc_altp2m_create_view is not used.

This patch assigns default_access to p2m->default_access at the time of initializing a new altp2m view.

---
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: George Dunlap <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Julien Grall <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
CC: George Dunlap <[email protected]>
---
Changes since V4:
- Add const struct p2m_domain *p2m to
xenmem_access_to_p2m_access()
- Pull xenmem_access_to_p2m_access() out of the locked area
- Add a check for NULL p2m in xenmem_access_to_p2m_access().

---
 xen/arch/x86/hvm/hvm.c          |  3 ++-
 xen/arch/x86/mm/mem_access.c    | 11 +++++++----
 xen/arch/x86/mm/p2m.c           | 21 ++++++++++++++++-----
 xen/include/asm-x86/p2m.h       |  3 ++-
 xen/include/public/hvm/hvm_op.h |  2 --
 xen/include/xen/mem_access.h    |  4 ++++
 6 files changed, 31 insertions(+), 13 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -1
index 98d1d9788b..d7a55568c9 100644
@@ -4687,7 +4687,8 @@ static int do_altp2m_op(
     }
 
     case HVMOP_altp2m_create_p2m:
-        if ( !(rc = p2m_init_next_altp2m(d, &a.u.view.view)) )
+        if ( !(rc = p2m_init_next_altp2m(d, &a.u.view.view,
+                                         a.u.view.hvmmem_default_access)) )
             rc = __copy_to_guest(arg, &a, 1) ? -EFAULT : 0;
         break;
 
xen/arch/x86/mm/mem_access.c +7 -4
index 33e379db8f..5b74a6898b 100644
@@ -314,9 +314,9 @@ static int set_mem_access(struct domain *d, struct p2m_domain *p2m,
     return rc;
 }
 
-static bool xenmem_access_to_p2m_access(struct p2m_domain *p2m,
-                                        xenmem_access_t xaccess,
-                                        p2m_access_t *paccess)
+bool xenmem_access_to_p2m_access(const struct p2m_domain *p2m,
+                                 xenmem_access_t xaccess,
+                                 p2m_access_t *paccess)
 {
     static const p2m_access_t memaccess[] = {
 #define ACCESS(ac) [XENMEM_access_##ac] = p2m_access_##ac
@@ -340,7 +340,10 @@ static bool xenmem_access_to_p2m_access(struct p2m_domain *p2m,
         *paccess = memaccess[xaccess];
         break;
     case XENMEM_access_default:
-        *paccess = p2m->default_access;
+        if ( !p2m )
+            *paccess = p2m_access_rwx;
+        else
+            *paccess = p2m->default_access;
         break;
     default:
         return false;
xen/arch/x86/mm/p2m.c +16 -5
index cb5b8d67d1..2774811bb8 100644
@@ -25,6 +25,7 @@
 
 #include <xen/guest_access.h> /* copy_from_guest() */
 #include <xen/iommu.h>
+#include <xen/mem_access.h>
 #include <xen/vm_event.h>
 #include <xen/event.h>
 #include <public/vm_event.h>
@@ -2533,7 +2534,8 @@ void p2m_flush_altp2m(struct domain *d)
     altp2m_list_unlock(d);
 }
 
-static int p2m_activate_altp2m(struct domain *d, unsigned int idx)
+static int p2m_activate_altp2m(struct domain *d, unsigned int idx,
+                               p2m_access_t hvmmem_default_access)
 {
     struct p2m_domain *hostp2m, *p2m;
     int rc;
@@ -2559,7 +2561,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)
         goto out;
     }
 
-    p2m->default_access = hostp2m->default_access;
+    p2m->default_access = hvmmem_default_access;
     p2m->domain = hostp2m->domain;
     p2m->global_logdirty = hostp2m->global_logdirty;
     p2m->min_remapped_gfn = gfn_x(INVALID_GFN);
@@ -2576,6 +2578,7 @@ static int p2m_activate_altp2m(struct domain *d, unsigned int idx)
 int p2m_init_altp2m_by_id(struct domain *d, unsigned int idx)
 {
     int rc = -EINVAL;
+    struct p2m_domain *hostp2m = p2m_get_hostp2m(d);
 
     if ( idx >= MAX_ALTP2M )
         return rc;
@@ -2584,16 +2587,23 @@ int p2m_init_altp2m_by_id(struct domain *d, unsigned int idx)
     altp2m_list_lock(d);
 
     if ( d->arch.altp2m_eptp[idx] == mfn_x(INVALID_MFN) )
-        rc = p2m_activate_altp2m(d, idx);
+        rc = p2m_activate_altp2m(d, idx, hostp2m->default_access);
 
     altp2m_list_unlock(d);
     return rc;
 }
 
-int p2m_init_next_altp2m(struct domain *d, uint16_t *idx)
+int p2m_init_next_altp2m(struct domain *d, uint16_t *idx,
+                         xenmem_access_t hvmmem_default_access)
 {
     int rc = -EINVAL;
     unsigned int i;
+    p2m_access_t a;
+    struct p2m_domain *p2m;
+
+    if ( hvmmem_default_access > XENMEM_access_default ||
+         !xenmem_access_to_p2m_access(NULL, hvmmem_default_access, &a) )
+        return rc;
 
     altp2m_list_lock(d);
 
@@ -2602,7 +2612,8 @@ int p2m_init_next_altp2m(struct domain *d, uint16_t *idx)
         if ( d->arch.altp2m_eptp[i] != mfn_x(INVALID_MFN) )
             continue;
 
-        rc = p2m_activate_altp2m(d, i);
+        p2m = d->arch.altp2m_p2m[i];
+        rc = p2m_activate_altp2m(d, i, a);
 
         if ( !rc )
             *idx = i;
xen/include/asm-x86/p2m.h +2 -1
index 94285db1b4..ac2d2787f4 100644
@@ -884,7 +884,8 @@ bool p2m_altp2m_get_or_propagate(struct p2m_domain *ap2m, unsigned long gfn_l,
 int p2m_init_altp2m_by_id(struct domain *d, unsigned int idx);
 
 /* Find an available alternate p2m and make it valid */
-int p2m_init_next_altp2m(struct domain *d, uint16_t *idx);
+int p2m_init_next_altp2m(struct domain *d, uint16_t *idx,
+                         xenmem_access_t hvmmem_default_access);
 
 /* Make a specific alternate p2m invalid */
 int p2m_destroy_altp2m_by_id(struct domain *d, unsigned int idx);
xen/include/public/hvm/hvm_op.h +0 -2
index 5446d634d8..49816d9312 100644
@@ -251,8 +251,6 @@ DEFINE_XEN_GUEST_HANDLE(xen_hvm_altp2m_vcpu_disable_notify_t);
 struct xen_hvm_altp2m_view {
     /* IN/OUT variable */
     uint16_t view;
-    /* Create view only: default access type
-     * NOTE: currently ignored */
     uint16_t hvmmem_default_access; /* xenmem_access_t */
 };
 typedef struct xen_hvm_altp2m_view xen_hvm_altp2m_view_t;
xen/include/xen/mem_access.h +4 -0
index 00e594a0ad..5d53fb8ce4 100644
@@ -58,6 +58,10 @@ typedef enum {
     /* NOTE: Assumed to be only 4 bits right now on x86. */
 } p2m_access_t;
 
+bool xenmem_access_to_p2m_access(const struct p2m_domain *p2m,
+                                 xenmem_access_t xaccess,
+                                 p2m_access_t *paccess);
+
 /*
  * Set access type for a region of gfns.
  * If gfn == INVALID_GFN, sets the default access type.
Signature · 6 lines
2.17.1

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw
In reply to Alexandru Stefan ISAILA
Quoted 30 lines · Alexandru Stefan ISAILA

This patch aims to sanitize indexes, potentially guest provided values, for altp2m_eptp[] and altp2m_p2m[] arrays.

---
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
---
Changes since V4:
- Change bounds check from MAX_EPTP to MAX_ALTP2M
- Move array_index_nospec() closer to the bounds check.

---
 xen/arch/x86/mm/mem_access.c | 15 +++++++++------
 xen/arch/x86/mm/p2m.c        | 20 ++++++++++++++------
 2 files changed, 23 insertions(+), 12 deletions(-)
xen/arch/x86/mm/mem_access.c +0 -0
index 320b9fe621..33e379db8f 100644

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 370

Quoted 5 lines
xen/arch/x86/mm/mem_access.c +1 -1
@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,
     if ( altp2m_idx )
     {
         if ( altp2m_idx >= MAX_ALTP2M ||
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==

As implied by a reply to v4, this is still latently buggy: There's no guarantee anyone will notice the issue here when bumping MAX_ALTP2M past MAX_EPTP. The only future proof thing to do here is, as suggested, using some form of min(MAX_ALTP2M, MAX_EPTP) in the actual bounds check. Then each array access itself can be made use the correct bound. In fact you'd probably have noticed this if you had made use of array_access_nospec() where possible (which also would help readability) - apparently not here, but ...

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 374

Quoted 5 lines
xen/arch/x86/mm/mem_access.c +2 -1
+             mfn_x(INVALID_MFN) )
             return -EINVAL;
 
-        ap2m = d->arch.altp2m_p2m[altp2m_idx];
+        ap2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx, MAX_ALTP2M)];

... here. The min() suggested above would then better become min(ARRAY_SIZE(d->arch.altp2m_eptp), MAX_EPTP), which I think would then even compile cleanly (the apparently simpler form above wouldn't as is afaict).

xen/arch/x86/mm/p2m.c int p2m_init_altp2m_by_id(struct domain *d, unsigned int idx) · line 2578

Quoted 8 lines
xen/arch/x86/mm/p2m.c +1 -0
@@ -2574,6 +2574,7 @@ int p2m_init_altp2m_by_id(struct domain *d, unsigned int idx)
     if ( idx >= MAX_ALTP2M )
         return rc;
 
+    idx = array_index_nospec(idx, MAX_ALTP2M);
     altp2m_list_lock(d);

I wouldn't object to there being no blank line between the if() and the line you add, but you surely want a blank line ahead of the unrelated lock acquire (similarly at least once more below).

Jan

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

#6 lore raw
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Alexandru Stefan ISAILA

This patch aims to sanitize indexes, potentially guest provided values, for altp2m_eptp[] and altp2m_p2m[] arrays.

---
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
---
Changes since V4:
- Change bounds check from MAX_EPTP to MAX_ALTP2M
- Move array_index_nospec() closer to the bounds check.

---
  xen/arch/x86/mm/mem_access.c | 15 +++++++++------
  xen/arch/x86/mm/p2m.c        | 20 ++++++++++++++------
  2 files changed, 23 insertions(+), 12 deletions(-)
Quoted 8 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 369

Quoted 8 lines
xen/arch/x86/mm/mem_access.c +0 -0
index 320b9fe621..33e379db8f 100644
@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,
      if ( altp2m_idx )
      {
          if ( altp2m_idx >= MAX_ALTP2M ||

Ok, so have if ( altp2m_idx >= min(ARRAY_SIZE(d->arch.altp2m_eptp), MAX_EPTP) ||
here and then...

Quoted 2 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 370

Quoted 2 lines
xen/arch/x86/mm/mem_access.c +1 -1
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==

have MAX_EPTP here and ...

Quoted 14 lines

As implied by a reply to v4, this is still latently buggy: There's no guarantee anyone will notice the issue here when bumping MAX_ALTP2M past MAX_EPTP. The only future proof thing to do here is, as suggested, using some form of min(MAX_ALTP2M, MAX_EPTP) in the actual bounds check. Then each array access itself can be made use the correct bound. In fact you'd probably have noticed this if you had made use of array_access_nospec() where possible (which also would help readability) - apparently not here, but ... >

Quoted 5 lines
xen/arch/x86/mm/mem_access.c +2 -1
+             mfn_x(INVALID_MFN) )
              return -EINVAL;
 
-        ap2m = d->arch.altp2m_p2m[altp2m_idx];
+        ap2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx, MAX_ALTP2M)];

MAX_ALTP2M here ?

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

#7 lore raw
Quoted 56 lines · Alexandru Stefan ISAILA
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Alexandru Stefan ISAILA

This patch aims to sanitize indexes, potentially guest provided values, for altp2m_eptp[] and altp2m_p2m[] arrays.

---
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
---
Changes since V4:
- Change bounds check from MAX_EPTP to MAX_ALTP2M
- Move array_index_nospec() closer to the bounds check.

---
  xen/arch/x86/mm/mem_access.c | 15 +++++++++------
  xen/arch/x86/mm/p2m.c        | 20 ++++++++++++++------
  2 files changed, 23 insertions(+), 12 deletions(-)
Quoted 8 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 369

Quoted 8 lines
xen/arch/x86/mm/mem_access.c +0 -0
index 320b9fe621..33e379db8f 100644
@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,
      if ( altp2m_idx )
      {
          if ( altp2m_idx >= MAX_ALTP2M ||

Ok, so have if ( altp2m_idx >= min(ARRAY_SIZE(d->arch.altp2m_eptp), MAX_EPTP) ||
here and then...

Quoted 2 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 370

Quoted 2 lines
xen/arch/x86/mm/mem_access.c +1 -1
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==

have MAX_EPTP here and ...

Quoted 9 lines

As implied by a reply to v4, this is still latently buggy: There's no guarantee anyone will notice the issue here when bumping MAX_ALTP2M past MAX_EPTP. The only future proof thing to do here is, as suggested, using some form of min(MAX_ALTP2M, MAX_EPTP) in the actual bounds check. Then each array access itself can be made use the correct bound. In fact you'd probably have noticed this if you had made use of array_access_nospec() where possible (which also would help readability) - apparently not here, but ... >

Quoted 7 lines
Quoted 5 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 374

Quoted 5 lines
xen/arch/x86/mm/mem_access.c +2 -1
+             mfn_x(INVALID_MFN) )
              return -EINVAL;
 
-        ap2m = d->arch.altp2m_p2m[altp2m_idx];
+        ap2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx, MAX_ALTP2M)];

MAX_ALTP2M here ?

Yes, that's how I think it ought to be. Give others a chance to disagree with me, though.

Jan

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

#8 lore raw
Quoted 60 lines · Jan Beulich
Quoted 59 lines · Alexandru Stefan ISAILA
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Alexandru Stefan ISAILA

This patch aims to sanitize indexes, potentially guest provided values, for altp2m_eptp[] and altp2m_p2m[] arrays.

---
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
---
Changes since V4:
- Change bounds check from MAX_EPTP to MAX_ALTP2M
- Move array_index_nospec() closer to the bounds check.

---
   xen/arch/x86/mm/mem_access.c | 15 +++++++++------
   xen/arch/x86/mm/p2m.c        | 20 ++++++++++++++------
   2 files changed, 23 insertions(+), 12 deletions(-)
Quoted 8 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 369

Quoted 8 lines
xen/arch/x86/mm/mem_access.c +0 -0
index 320b9fe621..33e379db8f 100644
@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,
       if ( altp2m_idx )
       {
           if ( altp2m_idx >= MAX_ALTP2M ||

Ok, so have if ( altp2m_idx >= min(ARRAY_SIZE(d->arch.altp2m_eptp), MAX_EPTP) ||
here and then...

Quoted 2 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 370

Quoted 2 lines
xen/arch/x86/mm/mem_access.c +1 -1
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==

have MAX_EPTP here and ...

Quoted 12 lines

As implied by a reply to v4, this is still latently buggy: There's no guarantee anyone will notice the issue here when bumping MAX_ALTP2M past MAX_EPTP. The only future proof thing to do here is, as suggested, using some form of min(MAX_ALTP2M, MAX_EPTP) in the actual bounds check. Then each array access itself can be made use the correct bound. In fact you'd probably have noticed this if you had made use of array_access_nospec() where possible (which also would help readability) - apparently not here, but ... >

Quoted 3 lines
xen/arch/x86/mm/mem_access.c +1 -0
+             mfn_x(INVALID_MFN) )
               return -EINVAL;
 
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 374

Quoted 2 lines
xen/arch/x86/mm/mem_access.c +1 -1
-        ap2m = d->arch.altp2m_p2m[altp2m_idx];
+        ap2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx, MAX_ALTP2M)];

MAX_ALTP2M here ?

Yes, that's how I think it ought to be. Give others a chance to disagree with me, though.

There is a slight problem with using (ARRAY_SIZE(..)) it will give "error: static assertion failed:" on __must_be_array(x) because d->arch.altp2m_eptp is not static.

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

#9 lore raw
Quoted 33 lines · Alexandru Stefan ISAILA
Quoted 30 lines · Jan Beulich
Quoted 29 lines · Alexandru Stefan ISAILA
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Alexandru Stefan ISAILA

This patch aims to sanitize indexes, potentially guest provided values, for altp2m_eptp[] and altp2m_p2m[] arrays.

---
CC: Razvan Cojocaru <[email protected]>
CC: Tamas K Lengyel <[email protected]>
CC: Petre Pircalabu <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
---
Changes since V4:
- Change bounds check from MAX_EPTP to MAX_ALTP2M
- Move array_index_nospec() closer to the bounds check.

---
   xen/arch/x86/mm/mem_access.c | 15 +++++++++------
   xen/arch/x86/mm/p2m.c        | 20 ++++++++++++++------
   2 files changed, 23 insertions(+), 12 deletions(-)
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/mm/mem_access.c +0 -0
index 320b9fe621..33e379db8f 100644
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 369

Quoted 4 lines
xen/arch/x86/mm/mem_access.c +0 -0
@@ -367,10 +367,11 @@ long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr,
       if ( altp2m_idx )
       {
           if ( altp2m_idx >= MAX_ALTP2M ||

Ok, so have if ( altp2m_idx >= min(ARRAY_SIZE(d->arch.altp2m_eptp), MAX_EPTP) ||
here and then...

The 1st arg to min() equals the 2nd, which is ...

Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 2 lines

xen/arch/x86/mm/mem_access.c long p2m_set_mem_access(struct domain *d, gfn_t gfn, uint32_t nr, · line 370

Quoted 2 lines
xen/arch/x86/mm/mem_access.c +1 -1
-             d->arch.altp2m_eptp[altp2m_idx] == mfn_x(INVALID_MFN) )
+             d->arch.altp2m_eptp[array_index_nospec(altp2m_idx, MAX_ALTP2M)] ==

have MAX_EPTP here and ...

Quoted 14 lines

As implied by a reply to v4, this is still latently buggy: There's no guarantee anyone will notice the issue here when bumping MAX_ALTP2M past MAX_EPTP. The only future proof thing to do here is, as suggested, using some form of min(MAX_ALTP2M, MAX_EPTP) in the actual bounds check. Then each array access itself can be made use the correct bound. In fact you'd probably have noticed this if you had made use of array_access_nospec() where possible (which also would help readability) - apparently not here, but ... >

Quoted 5 lines
xen/arch/x86/mm/mem_access.c +2 -1
+             mfn_x(INVALID_MFN) )
               return -EINVAL;
 
-        ap2m = d->arch.altp2m_p2m[altp2m_idx];
+        ap2m = d->arch.altp2m_p2m[array_index_nospec(altp2m_idx, MAX_ALTP2M)];

MAX_ALTP2M here ?

Quoted 7 lines
Quoted 3 lines

Yes, that's how I think it ought to be. Give others a chance to disagree with me, though.

There is a slight problem with using (ARRAY_SIZE(..)) it will give "error: static assertion failed:" on __must_be_array(x) because d->arch.altp2m_eptp is not static.

... causing this. Once you use the correct array above, I think things will work.

Jan

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