Skip to contentSkip to conversation

xen/memop: Latch current->domain in a local variable

PATCH In review Andrew Cooper 2 participants 8 messages Aug 19, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 xen/memop: Latch current->domain in a local variable

It is more efficient.

---
CC: Jan Beulich <[email protected]>
CC: George Dunlap <[email protected]>

---
 xen/common/memory.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/common/memory.c +4 -4
index 812334b..1ead35c 100644
@@ -864,7 +864,7 @@ static long xatp_permission_check(struct domain *d, unsigned int space)
 
 long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 {
-    struct domain *d;
+    struct domain *d, *curr_d = current->domain;
     long rc;
     struct xen_memory_reservation reservation;
     struct memop_args args;
@@ -905,7 +905,7 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
              && (reservation.mem_flags & XENMEMF_populate_on_demand) )
             args.memflags |= MEMF_populate_on_demand;
 
-        if ( xsm_memory_adjust_reservation(XSM_TARGET, current->domain, d) )
+        if ( xsm_memory_adjust_reservation(XSM_TARGET, curr_d, d) )
         {
             rcu_unlock_domain(d);
             return start_extent;
@@ -962,7 +962,7 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( d == NULL )
             return -ESRCH;
 
-        rc = xsm_memory_stat_reservation(XSM_TARGET, current->domain, d);
+        rc = xsm_memory_stat_reservation(XSM_TARGET, curr_d, d);
         if ( rc )
         {
             rcu_unlock_domain(d);
@@ -1086,7 +1086,7 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( d == NULL )
             return -ESRCH;
 
-        rc = xsm_remove_from_physmap(XSM_TARGET, current->domain, d);
+        rc = xsm_remove_from_physmap(XSM_TARGET, curr_d, d);
         if ( rc )
         {
             rcu_unlock_domain(d);
Signature · 7 lines
2.1.4


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

PATCH 2/2 xen/physmap: Do not permit a guest to populate PoD pages for itself

PoD is supposed to be entirely transparent to guest, but this interface has been left exposed for a long time.

The use of PoD requires careful co-ordination by the toolstack with the XENMEM_{get,set}_pod_target hypercalls, and xenstore ballooning target. The best a guest can do without toolstack cooperation crash.

Furthermore, there are combinations of features (e.g. c/s c63868ff "libxl:
disallow PCI device assignment for HVM guest when PoD is enabled") which a toolstack might wish to explicitly prohibit (in this case, because the two simply don't function in combination). In such cases, the guest mustn't be able to subvert the configuration chosen by the toolstack.

---
CC: Jan Beulich <[email protected]>
CC: George Dunlap <[email protected]>

---
 xen/common/memory.c | 9 +++++++++
 1 file changed, 9 insertions(+)
xen/common/memory.c +9 -0
index 1ead35c..ccb9207 100644
@@ -903,7 +903,16 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         if ( op == XENMEM_populate_physmap
              && (reservation.mem_flags & XENMEMF_populate_on_demand) )
+        {
+            /* Disallow populating PoD pages on oneself. */
+            if ( d == curr_d )
+            {
+                rcu_unlock_domain(d);
+                return start_extent;
+            }
+
             args.memflags |= MEMF_populate_on_demand;
+        }
 
         if ( xsm_memory_adjust_reservation(XSM_TARGET, curr_d, d) )
         {
Signature · 7 lines
2.1.4


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

Re: [PATCH 2/2] xen/physmap: Do not permit a guest to populate PoD pages for itself

In reply to Andrew Cooper
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 16:12, <[email protected]> wrote:
xen/common/memory.c +3 -0
@@ -903,7 +903,16 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         if ( op == XENMEM_populate_physmap
              && (reservation.mem_flags & XENMEMF_populate_on_demand) )
+        {
+            /* Disallow populating PoD pages on oneself. */
+            if ( d == curr_d )

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 918

Quoted 10 lines
xen/common/memory.c +6 -0
+            {
+                rcu_unlock_domain(d);
+                return start_extent;
+            }
+
             args.memflags |= MEMF_populate_on_demand;
+        }
 
         if ( xsm_memory_adjust_reservation(XSM_TARGET, curr_d, d) )
         {

Considering the code continues

            rcu_unlock_domain(d);
            return start_extent;
        }

        switch ( op )
        {
        case XENMEM_increase_reservation:
            increase_reservation(&args);
            break;
        case XENMEM_decrease_reservation:
            decrease_reservation(&args);
            break;
        default: /* XENMEM_populate_physmap */
            populate_physmap(&args);
            break;
        }

I think it is time to move this XENMEM_populate_physmap specific code chunk into populate_physmap(), at once eliminating the need for another not entirely trivial error exit path here.

Jan

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

#3 lore raw

Re: [PATCH 2/2] xen/physmap: Do not permit a guest to populate PoD pages for itself

Quoted 39 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 16:12, <[email protected]> wrote:
xen/common/memory.c +3 -0
@@ -903,7 +903,16 @@ long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         if ( op == XENMEM_populate_physmap
              && (reservation.mem_flags & XENMEMF_populate_on_demand) )
+        {
+            /* Disallow populating PoD pages on oneself. */
+            if ( d == curr_d )

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 918

Quoted 10 lines
xen/common/memory.c +6 -0
+            {
+                rcu_unlock_domain(d);
+                return start_extent;
+            }
+
             args.memflags |= MEMF_populate_on_demand;
+        }
 
         if ( xsm_memory_adjust_reservation(XSM_TARGET, curr_d, d) )
         {

Considering the code continues

            rcu_unlock_domain(d);
            return start_extent;
        }

        switch ( op )
        {
        case XENMEM_increase_reservation:
            increase_reservation(&args);
            break;
        case XENMEM_decrease_reservation:
            decrease_reservation(&args);
            break;
        default: /* XENMEM_populate_physmap */
            populate_physmap(&args);
            break;
        }
Quoted 3 lines

I think it is time to move this XENMEM_populate_physmap specific code chunk into populate_physmap(), at once eliminating the need for another not entirely trivial error exit path here.

Do you mean just my code addition, or the whole if condition? The former is easy, but the latter is hard as reservation is specifically not passed into populate_physmap().

~Andrew

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

#4 lore raw

Re: [PATCH 2/2] xen/physmap: Do not permit a guest to populate PoD pages for itself

Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 17:02, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich

xen/common/memory.c long do_memory_op(unsigned long cmd,

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 16:12, <[email protected]> wrote:
xen/common/memory.c +0 -0
@@ -903,7 +903,16 @@ long do_memory_op(unsigned long cmd, 

XEN_GUEST_HANDLE_PARAM(void) arg)

Quoted 35 lines
Quoted 6 lines
xen/common/memory.c +3 -0
         if ( op == XENMEM_populate_physmap
              && (reservation.mem_flags & XENMEMF_populate_on_demand) )
+        {
+            /* Disallow populating PoD pages on oneself. */
+            if ( d == curr_d )

xen/common/memory.c long do_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 918

Quoted 10 lines
xen/common/memory.c +6 -0
+            {
+                rcu_unlock_domain(d);
+                return start_extent;
+            }
+
             args.memflags |= MEMF_populate_on_demand;
+        }
 
         if ( xsm_memory_adjust_reservation(XSM_TARGET, curr_d, d) )
         {

Considering the code continues

            rcu_unlock_domain(d);
            return start_extent;
        }

        switch ( op )
        {
        case XENMEM_increase_reservation:
            increase_reservation(&args);
            break;
        case XENMEM_decrease_reservation:
            decrease_reservation(&args);
            break;
        default: /* XENMEM_populate_physmap */
            populate_physmap(&args);
            break;
        }
Quoted 7 lines
Quoted 3 lines

I think it is time to move this XENMEM_populate_physmap specific code chunk into populate_physmap(), at once eliminating the need for another not entirely trivial error exit path here.

Do you mean just my code addition, or the whole if condition? The former is easy, but the latter is hard as reservation is specifically not passed into populate_physmap().

Oh, I didn't realize that - just your addition then.

Jan

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

#5 lore raw

PATCH v2 2/2 xen/physmap: Do not permit a guest to populate PoD pages for itself

PoD is supposed to be entirely transparent to guest, but this interface has been left exposed for a long time.

The use of PoD requires careful co-ordination by the toolstack with the XENMEM_{get,set}_pod_target hypercalls, and xenstore ballooning target. The best a guest can do without toolstack cooperation crash.

Furthermore, there are combinations of features (e.g. c/s c63868ff "libxl:
disallow PCI device assignment for HVM guest when PoD is enabled") which a toolstack might wish to explicitly prohibit (in this case, because the two simply don't function in combination). In such cases, the guest mustn't be able to subvert the configuration chosen by the toolstack.

---
CC: Jan Beulich <[email protected]>
CC: George Dunlap <[email protected]>

v2:
* Move the exclusion logic into populate_physmap(). No functional change.

---
 xen/common/memory.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
xen/common/memory.c +6 -2
index 1ead35c..f34dd56 100644
@@ -140,14 +140,14 @@ static void populate_physmap(struct memop_args *a)
     struct page_info *page;
     unsigned int i, j;
     xen_pfn_t gpfn, mfn;
-    struct domain *d = a->domain;
+    struct domain *d = a->domain, *curr_d = current->domain;
 
     if ( !guest_handle_subrange_okay(a->extent_list, a->nr_done,
                                      a->nr_extents-1) )
         return;
 
     if ( a->extent_order > (a->memflags & MEMF_populate_on_demand ? MAX_ORDER :
-                            max_order(current->domain)) )
+                            max_order(curr_d)) )
         return;
 
     for ( i = a->nr_done; i < a->nr_extents; i++ )
@@ -163,6 +163,10 @@ static void populate_physmap(struct memop_args *a)
 
         if ( a->memflags & MEMF_populate_on_demand )
         {
+            /* Disallow populating PoD pages on oneself. */
+            if ( d == curr_d )
+                goto out;
+
             if ( guest_physmap_mark_populate_on_demand(d, gpfn,
                                                        a->extent_order) < 0 )
                 goto out;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] xen/physmap: Do not permit a guest to populate PoD pages for itself

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.08.16 at 18:00, <[email protected]> wrote:

PoD is supposed to be entirely transparent to guest, but this interface has been left exposed for a long time.

The use of PoD requires careful co-ordination by the toolstack with the XENMEM_{get,set}_pod_target hypercalls, and xenstore ballooning target. The best a guest can do without toolstack cooperation crash.

Quoted 7 lines

Furthermore, there are combinations of features (e.g. c/s c63868ff "libxl:
disallow PCI device assignment for HVM guest when PoD is enabled") which a toolstack might wish to explicitly prohibit (in this case, because the two simply don't function in combination). In such cases, the guest mustn't be able to subvert the configuration chosen by the toolstack.

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