mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2 0/2] of: fdt: fix memory leak and oob writes in fdt_ensure_space
@ 2024-02-01 11:28 Stefan Kerkmann
  2024-02-01 11:28 ` [PATCH v2 1/2] of: fdt: fix memory leak " Stefan Kerkmann
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Stefan Kerkmann @ 2024-02-01 11:28 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann, Ahmad Fatoum

I have encountered the oob write while attempting to modify a large FIT
image with of_property. While hunting for the root cause I noticed that
there is a potential memory leak in fdt_ensure_space as well. Both is
fixed in this series.

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
Changes in v2:
- Review feedback from Ahmad and Sascha
- Link to v1: https://lore.kernel.org/r/20240131-fix-fdt-memory-safety-v1-0-3d3a2c797eec@pengutronix.de

---
Stefan Kerkmann (2):
      of: fdt: fix memory leak in fdt_ensure_space
      of: fdt: fix oob writes with large fdt properties

 drivers/of/fdt.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
---
base-commit: cecd3fbea3550532d2175bc13aa479a45e605da0
change-id: 20240131-fix-fdt-memory-safety-b06f9164d953

Best regards,
-- 
Stefan Kerkmann <s.kerkmann@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/2] of: fdt: fix memory leak in fdt_ensure_space
  2024-02-01 11:28 [PATCH v2 0/2] of: fdt: fix memory leak and oob writes in fdt_ensure_space Stefan Kerkmann
@ 2024-02-01 11:28 ` Stefan Kerkmann
  2024-02-01 11:28 ` [PATCH v2 2/2] of: fdt: fix oob writes with large fdt properties Stefan Kerkmann
  2024-02-01 15:10 ` [PATCH v2 0/2] of: fdt: fix memory leak and oob writes in fdt_ensure_space Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Kerkmann @ 2024-02-01 11:28 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann, Ahmad Fatoum

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>
Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/fdt.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 5c21bab5de..544294a9ac 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -375,24 +375,38 @@ 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;
+
+		fdt->strings = realloc(previous, new_size);
+		if (!fdt->strings) {
+			free(previous);
 			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;
+
+		fdt->dt = memalign_realloc(previous, fdt->dt_size, new_size);
+		if (!fdt->dt) {
+			free(previous);
 			return -ENOMEM;
-		fdt->dt_size *= 2;
+		}
+
+		fdt->dt_size = new_size;
 	}
 
 	return 0;

-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] of: fdt: fix oob writes with large fdt properties
  2024-02-01 11:28 [PATCH v2 0/2] of: fdt: fix memory leak and oob writes in fdt_ensure_space Stefan Kerkmann
  2024-02-01 11:28 ` [PATCH v2 1/2] of: fdt: fix memory leak " Stefan Kerkmann
@ 2024-02-01 11:28 ` Stefan Kerkmann
  2024-02-01 15:10 ` [PATCH v2 0/2] of: fdt: fix memory leak and oob writes in fdt_ensure_space Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Stefan Kerkmann @ 2024-02-01 11:28 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

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 544294a9ac..cf08fa1cfd 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -14,6 +14,7 @@
 #include <memory.h>
 #include <linux/sizes.h>
 #include <linux/ctype.h>
+#include <linux/log2.h>
 #include <linux/overflow.h>
 #include <linux/string_helpers.h>
 #include <linux/err.h>
@@ -400,6 +401,9 @@ static int fdt_ensure_space(struct fdt *fdt, int dtsize)
 		previous = fdt->dt;
 		new_size = fdt->dt_size * 2;
 
+		if (new_size <= dtsize)
+			new_size = roundup_pow_of_two(fdt->dt_size + dtsize);
+
 		fdt->dt = memalign_realloc(previous, fdt->dt_size, new_size);
 		if (!fdt->dt) {
 			free(previous);

-- 
2.39.2




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 0/2] of: fdt: fix memory leak and oob writes in fdt_ensure_space
  2024-02-01 11:28 [PATCH v2 0/2] of: fdt: fix memory leak and oob writes in fdt_ensure_space Stefan Kerkmann
  2024-02-01 11:28 ` [PATCH v2 1/2] of: fdt: fix memory leak " Stefan Kerkmann
  2024-02-01 11:28 ` [PATCH v2 2/2] of: fdt: fix oob writes with large fdt properties Stefan Kerkmann
@ 2024-02-01 15:10 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2024-02-01 15:10 UTC (permalink / raw)
  To: BAREBOX, Stefan Kerkmann; +Cc: Ahmad Fatoum


On Thu, 01 Feb 2024 12:28:48 +0100, Stefan Kerkmann wrote:
> I have encountered the oob write while attempting to modify a large FIT
> image with of_property. While hunting for the root cause I noticed that
> there is a potential memory leak in fdt_ensure_space as well. Both is
> fixed in this series.
> 
> 

Applied, thanks!

[1/2] of: fdt: fix memory leak in fdt_ensure_space
      https://git.pengutronix.de/cgit/barebox/commit/?id=dd36494ae201 (link may not be stable)
[2/2] of: fdt: fix oob writes with large fdt properties
      https://git.pengutronix.de/cgit/barebox/commit/?id=96628249f26e (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-02-01 15:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-01 11:28 [PATCH v2 0/2] of: fdt: fix memory leak and oob writes in fdt_ensure_space Stefan Kerkmann
2024-02-01 11:28 ` [PATCH v2 1/2] of: fdt: fix memory leak " Stefan Kerkmann
2024-02-01 11:28 ` [PATCH v2 2/2] of: fdt: fix oob writes with large fdt properties Stefan Kerkmann
2024-02-01 15:10 ` [PATCH v2 0/2] of: fdt: fix memory leak and oob writes in fdt_ensure_space Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox