Skip to contentSkip to conversation

libxc: fix memory leak in migration v2

PATCH In review Wei Liu 3 participants 3 messages Jul 26, 2015 – Jul 27, 2015

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

PATCH v2 1/1 libxc: fix memory leak in migration v2

Originally there was only one counter to keep track of pages. It was used erroneously to keep track of how many pages were mapped and how many pages needed to be sent. In the end munmap(2) always had 0 as the length argument, which resulted in leaking the mapping.

This problem was discovered on 32bit toolstack because 32bit applications have notably smaller address space. In fact this bug affects 64bit toolstack too.

Use a separate counter to keep track of the number of mapped pages to solve this problem.

---
Cc: Andrew Cooper <[email protected]>
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>

v2:
1. Use nr_pages_mapped to produce smaller patch.
2. Fix typos in commit message.

---
 tools/libxc/xc_sr_save.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/libxc/xc_sr_save.c +3 -2
index d63b783..1b6be2a 100644
@@ -84,7 +84,7 @@ static int write_batch(struct xc_sr_context *ctx)
     void **guest_data = NULL;
     void **local_pages = NULL;
     int *errors = NULL, rc = -1;
-    unsigned i, p, nr_pages = 0;
+    unsigned i, p, nr_pages = 0, nr_pages_mapped = 0;
     unsigned nr_pfns = ctx->save.nr_batch_pfns;
     void *page, *orig_page;
     uint64_t *rec_pfns = NULL;
@@ -160,6 +160,7 @@ static int write_batch(struct xc_sr_context *ctx)
             PERROR("Failed to map guest pages");
             goto err;
         }
+        nr_pages_mapped = nr_pages;
 
         for ( i = 0, p = 0; i < nr_pfns; ++i )
         {
@@ -262,7 +263,7 @@ static int write_batch(struct xc_sr_context *ctx)
  err:
     free(rec_pfns);
     if ( guest_mapping )
-        munmap(guest_mapping, nr_pages * PAGE_SIZE);
+        munmap(guest_mapping, nr_pages_mapped * PAGE_SIZE);
     for ( i = 0; local_pages && i < nr_pfns; ++i )
         free(local_pages[i]);
     free(iov);
Signature · 1 line
2.1.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Wei Liu

Originally there was only one counter to keep track of pages. It was used erroneously to keep track of how many pages were mapped and how many pages needed to be sent. In the end munmap(2) always had 0 as the length argument, which resulted in leaking the mapping.

This problem was discovered on 32bit toolstack because 32bit applications have notably smaller address space. In fact this bug affects 64bit toolstack too.

Quoted 6 lines

Use a separate counter to keep track of the number of mapped pages to solve this problem.

---
Cc: Andrew Cooper <[email protected]>

Quoted 40 lines
Cc Ian Campbell <[email protected]>
Cc Ian Jackson <[email protected]>

v2:
1. Use nr_pages_mapped to produce smaller patch.
2. Fix typos in commit message.

---
 tools/libxc/xc_sr_save.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/libxc/xc_sr_save.c +3 -2
index d63b783..1b6be2a 100644
@@ -84,7 +84,7 @@ static int write_batch(struct xc_sr_context *ctx)
     void **guest_data = NULL;
     void **local_pages = NULL;
     int *errors = NULL, rc = -1;
-    unsigned i, p, nr_pages = 0;
+    unsigned i, p, nr_pages = 0, nr_pages_mapped = 0;
     unsigned nr_pfns = ctx->save.nr_batch_pfns;
     void *page, *orig_page;
     uint64_t *rec_pfns = NULL;
@@ -160,6 +160,7 @@ static int write_batch(struct xc_sr_context *ctx)
             PERROR("Failed to map guest pages");
             goto err;
         }
+        nr_pages_mapped = nr_pages;
 
         for ( i = 0, p = 0; i < nr_pfns; ++i )
         {
@@ -262,7 +263,7 @@ static int write_batch(struct xc_sr_context *ctx)
  err:
     free(rec_pfns);
     if ( guest_mapping )
-        munmap(guest_mapping, nr_pages * PAGE_SIZE);
+        munmap(guest_mapping, nr_pages_mapped * PAGE_SIZE);
     for ( i = 0; local_pages && i < nr_pfns; ++i )
         free(local_pages[i]);
     free(iov);
#2 lore raw
Quoted 15 lines · Andrew Cooper
Quoted 1 line · Wei Liu

Originally there was only one counter to keep track of pages. It

was

Quoted 1 line

used erroneously to keep track of how many pages were mapped and

how

Quoted 1 line

many pages needed to be sent. In the end munmap(2) always had 0 as

the

Quoted 3 lines

length argument, which resulted in leaking the mapping.

This problem was discovered on 32bit toolstack because 32bit

applications

Quoted 4 lines

have notably smaller address space. In fact this bug affects 64bit toolstack too.

Use a separate counter to keep track of the number of mapped pages

Quoted 8 lines

to

Quoted 5 lines

solve this problem.

---
Cc: Andrew Cooper <[email protected]>

acked and applied, thanks all.