[PATCH 2/2] of: fdt: fix oob writes with large ftd properties

Stefan Kerkmann s.kerkmann at pengutronix.de
Thu Feb 1 00:49:01 PST 2024


Hi Sascha,

On 01.02.24 08:47, Sascha Hauer wrote:
> 
> In the subject: s/ftd/fdt/
> 

Applied.

> Sascha
> 
> On Wed, Jan 31, 2024 at 05:57:00PM +0100, 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 at 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;
>> +		}
>> +
>>   		if ((fdt->dt = memalign_realloc(previous, fdt->dt_size,
>>   						new_size)) == NULL) {
>>   			free(previous);
>>
>> -- 
>> 2.39.2
>>
>>
> 

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    |



More information about the barebox mailing list