Skip to contentSkip to conversation

libfdt: fdt_get_alias_namelen: Validate aliases

PATCH Superseded Pierre-Clément Tosi 2 participants 4 messages Oct 9, 2023 – Oct 11, 2023

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libfdt: fdt_get_alias_namelen: Validate aliases

Ensure that the alias found matches the device tree specification v0.4:

Each property of the /aliases node defines an alias. The property name specifies the alias name. The property value specifies the full path to a node in the devicetree.

This protects against a stack overflow caused by

fdt_path_offset_namelen(fdt, path, namelen)

calling (if 'path' contains no '/')

fdt_path_offset(fdt, fdt_get_alias_namelen(fdt, path, namelen))

leading to infinite recursion on DTs with "circular" aliases.

This fix was originally written by Mike McTernan for Android in [1].

[1]: https://android.googlesource.com/platform/external/dtc/+/9308e7f9772bd226fea9925b1fc4d53c127ed4d5

---
v2
- replace memchr('/') with check on last character
- add test coverage of a self-referencing alias
- drop redundant test case on alias to non-absolute path
- reference the DT spec and AOSP patch in the commit message
- rephrase the infinite recursion case in the commit message

---
 libfdt/fdt_ro.c   | 11 ++++++++++-
 tests/aliases.dts |  4 ++++
 tests/get_alias.c | 14 +++++++++++++-
 3 files changed, 27 insertions(+), 2 deletions(-)
libfdt/fdt_ro.c +10 -1
index c4c520c..39b7c68 100644
@@ -537,7 +537,16 @@ static const void *fdt_path_getprop_namelen(const void *fdt, const char *path,
 const char *fdt_get_alias_namelen(const void *fdt,
 				  const char *name, int namelen)
 {
-	return fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, NULL);
+	int len;
+	const char *alias;
+
+	alias = fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, &len);
+
+	if (!can_assume(VALID_DTB) &&
+	    !(len > 0 && alias && alias[len - 1] == '\0' && *alias == '/'))
+		return NULL;
+
+	return alias;
 }
 
 const char *fdt_get_alias(const void *fdt, const char *name)
tests/aliases.dts +4 -0
index 853479a..b880176 100644
@@ -5,6 +5,10 @@
 	#size-cells = <0>;
 
 	aliases {
+		empty = "";
+		loop = "loop";
+		nonull = [626164];
+		relative = "rel/at/ive";
 		s1 = &sub1;
 		ss1 = &subsub1;
 		sss1 = &subsubsub1;
tests/get_alias.c +13 -1
index fb2c38c..d2888d6 100644
@@ -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();
 }
Signature · 5 lines
2.42.0.609.gbb76f46606-goog


-- 
Pierre
#1 lore raw
Quoted 11 lines · Pierre-Clément Tosi

Ensure that the alias found matches the device tree specification v0.4:

Each property of the /aliases node defines an alias. The property name specifies the alias name. The property value specifies the full path to a node in the devicetree.

This protects against a stack overflow caused by

fdt_path_offset_namelen(fdt, path, namelen)

calling (if 'path' contains no '/')

Uh.. this still seems confusing, or at least misleadingly specific. Having a self-referential alias doesn't really have anything to do with whether the path has any '/' or not.

Quoted 5 lines

fdt_path_offset(fdt, fdt_get_alias_namelen(fdt, path, namelen))

leading to infinite recursion on DTs with "circular" aliases.

This fix was originally written by Mike McTernan for Android in [1].

Urgh... I don't love the idea of merging something that doesn't have a Signed-off from the original author. I guess it's probably ok with something this small.

Quoted 23 lines

[1]: https://android.googlesource.com/platform/external/dtc/+/9308e7f9772bd226fea9925b1fc4d53c127ed4d5

---
v2
- replace memchr('/') with check on last character
- add test coverage of a self-referencing alias
- drop redundant test case on alias to non-absolute path
- reference the DT spec and AOSP patch in the commit message
- rephrase the infinite recursion case in the commit message

---
 libfdt/fdt_ro.c   | 11 ++++++++++-
 tests/aliases.dts |  4 ++++
 tests/get_alias.c | 14 +++++++++++++-
 3 files changed, 27 insertions(+), 2 deletions(-)
libfdt/fdt_ro.c +0 -0
index c4c520c..39b7c68 100644
@@ -537,7 +537,16 @@ static const void *fdt_path_getprop_namelen(const void *fdt, const char *path,
 const char *fdt_get_alias_namelen(const void *fdt,

libfdt/fdt_ro.c static const void *fdt_path_getprop_namelen(const void *fdt, const char *path, · line 546

Quoted 10 lines
libfdt/fdt_ro.c +7 -1
 				  const char *name, int namelen)
 {
-	return fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, NULL);
+	int len;
+	const char *alias;
+
+	alias = fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, &len);
+
+	if (!can_assume(VALID_DTB) &&
+	    !(len > 0 && alias && alias[len - 1] == '\0' && *alias == '/'))

I'd be more confortable with the alias test before the len > 0 test. It's probably fine either way: if !alias, then len should be an error code < 0. However, the point is len has a different interpretation depending on whether alias is NULL or not-NULL, making (len > 0) a slightly ambiguous statement if we haven't already established which case we're in.

Quoted 11 lines
libfdt/fdt_ro.c +3 -0
+		return NULL;
+
+	return alias;
 }
 
 const char *fdt_get_alias(const void *fdt, const char *name)
tests/aliases.dts +0 -0
index 853479a..b880176 100644

tests/aliases.dts line 11

Quoted 7 lines
tests/aliases.dts +4 -0
@@ -5,6 +5,10 @@
 	#size-cells = <0>;
 
 	aliases {
+		empty = "";
+		loop = "loop";
+		nonull = [626164];
+		relative = "rel/at/ive";

Since you're only testing fdt_get_alias() here, rather than the full path_offset(), this is probably ok, but there is a bit of ambuguity here in what's wrong with this. What you're testing here is that this is disallowed as an alias-to-an-alias. But if you were resolving this with path_offset(), you'd expect NOTFOUND even if aliases-to-aliases were allowed, both since the alias it's relative to doesn't exist, and the rest of the path won't resolve anyway.

I think it would be clearer and more robust to use a case here where the *only* thing wrong with the alias is that it involves another alias. e.g.

relative = "s1/subsubnode"

That would be an alias resolving to the same thing as 'ss1', if it were not for the alias-to-an-alias prohibition.

Quoted 40 lines
tests/aliases.dts +0 -0
 		s1 = &sub1;
 		ss1 = &subsub1;
 		sss1 = &subsubsub1;
tests/get_alias.c +13 -1
index fb2c38c..d2888d6 100644
@@ -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();
 }
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
#2 lore raw
Quoted 9 lines · David Gibson
Quoted 8 lines · Pierre-Clément Tosi

Ensure that the alias found matches the device tree specification v0.4:

Each property of the /aliases node defines an alias. The property name specifies the alias name. The property value specifies the full path to a node in the devicetree.

This protects against a stack overflow caused by

Quoted 7 lines
Quoted 3 lines

fdt_path_offset_namelen(fdt, path, namelen)

calling (if 'path' contains no '/')

Uh.. this still seems confusing, or at least misleadingly specific. Having a self-referential alias doesn't really have anything to do with whether the path has any '/' or not.

Because, even if fdt_path_offset() is called with a path containing one or more '/', the recursion will result in a fdt_path_offset() call with a path that doesn't have one, right? Good point, I've removed that condition from the commit message in v3.

Quoted 43 lines
Quoted 6 lines

fdt_path_offset(fdt, fdt_get_alias_namelen(fdt, path, namelen))

leading to infinite recursion on DTs with "circular" aliases.

This fix was originally written by Mike McTernan for Android in [1].

Urgh... I don't love the idea of merging something that doesn't have a Signed-off from the original author. I guess it's probably ok with something this small.

Quoted 32 lines

[1]: https://android.googlesource.com/platform/external/dtc/+/9308e7f9772bd226fea9925b1fc4d53c127ed4d5

---
v2
- replace memchr('/') with check on last character
- add test coverage of a self-referencing alias
- drop redundant test case on alias to non-absolute path
- reference the DT spec and AOSP patch in the commit message
- rephrase the infinite recursion case in the commit message

---
 libfdt/fdt_ro.c   | 11 ++++++++++-
 tests/aliases.dts |  4 ++++
 tests/get_alias.c | 14 +++++++++++++-
 3 files changed, 27 insertions(+), 2 deletions(-)
libfdt/fdt_ro.c +6 -1
index c4c520c..39b7c68 100644
@@ -537,7 +537,16 @@ static const void *fdt_path_getprop_namelen(const void *fdt, const char *path,
 const char *fdt_get_alias_namelen(const void *fdt,
 				  const char *name, int namelen)
 {
-	return fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, NULL);
+	int len;
+	const char *alias;
+
+	alias = fdt_path_getprop_namelen(fdt, "/aliases", name, namelen, &len);
+
+	if (!can_assume(VALID_DTB) &&
Quoted 8 lines

libfdt/fdt_ro.c static const void *fdt_path_getprop_namelen(const void *fdt, const char *path, · line 546

Quoted 1 line
libfdt/fdt_ro.c +1 -0
+	    !(len > 0 && alias && alias[len - 1] == '\0' && *alias == '/'))

I'd be more confortable with the alias test before the len > 0 test. It's probably fine either way: if !alias, then len should be an error code < 0. However, the point is len has a different interpretation depending on whether alias is NULL or not-NULL, making (len > 0) a slightly ambiguous statement if we haven't already established which case we're in.

Makes sense, done.

Quoted 28 lines
Quoted 12 lines
libfdt/fdt_ro.c +3 -0
+		return NULL;
+
+	return alias;
 }
 
 const char *fdt_get_alias(const void *fdt, const char *name)
tests/aliases.dts +0 -0
index 853479a..b880176 100644

tests/aliases.dts line 11

Quoted 7 lines
tests/aliases.dts +4 -0
@@ -5,6 +5,10 @@
 	#size-cells = <0>;
 
 	aliases {
+		empty = "";
+		loop = "loop";
+		nonull = [626164];
+		relative = "rel/at/ive";

Since you're only testing fdt_get_alias() here, rather than the full path_offset(), this is probably ok, but there is a bit of ambuguity here in what's wrong with this. What you're testing here is that this is disallowed as an alias-to-an-alias. But if you were resolving this with path_offset(), you'd expect NOTFOUND even if aliases-to-aliases were allowed, both since the alias it's relative to doesn't exist, and the rest of the path won't resolve anyway.

Quoted 8 lines

I think it would be clearer and more robust to use a case here where the *only* thing wrong with the alias is that it involves another alias. e.g.

relative = "s1/subsubnode"

That would be an alias resolving to the same thing as 'ss1', if it were not for the alias-to-an-alias prohibition.

Thanks, that's a much better test case.

Quoted 47 lines
Quoted 41 lines
tests/aliases.dts +0 -0
 		s1 = &sub1;
 		ss1 = &subsub1;
 		sss1 = &subsubsub1;
tests/get_alias.c +13 -1
index fb2c38c..d2888d6 100644
@@ -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();
 }
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
Signature · 1 line
Pierre
#3 lore raw
Quoted 14 lines · Pierre-Clément Tosi
Quoted 13 lines · David Gibson
Quoted 11 lines · Pierre-Clément Tosi

Ensure that the alias found matches the device tree specification v0.4:

Each property of the /aliases node defines an alias. The property name specifies the alias name. The property value specifies the full path to a node in the devicetree.

This protects against a stack overflow caused by

fdt_path_offset_namelen(fdt, path, namelen)

calling (if 'path' contains no '/')

Quoted 7 lines
Quoted 3 lines

Uh.. this still seems confusing, or at least misleadingly specific. Having a self-referential alias doesn't really have anything to do with whether the path has any '/' or not.

Because, even if fdt_path_offset() is called with a path containing one or more '/', the recursion will result in a fdt_path_offset() call with a path that doesn't have one, right?

Well, not necessarily, but you can get a loop even without that. Most trivially with:

	aliases {
		loop = "loop/some/path/or/other"
	};

As long as the first call to fdt_path_offset() has a '/' in it, so will every subsequent one, but you'll still get infinite recursion trying to resolve 'loop'.

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