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 master 4/4] of: fdt: bound node nesting depth in __of_unflatten_dtb
Date: Mon, 24 Aug 2026 13:59:58 +0200	[thread overview]
Message-ID: <20260824120022.3299742-4-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20260824120022.3299742-1-a.fatoum@pengutronix.de>

of_new_node() builds each node's full_name by concatenating the parent's
full path, so unflattening a chain of N nested nodes costs O(N^2) time and
memory. A crafted FIT/DTB with hundreds of thousands of nested nodes (e.g.
the BRLY-2026-042 U-Boot PoC, 500k deep) therefore drives barebox into
multi-gigabyte allocations and minutes of CPU before failing, a denial of
service, even though the iterative walk here never overflows the stack.

Reject blobs nested deeper than FDT_MAX_DEPTH (64, as Linux's own
drivers/of/fdt.c uses) by tracking depth across FDT_BEGIN_NODE/FDT_END_NODE.
Real device trees are only a handful of levels deep, so the limit is
generous for legitimate input while cutting the pathological case off early.

Assisted-by: Claude:fable-5
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/fdt.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 1648f4c2d945..b5b64cd06b8d 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -173,6 +173,12 @@ static int fdt_parse_header(const struct fdt_header *fdt, size_t fdt_size,
 	return 0;
 }
 
+/*
+ * Maximum node nesting depth we are willing to unflatten.
+ * Matches the limit Linux uses in its own drivers/of/fdt.c.
+ */
+#define FDT_MAX_DEPTH	64
+
 /**
  * of_unflatten_dtb - unflatten a dtb binary blob
  * @infdt - the fdt blob to unflatten
@@ -196,6 +202,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 	struct fdt_header f;
 	int ret;
 	int maxlen;
+	unsigned int depth = 0;
 	const struct fdt_header *fdt = infdt;
 
 	ret = fdt_parse_header(infdt, size, &f);
@@ -247,6 +254,12 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 				goto err;
 			}
 
+			if (++depth > FDT_MAX_DEPTH) {
+				pr_err("unflatten: node nesting too deep\n");
+				ret = -EINVAL;
+				goto err;
+			}
+
 			if (!node) {
 				/* The root node must have an empty name */
 				if (*pathp) {
@@ -272,6 +285,7 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 				goto err;
 			}
 
+			depth--;
 			node = node->parent;
 
 			dt_struct = dt_struct_advance(&f, dt_struct, FDT_TAGSIZE, 0);
-- 
2.47.3




  parent reply	other threads:[~2026-08-24 12:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-24 11:59 [PATCH master 1/4] of: only replace a device tree that is actually tentative Ahmad Fatoum
2026-08-24 11:59 ` [PATCH master 2/4] tlsf: unpoison whole block in malloc_usable_size() Ahmad Fatoum
2026-08-24 11:59 ` [PATCH master 3/4] partitions: dos: bound extended partition chain Ahmad Fatoum
2026-08-24 11:59 ` Ahmad Fatoum [this message]
2026-08-24 12:58 ` [PATCH master 1/4] of: only replace a device tree that is actually tentative 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=20260824120022.3299742-4-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