Skip to contentSkip to conversation

xen/arm: Fix memory leak in xenmem_add_to_physmap_one

PATCH In review Michal Orzel 3 participants 6 messages Feb 5 – Feb 23

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: Fix memory leak in xenmem_add_to_physmap_one

When a guest maps pages via XENMEM_add_to_physmap to a GFN that already has an existing mapping, the old page at that GFN was not being removed, causing a memory leak. This affects all mapping spaces including XENMAPSPACE_shared_info, XENMAPSPACE_grant_table, and XENMAPSPACE_gmfn_foreign. The memory would be reclaimed on domain destruction.

Add logic to remove the previously mapped page before creating the new mapping, matching the x86 implementation approach.

Additionally, skip removal if the same MFN is being remapped.

---
I'm not sure where to point the Fixes tag to.

---
 xen/arch/arm/mm.c | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)
xen/arch/arm/mm.c +29 -3
index 6df8b616e464..b9f1a493dcd7 100644
@@ -166,10 +166,11 @@ int xenmem_add_to_physmap_one(
     unsigned long idx,
     gfn_t gfn)
 {
-    mfn_t mfn = INVALID_MFN;
+    mfn_t mfn = INVALID_MFN, mfn_old;
     int rc;
     p2m_type_t t;
     struct page_info *page = NULL;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
     switch ( space )
     {
@@ -244,6 +245,33 @@ int xenmem_add_to_physmap_one(
         return -ENOSYS;
     }
 
+    /*
+     * Remove previously mapped page if it was present, to avoid leaking
+     * memory.
+     */
+    mfn_old = gfn_to_mfn(d, gfn);
+
+    if ( mfn_valid(mfn_old) )
+    {
+        if ( is_special_page(mfn_to_page(mfn_old)) )
+        {
+            /* Just unmap, don't free */
+            p2m_write_lock(p2m);
+            rc = p2m_set_entry(p2m, gfn, 1, INVALID_MFN,
+                               p2m_invalid, p2m->default_access);
+            p2m_write_unlock(p2m);
+            if ( rc )
+                return rc;
+        }
+        else if ( !mfn_eq(mfn, mfn_old) )
+        {
+            /* Normal domain memory is freed, to avoid leaking memory */
+            rc = guest_remove_page(d, gfn_x(gfn));
+            if ( rc )
+                return rc;
+        }
+    }
+
     /*
      * Map at new location. Here we need to map xenheap RAM page differently
      * because we need to store the valid GFN and make sure that nothing was
@@ -255,8 +283,6 @@ int xenmem_add_to_physmap_one(
         rc = guest_physmap_add_entry(d, gfn, mfn, 0, t);
     else
     {
-        struct p2m_domain *p2m = p2m_get_hostp2m(d);
-
         p2m_write_lock(p2m);
         if ( gfn_eq(page_get_xenheap_gfn(mfn_to_page(mfn)), INVALID_GFN) )
         {
Signature · 1 line
2.43.0
#1 lore raw
Quoted 56 lines · Michal Orzel

When a guest maps pages via XENMEM_add_to_physmap to a GFN that already has an existing mapping, the old page at that GFN was not being removed, causing a memory leak. This affects all mapping spaces including XENMAPSPACE_shared_info, XENMAPSPACE_grant_table, and XENMAPSPACE_gmfn_foreign. The memory would be reclaimed on domain destruction.

Add logic to remove the previously mapped page before creating the new mapping, matching the x86 implementation approach.

Additionally, skip removal if the same MFN is being remapped.

---
I'm not sure where to point the Fixes tag to.

---
 xen/arch/arm/mm.c | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)
xen/arch/arm/mm.c +18 -1
index 6df8b616e464..b9f1a493dcd7 100644
@@ -166,10 +166,11 @@ int xenmem_add_to_physmap_one(
     unsigned long idx,
     gfn_t gfn)
 {
-    mfn_t mfn = INVALID_MFN;
+    mfn_t mfn = INVALID_MFN, mfn_old;
     int rc;
     p2m_type_t t;
     struct page_info *page = NULL;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
     switch ( space )
     {
@@ -244,6 +245,33 @@ int xenmem_add_to_physmap_one(
         return -ENOSYS;
     }
 
+    /*
+     * Remove previously mapped page if it was present, to avoid leaking
+     * memory.
+     */
+    mfn_old = gfn_to_mfn(d, gfn);
+
+    if ( mfn_valid(mfn_old) )
+    {
+        if ( is_special_page(mfn_to_page(mfn_old)) )
+        {
+            /* Just unmap, don't free */
+            p2m_write_lock(p2m);
+            rc = p2m_set_entry(p2m, gfn, 1, INVALID_MFN,
+                               p2m_invalid, p2m->default_access);
+            p2m_write_unlock(p2m);
+            if ( rc )

xen/arch/arm/mm.c int xenmem_add_to_physmap_one( · line 273

Quoted 10 lines
xen/arch/arm/mm.c +10 -0
+                return rc;
+        }
+        else if ( !mfn_eq(mfn, mfn_old) )
+        {
+            /* Normal domain memory is freed, to avoid leaking memory */
+            rc = guest_remove_page(d, gfn_x(gfn));
+            if ( rc )
+                return rc;
+        }
+    }

This new code and what follows below it are not inside a single locked region, hence the cleanup done above may well have been "undone" again by the time the P2M lock is acquired below. That locking may not be apparent in the x86 variant when merely looking at functions used. There's a large comment, though, explaining how we actually (ab)use the simplified locking model (compared to what was once intended, but never carried out).

Further, doesn't P2M entry type influence what needs doing here, including possibly rejecting the request? x86 refuses to replace p2m_mmio_direct entries by this means, but seeing that Arm has XENMAPSPACE_dev_mmio, this case may need handling, but the handling may need to be different from what you do above. (Just to mention: Presumably on Arm it's no different from x86: An MMIO page may or may not pass an mfn_valid() check.)

Jan

#2 lore raw
Quoted 66 lines · Jan Beulich
Quoted 65 lines · Michal Orzel

When a guest maps pages via XENMEM_add_to_physmap to a GFN that already has an existing mapping, the old page at that GFN was not being removed, causing a memory leak. This affects all mapping spaces including XENMAPSPACE_shared_info, XENMAPSPACE_grant_table, and XENMAPSPACE_gmfn_foreign. The memory would be reclaimed on domain destruction.

Add logic to remove the previously mapped page before creating the new mapping, matching the x86 implementation approach.

Additionally, skip removal if the same MFN is being remapped.

---
I'm not sure where to point the Fixes tag to.

---
 xen/arch/arm/mm.c | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)
xen/arch/arm/mm.c +27 -1
index 6df8b616e464..b9f1a493dcd7 100644
@@ -166,10 +166,11 @@ int xenmem_add_to_physmap_one(
     unsigned long idx,
     gfn_t gfn)
 {
-    mfn_t mfn = INVALID_MFN;
+    mfn_t mfn = INVALID_MFN, mfn_old;
     int rc;
     p2m_type_t t;
     struct page_info *page = NULL;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
     switch ( space )
     {
@@ -244,6 +245,33 @@ int xenmem_add_to_physmap_one(
         return -ENOSYS;
     }
 
+    /*
+     * Remove previously mapped page if it was present, to avoid leaking
+     * memory.
+     */
+    mfn_old = gfn_to_mfn(d, gfn);
+
+    if ( mfn_valid(mfn_old) )
+    {
+        if ( is_special_page(mfn_to_page(mfn_old)) )
+        {
+            /* Just unmap, don't free */
+            p2m_write_lock(p2m);
+            rc = p2m_set_entry(p2m, gfn, 1, INVALID_MFN,
+                               p2m_invalid, p2m->default_access);
+            p2m_write_unlock(p2m);
+            if ( rc )
+                return rc;
+        }
+        else if ( !mfn_eq(mfn, mfn_old) )
+        {
+            /* Normal domain memory is freed, to avoid leaking memory */
+            rc = guest_remove_page(d, gfn_x(gfn));
+            if ( rc )
+                return rc;
+        }
Quoted 8 lines

xen/arch/arm/mm.c int xenmem_add_to_physmap_one( · line 273

Quoted 1 line
xen/arch/arm/mm.c +1 -0
+    }

This new code and what follows below it are not inside a single locked region, hence the cleanup done above may well have been "undone" again by the time the P2M lock is acquired below. That locking may not be apparent in the x86 variant when merely looking at functions used. There's a large comment, though, explaining how we actually (ab)use the simplified locking model (compared to what was once intended, but never carried out).

Do you suggest to put the new code and old code in a single locked region?

Quoted 7 lines

Further, doesn't P2M entry type influence what needs doing here, including possibly rejecting the request? x86 refuses to replace p2m_mmio_direct entries by this means, but seeing that Arm has XENMAPSPACE_dev_mmio, this case may need handling, but the handling may need to be different from what you do above. (Just to mention: Presumably on Arm it's no different from x86: An MMIO page may or may not pass an mfn_valid() check.)

I actually had the following in my initial implementation:
p2m_write_lock(p2m);
mfn_old = p2m_get_entry(p2m, gfn, &p2mt_old, NULL, NULL, NULL); if ( p2mt_old == p2m_mmio_direct )

{
    p2m_write_unlock(p2m);
    return -EPERM;
}

but realized this is actually a different issue than the one I want to solve and I don't want to fix two in one go.

~Michal

#3 lore raw
Quoted 77 lines · Michal
Quoted 66 lines · Jan Beulich
Quoted 65 lines · Michal Orzel

When a guest maps pages via XENMEM_add_to_physmap to a GFN that already has an existing mapping, the old page at that GFN was not being removed, causing a memory leak. This affects all mapping spaces including XENMAPSPACE_shared_info, XENMAPSPACE_grant_table, and XENMAPSPACE_gmfn_foreign. The memory would be reclaimed on domain destruction.

Add logic to remove the previously mapped page before creating the new mapping, matching the x86 implementation approach.

Additionally, skip removal if the same MFN is being remapped.

---
I'm not sure where to point the Fixes tag to.

---
 xen/arch/arm/mm.c | 32 +++++++++++++++++++++++++++++---
 1 file changed, 29 insertions(+), 3 deletions(-)
xen/arch/arm/mm.c +27 -1
index 6df8b616e464..b9f1a493dcd7 100644
@@ -166,10 +166,11 @@ int xenmem_add_to_physmap_one(
     unsigned long idx,
     gfn_t gfn)
 {
-    mfn_t mfn = INVALID_MFN;
+    mfn_t mfn = INVALID_MFN, mfn_old;
     int rc;
     p2m_type_t t;
     struct page_info *page = NULL;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
     switch ( space )
     {
@@ -244,6 +245,33 @@ int xenmem_add_to_physmap_one(
         return -ENOSYS;
     }
 
+    /*
+     * Remove previously mapped page if it was present, to avoid leaking
+     * memory.
+     */
+    mfn_old = gfn_to_mfn(d, gfn);
+
+    if ( mfn_valid(mfn_old) )
+    {
+        if ( is_special_page(mfn_to_page(mfn_old)) )
+        {
+            /* Just unmap, don't free */
+            p2m_write_lock(p2m);
+            rc = p2m_set_entry(p2m, gfn, 1, INVALID_MFN,
+                               p2m_invalid, p2m->default_access);
+            p2m_write_unlock(p2m);
+            if ( rc )
+                return rc;
+        }
+        else if ( !mfn_eq(mfn, mfn_old) )
+        {
+            /* Normal domain memory is freed, to avoid leaking memory */
+            rc = guest_remove_page(d, gfn_x(gfn));
+            if ( rc )
+                return rc;
+        }
Quoted 8 lines

xen/arch/arm/mm.c int xenmem_add_to_physmap_one( · line 273

Quoted 1 line
xen/arch/arm/mm.c +1 -0
+    }

This new code and what follows below it are not inside a single locked region, hence the cleanup done above may well have been "undone" again by the time the P2M lock is acquired below. That locking may not be apparent in the x86 variant when merely looking at functions used. There's a large comment, though, explaining how we actually (ab)use the simplified locking model (compared to what was once intended, but never carried out).

Quoted 1 line

Do you suggest to put the new code and old code in a single locked region?

Yes. Which may be difficult on Arm, where the P2M lock isn't recursive.

Quoted 10 lines
Quoted 6 lines

Further, doesn't P2M entry type influence what needs doing here, including possibly rejecting the request? x86 refuses to replace p2m_mmio_direct entries by this means, but seeing that Arm has XENMAPSPACE_dev_mmio, this case may need handling, but the handling may need to be different from what you do above. (Just to mention: Presumably on Arm it's no different from x86: An MMIO page may or may not pass an mfn_valid() check.)

I actually had the following in my initial implementation:
p2m_write_lock(p2m);
mfn_old = p2m_get_entry(p2m, gfn, &p2mt_old, NULL, NULL, NULL); if ( p2mt_old == p2m_mmio_direct )

Quoted 6 lines
{
    p2m_write_unlock(p2m);
    return -EPERM;
}

but realized this is actually a different issue than the one I want to solve and I don't want to fix two in one go.

Hmm. If you indeed want to separate both (and also not have both in a series), I'd then suggest to at least mention that this aspect (and whatever else there may be) is deliberately left out.

Jan

#4 lore raw
In reply to Michal Orzel

Hi Michal,

For the subject title and part of the code, "memory leak" tends to imply the memory is lost forever and therefore can never be recovered. AFAIU, in your case, the memory will be freed when the domain is destroyed. I would suggest to clarify it so it doesn't sound like a security issue.

What about:

"xen/arm: mm: Release the old page reference in xenmem_add_to_physmap_one()"

Quoted 11 lines · Michal Orzel

When a guest maps pages via XENMEM_add_to_physmap to a GFN that already has an existing mapping, the old page at that GFN was not being removed, causing a memory leak. This affects all mapping spaces including XENMAPSPACE_shared_info, XENMAPSPACE_grant_table, and XENMAPSPACE_gmfn_foreign. The memory would be reclaimed on domain destruction.

Add logic to remove the previously mapped page before creating the new mapping, matching the x86 implementation approach.

Additionally, skip removal if the same MFN is being remapped.

Can you explain why we skip the removal but not the insertion in this case?

Quoted 26 lines

---
I'm not sure where to point the Fixes tag to.

---
  xen/arch/arm/mm.c | 32 +++++++++++++++++++++++++++++---
  1 file changed, 29 insertions(+), 3 deletions(-)
xen/arch/arm/mm.c +2 -1
index 6df8b616e464..b9f1a493dcd7 100644
@@ -166,10 +166,11 @@ int xenmem_add_to_physmap_one(
      unsigned long idx,
      gfn_t gfn)
  {
-    mfn_t mfn = INVALID_MFN;
+    mfn_t mfn = INVALID_MFN, mfn_old;
      int rc;
      p2m_type_t t;
      struct page_info *page = NULL;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
      switch ( space )
      {

xen/arch/arm/mm.c int xenmem_add_to_physmap_one( · line 252

Quoted 8 lines
xen/arch/arm/mm.c +5 -0
@@ -244,6 +245,33 @@ int xenmem_add_to_physmap_one(
          return -ENOSYS;
      }
 
+    /*
+     * Remove previously mapped page if it was present, to avoid leaking
+     * memory.
+     */
+    mfn_old = gfn_to_mfn(d, gfn);

I saw Jan mentionned the fact that we have two section with the P2M lock taken. But isn't it in fact 3 sections as gfn_to_mfn(d, gfn) also take a lock?

I am not against the idea of not solving the locking right now. But I think it would at least be good to document it so this doesn't come as a surprise.

xen/arch/arm/mm.c int xenmem_add_to_physmap_one( · line 264

Quoted 12 lines
xen/arch/arm/mm.c +12 -0
+
+    if ( mfn_valid(mfn_old) )
+    {
+        if ( is_special_page(mfn_to_page(mfn_old)) )
+        {
+            /* Just unmap, don't free */
+            p2m_write_lock(p2m);
+            rc = p2m_set_entry(p2m, gfn, 1, INVALID_MFN,
+                               p2m_invalid, p2m->default_access);
+            p2m_write_unlock(p2m);
+            if ( rc )
+                return rc;

For here and the second "return" statement below. Above callers may have taken a reference on the new page. So shouldn't we drop it like this is done at the end of the function?

xen/arch/arm/mm.c int xenmem_add_to_physmap_one( · line 268

Quoted 4 lines
xen/arch/arm/mm.c +4 -0
+        }
+        else if ( !mfn_eq(mfn, mfn_old) )
+        {
+            /* Normal domain memory is freed, to avoid leaking memory */

Based on the thread with Jan, is this statement actually correct? Could we reach this call with an MMIO (or event foreign mapping). In which case, I am guessing we could fail? If so, is this the intended behavior change?

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw
Quoted 10 lines · Julien Grall

Hi Michal,

For the subject title and part of the code, "memory leak" tends to imply the memory is lost forever and therefore can never be recovered. AFAIU, in your case, the memory will be freed when the domain is destroyed. I would suggest to clarify it so it doesn't sound like a security issue.

What about:

"xen/arm: mm: Release the old page reference in xenmem_add_to_physmap_one()"

Reads ok.

Quoted 9 lines · Michal Orzel
Quoted 7 lines

When a guest maps pages via XENMEM_add_to_physmap to a GFN that already has an existing mapping, the old page at that GFN was not being removed, causing a memory leak. This affects all mapping spaces including XENMAPSPACE_shared_info, XENMAPSPACE_grant_table, and XENMAPSPACE_gmfn_foreign. The memory would be reclaimed on domain destruction.

Quoted 6 lines
Quoted 4 lines

Add logic to remove the previously mapped page before creating the new mapping, matching the x86 implementation approach.

Additionally, skip removal if the same MFN is being remapped.

Can you explain why we skip the removal but not the insertion in this case?

Hmm, I must have missed that the type is always fixed. In that case, we can skip the insertion/removal.

Quoted 40 lines
Quoted 27 lines

---
I'm not sure where to point the Fixes tag to.

---
  xen/arch/arm/mm.c | 32 +++++++++++++++++++++++++++++---
  1 file changed, 29 insertions(+), 3 deletions(-)
xen/arch/arm/mm.c +2 -1
index 6df8b616e464..b9f1a493dcd7 100644
@@ -166,10 +166,11 @@ int xenmem_add_to_physmap_one(
      unsigned long idx,
      gfn_t gfn)
  {
-    mfn_t mfn = INVALID_MFN;
+    mfn_t mfn = INVALID_MFN, mfn_old;
      int rc;
      p2m_type_t t;
      struct page_info *page = NULL;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
 
      switch ( space )
      {

xen/arch/arm/mm.c int xenmem_add_to_physmap_one( · line 252

Quoted 8 lines
xen/arch/arm/mm.c +5 -0
@@ -244,6 +245,33 @@ int xenmem_add_to_physmap_one(
          return -ENOSYS;
      }
 
+    /*
+     * Remove previously mapped page if it was present, to avoid leaking
+     * memory.
+     */
+    mfn_old = gfn_to_mfn(d, gfn);

I saw Jan mentionned the fact that we have two section with the P2M lock taken. But isn't it in fact 3 sections as gfn_to_mfn(d, gfn) also take a lock?

Quoted 3 lines

I am not against the idea of not solving the locking right now. But I think it would at least be good to document it so this doesn't come as a surprise.

I'll prepare a v2 with a comment and better explanation.

Quoted 14 lines

xen/arch/arm/mm.c int xenmem_add_to_physmap_one( · line 264

Quoted 13 lines
xen/arch/arm/mm.c +12 -0
+
+    if ( mfn_valid(mfn_old) )
+    {
+        if ( is_special_page(mfn_to_page(mfn_old)) )
+        {
+            /* Just unmap, don't free */
+            p2m_write_lock(p2m);
+            rc = p2m_set_entry(p2m, gfn, 1, INVALID_MFN,
+                               p2m_invalid, p2m->default_access);
+            p2m_write_unlock(p2m);
+            if ( rc )
+                return rc;
Quoted 3 lines

For here and the second "return" statement below. Above callers may have taken a reference on the new page. So shouldn't we drop it like this is done at the end of the function?

Yes, we should.

Quoted 10 lines

xen/arch/arm/mm.c int xenmem_add_to_physmap_one( · line 268

Quoted 5 lines
xen/arch/arm/mm.c +4 -0
+        }
+        else if ( !mfn_eq(mfn, mfn_old) )
+        {
+            /* Normal domain memory is freed, to avoid leaking memory */

Based on the thread with Jan, is this statement actually correct? Could we reach this call with an MMIO (or event foreign mapping). In which case, I am guessing we could fail? If so, is this the intended behavior change?

I wanted to leave MMIO case for a separate change but I can try to fit it all together.

Let's discuss on v2. I'll try to prepare it taking all the remarks into account.

~Michal