mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: Stefan Kerkmann <s.kerkmann@pengutronix.de>,
	Sascha Hauer <s.hauer@pengutronix.de>,
	BAREBOX <barebox@lists.infradead.org>
Subject: Re: [PATCH 2/2] of: fdt: fix oob writes with large ftd properties
Date: Thu, 1 Feb 2024 11:42:48 +0100	[thread overview]
Message-ID: <ea727a4b-7e8e-4502-af73-342132d8dd03@pengutronix.de> (raw)
In-Reply-To: <53ab0b01-2de6-4255-913e-963d921737b5@pengutronix.de>

On 01.02.24 11:24, Stefan Kerkmann wrote:
> Hello Ahmad,
> 
> On 31.01.24 18:21, Ahmad Fatoum wrote:
>> On 31.01.24 17:57, Stefan Kerkmann wrote:
>>> OOB writes can be triggered when fdt->dt_size * 2 is still smaller than
>>> the property for which memory should be allocated. This can happen under
>>> rare circumstances when editing a fdt with the of_property command and a
>>> property is larger than 128k in size.
>>>
>>> This happend when editing a FIT image (which is a ftd) with the
>>> of_property command and the Kernel image was around 8M in size.
>>>
>>> The simplified call chain is the following:
>>>
>>> of_property -> of_flatten_dtb -> create new fdt with 64k in size (this is
>>> fixed) -> __of_flatten_dtb -> attempt to copy kernel image (8M) ->
>>> fdt_ensure_space -> allocate only 128k for fdt->dt -> memcopy 8M into fdt->dt
>>> buffer -> crash
>>>
>>> The fix is to grow fdt->dt to hold at least the new property. The power
>>> of 2 increment is untouched to keep the same behaviour otherwise.
>>>
>>> Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
>>> ---
>>>   drivers/of/fdt.c | 4 ++++
>>>   1 file changed, 4 insertions(+)
>>>
>>> diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
>>> index 4f79a6120f..1f24ed0bbc 100644
>>> --- a/drivers/of/fdt.c
>>> +++ b/drivers/of/fdt.c
>>> @@ -399,6 +399,10 @@ static int fdt_ensure_space(struct fdt *fdt, int dtsize)
>>>           previous = fdt->dt;
>>>           new_size = fdt->dt_size * 2;
>>>   +        while (new_size <= dtsize) {
>>> +            new_size *= 2;
>>> +        }
>>
>> A nitpick that I solely note because I already had feedback on the first patch:
>> Kernel coding style is to omit { braces } for single statement blocks.
>>
>> In your case you could just do:
>>
>>   if (new_size <= dtsize)
>>       new_size = roundup_pow_of_two(new_size + dtsize);
>>
>> I think to skip the loop.
>>
> 
> Thanks! That is the better solution.
> 
> To not over provision memory I changed the new size to be `roundup_pow_of_two(fdt->dt_size + dtsize)` as we know for sure that `dtsize` is already larger than `fdt->dt_size * 2`.
> 
> In (made up) case that we already have 8k space for the fdt and got a 17k property we would allocate 65k (8k + 8k + 17k = 33k ⇾ rounded ⇾ 65k) and only 32k (8k + 17k = 25k → rounded → 32k) with `fdt->dt_size + dtsize`.

Sounds good.

> 
>> Cheers,
>> Ahmad
>>
>>> +
>>>           if ((fdt->dt = memalign_realloc(previous, fdt->dt_size,
>>>                           new_size)) == NULL) {
>>>               free(previous);
>>>
>>
> 
> Cheers
> Stefan
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




  reply	other threads:[~2024-02-01 10:43 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 in fdt_ensure_space 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
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 [this message]
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=ea727a4b-7e8e-4502-af73-342132d8dd03@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    --cc=s.kerkmann@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