Skip to contentSkip to conversation

libfdt: Check for 8-byte address alignment in fdt_ro_probe_()

PATCH Applied Tom Rini 2 participants 4 messages Nov 4, 2020 – Nov 24, 2020 2 messages not in the archive

lore
Share & tools
lore

Tom Rini

author
#0 lore raw

PATCH 1/1 libfdt: Check for 8-byte address alignment in fdt_ro_probe_()

The device tree must be loaded in to memory at an 8-byte aligned address. Add a check for this condition in fdt_ro_probe_() and a new error code to return if we are not.

---
 libfdt/fdt.c    | 4 ++++
 libfdt/libfdt.h | 6 +++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
libfdt/fdt.c +4 -0
index 6cf2fa03b037..3e893073da05 100644
@@ -22,6 +22,10 @@ int32_t fdt_ro_probe_(const void *fdt)
 	if (can_assume(VALID_DTB))
 		return totalsize;
 
+	/* The device tree must be at an 8-byte aligned address */
+	if ((uintptr_t)fdt & 7)
+		return -FDT_ERR_ALIGNMENT;
+
 	if (fdt_magic(fdt) == FDT_MAGIC) {
 		/* Complete tree */
 		if (!can_assume(LATEST)) {
libfdt/libfdt.h +5 -1
index 544d3efff584..b600c8d6dd41 100644
@@ -101,7 +101,11 @@ extern "C" {
 	/* FDT_ERR_BADFLAGS: The function was passed a flags field that
 	 * contains invalid flags or an invalid combination of flags. */
 
-#define FDT_ERR_MAX		18
+#define FDT_ERR_ALIGNMENT	19
+	/* FDT_ERR_ALIGNMENT: The device tree base address is not 8-byte
+	 * aligned. */
+
+#define FDT_ERR_MAX		19
 
 /* constants */
 #define FDT_MAX_PHANDLE 0xfffffffe
Signature · 1 line
2.17.1

A message that is not in the archive (sent off-list or not yet received): 20201104130605.28874-1-trini-OWPKS81ov/FWk0Htik3J/[email protected]

author
#2 lore raw
Quoted 32 lines · Tom Rini

The device tree must be loaded in to memory at an 8-byte aligned address. Add a check for this condition in fdt_ro_probe_() and a new error code to return if we are not.

---
 libfdt/fdt.c    | 4 ++++
 libfdt/libfdt.h | 6 +++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
libfdt/fdt.c +4 -0
index 6cf2fa03b037..3e893073da05 100644
@@ -22,6 +22,10 @@ int32_t fdt_ro_probe_(const void *fdt)
 	if (can_assume(VALID_DTB))
 		return totalsize;
 
+	/* The device tree must be at an 8-byte aligned address */
+	if ((uintptr_t)fdt & 7)
+		return -FDT_ERR_ALIGNMENT;
+
 	if (fdt_magic(fdt) == FDT_MAGIC) {
 		/* Complete tree */
 		if (!can_assume(LATEST)) {
libfdt/libfdt.h +0 -0
index 544d3efff584..b600c8d6dd41 100644
@@ -101,7 +101,11 @@ extern "C" {
 	/* FDT_ERR_BADFLAGS: The function was passed a flags field that
 	 * contains invalid flags or an invalid combination of flags. */

libfdt/libfdt.h extern "C" { · line 111

Quoted 10 lines
libfdt/libfdt.h +5 -1
 
-#define FDT_ERR_MAX		18
+#define FDT_ERR_ALIGNMENT	19
+	/* FDT_ERR_ALIGNMENT: The device tree base address is not 8-byte
+	 * aligned. */
+
+#define FDT_ERR_MAX		19
 
 /* constants */
 #define FDT_MAX_PHANDLE 0xfffffffe

Any comments? Thanks.

Signature · 1 line
Tom
Applied
#3 lore raw
In reply to a message not in the archive
Quoted 5 lines · Tom Rini

The device tree must be loaded in to memory at an 8-byte aligned address. Add a check for this condition in fdt_ro_probe_() and a new error code to return if we are not.

Applied, thanks, sorry it took so long.

A testcase to make sure this fails on an unaligned blob would be a nice addition

Quoted 37 lines
---
 libfdt/fdt.c    | 4 ++++
 libfdt/libfdt.h | 6 +++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
libfdt/fdt.c +4 -0
index 6cf2fa03b037..3e893073da05 100644
@@ -22,6 +22,10 @@ int32_t fdt_ro_probe_(const void *fdt)
 	if (can_assume(VALID_DTB))
 		return totalsize;
 
+	/* The device tree must be at an 8-byte aligned address */
+	if ((uintptr_t)fdt & 7)
+		return -FDT_ERR_ALIGNMENT;
+
 	if (fdt_magic(fdt) == FDT_MAGIC) {
 		/* Complete tree */
 		if (!can_assume(LATEST)) {
libfdt/libfdt.h +5 -1
index 544d3efff584..b600c8d6dd41 100644
@@ -101,7 +101,11 @@ extern "C" {
 	/* FDT_ERR_BADFLAGS: The function was passed a flags field that
 	 * contains invalid flags or an invalid combination of flags. */
 
-#define FDT_ERR_MAX		18
+#define FDT_ERR_ALIGNMENT	19
+	/* FDT_ERR_ALIGNMENT: The device tree base address is not 8-byte
+	 * aligned. */
+
+#define FDT_ERR_MAX		19
 
 /* constants */
 #define FDT_MAX_PHANDLE 0xfffffffe
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

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#5 lore raw
Quoted 8 lines · David Gibson
Quoted 5 lines · Tom Rini

The device tree must be loaded in to memory at an 8-byte aligned address. Add a check for this condition in fdt_ro_probe_() and a new error code to return if we are not.

Applied, thanks, sorry it took so long.

Thanks.

Quoted 2 lines

A testcase to make sure this fails on an unaligned blob would be a nice addition

I think I see how that might be done, I'll put it on the TODO list.

Signature · 1 line
Tom