libfdt: fdt_path_offset_namelen: Reject empty path
PATCH v3 1/1 libfdt: fdt_path_offset_namelen: Reject empty path
Reject empty paths and negative lengths, according to the DT spec v0.4:
The convention for specifying a device path is:
/node-name-1/node-name-2/node-name-N
The path to the root node is /.
This prevents the access to path[0] from ever being out-of-bounds.
---
v2
- allow the check to be optimized out when ASSUME_VALID_INPUT
- add test coverage for empty paths and negative size
- remove redundant part of the quote in the commit message
v3
- simplify condition in check_path_offset()
--- libfdt/fdt_ro.c | 3 +++ tests/path_offset.c | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-)
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 (!can_assume (VALID_INPUT ) && namelen <=0 ) +return -FDT_ERR_BADPATH ; +/* see if we have an alias */ if (*path !='/' ) {const char *q =memchr (path,'/' , end - p);
tests/path_offset.c +7 -1
@@ -48,10 +48,13 @@ static void check_path_offset(void *fdt, const char *path, int offset)verbose_printf ("Checking offset of \"%s\" is %d...\n" , path, offset); rc =fdt_path_offset (fdt, path); +if (rc == offset) +return ; +if (rc <0 )FAIL ("fdt_path_offset(\"%s\") failed: %s" , path,fdt_strerror (rc)); -if (rc != offset) +else FAIL ("fdt_path_offset(\"%s\") returned incorrect offset" " %d instead of %d" , path, rc, offset); }
@@ -102,6 +105,7 @@ int main(int argc, char *argv[])check_path_offset (fdt,"/subnode@2/subsubnode" , subsubnode2_offset2);/* Test paths with extraneous separators */ +check_path_offset (fdt,"" , -FDT_ERR_BADPATH );check_path_offset (fdt,"//" ,0 );check_path_offset (fdt,"///" ,0 );check_path_offset (fdt,"//subnode@1" , subnode1_offset);
@@ -110,6 +114,8 @@ int main(int argc, char *argv[])check_path_offset (fdt,"/subnode@2////subsubnode" , subsubnode2_offset2);/* Test fdt_path_offset_namelen() */ +check_path_offset_namelen (fdt,"/subnode@1" ,-1 , -FDT_ERR_BADPATH ); +check_path_offset_namelen (fdt,"/subnode@1" ,0 , -FDT_ERR_BADPATH );check_path_offset_namelen (fdt,"/subnode@1" ,1 ,0 );check_path_offset_namelen (fdt,"/subnode@1/subsubnode" ,10 , subnode1_offset);check_path_offset_namelen (fdt,"/subnode@1/subsubnode" ,11 , subnode1_offset);
Signature · 5 lines
2.42.0.609.gbb76f46606-goog -- Pierre
Quoted 10 lines · Pierre-Clément Tosi
Reject empty paths and negative lengths, according to the DT spec v0.4:
The convention for specifying a device path is:
/node-name-1/node-name-2/node-name-NThe path to the root node is /.
This prevents the access to path[0] from ever being out-of-bounds.
Signed-off-by Pierre-Clément Tosi <[email protected]>
Merged and pushed out, thanks.
Quoted 62 lines
---
v2
- allow the check to be optimized out whenASSUME_VALID_INPUT
- add test coverage for empty paths and negative size
- remove redundant part of the quote in the commit message
v3
- simplify condition incheck_path_offset()--- libfdt/fdt_ro.c | 3 +++ tests/path_offset.c | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-)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 (!can_assume (VALID_INPUT ) && namelen <=0 ) +return -FDT_ERR_BADPATH ; +/* see if we have an alias */ if (*path !='/' ) {const char *q =memchr (path,'/' , end - p);tests/path_offset.c +7
-1@@ -48,10 +48,13 @@ static void check_path_offset(void *fdt, const char *path, int offset)verbose_printf ("Checking offset of \"%s\" is %d...\n" , path, offset); rc =fdt_path_offset (fdt, path); +if (rc == offset) +return ; +if (rc <0 )FAIL ("fdt_path_offset(\"%s\") failed: %s" , path,fdt_strerror (rc)); -if (rc != offset) +else FAIL ("fdt_path_offset(\"%s\") returned incorrect offset" " %d instead of %d" , path, rc, offset); }@@ -102,6 +105,7 @@ int main(int argc, char *argv[])check_path_offset (fdt,"/subnode@2/subsubnode" , subsubnode2_offset2);/* Test paths with extraneous separators */ +check_path_offset (fdt,"" , -FDT_ERR_BADPATH );check_path_offset (fdt,"//" ,0 );check_path_offset (fdt,"///" ,0 );check_path_offset (fdt,"//subnode@1" , subnode1_offset);@@ -110,6 +114,8 @@ int main(int argc, char *argv[])check_path_offset (fdt,"/subnode@2////subsubnode" , subsubnode2_offset2);/* Test fdt_path_offset_namelen() */ +check_path_offset_namelen (fdt,"/subnode@1" ,-1 , -FDT_ERR_BADPATH ); +check_path_offset_namelen (fdt,"/subnode@1" ,0 , -FDT_ERR_BADPATH );check_path_offset_namelen (fdt,"/subnode@1" ,1 ,0 );check_path_offset_namelen (fdt,"/subnode@1/subsubnode" ,10 , subnode1_offset);check_path_offset_namelen (fdt,"/subnode@1/subsubnode" ,11 , subnode1_offset);
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