2015-12-09 10:02:05

by Pan Xinhui

[permalink] [raw]
Subject: [PATCH] powerpc/nvram: Fix a memory leak in err path


If kmemdup fails, We need kfree *buff* first then return -ENOMEM.
Otherwise there is a memory leak.

Signed-off-by: Pan Xinhui <[email protected]>
---
arch/powerpc/kernel/nvram_64.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/powerpc/kernel/nvram_64.c b/arch/powerpc/kernel/nvram_64.c
index 32e2652..21a278b7 100644
--- a/arch/powerpc/kernel/nvram_64.c
+++ b/arch/powerpc/kernel/nvram_64.c
@@ -542,9 +542,9 @@ static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
time->tv_nsec = 0;
}
*buf = kmemdup(buff + hdr_size, length, GFP_KERNEL);
+ kfree(buff);
if (*buf == NULL)
return -ENOMEM;
- kfree(buff);

if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
*compressed = true;
--
2.5.0


2015-12-09 15:21:43

by Nathan Fontenot

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Fix a memory leak in err path

On 12/09/2015 04:00 AM, xinhui wrote:
>
> If kmemdup fails, We need kfree *buff* first then return -ENOMEM.
> Otherwise there is a memory leak.
>
> Signed-off-by: Pan Xinhui <[email protected]>

Reviewed-by: Nathan Fontenot <[email protected]>

> ---
> arch/powerpc/kernel/nvram_64.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/powerpc/kernel/nvram_64.c b/arch/powerpc/kernel/nvram_64.c
> index 32e2652..21a278b7 100644
> --- a/arch/powerpc/kernel/nvram_64.c
> +++ b/arch/powerpc/kernel/nvram_64.c
> @@ -542,9 +542,9 @@ static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
> time->tv_nsec = 0;
> }
> *buf = kmemdup(buff + hdr_size, length, GFP_KERNEL);
> + kfree(buff);
> if (*buf == NULL)
> return -ENOMEM;
> - kfree(buff);
>
> if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
> *compressed = true;
>

2015-12-09 23:52:21

by Pan Xinhui

[permalink] [raw]
Subject: Re: [PATCH] powerpc/nvram: Fix a memory leak in err path


On 2015/12/9 23:21, Nathan Fontenot wrote:
> On 12/09/2015 04:00 AM, xinhui wrote:
>>
>> If kmemdup fails, We need kfree *buff* first then return -ENOMEM.
>> Otherwise there is a memory leak.
>>
>> Signed-off-by: Pan Xinhui <[email protected]>
>
> Reviewed-by: Nathan Fontenot <[email protected]>
>

Hi Nathan,
thank you for doing that :)

thanks
xinhui

>> ---
>> arch/powerpc/kernel/nvram_64.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/arch/powerpc/kernel/nvram_64.c b/arch/powerpc/kernel/nvram_64.c
>> index 32e2652..21a278b7 100644
>> --- a/arch/powerpc/kernel/nvram_64.c
>> +++ b/arch/powerpc/kernel/nvram_64.c
>> @@ -542,9 +542,9 @@ static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
>> time->tv_nsec = 0;
>> }
>> *buf = kmemdup(buff + hdr_size, length, GFP_KERNEL);
>> + kfree(buff);
>> if (*buf == NULL)
>> return -ENOMEM;
>> - kfree(buff);
>>
>> if (err_type == ERR_TYPE_KERNEL_PANIC_GZ)
>> *compressed = true;
>>