Skip to contentSkip to conversation

libfdt: Fix a few typos

PATCH In review Simon Glass 2 participants 5 messages Mar 2, 2020 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 libfdt: Fix a few typos

Fix 'saftey' and 'additional' typos noticed in the assumption series. Reword the ASSUME_NO_ROLLBACK slightly to improve clarity.

---

 libfdt/libfdt_internal.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
libfdt/libfdt_internal.h +3 -3
index e9913cd..3201678 100644
@@ -58,7 +58,7 @@ static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
 
 /*
  * Defines assumptions which can be enabled. Each of these can be enabled
- * individually. For maximum saftey, don't enable any assumptions!
+ * individually. For maximum safety, don't enable any assumptions!
  *
  * For minimal code size and no safety, use ASSUME_PERFECT at your own risk.
  * You should have another method of validating the device tree, such as a
@@ -121,8 +121,8 @@ enum {
 	ASSUME_LATEST		= 1 << 2,
 
 	/*
-	 * This assume that it is OK for a failed additional to the device tree
-	 * due to lack of space or some other problem can skip any rollback
+	 * This assumes that it is OK for a failed addition to the device tree,
+	 * due to lack of space or some other problem, to skip any rollback
 	 * steps (such as dropping the property name from the string table).
 	 * This is safe to enable in most circumstances, even though it may
 	 * leave the tree in a sub-optimal state.
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog

A message that is not in the archive (sent off-list or not yet received): 20200302190255.51426-1-sjg-F7+t8E8rja9g9hUCZPvPmw@public.gmane.org

#2 lore raw

PATCH 2/4 libfdt: Improve comments in some of the assumptions

Add a little more detail in a few of these comments.

---

 libfdt/libfdt_internal.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
libfdt/libfdt_internal.h +12 -3
index 3201678..7999f6a 100644
@@ -91,7 +91,9 @@ enum {
 	 *
 	 * With this assumption enabled, normal device trees produced by libfdt
 	 * and the compiler should be handled safely. Malicious device trees and
-	 * complete garbage may cause libfdt to behave badly or crash.
+	 * complete garbage may cause libfdt to behave badly or crash. Truncated
+	 * device trees (e.g. those only partially loaded) can also cause
+	 * problems.
 	 *
 	 * Note: Only checks that relate exclusively to the device tree itself
 	 * (not the parameters passed to libfdt) are disabled by this
@@ -130,8 +132,15 @@ enum {
 	ASSUME_NO_ROLLBACK	= 1 << 3,
 
 	/*
-	 * This assumes that the device tree components appear in the correct
-	 * order. As such it disables a check in fdt_open_into() and removes the
+	 * This assumes that the device tree components appear in a 'convenient'
+	 * order, i.e. the memory reservation block first, then the structure
+	 * block and finally the string block.
+	 *
+	 * This order is not specified by the device-tree specification,
+	 * but is expected by libfdt. The device-tree compiler always created
+	 * device trees with this order.
+	 *
+	 * This assumption disables a check in fdt_open_into() and removes the
 	 * ability to fix the problem there. This is safe if you know that the
 	 * device tree is correctly ordered. See fdt_blocks_misordered_().
 	 */
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#3 lore raw

PATCH 3/4 libfdt: Add support for disabling internal checks

In reply to a message not in the archive

If libfdt returns -FDT_ERR_INTERNAL that generally indicates a bug in the library. Add a new assumption for these cases since it should be save to disable these checks regardless of the input.

---
I have left other FDT_ERR_INTERNAL sites alone for now. I see that fdt_check_full() returns -FDT_ERR_INTERNAL which seems to be different from what you said about this only being for libfdt bugs.

 libfdt/fdt_ro.c          | 4 ++--
 libfdt/libfdt_internal.h | 9 +++++++++
 2 files changed, 11 insertions(+), 2 deletions(-)
libfdt/fdt_ro.c +2 -2
index 1945503..e03570a 100644
@@ -402,7 +402,7 @@ static const struct fdt_property *fdt_get_property_namelen_(const void *fdt,
 		const struct fdt_property *prop;
 
 		prop = fdt_get_property_by_offset_(fdt, offset, lenp);
-		if (!can_assume(VALID_DTB) && !prop) {
+		if (!can_assume(LIBFDT_FLAWLESS) && !prop) {
 			offset = -FDT_ERR_INTERNAL;
 			break;
 		}
@@ -634,7 +634,7 @@ int fdt_node_depth(const void *fdt, int nodeoffset)
 
 	err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
 	if (err)
-		return (can_assume(VALID_INPUT) || err < 0) ? err :
+		return (can_assume(LIBFDT_FLAWLESS) || err < 0) ? err :
 			-FDT_ERR_INTERNAL;
 	return nodedepth;
 }
libfdt/libfdt_internal.h +9 -0
index 7999f6a..d4e0bd4 100644
@@ -145,6 +145,15 @@ enum {
 	 * device tree is correctly ordered. See fdt_blocks_misordered_().
 	 */
 	ASSUME_LIBFDT_ORDER	= 1 << 4,
+
+	/*
+	 * This assumes that libfdt itself does not have any internal bugs. It
+	 * drops certain checks that should never be needed unless libfdt has an
+	 * undiscovered bug.
+	 *
+	 * This can generally be considered safe to enable.
+	 */
+	ASSUME_LIBFDT_FLAWLESS	= 1 << 5,
 };
 
 /**
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#4 lore raw

PATCH 4/4 libfdt: Use VALID_INPUT for FDT_ERR_BADSTATE checks

In reply to a message not in the archive

This error indicates a logic bug in the code calling libfdt, so VALID_DTB is not really the right check. Update it to use VALID_INPUT instead.

---

 libfdt/fdt.c    | 2 +-
 libfdt/fdt_sw.c | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)
libfdt/fdt.c +1 -1
index 4419204..c28fcc1 100644
@@ -33,7 +33,7 @@ int32_t fdt_ro_probe_(const void *fdt)
 		}
 	} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
 		/* Unfinished sequential-write blob */
-		if (fdt_size_dt_struct(fdt) == 0)
+		if (!can_assume(VALID_INPUT) && fdt_size_dt_struct(fdt) == 0)
 			return -FDT_ERR_BADSTATE;
 	} else {
 		return -FDT_ERR_BADMAGIC;
libfdt/fdt_sw.c +4 -3
index 352193c..26759d5 100644
@@ -12,7 +12,7 @@
 
 static int fdt_sw_probe_(void *fdt)
 {
-	if (!can_assume(VALID_DTB)) {
+	if (!can_assume(VALID_INPUT)) {
 		if (fdt_magic(fdt) == FDT_MAGIC)
 			return -FDT_ERR_BADSTATE;
 		else if (fdt_magic(fdt) != FDT_SW_MAGIC)
@@ -41,7 +41,7 @@ static int fdt_sw_probe_memrsv_(void *fdt)
 	if (err)
 		return err;
 
-	if (!can_assume(VALID_DTB) && fdt_off_dt_strings(fdt) != 0)
+	if (!can_assume(VALID_INPUT) && fdt_off_dt_strings(fdt) != 0)
 		return -FDT_ERR_BADSTATE;
 	return 0;
 }
@@ -67,7 +67,8 @@ static int fdt_sw_probe_struct_(void *fdt)
 	if (err)
 		return err;
 
-	if (fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
+	if (!can_assume(VALID_INPUT) &&
+	    fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
 		return -FDT_ERR_BADSTATE;
 	return 0;
 }
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#5 lore raw
In reply to a message not in the archive
Quoted 5 lines · Simon Glass

Fix 'saftey' and 'additional' typos noticed in the assumption series. Reword the ASSUME_NO_ROLLBACK slightly to improve clarity.

Series applied to master, thanks.

Quoted 29 lines

---

 libfdt/libfdt_internal.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
libfdt/libfdt_internal.h +3 -3
index e9913cd..3201678 100644
@@ -58,7 +58,7 @@ static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
 
 /*
  * Defines assumptions which can be enabled. Each of these can be enabled
- * individually. For maximum saftey, don't enable any assumptions!
+ * individually. For maximum safety, don't enable any assumptions!
  *
  * For minimal code size and no safety, use ASSUME_PERFECT at your own risk.
  * You should have another method of validating the device tree, such as a
@@ -121,8 +121,8 @@ enum {
 	ASSUME_LATEST		= 1 << 2,
 
 	/*
-	 * This assume that it is OK for a failed additional to the device tree
-	 * due to lack of space or some other problem can skip any rollback
+	 * This assumes that it is OK for a failed addition to the device tree,
+	 * due to lack of space or some other problem, to skip any rollback
 	 * steps (such as dropping the property name from the string table).
 	 * This is safe to enable in most circumstances, even though it may
 	 * leave the tree in a sub-optimal state.
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