libfdt: fdt_path_offset_namelen: Reject empty path
PATCH 1/1 libfdt: fdt_path_offset_namelen: Reject empty path
Make empty paths result in FDT_ERR_BADPATH.
Per the specification (v0.4-rc4):
Quoted 7 lines
The convention for specifying a device path is:
/node-name-1/node-name-2/node-name-NThe path to the root node is /.
A unit address may be omitted if the full path to the node is unambiguous.
--- libfdt/fdt_ro.c | 3 +++ 1 file changed, 3 insertions(+)
libfdt/fdt_ro.c +3 -0
@@ -255,6 +255,9 @@ int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)FDT_RO_PROBE (fdt); +if (namelen <1 ) +return -FDT_ERR_BADPATH ; +/* see if we have an alias */ if (*path !='/' ) {const char *q =memchr (path,'/' , end - p);
Signature · 5 lines
2.42.0.609.gbb76f46606-goog -- Pierre
Hi Pierre-Clément,
Quoted 23 lines · Pierre-Clément Tosi
Make empty paths result in
FDT_ERR_BADPATH.Per the specification (v0.4-rc4):
Quoted 7 lines
The convention for specifying a device path is:
/node-name-1/node-name-2/node-name-NThe path to the root node is /.
A unit address may be omitted if the full path to the node is unambiguous.
Signed-off-by Pierre-Clément Tosi <[email protected]>--- libfdt/fdt_ro.c | 3 +++ 1 file changed, 3 insertions(+)libfdt/fdt_ro.c +0
-0
libfdt/fdt_ro.c int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) · line 260
Quoted 6 lines
libfdt/fdt_ro.c +3
-0@@ -255,6 +255,9 @@ int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)FDT_RO_PROBE (fdt); +if (namelen <1 ) +return -FDT_ERR_BADPATH ; +
This would be end == path, right? Would it be better to check that? Are you worried about negative numbers?
Quoted 10 lines
libfdt/fdt_ro.c +0
-1/* see if we have an alias */ if (*path !='/' ) {const char *q =memchr (path,'/' , end - p); --2.42.0.609.gbb76f46606-goog
--
Pierre
Regards,
Simon
Hi Simon,
Quoted 11 lines · Simon Glass
Hi Pierre-Clément,
libfdt/fdt_ro.c int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) · line 260
Quoted 4 lines · Pierre-Clément Tosi
libfdt/fdt_ro.c +3
-0+if (namelen <1 ) +return -FDT_ERR_BADPATH ; +This would be end == path, right? Would it be better to check that? Are you worried about negative numbers?
The main focus of this patch was namelen==0 (e.g. fdt_path_offset(fdt, "")) which violates the spec and makes the function return the offset of the root node, a counter-intuitive result which, if used internally (other libfdt functions call fdt_path_offset()), could lead to unintended behavior.
Furthermore, under the right conditions, fdt_path_offset(fdt, "") may lead to a stack overflow attack, which this patch addresses but which is also separately addressed by [1] (although one doesn't make the other redundant).
As I was adding a check on namelen, I took the opportunity to also reject all negative values. Do you recommend I only reject end == path and accept/ignore negative lengths?
If this validation makes sense, v2 will add coverage for it in 'make check'.
[1]: https://lore.kernel.org/devicetree-compiler/[email protected]
Quoted 7 lines
Quoted 4 lines
/* see if we have an alias */ if (*path != '/') { const char *q = memchr(path, '/', end - p);--
Regards,
Simon
Thanks,
Signature · 1 line
Pierre
Quoted 27 lines · Simon Glass
Hi Pierre-Clément,
Quoted 24 lines · Pierre-Clément Tosi
Make empty paths result in
FDT_ERR_BADPATH.Per the specification (v0.4-rc4):
Quoted 7 lines
The convention for specifying a device path is:
/node-name-1/node-name-2/node-name-NThe path to the root node is /.
A unit address may be omitted if the full path to the node is unambiguous.
Signed-off-by Pierre-Clément Tosi <[email protected]>--- libfdt/fdt_ro.c | 3 +++ 1 file changed, 3 insertions(+)libfdt/fdt_ro.c +0
-0@@ -255,6 +255,9 @@ int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)
Quoted 7 lines
libfdt/fdt_ro.c int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) · line 260
Quoted 5 lines
libfdt/fdt_ro.c +3
-0FDT_RO_PROBE (fdt); +if (namelen <1 ) +return -FDT_ERR_BADPATH ; +This would be end == path, right? Would it be better to check that?
Not particularly, AFAICT.
Quoted 1 line
Are you worried about negative numbers?
If they're not, they probably should be..
Quoted 11 lines
libfdt/fdt_ro.c int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) · line 263
Quoted 7 lines
libfdt/fdt_ro.c +0
-0/* see if we have an alias */ if (*path !='/' ) {const char *q =memchr (path,'/' , end - p);Regards,
Simon
Signature · 4 lines
David Gibson | 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
Quoted 12 lines · Pierre-Clément Tosi
Make empty paths result in
FDT_ERR_BADPATH.Per the specification (v0.4-rc4):
Quoted 7 lines
The convention for specifying a device path is:
/node-name-1/node-name-2/node-name-NThe path to the root node is /.
A unit address may be omitted if the full path to the node is unambiguous.
How is this part relevant to this patch? In any case, I don't think we actually allow that. Maybe libfdt does, but at least it's a dtc warning.
Rob
Hi Rob,
Quoted 10 lines · Rob Herring
Quoted 8 lines · Pierre-Clément Tosi
Make empty paths result in
FDT_ERR_BADPATH.Per the specification (v0.4-rc4):
Quoted 3 lines
The convention for specifying a device path is:
/node-name-1/node-name-2/node-name-N
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
The path to the root node is /.
A unit address may be omitted if the full path to the node is unambiguous.
How is this part relevant to this patch? In any case, I don't think we actually allow that. Maybe libfdt does, but at least it's a dtc warning.
I agree it isn't relevant...re your point, see fdt_nodename_eq_() which matches without the '@' so long as the length provided is the length of the node name without the '@'. It doesn't check for ambiguous nodes.
Regards,
Simon
Quoted 18 lines · Simon Glass
Hi Rob,
Quoted 15 lines · Rob Herring
Quoted 12 lines · Pierre-Clément Tosi
Make empty paths result in
FDT_ERR_BADPATH.Per the specification (v0.4-rc4):
Quoted 7 lines
The convention for specifying a device path is:
/node-name-1/node-name-2/node-name-NThe path to the root node is /.
A unit address may be omitted if the full path to the node is unambiguous.
Quoted 9 lines
Quoted 3 lines
How is this part relevant to this patch? In any case, I don't think we actually allow that. Maybe libfdt does, but at least it's a dtc warning.
I agree it isn't relevant...re your point, see
fdt_nodename_eq_()which matches without the '@' so long as the length provided is the length of the node name without the '@'. It doesn't check for ambiguous nodes.
Agreed, I was trying to be exhaustive: the irrelevant part won't be in v2.
Quoted 2 lines
Regards,
Simon
Thanks,
Signature · 1 line
Pierre
Quoted 11 lines · Pierre-Clément Tosi
Make empty paths result in
FDT_ERR_BADPATH.Per the specification (v0.4-rc4):
Quoted 7 lines
The convention for specifying a device path is:
/node-name-1/node-name-2/node-name-NThe path to the root node is /.
A unit address may be omitted if the full path to the node is unambiguous.
As Rob noted, I don't really see how this quote is relevant to the change at hand.
The change itself looks like a good idea, though. Without this, we will at the very least do a one byte bad access in the next line. If someone does path a negative value it will do... something bad, probably.
Quoted 10 lines
Signed-off-by Pierre-Clément Tosi <[email protected]>--- libfdt/fdt_ro.c | 3 +++ 1 file changed, 3 insertions(+)libfdt/fdt_ro.c +0
-0
libfdt/fdt_ro.c int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) · line 259
Quoted 5 lines
libfdt/fdt_ro.c +2
-0@@ -255,6 +255,9 @@ int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen)FDT_RO_PROBE (fdt); +if (namelen <1 ) +return -FDT_ERR_BADPATH ;
It would be better to make this:
if (!can_assume(VALID_INPUT) && namelen <= 0)
This allows the test to be optimised out in builds where we can assume always valid parameters.
libfdt/fdt_ro.c int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) · line 263
Quoted 3 lines
libfdt/fdt_ro.c +0
-0/* see if we have an alias */ if (*path !='/' ) {const char *q =memchr (path,'/' , end - p);
It would also be really nice to add a testcase for behaviour in the namelen == 0 and namelen < 0 cases.
Signature · 4 lines
David Gibson | 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