Skip to contentSkip to conversation

kexec: fix memory leak in function kimage_normal_alloc

PATCH In review Zhang Yanfei 4 participants 4 messages Feb 22, 2013 – Feb 23, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kexec: fix memory leak in function kimage_normal_alloc

If kimage_normal_alloc() fails to alloc pages for image->swap_page, it should call kimage_free_page_list() to free allocated pages in image->control_pages list before it frees image.

Cc "Eric W. Biederman" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Sasha Levin <[email protected]>
---
 kernel/kexec.c |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)
kernel/kexec.c +10 -8
index 5e4bd78..a57face 100644
@@ -223,6 +223,8 @@ out:
 
 }
 
+static void kimage_free_page_list(struct list_head *list);
+
 static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
 				unsigned long nr_segments,
 				struct kexec_segment __user *segments)
@@ -248,22 +250,22 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
 					   get_order(KEXEC_CONTROL_PAGE_SIZE));
 	if (!image->control_code_page) {
 		printk(KERN_ERR "Could not allocate control_code_buffer\n");
-		goto out;
+		goto out_free;
 	}
 
 	image->swap_page = kimage_alloc_control_pages(image, 0);
 	if (!image->swap_page) {
 		printk(KERN_ERR "Could not allocate swap buffer\n");
-		goto out;
+		goto out_free;
 	}
 
-	result = 0;
- out:
-	if (result == 0)
-		*rimage = image;
-	else
-		kfree(image);
+	*rimage = image;
+	return 0;
 
+out_free:
+	kimage_free_page_list(&image->control_pages);
+	kfree(image);
+out:
 	return result;
 }
 
Signature · 1 line
1.7.1
maintainer
#1 lore raw
Quoted 37 lines · On Fri, 22 Feb 2013 12:36:13 +0800 Zhang Yanfei <[email protected]>…

If kimage_normal_alloc() fails to alloc pages for image->swap_page, it should call kimage_free_page_list() to free allocated pages in image->control_pages list before it frees image.

...

kernel/kexec.c +4 -7
@@ -223,6 +223,8 @@ out:
 
 }
 
+static void kimage_free_page_list(struct list_head *list);
+
 static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
 				unsigned long nr_segments,
 				struct kexec_segment __user *segments)
@@ -248,22 +250,22 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
 					   get_order(KEXEC_CONTROL_PAGE_SIZE));
 	if (!image->control_code_page) {
 		printk(KERN_ERR "Could not allocate control_code_buffer\n");
-		goto out;
+		goto out_free;
 	}
 
 	image->swap_page = kimage_alloc_control_pages(image, 0);
 	if (!image->swap_page) {
 		printk(KERN_ERR "Could not allocate swap buffer\n");
-		goto out;
+		goto out_free;
 	}
 
-	result = 0;
- out:
-	if (result == 0)
-		*rimage = image;
-	else

kernel/kexec.c static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, · line 270

Quoted 10 lines
kernel/kexec.c +6 -1
-		kfree(image);
+	*rimage = image;
+	return 0;
 
+out_free:
+	kimage_free_page_list(&image->control_pages);
+	kfree(image);
+out:
 	return result;
 }

kimage_alloc_normal_control_pages() won't add any pages to the image if one of its allocation attemtps failed. So afaict the first goto out_free' could be just goto out'.

The second `goto out_free' does appear to be needed: it frees the pages allocated by the first call to kimage_alloc_control_pages(). I think. The kimage_alloc_control_pages() handling of image->type is a bit twisty.

Please double-check the logic?

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

#2 lore raw

2013/2/23 Andrew Morton <[email protected]>:

Quoted 44 lines
Quoted 41 lines · On Fri, 22 Feb 2013 12:36:13 +0800 Zhang Yanfei <[email protected]>…

If kimage_normal_alloc() fails to alloc pages for image->swap_page, it should call kimage_free_page_list() to free allocated pages in image->control_pages list before it frees image.

...

kernel/kexec.c +6 -8
@@ -223,6 +223,8 @@ out:
 
 }
 
+static void kimage_free_page_list(struct list_head *list);
+
 static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
                              unsigned long nr_segments,
                              struct kexec_segment __user *segments)
@@ -248,22 +250,22 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
                                         get_order(KEXEC_CONTROL_PAGE_SIZE));
      if (!image->control_code_page) {
              printk(KERN_ERR "Could not allocate control_code_buffer\n");
-             goto out;
+             goto out_free;
      }
 
      image->swap_page = kimage_alloc_control_pages(image, 0);
      if (!image->swap_page) {
              printk(KERN_ERR "Could not allocate swap buffer\n");
-             goto out;
+             goto out_free;
      }
 
-     result = 0;
- out:
-     if (result == 0)
-             *rimage = image;
-     else
-             kfree(image);
+     *rimage = image;
+     return 0;
 
Quoted 10 lines

kernel/kexec.c static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, · line 270

Quoted 6 lines
kernel/kexec.c +4 -0
+out_free:
+     kimage_free_page_list(&image->control_pages);
+     kfree(image);
+out:
      return result;
 }

kimage_alloc_normal_control_pages() won't add any pages to the image if one of its allocation attemtps failed. So afaict the first goto out_free' could be just goto out'.

Nop, the first 'goto out_free' is to free allocated 'image' if the allocation of image->control_code_page failed. If changed to 'goto out', 'image' cannot be freed and leads to memory leak.

Quoted 5 lines

The second `goto out_free' does appear to be needed: it frees the pages allocated by the first call to kimage_alloc_control_pages(). I think. The kimage_alloc_control_pages() handling of image->type is a bit twisty.

Yeah, it could just call kimage_alloc_normal_control_pages directly.

Quoted 7 lines

Please double-check the logic?
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

Reviewed-by on v1 1/1
#3 lore raw
In reply to Zhang Yanfei
Quoted 8 lines · Zhang Yanfei

If kimage_normal_alloc() fails to alloc pages for image->swap_page, it should call kimage_free_page_list() to free allocated pages in image->control_pages list before it frees image.

Cc "Eric W. Biederman" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Sasha Levin <[email protected]>
Quoted 56 lines
---
 kernel/kexec.c |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)
kernel/kexec.c +10 -8
index 5e4bd78..a57face 100644
@@ -223,6 +223,8 @@ out:
 
 }
 
+static void kimage_free_page_list(struct list_head *list);
+
 static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
 				unsigned long nr_segments,
 				struct kexec_segment __user *segments)
@@ -248,22 +250,22 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
 					   get_order(KEXEC_CONTROL_PAGE_SIZE));
 	if (!image->control_code_page) {
 		printk(KERN_ERR "Could not allocate control_code_buffer\n");
-		goto out;
+		goto out_free;
 	}
 
 	image->swap_page = kimage_alloc_control_pages(image, 0);
 	if (!image->swap_page) {
 		printk(KERN_ERR "Could not allocate swap buffer\n");
-		goto out;
+		goto out_free;
 	}
 
-	result = 0;
- out:
-	if (result == 0)
-		*rimage = image;
-	else
-		kfree(image);
+	*rimage = image;
+	return 0;
 
+out_free:
+	kimage_free_page_list(&image->control_pages);
+	kfree(image);
+out:
 	return result;
 }
 
Signature · 6 lines
1.7.1

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec