kexec: prevent double free on image allocation failure
Subsystems (3)
PATCH 1/1 kexec: prevent double free on image allocation failure
If kimage_normal_alloc() fails to initialize an allocated kimage, it will free the image but would still set 'rimage', as a result kexec_load will try to free it again.
This would explode as part of the freeing process is accessing internal members which point to uninitialized memory.
--- kernel/kexec.c | 2 -- 1 file changed, 2 deletions(-)
kernel/kexec.c +0 -2
@@ -242,8 +242,6 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,if (result) goto out; - *rimage = image; -/* * Find a location for the control code buffer, and add it * the vector of segments so that it's pages will also be
Signature · 7 lines
1.8.1.2 _______________________________________________ kexec mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kexec
Quoted 6 lines · Sasha Levin
If
kimage_normal_alloc()fails to initialize an allocated kimage, it will free the image but would still set 'rimage', as a resultkexec_loadwill try to free it again.This would explode as part of the freeing process is accessing internal members which point to uninitialized memory.
Agreed.
I don't think that failure path has ever actually been exercised.
The code is wrong, and it is worth fixing.
Andrew I do you think you could queue this up? I don't have a handy tree.
Quoted 18 lines
Signed-off-by Sasha Levin <[email protected]>--- kernel/kexec.c | 2 -- 1 file changed, 2 deletions(-)kernel/kexec.c +0
-2@@ -242,8 +242,6 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,if (result) goto out; - *rimage = image; -/* * Find a location for the control code buffer, and add it * the vector of segments so that it's pages will also be
Quoted 6 lines · On 02/21/2013 08:55 PM, [email protected] wrote:
Quoted 4 lines · Sasha Levin
If
kimage_normal_alloc()fails to initialize an allocated kimage, it will free the image but would still set 'rimage', as a resultkexec_loadwill try to free it again.
Quoted 6 lines
Quoted 2 lines
This would explode as part of the freeing process is accessing internal members which point to uninitialized memory.
Agreed.
I don't think that failure path has ever actually been exercised.
trinity is actually quite good at hitting that, which is how I discovered it:
[ 418.138251] Could not allocate control_code_buffer [ 418.143739] general protection fault: 0000 [#1] PREEMPT SMP DEBUG_PAGEALLOC [ 418.147131] Dumping ftrace buffer: [ 418.147901] (ftrace buffer empty) [ 418.148697] Modules linked in: [ 418.153440] CPU 1 [ 418.153440] Pid: 18098, comm: trinity Tainted: G W 3.8.0-next-20130220-sasha-00037-gc07b3b2-dirty #7 [ 418.153440] RIP: 0010:[<ffffffff8119b5a6>] [<ffffffff8119b5a6>] kimage_free_page_list+0x16/0x50 [ 418.153440] RSP: 0018:ffff88009bfade78 EFLAGS: 00010292 [ 418.153440] RAX: 0000000000180004 RBX: 0000000000000002 RCX: 0000000000000000 [ 418.153440] RDX: ffff88009c1a0000 RSI: 0000000000000001 RDI: 6b6b6b6b6b6b6b6b [ 418.153440] RBP: ffff88009bfade98 R08: 0000000000002782 R09: 0000000000000000 [ 418.153440] R10: 0000000000000000 R11: 0000000000000000 R12: ffff88009c6cb4d0 [ 418.153440] R13: ffff88009c6cb720 R14: ffff88009c6cb4d0 R15: 00000000000000f6 [ 418.153440] FS: 00007fb7eb95b700(0000) GS:ffff8800bb800000(0000) knlGS:0000000000000000 [ 418.153440] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 418.153440] CR2: 00000000004808e0 CR3: 000000009eaaa000 CR4: 00000000000406e0 [ 418.153440] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 418.153440] DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400 [ 418.153440] Process trinity (pid: 18098, threadinfo ffff88009bfac000, task ffff88009c1a0000) [ 418.153440] Stack: [ 418.153440] ffffffff8546e948 0000000000000002 ffff88009c6cb4d0 0000000000000000 [ 418.153440] ffff88009bfaded8 ffffffff8119b60f 0000000000000002 0000000000000002 [ 418.153440] ffff88009c6cb4d0 ffff88009c6cb4d0 fffffffffffffff4 00000000000000f6 [ 418.153440] Call Trace: [ 418.153440] [<ffffffff8119b60f>] kimage_free+0x2f/0x100 [ 418.153440] [<ffffffff8119c563>] sys_kexec_load+0x593/0x660 [ 418.153440] [<ffffffff8118363d>] ? trace_hardirqs_on+0xd/0x10 [ 418.153440] [<ffffffff83dab7d8>] tracesys+0xe1/0xe6 [ 418.153440] Code: c1 ef 0c 55 48 c1 e7 06 48 89 e5 48 01 c7 e8 82 ff ff ff 5d c3 55 48 89 e5 41 55 49 89 fd 41 54 53 48 83 ec 08 48 8b 3f 49 39 fd <48> 8b 1f 75 08 eb 22 0f 1f 00 48 89 d3 4c 8d 67 e0 e8 54 a6 8a [ 418.153440] RIP [<ffffffff8119b5a6>] kimage_free_page_list+0x16/0x50 [ 418.153440] RSP <ffff88009bfade78> [ 418.219646] ---[ end trace 0adb1d6b71fefb29 ]---
Thanks,
Sasha
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Quoted 9 lines · Eric W. Biederman
Quoted 6 lines · Sasha Levin
If
kimage_normal_alloc()fails to initialize an allocated kimage, it will free the image but would still set 'rimage', as a resultkexec_loadwill try to free it again.This would explode as part of the freeing process is accessing internal members which point to uninitialized memory.
Quoted 7 lines
Agreed.
I don't think that failure path has ever actually been exercised.
The code is wrong, and it is worth fixing.
Andrew I do you think you could queue this up? I don't have a handy tree.
I still found another malloc/free problem in this function. So I update the patch.
---------------------
Quoted 1 line
From
1fb76a35e4109e1435f55048c20ea58622e7f87bMon Sep 17 00:00:00 2001
From: Zhang Yanfei <[email protected]>
Date: Fri, 22 Feb 2013 10:34:02 +0800
Subject: [PATCH] kexec: fix allocation problems in function kimage_normal_alloc
The function kimage_normal_alloc() has 2 allocation problems that may cause failures:
1. If kimage_normal_alloc() fails to initialize an allocated kimage, it will free the image but would still set 'rimage', as a result kexec_load will try to free it again.
This would explode as part of the freeing process is accessing internal members which point to uninitialized memory.
2. 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 | 10 ++++++---- 1 files changed, 6 insertions(+), 4 deletions(-)
kernel/kexec.c +6 -4
@@ -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)
@@ -236,8 +238,6 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,if (result) goto out; - *rimage = image; -/* * Find a location for the control code buffer, and add it * the vector of segments so that it's pages will also be
@@ -259,10 +259,12 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, result =0 ; out: -if (result ==0 ) +if (result ==0 ) { *rimage = image; -else + }else { +kimage_free_page_list (&image->control_pages);kfree (image); + }return result; }
Signature · 1 line
1.7.1
Quoted 75 lines · Zhang Yanfei
Quoted 9 lines · Eric W. Biederman
Quoted 6 lines · Sasha Levin
If
kimage_normal_alloc()fails to initialize an allocated kimage, it will free the image but would still set 'rimage', as a resultkexec_loadwill try to free it again.This would explode as part of the freeing process is accessing internal members which point to uninitialized memory.
Quoted 7 lines
Agreed.
I don't think that failure path has ever actually been exercised.
The code is wrong, and it is worth fixing.
Andrew I do you think you could queue this up? I don't have a handy tree.
I still found another malloc/free problem in this function. So I update the patch.
---------------------
From
1fb76a35e4109e1435f55048c20ea58622e7f87bMon Sep 17 00:00:00 2001
From: Zhang Yanfei <[email protected]>
Date: Fri, 22 Feb 2013 10:34:02 +0800
Subject: [PATCH] kexec: fix allocation problems in functionkimage_normal_allocThe function
kimage_normal_alloc()has 2 allocation problems that may cause failures:1. If
kimage_normal_alloc()fails to initialize an allocated kimage, it will free the image but would still set 'rimage', as a resultkexec_loadwill try to free it again.This would explode as part of the freeing process is accessing internal members which point to uninitialized memory.
2. If
kimage_normal_alloc()fails to alloc pages forimage->swap_page, it should callkimage_free_page_list()to free allocated pages inimage->control_pageslist before it frees image.Signed-off-by Sasha Levin <[email protected]>Signed-off-by Zhang Yanfei <[email protected]>--- kernel/kexec.c | 10 ++++++---- 1 files changed, 6 insertions(+), 4 deletions(-)kernel/kexec.c +2
-3@@ -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)@@ -236,8 +238,6 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,if (result) goto out; - *rimage = image; -/* * Find a location for the control code buffer, and add it * the vector of segments so that it's pages will also be @@ -259,10 +259,12 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, result =0 ; out: -if (result ==0 )
kernel/kexec.c static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, · line 270
Quoted 10 lines
kernel/kexec.c +4
-1+if (result ==0 ) { *rimage = image; - else + }else { +kimage_free_page_list (&image->control_pages);kfree (image); + }return result; }
And if do_kimage_alloc() fails instead of kimage_alloc_control_pages() you will NULL deref 'image', so now instead of leaking pages the kernel will explode.
Either way, this issue you've pointed out should be fixed in a separate patch.
Thanks,
Sasha
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Quoted 84 lines · Sasha Levin
Quoted 83 lines · Zhang Yanfei
Quoted 9 lines · Eric W. Biederman
Quoted 6 lines · Sasha Levin
If
kimage_normal_alloc()fails to initialize an allocated kimage, it will free the image but would still set 'rimage', as a resultkexec_loadwill try to free it again.This would explode as part of the freeing process is accessing internal members which point to uninitialized memory.
Quoted 7 lines
Agreed.
I don't think that failure path has ever actually been exercised.
The code is wrong, and it is worth fixing.
Andrew I do you think you could queue this up? I don't have a handy tree.
I still found another malloc/free problem in this function. So I update the patch.
---------------------
From
1fb76a35e4109e1435f55048c20ea58622e7f87bMon Sep 17 00:00:00 2001
From: Zhang Yanfei <[email protected]>
Date: Fri, 22 Feb 2013 10:34:02 +0800
Subject: [PATCH] kexec: fix allocation problems in functionkimage_normal_allocThe function
kimage_normal_alloc()has 2 allocation problems that may cause failures:1. If
kimage_normal_alloc()fails to initialize an allocated kimage, it will free the image but would still set 'rimage', as a resultkexec_loadwill try to free it again.This would explode as part of the freeing process is accessing internal members which point to uninitialized memory.
2. If
kimage_normal_alloc()fails to alloc pages forimage->swap_page, it should callkimage_free_page_list()to free allocated pages inimage->control_pageslist before it frees image.Signed-off-by Sasha Levin <[email protected]>Signed-off-by Zhang Yanfei <[email protected]>--- kernel/kexec.c | 10 ++++++---- 1 files changed, 6 insertions(+), 4 deletions(-)kernel/kexec.c +6
-4@@ -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)@@ -236,8 +238,6 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry,if (result) goto out; - *rimage = image; -/* * Find a location for the control code buffer, and add it * the vector of segments so that it's pages will also be @@ -259,10 +259,12 @@ static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, result =0 ; out: -if (result ==0 ) +if (result ==0 ) { *rimage = image; -else + }else { +kimage_free_page_list (&image->control_pages);kfree (image); + }
Quoted 6 lines
kernel/kexec.c static int kimage_normal_alloc(struct kimage **rimage, unsigned long entry, · line 270
Quoted 2 lines
kernel/kexec.c +0
-0return result; }And if
do_kimage_alloc()fails instead ofkimage_alloc_control_pages()you will NULL deref 'image', so now instead of leaking pages the kernel will explode.
Oh, I missed this.
Quoted 5 lines
Either way, this issue you've pointed out should be fixed in a separate patch.
OK,I will send another patch.
Thanks
Zhang