[PATCH] of: fdt: fix double free in fdt_ensure_space
Ahmad Fatoum
a.fatoum at pengutronix.de
Sun Feb 15 09:24:19 PST 2026
fdt_ensure_space() frees the original buffer on memalign_realloc()
failure, but on error, the buffer is already freed by
memalign_realloc() unlike realloc().
Fix this by aligning memalign_realloc() behavior with normal realloc()
and adapting callers accordingly.
Fixes: dd36494ae2 ("of: fdt: fix memory leak in fdt_ensure_space")
Reported-by: Claude Opus 4.6 <noreply at anthropic.com>
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
drivers/of/fdt.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index ea763cf7a52a..658718d4e238 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -421,10 +421,8 @@ static void *memalign_realloc(void *orig, size_t oldsize, size_t newsize)
return memalign(align, newsize);
newbuf = memalign(align, newsize);
- if (!newbuf) {
- free(orig);
+ if (!newbuf)
return NULL;
- }
memcpy(newbuf, orig, oldsize);
free(orig);
@@ -606,10 +604,13 @@ void *of_flatten_dtb(struct device_node *node)
header.size_dt_strings = cpu_to_fdt32(fdt.str_nextofs);
if (fdt.dt_size - fdt.dt_nextofs < fdt.str_nextofs) {
- fdt.dt = memalign_realloc(fdt.dt, fdt.dt_size,
+ void *newmem;
+
+ newmem = memalign_realloc(fdt.dt, fdt.dt_size,
fdt.dt_nextofs + fdt.str_nextofs);
- if (!fdt.dt)
+ if (!newmem)
goto out_free;
+ fdt.dt = newmem;
}
memcpy(fdt.dt + fdt.dt_nextofs, fdt.strings, fdt.str_nextofs);
--
2.47.3
More information about the barebox
mailing list