[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen staging-4.19] x86/mm: take an extra page reference in xenmem_add_to_physmap_one()



commit bcdbbebaf7919d40f109acf902d8db713d5f6202
Author:     Roger Pau Monné <roger@xxxxxxxxxxxxxx>
AuthorDate: Thu Nov 26 17:58:28 2026 +0100
Commit:     Jan Beulich <jbeulich@xxxxxxxx>
CommitDate: Thu Nov 26 17:58:28 2026 +0100

    x86/mm: take an extra page reference in xenmem_add_to_physmap_one()
    
    xenmem_add_to_physmap_one() holds the domain's p2m lock when calling
    guest_remove_page(), which inhibits the p2m TLB flush when
    guest_remove_page() releases the gfn.  However xenmem_add_to_physmap_one()
    does not hold a reference to the removed page, and so the page is freed
    when guest_remove_page() calls put_page() on it.  This is problematic, as
    the page is freed before the p2m TLB is flushed.
    
    Modify xenmem_add_to_physmap_one() so it takes a reference to the page to
    be removed, and make sure that reference is only dropped after the p2m lock
    has been released.  Note the GFN to be removed needs to be unshared before
    the reference is taken, as otherwise the call to get_page() would fail if
    the entry is shared.
    
    Also add an ASSERT() to ensure the function is called without the p2m being
    already locked, so that we ensure the last put_gfn() in the function
    performs a p2m flush if needed.
    
    This is XSA-535 / CVE-2026-119782
    
    Signed-off-by: Roger Pau Monné <roger@xxxxxxxxxxxxxx>
    Acked-by: Jan Beulich <jbeulich@xxxxxxxx>
    master commit: a644c78e9740cd69645d0ccc08f86f3c9297ce7d
    master date: 2026-11-26 17:26:10 +0100
---
 xen/arch/x86/mm/p2m.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c
index 29f5c7c279..cbc98eef42 100644
--- a/xen/arch/x86/mm/p2m.c
+++ b/xen/arch/x86/mm/p2m.c
@@ -1969,13 +1969,15 @@ int xenmem_add_to_physmap_one(
     unsigned long idx,
     gfn_t gfn)
 {
-    struct page_info *page = NULL;
+    struct page_info *page = NULL, *prev_page = NULL;
     unsigned long gmfn = 0 /* gcc ... */, old_gfn;
     mfn_t prev_mfn;
     int rc = 0;
     mfn_t mfn = INVALID_MFN;
     p2m_type_t p2mt;
 
+    ASSERT(!p2m_locked_by_me(p2m_get_hostp2m(d)));
+
     switch ( space )
     {
     case XENMAPSPACE_shared_info:
@@ -2031,8 +2033,15 @@ int xenmem_add_to_physmap_one(
      * general, for pages which can subsequently be freed this imperative:
      * Upon freeing we wouldn't be able to find other mappings in the P2M
      * (unless we did a brute force search).
+     *
+     * FIXME: remove a shared p2m entry directly, without unsharing first.
      */
-    prev_mfn = get_gfn(d, gfn_x(gfn), &p2mt);
+    prev_mfn = get_gfn_unshare(d, gfn_x(gfn), &p2mt);
+    if ( p2m_is_shared(p2mt) )
+    {
+        rc = -ENOMEM;
+        goto put_all;
+    }
 
     /* XENMAPSPACE_gmfn: Check if the MFN is associated with another GFN. */
     old_gfn = get_gpfn_from_mfn(mfn_x(mfn));
@@ -2052,8 +2061,17 @@ int xenmem_add_to_physmap_one(
             /* Special pages are simply unhooked from this phys slot. */
             rc = p2m_remove_page(d, gfn, prev_mfn, PAGE_ORDER_4K);
         else if ( !mfn_eq(mfn, prev_mfn) )
+        {
+            prev_page = mfn_to_page(prev_mfn);
+            if ( !get_page(prev_page, d) )
+            {
+                prev_page = NULL;
+                rc = -ENXIO;
+                goto put_all;
+            }
             /* Normal domain memory is freed, to avoid leaking memory. */
             rc = guest_remove_page(d, gfn_x(gfn));
+        }
     }
 
     /* Unmap from old location, if any. */
@@ -2095,6 +2113,9 @@ int xenmem_add_to_physmap_one(
     if ( page )
         put_page(page);
 
+    if ( prev_page )
+        put_page(prev_page);
+
     return rc;
 }
 
--
generated by git-patchbot for /home/xen/git/xen.git#staging-4.19



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.