tpm: Support boot measurements
v4 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 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: Support boot measurements
bootm: Support boot measurement
tpm: sandbox: Update for needed TPM2 capabilities
test: Add sandbox TPM boot measurement
doc: Add measured boot documentation
arch/sandbox/dts/sandbox.dtsi | 14 + arch/sandbox/dts/test.dts | 13 + boot/Kconfig | 23 + boot/bootm.c | 70 +++ 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 | 2 + include/efi_tcg2.h | 44 -- include/image.h | 1 + include/test/suites.h | 1 + include/tpm-v2.h | 258 ++++++++- lib/efi_loader/efi_tcg2.c | 975 +++------------------------------ lib/tpm-v2.c | 799 +++++++++++++++++++++++++++ test/boot/Makefile | 1 + test/boot/measurement.c | 66 +++ test/cmd_ut.c | 2 + 21 files changed, 1425 insertions(+), 973 deletions(-) create mode 100644 doc/usage/measured_boot.rst create mode 100644 test/boot/measurement.c
Signature · 1 line
2.31.1
PATCH v4 1/6 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
@@ -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
PATCH v4 6/6 doc: Add measured boot documentation
In reply to Eddie JamesBriefly describe the feature and specify the requirements.
--- doc/usage/index.rst | 1 + doc/usage/measured_boot.rst | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 doc/usage/measured_boot.rst
doc/usage/index.rst +1 -0
@@ -12,6 +12,7 @@ Use U-Boot partitions cmdline semihosting + measured_boot Shell commands --------------
doc/usage/measured_boot.rst new file +23 -0
@@ -0,0 +1,23 @@ +.. 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. + +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.31.1
PATCH v4 3/6 bootm: Support boot measurement
In reply to Eddie JamesAdd a configuration option to measure the boot through the bootm function. Add the measurement state to the booti and bootz paths as well.
--- boot/Kconfig | 23 ++++++++++++++++ boot/bootm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/booti.c | 1 + cmd/bootm.c | 2 ++ cmd/bootz.c | 1 + include/bootm.h | 2 ++ include/image.h | 1 + 7 files changed, 100 insertions(+)
boot/Kconfig +23 -0
@@ -601,6 +601,29 @@ 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. +endif # MEASURED_BOOT + config SUPPORT_RAW_INITRD bool "Enable raw initrd images" help
boot/bootm.c +70 -0
88 lines not on this page · raw message
cmd/booti.c +1 -0
@@ -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
@@ -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 );
cmd/bootz.c +1 -0
@@ -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 +2 -0
@@ -55,6 +55,8 @@ ulong bootm_disable_interrupts(void);int bootm_find_images (int flag,int argc,char *const argv[],ulong start,ulong size); +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
@@ -407,6 +407,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 0x00001000int state;#if defined(CONFIG_LMB ) && !defined(USE_HOSTCC )
Signature · 1 line
2.31.1
PATCH v4 5/6 test: Add sandbox TPM boot measurement
In reply to Eddie JamesUse the sandbox TPM driver to measure some boot images in a unit test case.
--- arch/sandbox/dts/sandbox.dtsi | 14 ++++++++ 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 | 2 ++ 7 files changed, 98 insertions(+) create mode 100644 test/boot/measurement.c
arch/sandbox/dts/sandbox.dtsi +14 -0
@@ -4,9 +4,22 @@ * and sandbox64 builds. */ +#include <config.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 { };
@@ -332,6 +345,7 @@ tpm2 { compatible = "sandbox,tpm2"; + memory-region = <&event_log>; }; triangle {
arch/sandbox/dts/test.dts +13 -0
@@ -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>
@@ -66,6 +67,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 { };
@@ -1343,6 +1355,7 @@ tpm2 { compatible = "sandbox,tpm2"; + memory-region = <&event_log>; }; uart0: serial {
configs/sandbox_defconfig +1 -0
@@ -337,3 +337,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
@@ -44,6 +44,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
@@ -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
test/boot/measurement.c new file +66 -0
66 lines not on this page · raw message
test/cmd_ut.c +2 -0
@@ -101,6 +101,8 @@ static struct cmd_tbl cmd_ut_sub[] = { "", ""),U_BOOT_CMD_MKENT (bloblist,CONFIG_SYS_MAXARGS ,1 , do_ut_bloblist,"" ,"" ), +U_BOOT_CMD_MKENT (measurement,CONFIG_SYS_MAXARGS ,1 , do_ut_measurement, +"" ,"" ),U_BOOT_CMD_MKENT (bootm,CONFIG_SYS_MAXARGS ,1 , do_ut_bootm,"" ,"" ), #endif U_BOOT_CMD_MKENT(str, CONFIG_SYS_MAXARGS, 1, do_ut_str, "", ""),
Signature · 1 line
2.31.1
PATCH v4 2/6 tpm: Support boot measurements
In reply to Eddie JamesAdd 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.
--- include/efi_tcg2.h | 44 -- include/tpm-v2.h | 254 ++++++++++ lib/efi_loader/efi_tcg2.c | 975 +++----------------------------------- lib/tpm-v2.c | 799 +++++++++++++++++++++++++++++++ 4 files changed, 1129 insertions(+), 943 deletions(-)
include/efi_tcg2.h +0 -44
@@ -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 +254 -0
278 lines not on this page · raw message
lib/efi_loader/efi_tcg2.c +76 -899
1,242 lines not on this page · raw message
lib/tpm-v2.c +799 -0
816 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v4 4/6 tpm: sandbox: Update for needed TPM2 capabilities
In reply to Eddie JamesThe driver needs to support getting the PCRs in the capabilities command. Fix various other things and support the max number of PCRs for TPM2.
--- drivers/tpm/tpm2_tis_sandbox.c | 100 ++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 28 deletions(-)
drivers/tpm/tpm2_tis_sandbox.c +72 -28
@@ -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 */
Signature · 1 line
2.31.1
Re: [PATCH v4 6/6] doc: Add measured boot documentation
In reply to Eddie JamesQuoted 32 lines · Eddie James
Briefly describe the feature and specify the requirements.
Signed-off-by Eddie James <[email protected]>--- doc/usage/index.rst | 1 + doc/usage/measured_boot.rst | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+)create mode 100644
doc/usage/measured_boot.rstdoc/usage/index.rst +1
-0@@ -12,6 +12,7 @@ Use U-Boot partitions cmdline semihosting + measured_boot Shell commands --------------doc/usage/measured_boot.rst new file +5
-0@@ -0,0 +1,23 @@ +.. SPDX-License-Identifier: GPL-2.0+ + +Measured Boot +===================== +
doc/usage/measured_boot.rst line 15
Quoted 10 lines
doc/usage/measured_boot.rst new file +10
-0+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. + +Requirements +--------------------- + +A hardware TPM 2.0 supported by the U-Boot drivers +CONFIG_TPM=y +CONFIG_MEASURED_BOOT=y
I guess this should be a bullet list instead of unformatted text.
https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#lists-and-quote-like-blocks
shows how to create a bullet list.
Please, use 'make htmldocs' to check the output. See
https://u-boot.readthedocs.io/en/latest/build/documentation.html
for setting up a build environment.
Best regards
Heinrich
Quoted 8 lines
doc/usage/measured_boot.rst new file +8
-0+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.
Re: [PATCH v4 3/6] bootm: Support boot measurement
In reply to Eddie JamesHi Eddie,
Quoted 6 lines · 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.
Signed-off-by Eddie James <[email protected]>
Quoted 9 lines
--- boot/Kconfig | 23 ++++++++++++++++ boot/bootm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/booti.c | 1 + cmd/bootm.c | 2 ++ cmd/bootz.c | 1 + include/bootm.h | 2 ++ include/image.h | 1 + 7 files changed, 100 insertions(+)
Can you please add a change log? I recall making comments but cannot see the changes here.
Regards,
Simon
Re: [PATCH v4 5/6] test: Add sandbox TPM boot measurement
In reply to Eddie JamesHi Eddie,
Quoted 5 lines · Eddie James
Use the sandbox TPM driver to measure some boot images in a unit test case.
Signed-off-by Eddie James <[email protected]>
Quoted 10 lines
--- arch/sandbox/dts/sandbox.dtsi | 14 ++++++++ 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 | 2 ++ 7 files changed, 98 insertions(+) create mode 100644 test/boot/measurement.c
Please see below
Quoted 150 lines
arch/sandbox/dts/sandbox.dtsi +14
-0@@ -4,9 +4,22 @@ * and sandbox64 builds. */ +#include <config.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 { };@@ -332,6 +345,7 @@ tpm2 { compatible = "sandbox,tpm2"; + memory-region = <&event_log>; }; triangle {arch/sandbox/dts/test.dts +13
-0@@ -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>@@ -66,6 +67,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 { };@@ -1343,6 +1355,7 @@ tpm2 { compatible = "sandbox,tpm2"; + memory-region = <&event_log>; }; uart0: serial {configs/sandbox_defconfig +1
-0@@ -337,3 +337,4 @@ CONFIG_TEST_FDTDEC=y CONFIG_UNIT_TEST=y CONFIG_UT_TIME=y CONFIG_UT_DM=y +CONFIG_MEASURED_BOOT=yinclude/test/suites.h +1
-0@@ -44,6 +44,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@@ -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.otest/boot/measurement.c new file +37
-0@@ -0,0 +1,66 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* Test for measured boot functions +* +* Copyright 2023 IBM Corp. +* Written by Eddie James <[email protected]> +*/ + +#include <common.h> +#include <bootm.h> +#include <malloc.h> +#include <test/suites.h> +#include <test/test.h> +#include <test/ut.h> +#include <asm/io.h> + +#define MEASUREMENT_TEST (_name, _flags) \ + UNIT_TEST(_name, _flags, measurement_test) + +static int measure (struct unit_test_state *uts) +{ +struct bootm_headers images; +const size_t size =1024 ; +u8 *kernel; +u8 *initrd; +size_t i; + + kernel =malloc (size); + initrd =malloc (size); + + images.os.image_start =map_to_sysmem (kernel); + images.os.image_len = size; + + images.rd_start =map_to_sysmem (initrd); + images.rd_end = images.rd_start + size; + + images.ft_addr =malloc (size);
test/boot/measurement.c line 45
Quoted 10 lines
test/boot/measurement.c new file +8
-0+ images.ft_len = size; + +env_set ("bootargs" ,"measurement testing" ); + +for (i =0 ; i < size; ++i) { + kernel[i] = (u8)(0xf0 | (i &0xf )); + initrd[i] = (u8)((i &0xf0 ) |0xf ); + ((u8 *)images.ft_addr)[i] = (u8)(i &0xff );
Gosh this is pretty ugly with all the casting (which I suspect could be dropped).
At least, can you drop the cast on images.ft_addr? If you don't want to do that, please create a u8 * local variable.
Quoted 35 lines
test/boot/measurement.c new file +21
-0+ } + +ut_assertok (bootm_measure (&images)); + +free (images.ft_addr); +free (initrd); +free (kernel); + +return 0 ; +} +MEASUREMENT_TEST (measure,0 ); + +int do_ut_measurement (struct cmd_tbl *cmdtp,int flag,int argc, +char *const argv[]) +{ +struct unit_test *tests =UNIT_TEST_SUITE_START (measurement_test); +const int n_ents =UNIT_TEST_SUITE_COUNT (measurement_test); + +return cmd_ut_category ("measurement" ,"measurement_test_" , tests, + n_ents, argc, argv); +}test/cmd_ut.c +2
-1@@ -101,6 +101,8 @@ static struct cmd_tbl cmd_ut_sub[] = { "", ""),U_BOOT_CMD_MKENT (bloblist,CONFIG_SYS_MAXARGS ,1 , do_ut_bloblist,"" ,"" ), +U_BOOT_CMD_MKENT (measurement,CONFIG_SYS_MAXARGS ,1 , do_ut_measurement, +"" ,"" ),U_BOOT_CMD_MKENT (bootm,CONFIG_SYS_MAXARGS ,1 , do_ut_bootm,"" ,"" ), #endif U_BOOT_CMD_MKENT(str, CONFIG_SYS_MAXARGS, 1, do_ut_str, "", ""), --
Quoted 2 lines
2.31.1
Regards,
Simon
Re: [PATCH v4 4/6] tpm: sandbox: Update for needed TPM2 capabilities
In reply to Eddie JamesQuoted 154 lines · Eddie James
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.
Signed-off-by Eddie James <[email protected]>Reviewed-by Simon Glass <[email protected]>--- drivers/tpm/tpm2_tis_sandbox.c | 100 ++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 28 deletions(-)drivers/tpm/tpm2_tis_sandbox.c +72
-29@@ -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 */ --
Quoted 2 lines
2.31.1
Re: [PATCH v4 3/6] bootm: Support boot measurement
In reply to Eddie JamesHi Eddie,
Quoted 90 lines · 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.
Signed-off-by Eddie James <[email protected]>--- boot/Kconfig | 23 ++++++++++++++++ boot/bootm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/booti.c | 1 + cmd/bootm.c | 2 ++ cmd/bootz.c | 1 + include/bootm.h | 2 ++ include/image.h | 1 + 7 files changed, 100 insertions(+)boot/Kconfig +23
-0@@ -601,6 +601,29 @@ 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. +endif # MEASURED_BOOT + config SUPPORT_RAW_INITRD bool "Enable raw initrd images" helpboot/bootm.c +26
-0@@ -22,6 +22,7 @@#include <asm/global_data.h> #include <asm/io.h> #include <linux/sizes.h> +#include <tpm-v2.h> #if defined(CONFIG_CMD_USB )#include <usb.h> #endif @@ -659,6 +660,72 @@ int bootm_process_cmdline_env(int flags)return 0 ; } +int bootm_measure (struct bootm_headers *images) +{ +int ret =0 ; + +/* Skip measurement if EFI is going to do it */ +if (images->os.os ==IH_OS_EFI && +IS_ENABLED (CONFIG_EFI_TCG2_PROTOCOL ) && +IS_ENABLED (CONFIG_BOOTM_EFI )) +return ret; + +if (IS_ENABLED (CONFIG_MEASURED_BOOT )) { +struct tcg2_event_log elog; +struct udevice *dev; +void *initrd_buf; +void *image_buf; +const char *s; +u32 rd_len; + + ret =tcg2_measurement_init (&dev, &elog); +if (ret) +return ret; + + image_buf =map_sysmem (images->os.image_start, + images->os.image_len); + ret = tcg2_measure_data(dev, &elog, 8, images->os.image_len,
boot/bootm.c int bootm_process_cmdline_env(int flags) · line 697
Quoted 10 lines
boot/bootm.c +10
-0+ 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 (dev, &elog,8 , rd_len, initrd_buf, +EV_COMPACT_HASH ,strlen ("initrd" ) +1 , + (u8 *)"initrd" );
The kernel, if loaded with EFI, measures the initrd data into PCR9. Although it's not important can we please use the same PCR here?
boot/bootm.c int bootm_process_cmdline_env(int flags) · line 706
Quoted 9 lines
boot/bootm.c +9
-0+if (ret) + goto unmap_initrd; + +if (IS_ENABLED (CONFIG_MEASURE_DEVICETREE )) { + ret =tcg2_measure_data (dev, &elog,9 , images->ft_len, + (u8 *)images->ft_addr, +EV_TABLE_OF_DEVICES , +strlen ("dts" ) +1 , + (u8 *)"dts" );
The PC client spec describes how ACPI data should be measured. Although it has no wording on the DTB, I think it should be treated in a similar fashion. This [0] was trying to do the same thing for EFI. Any reason we can't use PCR0 here?
boot/bootm.c int bootm_process_cmdline_env(int flags) · line 719
Quoted 13 lines
boot/bootm.c +13
-0+if (ret) + goto unmap_initrd; + } + + s =env_get ("bootargs" ); +if (!s) + s ="" ; + ret =tcg2_measure_data (dev, &elog,1 ,strlen (s) +1 , (u8 *)s, +EV_PLATFORM_CONFIG_FLAGS , +strlen (s) +1 , (u8 *)s); + +unmap_initrd: +unmap_sysmem (initrd_buf);
[0] https://lore.kernel.org/u-boot/[email protected]/
Regards
/Ilias
Hi Eddie,
Thanks for the cleanup! Unfortunately this doesn't compile with EFI selected, but in general it looks pretty good.
Quoted 16 lines · 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.
Signed-off-by Eddie James <[email protected]>--- include/efi_tcg2.h | 44 -- include/tpm-v2.h | 254 ++++++++++ lib/efi_loader/efi_tcg2.c | 975 +++----------------------------------- lib/tpm-v2.c | 799 +++++++++++++++++++++++++++++++ 4 files changed, 1129 insertions(+), 943 deletions(-)
HR_NV_INDEX=TPM_HT_NV_INDEX<<HR_SHIFT,
};
[...]
Quoted 31 lines
lib/tpm-v2.c +29
-0} +} + +int tcg2_measure_data (struct udevice *dev,struct tcg2_event_log *elog, +u32 pcr_index,u32 size,const u8 *data,u32 event_type, +u32 event_size,const u8 *event) +{ +struct tpml_digest_values digest_list; +int rc; + + rc =tcg2_create_digest (dev, data, size, &digest_list); +if (rc) +return rc; + + rc =tcg2_pcr_extend (dev, pcr_index, &digest_list); +if (rc) +return rc; + +return tcg2_log_append_check (elog, pcr_index, event_type, &digest_list, + event_size, event); +} + +int tcg2_measure_event (struct udevice *dev,struct tcg2_event_log *elog, +u32 pcr_index,u32 event_type,u32 size, +const u8 *event) +{ +struct tpml_digest_values digest_list; +int rc; + + rc =tcg2_create_digest (dev, event, size, &digest_list);
lib/tpm-v2.c line 556
Quoted 10 lines
lib/tpm-v2.c +10
-0+if (rc) +return rc; + + rc =tcg2_pcr_extend (dev, pcr_index, &digest_list); +if (rc) +return rc; + +return tcg2_log_append_check (elog, pcr_index, event_type, &digest_list, + size, event); +}
The only difference between these 2 is the measured data. Can't we make one function? If data = NULL we could just measure the event
Quoted 9 lines
lib/tpm-v2.c +9
-0+ +int tcg2_init_log (struct udevice *dev,struct tcg2_event_log *elog) +{ +int rc; + + rc =tcg2_platform_get_log (dev, (void **)&elog->log, &elog->log_size); +if (rc) +return rc; +
lib/tpm-v2.c line 575
Quoted 10 lines
lib/tpm-v2.c +10
-0+ elog->log_position =0 ; + elog->found = false; + rc =tcg2_log_parse (dev, elog); +if (rc) +return rc; + +if (!elog->found) { + rc =tcg2_log_init (dev, elog); +if (rc) +return rc;
You can get rid of this if and just return rc on the function
Quoted 16 lines
lib/tpm-v2.c +16
-0+ } + +return 0 ; +} + +int tcg2_measurement_init (struct udevice **dev,struct tcg2_event_log *elog) +{ +int rc; + + rc =tcg2_platform_get_tpm2 (dev); +if (rc) +return rc; + + rc =tpm_init (*dev); +if (rc) +return rc;
lib/tpm-v2.c line 601
Quoted 10 lines
lib/tpm-v2.c +10
-0+ + rc =tpm2_startup (*dev,TPM2_SU_CLEAR ); +if (rc) { +tcg2_platform_startup_error (*dev, rc); +return rc; + } + + rc =tpm2_self_test (*dev,TPMI_YES ); +if (rc) +printf ("%s: self test error, continuing.\n" , __func__);
This is the correct init sequence of the TPM. I was trying to solve something similar a few days ago [0]. I haven't merged that patch yet, but I will send a new version that also calls tpm_init() and doesn't exit on -EBUSY. Can you use that patch instead of open coding the init sequence?
Also we should be bailing out on selftest errors?
The problem here is that with U-Boot's lazy binding we might need to init the TPM in other subsystems and at the very least we can have a function doing that for us reliably.
Quoted 17 lines
lib/tpm-v2.c +17
-0+ + rc =tcg2_init_log (*dev, elog); +if (rc) { +tcg2_measurement_term (*dev, elog, true); +return rc; + } + + rc =tcg2_measure_event (*dev, elog,0 ,EV_S_CRTM_VERSION , +strlen (version_string) +1 , + (u8 *)version_string); +if (rc) { +tcg2_measurement_term (*dev, elog, true); +return rc; + } + +return 0 ; +}
lib/tpm-v2.c line 628
Quoted 10 lines
lib/tpm-v2.c +10
-0+ +void tcg2_measurement_term (struct udevice *dev,struct tcg2_event_log *elog, +bool error) +{ +u32 event = error ?0x1 :0xffffffff ; +int i; + +for (i =0 ; i <8 ; ++i) +tcg2_measure_event (dev, elog, i,EV_SEPARATOR ,sizeof (event), + (const u8 *)&event);
Is the separator event at the end of the eventlog described in the spec?
Quoted 80 lines
lib/tpm-v2.c +73
-0+ +if (elog->log) +unmap_physmem (elog->log,MAP_NOCACHE ); +} + +__weak int tcg2_platform_get_log (struct udevice *dev,void **addr,u32 *size) +{ +const __be32 *addr_prop; +const __be32 *size_prop; +int asize; +int ssize; + + *addr =NULL ; + *size =0 ; + + addr_prop =dev_read_prop (dev,"linux,sml-base" , &asize); +if (!addr_prop) + addr_prop =dev_read_prop (dev,"tpm_event_log_addr" , &asize); + size_prop =dev_read_prop (dev,"linux,sml-size" , &ssize); +if (!size_prop) + size_prop =dev_read_prop (dev,"tpm_event_log_size" , &ssize); +if (addr_prop && size_prop) { +u64 a =of_read_number (addr_prop, asize /sizeof (__be32)); +u64 s =of_read_number (size_prop, ssize /sizeof (__be32)); + + *addr =map_physmem (a, s,MAP_NOCACHE ); + *size = (u32 )s; + }else { +struct ofnode_phandle_args args; +phys_addr_t a; +phys_size_t s; + +if (dev_read_phandle_with_args (dev,"memory-region" ,NULL ,0 , +0 , &args)) +return -ENODEV ; + + a =ofnode_get_addr_size (args.node,"reg" , &s); +if (a ==FDT_ADDR_T_NONE ) +return -ENOMEM ; + + *addr =map_physmem (a, s,MAP_NOCACHE ); + *size = (u32 )s; + } + +return 0 ; +} + +__weak int tcg2_platform_get_tpm2 (struct udevice **dev) +{ +for_each_tpm_device (*dev) { +if (tpm_get_version (*dev) ==TPM_V2 ) +return 0 ; + } + +return -ENODEV ; +} + +__weak void tcg2_platform_startup_error (struct udevice *dev,int rc) {} +u32 tpm2_startup (struct udevice *dev,enum tpm2_startup_types mode) {const u8 command_v2[12 ] = {@@ -342,6 +1016,131 @@ u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,return 0 ; } +static int tpm2_get_num_pcr (struct udevice *dev,u32 *num_pcr) +{ +u8 response[(sizeof (struct tpms_capability_data ) - + offsetof(struct tpms_capability_data , data))]; +u32 properties_offset = + offsetof(struct tpml_tagged_tpm_property , tpm_property) + + offsetof(struct tpms_tagged_property , value); +u32 ret; + +memset (response,0 ,sizeof (response)); + ret =tpm2_get_capability (dev,TPM2_CAP_TPM_PROPERTIES , +TPM2_PT_PCR_COUNT , response,1 ); +if (ret) +return ret;
lib/tpm-v2.c u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property, · line 1042
Quoted 10 lines
lib/tpm-v2.c +10
-0+ + *num_pcr =get_unaligned_be32 (response + properties_offset); +if (*num_pcr >TPM2_MAX_PCRS ) { +printf ("%s: too many pcrs: %u\n" , __func__, *num_pcr); +return -E2BIG ; + } + +return 0 ; +} +
[...]
[0] https://lore.kernel.org/u-boot/[email protected]/
Thanks
/Ilias
Re: [PATCH v4 3/6] bootm: Support boot measurement
In reply to Simon GlassQuoted 17 lines · Simon Glass
Hi Eddie,
Quoted 5 lines · 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.
Signed-off-by Eddie James <[email protected]>Quoted 9 lines
--- boot/Kconfig | 23 ++++++++++++++++ boot/bootm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/booti.c | 1 + cmd/bootm.c | 2 ++ cmd/bootz.c | 1 + include/bootm.h | 2 ++ include/image.h | 1 + 7 files changed, 100 insertions(+)
Quoted 2 lines
Can you please add a change log? I recall making comments but cannot see the changes here.
Sorry, sure. I put them all in the cover letter, but I can add them here too.
Thanks,
Eddie
Quoted 3 lines
Regards,
Simon
Re: [PATCH v4 3/6] bootm: Support boot measurement
Hi Eddie,
Quoted 20 lines · Eddie James
Quoted 17 lines · Simon Glass
Hi Eddie,
Quoted 14 lines · 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.
Signed-off-by Eddie James <[email protected]>--- boot/Kconfig | 23 ++++++++++++++++ boot/bootm.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ cmd/booti.c | 1 + cmd/bootm.c | 2 ++ cmd/bootz.c | 1 + include/bootm.h | 2 ++ include/image.h | 1 + 7 files changed, 100 insertions(+)
Quoted 6 lines
Quoted 2 lines
Can you please add a change log? I recall making comments but cannot see the changes here.
Sorry, sure. I put them all in the cover letter, but I can add them here too.
It makes it much easier for your reviewer.
Sorry if I have mentioned this before, but patman does this automatically.
Regards,
Simon
Quoted 4 lines · Ilias Apalodimas
Hi Eddie,
Thanks for the cleanup! Unfortunately this doesn't compile with EFI selected, but in general it looks pretty good.
Thanks, yes I forgot to remove tcg2_pcr_read
Quoted 55 lines · Eddie James
Quoted 16 lines
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.
Signed-off-by Eddie James <[email protected]>--- include/efi_tcg2.h | 44 -- include/tpm-v2.h | 254 ++++++++++ lib/efi_loader/efi_tcg2.c | 975 +++----------------------------------- lib/tpm-v2.c | 799 +++++++++++++++++++++++++++++++ 4 files changed, 1129 insertions(+), 943 deletions(-)
HR_NV_INDEX=TPM_HT_NV_INDEX<<HR_SHIFT,
};[...]
Quoted 35 lines
}lib/tpm-v2.c +33
-0+} + +int tcg2_measure_data (struct udevice *dev,struct tcg2_event_log *elog, +u32 pcr_index,u32 size,const u8 *data,u32 event_type, +u32 event_size,const u8 *event) +{ +struct tpml_digest_values digest_list; +int rc; + + rc =tcg2_create_digest (dev, data, size, &digest_list); +if (rc) +return rc; + + rc =tcg2_pcr_extend (dev, pcr_index, &digest_list); +if (rc) +return rc; + +return tcg2_log_append_check (elog, pcr_index, event_type, &digest_list, + event_size, event); +} + +int tcg2_measure_event (struct udevice *dev,struct tcg2_event_log *elog, +u32 pcr_index,u32 event_type,u32 size, +const u8 *event) +{ +struct tpml_digest_values digest_list; +int rc; + + rc =tcg2_create_digest (dev, event, size, &digest_list); +if (rc) +return rc; + + rc =tcg2_pcr_extend (dev, pcr_index, &digest_list);
Quoted 8 lines
lib/tpm-v2.c line 556
Quoted 6 lines
lib/tpm-v2.c +6
-0+if (rc) +return rc; + +return tcg2_log_append_check (elog, pcr_index, event_type, &digest_list, + size, event); +}The only difference between these 2 is the measured data. Can't we make one function? If data = NULL we could just measure the event
Ok, yes that should work. I'll probably add a macro for tcg2_measure_event that fills in NULL and 0 for data and size for convenience.
Quoted 13 lines
Quoted 13 lines
lib/tpm-v2.c +12
-0+ +int tcg2_init_log (struct udevice *dev,struct tcg2_event_log *elog) +{ +int rc; + + rc =tcg2_platform_get_log (dev, (void **)&elog->log, &elog->log_size); +if (rc) +return rc; + + elog->log_position =0 ; + elog->found = false; + rc =tcg2_log_parse (dev, elog);
Quoted 8 lines
lib/tpm-v2.c line 575
Quoted 7 lines
lib/tpm-v2.c +7
-0+if (rc) +return rc; + +if (!elog->found) { + rc =tcg2_log_init (dev, elog); +if (rc) +return rc;You can get rid of this if and just return rc on the function
Ack.
Quoted 24 lines
Quoted 24 lines
lib/tpm-v2.c +23
-0+ } + +return 0 ; +} + +int tcg2_measurement_init (struct udevice **dev,struct tcg2_event_log *elog) +{ +int rc; + + rc =tcg2_platform_get_tpm2 (dev); +if (rc) +return rc; + + rc =tpm_init (*dev); +if (rc) +return rc; + + rc =tpm2_startup (*dev,TPM2_SU_CLEAR ); +if (rc) { +tcg2_platform_startup_error (*dev, rc); +return rc; + } +
Quoted 8 lines
lib/tpm-v2.c line 601
Quoted 3 lines
lib/tpm-v2.c +3
-0+ rc =tpm2_self_test (*dev,TPMI_YES ); +if (rc) +printf ("%s: self test error, continuing.\n" , __func__);This is the correct init sequence of the TPM. I was trying to solve something similar a few days ago [0]. I haven't merged that patch yet, but I will send a new version that also calls
tpm_init()and doesn't exit on-EBUSY. Can you use that patch instead of open coding the init sequence?
Yes I'd be happy to use it. I'll just base my series on yours.
Quoted 1 line
Also we should be bailing out on selftest errors?'
Maybe so, yes. Whatever behavior your auto startup function implements will be fine, I'm sure.
Quoted 25 lines
The problem here is that with U-Boot's lazy binding we might need to init the TPM in other subsystems and at the very least we can have a function doing that for us reliably.
Quoted 20 lines
lib/tpm-v2.c +20
-0+ + rc =tcg2_init_log (*dev, elog); +if (rc) { +tcg2_measurement_term (*dev, elog, true); +return rc; + } + + rc =tcg2_measure_event (*dev, elog,0 ,EV_S_CRTM_VERSION , +strlen (version_string) +1 , + (u8 *)version_string); +if (rc) { +tcg2_measurement_term (*dev, elog, true); +return rc; + } + +return 0 ; +} + +void tcg2_measurement_term (struct udevice *dev,struct tcg2_event_log *elog, +bool error)
Quoted 8 lines
lib/tpm-v2.c line 628
Quoted 7 lines
lib/tpm-v2.c +7
-0+{ +u32 event = error ?0x1 :0xffffffff ; +int i; + +for (i =0 ; i <8 ; ++i) +tcg2_measure_event (dev, elog, i,EV_SEPARATOR ,sizeof (event), + (const u8 *)&event);Is the separator event at the end of the eventlog described in the spec?
Yes. https://trustedcomputinggroup.org/wp-content/uploads/TCG_ServerManagDomainFWProfile_r1p00_pub.pdf
See section 7.1.1
Quoted 97 lines
Quoted 81 lines
lib/tpm-v2.c +73
-0+ +if (elog->log) +unmap_physmem (elog->log,MAP_NOCACHE ); +} + +__weak int tcg2_platform_get_log (struct udevice *dev,void **addr,u32 *size) +{ +const __be32 *addr_prop; +const __be32 *size_prop; +int asize; +int ssize; + + *addr =NULL ; + *size =0 ; + + addr_prop =dev_read_prop (dev,"linux,sml-base" , &asize); +if (!addr_prop) + addr_prop =dev_read_prop (dev,"tpm_event_log_addr" , &asize); + size_prop =dev_read_prop (dev,"linux,sml-size" , &ssize); +if (!size_prop) + size_prop =dev_read_prop (dev,"tpm_event_log_size" , &ssize); +if (addr_prop && size_prop) { +u64 a =of_read_number (addr_prop, asize /sizeof (__be32)); +u64 s =of_read_number (size_prop, ssize /sizeof (__be32)); + + *addr =map_physmem (a, s,MAP_NOCACHE ); + *size = (u32 )s; + }else { +struct ofnode_phandle_args args; +phys_addr_t a; +phys_size_t s; + +if (dev_read_phandle_with_args (dev,"memory-region" ,NULL ,0 , +0 , &args)) +return -ENODEV ; + + a =ofnode_get_addr_size (args.node,"reg" , &s); +if (a ==FDT_ADDR_T_NONE ) +return -ENOMEM ; + + *addr =map_physmem (a, s,MAP_NOCACHE ); + *size = (u32 )s; + } + +return 0 ; +} + +__weak int tcg2_platform_get_tpm2 (struct udevice **dev) +{ +for_each_tpm_device (*dev) { +if (tpm_get_version (*dev) ==TPM_V2 ) +return 0 ; + } + +return -ENODEV ; +} + +__weak void tcg2_platform_startup_error (struct udevice *dev,int rc) {} +u32 tpm2_startup (struct udevice *dev,enum tpm2_startup_types mode) {const u8 command_v2[12 ] = {@@ -342,6 +1016,131 @@ u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,return 0 ; } +static int tpm2_get_num_pcr (struct udevice *dev,u32 *num_pcr) +{ +u8 response[(sizeof (struct tpms_capability_data ) - + offsetof(struct tpms_capability_data , data))]; +u32 properties_offset = + offsetof(struct tpml_tagged_tpm_property , tpm_property) + + offsetof(struct tpms_tagged_property , value); +u32 ret; + +memset (response,0 ,sizeof (response)); + ret =tpm2_get_capability (dev,TPM2_CAP_TPM_PROPERTIES , +TPM2_PT_PCR_COUNT , response,1 ); +if (ret) +return ret;lib/tpm-v2.c u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property, · line 1042
Quoted 10 lines
lib/tpm-v2.c +10
-0+ + *num_pcr =get_unaligned_be32 (response + properties_offset); +if (*num_pcr >TPM2_MAX_PCRS ) { +printf ("%s: too many pcrs: %u\n" , __func__, *num_pcr); +return -E2BIG ; + } + +return 0 ; +} +[...]
[0] https://lore.kernel.org/u-boot/[email protected]/
Thanks
/Ilias
Hi Eddie / Ilias,
Quoted 26 lines · Eddie James
Quoted 4 lines · Ilias Apalodimas
Hi Eddie,
Thanks for the cleanup! Unfortunately this doesn't compile with EFI selected, but in general it looks pretty good.
Thanks, yes I forgot to remove
tcg2_pcr_readQuoted 14 lines · Eddie James
Quoted 12 lines
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.
Signed-off-by Eddie James <[email protected]>--- include/efi_tcg2.h | 44 -- include/tpm-v2.h | 254 ++++++++++ lib/efi_loader/efi_tcg2.c | 975 +++----------------------------------- lib/tpm-v2.c | 799 +++++++++++++++++++++++++++++++ 4 files changed, 1129 insertions(+), 943 deletions(-)
Can you please point to the spec containing what this implements? I am still not sure why the identifiers and filenames have EFI in them.
Regards,
Simon
Quoted 26 lines · Simon Glass
Hi Eddie / Ilias,
Quoted 23 lines · Eddie James
Quoted 4 lines · Ilias Apalodimas
Hi Eddie,
Thanks for the cleanup! Unfortunately this doesn't compile with EFI selected, but in general it looks pretty good.
Thanks, yes I forgot to remove
tcg2_pcr_readQuoted 13 lines · Eddie James
Quoted 12 lines
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.
Signed-off-by Eddie James <[email protected]>--- include/efi_tcg2.h | 44 -- include/tpm-v2.h | 254 ++++++++++ lib/efi_loader/efi_tcg2.c | 975 +++----------------------------------- lib/tpm-v2.c | 799 +++++++++++++++++++++++++++++++ 4 files changed, 1129 insertions(+), 943 deletions(-)
Quoted 2 lines
Can you please point to the spec containing what this implements? I am still not sure why the identifiers and filenames have EFI in them.
Yes. Primarily implementing this specification: https://trustedcomputinggroup.org/wp-content/uploads/TCG_ServerManagDomainFWProfile_r1p00_pub.pdf
See chapter 9, event logging, which mentions the TCG_EfiSpecIdEvent as the first event in the event log.
Thanks,
Eddie
Quoted 3 lines
Regards,
Simon
Hi Eddie,
Quoted 33 lines · Eddie James
Quoted 26 lines · Simon Glass
Hi Eddie / Ilias,
Quoted 23 lines · Eddie James
Quoted 4 lines · Ilias Apalodimas
Hi Eddie,
Thanks for the cleanup! Unfortunately this doesn't compile with EFI selected, but in general it looks pretty good.
Thanks, yes I forgot to remove
tcg2_pcr_readQuoted 13 lines · Eddie James
Quoted 12 lines
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.
Signed-off-by Eddie James <[email protected]>--- include/efi_tcg2.h | 44 -- include/tpm-v2.h | 254 ++++++++++ lib/efi_loader/efi_tcg2.c | 975 +++----------------------------------- lib/tpm-v2.c | 799 +++++++++++++++++++++++++++++++ 4 files changed, 1129 insertions(+), 943 deletions(-)Quoted 2 lines
Can you please point to the spec containing what this implements? I am still not sure why the identifiers and filenames have EFI in them.
Quoted 6 lines
Yes. Primarily implementing this specification:
https://trustedcomputinggroup.org/wp-content/uploads/TCG_ServerManagDomainFWProfile_r1p00_pub.pdfSee chapter 9, event logging, which mentions the
TCG_EfiSpecIdEventas the first event in the event log.
OK thank you for the reference. What is the difference between that and TCG_PCClientPCREvent?
Regards,
Simon