mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Stefan Kerkmann <s.kerkmann@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>,
	 BAREBOX <barebox@lists.infradead.org>
Cc: Stefan Kerkmann <s.kerkmann@pengutronix.de>,
	 Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 1/2] of: fdt: fix memory leak in fdt_ensure_space
Date: Thu, 01 Feb 2024 12:28:49 +0100	[thread overview]
Message-ID: <20240201-fix-fdt-memory-safety-v2-1-267b7b8813fd@pengutronix.de> (raw)
In-Reply-To: <20240201-fix-fdt-memory-safety-v2-0-267b7b8813fd@pengutronix.de>

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




  reply	other threads:[~2024-02-01 11:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-01 11:28 [PATCH v2 0/2] of: fdt: fix memory leak and oob writes " Stefan Kerkmann
2024-02-01 11:28 ` Stefan Kerkmann [this message]
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

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=20240201-fix-fdt-memory-safety-v2-1-267b7b8813fd@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