Merge changes I1e5a8903,I43c6ad22,If9a102a6,I702e619c into main

* changes:
  FROMGIT: libfdt: fdt_get_alias_namelen: Validate aliases
  FROMGIT: use fdt_path_getprop_namelen() in fdt_get_alias_namelen()
  FROMGIT: add fdt_path_getprop_namelen() helper
  ANDROID: Revert "libfdt: Validate alias property value is a valid string."
This commit is contained in:
Pierre-Clément Tosi
2023-10-12 12:26:27 +00:00
committed by Gerrit Code Review
3 changed files with 36 additions and 27 deletions

View File

@@ -10,14 +10,6 @@
#include "libfdt_internal.h"
/* Check if a buffer contains a nul-terminated string.
* Used for checking property values which should be strings.
*/
static bool is_nul_string(const char *buf, const size_t buf_len) {
return buf_len > 0 && buf[buf_len - 1] == '\0' &&
strnlen(buf, buf_len) == buf_len - 1;
}
static int fdt_nodename_eq_(const void *fdt, int offset,
const char *s, int len)
{
@@ -533,30 +525,31 @@ uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
return fdt32_ld_(php);
}
static const void *fdt_path_getprop_namelen(const void *fdt, const char *path,
const char *propname, int propnamelen,
int *lenp)
{
int offset = fdt_path_offset(fdt, path);
if (offset < 0)
return NULL;
return fdt_getprop_namelen(fdt, offset, propname, propnamelen, lenp);
}
const char *fdt_get_alias_namelen(const void *fdt,
const char *name, int namelen)
{
const char *prop;
int aliasoffset;
int prop_len;
int len;
const char *alias;
aliasoffset = fdt_path_offset(fdt, "/aliases");
if (aliasoffset < 0)
alias = fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, &len);
if (!can_assume(VALID_DTB) &&
!(alias && len > 0 && alias[len - 1] == '\0' && *alias == '/'))
return NULL;
prop = fdt_getprop_namelen(fdt, aliasoffset, name, namelen, &prop_len);
if (prop && !can_assume(VALID_INPUT)) {
/* Validate the alias value. From the devicetree spec v0.3:
* "An alias value is a device path and is encoded as a string.
* The value representes the full path to a node, ..."
* A full path must start at the root to prevent recursion.
*/
if (prop_len == 0 || *prop != '/' || !is_nul_string(prop, prop_len)) {
prop = NULL;
}
}
return prop;
return alias;
}
const char *fdt_get_alias(const void *fdt, const char *name)

View File

@@ -5,6 +5,10 @@
#size-cells = <0>;
aliases {
empty = "";
loop = "loop";
nonull = [626164];
relative = "s1/subsubnode";
s1 = &sub1;
ss1 = &subsub1;
sss1 = &subsubsub1;

View File

@@ -21,9 +21,16 @@ static void check_alias(void *fdt, const char *path, const char *alias)
aliaspath = fdt_get_alias(fdt, alias);
if (path && !aliaspath)
if (!path && !aliaspath)
return;
if (!aliaspath)
FAIL("fdt_get_alias(%s) failed\n", alias);
if (!path)
FAIL("fdt_get_alias(%s) returned %s instead of NULL",
alias, aliaspath);
if (strcmp(aliaspath, path) != 0)
FAIL("fdt_get_alias(%s) returned %s instead of %s\n",
alias, aliaspath, path);
@@ -36,9 +43,14 @@ int main(int argc, char *argv[])
test_init(argc, argv);
fdt = load_blob_arg(argc, argv);
check_alias(fdt, NULL, "empty");
check_alias(fdt, NULL, "nonull");
check_alias(fdt, NULL, "relative");
check_alias(fdt, "/subnode@1", "s1");
check_alias(fdt, "/subnode@1/subsubnode", "ss1");
check_alias(fdt, "/subnode@1/subsubnode/subsubsubnode", "sss1");
check_alias(fdt, NULL, "loop"); // Might trigger a stack overflow
PASS();
}