Skip to contentSkip to conversation

libfdt: Validate alias property value is a valid string.

PATCH In review Mike McTernan 2 participants 2 messages Oct 10, 2022 – May 14, 2023 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libfdt: Validate alias property value is a valid string.

Prevent circular alias resolution causing infinite recursion.

---
 libfdt/fdt_ro.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
libfdt/fdt_ro.c +15 -1
index 9f6c551..870c4a5 100644
@@ -525,13 +525,27 @@ uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
 const char *fdt_get_alias_namelen(const void *fdt,
 				  const char *name, int namelen)
 {
+	const char *prop;
 	int aliasoffset;
+	int prop_len;
 
 	aliasoffset = fdt_path_offset(fdt, "/aliases");
 	if (aliasoffset < 0)
 		return NULL;
 
-	return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, 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 < 2 || *prop != '/' || strnlen(prop, prop_len) != prop_len - 1) {
+			prop = NULL;
+		}
+	}
+
+	return prop;
 }
 
 const char *fdt_get_alias(const void *fdt, const char *name)
Signature · 1 line
2.38.0.rc2.412.g84df46c1b4-goog

A message that is not in the archive (sent off-list or not yet received): 20221010100328.2207018-1-mikemcternan-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org

#2 lore raw
Quoted 1 line · Mike McTernan

Prevent circular alias resolution causing infinite recursion.

Sorry, I've neglected this forever. Unfortunately, I'm not really sure what to do with it. On the one hand, this does prevent infinite recursion which supports the libfdt design goal of being robust against being given bad trees.

However, although the modern device spec does say "full path" (although I'm not sure it spells out exactly what that means), I have a fairly strong memory that IEEE 1275 did allow aliases to reference one another. Ideally I'd like libfdt to be able to handle trees which are transcribed from Open Firmware as well as more modern dtbs created with dtc.

Quoted 38 lines
---
 libfdt/fdt_ro.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
libfdt/fdt_ro.c +15 -1
index 9f6c551..870c4a5 100644
@@ -525,13 +525,27 @@ uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
 const char *fdt_get_alias_namelen(const void *fdt,
 				  const char *name, int namelen)
 {
+	const char *prop;
 	int aliasoffset;
+	int prop_len;
 
 	aliasoffset = fdt_path_offset(fdt, "/aliases");
 	if (aliasoffset < 0)
 		return NULL;
 
-	return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, 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 < 2 || *prop != '/' || strnlen(prop, prop_len) != prop_len - 1) {
+			prop = NULL;
+		}
+	}
+
+	return prop;
 }
 
 const char *fdt_get_alias(const void *fdt, const char *name)
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