Skip to contentSkip to conversation

Populate kaslr seed with RNG

PATCH In review [email protected] 3 participants 15 messages Nov 17, 2023 – Nov 29, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v5 cover Populate kaslr seed with RNG

From: Sean Edmond <[email protected]>

This patch series creates a common API (fdt_fixup_kaslr_seed()) for populating the kaslr seed in the DTB. Existing users (kaslrseed, and ARMv8 sec firmware) have been updated to use this common API.

New functionality has been introduced to populate the kaslr using the RNG. This can be enabled with CONFIG_RNG_TPM_SEED.

changes in v5:
- Always enable OFNODE_MULTI_TREE
- include dm/ofnode-decl.h instead of dm/ofnode.h
- cast buffer to (u8 *) in kaslrseed.c

changes in v4:
- Fix compile issue when CONFIG_OF_CONTROL not set

changes in v3:
- Populate with RNG device instead of TPM device (this is a more generic solution)
- Use event spy to do the FDT fixup
- fix compile error for sandbox for !OFNODE_MULTI_TREE

changes in v2:
- fdt_fixup_kaslr_seed() uses the ofnode API
- Add root_ofnode_from_fdt() to get the root node from an FDT and perform error checking on the oftree
- add comments to exported functions
- Add error checking in image_setup_libfdt() for return from fdt_tpm_kaslr_seed()
- uclass_get_device() -> uclass_first_device_err()
- Change default config for OFNODE_MULTI_TREE (y if !OF_LIVE)

Dhananjay Phadke (2):
fdt: common API to populate kaslr seed
fdt: kaslr seed from RNG device

Sean Edmond (3):
cmd: kaslrseed: Use common API to fixup FDT
dm: core: Modify default for OFNODE_MULTI_TREE
fdt: Fix compile error for !OFNODE_MULTI_TREE

 arch/arm/cpu/armv8/sec_firmware.c | 39 ++++++++--------------
 boot/fdt_support.c                | 55 +++++++++++++++++++++++++++++++
 cmd/kaslrseed.c                   | 22 +++++--------
 drivers/core/Kconfig              |  3 +-
 drivers/core/ofnode.c             | 29 ++++++++++++----
 include/dm/ofnode.h               | 12 +++++++
 include/fdt_support.h             |  9 +++++
 lib/Kconfig                       |  7 ++++
 8 files changed, 130 insertions(+), 46 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH v5 1/5 fdt: common API to populate kaslr seed

From: Dhananjay Phadke <[email protected]>

fdt_fixup_kaslr_seed() will update given ofnode with random seed value. Source for random seed can be TPM or RNG driver in u-boot or sec firmware (ARM).

---
 arch/arm/cpu/armv8/sec_firmware.c | 39 +++++++++++--------------------
 boot/fdt_support.c                | 19 +++++++++++++++
 drivers/core/ofnode.c             | 17 ++++++++++++++
 include/dm/ofnode.h               | 12 ++++++++++
 include/fdt_support.h             |  9 +++++++
 5 files changed, 71 insertions(+), 25 deletions(-)
arch/arm/cpu/armv8/sec_firmware.c +14 -25
index c0e8726346f..5f04cd8aecd 100644
@@ -411,46 +411,35 @@ int sec_firmware_init(const void *sec_firmware_img,
 /*
  * fdt_fix_kaslr - Add kalsr-seed node in Device tree
  * @fdt:		Device tree
- * @eret:		0 in case of error, 1 for success
+ * @eret:		0 for success
  */
 int fdt_fixup_kaslr(void *fdt)
 {
-	int nodeoffset;
-	int err, ret = 0;
-	u8 rand[8];
+	int ret = 0;
 
 #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
+	u8 rand[8];
+	ofnode root;
+
 	/* Check if random seed generation is  supported */
 	if (sec_firmware_support_hwrng() == false) {
 		printf("WARNING: SEC firmware not running, no kaslr-seed\n");
-		return 0;
+		return -EOPNOTSUPP;
 	}
 
-	err = sec_firmware_get_random(rand, 8);
-	if (err < 0) {
+	ret = sec_firmware_get_random(rand, 8);
+	if (ret < 0) {
 		printf("WARNING: No random number to set kaslr-seed\n");
-		return 0;
+		return ret;
 	}
 
-	err = fdt_check_header(fdt);
-	if (err < 0) {
-		printf("fdt_chosen: %s\n", fdt_strerror(err));
-		return 0;
+	ret = root_ofnode_from_fdt(fdt, &root);
+	if (ret < 0) {
+		printf("WARNING: Unable to get root ofnode\n");
+		return ret;
 	}
 
-	/* find or create "/chosen" node. */
-	nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
-	if (nodeoffset < 0)
-		return 0;
-
-	err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand,
-				  sizeof(rand));
-	if (err < 0) {
-		printf("WARNING: can't set kaslr-seed %s.\n",
-		       fdt_strerror(err));
-		return 0;
-	}
-	ret = 1;
+	ret = fdt_fixup_kaslr_seed(root, rand, sizeof(rand));
 #endif
 
 	return ret;
boot/fdt_support.c +19 -0
index b15d07765fe..49d14a949be 100644
@@ -631,6 +631,25 @@ void fdt_fixup_ethernet(void *fdt)
 	}
 }
 
+int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len)
+{
+	ofnode chosen;
+	int ret;
+
+	/* find or create "/chosen" node. */
+	ret = ofnode_add_subnode(node, "chosen", &chosen);
+	if (ret && ret != -EEXIST)
+		return -ENOENT;
+
+	ret = ofnode_write_prop(chosen, "kaslr-seed", seed, len, true);
+	if (ret) {
+		printf("WARNING: can't set kaslr-seed\n");
+		return ret;
+	}
+
+	return 0;
+}
+
 int fdt_record_loadable(void *blob, u32 index, const char *name,
 			uintptr_t load_addr, u32 size, uintptr_t entry_point,
 			const char *type, const char *os, const char *arch)
drivers/core/ofnode.c +17 -0
index f72ea416cf1..6ce468d9cac 100644
@@ -966,6 +966,23 @@ ofnode oftree_path(oftree tree, const char *path)
 	}
 }
 
+int root_ofnode_from_fdt(void *fdt, ofnode *root_node)
+{
+	oftree tree;
+	/* If OFNODE_MULTI_TREE is not set, and if fdt is not the control FDT,
+	 *  oftree_from_fdt() will return NULL
+	 */
+	tree = oftree_from_fdt(fdt);
+
+	if (!oftree_valid(tree)) {
+		printf("Cannot create oftree\n");
+		return -EINVAL;
+	}
+	*root_node = oftree_root(tree);
+
+	return 0;
+}
+
 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
 {
 	ofnode chosen_node;
include/dm/ofnode.h +12 -0
index 5795115c490..3e0bbd0c33e 100644
@@ -936,6 +936,18 @@ ofnode oftree_path(oftree tree, const char *path);
  */
 ofnode oftree_root(oftree tree);
 
+/**
+ * root_ofnode_from_fdt() - Gets the root ofnode given an FDT blob.
+ *                          Note, this will fail if OFNODE_MULTI_TREE
+ *                          is not set.
+ *
+ * @fdt: Device tree to use
+ * @root_node : Root ofnode
+ *
+ * Return: 0 if OK, -ve on error
+ */
+int root_ofnode_from_fdt(void *fdt, ofnode *root_node);
+
 /**
  * ofnode_read_chosen_prop() - get the value of a chosen property
  *
include/fdt_support.h +9 -0
index 2cd83668982..0624650dcee 100644
@@ -11,6 +11,7 @@
 	!defined(USE_HOSTCC)
 
 #include <asm/u-boot.h>
+#include <dm/ofnode_decl.h>
 #include <linux/libfdt.h>
 #include <abuf.h>
 
@@ -121,6 +122,14 @@ static inline int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[],
 #endif
 
 void fdt_fixup_ethernet(void *fdt);
+
+/*
+ * fdt_fixup_kaslr_seed - Add kaslr-seed node in Device tree
+ * @node:		ofnode
+ * @eret:		0 for success
+ */
+int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len);
+
 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
 			 const void *val, int len, int create);
 void fdt_fixup_qe_firmware(void *fdt);
Signature · 1 line
2.42.0
#2 lore raw

Re: [PATCH v5 1/5] fdt: common API to populate kaslr seed

Hi Sean,

Quoted 7 lines · On Thu, 16 Nov 2023 at 19:02, <[email protected]> wrote:

From: Dhananjay Phadke <[email protected]>

fdt_fixup_kaslr_seed() will update given ofnode with random seed value. Source for random seed can be TPM or RNG driver in u-boot or sec firmware (ARM).

Quoted 7 lines
---
 arch/arm/cpu/armv8/sec_firmware.c | 39 +++++++++++--------------------
 boot/fdt_support.c                | 19 +++++++++++++++
 drivers/core/ofnode.c             | 17 ++++++++++++++
 include/dm/ofnode.h               | 12 ++++++++++

Please can you put the core changes in their own patch?

Quoted 103 lines
 include/fdt_support.h             |  9 +++++++
 5 files changed, 71 insertions(+), 25 deletions(-)
arch/arm/cpu/armv8/sec_firmware.c +14 -25
index c0e8726346f..5f04cd8aecd 100644
@@ -411,46 +411,35 @@ int sec_firmware_init(const void *sec_firmware_img,
 /*
  * fdt_fix_kaslr - Add kalsr-seed node in Device tree
  * @fdt:               Device tree
- * @eret:              0 in case of error, 1 for success
+ * @eret:              0 for success
  */
 int fdt_fixup_kaslr(void *fdt)
 {
-       int nodeoffset;
-       int err, ret = 0;
-       u8 rand[8];
+       int ret = 0;
 
 #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
+       u8 rand[8];
+       ofnode root;
+
        /* Check if random seed generation is  supported */
        if (sec_firmware_support_hwrng() == false) {
                printf("WARNING: SEC firmware not running, no kaslr-seed\n");
-               return 0;
+               return -EOPNOTSUPP;
        }
 
-       err = sec_firmware_get_random(rand, 8);
-       if (err < 0) {
+       ret = sec_firmware_get_random(rand, 8);
+       if (ret < 0) {
                printf("WARNING: No random number to set kaslr-seed\n");
-               return 0;
+               return ret;
        }
 
-       err = fdt_check_header(fdt);
-       if (err < 0) {
-               printf("fdt_chosen: %s\n", fdt_strerror(err));
-               return 0;
+       ret = root_ofnode_from_fdt(fdt, &root);
+       if (ret < 0) {
+               printf("WARNING: Unable to get root ofnode\n");
+               return ret;
        }
 
-       /* find or create "/chosen" node. */
-       nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
-       if (nodeoffset < 0)
-               return 0;
-
-       err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand,
-                                 sizeof(rand));
-       if (err < 0) {
-               printf("WARNING: can't set kaslr-seed %s.\n",
-                      fdt_strerror(err));
-               return 0;
-       }
-       ret = 1;
+       ret = fdt_fixup_kaslr_seed(root, rand, sizeof(rand));
 #endif
 
        return ret;
boot/fdt_support.c +19 -0
index b15d07765fe..49d14a949be 100644
@@ -631,6 +631,25 @@ void fdt_fixup_ethernet(void *fdt)
        }
 }
 
+int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len)
+{
+       ofnode chosen;
+       int ret;
+
+       /* find or create "/chosen" node. */
+       ret = ofnode_add_subnode(node, "chosen", &chosen);
+       if (ret && ret != -EEXIST)
+               return -ENOENT;
+
+       ret = ofnode_write_prop(chosen, "kaslr-seed", seed, len, true);
+       if (ret) {
+               printf("WARNING: can't set kaslr-seed\n");
+               return ret;
+       }
+
+       return 0;
+}
+
 int fdt_record_loadable(void *blob, u32 index, const char *name,
                        uintptr_t load_addr, u32 size, uintptr_t entry_point,
                        const char *type, const char *os, const char *arch)
drivers/core/ofnode.c +0 -0
index f72ea416cf1..6ce468d9cac 100644

drivers/core/ofnode.c ofnode oftree_path(oftree tree, const char *path) · line 969

Quoted 4 lines
drivers/core/ofnode.c +1 -0
@@ -966,6 +966,23 @@ ofnode oftree_path(oftree tree, const char *path)
        }
 }
 
+int root_ofnode_from_fdt(void *fdt, ofnode *root_node)

Can you use ofnode_root_from_fdt() so that ofnode is first?

Quoted 70 lines
drivers/core/ofnode.c +16 -0
+{
+       oftree tree;
+       /* If OFNODE_MULTI_TREE is not set, and if fdt is not the control FDT,
+        *  oftree_from_fdt() will return NULL
+        */
+       tree = oftree_from_fdt(fdt);
+
+       if (!oftree_valid(tree)) {
+               printf("Cannot create oftree\n");
+               return -EINVAL;
+       }
+       *root_node = oftree_root(tree);
+
+       return 0;
+}
+
 const void *ofnode_read_chosen_prop(const char *propname, int *sizep)
 {
        ofnode chosen_node;
include/dm/ofnode.h +12 -0
index 5795115c490..3e0bbd0c33e 100644
@@ -936,6 +936,18 @@ ofnode oftree_path(oftree tree, const char *path);
  */
 ofnode oftree_root(oftree tree);
 
+/**
+ * root_ofnode_from_fdt() - Gets the root ofnode given an FDT blob.
+ *                          Note, this will fail if OFNODE_MULTI_TREE
+ *                          is not set.
+ *
+ * @fdt: Device tree to use
+ * @root_node : Root ofnode
+ *
+ * Return: 0 if OK, -ve on error
+ */
+int root_ofnode_from_fdt(void *fdt, ofnode *root_node);
+
 /**
  * ofnode_read_chosen_prop() - get the value of a chosen property
  *
include/fdt_support.h +9 -1
index 2cd83668982..0624650dcee 100644
@@ -11,6 +11,7 @@
        !defined(USE_HOSTCC)
 
 #include <asm/u-boot.h>
+#include <dm/ofnode_decl.h>
 #include <linux/libfdt.h>
 #include <abuf.h>
 
@@ -121,6 +122,14 @@ static inline int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[],
 #endif
 
 void fdt_fixup_ethernet(void *fdt);
+
+/*
+ * fdt_fixup_kaslr_seed - Add kaslr-seed node in Device tree
+ * @node:              ofnode
+ * @eret:              0 for success
+ */
+int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len);
+
 int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
                         const void *val, int len, int create);
 void fdt_fixup_qe_firmware(void *fdt);
--
Quoted 2 lines

2.42.0

Regards,
Simon

#3 lore raw

PATCH v5 2/5 fdt: kaslr seed from RNG device

In reply to [email protected]

From: Dhananjay Phadke <[email protected]>

Add support for KASLR seed from the RNG device. Invokes dm_rng_read() API to read 8-bytes of random bytes. Performs the FDT fixup using event spy. To enable use CONFIG_KASLR_RNG_SEED

---
 boot/fdt_support.c | 36 ++++++++++++++++++++++++++++++++++++
 lib/Kconfig        |  7 +++++++
 2 files changed, 43 insertions(+)
boot/fdt_support.c +36 -0
index 49d14a949be..12defcf645c 100644
@@ -12,7 +12,10 @@
 #include <log.h>
 #include <mapmem.h>
 #include <net.h>
+#include <rng.h>
 #include <stdio_dev.h>
+#include <dm/device.h>
+#include <dm/uclass.h>
 #include <dm/ofnode.h>
 #include <linux/ctype.h>
 #include <linux/types.h>
@@ -650,6 +653,39 @@ int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len)
 	return 0;
 }
 
+int fdt_rng_kaslr_seed(void *ctx, struct event *event)
+{
+	u8 rand[8] = {0};
+	struct udevice *dev;
+	int ret;
+	oftree tree = event->data.ft_fixup.tree;
+	ofnode root_node = oftree_root(tree);
+
+	ret = uclass_first_device_err(UCLASS_RNG, &dev);
+	if (ret) {
+		printf("ERROR: Failed to find RNG device\n");
+		return ret;
+	}
+
+	ret = dm_rng_read(dev, rand, sizeof(rand));
+	if (ret) {
+		printf("ERROR: RNG read failed, ret=%d\n", ret);
+		return ret;
+	}
+
+	ret = fdt_fixup_kaslr_seed(root_node, rand, sizeof(rand));
+	if (ret) {
+		printf("ERROR: failed to add kaslr-seed to fdt\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+#if defined(CONFIG_KASLR_RNG_SEED)
+EVENT_SPY(EVT_FT_FIXUP, fdt_rng_kaslr_seed);
+#endif
+
 int fdt_record_loadable(void *blob, u32 index, const char *name,
 			uintptr_t load_addr, u32 size, uintptr_t entry_point,
 			const char *type, const char *os, const char *arch)
lib/Kconfig +7 -0
index 19649517a39..4f5dfc00d6f 100644
@@ -477,6 +477,13 @@ config VPL_TPM
 	  for the low-level TPM interface, but only one TPM is supported at
 	  a time by the TPM library.
 
+config KASLR_RNG_SEED
+	bool "Use RNG driver for KASLR random seed"
+	depends on DM_RNG
+	help
+	  This enables support for using the RNG driver as entropy source for
+	  KASLR seed populated in kernel's device tree.
+
 endmenu
 
 menu "Android Verified Boot"
Signature · 1 line
2.42.0
#4 lore raw

Re: [PATCH v5 2/5] fdt: kaslr seed from RNG device

Hi Sean
There are a few reviewed-by tags missing since v3/v4. Any reason you dropped those? The change log doesn't mention anything

Thanks
/Ilias

Quoted 91 lines · On Fri, 17 Nov 2023 at 04:02, <[email protected]> wrote:

From: Dhananjay Phadke <[email protected]>

Add support for KASLR seed from the RNG device. Invokes dm_rng_read() API to read 8-bytes of random bytes. Performs the FDT fixup using event spy. To enable use CONFIG_KASLR_RNG_SEED

---
 boot/fdt_support.c | 36 ++++++++++++++++++++++++++++++++++++
 lib/Kconfig        |  7 +++++++
 2 files changed, 43 insertions(+)
boot/fdt_support.c +36 -0
index 49d14a949be..12defcf645c 100644
@@ -12,7 +12,10 @@
 #include <log.h>
 #include <mapmem.h>
 #include <net.h>
+#include <rng.h>
 #include <stdio_dev.h>
+#include <dm/device.h>
+#include <dm/uclass.h>
 #include <dm/ofnode.h>
 #include <linux/ctype.h>
 #include <linux/types.h>
@@ -650,6 +653,39 @@ int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len)
        return 0;
 }
 
+int fdt_rng_kaslr_seed(void *ctx, struct event *event)
+{
+       u8 rand[8] = {0};
+       struct udevice *dev;
+       int ret;
+       oftree tree = event->data.ft_fixup.tree;
+       ofnode root_node = oftree_root(tree);
+
+       ret = uclass_first_device_err(UCLASS_RNG, &dev);
+       if (ret) {
+               printf("ERROR: Failed to find RNG device\n");
+               return ret;
+       }
+
+       ret = dm_rng_read(dev, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: RNG read failed, ret=%d\n", ret);
+               return ret;
+       }
+
+       ret = fdt_fixup_kaslr_seed(root_node, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: failed to add kaslr-seed to fdt\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+#if defined(CONFIG_KASLR_RNG_SEED)
+EVENT_SPY(EVT_FT_FIXUP, fdt_rng_kaslr_seed);
+#endif
+
 int fdt_record_loadable(void *blob, u32 index, const char *name,
                        uintptr_t load_addr, u32 size, uintptr_t entry_point,
                        const char *type, const char *os, const char *arch)
lib/Kconfig +7 -1
index 19649517a39..4f5dfc00d6f 100644
@@ -477,6 +477,13 @@ config VPL_TPM
          for the low-level TPM interface, but only one TPM is supported at
          a time by the TPM library.
 
+config KASLR_RNG_SEED
+       bool "Use RNG driver for KASLR random seed"
+       depends on DM_RNG
+       help
+         This enables support for using the RNG driver as entropy source for
+         KASLR seed populated in kernel's device tree.
+
 endmenu
 
 menu "Android Verified Boot"
--

2.42.0

#5 lore raw

Re: [PATCH v5 2/5] fdt: kaslr seed from RNG device

Hi Ilias, Applogies, I'm fairly new to upstream contributions and I've missed a few things on multi-version patch series such as this one. Simon has elightened about patman though (I feel a bit sheepish that I haven't been using this tool all along). Should I resubmit this as a new series to ensure prior reviews and change logs are captured?

Sean

Quoted 98 lines · Ilias Apalodimas

Hi Sean
There are a few reviewed-by tags missing since v3/v4. Any reason you dropped those? The change log doesn't mention anything

Thanks
/Ilias

Quoted 90 lines · On Fri, 17 Nov 2023 at 04:02,<[email protected]> wrote:

From: Dhananjay Phadke<[email protected]>

Add support for KASLR seed from the RNG device. Invokes dm_rng_read() API to read 8-bytes of random bytes. Performs the FDT fixup using event spy. To enable use CONFIG_KASLR_RNG_SEED

---
  boot/fdt_support.c | 36 ++++++++++++++++++++++++++++++++++++
  lib/Kconfig        |  7 +++++++
  2 files changed, 43 insertions(+)
boot/fdt_support.c +36 -0
index 49d14a949be..12defcf645c 100644
@@ -12,7 +12,10 @@
  #include <log.h>
  #include <mapmem.h>
  #include <net.h>
+#include <rng.h>
  #include <stdio_dev.h>
+#include <dm/device.h>
+#include <dm/uclass.h>
  #include <dm/ofnode.h>
  #include <linux/ctype.h>
  #include <linux/types.h>
@@ -650,6 +653,39 @@ int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len)
         return 0;
  }
 
+int fdt_rng_kaslr_seed(void *ctx, struct event *event)
+{
+       u8 rand[8] = {0};
+       struct udevice *dev;
+       int ret;
+       oftree tree = event->data.ft_fixup.tree;
+       ofnode root_node = oftree_root(tree);
+
+       ret = uclass_first_device_err(UCLASS_RNG, &dev);
+       if (ret) {
+               printf("ERROR: Failed to find RNG device\n");
+               return ret;
+       }
+
+       ret = dm_rng_read(dev, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: RNG read failed, ret=%d\n", ret);
+               return ret;
+       }
+
+       ret = fdt_fixup_kaslr_seed(root_node, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: failed to add kaslr-seed to fdt\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+#if defined(CONFIG_KASLR_RNG_SEED)
+EVENT_SPY(EVT_FT_FIXUP, fdt_rng_kaslr_seed);
+#endif
+
  int fdt_record_loadable(void *blob, u32 index, const char *name,
                         uintptr_t load_addr, u32 size, uintptr_t entry_point,
                         const char *type, const char *os, const char *arch)
lib/Kconfig +7 -1
index 19649517a39..4f5dfc00d6f 100644
@@ -477,6 +477,13 @@ config VPL_TPM
           for the low-level TPM interface, but only one TPM is supported at
           a time by the TPM library.
 
+config KASLR_RNG_SEED
+       bool "Use RNG driver for KASLR random seed"
+       depends on DM_RNG
+       help
+         This enables support for using the RNG driver as entropy source for
+         KASLR seed populated in kernel's device tree.
+
  endmenu
 
  menu "Android Verified Boot"
--

2.42.0

#6 lore raw

Re: [PATCH v5 2/5] fdt: kaslr seed from RNG device

Hi Sean,

Quoted 2 lines · Sean Edmond

Hi Ilias, Applogies, I'm fairly new to upstream contributions and I've missed a few things on multi-version patch series such as this one. Simon has elightened about patman though (I feel a bit sheepish that I haven't been using this tool all along). Should I resubmit this as a new series to ensure prior reviews and change logs are captured?

No worries, we've all been there. I can re-send my reviewed-by on those patches. Generally speaking, try to preserve these across versions, unless something has changed on the patch

Thanks
/Ilias

Quoted 106 lines

Sean

On 2023-11-17 11:33 p.m., Ilias Apalodimas wrote:

Hi Sean
There are a few reviewed-by tags missing since v3/v4. Any reason you dropped those? The change log doesn't mention anything

Thanks
/Ilias

On Fri, 17 Nov 2023 at 04:02, <[email protected]> wrote:

From: Dhananjay Phadke <[email protected]>

Add support for KASLR seed from the RNG device. Invokes dm_rng_read() API to read 8-bytes of random bytes. Performs the FDT fixup using event spy. To enable use CONFIG_KASLR_RNG_SEED

---
 boot/fdt_support.c | 36 ++++++++++++++++++++++++++++++++++++
 lib/Kconfig        |  7 +++++++
 2 files changed, 43 insertions(+)
boot/fdt_support.c +36 -0
index 49d14a949be..12defcf645c 100644
@@ -12,7 +12,10 @@
 #include <log.h>
 #include <mapmem.h>
 #include <net.h>
+#include <rng.h>
 #include <stdio_dev.h>
+#include <dm/device.h>
+#include <dm/uclass.h>
 #include <dm/ofnode.h>
 #include <linux/ctype.h>
 #include <linux/types.h>
@@ -650,6 +653,39 @@ int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len)
        return 0;
 }
 
+int fdt_rng_kaslr_seed(void *ctx, struct event *event)
+{
+       u8 rand[8] = {0};
+       struct udevice *dev;
+       int ret;
+       oftree tree = event->data.ft_fixup.tree;
+       ofnode root_node = oftree_root(tree);
+
+       ret = uclass_first_device_err(UCLASS_RNG, &dev);
+       if (ret) {
+               printf("ERROR: Failed to find RNG device\n");
+               return ret;
+       }
+
+       ret = dm_rng_read(dev, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: RNG read failed, ret=%d\n", ret);
+               return ret;
+       }
+
+       ret = fdt_fixup_kaslr_seed(root_node, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: failed to add kaslr-seed to fdt\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+#if defined(CONFIG_KASLR_RNG_SEED)
+EVENT_SPY(EVT_FT_FIXUP, fdt_rng_kaslr_seed);
+#endif
+
 int fdt_record_loadable(void *blob, u32 index, const char *name,
                        uintptr_t load_addr, u32 size, uintptr_t entry_point,
                        const char *type, const char *os, const char *arch)
lib/Kconfig +7 -1
index 19649517a39..4f5dfc00d6f 100644
@@ -477,6 +477,13 @@ config VPL_TPM
          for the low-level TPM interface, but only one TPM is supported at
          a time by the TPM library.
 
+config KASLR_RNG_SEED
+       bool "Use RNG driver for KASLR random seed"
+       depends on DM_RNG
+       help
+         This enables support for using the RNG driver as entropy source for
+         KASLR seed populated in kernel's device tree.
+
 endmenu
 
 menu "Android Verified Boot"
--

2.42.0

Reviewed-by on v5 2/5
#7 lore raw

Re: [PATCH v5 2/5] fdt: kaslr seed from RNG device

In reply to [email protected]

Hi Sean,

Quoted 7 lines · On Thu, 16 Nov 2023 at 19:02, <[email protected]> wrote:

From: Dhananjay Phadke <[email protected]>

Add support for KASLR seed from the RNG device. Invokes dm_rng_read() API to read 8-bytes of random bytes. Performs the FDT fixup using event spy. To enable use CONFIG_KASLR_RNG_SEED

Quoted 8 lines
---
 boot/fdt_support.c | 36 ++++++++++++++++++++++++++++++++++++
 lib/Kconfig        |  7 +++++++
 2 files changed, 43 insertions(+)
Quoted 39 lines
boot/fdt_support.c +23 -0
index 49d14a949be..12defcf645c 100644
@@ -12,7 +12,10 @@
 #include <log.h>
 #include <mapmem.h>
 #include <net.h>
+#include <rng.h>
 #include <stdio_dev.h>
+#include <dm/device.h>
+#include <dm/uclass.h>
 #include <dm/ofnode.h>
 #include <linux/ctype.h>
 #include <linux/types.h>
@@ -650,6 +653,39 @@ int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len)
        return 0;
 }
 
+int fdt_rng_kaslr_seed(void *ctx, struct event *event)
+{
+       u8 rand[8] = {0};
+       struct udevice *dev;
+       int ret;
+       oftree tree = event->data.ft_fixup.tree;
+       ofnode root_node = oftree_root(tree);
+
+       ret = uclass_first_device_err(UCLASS_RNG, &dev);
+       if (ret) {
+               printf("ERROR: Failed to find RNG device\n");
+               return ret;
+       }
+
+       ret = dm_rng_read(dev, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: RNG read failed, ret=%d\n", ret);
+               return ret;
+       }
+

boot/fdt_support.c int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len) · line 685

Quoted 10 lines
boot/fdt_support.c +10 -0
+       ret = fdt_fixup_kaslr_seed(root_node, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: failed to add kaslr-seed to fdt\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+#if defined(CONFIG_KASLR_RNG_SEED)

You could put this if() in the function above

Quoted 27 lines
boot/fdt_support.c +3 -0
+EVENT_SPY(EVT_FT_FIXUP, fdt_rng_kaslr_seed);
+#endif
+
 int fdt_record_loadable(void *blob, u32 index, const char *name,
                        uintptr_t load_addr, u32 size, uintptr_t entry_point,
                        const char *type, const char *os, const char *arch)
lib/Kconfig +7 -1
index 19649517a39..4f5dfc00d6f 100644
@@ -477,6 +477,13 @@ config VPL_TPM
          for the low-level TPM interface, but only one TPM is supported at
          a time by the TPM library.
 
+config KASLR_RNG_SEED
+       bool "Use RNG driver for KASLR random seed"
+       depends on DM_RNG
+       help
+         This enables support for using the RNG driver as entropy source for
+         KASLR seed populated in kernel's device tree.
+
 endmenu
 
 menu "Android Verified Boot"
--

2.42.0

Reviewed-by
#8 lore raw

Re: [PATCH v5 2/5] fdt: kaslr seed from RNG device

Quoted 90 lines · Simon Glass

Hi Sean,

Quoted 7 lines · On Thu, 16 Nov 2023 at 19:02, <[email protected]> wrote:

From: Dhananjay Phadke <[email protected]>

Add support for KASLR seed from the RNG device. Invokes dm_rng_read() API to read 8-bytes of random bytes. Performs the FDT fixup using event spy. To enable use CONFIG_KASLR_RNG_SEED

Quoted 8 lines
---
 boot/fdt_support.c | 36 ++++++++++++++++++++++++++++++++++++
 lib/Kconfig        |  7 +++++++
 2 files changed, 43 insertions(+)

boot/fdt_support.c int fdt_fixup_kaslr_seed(ofnode node, const u8

Quoted 16 lines
boot/fdt_support.c +3 -0
index 49d14a949be..12defcf645c 100644
@@ -12,7 +12,10 @@
 #include <log.h>
 #include <mapmem.h>
 #include <net.h>
+#include <rng.h>
 #include <stdio_dev.h>
+#include <dm/device.h>
+#include <dm/uclass.h>
 #include <dm/ofnode.h>
 #include <linux/ctype.h>
 #include <linux/types.h>
@@ -650,6 +653,39 @@ int fdt_fixup_kaslr_seed(ofnode node, const u8

*seed, int len)

Quoted 23 lines
boot/fdt_support.c +20 -0
        return 0;
 }
 
+int fdt_rng_kaslr_seed(void *ctx, struct event *event)
+{
+       u8 rand[8] = {0};
+       struct udevice *dev;
+       int ret;
+       oftree tree = event->data.ft_fixup.tree;
+       ofnode root_node = oftree_root(tree);
+
+       ret = uclass_first_device_err(UCLASS_RNG, &dev);
+       if (ret) {
+               printf("ERROR: Failed to find RNG device\n");
+               return ret;
+       }
+
+       ret = dm_rng_read(dev, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: RNG read failed, ret=%d\n", ret);
+               return ret;
+       }
+

boot/fdt_support.c int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len) · line 685

Quoted 10 lines
boot/fdt_support.c +10 -0
+       ret = fdt_fixup_kaslr_seed(root_node, rand, sizeof(rand));
+       if (ret) {
+               printf("ERROR: failed to add kaslr-seed to fdt\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+#if defined(CONFIG_KASLR_RNG_SEED)

You could put this if() in the function above

boot/fdt_support.c int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len)

Quoted 5 lines
boot/fdt_support.c +3 -0
+EVENT_SPY(EVT_FT_FIXUP, fdt_rng_kaslr_seed);
+#endif
+
 int fdt_record_loadable(void *blob, u32 index, const char *name,
                        uintptr_t load_addr, u32 size, uintptr_t

entry_point,

boot/fdt_support.c int fdt_fixup_kaslr_seed(ofnode node, const u8 · line 691

Quoted 1 line
boot/fdt_support.c +0 -0
                        const char *type, const char *os, const char

*arch)

lib/Kconfig config VPL_TPM · line 477

Quoted 6 lines
lib/Kconfig +0 -0
index 19649517a39..4f5dfc00d6f 100644
@@ -477,6 +477,13 @@ config VPL_TPM
          for the low-level TPM interface, but only one TPM is supported

at

Quoted 17 lines

lib/Kconfig config VPL_TPM

Quoted 7 lines
lib/Kconfig +5 -0
          a time by the TPM library.
 
+config KASLR_RNG_SEED
+       bool "Use RNG driver for KASLR random seed"
+       depends on DM_RNG
+       help
+         This enables support for using the RNG driver as entropy

source for

Quoted 8 lines
lib/Kconfig +2 -1
+         KASLR seed populated in kernel's device tree.
+
 endmenu
 
 menu "Android Verified Boot"
--

2.42.0

#9 lore raw

PATCH v5 3/5 cmd: kaslrseed: Use common API to fixup FDT

In reply to [email protected]

From: Sean Edmond <[email protected]>

Use the newly introduced common API fdt_fixup_kaslr_seed() in the kaslrseed command.

---
 cmd/kaslrseed.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
cmd/kaslrseed.c +8 -14
index 9acb8e16386..23ed7c066aa 100644
@@ -19,7 +19,7 @@ static int do_kaslr_seed(struct cmd_tbl *cmdtp, int flag, int argc, char *const
 	size_t n = 0x8;
 	struct udevice *dev;
 	u64 *buf;
-	int nodeoffset;
+	ofnode root;
 	int ret = CMD_RET_SUCCESS;
 
 	if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
@@ -45,21 +45,15 @@ static int do_kaslr_seed(struct cmd_tbl *cmdtp, int flag, int argc, char *const
 		return CMD_RET_FAILURE;
 	}
 
-	ret = fdt_check_header(working_fdt);
-	if (ret < 0) {
-		printf("fdt_chosen: %s\n", fdt_strerror(ret));
-		return CMD_RET_FAILURE;
-	}
-
-	nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen");
-	if (nodeoffset < 0) {
-		printf("Reading chosen node failed\n");
-		return CMD_RET_FAILURE;
+	ret = root_ofnode_from_fdt(working_fdt, &root);
+	if (ret) {
+		printf("ERROR: Unable to get root ofnode\n");
+		goto CMD_RET_FAILURE;
 	}
 
-	ret = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf));
-	if (ret < 0) {
-		printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(ret));
+	ret = fdt_fixup_kaslr_seed(root, (u8 *)buf, sizeof(buf));
+	if (ret) {
+		printf("ERROR: failed to add kaslr-seed to fdt\n");
 		return CMD_RET_FAILURE;
 	}
 
Signature · 1 line
2.42.0
Reviewed-by on v5 3/5
#10 lore raw

Re: [PATCH v5 3/5] cmd: kaslrseed: Use common API to fixup FDT

Quoted 11 lines · On Thu, 16 Nov 2023 at 19:02, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Use the newly introduced common API fdt_fixup_kaslr_seed() in the kaslrseed command.

---
 cmd/kaslrseed.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

As a follow-up, you could add a test for this command

Quoted 45 lines
cmd/kaslrseed.c +8 -15
index 9acb8e16386..23ed7c066aa 100644
@@ -19,7 +19,7 @@ static int do_kaslr_seed(struct cmd_tbl *cmdtp, int flag, int argc, char *const
        size_t n = 0x8;
        struct udevice *dev;
        u64 *buf;
-       int nodeoffset;
+       ofnode root;
        int ret = CMD_RET_SUCCESS;
 
        if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
@@ -45,21 +45,15 @@ static int do_kaslr_seed(struct cmd_tbl *cmdtp, int flag, int argc, char *const
                return CMD_RET_FAILURE;
        }
 
-       ret = fdt_check_header(working_fdt);
-       if (ret < 0) {
-               printf("fdt_chosen: %s\n", fdt_strerror(ret));
-               return CMD_RET_FAILURE;
-       }
-
-       nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen");
-       if (nodeoffset < 0) {
-               printf("Reading chosen node failed\n");
-               return CMD_RET_FAILURE;
+       ret = root_ofnode_from_fdt(working_fdt, &root);
+       if (ret) {
+               printf("ERROR: Unable to get root ofnode\n");
+               goto CMD_RET_FAILURE;
        }
 
-       ret = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf));
-       if (ret < 0) {
-               printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(ret));
+       ret = fdt_fixup_kaslr_seed(root, (u8 *)buf, sizeof(buf));
+       if (ret) {
+               printf("ERROR: failed to add kaslr-seed to fdt\n");
                return CMD_RET_FAILURE;
        }
 
--

2.42.0

#11 lore raw

PATCH v5 4/5 dm: core: Modify default for OFNODE_MULTI_TREE

In reply to [email protected]

From: Sean Edmond <[email protected]>

There is a preference to use the "ofnode" API for FDT fixups moving forward. The FDT fixup will usually be for the kernel FDT. To fixup the kernel FDT with the ofnode API, it's required to set the OFNODE_MULTI_TREE option.

To ensure existing users of kaslr fdt fixup are not impacted, Let's modify the default value for OFNODE_MULTI_TREE to ensure it's always set. This will cause a 1007 byte increase in the code size.

---
 drivers/core/Kconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/core/Kconfig +2 -1
index 737d4590d5b..c01a8dc7e0a 100644
@@ -423,7 +423,8 @@ config DM_DEV_READ_INLINE
 
 config OFNODE_MULTI_TREE
 	bool "Allow the ofnode interface to access any tree"
-	default y if EVENT && !DM_DEV_READ_INLINE && !DM_INLINE_OFNODE
+	depends on OF_CONTROL
+	default y
 	help
 	  Normally U-Boot makes use of its control FDT, the one used to bind
 	  devices and provide options. In some cases, U-Boot must also process
Signature · 1 line
2.42.0
Reviewed-by on v5 4/5
#12 lore raw

Re: [PATCH v5 4/5] dm: core: Modify default for OFNODE_MULTI_TREE

Quoted 8 lines · On Thu, 16 Nov 2023 at 19:02, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

There is a preference to use the "ofnode" API for FDT fixups moving forward. The FDT fixup will usually be for the kernel FDT. To fixup the kernel FDT with the ofnode API, it's required to set the OFNODE_MULTI_TREE option.

Quoted 8 lines

To ensure existing users of kaslr fdt fixup are not impacted, Let's modify the default value for OFNODE_MULTI_TREE to ensure it's always set. This will cause a 1007 byte increase in the code size.

---
 drivers/core/Kconfig | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
#13 lore raw

PATCH v5 5/5 fdt: Fix compile error for !OFNODE_MULTI_TREE

In reply to [email protected]

From: Sean Edmond <[email protected]>

Required to fix the following compile error when building sandbox:
/tmp/cci9ibby.ltrans21.ltrans.o: In function `do_cedit_load':
<artificial>:(.text+0x601d): undefined reference to `oftree_dispose'

---
 drivers/core/ofnode.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/core/ofnode.c +6 -6
index 6ce468d9cac..ec25466a5e7 100644
@@ -135,12 +135,6 @@ int oftree_new(oftree *treep)
 	return 0;
 }
 
-void oftree_dispose(oftree tree)
-{
-	if (of_live_active())
-		of_live_free(tree.np);
-}
-
 void *ofnode_lookup_fdt(ofnode node)
 {
 	if (gd->flags & GD_FLG_RELOC) {
@@ -243,6 +237,12 @@ int oftree_new(oftree *treep)
 
 #endif /* OFNODE_MULTI_TREE */
 
+void oftree_dispose(oftree tree)
+{
+	if (of_live_active())
+		of_live_free(tree.np);
+}
+
 int oftree_to_fdt(oftree tree, struct abuf *buf)
 {
 	int ret;
Signature · 1 line
2.42.0
Reviewed-by on v5 5/5
#14 lore raw

Re: [PATCH v5 5/5] fdt: Fix compile error for !OFNODE_MULTI_TREE

Quoted 12 lines · On Thu, 16 Nov 2023 at 19:02, <[email protected]> wrote:

From: Sean Edmond <[email protected]>

Required to fix the following compile error when building sandbox:
/tmp/cci9ibby.ltrans21.ltrans.o: In function `do_cedit_load':
<artificial>:(.text+0x601d): undefined reference to `oftree_dispose'

---
 drivers/core/ofnode.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)