Skip to contentSkip to conversation

tpm: Support boot measurements

PATCH Superseded Eddie James 4 participants 12 messages Jan 3, 2023 – Jan 5, 2023 Library code

v1 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

cover tpm: Support boot measurements

This series adds support for measuring the boot images more generically than the existing EFI support. The series includes optional measurement from the bootm command.
Eventually the EFI code could be refactored to use the generic functions.

Eddie James (3):
tpm: Fix spelling for tpmu_ha union
tpm: Support boot measurements
bootm: Support boot measurement

 boot/bootm.c       |  53 ++++
 cmd/bootm.c        |   2 +
 common/Kconfig     |   6 +
 include/efi_tcg2.h |  44 ---
 include/image.h    |   1 +
 include/tpm-v2.h   | 139 ++++++++-
 lib/tpm-v2.c       | 700 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 899 insertions(+), 46 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/3 tpm: Fix spelling for tpmu_ha union

tmpu -> tpmu

---
 include/tpm-v2.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/tpm-v2.h +2 -2
index 737e57551d..85feda3e06 100644
@@ -167,7 +167,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];
@@ -183,7 +183,7 @@ union tmpu_ha {
  */
 struct tpmt_ha {
 	u16 hash_alg;
-	union tmpu_ha digest;
+	union tpmu_ha digest;
 } __packed;
 
 /**
Signature · 1 line
2.31.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] tpm: Fix spelling for tpmu_ha union

Quoted 31 lines · Eddie James

tmpu -> tpmu

---
 include/tpm-v2.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/tpm-v2.h +2 -3
index 737e57551d..85feda3e06 100644
@@ -167,7 +167,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];
@@ -183,7 +183,7 @@ union tmpu_ha {
  */
 struct tpmt_ha {
        u16 hash_alg;
-       union tmpu_ha digest;
+       union tpmu_ha digest;
 } __packed;
 
 /**
--
Quoted 2 lines

2.31.1

#3 lore raw

PATCH 2/3 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.

---
 include/efi_tcg2.h |  44 ---
 include/tpm-v2.h   | 135 +++++++++
 lib/tpm-v2.c       | 700 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 835 insertions(+), 44 deletions(-)
include/efi_tcg2.h +0 -44
index 874306dc11..23016773f4 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 +135 -0

159 lines not on this page · raw message

lib/tpm-v2.c +700 -0

719 lines not on this page · raw message

Signature · 1 line
2.31.1
#4 lore raw

PATCH 3/3 bootm: Support boot measurement

In reply to Eddie James

Add a configuration option to measure the boot through the bootm function.

---
 boot/bootm.c    | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 cmd/bootm.c     |  2 ++
 common/Kconfig  |  6 ++++++
 include/image.h |  1 +
 4 files changed, 62 insertions(+)
boot/bootm.c +53 -0
index a4c0870c0f..7f64d79035 100644
@@ -22,6 +22,9 @@
 #include <asm/global_data.h>
 #include <asm/io.h>
 #include <linux/sizes.h>
+#if defined(CONFIG_MEASURED_BOOT)
+#include <tpm-v2.h>
+#endif
 #if defined(CONFIG_CMD_USB)
 #include <usb.h>
 #endif
@@ -713,6 +716,56 @@ int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
 	if (!ret && (states & BOOTM_STATE_FINDOTHER))
 		ret = bootm_find_other(cmdtp, flag, argc, argv);
 
+#if defined(CONFIG_MEASURED_BOOT)
+	if (!ret && (states & BOOTM_STATE_MEASURE)) {
+		void *initrd_buf;
+		void *image_buf;
+		const char *s;
+		u32 rd_len;
+
+		ret = tcg2_measurement_init();
+		if (ret)
+			goto measure_err;
+
+		image_buf = map_sysmem(images->os.image_start,
+				       images->os.image_len);
+		ret = tcg2_measure_data(8, images->os.image_len, image_buf,
+					EV_COMPACT_HASH, strlen("linux") + 1,
+					(u8 *)"linux");
+		if (ret)
+			goto unmap_image;
+
+		rd_len = images->rd_end - images->rd_start;
+		initrd_buf = map_sysmem(images->rd_start, rd_len);
+		ret = tcg2_measure_data(8, rd_len, initrd_buf, EV_COMPACT_HASH,
+					strlen("initrd") + 1, (u8 *)"initrd");
+		if (ret)
+			goto unmap_initrd;
+
+		ret = tcg2_measure_data(9, images->ft_len,
+					(u8 *)images->ft_addr,
+					EV_TABLE_OF_DEVICES, strlen("dts") + 1,
+					(u8 *)"dts");
+		if (ret)
+			goto unmap_initrd;
+
+		s = env_get("bootargs");
+		if (!s)
+			s = "";
+		tcg2_measure_data(1, strlen(s) + 1, (u8 *)s,
+				  EV_PLATFORM_CONFIG_FLAGS, strlen(s) + 1,
+				  (u8 *)s);
+
+unmap_initrd:
+		unmap_sysmem(initrd_buf);
+unmap_image:
+		unmap_sysmem(image_buf);
+		tcg2_measurement_term();
+measure_err:
+		ret = 0;
+	}
+#endif
+
 	/* Load the OS */
 	if (!ret && (states & BOOTM_STATE_LOADOS)) {
 		iflag = bootm_disable_interrupts();
cmd/bootm.c +2 -0
index 37c2af96e0..0c4a713e02 100644
@@ -161,6 +161,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);
common/Kconfig +6 -0
index 21434c5cf1..57ef68e4f3 100644
@@ -799,6 +799,12 @@ config AVB_BUF_SIZE
 
 endif # AVB_VERIFY
 
+config MEASURED_BOOT
+	bool "Measure the boot to TPM and event log"
+	depends on HASH && TPM_V2
+	help
+	  This option enables measurement of the boot process.
+
 config SCP03
 	bool "Build SCP03 - Secure Channel Protocol O3 - controls"
 	depends on OPTEE || SANDBOX
include/image.h +1 -0
index 6f21dafba8..b00803eeac 100644
@@ -406,6 +406,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.31.1
#5 lore raw

Re: [PATCH 3/3] bootm: Support boot measurement

Hello Eddie and all,

Quoted 50 lines · Eddie James

Add a configuration option to measure the boot through the bootm function.

---
 boot/bootm.c    | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 cmd/bootm.c     |  2 ++
 common/Kconfig  |  6 ++++++
 include/image.h |  1 +
 4 files changed, 62 insertions(+)
boot/bootm.c +23 -0
index a4c0870c0f..7f64d79035 100644
@@ -22,6 +22,9 @@
 #include <asm/global_data.h>
 #include <asm/io.h>
 #include <linux/sizes.h>
+#if defined(CONFIG_MEASURED_BOOT)
+#include <tpm-v2.h>
+#endif
 #if defined(CONFIG_CMD_USB)
 #include <usb.h>
 #endif
@@ -713,6 +716,56 @@ int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
        if (!ret && (states & BOOTM_STATE_FINDOTHER))
                ret = bootm_find_other(cmdtp, flag, argc, argv);
 
+#if defined(CONFIG_MEASURED_BOOT)
+       if (!ret && (states & BOOTM_STATE_MEASURE)) {
+               void *initrd_buf;
+               void *image_buf;
+               const char *s;
+               u32 rd_len;
+
+               ret = tcg2_measurement_init();
+               if (ret)
+                       goto measure_err;
+
+               image_buf = map_sysmem(images->os.image_start,
+                                      images->os.image_len);
+               ret = tcg2_measure_data(8, images->os.image_len, image_buf,
+                                       EV_COMPACT_HASH, strlen("linux") + 1,
+                                       (u8 *)"linux");
+               if (ret)
+                       goto unmap_image;
+
+               rd_len = images->rd_end - images->rd_start;

boot/bootm.c int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc, · line 748

Quoted 10 lines
boot/bootm.c +10 -0
+               initrd_buf = map_sysmem(images->rd_start, rd_len);
+               ret = tcg2_measure_data(8, rd_len, initrd_buf, EV_COMPACT_HASH,
+                                       strlen("initrd") + 1, (u8 *)"initrd");
+               if (ret)
+                       goto unmap_initrd;
+
+               ret = tcg2_measure_data(9, images->ft_len,
+                                       (u8 *)images->ft_addr,
+                                       EV_TABLE_OF_DEVICES, strlen("dts") + 1,
+                                       (u8 *)"dts");

This unconditionally measures DTB content but maybe there are information that may differ at each boot as a random MAC address or an ASLR seed.
Do you think something should condition measurement of the passed DTB as proposed using a config switch in [1]?

[1] https://lore.kernel.org/all/[email protected]/

Regards,
Etienne

Quoted 68 lines
boot/bootm.c +20 -0
+               if (ret)
+                       goto unmap_initrd;
+
+               s = env_get("bootargs");
+               if (!s)
+                       s = "";
+               tcg2_measure_data(1, strlen(s) + 1, (u8 *)s,
+                                 EV_PLATFORM_CONFIG_FLAGS, strlen(s) + 1,
+                                 (u8 *)s);
+
+unmap_initrd:
+               unmap_sysmem(initrd_buf);
+unmap_image:
+               unmap_sysmem(image_buf);
+               tcg2_measurement_term();
+measure_err:
+               ret = 0;
+       }
+#endif
+
        /* Load the OS */
        if (!ret && (states & BOOTM_STATE_LOADOS)) {
                iflag = bootm_disable_interrupts();
cmd/bootm.c +2 -0
index 37c2af96e0..0c4a713e02 100644
@@ -161,6 +161,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);
common/Kconfig +6 -0
index 21434c5cf1..57ef68e4f3 100644
@@ -799,6 +799,12 @@ config AVB_BUF_SIZE
 
 endif # AVB_VERIFY
 
+config MEASURED_BOOT
+       bool "Measure the boot to TPM and event log"
+       depends on HASH && TPM_V2
+       help
+         This option enables measurement of the boot process.
+
 config SCP03
        bool "Build SCP03 - Secure Channel Protocol O3 - controls"
        depends on OPTEE || SANDBOX
include/image.h +1 -1
index 6f21dafba8..b00803eeac 100644
@@ -406,6 +406,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)
--

2.31.1

#6 lore raw

Re: [PATCH 3/3] bootm: Support boot measurement

Quoted 68 lines · Etienne Carriere

Hello Eddie and all,

Quoted 49 lines · Eddie James

Add a configuration option to measure the boot through the bootm function.

---
  boot/bootm.c    | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
  cmd/bootm.c     |  2 ++
  common/Kconfig  |  6 ++++++
  include/image.h |  1 +
  4 files changed, 62 insertions(+)
boot/bootm.c +23 -0
index a4c0870c0f..7f64d79035 100644
@@ -22,6 +22,9 @@
  #include <asm/global_data.h>
  #include <asm/io.h>
  #include <linux/sizes.h>
+#if defined(CONFIG_MEASURED_BOOT)
+#include <tpm-v2.h>
+#endif
  #if defined(CONFIG_CMD_USB)
  #include <usb.h>
  #endif
@@ -713,6 +716,56 @@ int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
         if (!ret && (states & BOOTM_STATE_FINDOTHER))
                 ret = bootm_find_other(cmdtp, flag, argc, argv);
 
+#if defined(CONFIG_MEASURED_BOOT)
+       if (!ret && (states & BOOTM_STATE_MEASURE)) {
+               void *initrd_buf;
+               void *image_buf;
+               const char *s;
+               u32 rd_len;
+
+               ret = tcg2_measurement_init();
+               if (ret)
+                       goto measure_err;
+
+               image_buf = map_sysmem(images->os.image_start,
+                                      images->os.image_len);
+               ret = tcg2_measure_data(8, images->os.image_len, image_buf,
+                                       EV_COMPACT_HASH, strlen("linux") + 1,
+                                       (u8 *)"linux");
+               if (ret)
+                       goto unmap_image;
+
+               rd_len = images->rd_end - images->rd_start;

boot/bootm.c int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc, · line 748

Quoted 10 lines
boot/bootm.c +10 -0
+               initrd_buf = map_sysmem(images->rd_start, rd_len);
+               ret = tcg2_measure_data(8, rd_len, initrd_buf, EV_COMPACT_HASH,
+                                       strlen("initrd") + 1, (u8 *)"initrd");
+               if (ret)
+                       goto unmap_initrd;
+
+               ret = tcg2_measure_data(9, images->ft_len,
+                                       (u8 *)images->ft_addr,
+                                       EV_TABLE_OF_DEVICES, strlen("dts") + 1,
+                                       (u8 *)"dts");

This unconditionally measures DTB content but maybe there are information that may differ at each boot as a random MAC address or an ASLR seed.
Do you think something should condition measurement of the passed DTB as proposed using a config switch in [1]?

Quoted 1 line

[1] https://lore.kernel.org/all/[email protected]/

That would make sense, I am happy to add that in v2. Should I use that config switch as-is or add a new one for the bootm measurement?

Thanks

Eddie

Quoted 72 lines

Regards,
Etienne

Quoted 68 lines
boot/bootm.c +20 -0
+               if (ret)
+                       goto unmap_initrd;
+
+               s = env_get("bootargs");
+               if (!s)
+                       s = "";
+               tcg2_measure_data(1, strlen(s) + 1, (u8 *)s,
+                                 EV_PLATFORM_CONFIG_FLAGS, strlen(s) + 1,
+                                 (u8 *)s);
+
+unmap_initrd:
+               unmap_sysmem(initrd_buf);
+unmap_image:
+               unmap_sysmem(image_buf);
+               tcg2_measurement_term();
+measure_err:
+               ret = 0;
+       }
+#endif
+
         /* Load the OS */
         if (!ret && (states & BOOTM_STATE_LOADOS)) {
                 iflag = bootm_disable_interrupts();
cmd/bootm.c +2 -0
index 37c2af96e0..0c4a713e02 100644
@@ -161,6 +161,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);
common/Kconfig +6 -0
index 21434c5cf1..57ef68e4f3 100644
@@ -799,6 +799,12 @@ config AVB_BUF_SIZE
 
  endif # AVB_VERIFY
 
+config MEASURED_BOOT
+       bool "Measure the boot to TPM and event log"
+       depends on HASH && TPM_V2
+       help
+         This option enables measurement of the boot process.
+
  config SCP03
         bool "Build SCP03 - Secure Channel Protocol O3 - controls"
         depends on OPTEE || SANDBOX
include/image.h +1 -1
index 6f21dafba8..b00803eeac 100644
@@ -406,6 +406,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)
--

2.31.1

#7 lore raw

Re: [PATCH 3/3] bootm: Support boot measurement

In reply to Eddie James

Hi Eddie,

Quoted 11 lines · Eddie James

Add a configuration option to measure the boot through the bootm function.

---
 boot/bootm.c    | 53 +++++++++++++++++++++++++++++++++++++++++++++++++
 cmd/bootm.c     |  2 ++
 common/Kconfig  |  6 ++++++
 include/image.h |  1 +
 4 files changed, 62 insertions(+)

Please add a test for this feature, e.g. in test/boot or test/bootm.c or in test/py/tests/test_fit.py or similar.

boot/bootm.c line 27

Quoted 12 lines
boot/bootm.c +3 -0
index a4c0870c0f..7f64d79035 100644
@@ -22,6 +22,9 @@
 #include <asm/global_data.h>
 #include <asm/io.h>
 #include <linux/sizes.h>
+#if defined(CONFIG_MEASURED_BOOT)
+#include <tpm-v2.h>
+#endif

Drop the #ifdef as you can always include headers

boot/bootm.c int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc, · line 720

Quoted 9 lines
boot/bootm.c +2 -0
 #if defined(CONFIG_CMD_USB)
 #include <usb.h>
 #endif
@@ -713,6 +716,56 @@ int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
        if (!ret && (states & BOOTM_STATE_FINDOTHER))
                ret = bootm_find_other(cmdtp, flag, argc, argv);
 
+#if defined(CONFIG_MEASURED_BOOT)
+       if (!ret && (states & BOOTM_STATE_MEASURE)) {

Please avoid #ifdef

Are you using patman? If so, it will automatically warn you about this sort of thing.

       if (!IS_ENABLED(CONFIG_MEASURED_BOOT) && ret && (states &

BOOTM_STATE_MEASURE)) {

boot/bootm.c int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc, · line 725

Quoted 5 lines
boot/bootm.c +5 -0
+               void *initrd_buf;
+               void *image_buf;
+               const char *s;
+               u32 rd_len;
+

Please put all this code in a separate function.

Quoted 64 lines
boot/bootm.c +43 -0
+               ret = tcg2_measurement_init();
+               if (ret)
+                       goto measure_err;
+
+               image_buf = map_sysmem(images->os.image_start,
+                                      images->os.image_len);
+               ret = tcg2_measure_data(8, images->os.image_len, image_buf,
+                                       EV_COMPACT_HASH, strlen("linux") + 1,
+                                       (u8 *)"linux");
+               if (ret)
+                       goto unmap_image;
+
+               rd_len = images->rd_end - images->rd_start;
+               initrd_buf = map_sysmem(images->rd_start, rd_len);
+               ret = tcg2_measure_data(8, rd_len, initrd_buf, EV_COMPACT_HASH,
+                                       strlen("initrd") + 1, (u8 *)"initrd");
+               if (ret)
+                       goto unmap_initrd;
+
+               ret = tcg2_measure_data(9, images->ft_len,
+                                       (u8 *)images->ft_addr,
+                                       EV_TABLE_OF_DEVICES, strlen("dts") + 1,
+                                       (u8 *)"dts");
+               if (ret)
+                       goto unmap_initrd;
+
+               s = env_get("bootargs");
+               if (!s)
+                       s = "";
+               tcg2_measure_data(1, strlen(s) + 1, (u8 *)s,
+                                 EV_PLATFORM_CONFIG_FLAGS, strlen(s) + 1,
+                                 (u8 *)s);
+
+unmap_initrd:
+               unmap_sysmem(initrd_buf);
+unmap_image:
+               unmap_sysmem(image_buf);
+               tcg2_measurement_term();
+measure_err:
+               ret = 0;
+       }
+#endif
+
        /* Load the OS */
        if (!ret && (states & BOOTM_STATE_LOADOS)) {
                iflag = bootm_disable_interrupts();
cmd/bootm.c +2 -0
index 37c2af96e0..0c4a713e02 100644
@@ -161,6 +161,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);
common/Kconfig +0 -0
index 21434c5cf1..57ef68e4f3 100644

common/Kconfig config AVB_BUF_SIZE · line 806

Quoted 8 lines
common/Kconfig +5 -0
@@ -799,6 +799,12 @@ config AVB_BUF_SIZE
 
 endif # AVB_VERIFY
 
+config MEASURED_BOOT
+       bool "Measure the boot to TPM and event log"
+       depends on HASH && TPM_V2
+       help
+         This option enables measurement of the boot process.

Please add proper help. This will also come up as a warning if you use patman

Quoted 17 lines
common/Kconfig +1 -0
+
 config SCP03
        bool "Build SCP03 - Secure Channel Protocol O3 - controls"
        depends on OPTEE || SANDBOX
include/image.h +1 -1
index 6f21dafba8..b00803eeac 100644
@@ -406,6 +406,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)
--
Quoted 2 lines

2.31.1

Regards,
Simon

#8 lore raw
In reply to Eddie James

Hi Eddie,
Thanks for the patch

Looking at the patch there's a lot of code duplication with lib/efi_loader/efi_tcg2.c.
Any reason why we aren't reusing that ?

Regards
/Ilias

Quoted 23 lines · Eddie James

This series adds support for measuring the boot images more generically than the existing EFI support. The series includes optional measurement from the bootm command.
Eventually the EFI code could be refactored to use the generic functions.

Eddie James (3):
tpm: Fix spelling for tpmu_ha union
tpm: Support boot measurements
bootm: Support boot measurement

 boot/bootm.c       |  53 ++++
 cmd/bootm.c        |   2 +
 common/Kconfig     |   6 +
 include/efi_tcg2.h |  44 ---
 include/image.h    |   1 +
 include/tpm-v2.h   | 139 ++++++++-
 lib/tpm-v2.c       | 700 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 899 insertions(+), 46 deletions(-)

--
2.31.1

#9 lore raw
Quoted 6 lines · Ilias Apalodimas

Hi Eddie,
Thanks for the patch

Looking at the patch there's a lot of code duplication with lib/efi_loader/efi_tcg2.c.
Any reason why we aren't reusing that ?

Hi,

Well the EFI code can't be used directly without configuring to include the EFI subsystem and exporting a bunch of those functions in a header file somewhere, so I added the functions in the generic tpm librrary. Now it's a matter of doing the work to use the generic functions in the EFI system. I can do that in this series if necesssary, I just haven't gotten to it.

Thanks,

Eddie

Quoted 27 lines

Regards
/Ilias

Quoted 22 lines · Eddie James

This series adds support for measuring the boot images more generically than the existing EFI support. The series includes optional measurement from the bootm command.
Eventually the EFI code could be refactored to use the generic functions.

Eddie James (3):
tpm: Fix spelling for tpmu_ha union
tpm: Support boot measurements
bootm: Support boot measurement

  boot/bootm.c       |  53 ++++
  cmd/bootm.c        |   2 +
  common/Kconfig     |   6 +
  include/efi_tcg2.h |  44 ---
  include/image.h    |   1 +
  include/tpm-v2.h   | 139 ++++++++-
  lib/tpm-v2.c       | 700 +++++++++++++++++++++++++++++++++++++++++++++
  7 files changed, 899 insertions(+), 46 deletions(-)

--
2.31.1

#10 lore raw

Hi Eddie,

Quoted 11 lines · Eddie James
Quoted 6 lines · Ilias Apalodimas

Hi Eddie,
Thanks for the patch

Looking at the patch there's a lot of code duplication with lib/efi_loader/efi_tcg2.c.
Any reason why we aren't reusing that ?

Quoted 8 lines

Hi,

Well the EFI code can't be used directly without configuring to include the EFI subsystem and exporting a bunch of those functions in a header file somewhere, so I added the functions in the generic tpm librrary. Now it's a matter of doing the work to use the generic functions in the EFI system. I can do that in this series if necesssary, I just haven't gotten to it.

We cannot have EFI code in tpm-v2.h

The TPM layer should implement the feature, with the EFI layer calling into it. This allows the TPM feature to be used by VBE and other non-EFI systems.

Regards,
Simon

#11 lore raw
In reply to Eddie James

Hi Eddie,

Quoted 10 lines · Eddie James
Quoted 6 lines · Ilias Apalodimas

Hi Eddie,
Thanks for the patch

Looking at the patch there's a lot of code duplication with lib/efi_loader/efi_tcg2.c.
Any reason why we aren't reusing that ?

Quoted 7 lines

Hi,

Well the EFI code can't be used directly without configuring to include the EFI subsystem and exporting a bunch of those functions in a header file somewhere, so I added the functions in the generic tpm librrary. Now it's a matter of doing the work to use the generic functions in the EFI system. I can do that in this series if necesssary, I just haven't gotten to it.

Doing it the other way around is easier. Just carve out the functions you want from efi_tcg.c to tpm-v2.c and use them for bootm measurements. Then the rest of the EFI will remain unaffected.

Cheers
/Ilias

Quoted 33 lines

Thanks,

Eddie

Quoted 27 lines

Regards
/Ilias

Quoted 22 lines · Eddie James

This series adds support for measuring the boot images more generically than the existing EFI support. The series includes optional measurement from the bootm command.
Eventually the EFI code could be refactored to use the generic functions.

Eddie James (3):
tpm: Fix spelling for tpmu_ha union
tpm: Support boot measurements
bootm: Support boot measurement

  boot/bootm.c       |  53 ++++
  cmd/bootm.c        |   2 +
  common/Kconfig     |   6 +
  include/efi_tcg2.h |  44 ---
  include/image.h    |   1 +
  include/tpm-v2.h   | 139 ++++++++-
  lib/tpm-v2.c       | 700 +++++++++++++++++++++++++++++++++++++++++++++
  7 files changed, 899 insertions(+), 46 deletions(-)

--
2.31.1