Skip to contentSkip to conversation

PE/COFF measurement support

PATCH Superseded Masahisa Kojima 3 participants 7 messages May 12, 2021 – May 13, 2021

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v5 cover PE/COFF measurement support

This patch series add the PE/COFF measurement support. Extending PCR and Event Log is tested with fTPM running as a OP-TEE TA.
Unit test will be added in the separate series.

Masahisa Kojima (3):
efi_loader: expose efi_image_parse() even if UEFI Secure Boot is
disabled
efi_loader: add PE/COFF image measurement
efi_loader: add FIT_SIGNATURE option to use hash_calculate()

 include/efi_loader.h              |   6 +
 include/efi_tcg2.h                |   9 ++
 include/tpm-v2.h                  |  18 +++
 lib/efi_loader/Kconfig            |   9 ++
 lib/efi_loader/Makefile           |   2 +-
 lib/efi_loader/efi_image_loader.c | 123 +++++++++++++++---
 lib/efi_loader/efi_signature.c    |  67 +---------
 lib/efi_loader/efi_tcg2.c         | 207 ++++++++++++++++++++++++++++--
 lib/efi_loader/efi_var_common.c   |   3 +
 9 files changed, 352 insertions(+), 92 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v5 1/3 efi_loader: expose efi_image_parse() even if UEFI Secure Boot is disabled

This is preparation for PE/COFF measurement support. PE/COFF image hash calculation is same in both UEFI Secure Boot image verification and measurement in measured boot. PE/COFF image parsing functions are gathered into efi_image_loader.c, and exposed even if UEFI Secure Boot is not enabled.

This commit also adds the EFI_SIGNATURE_SUPPORT option to decide if efi_signature.c shall be compiled.

---

(no changes since v4)

Changes in v4:
- revert #ifdef instead of using "if (!IS_ENABLED())" statement, not to rely on the compiler optimization.

Changes in v3:
- hide EFI_SIGNATURE_SUPPORT option

Changes in v2:
- Remove all #ifdef from efi_image_loader.c and efi_signature.c
- Add EFI_SIGNATURE_SUPPORT option
- Explicitly include <u-boot/hash-checksum.h>
- Gather PE/COFF parsing functions into efi_image_loader.c
- Move efi_guid_t efi_guid_image_security_database in efi_var_common.c

 lib/efi_loader/Kconfig            |  6 +++
 lib/efi_loader/Makefile           |  2 +-
 lib/efi_loader/efi_image_loader.c | 64 ++++++++++++++++++++++++++++-
 lib/efi_loader/efi_signature.c    | 67 +------------------------------
 lib/efi_loader/efi_var_common.c   |  3 ++
 5 files changed, 74 insertions(+), 68 deletions(-)
lib/efi_loader/Kconfig +6 -0
index c259abe033..385a81d7d9 100644
@@ -174,6 +174,7 @@ config EFI_CAPSULE_AUTHENTICATE
 	select PKCS7_MESSAGE_PARSER
 	select PKCS7_VERIFY
 	select IMAGE_SIGN_INFO
+	select EFI_SIGNATURE_SUPPORT
 	default n
 	help
 	  Select this option if you want to enable capsule
@@ -342,6 +343,7 @@ config EFI_SECURE_BOOT
 	select X509_CERTIFICATE_PARSER
 	select PKCS7_MESSAGE_PARSER
 	select PKCS7_VERIFY
+	select EFI_SIGNATURE_SUPPORT
 	default n
 	help
 	  Select this option to enable EFI secure boot support.
@@ -349,6 +351,10 @@ config EFI_SECURE_BOOT
 	  it is signed with a trusted key. To do that, you need to install,
 	  at least, PK, KEK and db.
 
+config EFI_SIGNATURE_SUPPORT
+	bool
+	depends on EFI_SECURE_BOOT || EFI_CAPSULE_AUTHENTICATE
+
 config EFI_ESRT
 	bool "Enable the UEFI ESRT generation"
 	depends on EFI_CAPSULE_FIRMWARE_MANAGEMENT
lib/efi_loader/Makefile +1 -1
index 8bd343e258..fd344cea29 100644
@@ -63,7 +63,7 @@ obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
 obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o
 obj-$(CONFIG_EFI_TCG2_PROTOCOL) += efi_tcg2.o
 obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
-obj-y += efi_signature.o
+obj-$(CONFIG_EFI_SIGNATURE_SUPPORT) += efi_signature.o
 
 EFI_VAR_SEED_FILE := $(subst $\",,$(CONFIG_EFI_VAR_SEED_FILE))
 $(obj)/efi_var_seed.o: $(srctree)/$(EFI_VAR_SEED_FILE)
lib/efi_loader/efi_image_loader.c +63 -1

76 lines not on this page · raw message

lib/efi_loader/efi_signature.c +1 -66

91 lines not on this page · raw message

lib/efi_loader/efi_var_common.c +3 -0
index b11ed91a74..83479dd142 100644
@@ -24,6 +24,9 @@ struct efi_auth_var_name_type {
 	const enum efi_auth_var_type type;
 };
 
+const efi_guid_t efi_guid_image_security_database =
+		EFI_IMAGE_SECURITY_DATABASE_GUID;
+
 static const struct efi_auth_var_name_type name_type[] = {
 	{u"PK", &efi_global_variable_guid, EFI_AUTH_VAR_PK},
 	{u"KEK", &efi_global_variable_guid, EFI_AUTH_VAR_KEK},
Signature · 1 line
2.17.1
#2 lore raw

PATCH v5 1/3 efi_loader: expose efi_image_parse() even if UEFI Secure Boot is disabled

Quoted 11 lines · Masahisa Kojima

This is preparation for PE/COFF measurement support. PE/COFF image hash calculation is same in both UEFI Secure Boot image verification and measurement in measured boot. PE/COFF image parsing functions are gathered into efi_image_loader.c, and exposed even if UEFI Secure Boot is not enabled.

This commit also adds the EFI_SIGNATURE_SUPPORT option to decide if efi_signature.c shall be compiled.

This patch leads to an error:

lib/crypto/x509_public_key.c:85: more undefined references to `hash_calculate' follow
collect2: error: ld returned 1 exit status
make: *** [Makefile:1726: u-boot] Error 1

Applying the patches in the given sequence will break git bisect.

Please, correct the sequence of the patches in the series.

Best regards

Heinrich

Quoted 264 lines

---

(no changes since v4)

Changes in v4:
- revert #ifdef instead of using "if (!IS_ENABLED())" statement, not to rely on the compiler optimization.

Changes in v3:
- hide EFI_SIGNATURE_SUPPORT option

Changes in v2:
- Remove all #ifdef from efi_image_loader.c and efi_signature.c
- Add EFI_SIGNATURE_SUPPORT option
- Explicitly include <u-boot/hash-checksum.h>
- Gather PE/COFF parsing functions into efi_image_loader.c
- Move efi_guid_t efi_guid_image_security_database in efi_var_common.c

  lib/efi_loader/Kconfig            |  6 +++
  lib/efi_loader/Makefile           |  2 +-
  lib/efi_loader/efi_image_loader.c | 64 ++++++++++++++++++++++++++++-
  lib/efi_loader/efi_signature.c    | 67 +------------------------------
  lib/efi_loader/efi_var_common.c   |  3 ++
  5 files changed, 74 insertions(+), 68 deletions(-)
lib/efi_loader/Kconfig +6 -0
index c259abe033..385a81d7d9 100644
@@ -174,6 +174,7 @@ config EFI_CAPSULE_AUTHENTICATE
  	select PKCS7_MESSAGE_PARSER
  	select PKCS7_VERIFY
  	select IMAGE_SIGN_INFO
+	select EFI_SIGNATURE_SUPPORT
  	default n
  	help
  	  Select this option if you want to enable capsule
@@ -342,6 +343,7 @@ config EFI_SECURE_BOOT
  	select X509_CERTIFICATE_PARSER
  	select PKCS7_MESSAGE_PARSER
  	select PKCS7_VERIFY
+	select EFI_SIGNATURE_SUPPORT
  	default n
  	help
  	  Select this option to enable EFI secure boot support.
@@ -349,6 +351,10 @@ config EFI_SECURE_BOOT
  	  it is signed with a trusted key. To do that, you need to install,
  	  at least, PK, KEK and db.
 
+config EFI_SIGNATURE_SUPPORT
+	bool
+	depends on EFI_SECURE_BOOT || EFI_CAPSULE_AUTHENTICATE
+
  config EFI_ESRT
  	bool "Enable the UEFI ESRT generation"
  	depends on EFI_CAPSULE_FIRMWARE_MANAGEMENT
lib/efi_loader/Makefile +1 -1
index 8bd343e258..fd344cea29 100644
@@ -63,7 +63,7 @@ obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
  obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o
  obj-$(CONFIG_EFI_TCG2_PROTOCOL) += efi_tcg2.o
  obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
-obj-y += efi_signature.o
+obj-$(CONFIG_EFI_SIGNATURE_SUPPORT) += efi_signature.o
 
  EFI_VAR_SEED_FILE := $(subst $\",,$(CONFIG_EFI_VAR_SEED_FILE))
  $(obj)/efi_var_seed.o: $(srctree)/$(EFI_VAR_SEED_FILE)
lib/efi_loader/efi_image_loader.c +63 -1
index f53ef367ec..fe1ee198e2 100644
@@ -213,7 +213,68 @@ static void efi_set_code_and_data_type(
  	}
  }
 
-#ifdef CONFIG_EFI_SECURE_BOOT
+/**
+ * efi_image_region_add() - add an entry of region
+ * @regs:	Pointer to array of regions
+ * @start:	Start address of region (included)
+ * @end:	End address of region (excluded)
+ * @nocheck:	flag against overlapped regions
+ *
+ * Take one entry of region [@start, @end[ and insert it into the list.
+ *
+ * * If @nocheck is false, the list will be sorted ascending by address.
+ *   Overlapping entries will not be allowed.
+ *
+ * * If @nocheck is true, the list will be sorted ascending by sequence
+ *   of adding the entries. Overlapping is allowed.
+ *
+ * Return:	status code
+ */
+efi_status_t efi_image_region_add(struct efi_image_regions *regs,
+				  const void *start, const void *end,
+				  int nocheck)
+{
+	struct image_region *reg;
+	int i, j;
+
+	if (regs->num >= regs->max) {
+		EFI_PRINT("%s: no more room for regions\n", __func__);
+		return EFI_OUT_OF_RESOURCES;
+	}
+
+	if (end < start)
+		return EFI_INVALID_PARAMETER;
+
+	for (i = 0; i < regs->num; i++) {
+		reg = &regs->reg[i];
+		if (nocheck)
+			continue;
+
+		/* new data after registered region */
+		if (start >= reg->data + reg->size)
+			continue;
+
+		/* new data preceding registered region */
+		if (end <= reg->data) {
+			for (j = regs->num - 1; j >= i; j--)
+				memcpy(&regs->reg[j + 1], &regs->reg[j],
+				       sizeof(*reg));
+			break;
+		}
+
+		/* new data overlapping registered region */
+		EFI_PRINT("%s: new region already part of another\n", __func__);
+		return EFI_INVALID_PARAMETER;
+	}
+
+	reg = &regs->reg[i];
+	reg->data = start;
+	reg->size = end - start;
+	regs->num++;
+
+	return EFI_SUCCESS;
+}
+
  /**
   * cmp_pe_section() - compare virtual addresses of two PE image sections
   * @arg1:	pointer to pointer to first section header
@@ -422,6 +483,7 @@ err:
  	return false;
  }
 
+#ifdef CONFIG_EFI_SECURE_BOOT
  /**
   * efi_image_unsigned_authenticate() - authenticate unsigned image with
   * SHA256 hash
lib/efi_loader/efi_signature.c +1 -66
index c7ec275414..bdd09881fc 100644
@@ -15,18 +15,16 @@
  #include <crypto/public_key.h>
  #include <linux/compat.h>
  #include <linux/oid_registry.h>
+#include <u-boot/hash-checksum.h>
  #include <u-boot/rsa.h>
  #include <u-boot/sha256.h>
 
-const efi_guid_t efi_guid_image_security_database =
-		EFI_IMAGE_SECURITY_DATABASE_GUID;
  const efi_guid_t efi_guid_sha256 = EFI_CERT_SHA256_GUID;
  const efi_guid_t efi_guid_cert_rsa2048 = EFI_CERT_RSA2048_GUID;
  const efi_guid_t efi_guid_cert_x509 = EFI_CERT_X509_GUID;
  const efi_guid_t efi_guid_cert_x509_sha256 = EFI_CERT_X509_SHA256_GUID;
  const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
 
-#if defined(CONFIG_EFI_SECURE_BOOT) || defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
  static u8 pkcs7_hdr[] = {
  	/* SEQUENCE */
  	0x30, 0x82, 0x05, 0xc7,
@@ -539,68 +537,6 @@ out:
  	return !revoked;
  }
 
-/**
- * efi_image_region_add() - add an entry of region
- * @regs:	Pointer to array of regions
- * @start:	Start address of region (included)
- * @end:	End address of region (excluded)
- * @nocheck:	flag against overlapped regions
- *
- * Take one entry of region [@start, @end[ and insert it into the list.
- *
- * * If @nocheck is false, the list will be sorted ascending by address.
- *   Overlapping entries will not be allowed.
- *
- * * If @nocheck is true, the list will be sorted ascending by sequence
- *   of adding the entries. Overlapping is allowed.
- *
- * Return:	status code
- */
-efi_status_t efi_image_region_add(struct efi_image_regions *regs,
-				  const void *start, const void *end,
-				  int nocheck)
-{
-	struct image_region *reg;
-	int i, j;
-
-	if (regs->num >= regs->max) {
-		EFI_PRINT("%s: no more room for regions\n", __func__);
-		return EFI_OUT_OF_RESOURCES;
-	}
-
-	if (end < start)
-		return EFI_INVALID_PARAMETER;
-
-	for (i = 0; i < regs->num; i++) {
-		reg = &regs->reg[i];
-		if (nocheck)
-			continue;
-
-		/* new data after registered region */
-		if (start >= reg->data + reg->size)
-			continue;
-
-		/* new data preceding registered region */
-		if (end <= reg->data) {
-			for (j = regs->num - 1; j >= i; j--)
-				memcpy(&regs->reg[j + 1], &regs->reg[j],
-				       sizeof(*reg));
-			break;
-		}
-
-		/* new data overlapping registered region */
-		EFI_PRINT("%s: new region already part of another\n", __func__);
-		return EFI_INVALID_PARAMETER;
-	}
-
-	reg = &regs->reg[i];
-	reg->data = start;
-	reg->size = end - start;
-	regs->num++;
-
-	return EFI_SUCCESS;
-}
-
  /**
   * efi_sigstore_free - free signature store
   * @sigstore:	Pointer to signature store structure
@@ -846,4 +782,3 @@ struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name)
 
  	return efi_build_signature_store(db, db_size);
  }
-#endif /* CONFIG_EFI_SECURE_BOOT || CONFIG_EFI_CAPSULE_AUTHENTICATE */
lib/efi_loader/efi_var_common.c +3 -0
index b11ed91a74..83479dd142 100644
@@ -24,6 +24,9 @@ struct efi_auth_var_name_type {
  	const enum efi_auth_var_type type;
  };
 
+const efi_guid_t efi_guid_image_security_database =
+		EFI_IMAGE_SECURITY_DATABASE_GUID;
+
  static const struct efi_auth_var_name_type name_type[] = {
  	{u"PK", &efi_global_variable_guid, EFI_AUTH_VAR_PK},
  	{u"KEK", &efi_global_variable_guid, EFI_AUTH_VAR_KEK},
#3 lore raw

PATCH v5 1/3 efi_loader: expose efi_image_parse() even if UEFI Secure Boot is disabled

Quoted 16 lines · Heinrich Schuchardt
Quoted 11 lines · Masahisa Kojima

This is preparation for PE/COFF measurement support. PE/COFF image hash calculation is same in both UEFI Secure Boot image verification and measurement in measured boot. PE/COFF image parsing functions are gathered into efi_image_loader.c, and exposed even if UEFI Secure Boot is not enabled.

This commit also adds the EFI_SIGNATURE_SUPPORT option to decide if efi_signature.c shall be compiled.

This patch leads to an error:

Quoted 8 lines

lib/crypto/x509_public_key.c:85: more undefined references to `hash_calculate' follow
collect2: error: ld returned 1 exit status
make: *** [Makefile:1726: u-boot] Error 1

Applying the patches in the given sequence will break git bisect.

Please, correct the sequence of the patches in the series.

Sorry, my patch sequence is wrong.
Modification to include hash-checksum.o as compilation target must be the first patch.

Quoted 270 lines

Best regards

Heinrich

Quoted 264 lines

---

(no changes since v4)

Changes in v4:
- revert #ifdef instead of using "if (!IS_ENABLED())" statement, not to rely on the compiler optimization.

Changes in v3:
- hide EFI_SIGNATURE_SUPPORT option

Changes in v2:
- Remove all #ifdef from efi_image_loader.c and efi_signature.c
- Add EFI_SIGNATURE_SUPPORT option
- Explicitly include <u-boot/hash-checksum.h>
- Gather PE/COFF parsing functions into efi_image_loader.c
- Move efi_guid_t efi_guid_image_security_database in efi_var_common.c

  lib/efi_loader/Kconfig            |  6 +++
  lib/efi_loader/Makefile           |  2 +-
  lib/efi_loader/efi_image_loader.c | 64 ++++++++++++++++++++++++++++-
  lib/efi_loader/efi_signature.c    | 67 +------------------------------
  lib/efi_loader/efi_var_common.c   |  3 ++
  5 files changed, 74 insertions(+), 68 deletions(-)
lib/efi_loader/Kconfig +6 -0
index c259abe033..385a81d7d9 100644
@@ -174,6 +174,7 @@ config EFI_CAPSULE_AUTHENTICATE
      select PKCS7_MESSAGE_PARSER
      select PKCS7_VERIFY
      select IMAGE_SIGN_INFO
+     select EFI_SIGNATURE_SUPPORT
      default n
      help
        Select this option if you want to enable capsule
@@ -342,6 +343,7 @@ config EFI_SECURE_BOOT
      select X509_CERTIFICATE_PARSER
      select PKCS7_MESSAGE_PARSER
      select PKCS7_VERIFY
+     select EFI_SIGNATURE_SUPPORT
      default n
      help
        Select this option to enable EFI secure boot support.
@@ -349,6 +351,10 @@ config EFI_SECURE_BOOT
        it is signed with a trusted key. To do that, you need to install,
        at least, PK, KEK and db.
 
+config EFI_SIGNATURE_SUPPORT
+     bool
+     depends on EFI_SECURE_BOOT || EFI_CAPSULE_AUTHENTICATE
+
  config EFI_ESRT
      bool "Enable the UEFI ESRT generation"
      depends on EFI_CAPSULE_FIRMWARE_MANAGEMENT
lib/efi_loader/Makefile +1 -1
index 8bd343e258..fd344cea29 100644
@@ -63,7 +63,7 @@ obj-$(CONFIG_GENERATE_SMBIOS_TABLE) += efi_smbios.o
  obj-$(CONFIG_EFI_RNG_PROTOCOL) += efi_rng.o
  obj-$(CONFIG_EFI_TCG2_PROTOCOL) += efi_tcg2.o
  obj-$(CONFIG_EFI_LOAD_FILE2_INITRD) += efi_load_initrd.o
-obj-y += efi_signature.o
+obj-$(CONFIG_EFI_SIGNATURE_SUPPORT) += efi_signature.o
 
  EFI_VAR_SEED_FILE := $(subst $\",,$(CONFIG_EFI_VAR_SEED_FILE))
  $(obj)/efi_var_seed.o: $(srctree)/$(EFI_VAR_SEED_FILE)
lib/efi_loader/efi_image_loader.c +63 -1
index f53ef367ec..fe1ee198e2 100644
@@ -213,7 +213,68 @@ static void efi_set_code_and_data_type(
      }
  }
 
-#ifdef CONFIG_EFI_SECURE_BOOT
+/**
+ * efi_image_region_add() - add an entry of region
+ * @regs:    Pointer to array of regions
+ * @start:   Start address of region (included)
+ * @end:     End address of region (excluded)
+ * @nocheck: flag against overlapped regions
+ *
+ * Take one entry of region [@start, @end[ and insert it into the list.
+ *
+ * * If @nocheck is false, the list will be sorted ascending by address.
+ *   Overlapping entries will not be allowed.
+ *
+ * * If @nocheck is true, the list will be sorted ascending by sequence
+ *   of adding the entries. Overlapping is allowed.
+ *
+ * Return:   status code
+ */
+efi_status_t efi_image_region_add(struct efi_image_regions *regs,
+                               const void *start, const void *end,
+                               int nocheck)
+{
+     struct image_region *reg;
+     int i, j;
+
+     if (regs->num >= regs->max) {
+             EFI_PRINT("%s: no more room for regions\n", __func__);
+             return EFI_OUT_OF_RESOURCES;
+     }
+
+     if (end < start)
+             return EFI_INVALID_PARAMETER;
+
+     for (i = 0; i < regs->num; i++) {
+             reg = &regs->reg[i];
+             if (nocheck)
+                     continue;
+
+             /* new data after registered region */
+             if (start >= reg->data + reg->size)
+                     continue;
+
+             /* new data preceding registered region */
+             if (end <= reg->data) {
+                     for (j = regs->num - 1; j >= i; j--)
+                             memcpy(&regs->reg[j + 1], &regs->reg[j],
+                                    sizeof(*reg));
+                     break;
+             }
+
+             /* new data overlapping registered region */
+             EFI_PRINT("%s: new region already part of another\n", __func__);
+             return EFI_INVALID_PARAMETER;
+     }
+
+     reg = &regs->reg[i];
+     reg->data = start;
+     reg->size = end - start;
+     regs->num++;
+
+     return EFI_SUCCESS;
+}
+
  /**
   * cmp_pe_section() - compare virtual addresses of two PE image sections
   * @arg1:   pointer to pointer to first section header
@@ -422,6 +483,7 @@ err:
      return false;
  }
 
+#ifdef CONFIG_EFI_SECURE_BOOT
  /**
   * efi_image_unsigned_authenticate() - authenticate unsigned image with
   * SHA256 hash
lib/efi_loader/efi_signature.c +1 -66
index c7ec275414..bdd09881fc 100644
@@ -15,18 +15,16 @@
  #include <crypto/public_key.h>
  #include <linux/compat.h>
  #include <linux/oid_registry.h>
+#include <u-boot/hash-checksum.h>
  #include <u-boot/rsa.h>
  #include <u-boot/sha256.h>
 
-const efi_guid_t efi_guid_image_security_database =
-             EFI_IMAGE_SECURITY_DATABASE_GUID;
  const efi_guid_t efi_guid_sha256 = EFI_CERT_SHA256_GUID;
  const efi_guid_t efi_guid_cert_rsa2048 = EFI_CERT_RSA2048_GUID;
  const efi_guid_t efi_guid_cert_x509 = EFI_CERT_X509_GUID;
  const efi_guid_t efi_guid_cert_x509_sha256 = EFI_CERT_X509_SHA256_GUID;
  const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
 
-#if defined(CONFIG_EFI_SECURE_BOOT) || defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
  static u8 pkcs7_hdr[] = {
      /* SEQUENCE */
      0x30, 0x82, 0x05, 0xc7,
@@ -539,68 +537,6 @@ out:
      return !revoked;
  }
 
-/**
- * efi_image_region_add() - add an entry of region
- * @regs:    Pointer to array of regions
- * @start:   Start address of region (included)
- * @end:     End address of region (excluded)
- * @nocheck: flag against overlapped regions
- *
- * Take one entry of region [@start, @end[ and insert it into the list.
- *
- * * If @nocheck is false, the list will be sorted ascending by address.
- *   Overlapping entries will not be allowed.
- *
- * * If @nocheck is true, the list will be sorted ascending by sequence
- *   of adding the entries. Overlapping is allowed.
- *
- * Return:   status code
- */
-efi_status_t efi_image_region_add(struct efi_image_regions *regs,
-                               const void *start, const void *end,
-                               int nocheck)
-{
-     struct image_region *reg;
-     int i, j;
-
-     if (regs->num >= regs->max) {
-             EFI_PRINT("%s: no more room for regions\n", __func__);
-             return EFI_OUT_OF_RESOURCES;
-     }
-
-     if (end < start)
-             return EFI_INVALID_PARAMETER;
-
-     for (i = 0; i < regs->num; i++) {
-             reg = &regs->reg[i];
-             if (nocheck)
-                     continue;
-
-             /* new data after registered region */
-             if (start >= reg->data + reg->size)
-                     continue;
-
-             /* new data preceding registered region */
-             if (end <= reg->data) {
-                     for (j = regs->num - 1; j >= i; j--)
-                             memcpy(&regs->reg[j + 1], &regs->reg[j],
-                                    sizeof(*reg));
-                     break;
-             }
-
-             /* new data overlapping registered region */
-             EFI_PRINT("%s: new region already part of another\n", __func__);
-             return EFI_INVALID_PARAMETER;
-     }
-
-     reg = &regs->reg[i];
-     reg->data = start;
-     reg->size = end - start;
-     regs->num++;
-
-     return EFI_SUCCESS;
-}
-
  /**
   * efi_sigstore_free - free signature store
   * @sigstore:       Pointer to signature store structure
@@ -846,4 +782,3 @@ struct efi_signature_store *efi_sigstore_parse_sigdb(u16 *name)
 
      return efi_build_signature_store(db, db_size);
  }
-#endif /* CONFIG_EFI_SECURE_BOOT || CONFIG_EFI_CAPSULE_AUTHENTICATE */
lib/efi_loader/efi_var_common.c +3 -0
index b11ed91a74..83479dd142 100644
@@ -24,6 +24,9 @@ struct efi_auth_var_name_type {
      const enum efi_auth_var_type type;
  };
 
+const efi_guid_t efi_guid_image_security_database =
+             EFI_IMAGE_SECURITY_DATABASE_GUID;
+
  static const struct efi_auth_var_name_type name_type[] = {
      {u"PK", &efi_global_variable_guid, EFI_AUTH_VAR_PK},
      {u"KEK", &efi_global_variable_guid, EFI_AUTH_VAR_KEK},
#4 lore raw

PATCH v5 2/3 efi_loader: add PE/COFF image measurement

In reply to Masahisa Kojima

"TCG PC Client Platform Firmware Profile Specification" requires to measure every attempt to load and execute a OS Loader(a UEFI application) into PCR[4].
This commit adds the PE/COFF image measurement, extends PCR, and appends measurement into Event Log.

---

(no changes since v2)

Changes in v2:
- Remove duplicate <efi.h> include
- Remove unnecessary __packed attribute
- Add all EV_EFI_* event definition
- Create common function to prepare 8-byte aligned image
- Add measurement for EV_EFI_BOOT_SERVICES_DRIVER and EV_EFI_RUNTIME_SERVICES_DRIVER
- Use efi_search_protocol() to get device_path
- Add function comment

 include/efi_loader.h              |   6 +
 include/efi_tcg2.h                |   9 ++
 include/tpm-v2.h                  |  18 +++
 lib/efi_loader/efi_image_loader.c |  59 +++++++--
 lib/efi_loader/efi_tcg2.c         | 207 ++++++++++++++++++++++++++++--
 5 files changed, 275 insertions(+), 24 deletions(-)
include/efi_loader.h +6 -0
index de1a496a97..9f2854a255 100644
@@ -426,6 +426,10 @@ efi_status_t efi_disk_register(void);
 efi_status_t efi_rng_register(void);
 /* Called by efi_init_obj_list() to install EFI_TCG2_PROTOCOL */
 efi_status_t efi_tcg2_register(void);
+/* measure the pe-coff image, extend PCR and add Event Log */
+efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
+				   struct efi_loaded_image_obj *handle,
+				   struct efi_loaded_image *loaded_image_info);
 /* Create handles and protocols for the partitions of a block device */
 int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
 			       const char *if_typename, int diskid,
@@ -847,6 +851,8 @@ bool efi_secure_boot_enabled(void);
 
 bool efi_capsule_auth_enabled(void);
 
+void *efi_prepare_aligned_image(void *efi, u64 *efi_size, void **new_efi);
+
 bool efi_image_parse(void *efi, size_t len, struct efi_image_regions **regp,
 		     WIN_CERTIFICATE **auth, size_t *auth_len);
 
include/efi_tcg2.h +9 -0
index 40e241ce31..bcfb98168a 100644
@@ -9,6 +9,7 @@
 #if !defined _EFI_TCG2_PROTOCOL_H_
 #define _EFI_TCG2_PROTOCOL_H_
 
+#include <efi_api.h>
 #include <tpm-v2.h>
 
 #define EFI_TCG2_PROTOCOL_GUID \
@@ -53,6 +54,14 @@ struct efi_tcg2_event {
 	u8 event[];
 } __packed;
 
+struct uefi_image_load_event {
+	efi_physical_addr_t image_location_in_memory;
+	u64 image_length_in_memory;
+	u64 image_link_time_address;
+	u64 length_of_device_path;
+	struct efi_device_path device_path[];
+};
+
 struct efi_tcg2_boot_service_capability {
 	u8 size;
 	struct efi_tcg2_version structure_version;
include/tpm-v2.h +18 -0
index 7de7d6a57d..247b386967 100644
@@ -70,6 +70,24 @@ struct udevice;
 #define EV_TABLE_OF_DEVICES		((u32)0x0000000B)
 #define EV_COMPACT_HASH			((u32)0x0000000C)
 
+/*
+ * event types, cf.
+ * "TCG PC Client Platform Firmware Profile Specification", Family "2.0"
+ * rev 1.04, June 3, 2019
+ */
+#define EV_EFI_EVENT_BASE			((u32)0x80000000)
+#define EV_EFI_VARIABLE_DRIVER_CONFIG		((u32)0x80000001)
+#define EV_EFI_VARIABLE_BOOT			((u32)0x80000002)
+#define EV_EFI_BOOT_SERVICES_APPLICATION	((u32)0x80000003)
+#define EV_EFI_BOOT_SERVICES_DRIVER		((u32)0x80000004)
+#define EV_EFI_RUNTIME_SERVICES_DRIVER		((u32)0x80000005)
+#define EV_EFI_GPT_EVENT			((u32)0x80000006)
+#define EV_EFI_ACTION				((u32)0x80000007)
+#define EV_EFI_PLATFORM_FIRMWARE_BLOB		((u32)0x80000008)
+#define EV_EFI_HANDOFF_TABLES			((u32)0x80000009)
+#define EV_EFI_HCRTM_EVENT			((u32)0x80000010)
+#define EV_EFI_VARIABLE_AUTHORITY		((u32)0x800000E0)
+
 /* TPMS_TAGGED_PROPERTY Structure */
 struct tpms_tagged_property {
 	u32 property;
lib/efi_loader/efi_image_loader.c +45 -14

83 lines not on this page · raw message

lib/efi_loader/efi_tcg2.c +197 -10

235 lines not on this page · raw message

Signature · 1 line
2.17.1
#5 lore raw

PATCH v5 2/3 efi_loader: add PE/COFF image measurement

[...]

Quoted 26 lines
lib/efi_loader/efi_tcg2.c +7 -8
+
 /**
  * efi_tcg2_hash_log_extend_event() - extend and optionally log events
  *
@@ -758,24 +937,32 @@ efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
 	/*
 	 * if PE_COFF_IMAGE is set we need to make sure the image is not
 	 * corrupted, verify it and hash the PE/COFF image in accordance with
-	 * the  procedure  specified  in  "Calculating  the  PE  Image  Hash"
-	 * section  of the "Windows Authenticode Portable Executable Signature
+	 * the procedure specified in "Calculating the PE Image Hash"
+	 * section of the "Windows Authenticode Portable Executable Signature
 	 * Format"
-	 * Not supported for now
 	 */
 	if (flags & PE_COFF_IMAGE) {
-		ret = EFI_UNSUPPORTED;
-		goto out;
-	}
+		IMAGE_NT_HEADERS32 *nt;
 
-	pcr_index = efi_tcg_event->header.pcr_index;
-	event_type = efi_tcg_event->header.event_type;
+		ret = efi_check_pe((void *)data_to_hash, data_to_hash_len,
+				   (void **)&nt);
+		if (ret != EFI_SUCCESS) {

lib/efi_loader/efi_tcg2.c efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags, · line 957

Quoted 10 lines
lib/efi_loader/efi_tcg2.c +7 -2
+			log_err("Not a valid PE-COFF file\n");
+			goto out;
+		}
 
-	ret = tcg2_create_digest((u8 *)data_to_hash, data_to_hash_len,
-				 &digest_list);
+		ret = tcg2_hash_pe_image((void *)data_to_hash, data_to_hash_len,
+					 &digest_list);
+	} else {
+		ret = tcg2_create_digest((u8 *)data_to_hash, data_to_hash_len,

I think this needs a (u8 *)(uintptr_t) which is carried on from the original code. I don't mind merging this as-is and I send a follow up. Heinrich any preference?

Quoted 14 lines
lib/efi_loader/efi_tcg2.c +5 -0
+					 &digest_list);
+	}
 	if (ret != EFI_SUCCESS)
 		goto out;
 
+	pcr_index = efi_tcg_event->header.pcr_index;
+	event_type = efi_tcg_event->header.event_type;
+
 	ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
 	if (ret != EFI_SUCCESS)
 		goto out;
Signature · 1 line
2.17.1

Cheers
/Ilias

#6 lore raw

PATCH v5 3/3 efi_loader: add FIT_SIGNATURE option to use hash_calculate()

In reply to Masahisa Kojima

Build error occurs when CONFIG_EFI_SECURE_BOOT/ CONFIG_EFI_CAPSULE_AUTHENTICATE/CONFIG_EFI_TCG2_PROTOCOL is enabled, because hash-checksum.c is not compiled.

With the following commit,
commit 0bcb28dfb946 ("lib: Rename rsa-checksum.c to hash-checksum.c") CONFIG_FIT_SIGNATURE option is required to use hash_calculate() function.

This commit selects FIT_SIGNATURE option in Kconfig.

---

Changes in v5:
- Missing option for EFI_TCG2_PROTOROL already added in different commit. This commit adds FIT_SIGNATRE only.

Changes in v4:
- newly added in this patch series, due to rebasing the base code.

 lib/efi_loader/Kconfig | 3 +++
 1 file changed, 3 insertions(+)
lib/efi_loader/Kconfig +3 -0
index 385a81d7d9..0ffced25b1 100644
@@ -175,6 +175,7 @@ config EFI_CAPSULE_AUTHENTICATE
 	select PKCS7_VERIFY
 	select IMAGE_SIGN_INFO
 	select EFI_SIGNATURE_SUPPORT
+	select FIT_SIGNATURE
 	default n
 	help
 	  Select this option if you want to enable capsule
@@ -308,6 +309,7 @@ config EFI_TCG2_PROTOCOL
 	select SHA512_ALGO
 	select SHA384
 	select SHA512
+	select FIT_SIGNATURE
 	help
 	  Provide a EFI_TCG2_PROTOCOL implementation using the TPM hardware
 	  of the platform.
@@ -344,6 +346,7 @@ config EFI_SECURE_BOOT
 	select PKCS7_MESSAGE_PARSER
 	select PKCS7_VERIFY
 	select EFI_SIGNATURE_SUPPORT
+	select FIT_SIGNATURE
 	default n
 	help
 	  Select this option to enable EFI secure boot support.
Signature · 1 line
2.17.1