mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 2/2] state: dtb: enforce minimum FDT length
Date: Thu, 21 Aug 2025 22:46:58 +0200	[thread overview]
Message-ID: <20250821204658.1426566-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250821204658.1426566-1-a.fatoum@pengutronix.de>

We should check that we have at least a FDT header before dereferencing
pointers to the structure. Issue detected with libfuzzer.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> v2:
  - new patch
---
 common/state/backend_format_dtb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/common/state/backend_format_dtb.c b/common/state/backend_format_dtb.c
index b41b896aaced..9624d1e8103e 100644
--- a/common/state/backend_format_dtb.c
+++ b/common/state/backend_format_dtb.c
@@ -45,9 +45,13 @@ static int state_backend_format_dtb_verify(struct state_backend_format *format,
 	struct state_backend_format_dtb *fdtb = get_format_dtb(format);
 	struct device_node *root;
 	struct fdt_header *fdt = (struct fdt_header *)buf;
-	size_t dtb_len = fdt32_to_cpu(fdt->totalsize);
+	size_t dtb_len;
 	size_t len = *lenp;
 
+	if (len < sizeof(*fdt))
+		return -EINVAL;
+
+	dtb_len = fdt32_to_cpu(fdt->totalsize);
 	if (dtb_len > len) {
 		dev_err(fdtb->dev, "Error, stored DTB length (%zd) longer than read buffer (%zd)\n",
 			dtb_len, len);
-- 
2.39.5




      reply	other threads:[~2025-08-22  2:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-21 20:46 [PATCH v2 1/2] state: do not panic on flipped bits in on-disk sizes Ahmad Fatoum
2025-08-21 20:46 ` Ahmad Fatoum [this message]

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=20250821204658.1426566-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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