Skip to content

Commit b92e7e0

Browse files
Zhang Yanfeitorvalds
authored andcommitted
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. Signed-off-by: Zhang Yanfei <[email protected]> Cc: "Eric W. Biederman" <[email protected]> Cc: Sasha Levin <[email protected]> Reviewed-by: Simon Horman <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent fe88f2e commit b92e7e0

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

kernel/kexec.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ static int do_kimage_alloc(struct kimage **rimage, unsigned long entry,
229229

230230
}
231231

232+
static void kimage_free_page_list(struct list_head *list);
233+
232234
static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
233235
unsigned long nr_segments,
234236
struct kexec_segment __user *segments)
@@ -252,22 +254,22 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,
252254
get_order(KEXEC_CONTROL_PAGE_SIZE));
253255
if (!image->control_code_page) {
254256
printk(KERN_ERR "Could not allocate control_code_buffer\n");
255-
goto out;
257+
goto out_free;
256258
}
257259

258260
image->swap_page = kimage_alloc_control_pages(image, 0);
259261
if (!image->swap_page) {
260262
printk(KERN_ERR "Could not allocate swap buffer\n");
261-
goto out;
263+
goto out_free;
262264
}
263265

264-
result = 0;
265-
out:
266-
if (result == 0)
267-
*rimage = image;
268-
else
269-
kfree(image);
266+
*rimage = image;
267+
return 0;
270268

269+
out_free:
270+
kimage_free_page_list(&image->control_pages);
271+
kfree(image);
272+
out:
271273
return result;
272274
}
273275

0 commit comments

Comments
 (0)