Skip to contentSkip to conversation

libfdt: Add FDT alignment check to fdt_check_header()

PATCH Applied Rob Herring 4 participants 8 messages Apr 6, 2021 – Apr 7, 2021 4 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libfdt: Add FDT alignment check to fdt_check_header()

Only checking the FDT alignment in fdt_ro_probe_() means that fdt_check_header() can pass, but then subsequent API calls fail on alignment checks. Let's add an alignment check to fdt_check_header() so alignment errors are found up front.

Cc Tom Rini <trini-OWPKS81ov/FWk0Htik3J/[email protected]>
Cc Frank Rowand <[email protected]>

---
For background, the new alignment check triggered a crash in the linux kernel. Yes, we should fix the error handling, but fdt_check_header() shouldn't tell us the FDT is valid only to fail later on.

Maybe we should move the check instead, but fdt_ro_probe_() and fdt_check_header() already have a lot of the same checks.

 libfdt/fdt.c | 4 ++++
 1 file changed, 4 insertions(+)
libfdt/fdt.c +4 -0
index 3e893073da05..9fe7cf4b747d 100644
@@ -90,6 +90,10 @@ int fdt_check_header(const void *fdt)
 {
 	size_t hdrsize;
 
+	/* 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)
 		return -FDT_ERR_BADMAGIC;
 	if (!can_assume(LATEST)) {
Signature · 1 line
2.27.0

A message that is not in the archive (sent off-list or not yet received): 20210406190712.2118098-1-robh-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org

Applied
#2 lore raw
Quoted 8 lines · Rob Herring

Only checking the FDT alignment in fdt_ro_probe_() means that fdt_check_header() can pass, but then subsequent API calls fail on alignment checks. Let's add an alignment check to fdt_check_header() so alignment errors are found up front.

Cc Tom Rini <trini-OWPKS81ov/FWk0Htik3J/[email protected]>
Cc Frank Rowand <[email protected]>

Good catch.

Applied, thanks.

Quoted 27 lines

---
For background, the new alignment check triggered a crash in the linux kernel. Yes, we should fix the error handling, but fdt_check_header() shouldn't tell us the FDT is valid only to fail later on.

Maybe we should move the check instead, but fdt_ro_probe_() and fdt_check_header() already have a lot of the same checks.

 libfdt/fdt.c | 4 ++++
 1 file changed, 4 insertions(+)
libfdt/fdt.c +4 -0
index 3e893073da05..9fe7cf4b747d 100644
@@ -90,6 +90,10 @@ int fdt_check_header(const void *fdt)
 {
 	size_t hdrsize;
 
+	/* 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)
 		return -FDT_ERR_BADMAGIC;
 	if (!can_assume(LATEST)) {
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
#3 lore raw
In reply to a message not in the archive

Hi Rob,

Quoted 15 lines · Rob Herring

Only checking the FDT alignment in fdt_ro_probe_() means that fdt_check_header() can pass, but then subsequent API calls fail on alignment checks. Let's add an alignment check to fdt_check_header() so alignment errors are found up front.

Cc Tom Rini <trini-OWPKS81ov/FWk0Htik3J/[email protected]>
Cc Frank Rowand <[email protected]>

---
For background, the new alignment check triggered a crash in the linux kernel. Yes, we should fix the error handling, but fdt_check_header() shouldn't tell us the FDT is valid only to fail later on.

Quoted 5 lines

Maybe we should move the check instead, but fdt_ro_probe_() and fdt_check_header() already have a lot of the same checks.

 libfdt/fdt.c | 4 ++++
 1 file changed, 4 insertions(+)

At present U-Boot uses a 4-byte alignment, so far as I know, so this will break things.

Is this because of the need to align the memory-reservation block?

Quoted 17 lines
libfdt/fdt.c +4 -1
index 3e893073da05..9fe7cf4b747d 100644
@@ -90,6 +90,10 @@ int fdt_check_header(const void *fdt)
 {
        size_t hdrsize;
 
+       /* 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)
                return -FDT_ERR_BADMAGIC;
        if (!can_assume(LATEST)) {
--
Quoted 2 lines

2.27.0

Regards,
Simon

A message that is not in the archive (sent off-list or not yet received): CAPnjgZ3fmzymABa4orPYxVQG4SaprSXZa+4AT9=yBYm8B6Md_Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#5 lore raw
Quoted 19 lines · Simon Glass

Hi Rob,

Quoted 15 lines · Rob Herring

Only checking the FDT alignment in fdt_ro_probe_() means that fdt_check_header() can pass, but then subsequent API calls fail on alignment checks. Let's add an alignment check to fdt_check_header() so alignment errors are found up front.

Cc Tom Rini <trini-OWPKS81ov/FWk0Htik3J/[email protected]>
Cc Frank Rowand <[email protected]>

---
For background, the new alignment check triggered a crash in the linux kernel. Yes, we should fix the error handling, but fdt_check_header() shouldn't tell us the FDT is valid only to fail later on.

Quoted 8 lines
Quoted 5 lines

Maybe we should move the check instead, but fdt_ro_probe_() and fdt_check_header() already have a lot of the same checks.

 libfdt/fdt.c | 4 ++++
 1 file changed, 4 insertions(+)

At present U-Boot uses a 4-byte alignment, so far as I know, so this will break things.

It was the u-boot folks that wanted this in the first place... Look at the recent commits from Tom and the discussion on the list about them.

Quoted 1 line

Is this because of the need to align the memory-reservation block?

But yes, the spec does require some sections to be 8-byte aligned which implies the whole thing has to be.

Rob

A message that is not in the archive (sent off-list or not yet received): CAL_JsqK+h8v2PY9W2jzoAihFwb+7T0oqkoSKrtOSf1mgYfU1mA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#7 lore raw

Hi Rob,

Quoted 24 lines · Rob Herring
Quoted 22 lines · Simon Glass

Hi Rob,

Quoted 18 lines · Rob Herring

Only checking the FDT alignment in fdt_ro_probe_() means that fdt_check_header() can pass, but then subsequent API calls fail on alignment checks. Let's add an alignment check to fdt_check_header() so alignment errors are found up front.

Cc Tom Rini <trini-OWPKS81ov/FWk0Htik3J/[email protected]>
Cc Frank Rowand <[email protected]>

---
For background, the new alignment check triggered a crash in the linux kernel. Yes, we should fix the error handling, but fdt_check_header() shouldn't tell us the FDT is valid only to fail later on.

Maybe we should move the check instead, but fdt_ro_probe_() and fdt_check_header() already have a lot of the same checks.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
 libfdt/fdt.c | 4 ++++
 1 file changed, 4 insertions(+)

At present U-Boot uses a 4-byte alignment, so far as I know, so this will break things.

It was the u-boot folks that wanted this in the first place... Look at the recent commits from Tom and the discussion on the list about them.

OK I guess I just missed that. I recall the push-back against supporting unaligned access but not the 8-byte stuff.

Quoted 5 lines
Quoted 2 lines

Is this because of the need to align the memory-reservation block?

But yes, the spec does require some sections to be 8-byte aligned which implies the whole thing has to be.

I was looking at that but from what I could tell it is not stated anywhere. In fact it is, but I missed it.

I sent:

https://github.com/devicetree-org/devicetree-specification/pull/43

Regards,
Simon

#8 lore raw
In reply to a message not in the archive
Quoted 18 lines · Simon Glass

Hi Rob,

Quoted 15 lines · Rob Herring

Only checking the FDT alignment in fdt_ro_probe_() means that fdt_check_header() can pass, but then subsequent API calls fail on alignment checks. Let's add an alignment check to fdt_check_header() so alignment errors are found up front.

Cc Tom Rini <trini-OWPKS81ov/FWk0Htik3J/[email protected]>
Cc Frank Rowand <[email protected]>

---
For background, the new alignment check triggered a crash in the linux kernel. Yes, we should fix the error handling, but fdt_check_header() shouldn't tell us the FDT is valid only to fail later on.

Quoted 8 lines
Quoted 5 lines

Maybe we should move the check instead, but fdt_ro_probe_() and fdt_check_header() already have a lot of the same checks.

 libfdt/fdt.c | 4 ++++
 1 file changed, 4 insertions(+)

At present U-Boot uses a 4-byte alignment, so far as I know, so this will break things.

It's 8 byte, not 4 byte and I have nothing good to say about places that get by with 4-and-not-8 alignment.

Signature · 1 line
Tom
#9 lore raw

Hi Tom & Rob,

Quoted 23 lines · Tom Rini
Quoted 21 lines · Simon Glass

Hi Rob,

Quoted 18 lines · Rob Herring

Only checking the FDT alignment in fdt_ro_probe_() means that fdt_check_header() can pass, but then subsequent API calls fail on alignment checks. Let's add an alignment check to fdt_check_header() so alignment errors are found up front.

Cc Tom Rini <trini-OWPKS81ov/FWk0Htik3J/[email protected]>
Cc Frank Rowand <[email protected]>

---
For background, the new alignment check triggered a crash in the linux kernel. Yes, we should fix the error handling, but fdt_check_header() shouldn't tell us the FDT is valid only to fail later on.

Maybe we should move the check instead, but fdt_ro_probe_() and fdt_check_header() already have a lot of the same checks.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
 libfdt/fdt.c | 4 ++++
 1 file changed, 4 insertions(+)

At present U-Boot uses a 4-byte alignment, so far as I know, so this will break things.

It's 8 byte, not 4 byte and I have nothing good to say about places that get by with 4-and-not-8 alignment.

I am thinking of this in arch/arm/cpu/u-boot.lds :

. = ALIGN(4);

.image_copy_end : {

At least for now, we use 4-byte alignment on 32-bit ARM, for example.

Regards,
Simon

A message that is not in the archive (sent off-list or not yet received): CAPnjgZ2rGyCVQwQ3aP+=wF99R+2C_POV1Y=18jd8eAd+sOqSZQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#11 lore raw
Quoted 35 lines · Simon Glass

Hi Tom & Rob,

Quoted 23 lines · Tom Rini
Quoted 21 lines · Simon Glass

Hi Rob,

Quoted 18 lines · Rob Herring

Only checking the FDT alignment in fdt_ro_probe_() means that fdt_check_header() can pass, but then subsequent API calls fail on alignment checks. Let's add an alignment check to fdt_check_header() so alignment errors are found up front.

Cc Tom Rini <trini-OWPKS81ov/FWk0Htik3J/[email protected]>
Cc Frank Rowand <[email protected]>

---
For background, the new alignment check triggered a crash in the linux kernel. Yes, we should fix the error handling, but fdt_check_header() shouldn't tell us the FDT is valid only to fail later on.

Maybe we should move the check instead, but fdt_ro_probe_() and fdt_check_header() already have a lot of the same checks.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
 libfdt/fdt.c | 4 ++++
 1 file changed, 4 insertions(+)

At present U-Boot uses a 4-byte alignment, so far as I know, so this will break things.

It's 8 byte, not 4 byte and I have nothing good to say about places that get by with 4-and-not-8 alignment.

Quoted 7 lines

I am thinking of this in arch/arm/cpu/u-boot.lds :

. = ALIGN(4);

.image_copy_end : {

At least for now, we use 4-byte alignment on 32-bit ARM, for example.

Lets move that over to the U-Boot list and see what needs whacking where then, keeping in mind that it's where we use the DTB once it's in memory that needs 8 byte alignment, not where it might be placed in a blob for storage.

Signature · 1 line
Tom