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: Steffen Trumtrar <s.trumtrar@pengutronix.de>,
	Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] of: fdt: verify length within bounds before using it
Date: Thu,  5 Jun 2025 13:26:07 +0200	[thread overview]
Message-ID: <20250605112607.1970520-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250605112607.1970520-1-a.fatoum@pengutronix.de>

We currently call dt_struct_advance() at the end of processing a tag
to advance to the next tag with a check after the switch to verify that
we are within bounds.

This is error prone as it expects that code that comes before it also
checks that len is not exceeded as dt_struct_advance would come too late
to go anything about this.

Avoid this by doing dt_struct_advance earlier in the switch cases and
bailing out directly if sizes aren't sane.

Reported-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/of/fdt.c | 53 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 19 deletions(-)

diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 5eead271edb7..9638b3d238be 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -227,6 +227,13 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 				goto err;
 			}
 
+			dt_struct = dt_struct_advance(&f, dt_struct,
+					sizeof(struct fdt_node_header) + len + 1);
+			if (!dt_struct) {
+				ret = -ESPIPE;
+				goto err;
+			}
+
 			if (!node) {
 				/* The root node must have an empty name */
 				if (*pathp) {
@@ -243,9 +250,6 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 				node = of_new_node(node, pathp);
 			}
 
-			dt_struct = dt_struct_advance(&f, dt_struct,
-					sizeof(struct fdt_node_header) + len + 1);
-
 			break;
 
 		case FDT_END_NODE:
@@ -258,6 +262,10 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 			node = node->parent;
 
 			dt_struct = dt_struct_advance(&f, dt_struct, FDT_TAGSIZE);
+			if (!dt_struct) {
+				ret = -ESPIPE;
+				goto err;
+			}
 
 			break;
 
@@ -272,7 +280,14 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 			nodep = fdt_prop->data;
 
 			name = dt_string(&f, dt_strings, fdt32_to_cpu(fdt_prop->nameoff));
-			if (!name || !node || is_reserved_name(name)) {
+			if (!name || !node ||  is_reserved_name(name)) {
+				ret = -ESPIPE;
+				goto err;
+			}
+
+			dt_struct = dt_struct_advance(&f, dt_struct,
+					sizeof(struct fdt_property) + len);
+			if (!dt_struct) {
 				ret = -ESPIPE;
 				goto err;
 			}
@@ -285,13 +300,15 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 			if (!strcmp(name, "phandle") && len == 4)
 				node->phandle = be32_to_cpup(of_property_get_value(p));
 
-			dt_struct = dt_struct_advance(&f, dt_struct,
-					sizeof(struct fdt_property) + len);
 
 			break;
 
 		case FDT_NOP:
 			dt_struct = dt_struct_advance(&f, dt_struct, FDT_TAGSIZE);
+			if (!dt_struct) {
+				ret = -ESPIPE;
+				goto err;
+			}
 
 			break;
 
@@ -303,11 +320,6 @@ static struct device_node *__of_unflatten_dtb(const void *infdt, int size,
 			ret = -EINVAL;
 			goto err;
 		}
-
-		if (!dt_struct) {
-			ret = -ESPIPE;
-			goto err;
-		}
 	}
 err:
 	of_delete_node(root);
@@ -752,6 +764,8 @@ int fdt_machine_is_compatible(const struct fdt_header *fdt, size_t fdt_size, con
 
 			dt_struct = dt_struct_advance(&f, dt_struct,
 					sizeof(struct fdt_node_header) + 1);
+			if (!dt_struct)
+				return 0;
 
 			/*
 			 * Quoting Device Tree Specification v0.4 §5.4.2:
@@ -775,24 +789,25 @@ int fdt_machine_is_compatible(const struct fdt_header *fdt, size_t fdt_size, con
 			if (!name)
 				return 0;
 
-			if (strcmp(name, "compatible")) {
-				dt_struct = dt_struct_advance(&f, dt_struct,
-							      sizeof(struct fdt_property) + len);
-				break;
-			}
+			dt_struct = dt_struct_advance(&f, dt_struct,
+						      sizeof(struct fdt_property) + len);
+			if (!dt_struct)
+				return 0;
+
+			if (strcmp(name, "compatible"))
+				continue;
 
 			return fdt_string_is_compatible(fdt_prop->data, len, compat, compat_len);
 
 		case FDT_NOP:
 			dt_struct = dt_struct_advance(&f, dt_struct, FDT_TAGSIZE);
+			if (!dt_struct)
+				return 0;
 			break;
 
 		default:
 			return 0;
 		}
-
-		if (!dt_struct)
-			return 0;
 	}
 
 	return 0;
-- 
2.39.5




  reply	other threads:[~2025-06-05 11:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-05 11:26 [PATCH 1/2] of: fdt: fix length comparison Ahmad Fatoum
2025-06-05 11:26 ` Ahmad Fatoum [this message]
2025-06-05 11:57 ` 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=20250605112607.1970520-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.trumtrar@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