All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libfdt: Fix fdt_get_path() reading before the start of the buffer
@ 2026-10-01 12:10 Ayan Kumar Halder
  2026-10-01 15:10 ` Herve Codina
  2026-10-06  2:59 ` David Gibson
  0 siblings, 2 replies; 6+ messages in thread
From: Ayan Kumar Halder @ 2026-10-01 12:10 UTC (permalink / raw)
  To: devicetree-compiler
  Cc: David Gibson, Rob Herring, Andrew Cooper, Jan Beulich,
	Julien Grall, Stefano Stabellini, Michal Orzel, Bertrand Marquis,
	Ayan Kumar Halder

fdt_get_path() walks the tree from the root, appending each node's name
to the caller's buffer as it goes down and removing the last component
again as it comes back up.  It keeps the number of components currently
in the buffer in pdepth, and removes components while pdepth is greater
than the depth reported by fdt_next_node().

fdt_next_node() reports that it has left the tree by taking the depth
negative, but the offset it returns at that point is the offset of the
end tag, which is not negative.  The loop only tests the offset, so for a
nodeoffset at or past the end tag it runs one more time with a negative
depth.  pdepth still counts the components in the buffer, so the removal
loop is asked to remove more components than are there: it takes the
index down to zero and carries on scanning backwards for a '/' that is
not in the buffer.

Stop the walk when the depth shows it has left the tree.  Offsets that
far along are rejected by the error paths below in any case, so this
does not change what callers see.

Signed-off-by: Ayan Kumar Halder <ayan.kumar.halder@amd.com>
---
 libfdt/fdt_ro.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c
index 11f2e2e..32689c7 100644
--- a/libfdt/fdt_ro.c
+++ b/libfdt/fdt_ro.c
@@ -582,7 +582,7 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
 		return -FDT_ERR_NOSPACE;
 
 	for (offset = 0, depth = 0;
-	     (offset >= 0) && (offset <= nodeoffset);
+	     (offset >= 0) && (depth >= 0) && (offset <= nodeoffset);
 	     offset = fdt_next_node(fdt, offset, &depth)) {
 		while (pdepth > depth) {
 			do {
-- 
2.25.1


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

end of thread, other threads:[~2026-10-08 12:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-01 12:10 [PATCH] libfdt: Fix fdt_get_path() reading before the start of the buffer Ayan Kumar Halder
2026-10-01 15:10 ` Herve Codina
2026-10-01 17:18   ` Halder, Ayan Kumar
2026-10-02  9:28     ` Herve Codina
2026-10-06  2:59 ` David Gibson
2026-10-08 12:15   ` Simon Glass

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.