Skip to contentSkip to conversation

fdtdec: protect against another NULL phandlep in fdtdec_add_reserved_memory()

PATCH Superseded Heiko Stuebner 3 participants 9 messages Oct 8, 2019 – Oct 22, 2019 Library code

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 fdtdec: protect against another NULL phandlep in fdtdec_add_reserved_memory()

The change adding fdtdec_add_reserved_memory() already protected the added phandle against the phandlep being NULL - making the phandlep var optional.

But in the early code checking for an already existing carveout this check was not done and thus the phandle assignment could run into trouble, so add a check there as well, which makes the function still return sucessfully if a matching region is found, even though no-one wants to work with the phandle.

Fixes c9222a08b3f7 ("fdtdec: Implement fdtdec_add_reserved_memory()")
---
 lib/fdtdec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/fdtdec.c +2 -1
index 74525c84e7..17455c5506 100644
@@ -1363,7 +1363,8 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename,
 		}
 
 		if (addr == carveout->start && (addr + size) == carveout->end) {
-			*phandlep = fdt_get_phandle(blob, node);
+			if (phandlep)
+				*phandlep = fdt_get_phandle(blob, node);
 			return 0;
 		}
 	}
Signature · 1 line
2.23.0
#1 lore raw

PATCH 2/3 fdtdec: only create phandle if caller wants it in fdtdec_add_reserved_memory()

The phandlep pointer returning the phandle to the caller is optional and if it is not set when calling fdtdec_add_reserved_memory() it is highly likely that the caller is not interested in a phandle to the created reserved-memory area and really just wants that area added.

So just don't create a phandle in that case.

---
 lib/fdtdec.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
lib/fdtdec.c +9 -7
index 17455c5506..39e87e89c9 100644
@@ -1393,13 +1393,15 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename,
 	if (node < 0)
 		return node;
 
-	err = fdt_generate_phandle(blob, &phandle);
-	if (err < 0)
-		return err;
-
-	err = fdtdec_set_phandle(blob, node, phandle);
-	if (err < 0)
-		return err;
+	if (phandlep) {
+		err = fdt_generate_phandle(blob, &phandle);
+		if (err < 0)
+			return err;
+
+		err = fdtdec_set_phandle(blob, node, phandle);
+		if (err < 0)
+			return err;
+	}
 
 	/* store one or two address cells */
 	if (na > 1)
Signature · 1 line
2.23.0
#2 lore raw

PATCH 2/3 fdtdec: only create phandle if caller wants it in fdtdec_add_reserved_memory()

Hi Heiko,

Quoted 7 lines · Heiko Stuebner

The phandlep pointer returning the phandle to the caller is optional and if it is not set when calling fdtdec_add_reserved_memory() it is highly likely that the caller is not interested in a phandle to the created reserved-memory area and really just wants that area added.

So just don't create a phandle in that case.

Update function comment?

Quoted 5 lines
---
 lib/fdtdec.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

Regards,
Simon

#3 lore raw

PATCH 3/3 image: fdt: copy possible optee nodes to a loaded devicetree

In reply to Heiko Stuebner

The loading convention for optee or any other tee on arm64 is as bl32 parameter to the trusted-firmware. So TF-A gets invoked with the TEE as bl32 and main u-boot as bl33. Once it has done its startup TF-A jumps into the bl32 for the TEE startup, returns to TF-A and then jumps to bl33.

All of them get passed a devicetree as parameter and all components often get loaded from a FIT image.

OP-TEE will create additional nodes in that devicetree namely a firmware node and possibly multiple reserved-memory nodes.

While this devicetree is used in main u-boot, in most cases it won't be the one passed to the actual kernel. Instead most boot commands will load a new devicetree from somewhere like mass storage of the network, so if that happens u-boot should transfer the optee nodes to that new devicetree.

To make that happen introduce optee_copy_fdt_nodes() called from the dt setup function in image-fdt which after checking for the optee presence in the u-boot dt will make sure a optee node is present in the kernel dt and transfer any reserved-memory regions it can find.

---
This goes together with my bl32 work for the spl_atf loader in
https://patchwork.ozlabs.org/patch/1172565/

 common/image-fdt.c  |   8 ++++
 include/tee/optee.h |   9 ++++
 lib/optee/optee.c   | 112 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+)
common/image-fdt.c +8 -0
index 4247dcee0c..48388488d9 100644
@@ -17,6 +17,7 @@
 #include <linux/libfdt.h>
 #include <mapmem.h>
 #include <asm/io.h>
+#include <tee/optee.h>
 
 #ifndef CONFIG_SYS_FDT_PAD
 #define CONFIG_SYS_FDT_PAD 0x3000
@@ -561,6 +562,13 @@ int image_setup_libfdt(bootm_headers_t *images, void *blob,
 		}
 	}
 
+	fdt_ret = optee_copy_fdt_nodes(gd->fdt_blob, blob);
+	if (fdt_ret) {
+		printf("ERROR: transfer of optee nodes to new fdt failed: %s\n",
+		       fdt_strerror(fdt_ret));
+		goto err;
+	}
+
 	/* Delete the old LMB reservation */
 	if (lmb)
 		lmb_free(lmb, (phys_addr_t)(u32)(uintptr_t)blob,
include/tee/optee.h +9 -0
index 9446928fd4..121b30a303 100644
@@ -67,4 +67,13 @@ static inline int optee_verify_bootm_image(unsigned long image_addr,
 }
 #endif
 
+#if defined(CONFIG_OPTEE) && defined(CONFIG_OF_LIBFDT)
+int optee_copy_fdt_nodes(const void *old_blob, void *new_blob);
+#else
+static inline int optee_copy_fdt_nodes(const void *old_blob, void *new_blob)
+{
+	return 0;
+}
+#endif
+
 #endif /* _OPTEE_H */
lib/optee/optee.c +112 -0

121 lines not on this page · raw message

Signature · 1 line
2.23.0
#4 lore raw

PATCH 3/3 image: fdt: copy possible optee nodes to a loaded devicetree

Hi Heiko,

Quoted 23 lines · Heiko Stuebner

The loading convention for optee or any other tee on arm64 is as bl32 parameter to the trusted-firmware. So TF-A gets invoked with the TEE as bl32 and main u-boot as bl33. Once it has done its startup TF-A jumps into the bl32 for the TEE startup, returns to TF-A and then jumps to bl33.

All of them get passed a devicetree as parameter and all components often get loaded from a FIT image.

OP-TEE will create additional nodes in that devicetree namely a firmware node and possibly multiple reserved-memory nodes.

While this devicetree is used in main u-boot, in most cases it won't be the one passed to the actual kernel. Instead most boot commands will load a new devicetree from somewhere like mass storage of the network, so if that happens u-boot should transfer the optee nodes to that new devicetree.

To make that happen introduce optee_copy_fdt_nodes() called from the dt setup function in image-fdt which after checking for the optee presence in the u-boot dt will make sure a optee node is present in the kernel dt and transfer any reserved-memory regions it can find.

Quoted 8 lines

---
This goes together with my bl32 work for the spl_atf loader in
https://patchwork.ozlabs.org/patch/1172565/

 common/image-fdt.c  |   8 ++++
 include/tee/optee.h |   9 ++++
 lib/optee/optee.c   | 112 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+)

Could we please get a test for this new functionality?

Regards,
Simon

#5 lore raw

PATCH 3/3 image: fdt: copy possible optee nodes to a loaded devicetree

Hi Simon,

Am Dienstag, 22. Oktober 2019, 02:17:00 CEST schrieb Simon Glass:

Quoted 28 lines · Heiko Stuebner
Quoted 27 lines

The loading convention for optee or any other tee on arm64 is as bl32 parameter to the trusted-firmware. So TF-A gets invoked with the TEE as bl32 and main u-boot as bl33. Once it has done its startup TF-A jumps into the bl32 for the TEE startup, returns to TF-A and then jumps to bl33.

All of them get passed a devicetree as parameter and all components often get loaded from a FIT image.

OP-TEE will create additional nodes in that devicetree namely a firmware node and possibly multiple reserved-memory nodes.

While this devicetree is used in main u-boot, in most cases it won't be the one passed to the actual kernel. Instead most boot commands will load a new devicetree from somewhere like mass storage of the network, so if that happens u-boot should transfer the optee nodes to that new devicetree.

To make that happen introduce optee_copy_fdt_nodes() called from the dt setup function in image-fdt which after checking for the optee presence in the u-boot dt will make sure a optee node is present in the kernel dt and transfer any reserved-memory regions it can find.

---
This goes together with my bl32 work for the spl_atf loader in
https://patchwork.ozlabs.org/patch/1172565/

Quoted 6 lines
Quoted 4 lines
 common/image-fdt.c  |   8 ++++
 include/tee/optee.h |   9 ++++
 lib/optee/optee.c   | 112 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+)

Could we please get a test for this new functionality?

We can try ;-) , but I think I'll need some pointers how this should be done.

On first glance, test/overlay/* seems to be a good inspiration. Aka the new function relies on an opaque OP-TEE binary modifying the .itb's dt-blob between SPL and main-U-Boot, so working with stub devicetrees for testing seems reasonable.

Aka checking cases of optee-nodes in old_fdt and not having them.

Looking at the README though points me to "TODO: convert to pytest" it seems. Though that README.md only talks about interacting with the u-boot console, so I'm not sure how that should work.

Thanks
Heiko

#6 lore raw

PATCH 3/3 image: fdt: copy possible optee nodes to a loaded devicetree

Hi Simon,

Am Dienstag, 22. Oktober 2019, 14:08:04 CEST schrieb Heiko Stübner:

Quoted 36 lines

Am Dienstag, 22. Oktober 2019, 02:17:00 CEST schrieb Simon Glass:

Quoted 28 lines · Heiko Stuebner
Quoted 27 lines

The loading convention for optee or any other tee on arm64 is as bl32 parameter to the trusted-firmware. So TF-A gets invoked with the TEE as bl32 and main u-boot as bl33. Once it has done its startup TF-A jumps into the bl32 for the TEE startup, returns to TF-A and then jumps to bl33.

All of them get passed a devicetree as parameter and all components often get loaded from a FIT image.

OP-TEE will create additional nodes in that devicetree namely a firmware node and possibly multiple reserved-memory nodes.

While this devicetree is used in main u-boot, in most cases it won't be the one passed to the actual kernel. Instead most boot commands will load a new devicetree from somewhere like mass storage of the network, so if that happens u-boot should transfer the optee nodes to that new devicetree.

To make that happen introduce optee_copy_fdt_nodes() called from the dt setup function in image-fdt which after checking for the optee presence in the u-boot dt will make sure a optee node is present in the kernel dt and transfer any reserved-memory regions it can find.

---
This goes together with my bl32 work for the spl_atf loader in
https://patchwork.ozlabs.org/patch/1172565/

Quoted 6 lines
Quoted 4 lines
 common/image-fdt.c  |   8 ++++
 include/tee/optee.h |   9 ++++
 lib/optee/optee.c   | 112 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+)

Could we please get a test for this new functionality?

Quoted 8 lines

We can try ;-) , but I think I'll need some pointers how this should be done.

On first glance, test/overlay/* seems to be a good inspiration. Aka the new function relies on an opaque OP-TEE binary modifying the .itb's dt-blob between SPL and main-U-Boot, so working with stub devicetrees for testing seems reasonable.

Aka checking cases of optee-nodes in old_fdt and not having them.

I just did go ahead and implemented a test like that, see v2 series. Is this what you had in mind?

Heiko

#7 lore raw

PATCH 3/3 image: fdt: copy possible optee nodes to a loaded devicetree

In reply to Heiko Stuebner

Hi Heiko,

[+Igor]

Quoted 136 lines · Heiko Stuebner

The loading convention for optee or any other tee on arm64 is as bl32 parameter to the trusted-firmware. So TF-A gets invoked with the TEE as bl32 and main u-boot as bl33. Once it has done its startup TF-A jumps into the bl32 for the TEE startup, returns to TF-A and then jumps to bl33.

All of them get passed a devicetree as parameter and all components often get loaded from a FIT image.

OP-TEE will create additional nodes in that devicetree namely a firmware node and possibly multiple reserved-memory nodes.

While this devicetree is used in main u-boot, in most cases it won't be the one passed to the actual kernel. Instead most boot commands will load a new devicetree from somewhere like mass storage of the network, so if that happens u-boot should transfer the optee nodes to that new devicetree.

To make that happen introduce optee_copy_fdt_nodes() called from the dt setup function in image-fdt which after checking for the optee presence in the u-boot dt will make sure a optee node is present in the kernel dt and transfer any reserved-memory regions it can find.

---
This goes together with my bl32 work for the spl_atf loader in
https://patchwork.ozlabs.org/patch/1172565/

 common/image-fdt.c  |   8 ++++
 include/tee/optee.h |   9 ++++
 lib/optee/optee.c   | 112 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 129 insertions(+)
common/image-fdt.c +8 -0
index 4247dcee0c..48388488d9 100644
@@ -17,6 +17,7 @@
 #include <linux/libfdt.h>
 #include <mapmem.h>
 #include <asm/io.h>
+#include <tee/optee.h>
 
 #ifndef CONFIG_SYS_FDT_PAD
 #define CONFIG_SYS_FDT_PAD 0x3000
@@ -561,6 +562,13 @@ int image_setup_libfdt(bootm_headers_t *images, void *blob,
                }
        }
 
+       fdt_ret = optee_copy_fdt_nodes(gd->fdt_blob, blob);
+       if (fdt_ret) {
+               printf("ERROR: transfer of optee nodes to new fdt failed: %s\n",
+                      fdt_strerror(fdt_ret));
+               goto err;
+       }
+
        /* Delete the old LMB reservation */
        if (lmb)
                lmb_free(lmb, (phys_addr_t)(u32)(uintptr_t)blob,
include/tee/optee.h +9 -0
index 9446928fd4..121b30a303 100644
@@ -67,4 +67,13 @@ static inline int optee_verify_bootm_image(unsigned long image_addr,
 }
 #endif
 
+#if defined(CONFIG_OPTEE) && defined(CONFIG_OF_LIBFDT)
+int optee_copy_fdt_nodes(const void *old_blob, void *new_blob);
+#else
+static inline int optee_copy_fdt_nodes(const void *old_blob, void *new_blob)
+{
+       return 0;
+}
+#endif
+
 #endif /* _OPTEE_H */
lib/optee/optee.c +45 -0
index db92cd9af2..f484b12e67 100644
@@ -5,6 +5,8 @@
  */
 
 #include <common.h>
+#include <malloc.h>
+#include <linux/libfdt.h>
 #include <tee/optee.h>
 
 #define optee_hdr_err_msg \
@@ -63,3 +65,113 @@ error:
 
        return ret;
 }
+
+#if defined(CONFIG_OF_LIBFDT)
+static int optee_add_firmware_node(void *fdt_blob)
+{
+       int offs, ret;
+
+       if (fdt_path_offset(fdt_blob, "/firmware/optee") >= 0) {
+               debug("OP-TEE Device Tree node already exists");
+               return 0;
+       }
+
+       offs = fdt_path_offset(fdt_blob, "/firmware");
+       if (offs < 0) {
+               offs = fdt_path_offset(fdt_blob, "/");
+               if (offs < 0)
+                       return offs;
+
+               offs = fdt_add_subnode(fdt_blob, offs, "firmware");
+               if (offs < 0)
+                       return offs;
+       }
+
+       offs = fdt_add_subnode(fdt_blob, offs, "optee");
+       if (offs < 0)
+               return ret;
+
+       ret = fdt_setprop_string(fdt_blob, offs, "compatible",
+                                "linaro,optee-tz");
+       if (ret < 0)
+               return ret;
+
+       ret = fdt_setprop_string(fdt_blob, offs, "method", "smc");
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+int optee_copy_fdt_nodes(const void *old_blob, void *new_blob)
+{
+       int nodeoffset, subnode, ret;
+       struct fdt_resource res;
+

lib/optee/optee.c error: · line 120

Quoted 10 lines
lib/optee/optee.c +10 -0
+       if (fdt_check_header(old_blob))
+               return -EINVAL;
+
+       /* only proceed if there is an /firmware/optee node */
+       if (fdt_path_offset(old_blob, "/firmware/optee") < 0) {
+               debug("No OP-TEE firmware node in old fdt, nothing to do");
+               return 0;
+       }
+
+       ret = optee_add_firmware_node(new_blob);

I think it's more safe to copy the OP-TEE node instead. The values of "compatible" and "method" may be different on other platforms, "hvc" instead of "smc" for instance.

Thanks,
Jens

Quoted 60 lines
lib/optee/optee.c +57 -1
+       if (ret < 0) {
+               printf("Failed to add OP-TEE firmware node\n");
+               return ret;
+       }
+
+       /* optee inserts its memory regions as reserved-memory nodes */
+       nodeoffset = fdt_subnode_offset(old_blob, 0, "reserved-memory");
+       if (nodeoffset >= 0) {
+               subnode = fdt_first_subnode(old_blob, nodeoffset);
+               while (subnode >= 0) {
+                       const char *name = fdt_get_name(old_blob,
+                                                       subnode, NULL);
+                       if (!name)
+                               return -EINVAL;
+
+                       /* only handle optee reservations */
+                       if (strncmp(name, "optee", 5))
+                               continue;
+
+                       /* check if this subnode has a reg property */
+                       ret = fdt_get_resource(old_blob, subnode, "reg", 0,
+                                              &res);
+                       if (!ret) {
+                               struct fdt_memory carveout = {
+                                       .start = res.start,
+                                       .end = res.end,
+                               };
+                               char *oldname, *nodename, *tmp;
+
+                               oldname = strdup(name);
+                               if (!oldname)
+                                       return -ENOMEM;
+
+                               tmp = oldname;
+                               nodename = strsep(&tmp, "@");
+                               if (!nodename) {
+                                       free(oldname);
+                                       return -EINVAL;
+                               }
+
+                               ret = fdtdec_add_reserved_memory(new_blob,
+                                                                nodename,
+                                                                &carveout,
+                                                                NULL);
+                               free(oldname);
+
+                               if (ret < 0)
+                                       return ret;
+                       }
+
+                       subnode = fdt_next_subnode(old_blob, subnode);
+               }
+       }
+
+       return 0;
+}
+#endif
--

2.23.0

#8 lore raw

PATCH 1/3 fdtdec: protect against another NULL phandlep in fdtdec_add_reserved_memory()

In reply to Heiko Stuebner

Hi Heiko,

Quoted 8 lines · Heiko Stuebner

The change adding fdtdec_add_reserved_memory() already protected the added phandle against the phandlep being NULL - making the phandlep var optional.

But in the early code checking for an already existing carveout this check was not done and thus the phandle assignment could run into trouble, so add a check there as well, which makes the function still return sucessfully if a matching region is found, even though no-one wants to

successfully

Quoted 13 lines

work with the phandle.

Fixes c9222a08b3f7 ("fdtdec: Implement fdtdec_add_reserved_memory()")
---
 lib/fdtdec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/fdtdec.c +0 -0
index 74525c84e7..17455c5506 100644

lib/fdtdec.c int fdtdec_add_reserved_memory(void *blob, const char *basename, · line 1367

Quoted 6 lines
lib/fdtdec.c +2 -1
@@ -1363,7 +1363,8 @@ int fdtdec_add_reserved_memory(void *blob, const char *basename,
                }
 
                if (addr == carveout->start && (addr + size) == carveout->end) {
-                       *phandlep = fdt_get_phandle(blob, node);
+                       if (phandlep)
+                               *phandlep = fdt_get_phandle(blob, node);

Can you please update the function comment to indicate that this can be NULL?

Quoted 6 lines
lib/fdtdec.c +0 -1
                        return 0;
                }
        }
--

2.23.0

Regards,
Simon