Skip to contentSkip to conversation

fix BUG in gnttab_unpopulate_status_frames()

PATCH In review Paul Durrant 3 participants 5 messages Jul 30, 2019 – Aug 12, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix BUG in gnttab_unpopulate_status_frames()

Since commit ec83f825627 "mm.h: add helper function to test-and-clear _PGC_allocated" (and subsequent fix-up 44a887d021d "mm.h: fix BUG_ON() condition in put_page_alloc_ref()") setting grant table version from 2 back to 1 has been vulnerable to hitting the BUG_ON in put_page_alloc_ref() during gnttab_unpopulate_status_frames() because that function does not acquire a local page reference.

This patch fixes the problem by first acquiring a local page reference on a status frame (which should always succeed and so a failure results in a domain_crash()) before attempting to 'unassign' it from the guest by dropping the allocation reference. The local reference can then be dropped.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 xen/common/grant_table.c | 9 +++++++++
 1 file changed, 9 insertions(+)
xen/common/grant_table.c +9 -0
index 97695a221a..b9ca388051 100644
@@ -1682,6 +1682,14 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
         struct page_info *pg = virt_to_page(gt->status[i]);
         gfn_t gfn = gnttab_get_frame_gfn(gt, true, i);
 
+        if ( !get_page(pg, d) )
+        {
+            gprintk(XENLOG_ERR,
+                    "Could not get a reference to status frame %u\n", i);
+            domain_crash(d);
+            return -EINVAL;
+        }
+
         /*
          * For translated domains, recovering from failure after partial
          * changes were made is more complicated than it seems worth
@@ -1708,6 +1716,7 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
 
         BUG_ON(page_get_owner(pg) != d);
         put_page_alloc_ref(pg);
+        put_page(pg);
 
         if ( pg->count_info & ~PGC_xen_heap )
         {
Signature · 7 lines
2.20.1.2.gb21ebb671


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 19 lines · Paul Durrant
xen/common/grant_table.c +8 -0
@@ -1682,6 +1682,14 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
          struct page_info *pg = virt_to_page(gt->status[i]);
          gfn_t gfn = gnttab_get_frame_gfn(gt, true, i);
 
+        if ( !get_page(pg, d) )
+        {
+            gprintk(XENLOG_ERR,
+                    "Could not get a reference to status frame %u\n", i);
+            domain_crash(d);
+            return -EINVAL;
+        }
+
          /*
           * For translated domains, recovering from failure after partial
           * changes were made is more complicated than it seems worth

xen/common/grant_table.c gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt) · line 1722

Quoted 7 lines
xen/common/grant_table.c +1 -0
@@ -1708,6 +1716,7 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
 
          BUG_ON(page_get_owner(pg) != d);
          put_page_alloc_ref(pg);
+        put_page(pg);
 
          if ( pg->count_info & ~PGC_xen_heap )
          {

I dislike this approach, and not chosing the alternative of excluding xenheap pages in the check in put_page_alloc_ref() (as I had recommended elsewhere) should at least be discussed in the description. It is the very nature of xenheap pages that they won't get freed, and hence don't need this extra ref to be held for clearing PGC_allocated.

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

#2 lore raw
Quoted 24 lines · Jan Beulich
Quoted 23 lines · Paul Durrant
xen/common/grant_table.c +9 -0
@@ -1682,6 +1682,14 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
          struct page_info *pg = virt_to_page(gt->status[i]);
          gfn_t gfn = gnttab_get_frame_gfn(gt, true, i);
 
+        if ( !get_page(pg, d) )
+        {
+            gprintk(XENLOG_ERR,
+                    "Could not get a reference to status frame %u\n", i);
+            domain_crash(d);
+            return -EINVAL;
+        }
+
          /*
           * For translated domains, recovering from failure after partial
           * changes were made is more complicated than it seems worth
@@ -1708,6 +1716,7 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
 
          BUG_ON(page_get_owner(pg) != d);
          put_page_alloc_ref(pg);
+        put_page(pg);
 
Quoted 9 lines

xen/common/grant_table.c gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt) · line 1722

Quoted 3 lines
xen/common/grant_table.c +0 -0
          if ( pg->count_info & ~PGC_xen_heap )
          {

I dislike this approach, and not chosing the alternative of excluding xenheap pages in the check in put_page_alloc_ref() (as I had recommended elsewhere) should at least be discussed in the description. It is the very nature of xenheap pages that they won't get freed, and hence don't need this extra ref to be held for clearing PGC_allocated.

Also, it looks like there are other places where the BUG_ON() may / should be firing: namely, vmx_free_vlapic_mapping() and unshare_xenoprof_page_with_guest(). Teaching put_page_alloc_ref() that dropping PGC_allocated when PGC_xen_heap is set is safe would fix all three at once.

Possibly more importantly, suppose that the first time gnttab_unpopulate_status_frames() comes around, gt->status[1] is still mapped by the guest. Then gt->status[0] will have its refcount reduced to 0 (but not freed), but gt->status[1] will be restored to its previous state. If the guest unmaps gt->status[1] and gnttab_unpopulate_status_frames() is called again, then the get_page(gt->status[0]) will fail (since its refcount is 0), causing the guest to be crashed instead.

Not terrible for such a wonkily-behaving guest; but I think I'd rather go with the "special-case xenheap pages" option.

-George

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

#3 lore raw
Quoted 60 lines

-----Original Message-----
From: George Dunlap <[email protected]>
Sent: 02 August 2019 16:28
To: Jan Beulich <[email protected]>; Paul Durrant <[email protected]>
Cc: [email protected]; Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Stefano Stabellini <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>; Wei Liu <[email protected]>
Subject: Re: [PATCH] fix BUG in gnttab_unpopulate_status_frames()

Quoted 24 lines · Jan Beulich
Quoted 23 lines · Paul Durrant
xen/common/grant_table.c +9 -0
@@ -1682,6 +1682,14 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
          struct page_info *pg = virt_to_page(gt->status[i]);
          gfn_t gfn = gnttab_get_frame_gfn(gt, true, i);
 
+        if ( !get_page(pg, d) )
+        {
+            gprintk(XENLOG_ERR,
+                    "Could not get a reference to status frame %u\n", i);
+            domain_crash(d);
+            return -EINVAL;
+        }
+
          /*
           * For translated domains, recovering from failure after partial
           * changes were made is more complicated than it seems worth
@@ -1708,6 +1716,7 @@ gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt)
 
          BUG_ON(page_get_owner(pg) != d);
          put_page_alloc_ref(pg);
+        put_page(pg);
 
Quoted 9 lines

xen/common/grant_table.c gnttab_unpopulate_status_frames(struct domain *d, struct grant_table *gt) · line 1722

Quoted 3 lines
xen/common/grant_table.c +0 -0
          if ( pg->count_info & ~PGC_xen_heap )
          {

I dislike this approach, and not chosing the alternative of excluding xenheap pages in the check in put_page_alloc_ref() (as I had recommended elsewhere) should at least be discussed in the description. It is the very nature of xenheap pages that they won't get freed, and hence don't need this extra ref to be held for clearing PGC_allocated.

Also, it looks like there are other places where the BUG_ON() may / should be firing: namely, vmx_free_vlapic_mapping() and unshare_xenoprof_page_with_guest(). Teaching put_page_alloc_ref() that dropping PGC_allocated when PGC_xen_heap is set is safe would fix all three at once.

Possibly more importantly, suppose that the first time gnttab_unpopulate_status_frames() comes around, gt->status[1] is still mapped by the guest. Then gt->status[0] will have its refcount reduced to 0 (but not freed), but gt->status[1] will be restored to its previous state. If the guest unmaps gt->status[1] and gnttab_unpopulate_status_frames() is called again, then the get_page(gt->status[0]) will fail (since its refcount is 0), causing the guest to be crashed instead.

Quoted 2 lines

Not terrible for such a wonkily-behaving guest; but I think I'd rather go with the "special-case xenheap pages" option.

Ok, I see you've committed a patch to that effect while I was away.

Paul

Quoted 2 lines

-George

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

#4 lore raw
Quoted 6 lines · Paul Durrant
Quoted 4 lines

-----Original Message-----
From: George Dunlap <[email protected]>
Not terrible for such a wonkily-behaving guest; but I think I'd rather go with the "special-case xenheap pages" option.

Ok, I see you've committed a patch to that effect while I was away.

Yes; Andy wanted to get tests passing, and asked me to submit a change along the lines of what I was proposing.

-George

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