Skip to contentSkip to conversation

tpm: Support boot measurements

PATCH Superseded Eddie James 3 participants 15 messages Oct 19, 2023 – Oct 27, 2023 Library code

v13 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v13 cover tpm: Support boot measurements

This series adds support for measuring the boot images more generically than the existing EFI support. Several EFI functions have been moved to the TPM layer. The series includes optional measurement from the bootm command.
A new test case has been added for the bootm measurement to test the new path, and the sandbox TPM2 driver has been updated to support this use case.

Changes since v12:
- Rebase on master.
- Add detail to documentation.

Changes since v11:
- Rebase on next. Sorry for the delay (been on leave).

Changes since v10:
- Fix commit message on efi_loader change
- Drop python test change
- Squash armv7 fix from Ilias

Changes since v9:
- Rebase and add Ilias' fixes (thanks!)

Changes since v8:
- Fix a sandbox driver off-by-one error in checking the property type.
- Fix log parsing again - any data corruption seen while replaying the event log was failing the entire measurement.
- Added an option to ignore the existing log and a configuration option for systems to select that for the bootm measurement. This would only be selected for systems that know that U-Boot is the first stage bootloader. This is necessary because the reserved memory region may persist through resets and so U-Boot attempts to append to the previous boot's log.

Changes since v7:
- Change name of tcg2_init_log and add more documentation
- Add a check, when parsing the event log header, to ensure that the previous stage bootloader used all the active PCRs.
- Change name of tcg2_log_find_end
- Fix the greater than or equal to check to exit the log parsing
- Make sure log_position is 0 if there is any error discovering the log
- Return errors parsing the log if the data is corrupt so that we don't end up with half a log

Changes since v6:
- Added comment for bootm_measure
- Fixed line length in bootm_measure
- Added Linaro copyright for all the EFI moved code
- Changed tcg2_init_log (and by extension, tcg2_measurement_init) to copy any discovered event log to the user's log if passed in.

Changes since v5:
- Re-ordered the patches to put the sandbox TPM driver patch second
- Remove unused platform_get_eventlog in efi_tcg2.c
- First look for tpm_event_log_* properties instead of linux,sml-*
- Fix efi_tcg2.c compilation
- Select SHA* configs
- Remove the !SANDBOX dependency for EFI TCG2
- Only compile in the measurement u-boot command when CONFIG_MEASURED_BOOT is enabled

Changes since v4:
- Remove tcg2_measure_event function and check for NULL data in tcg2_measure_data
- Use tpm_auto_startup
- Fix efi_tcg2.c compilation for removing tcg2_pcr_read function
- Change PCR indexes for initrd and dtb
- Drop u8 casting in measurement test
- Use bullets in documentation

Changes since v3:
- Reordered headers
- Refactored more of EFI code into common code
Removed digest_info structure and instead used the common alg_to_mask
and alg_to_len
Improved event log parsing in common code to get it equivalent to EFI
Common code now extends PCR if previous bootloader stage couldn't No need to allocate memory in the common code, so EFI copies the discovered buffer like it did before
Rename efi measure_event function

Changes since v2:
- Add documentation.
- Changed reserved memory address to the top of the RAM for sandbox dts.
- Add measure state to booti and bootz.
- Skip measurement for EFI images that should be measured

Changes since v1:
- Refactor TPM layer functions to allow EFI system to use them, and remove duplicate EFI functions.
- Add test case
- Drop #ifdefs for bootm
- Add devicetree measurement config option
- Update sandbox TPM driver

Eddie James (6):
tpm: Fix spelling for tpmu_ha union
tpm: sandbox: Update for needed TPM2 capabilities
tpm: Support boot measurements
bootm: Support boot measurement
test: Add sandbox TPM boot measurement
doc: Add measured boot documentation

Ilias Apalodimas (2):
efi_loader: fix EFI_ENTRY point on get_active_pcr_banks
test: use a non system PCR for testing PCR extend

 arch/sandbox/dts/sandbox.dtsi  |  13 +
 arch/sandbox/dts/test.dts      |  13 +
 boot/Kconfig                   |  32 ++
 boot/bootm.c                   |  74 +++
 cmd/booti.c                    |   1 +
 cmd/bootm.c                    |   2 +
 cmd/bootz.c                    |   1 +
 configs/sandbox_defconfig      |   1 +
 doc/usage/index.rst            |   1 +
 doc/usage/measured_boot.rst    |  23 +
 drivers/tpm/tpm2_tis_sandbox.c | 100 ++--
 include/bootm.h                |  11 +
 include/efi_tcg2.h             |  44 --
 include/image.h                |   1 +
 include/test/suites.h          |   1 +
 include/tpm-v2.h               | 263 ++++++++++-
 lib/Kconfig                    |   4 +
 lib/efi_loader/Kconfig         |   2 -
 lib/efi_loader/efi_tcg2.c      | 823 ++++-----------------------------
 lib/tpm-v2.c                   | 814 ++++++++++++++++++++++++++++++++
 test/boot/Makefile             |   1 +
 test/boot/measurement.c        |  66 +++
 test/cmd_ut.c                  |   4 +
 test/py/tests/test_tpm2.py     |  16 +-
 24 files changed, 1482 insertions(+), 829 deletions(-)
 create mode 100644 doc/usage/measured_boot.rst
 create mode 100644 test/boot/measurement.c
Signature · 1 line
2.39.3
#2 lore raw

PATCH v13 2/8 tpm: sandbox: Update for needed TPM2 capabilities

The driver needs to support getting the PCRs in the capabilities command. Fix various other things and support the max number of PCRs for TPM2.
Remove the !SANDBOX dependency for EFI TCG2 as well.

---
Changes since v8:
- Use >= for checking the property against TPM2_PROPERTIES_OFFSET

Changes since v5:
- Remove the !SANDBOX dependency for EFI TCG2

 drivers/tpm/tpm2_tis_sandbox.c | 100 ++++++++++++++++++++++++---------
 lib/efi_loader/Kconfig         |   2 -
 2 files changed, 72 insertions(+), 30 deletions(-)
drivers/tpm/tpm2_tis_sandbox.c +72 -28
index e4004cfcca..d15a28d9fc 100644
@@ -22,11 +22,6 @@ enum tpm2_hierarchy {
 	TPM2_HIERARCHY_NB,
 };
 
-/* Subset of supported capabilities */
-enum tpm2_capability {
-	TPM_CAP_TPM_PROPERTIES = 0x6,
-};
-
 /* Subset of supported properties */
 #define TPM2_PROPERTIES_OFFSET 0x0000020E
 
@@ -38,7 +33,8 @@ enum tpm2_cap_tpm_property {
 	TPM2_PROPERTY_NB,
 };
 
-#define SANDBOX_TPM_PCR_NB 1
+#define SANDBOX_TPM_PCR_NB TPM2_MAX_PCRS
+#define SANDBOX_TPM_PCR_SELECT_MAX	((SANDBOX_TPM_PCR_NB + 7) / 8)
 
 /*
  * Information about our TPM emulation. This is preserved in the sandbox
@@ -433,7 +429,7 @@ static int sandbox_tpm2_xfer(struct udevice *dev, const u8 *sendbuf,
 	int i, j;
 
 	/* TPM2_GetProperty */
-	u32 capability, property, property_count;
+	u32 capability, property, property_count, val;
 
 	/* TPM2_PCR_Read/Extend variables */
 	int pcr_index = 0;
@@ -542,19 +538,32 @@ static int sandbox_tpm2_xfer(struct udevice *dev, const u8 *sendbuf,
 	case TPM2_CC_GET_CAPABILITY:
 		capability = get_unaligned_be32(sent);
 		sent += sizeof(capability);
-		if (capability != TPM_CAP_TPM_PROPERTIES) {
-			printf("Sandbox TPM only support TPM_CAPABILITIES\n");
-			return TPM2_RC_HANDLE;
-		}
-
 		property = get_unaligned_be32(sent);
 		sent += sizeof(property);
-		property -= TPM2_PROPERTIES_OFFSET;
-
 		property_count = get_unaligned_be32(sent);
 		sent += sizeof(property_count);
-		if (!property_count ||
-		    property + property_count > TPM2_PROPERTY_NB) {
+
+		switch (capability) {
+		case TPM2_CAP_PCRS:
+			break;
+		case TPM2_CAP_TPM_PROPERTIES:
+			if (!property_count) {
+				rc = TPM2_RC_HANDLE;
+				return sandbox_tpm2_fill_buf(recv, recv_len,
+							     tag, rc);
+			}
+
+			if (property >= TPM2_PROPERTIES_OFFSET &&
+			    ((property - TPM2_PROPERTIES_OFFSET) +
+			     property_count > TPM2_PROPERTY_NB)) {
+				rc = TPM2_RC_HANDLE;
+				return sandbox_tpm2_fill_buf(recv, recv_len,
+							     tag, rc);
+			}
+			break;
+		default:
+			printf("Sandbox TPM2 only supports TPM2_CAP_PCRS or "
+			       "TPM2_CAP_TPM_PROPERTIES\n");
 			rc = TPM2_RC_HANDLE;
 			return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
 		}
@@ -578,18 +587,53 @@ static int sandbox_tpm2_xfer(struct udevice *dev, const u8 *sendbuf,
 		put_unaligned_be32(capability, recv);
 		recv += sizeof(capability);
 
-		/* Give the number of properties that follow */
-		put_unaligned_be32(property_count, recv);
-		recv += sizeof(property_count);
-
-		/* Fill with the properties */
-		for (i = 0; i < property_count; i++) {
-			put_unaligned_be32(TPM2_PROPERTIES_OFFSET + property +
-					   i, recv);
-			recv += sizeof(property);
-			put_unaligned_be32(tpm->properties[property + i],
-					   recv);
-			recv += sizeof(property);
+		switch (capability) {
+		case TPM2_CAP_PCRS:
+			/* Give the number of algorithms supported - just SHA256 */
+			put_unaligned_be32(1, recv);
+			recv += sizeof(u32);
+
+			/* Give SHA256 algorithm */
+			put_unaligned_be16(TPM2_ALG_SHA256, recv);
+			recv += sizeof(u16);
+
+			/* Select the PCRs supported */
+			*recv = SANDBOX_TPM_PCR_SELECT_MAX;
+			recv++;
+
+			/* Activate all the PCR bits */
+			for (i = 0; i < SANDBOX_TPM_PCR_SELECT_MAX; ++i) {
+				*recv = 0xff;
+				recv++;
+			}
+			break;
+		case TPM2_CAP_TPM_PROPERTIES:
+			/* Give the number of properties that follow */
+			put_unaligned_be32(property_count, recv);
+			recv += sizeof(property_count);
+
+			/* Fill with the properties */
+			for (i = 0; i < property_count; i++) {
+				put_unaligned_be32(property + i, recv);
+				recv += sizeof(property);
+				if (property >= TPM2_PROPERTIES_OFFSET) {
+					val = tpm->properties[(property -
+						TPM2_PROPERTIES_OFFSET) + i];
+				} else {
+					switch (property) {
+					case TPM2_PT_PCR_COUNT:
+						val = SANDBOX_TPM_PCR_NB;
+						break;
+					default:
+						val = 0xffffffff;
+						break;
+					}
+				}
+
+				put_unaligned_be32(val, recv);
+				recv += sizeof(property);
+			}
+			break;
 		}
 
 		/* Add trailing \0 */
lib/efi_loader/Kconfig +0 -2
index d20aaab6db..4ccd26f94a 100644
@@ -379,8 +379,6 @@ config EFI_TCG2_PROTOCOL
 	bool "EFI_TCG2_PROTOCOL support"
 	default y
 	depends on TPM_V2
-	# Sandbox TPM currently fails on GetCapabilities needed for TCG2
-	depends on !SANDBOX
 	select SHA1
 	select SHA256
 	select SHA384
Signature · 1 line
2.39.3
#1 lore raw

PATCH v13 1/8 tpm: Fix spelling for tpmu_ha union

In reply to Eddie James

tmpu -> tpmu

---
 include/tpm-v2.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/tpm-v2.h +2 -2
index 2b6980e441..6684033deb 100644
@@ -169,7 +169,7 @@ struct tcg_pcr_event {
 /**
  * Definition of TPMU_HA Union
  */
-union tmpu_ha {
+union tpmu_ha {
 	u8 sha1[TPM2_SHA1_DIGEST_SIZE];
 	u8 sha256[TPM2_SHA256_DIGEST_SIZE];
 	u8 sm3_256[TPM2_SM3_256_DIGEST_SIZE];
@@ -185,7 +185,7 @@ union tmpu_ha {
  */
 struct tpmt_ha {
 	u16 hash_alg;
-	union tmpu_ha digest;
+	union tpmu_ha digest;
 } __packed;
 
 /**
Signature · 1 line
2.39.3
#9 lore raw

PATCH v13 7/8 efi_loader: fix EFI_ENTRY point on get_active_pcr_banks

In reply to Eddie James

From: Ilias Apalodimas <[email protected]>

efi_tcg2_get_active_pcr_banks doesn't immediately call the EFI_ENTRY() wrapper once it enters the function. Move the call a few lines above to cover the error cases properly as well.

---
 lib/efi_loader/efi_tcg2.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/efi_loader/efi_tcg2.c +2 -2
index 7ada536568..dae1b166c8 100644
@@ -949,16 +949,16 @@ efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
 	struct udevice *dev;
 	efi_status_t ret;
 
+	EFI_ENTRY("%p, %p", this, active_pcr_banks);
+
 	if (!this || !active_pcr_banks) {
 		ret = EFI_INVALID_PARAMETER;
 		goto out;
 	}
-
 	ret = tcg2_platform_get_tpm2(&dev);
 	if (ret != EFI_SUCCESS)
 		goto out;
 
-	EFI_ENTRY("%p, %p", this, active_pcr_banks);
 	ret = tcg2_get_active_pcr_banks(dev, active_pcr_banks);
 
 out:
Signature · 1 line
2.39.3
#5 lore raw

PATCH v13 5/8 test: Add sandbox TPM boot measurement

In reply to Eddie James

Use the sandbox TPM driver to measure some boot images in a unit test case.

---
Changes since v5:
- Only compile in the measurement u-boot command when CONFIG_MEASURED_BOOT is enabled.

 arch/sandbox/dts/sandbox.dtsi | 13 +++++++
 arch/sandbox/dts/test.dts     | 13 +++++++
 configs/sandbox_defconfig     |  1 +
 include/test/suites.h         |  1 +
 test/boot/Makefile            |  1 +
 test/boot/measurement.c       | 66 +++++++++++++++++++++++++++++++++++
 test/cmd_ut.c                 |  4 +++
 7 files changed, 99 insertions(+)
 create mode 100644 test/boot/measurement.c
arch/sandbox/dts/sandbox.dtsi +13 -0
index ff7e5584c5..241f397ba6 100644
@@ -4,11 +4,23 @@
  * and sandbox64 builds.
  */
 
+#include <config.h>
 #include <dt-bindings/input/input.h>
 
 #define USB_CLASS_HUB			9
 
 / {
+	reserved-memory {
+		#address-cells = <1>;
+		#size-cells = <1>;
+		ranges;
+
+		event_log: tcg_event_log {
+			no-map;
+			reg = <(CFG_SYS_SDRAM_SIZE - 0x2000) 0x2000>;
+		};
+	};
+
 	binman {
 	};
 
@@ -342,6 +354,7 @@
 
 	tpm2 {
 		compatible = "sandbox,tpm2";
+		memory-region = <&event_log>;
 	};
 
 	triangle {
arch/sandbox/dts/test.dts +13 -0
index e430347356..67159b3d01 100644
@@ -9,6 +9,7 @@
 
 /dts-v1/;
 
+#include <config.h>
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/gpio/sandbox-gpio.h>
 #include <dt-bindings/input/input.h>
@@ -68,6 +69,17 @@
 		osd0 = "/osd";
 	};
 
+	reserved-memory {
+		#address-cells = <1>;
+		#size-cells = <1>;
+		ranges;
+
+		event_log: tcg_event_log {
+			no-map;
+			reg = <(CFG_SYS_SDRAM_SIZE - 0x2000) 0x2000>;
+		};
+	};
+
 	binman: binman {
 	};
 
@@ -1423,6 +1435,7 @@
 
 	tpm2 {
 		compatible = "sandbox,tpm2";
+		memory-region = <&event_log>;
 	};
 
 	tpm {
configs/sandbox_defconfig +1 -0
index 47417cb039..757dcac4ca 100644
@@ -349,3 +349,4 @@ CONFIG_TEST_FDTDEC=y
 CONFIG_UNIT_TEST=y
 CONFIG_UT_TIME=y
 CONFIG_UT_DM=y
+CONFIG_MEASURED_BOOT=y
include/test/suites.h +1 -0
index 1c7dc65966..48ed549c13 100644
@@ -45,6 +45,7 @@ int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_loadm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
+int do_ut_measurement(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc,
test/boot/Makefile +1 -0
index 52947580ae..068522cb9e 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_BOOTSTD) += bootdev.o bootstd_common.o bootflow.o bootmeth.o
 obj-$(CONFIG_FIT) += image.o
+obj-$(CONFIG_MEASURED_BOOT) += measurement.o
 
 obj-$(CONFIG_EXPO) += expo.o
 obj-$(CONFIG_CEDIT) += cedit.o
test/boot/measurement.c new file +66 -0

66 lines not on this page · raw message

test/cmd_ut.c +4 -0
index 477d475952..32d270fc89 100644
@@ -99,6 +99,10 @@ static struct cmd_tbl cmd_ut_sub[] = {
 #if CONFIG_IS_ENABLED(UT_UNICODE) && !defined(API_BUILD)
 	U_BOOT_CMD_MKENT(unicode, CONFIG_SYS_MAXARGS, 1, do_ut_unicode, "", ""),
 #endif
+#ifdef CONFIG_MEASURED_BOOT
+	U_BOOT_CMD_MKENT(measurement, CONFIG_SYS_MAXARGS, 1, do_ut_measurement,
+			 "", ""),
+#endif
 #ifdef CONFIG_SANDBOX
 	U_BOOT_CMD_MKENT(compression, CONFIG_SYS_MAXARGS, 1, do_ut_compression,
 			 "", ""),
Signature · 1 line
2.39.3
#6 lore raw

PATCH v13 6/8 doc: Add measured boot documentation

In reply to Eddie James

Briefly describe the feature and specify the requirements.

---
Changes since v12:
- Add a bit of detail about OS usage and what pieces are measured

 doc/usage/index.rst         |  1 +
 doc/usage/measured_boot.rst | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
 create mode 100644 doc/usage/measured_boot.rst
doc/usage/index.rst +1 -0
index 98b4719c40..bf53bb6bda 100644
@@ -14,6 +14,7 @@ Use U-Boot
    partitions
    cmdline
    semihosting
+   measured_boot
 
 Shell commands
 --------------
doc/usage/measured_boot.rst new file +31 -0
new file mode 100644
index 0000000000..0aad590859
@@ -0,0 +1,31 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+Measured Boot
+=====================
+
+U-Boot can perform a measured boot, the process of hashing various components
+of the boot process, extending the results in the TPM and logging the
+component's measurement in memory for the operating system to consume.
+
+By default, U-Boot will measure the operating system (linux) image, the
+initrd image, and the "bootargs" environment variable. By enabling
+CONFIG_MEASURE_DEVICETREE, U-Boot will also measure the devicetree image.
+
+The operating system typically would verify that the hashes found in the
+TPM PCRs match the contents of the event log. This can further be checked
+against the hash results of previous boots.
+
+Requirements
+---------------------
+
+* A hardware TPM 2.0 supported by the U-Boot drivers
+* CONFIG_TPM=y
+* CONFIG_MEASURED_BOOT=y
+* Device-tree configuration of the TPM device to specify the memory area
+  for event logging. The TPM device node must either contain a phandle to
+  a reserved memory region or "linux,sml-base" and "linux,sml-size"
+  indicating the address and size of the memory region. An example can be
+  found in arch/sandbox/dts/test.dts
+* The operating system must also be configured to use the memory regions
+  specified in the U-Boot device-tree in order to make use of the event
+  log.
Signature · 1 line
2.39.3
#3 lore raw

PATCH v13 3/8 tpm: Support boot measurements

In reply to Eddie James

Add TPM2 functions to support boot measurement. This includes starting up the TPM, initializing/appending the event log, and measuring the U-Boot version. Much of the code was used in the EFI subsystem, so remove it there and use the common functions.

---
Changes since v10:
- Fix compile warning for armv7 (thanks Ilias)

Changes since v8:
- Fix log parsing again - any data corruption seen while replaying the event log was failing the entire measurement.
- Added an option to ignore the existing log. This should only be used for systems that know that U-Boot is the first stage bootloader. This is necessary because the reserved memory region may persist through resets and so U-Boot attempts to append to the previous boot's log.

Changes since v7:
- Change name of tcg2_init_log and add more documentation
- Add a check, when parsing the event log header, to ensure that the previous stage bootloader used all the active PCRs.
- Change name of tcg2_log_find_end
- Fix the greater than or equal to check to exit the log parsing
- Make sure log_position is 0 if there is any error discovering the log
- Return errors parsing the log if the data is corrupt so that we don't end up with half a log

Changes since v6:
- Added Linaro copyright for all the EFI moved code
- Changed tcg2_init_log (and by extension, tcg2_measurement_init) to copy any discovered event log to the user's log if passed in.

Changes since v5:
- Remove unused platform_get_eventlog in efi_tcg2.c
- First look for tpm_event_log_* properties instead of linux,sml-*
- Fix efi_tcg2.c compilation
- Select SHA* configs

Changes since v4:
- Remove tcg2_measure_event function and check for NULL data in tcg2_measure_data
- Use tpm_auto_startup
- Fix efi_tcg2.c compilation for removing tcg2_pcr_read function

Changes since v3:
- Reordered headers
- Refactored more of EFI code into common code
Removed digest_info structure and instead used the common alg_to_mask
and alg_to_len
Improved event log parsing in common code to get it equivalent to EFI
Common code now extends PCR if previous bootloader stage couldn't No need to allocate memory in the common code, so EFI copies the discovered buffer like it did before
Rename efi measure_event function

Changes since v1:
- Refactor TPM layer functions to allow EFI system to use them, and remove duplicate EFI functions

 include/efi_tcg2.h        |  44 --
 include/tpm-v2.h          | 259 ++++++++++++
 lib/Kconfig               |   4 +
 lib/efi_loader/efi_tcg2.c | 821 ++++----------------------------------
 lib/tpm-v2.c              | 814 +++++++++++++++++++++++++++++++++++++
 5 files changed, 1154 insertions(+), 788 deletions(-)
include/efi_tcg2.h +0 -44
index b1c3abd097..b21c5cb3dd 100644
@@ -129,50 +129,6 @@ struct efi_tcg2_boot_service_capability {
 #define BOOT_SERVICE_CAPABILITY_MIN \
 	offsetof(struct efi_tcg2_boot_service_capability, number_of_pcr_banks)
 
-#define TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03 "Spec ID Event03"
-#define TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2 2
-#define TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 0
-#define TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2 2
-
-/**
- *  struct TCG_EfiSpecIdEventAlgorithmSize - hashing algorithm information
- *
- *  @algorithm_id:	algorithm defined in enum tpm2_algorithms
- *  @digest_size:	size of the algorithm
- */
-struct tcg_efi_spec_id_event_algorithm_size {
-	u16      algorithm_id;
-	u16      digest_size;
-} __packed;
-
-/**
- * struct TCG_EfiSpecIDEventStruct - content of the event log header
- *
- * @signature:			signature, set to Spec ID Event03
- * @platform_class:		class defined in TCG ACPI Specification
- *				Client  Common Header.
- * @spec_version_minor:		minor version
- * @spec_version_major:		major version
- * @spec_version_errata:	major version
- * @uintn_size:			size of the efi_uintn_t fields used in various
- *				data structures used in this specification.
- *				0x01 indicates u32  and 0x02  indicates u64
- * @number_of_algorithms:	hashing algorithms used in this event log
- * @digest_sizes:		array of number_of_algorithms pairs
- *				1st member defines the algorithm id
- *				2nd member defines the algorithm size
- */
-struct tcg_efi_spec_id_event {
-	u8 signature[16];
-	u32 platform_class;
-	u8 spec_version_minor;
-	u8 spec_version_major;
-	u8 spec_errata;
-	u8 uintn_size;
-	u32 number_of_algorithms;
-	struct tcg_efi_spec_id_event_algorithm_size digest_sizes[];
-} __packed;
-
 /**
  * struct tdEFI_TCG2_FINAL_EVENTS_TABLE - log entries after Get Event Log
  * @version:		version number for this structure
include/tpm-v2.h +259 -0

283 lines not on this page · raw message

lib/Kconfig +4 -0
index f6ca559897..19649517a3 100644
@@ -431,6 +431,10 @@ config TPM
 	bool "Trusted Platform Module (TPM) Support"
 	depends on DM
 	imply DM_RNG
+	select SHA1
+	select SHA256
+	select SHA384
+	select SHA512
 	help
 	  This enables support for TPMs which can be used to provide security
 	  features for your board. The TPM can be connected via LPC or I2C
lib/efi_loader/efi_tcg2.c +77 -744

1,081 lines not on this page · raw message

lib/tpm-v2.c +814 -0

836 lines not on this page · raw message

Signature · 1 line
2.39.3
#11 lore raw

PATCH v13 8/8 test: use a non system PCR for testing PCR extend

In reply to Eddie James

From: Ilias Apalodimas <[email protected]>

We currently use PCR 0 for testing the PCR read/extend functionality in our selftests. How ever those PCRs are defined by the TCG spec for platform use. For example if the tests run *after* the efi subsystem initialization, which extends PCRs 0 & 7 it will give a false positive.

So let's switch over to a PCR which is more suitable and is defined for OS use. It's worth noting that we are using PCR10 here, since PCR9 is used internally by U-Boot if we choose to measure the loaded DTB

---
 test/py/tests/test_tpm2.py | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
test/py/tests/test_tpm2.py +8 -8
index c2579fa02c..47392b87a9 100644
@@ -239,7 +239,7 @@ def test_tpm2_dam_parameters(u_boot_console):
 def test_tpm2_pcr_read(u_boot_console):
     """Execute a TPM2_PCR_Read command.
 
-    Perform a PCR read of the 0th PCR. Must be zero.
+    Perform a PCR read of the 10th PCR. Must be zero.
     """
     if is_sandbox(u_boot_console):
         tpm2_sandbox_init(u_boot_console)
@@ -247,7 +247,7 @@ def test_tpm2_pcr_read(u_boot_console):
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)
 
-    read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % ram)
+    read_pcr = u_boot_console.run_command('tpm2 pcr_read 10 0x%x' % ram)
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
 
@@ -257,7 +257,7 @@ def test_tpm2_pcr_read(u_boot_console):
     updates = int(re.findall(r'\d+', str)[0])
 
     # Check the output value
-    assert 'PCR #0 content' in read_pcr
+    assert 'PCR #10 content' in read_pcr
     assert '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00' in read_pcr
 
 @pytest.mark.buildconfigspec('cmd_tpm_v2')
@@ -275,19 +275,19 @@ def test_tpm2_pcr_extend(u_boot_console):
     force_init(u_boot_console)
     ram = u_boot_utils.find_ram_base(u_boot_console)
 
-    read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
+    read_pcr = u_boot_console.run_command('tpm2 pcr_read 10 0x%x' % (ram + 0x20))
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
     str = re.findall(r'\d+ known updates', read_pcr)[0]
     updates = int(re.findall(r'\d+', str)[0])
 
-    u_boot_console.run_command('tpm2 pcr_extend 0 0x%x' % ram)
+    u_boot_console.run_command('tpm2 pcr_extend 10 0x%x' % ram)
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
 
     # Read the value back into a different place so we can still use 'ram' as
     # our zero bytes
-    read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
+    read_pcr = u_boot_console.run_command('tpm2 pcr_read 10 0x%x' % (ram + 0x20))
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
     assert 'f5 a5 fd 42 d1 6a 20 30 27 98 ef 6e d3 09 97 9b' in read_pcr
@@ -297,11 +297,11 @@ def test_tpm2_pcr_extend(u_boot_console):
     new_updates = int(re.findall(r'\d+', str)[0])
     assert (updates + 1) == new_updates
 
-    u_boot_console.run_command('tpm2 pcr_extend 0 0x%x' % ram)
+    u_boot_console.run_command('tpm2 pcr_extend 10 0x%x' % ram)
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
 
-    read_pcr = u_boot_console.run_command('tpm2 pcr_read 0 0x%x' % (ram + 0x20))
+    read_pcr = u_boot_console.run_command('tpm2 pcr_read 10 0x%x' % (ram + 0x20))
     output = u_boot_console.run_command('echo $?')
     assert output.endswith('0')
     assert '7a 05 01 f5 95 7b df 9c b3 a8 ff 49 66 f0 22 65' in read_pcr
Signature · 1 line
2.39.3
#4 lore raw

PATCH v13 4/8 bootm: Support boot measurement

In reply to Eddie James

Add a configuration option to measure the boot through the bootm function. Add the measurement state to the booti and bootz paths as well.

---
Changes since v8:
- Added a configuration option to select to ignore any existing event log. This would only be selected for systems that know that U-Boot is the first stage bootloader. This is necessary because the reserved memory region may persist through resets and so U-Boot attempts to append to the previous boot's log.

Changes since v6:
- Added comment for bootm_measure
- Fixed line length in bootm_measure

 boot/Kconfig    | 32 +++++++++++++++++++++
 boot/bootm.c    | 74 +++++++++++++++++++++++++++++++++++++++++++++++++
 cmd/booti.c     |  1 +
 cmd/bootm.c     |  2 ++
 cmd/bootz.c     |  1 +
 include/bootm.h | 11 ++++++++
 include/image.h |  1 +
 7 files changed, 122 insertions(+)
boot/Kconfig +32 -0
index a01e6cb8aa..abbc72f4cf 100644
@@ -685,6 +685,38 @@ config LEGACY_IMAGE_FORMAT
 	  loaded. If a board needs the legacy image format support in this
 	  case, enable it here.
 
+config MEASURED_BOOT
+	bool "Measure boot images and configuration to TPM and event log"
+	depends on HASH && TPM_V2
+	help
+	  This option enables measurement of the boot process. Measurement
+	  involves creating cryptographic hashes of the binary images that
+	  are booting and storing them in the TPM. In addition, a log of
+	  these hashes is stored in memory for the OS to verify the booted
+	  images and configuration. Enable this if the OS has configured
+	  some memory area for the event log and you intend to use some
+	  attestation tools on your system.
+
+if MEASURED_BOOT
+	config MEASURE_DEVICETREE
+	bool "Measure the devicetree image"
+	default y if MEASURED_BOOT
+	help
+	  On some platforms, the devicetree is not static as it may contain
+	  random MAC addresses or other such data that changes each boot.
+	  Therefore, it should not be measured into the TPM. In that case,
+	  disable the measurement here.
+
+	config MEASURE_IGNORE_LOG
+	bool "Ignore the existing event log"
+	default n
+	help
+	  On platforms that use an event log memory region that persists
+	  through system resets and are the first stage bootloader, then
+	  this option should be enabled to ignore any existing data in the
+	  event log memory region.
+endif # MEASURED_BOOT
+
 config SUPPORT_RAW_INITRD
 	bool "Enable raw initrd images"
 	help
boot/bootm.c +74 -0

92 lines not on this page · raw message

cmd/booti.c +1 -0
index 1d531bdd06..a6c7db272c 100644
@@ -127,6 +127,7 @@ int do_booti(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
 			      BOOTM_STATE_RAMDISK |
 #endif
+			      BOOTM_STATE_MEASURE |
 			      BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
 			      BOOTM_STATE_OS_GO,
 			      &images, 1);
cmd/bootm.c +2 -0
index 3e504ccfe8..6ded091dd5 100644
@@ -147,6 +147,8 @@ int do_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 		BOOTM_STATE_OS_GO;
 	if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH))
 		states |= BOOTM_STATE_RAMDISK;
+	if (IS_ENABLED(CONFIG_MEASURED_BOOT))
+		states |= BOOTM_STATE_MEASURE;
 	if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS))
 		states |= BOOTM_STATE_OS_CMDLINE;
 	ret = do_bootm_states(cmdtp, flag, argc, argv, states, &images, 1);
cmd/bootz.c +1 -0
index 742889f21d..dd6fe4904b 100644
@@ -81,6 +81,7 @@ int do_bootz(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
 			      BOOTM_STATE_RAMDISK |
 #endif
+			      BOOTM_STATE_MEASURE |
 			      BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
 			      BOOTM_STATE_OS_GO,
 			      &images, 1);
include/bootm.h +11 -0
index c3c7336207..10a1bd65a7 100644
@@ -56,6 +56,17 @@ ulong bootm_disable_interrupts(void);
 int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
 		      ulong size);
 
+/*
+ * Measure the boot images. Measurement is the process of hashing some binary
+ * data and storing it into secure memory, i.e. TPM PCRs. In addition, each
+ * measurement is logged into the platform event log such that the operating
+ * system can access it and perform attestation of the boot.
+ *
+ * @images:	The structure containing the various images to boot (linux,
+ *		initrd, dts, etc.)
+ */
+int bootm_measure(struct bootm_headers *images);
+
 int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
 		    char *const argv[], int states, struct bootm_headers *images,
 		    int boot_progress);
include/image.h +1 -0
index 5f85bf84a2..2e3cf839ee 100644
@@ -409,6 +409,7 @@ struct bootm_headers {
 #define BOOTM_STATE_OS_FAKE_GO	0x00000200	/* 'Almost' run the OS */
 #define BOOTM_STATE_OS_GO	0x00000400
 #define BOOTM_STATE_PRE_LOAD	0x00000800
+#define BOOTM_STATE_MEASURE	0x00001000
 	int		state;
 
 #if defined(CONFIG_LMB) && !defined(USE_HOSTCC)
Signature · 1 line
2.39.3
#12 lore raw
In reply to Eddie James

Thanks Eddie

I've queued this up on public CI. I also have an internal one, this one failed to add the TF-A eventlog, but everything else looks fine. I'll have a look tomorrow, but since this used to work on earlier versions I suspect it's going to be trivial to fix

Cheers
/Ilias

Quoted 139 lines · Eddie James

This series adds support for measuring the boot images more generically than the existing EFI support. Several EFI functions have been moved to the TPM layer. The series includes optional measurement from the bootm command.
A new test case has been added for the bootm measurement to test the new path, and the sandbox TPM2 driver has been updated to support this use case.

Changes since v12:
- Rebase on master.
- Add detail to documentation.

Changes since v11:
- Rebase on next. Sorry for the delay (been on leave).

Changes since v10:
- Fix commit message on efi_loader change
- Drop python test change
- Squash armv7 fix from Ilias

Changes since v9:
- Rebase and add Ilias' fixes (thanks!)

Changes since v8:
- Fix a sandbox driver off-by-one error in checking the property type.
- Fix log parsing again - any data corruption seen while replaying the event log was failing the entire measurement.
- Added an option to ignore the existing log and a configuration option for systems to select that for the bootm measurement. This would only be selected for systems that know that U-Boot is the first stage bootloader. This is necessary because the reserved memory region may persist through resets and so U-Boot attempts to append to the previous boot's log.

Changes since v7:
- Change name of tcg2_init_log and add more documentation
- Add a check, when parsing the event log header, to ensure that the previous stage bootloader used all the active PCRs.
- Change name of tcg2_log_find_end
- Fix the greater than or equal to check to exit the log parsing
- Make sure log_position is 0 if there is any error discovering the log
- Return errors parsing the log if the data is corrupt so that we don't end up with half a log

Changes since v6:
- Added comment for bootm_measure
- Fixed line length in bootm_measure
- Added Linaro copyright for all the EFI moved code
- Changed tcg2_init_log (and by extension, tcg2_measurement_init) to copy any discovered event log to the user's log if passed in.

Changes since v5:
- Re-ordered the patches to put the sandbox TPM driver patch second
- Remove unused platform_get_eventlog in efi_tcg2.c
- First look for tpm_event_log_* properties instead of linux,sml-*
- Fix efi_tcg2.c compilation
- Select SHA* configs
- Remove the !SANDBOX dependency for EFI TCG2
- Only compile in the measurement u-boot command when CONFIG_MEASURED_BOOT is enabled

Changes since v4:
- Remove tcg2_measure_event function and check for NULL data in tcg2_measure_data
- Use tpm_auto_startup
- Fix efi_tcg2.c compilation for removing tcg2_pcr_read function
- Change PCR indexes for initrd and dtb
- Drop u8 casting in measurement test
- Use bullets in documentation

Changes since v3:
- Reordered headers
- Refactored more of EFI code into common code
Removed digest_info structure and instead used the common alg_to_mask
and alg_to_len
Improved event log parsing in common code to get it equivalent to EFI
Common code now extends PCR if previous bootloader stage couldn't No need to allocate memory in the common code, so EFI copies the discovered buffer like it did before
Rename efi measure_event function

Changes since v2:
- Add documentation.
- Changed reserved memory address to the top of the RAM for sandbox dts.
- Add measure state to booti and bootz.
- Skip measurement for EFI images that should be measured

Changes since v1:
- Refactor TPM layer functions to allow EFI system to use them, and remove duplicate EFI functions.
- Add test case
- Drop #ifdefs for bootm
- Add devicetree measurement config option
- Update sandbox TPM driver

Eddie James (6):
tpm: Fix spelling for tpmu_ha union
tpm: sandbox: Update for needed TPM2 capabilities
tpm: Support boot measurements
bootm: Support boot measurement
test: Add sandbox TPM boot measurement
doc: Add measured boot documentation

Ilias Apalodimas (2):
efi_loader: fix EFI_ENTRY point on get_active_pcr_banks
test: use a non system PCR for testing PCR extend

 arch/sandbox/dts/sandbox.dtsi  |  13 +
 arch/sandbox/dts/test.dts      |  13 +
 boot/Kconfig                   |  32 ++
 boot/bootm.c                   |  74 +++
 cmd/booti.c                    |   1 +
 cmd/bootm.c                    |   2 +
 cmd/bootz.c                    |   1 +
 configs/sandbox_defconfig      |   1 +
 doc/usage/index.rst            |   1 +
 doc/usage/measured_boot.rst    |  23 +
 drivers/tpm/tpm2_tis_sandbox.c | 100 ++--
 include/bootm.h                |  11 +
 include/efi_tcg2.h             |  44 --
 include/image.h                |   1 +
 include/test/suites.h          |   1 +
 include/tpm-v2.h               | 263 ++++++++++-
 lib/Kconfig                    |   4 +
 lib/efi_loader/Kconfig         |   2 -
 lib/efi_loader/efi_tcg2.c      | 823 ++++-----------------------------
 lib/tpm-v2.c                   | 814 ++++++++++++++++++++++++++++++++
 test/boot/Makefile             |   1 +
 test/boot/measurement.c        |  66 +++
 test/cmd_ut.c                  |   4 +
 test/py/tests/test_tpm2.py     |  16 +-
 24 files changed, 1482 insertions(+), 829 deletions(-)
 create mode 100644 doc/usage/measured_boot.rst
 create mode 100644 test/boot/measurement.c

--
2.39.3

Reviewed-by on v13 7/8
#10 lore raw

Re: [PATCH v13 7/8] efi_loader: fix EFI_ENTRY point on get_active_pcr_banks

In reply to Eddie James
Quoted 7 lines · Eddie James

From: Ilias Apalodimas <[email protected]>

efi_tcg2_get_active_pcr_banks doesn't immediately call the EFI_ENTRY() wrapper once it enters the function. Move the call a few lines above to cover the error cases properly as well.

Quoted 27 lines
---
  lib/efi_loader/efi_tcg2.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
lib/efi_loader/efi_tcg2.c +2 -2
index 7ada536568..dae1b166c8 100644
@@ -949,16 +949,16 @@ efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
  	struct udevice *dev;
  	efi_status_t ret;
 
+	EFI_ENTRY("%p, %p", this, active_pcr_banks);
+
  	if (!this || !active_pcr_banks) {
  		ret = EFI_INVALID_PARAMETER;
  		goto out;
  	}
-
  	ret = tcg2_platform_get_tpm2(&dev);
  	if (ret != EFI_SUCCESS)
  		goto out;
 
-	EFI_ENTRY("%p, %p", this, active_pcr_banks);
  	ret = tcg2_get_active_pcr_banks(dev, active_pcr_banks);
 
  out:
#7 lore raw

Re: [PATCH v13 6/8] doc: Add measured boot documentation

In reply to Eddie James
Quoted 31 lines · Eddie James

Briefly describe the feature and specify the requirements.

---
Changes since v12:
- Add a bit of detail about OS usage and what pieces are measured

  doc/usage/index.rst         |  1 +
  doc/usage/measured_boot.rst | 31 +++++++++++++++++++++++++++++++
  2 files changed, 32 insertions(+)

create mode 100644 doc/usage/measured_boot.rst

doc/usage/index.rst +1 -0
index 98b4719c40..bf53bb6bda 100644
@@ -14,6 +14,7 @@ Use U-Boot
     partitions
     cmdline
     semihosting
+   measured_boot
 
  Shell commands
  --------------
doc/usage/measured_boot.rst new file +0 -0
new file mode 100644
index 0000000000..0aad590859

doc/usage/measured_boot.rst line 10

Quoted 10 lines
doc/usage/measured_boot.rst new file +10 -0
@@ -0,0 +1,31 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+Measured Boot
+=====================
+
+U-Boot can perform a measured boot, the process of hashing various components
+of the boot process, extending the results in the TPM and logging the
+component's measurement in memory for the operating system to consume.
+
+By default, U-Boot will measure the operating system (linux) image, the

The OS image would mean the root file system? I guess you mean the loaded EFI binary here. Measured boot is not Linux specific.

We should consider different scenarios:

* U-Boot loading kernel, initrd, device-tree
* U-Boot loading GRUB (or another boot manager), device-tree, GRUB loading initrd
* U-Boot loading GRUB and passing a device-tree to GRUB, GRUB loading initrd and a new device-tree

With the documentation the user should be enabled to understand what U-Boot measures in each of these cases. Please, observe that upstream GRUB uses LoadImage() while distros (e.g. Debian and Ubuntu) tend to bypass LoadImage().

Arent't there some other objects measured?

- SMBIOS table
- ACPI tables (still to be implemented)
- EFI variables

Best regards

Heinrich

Quoted 21 lines
doc/usage/measured_boot.rst new file +21 -0
+initrd image, and the "bootargs" environment variable. By enabling
+CONFIG_MEASURE_DEVICETREE, U-Boot will also measure the devicetree image.
+
+The operating system typically would verify that the hashes found in the
+TPM PCRs match the contents of the event log. This can further be checked
+against the hash results of previous boots.
+
+Requirements
+---------------------
+
+* A hardware TPM 2.0 supported by the U-Boot drivers
+* CONFIG_TPM=y
+* CONFIG_MEASURED_BOOT=y
+* Device-tree configuration of the TPM device to specify the memory area
+  for event logging. The TPM device node must either contain a phandle to
+  a reserved memory region or "linux,sml-base" and "linux,sml-size"
+  indicating the address and size of the memory region. An example can be
+  found in arch/sandbox/dts/test.dts
+* The operating system must also be configured to use the memory regions
+  specified in the U-Boot device-tree in order to make use of the event
+  log.
#13 lore raw
In reply to Ilias Apalodimas

Hi Eddie,

Does the series compile for you against -master?
For qemu_arm64_defonfig I am getting compilation errors both locally and on the CI
https://source.denx.de/u-boot/custodians/u-boot-tpm/-/jobs/717362#L39

Thanks
/Ilias

Quoted 151 lines · Ilias Apalodimas

Thanks Eddie

I've queued this up on public CI. I also have an internal one, this one failed to add the TF-A eventlog, but everything else looks fine. I'll have a look tomorrow, but since this used to work on earlier versions I suspect it's going to be trivial to fix

Cheers
/Ilias

Quoted 139 lines · Eddie James

This series adds support for measuring the boot images more generically than the existing EFI support. Several EFI functions have been moved to the TPM layer. The series includes optional measurement from the bootm command.
A new test case has been added for the bootm measurement to test the new path, and the sandbox TPM2 driver has been updated to support this use case.

Changes since v12:
- Rebase on master.
- Add detail to documentation.

Changes since v11:
- Rebase on next. Sorry for the delay (been on leave).

Changes since v10:
- Fix commit message on efi_loader change
- Drop python test change
- Squash armv7 fix from Ilias

Changes since v9:
- Rebase and add Ilias' fixes (thanks!)

Changes since v8:
- Fix a sandbox driver off-by-one error in checking the property type.
- Fix log parsing again - any data corruption seen while replaying the event log was failing the entire measurement.
- Added an option to ignore the existing log and a configuration option for systems to select that for the bootm measurement. This would only be selected for systems that know that U-Boot is the first stage bootloader. This is necessary because the reserved memory region may persist through resets and so U-Boot attempts to append to the previous boot's log.

Changes since v7:
- Change name of tcg2_init_log and add more documentation
- Add a check, when parsing the event log header, to ensure that the previous stage bootloader used all the active PCRs.
- Change name of tcg2_log_find_end
- Fix the greater than or equal to check to exit the log parsing
- Make sure log_position is 0 if there is any error discovering the log
- Return errors parsing the log if the data is corrupt so that we don't end up with half a log

Changes since v6:
- Added comment for bootm_measure
- Fixed line length in bootm_measure
- Added Linaro copyright for all the EFI moved code
- Changed tcg2_init_log (and by extension, tcg2_measurement_init) to copy any discovered event log to the user's log if passed in.

Changes since v5:
- Re-ordered the patches to put the sandbox TPM driver patch second
- Remove unused platform_get_eventlog in efi_tcg2.c
- First look for tpm_event_log_* properties instead of linux,sml-*
- Fix efi_tcg2.c compilation
- Select SHA* configs
- Remove the !SANDBOX dependency for EFI TCG2
- Only compile in the measurement u-boot command when CONFIG_MEASURED_BOOT is enabled

Changes since v4:
- Remove tcg2_measure_event function and check for NULL data in tcg2_measure_data
- Use tpm_auto_startup
- Fix efi_tcg2.c compilation for removing tcg2_pcr_read function
- Change PCR indexes for initrd and dtb
- Drop u8 casting in measurement test
- Use bullets in documentation

Changes since v3:
- Reordered headers
- Refactored more of EFI code into common code
Removed digest_info structure and instead used the common alg_to_mask
and alg_to_len
Improved event log parsing in common code to get it equivalent to EFI
Common code now extends PCR if previous bootloader stage couldn't No need to allocate memory in the common code, so EFI copies the discovered buffer like it did before
Rename efi measure_event function

Changes since v2:
- Add documentation.
- Changed reserved memory address to the top of the RAM for sandbox dts.
- Add measure state to booti and bootz.
- Skip measurement for EFI images that should be measured

Changes since v1:
- Refactor TPM layer functions to allow EFI system to use them, and remove duplicate EFI functions.
- Add test case
- Drop #ifdefs for bootm
- Add devicetree measurement config option
- Update sandbox TPM driver

Eddie James (6):
tpm: Fix spelling for tpmu_ha union
tpm: sandbox: Update for needed TPM2 capabilities
tpm: Support boot measurements
bootm: Support boot measurement
test: Add sandbox TPM boot measurement
doc: Add measured boot documentation

Ilias Apalodimas (2):
efi_loader: fix EFI_ENTRY point on get_active_pcr_banks
test: use a non system PCR for testing PCR extend

 arch/sandbox/dts/sandbox.dtsi  |  13 +
 arch/sandbox/dts/test.dts      |  13 +
 boot/Kconfig                   |  32 ++
 boot/bootm.c                   |  74 +++
 cmd/booti.c                    |   1 +
 cmd/bootm.c                    |   2 +
 cmd/bootz.c                    |   1 +
 configs/sandbox_defconfig      |   1 +
 doc/usage/index.rst            |   1 +
 doc/usage/measured_boot.rst    |  23 +
 drivers/tpm/tpm2_tis_sandbox.c | 100 ++--
 include/bootm.h                |  11 +
 include/efi_tcg2.h             |  44 --
 include/image.h                |   1 +
 include/test/suites.h          |   1 +
 include/tpm-v2.h               | 263 ++++++++++-
 lib/Kconfig                    |   4 +
 lib/efi_loader/Kconfig         |   2 -
 lib/efi_loader/efi_tcg2.c      | 823 ++++-----------------------------
 lib/tpm-v2.c                   | 814 ++++++++++++++++++++++++++++++++
 test/boot/Makefile             |   1 +
 test/boot/measurement.c        |  66 +++
 test/cmd_ut.c                  |   4 +
 test/py/tests/test_tpm2.py     |  16 +-
 24 files changed, 1482 insertions(+), 829 deletions(-)
 create mode 100644 doc/usage/measured_boot.rst
 create mode 100644 test/boot/measurement.c

--
2.39.3

#14 lore raw
Quoted 6 lines · Ilias Apalodimas

Hi Eddie,

Does the series compile for you against -master?
For qemu_arm64_defonfig I am getting compilation errors both locally and on the CI
https://source.denx.de/u-boot/custodians/u-boot-tpm/-/jobs/717362#L39

Yea it was a problem rebasing. I'll send v14 soon.

Thanks,

Eddie

Quoted 155 lines

Thanks
/Ilias

Quoted 149 lines · Ilias Apalodimas

Thanks Eddie

I've queued this up on public CI. I also have an internal one, this one failed to add the TF-A eventlog, but everything else looks fine. I'll have a look tomorrow, but since this used to work on earlier versions I suspect it's going to be trivial to fix

Cheers
/Ilias

Quoted 138 lines · Eddie James

This series adds support for measuring the boot images more generically than the existing EFI support. Several EFI functions have been moved to the TPM layer. The series includes optional measurement from the bootm command.
A new test case has been added for the bootm measurement to test the new path, and the sandbox TPM2 driver has been updated to support this use case.

Changes since v12:
- Rebase on master.
- Add detail to documentation.

Changes since v11:
- Rebase on next. Sorry for the delay (been on leave).

Changes since v10:
- Fix commit message on efi_loader change
- Drop python test change
- Squash armv7 fix from Ilias

Changes since v9:
- Rebase and add Ilias' fixes (thanks!)

Changes since v8:
- Fix a sandbox driver off-by-one error in checking the property type.
- Fix log parsing again - any data corruption seen while replaying the event log was failing the entire measurement.
- Added an option to ignore the existing log and a configuration option for systems to select that for the bootm measurement. This would only be selected for systems that know that U-Boot is the first stage bootloader. This is necessary because the reserved memory region may persist through resets and so U-Boot attempts to append to the previous boot's log.

Changes since v7:
- Change name of tcg2_init_log and add more documentation
- Add a check, when parsing the event log header, to ensure that the previous stage bootloader used all the active PCRs.
- Change name of tcg2_log_find_end
- Fix the greater than or equal to check to exit the log parsing
- Make sure log_position is 0 if there is any error discovering the log
- Return errors parsing the log if the data is corrupt so that we don't end up with half a log

Changes since v6:
- Added comment for bootm_measure
- Fixed line length in bootm_measure
- Added Linaro copyright for all the EFI moved code
- Changed tcg2_init_log (and by extension, tcg2_measurement_init) to copy any discovered event log to the user's log if passed in.

Changes since v5:
- Re-ordered the patches to put the sandbox TPM driver patch second
- Remove unused platform_get_eventlog in efi_tcg2.c
- First look for tpm_event_log_* properties instead of linux,sml-*
- Fix efi_tcg2.c compilation
- Select SHA* configs
- Remove the !SANDBOX dependency for EFI TCG2
- Only compile in the measurement u-boot command when CONFIG_MEASURED_BOOT is enabled

Changes since v4:
- Remove tcg2_measure_event function and check for NULL data in tcg2_measure_data
- Use tpm_auto_startup
- Fix efi_tcg2.c compilation for removing tcg2_pcr_read function
- Change PCR indexes for initrd and dtb
- Drop u8 casting in measurement test
- Use bullets in documentation

Changes since v3:
- Reordered headers
- Refactored more of EFI code into common code
Removed digest_info structure and instead used the common alg_to_mask
and alg_to_len
Improved event log parsing in common code to get it equivalent to EFI
Common code now extends PCR if previous bootloader stage couldn't No need to allocate memory in the common code, so EFI copies the discovered buffer like it did before
Rename efi measure_event function

Changes since v2:
- Add documentation.
- Changed reserved memory address to the top of the RAM for sandbox dts.
- Add measure state to booti and bootz.
- Skip measurement for EFI images that should be measured

Changes since v1:
- Refactor TPM layer functions to allow EFI system to use them, and remove duplicate EFI functions.
- Add test case
- Drop #ifdefs for bootm
- Add devicetree measurement config option
- Update sandbox TPM driver

Eddie James (6):
tpm: Fix spelling for tpmu_ha union
tpm: sandbox: Update for needed TPM2 capabilities
tpm: Support boot measurements
bootm: Support boot measurement
test: Add sandbox TPM boot measurement
doc: Add measured boot documentation

Ilias Apalodimas (2):
efi_loader: fix EFI_ENTRY point on get_active_pcr_banks
test: use a non system PCR for testing PCR extend

  arch/sandbox/dts/sandbox.dtsi  |  13 +
  arch/sandbox/dts/test.dts      |  13 +
  boot/Kconfig                   |  32 ++
  boot/bootm.c                   |  74 +++
  cmd/booti.c                    |   1 +
  cmd/bootm.c                    |   2 +
  cmd/bootz.c                    |   1 +
  configs/sandbox_defconfig      |   1 +
  doc/usage/index.rst            |   1 +
  doc/usage/measured_boot.rst    |  23 +
  drivers/tpm/tpm2_tis_sandbox.c | 100 ++--
  include/bootm.h                |  11 +
  include/efi_tcg2.h             |  44 --
  include/image.h                |   1 +
  include/test/suites.h          |   1 +
  include/tpm-v2.h               | 263 ++++++++++-
  lib/Kconfig                    |   4 +
  lib/efi_loader/Kconfig         |   2 -
  lib/efi_loader/efi_tcg2.c      | 823 ++++-----------------------------
  lib/tpm-v2.c                   | 814 ++++++++++++++++++++++++++++++++
  test/boot/Makefile             |   1 +
  test/boot/measurement.c        |  66 +++
  test/cmd_ut.c                  |   4 +
  test/py/tests/test_tpm2.py     |  16 +-
  24 files changed, 1482 insertions(+), 829 deletions(-)

create mode 100644 doc/usage/measured_boot.rst
create mode 100644 test/boot/measurement.c

--
2.39.3

#8 lore raw

Re: [PATCH v13 6/8] doc: Add measured boot documentation

In reply to Heinrich Schuchardt

Hi Heinrich

Quoted 60 lines · Heinrich Schuchardt
Quoted 31 lines · Eddie James

Briefly describe the feature and specify the requirements.

---
Changes since v12:
- Add a bit of detail about OS usage and what pieces are measured

  doc/usage/index.rst         |  1 +
  doc/usage/measured_boot.rst | 31 +++++++++++++++++++++++++++++++
  2 files changed, 32 insertions(+)

create mode 100644 doc/usage/measured_boot.rst

doc/usage/index.rst +1 -0
index 98b4719c40..bf53bb6bda 100644
@@ -14,6 +14,7 @@ Use U-Boot
     partitions
     cmdline
     semihosting
+   measured_boot
 
  Shell commands
  --------------
doc/usage/measured_boot.rst new file +0 -0
new file mode 100644
index 0000000000..0aad590859

doc/usage/measured_boot.rst line 10

Quoted 10 lines
doc/usage/measured_boot.rst new file +10 -0
@@ -0,0 +1,31 @@
+.. SPDX-License-Identifier: GPL-2.0+
+
+Measured Boot
+=====================
+
+U-Boot can perform a measured boot, the process of hashing various components
+of the boot process, extending the results in the TPM and logging the
+component's measurement in memory for the operating system to consume.
+
+By default, U-Boot will measure the operating system (linux) image, the

The OS image would mean the root file system? I guess you mean the loaded EFI binary here. Measured boot is not Linux specific.

We should consider different scenarios:

* U-Boot loading kernel, initrd, device-tree
* U-Boot loading GRUB (or another boot manager), device-tree, GRUB loading initrd
* U-Boot loading GRUB and passing a device-tree to GRUB, GRUB loading initrd and a new device-tree

With the documentation the user should be enabled to understand what U-Boot measures in each of these cases. Please, observe that upstream GRUB uses LoadImage() while distros (e.g. Debian and Ubuntu) tend to bypass LoadImage().

Quoted 5 lines

Arent't there some other objects measured?

- SMBIOS table
- ACPI tables (still to be implemented)
- EFI variables

Eddie's part is trying to describe how 'bootm' would measure binaries. I'll update it with the EFI part once we merge this.

Regards
/Ilias

Quoted 27 lines

Best regards

Heinrich

Quoted 21 lines
doc/usage/measured_boot.rst new file +21 -0
+initrd image, and the "bootargs" environment variable. By enabling
+CONFIG_MEASURE_DEVICETREE, U-Boot will also measure the devicetree image.
+
+The operating system typically would verify that the hashes found in the
+TPM PCRs match the contents of the event log. This can further be checked
+against the hash results of previous boots.
+
+Requirements
+---------------------
+
+* A hardware TPM 2.0 supported by the U-Boot drivers
+* CONFIG_TPM=y
+* CONFIG_MEASURED_BOOT=y
+* Device-tree configuration of the TPM device to specify the memory area
+  for event logging. The TPM device node must either contain a phandle to
+  a reserved memory region or "linux,sml-base" and "linux,sml-size"
+  indicating the address and size of the memory region. An example can be
+  found in arch/sandbox/dts/test.dts
+* The operating system must also be configured to use the memory regions
+  specified in the U-Boot device-tree in order to make use of the event
+  log.