From: Sasha Levin <sasha.levin@oracle.com>
To: Zhang Yanfei <zhangyanfei@cn.fujitsu.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
kexec@lists.infradead.org,
"Eric W. Biederman" <ebiederm@xmission.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] kexec: prevent double free on image allocation failure
Date: Thu, 21 Feb 2013 22:41:38 -0500 [thread overview]
Message-ID: <5126E8F2.70502@oracle.com> (raw)
In-Reply-To: <5126DC06.5020005@cn.fujitsu.com>
On 02/21/2013 09:46 PM, Zhang Yanfei wrote:
> 于 2013年02月22日 09:55, Eric W. Biederman 写道:
>> Sasha Levin <sasha.levin@oracle.com> writes:
>>
>>> 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.
>>
>> 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 1fb76a35e4109e1435f55048c20ea58622e7f87b Mon Sep 17 00:00:00 2001
> From: Zhang Yanfei <zhangyanfei@cn.fujitsu.com>
> 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.
>
> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
> Signed-off-by: Zhang Yanfei <zhangyanfei@cn.fujitsu.com>
> ---
> kernel/kexec.c | 10 ++++++----
> 1 files changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/kernel/kexec.c b/kernel/kexec.c
> index 5e4bd78..f219357 100644
> --- a/kernel/kexec.c
> +++ b/kernel/kexec.c
> @@ -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;
> }
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
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec
next prev parent reply other threads:[~2013-02-22 3:42 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-02-22 1:26 [PATCH] kexec: prevent double free on image allocation failure Sasha Levin
2013-02-22 1:55 ` Eric W. Biederman
2013-02-22 2:33 ` Sasha Levin
2013-02-22 2:46 ` Zhang Yanfei
2013-02-22 3:41 ` Sasha Levin [this message]
2013-02-22 3:58 ` Zhang Yanfei
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5126E8F2.70502@oracle.com \
--to=sasha.levin@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=ebiederm@xmission.com \
--cc=kexec@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=zhangyanfei@cn.fujitsu.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox