mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Stefan Kerkmann <s.kerkmann@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>,
	Sascha Hauer <s.hauer@pengutronix.de>,
	BAREBOX <barebox@lists.infradead.org>
Subject: Re: [PATCH 1/2] of: fdt: fix memory leak in fdt_ensure_space
Date: Thu, 1 Feb 2024 09:34:24 +0100	[thread overview]
Message-ID: <4efd89d7-2bf8-4448-96f2-2fcd91241411@pengutronix.de> (raw)
In-Reply-To: <7ff7301e-1594-40c0-8c39-bbd51ee53b54@pengutronix.de>

Hello Ahmad,

On 31.01.24 18:15, Ahmad Fatoum wrote:
> Hello Stefan,
> 
> On 31.01.24 17:56, Stefan Kerkmann wrote:
>> If the reallocation failed the old memory remains allocated and is never
>> freed, this is fixed by freeing the old memory on error.
>>
>> Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
>> ---
>>   drivers/of/fdt.c | 27 ++++++++++++++++++++-------
>>   1 file changed, 20 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
>> index 5c21bab5de..4f79a6120f 100644
>> --- a/drivers/of/fdt.c
>> +++ b/drivers/of/fdt.c
>> @@ -375,24 +375,37 @@ static void *memalign_realloc(void *orig, size_t oldsize, size_t newsize)
>>   
>>   static int fdt_ensure_space(struct fdt *fdt, int dtsize)
>>   {
>> +	size_t new_size;
>> +	void *previous;
>> +
>>   	/*
>>   	 * We assume strings and names have a maximum length of 1024
>>   	 * whereas properties can be longer. We allocate new memory
>>   	 * if we have less than 1024 bytes (+ the property size left.
>>   	 */
>>   	if (fdt->str_size - fdt->str_nextofs < 1024) {
>> -		fdt->strings = realloc(fdt->strings, fdt->str_size * 2);
>> -		if (!fdt->strings)
>> +		previous = fdt->strings;
>> +		new_size = fdt->str_size * 2;
>> +
>> +		if ((fdt->strings = realloc(previous, new_size)) == NULL) {
> 
> IMO, it's less readable this way. I'd prefer we leave the realloc line and
> then !fdt->strings check on separate lines as before.

Applied.

>> +			free(previous);
> 
> This could happen later in the callers (look for out_free) if one wouldn't
> set fdt->strings to NULL on error. I don't feel strongly either way, so doing
> it this way is fine too.
> 

Freeing the previous memory should happen in this function IMHO. In this 
way `fdt->[strings|dt]` can be set to `NULL` and the caller will get a 
segfaulty reminder if they choose to ignore error returned from the 
function and still access `fdt->[strings|dt]`.

> Change looks good otherwise, so with first comment addressed:
> 

Thank you :-).

> Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> 
> Cheers,
> Ahmad
> 
>>   			return -ENOMEM;
>> -		fdt->str_size *= 2;
>> +		}
>> +
>> +		fdt->str_size = new_size;
>>   	}
>>   
>>   	if (fdt->dt_size - fdt->dt_nextofs < 1024 + dtsize) {
>> -		fdt->dt = memalign_realloc(fdt->dt, fdt->dt_size,
>> -				fdt->dt_size * 2);
>> -		if (!fdt->dt)
>> +		previous = fdt->dt;
>> +		new_size = fdt->dt_size * 2;
>> +
>> +		if ((fdt->dt = memalign_realloc(previous, fdt->dt_size,
>> +						new_size)) == NULL) {
>> +			free(previous);
>>   			return -ENOMEM;
>> -		fdt->dt_size *= 2;
>> +		}
>> +
>> +		fdt->dt_size = new_size;
>>   	}
>>   
>>   	return 0;
>>
> 

Cheers
Stefan

-- 
Pengutronix e.K.                       | Stefan Kerkmann             |
Steuerwalder Str. 21                   | https://www.pengutronix.de/ |
31137 Hildesheim, Germany              | Phone: +49-5121-206917-128  |
Amtsgericht Hildesheim, HRA 2686       | Fax:   +49-5121-206917-9    |



  reply	other threads:[~2024-02-01  8:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-31 16:56 [PATCH 0/2] of: fdt: fix memory leak and oob writes " Stefan Kerkmann
2024-01-31 16:56 ` [PATCH 1/2] of: fdt: fix memory leak " Stefan Kerkmann
2024-01-31 17:15   ` Ahmad Fatoum
2024-02-01  8:34     ` Stefan Kerkmann [this message]
2024-01-31 16:57 ` [PATCH 2/2] of: fdt: fix oob writes with large ftd properties Stefan Kerkmann
2024-01-31 17:21   ` Ahmad Fatoum
2024-02-01 10:24     ` Stefan Kerkmann
2024-02-01 10:42       ` Ahmad Fatoum
2024-02-01  7:47   ` Sascha Hauer
2024-02-01  8:49     ` Stefan Kerkmann

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=4efd89d7-2bf8-4448-96f2-2fcd91241411@pengutronix.de \
    --to=s.kerkmann@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /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