libfdt: Fix fdt_get_path() reading before the start of the buffer
PATCH 1/1 libfdt: Fix fdt_get_path() reading before the start of the buffer
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.
--- libfdt/fdt_ro.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
libfdt/fdt_ro.c +1 -1
@@ -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 {
Signature · 1 line
2.25.1
Hi Ayan,
Quoted 9 lines · On Thu, 1 Oct 2026 13:10:36 +0100 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 byfdt_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
fdt_next_node() returns an error (-FDT_ERR_NOTFOUND) when it reaches the FDT_END tag [1].
I don't understand your point. Maybe I've missed something. Can you clarify?
[1] https://github.com/dgibson/dtc/blob/7a1e017926004ecff5fce62d62d42ce9f3e00082/libfdt/fdt.c#L283
Quoted 9 lines
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.
If fdt_next_node() can return an offset (positive value) which is not pointing to a node without any errors (negative returned value), fdt_next_node() has to be fixed not callers.
Can you give details leading fdt_next_node() returning a valid offset which doesn't point to a node?
Best regards,
Hervé
Quoted 19 lines
Signed-off-by Ayan Kumar Halder <[email protected]>--- libfdt/fdt_ro.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/fdt_ro.c +1
-1@@ -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 {
Quoted 1 line · Herve Codina
Hi Ayan,
Hi Herve,
Thanks for the review.
Quoted 10 lines
Quoted 6 lines · On Thu, 1 Oct 2026 13:10:36 +0100 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 byfdt_next_node().
Quoted 8 lines
Quoted 3 lines
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
fdt_next_node()returns an error (-FDT_ERR_NOTFOUND) when it reaches theFDT_ENDtag [1].I don't understand your point. Maybe I've missed something. Can you clarify?
Sorry, my wording was wrong. I meant the FDT_END_NODE case, just above the one you linked:
case FDT_END_NODE:
if (depth && ((--(*depth)) < 0))
return nextoffset;
Leaving the root node is an FDT_END_NODE tag, so --(*depth) takes the depth from 0 to -1 and fdt_next_node() returns nextoffset, which is not negative. FDT_END is only reached on the next call.
Quoted 12 lines
[1] https://github.com/dgibson/dtc/blob/7a1e017926004ecff5fce62d62d42ce9f3e00082/libfdt/fdt.c#L283
Quoted 9 lines
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.
Quoted 6 lines
If
fdt_next_node()can return an offset (positive value) which is not pointing to a node without any errors (negative returned value),fdt_next_node()has to be fixed not callers.Can you give details leading
fdt_next_node()returning a valid offset which doesn't point to a node?
The offset is the one past root's FDT_END_NODE tag, so on tests/test_tree1.dtb:
offset depth
540 2
560 -1 <- FDT_END_NODE: offset >= 0, depth < 0 -4 -1 <- FDT_ERR_BADOFFSET, from the next call
Does it make sense ?
I discovered this issue by building libfdt with AddressSanitizer.
( I am new to this, so it may be my misunderstanding )
- Ayan
Quoted 28 lines · Ayan Kumar
Quoted 1 line · Herve Codina
Hi Ayan,
Hi Herve,
Thanks for the review.
Quoted 10 lines
Quoted 6 lines · On Thu, 1 Oct 2026 13:10:36 +0100 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 byfdt_next_node().Quoted 8 lines
Quoted 3 lines
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
fdt_next_node()returns an error (-FDT_ERR_NOTFOUND) when it reaches theFDT_ENDtag [1].I don't understand your point. Maybe I've missed something. Can you clarify?
Sorry, my wording was wrong. I meant the
FDT_END_NODEcase, just above the one you linked:
Quoted 7 lines
case
FDT_END_NODE:
if (depth && ((--(*depth)) < 0))
return nextoffset;Leaving the root node is an
FDT_END_NODEtag, so --(*depth) takes the depth from 0 to -1 andfdt_next_node()returns nextoffset, which is not negative.FDT_ENDis only reached on the next call.
Ok
Quoted 22 lines
Quoted 13 lines
[1] https://github.com/dgibson/dtc/blob/7a1e017926004ecff5fce62d62d42ce9f3e00082/libfdt/fdt.c#L283
Quoted 9 lines
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.
Quoted 6 lines
If
fdt_next_node()can return an offset (positive value) which is not pointing to a node without any errors (negative returned value),fdt_next_node()has to be fixed not callers.Can you give details leading
fdt_next_node()returning a valid offset which doesn't point to a node?The offset is the one past root's
FDT_END_NODEtag, so on tests/test_tree1.dtb:
Quoted 6 lines
offset depth
540 2
560 -1 <-FDT_END_NODE: offset >= 0, depth < 0 -4 -1 <-FDT_ERR_BADOFFSET, from the next callDoes it make sense ?
It could but I still don't see where the issue is.
I mean, in the fdt_get_path() we have the following loop:
for (offset = 0, depth = 0;
(offset >= 0) && (offset <= nodeoffset);
offset = fdt_next_node(fdt, offset, &depth)) {
...
}fdt_next_node() returns an offset past the root's FDT_END_NODE tag.
In that case, how the 'offset <= nodeoffset' condition can be true in order to continue the loop?
nodeoffset is an offset to a node. It is either the root node or a node inside the root tree. And so this offset points to something before the root's FDT_END_NODE tag offset.
Do I miss something?
Best regards,
Hervé
Quoted 15 lines · 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 byfdt_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.
Quoted 5 lines
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 <[email protected]>
I think this is a bug in fdt_next_node() rather than in fdt_get_path(). It should not be returning offsets that are not valid node offsets (excepting negative error codes, of course).
Quoted 21 lines
--- libfdt/fdt_ro.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/fdt_ro.c +1
-1@@ -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 {Signature · 1 line
2.25.1
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Hi,
Quoted 21 lines · David Gibson
Quoted 19 lines · 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 byfdt_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.
Quoted 5 lines
Quoted 1 line
Signed-off-by Ayan Kumar Halder <[email protected]>I think this is a bug in
fdt_next_node()rather than infdt_get_path(). It should not be returning offsets that are not valid node offsets (excepting negative error codes, of course).
The bug is only with an invalid nodeoffset past the root's FDT_END_NODE
I don't think fdt_next_node() should change - fdt_node_end_offset_() relies on it returning the offset after the FDT_END_NODE when depth goes negative, and fdt_nop_node() and fdt_del_node() use that. For the last child of a node, that offset is the parent's FDT_END_NODE, so it is not a node offset.
One option would be to reject nodeoffset up front with fdt_check_node_offset_()
For the commit message, as you mention, use 'FDT_END_NODE' rather than 'end tag' and make it clear this needs a bad nodeoffset - also try to explain the problem in the first paragraph.
Also please add a bad-offset case to tests/get_path.c
Regards,
Simon
Quoted 12 lines
Quoted 12 lines
--- libfdt/fdt_ro.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)libfdt/fdt_ro.c +0
-0@@ -582,7 +582,7 @@ int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)return -FDT_ERR_NOSPACE ;
Quoted 10 lines
Quoted 10 lines
libfdt/fdt_ro.c +1
-2for (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
[..]
Regards,
SImon
Quoted 30 lines · Simon Glass
Hi,
Quoted 21 lines · David Gibson
Quoted 19 lines · 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 byfdt_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.
Quoted 5 lines
Quoted 1 line
Signed-off-by Ayan Kumar Halder <[email protected]>I think this is a bug in
fdt_next_node()rather than infdt_get_path(). It should not be returning offsets that are not valid node offsets (excepting negative error codes, of course).
Quoted 7 lines
The bug is only with an invalid nodeoffset past the root's
FDT_END_NODEI don't think
fdt_next_node()should change -fdt_node_end_offset_()relies on it returning the offset after theFDT_END_NODEwhen depth goes negative, andfdt_nop_node()andfdt_del_node()use that. For the last child of a node, that offset is the parent'sFDT_END_NODE, so it is not a node offset.
Huh, good catch. Looks like I deliberately changed it to be that way in ce4d9c0f ("libfdt: Rework fdt_next_node()"), but I'd forgotten.
Quoted 1 line
One option would be to reject nodeoffset up front with
fdt_check_node_offset_()
I'm not entirely sure where you're suggesting putting this. I think the original proposed fix is correct, although having an additional offset test for robustness might not be a bad idea as well.
Quoted 5 lines
For the commit message, as you mention, use '
FDT_END_NODE' rather than 'end tag' and make it clear this needs a bad nodeoffset - also try to explain the problem in the first paragraph.Also please add a bad-offset case to
tests/get_path.c
Right. I now think the fix itself is good, but some changes around it are desirable:
* Correct the commit message as indicated
* Update the commit message to explicitly reference commit ce4d9c0f, which explains why fdt_next_node() works the way it does
* Add a test case
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
Hi David/all,
Apologies for the long delay.
Quoted 35 lines · David Gibson
Quoted 27 lines · Simon Glass
Hi,
Quoted 16 lines · David Gibson
Quoted 15 lines · 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 byfdt_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.Quoted 8 lines
Quoted 5 lines
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 <[email protected]>I think this is a bug in
fdt_next_node()rather than infdt_get_path(). It should not be returning offsets that are not valid node offsets (excepting negative error codes, of course).Quoted 7 lines
The bug is only with an invalid nodeoffset past the root's
FDT_END_NODEI don't think
fdt_next_node()should change -fdt_node_end_offset_()relies on it returning the offset after theFDT_END_NODEwhen depth goes negative, andfdt_nop_node()andfdt_del_node()use that. For the last child of a node, that offset is the parent'sFDT_END_NODE, so it is not a node offset.
Quoted 7 lines
Huh, good catch. Looks like I deliberately changed it to be that way in
ce4d9c0f("libfdt: Reworkfdt_next_node()"), but I'd forgotten.Quoted 1 line
One option would be to reject nodeoffset up front with
fdt_check_node_offset_()I'm not entirely sure where you're suggesting putting this. I think the original proposed fix is correct, although having an additional offset test for robustness might not be a bad idea as well.
I have added it at the top of fdt_get_path(), after the buflen check:
if (!can_assume(VALID_INPUT)
&& ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0)) return err;
Quoted 5 lines
Quoted 5 lines
For the commit message, as you mention, use '
FDT_END_NODE' rather than 'end tag' and make it clear this needs a bad nodeoffset - also try to explain the problem in the first paragraph.
Quoted 7 lines
Quoted 1 line
Also please add a bad-offset case to
tests/get_path.cRight. I now think the fix itself is good, but some changes around it are desirable:
* Correct the commit message as indicated
* Update the commit message to explicitly reference commitce4d9c0f, which explains whyfdt_next_node()works the way it does
* Add a test case
I have fixed all the three in v2. Please see "[PATCH v2] libfdt: Fix fdt_get_path() reading before the start of the buffer".
- Ayan