public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: seanedmond@linux.microsoft.com
To: u-boot@lists.denx.de
Cc: dphadke@linux.microsoft.com, macromorgan@hotmail.com, sjg@chromium.org
Subject: [PATCH v2 2/4] fdt: kaslr seed from tpm entropy
Date: Tue, 29 Aug 2023 13:32:08 -0700	[thread overview]
Message-ID: <20230829203210.83914-7-seanedmond@linux.microsoft.com> (raw)
In-Reply-To: <20230829203210.83914-1-seanedmond@linux.microsoft.com>

From: Dhananjay Phadke <dphadke@linux.microsoft.com>

Add support for KASLR seed from TPM device. Invokes tpm_get_random()
API to read 8-bytes of random bytes for KASLR.

Signed-off-by: Dhananjay Phadke <dphadke@linux.microsoft.com>
Signed-off-by: Drew Kluemke <ankluemk@microsoft.com>
Signed-off-by: Sean Edmond <seanedmond@microsoft.com>
---
 boot/image-fdt.c      | 15 +++++++++++++++
 common/fdt_support.c  | 30 ++++++++++++++++++++++++++++++
 include/fdt_support.h |  8 ++++++++
 lib/Kconfig           |  9 +++++++++
 4 files changed, 62 insertions(+)

diff --git a/boot/image-fdt.c b/boot/image-fdt.c
index f10200f647..ed38ed77b9 100644
--- a/boot/image-fdt.c
+++ b/boot/image-fdt.c
@@ -624,6 +624,21 @@ int image_setup_libfdt(struct bootm_headers *images, void *blob,
 		goto err;
 	}
 
+	if (IS_ENABLED(CONFIG_KASLR_TPM_SEED)) {
+		ofnode root;
+
+		ret = root_ofnode_from_fdt(blob, &root);
+		if (ret) {
+			printf("ERROR: Unable to get root ofnode\n");
+			goto err;
+		}
+		ret = fdt_tpm_kaslr_seed(root);
+		if (ret) {
+			printf("ERROR: fdt fixup KASLR failed: %d\n", ret);
+			goto err;
+		}
+	}
+
 	fdt_ret = optee_copy_fdt_nodes(blob);
 	if (fdt_ret) {
 		printf("ERROR: transfer of optee nodes to new fdt failed: %s\n",
diff --git a/common/fdt_support.c b/common/fdt_support.c
index 52be4375b4..d338fcde54 100644
--- a/common/fdt_support.c
+++ b/common/fdt_support.c
@@ -13,6 +13,9 @@
 #include <mapmem.h>
 #include <net.h>
 #include <stdio_dev.h>
+#include <tpm_api.h>
+#include <dm/device.h>
+#include <dm/uclass.h>
 #include <dm/ofnode.h>
 #include <linux/ctype.h>
 #include <linux/types.h>
@@ -650,6 +653,33 @@ int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len)
 	return 0;
 }
 
+int fdt_tpm_kaslr_seed(ofnode node)
+{
+	u8 rand[8] = {0};
+	struct udevice *dev;
+	int ret;
+
+	ret = uclass_first_device_err(UCLASS_TPM, &dev);
+	if (ret) {
+		printf("ERROR: Failed to find TPM device\n");
+		return ret;
+	}
+
+	ret = tpm_get_random(dev, rand, sizeof(rand));
+	if (ret) {
+		printf("ERROR: TPM GetRandom failed, ret=%d\n", ret);
+		return ret;
+	}
+
+	ret = fdt_fixup_kaslr_seed(node, rand, sizeof(rand));
+	if (ret) {
+		printf("ERROR: failed to add kaslr-seed to fdt\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)
diff --git a/include/fdt_support.h b/include/fdt_support.h
index d967118bed..117ca14ca5 100644
--- a/include/fdt_support.h
+++ b/include/fdt_support.h
@@ -130,6 +130,14 @@ void fdt_fixup_ethernet(void *fdt);
  */
 int fdt_fixup_kaslr_seed(ofnode node, const u8 *seed, int len);
 
+/*
+ * fdt_add_tpm_kaslr_seed - Add kalsr-seed node in Device tree with random
+ *			    bytes from TPM device
+ * @node:		ofnode
+ * @eret:		0 for success
+ */
+int fdt_tpm_kaslr_seed(ofnode node);
+
 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);
diff --git a/lib/Kconfig b/lib/Kconfig
index 3926652db6..1530ef7c86 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -465,6 +465,15 @@ config VPL_TPM
 	  for the low-level TPM interface, but only one TPM is supported at
 	  a time by the TPM library.
 
+config KASLR_TPM_SEED
+	bool "Use TPM for KASLR random seed"
+	depends on TPM_V1 || TPM_V2
+	help
+	  This enables support for using TPMs as entropy source for KASLR seed
+	  populated in kernel's device tree. Both TPMv1 and TPMv2 are supported
+	  for the low-level TPM interface, but only one TPM is supported at
+	  a time by the library.
+
 endmenu
 
 menu "Android Verified Boot"
-- 
2.40.0


  parent reply	other threads:[~2023-08-29 20:33 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-29 20:32 [PATCH 1/4] fdt: common API to populate kaslr seed seanedmond
2023-08-29 20:32 ` [PATCH 2/4] fdt: kaslr seed from tpm entropy seanedmond
2023-08-29 20:32 ` [PATCH 3/4] cmd: kaslrseed: Use common API to fixup FDT seanedmond
2023-08-29 20:32 ` [PATCH 4/4] dm: core: Modify default for OFNODE_MULTI_TREE seanedmond
2023-08-29 20:32 ` [PATCH v2 0/4] Populate kaslr seed with TPM seanedmond
2023-08-29 20:32 ` [PATCH v2 1/4] fdt: common API to populate kaslr seed seanedmond
2023-08-29 20:32 ` seanedmond [this message]
2023-08-29 20:32 ` [PATCH v2 3/4] cmd: kaslrseed: Use common API to fixup FDT seanedmond
2023-08-29 20:32 ` [PATCH v2 4/4] dm: core: Modify default for OFNODE_MULTI_TREE seanedmond
  -- strict thread matches above, loose matches on Subject: below --
2023-08-29 20:37 [PATCH v2 0/4] Populate kaslr seed with TPM seanedmond
2023-08-29 20:37 ` [PATCH v2 2/4] fdt: kaslr seed from tpm entropy seanedmond
2023-08-31 19:02   ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230829203210.83914-7-seanedmond@linux.microsoft.com \
    --to=seanedmond@linux.microsoft.com \
    --cc=dphadke@linux.microsoft.com \
    --cc=macromorgan@hotmail.com \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox