Blob Blame History Raw
From 0a8f7ade76ff3eede486027eaa638181e6bed3b8 Mon Sep 17 00:00:00 2001
From: Javier Martinez Canillas <javierm@redhat.com>
Date: Tue, 18 Feb 2020 12:03:17 +0100
Subject: [PATCH 46/62] tpm: Include information about PE/COFF images in the
 TPM Event Log

The "TCG PC Client Specific Platform Firmware Profile Specification" says
that when measuring a PE/COFF image, the TCG_PCR_EVENT2 structure Event
field MUST contain a UEFI_IMAGE_LOAD_EVENT structure.

Currently an empty UEFI_IMAGE_LOAD_EVENT structure is passed so users only
have the hash of the PE/COFF image, but not information such the file path
of the binary.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Upstream-commit-id: c252b9ee94c
---
 shim.c        |  7 +++++--
 tpm.c         | 46 ++++++++++++++++++++++++++++++++--------------
 include/tpm.h |  5 +++--
 3 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/shim.c b/shim.c
index a4f7769b38b..b35b0ad90cc 100644
--- a/shim.c
+++ b/shim.c
@@ -1274,7 +1274,9 @@ static EFI_STATUS handle_image (void *data, unsigned int datasize,
 #ifdef REQUIRE_TPM
 	efi_status =
 #endif
-	tpm_log_pe((EFI_PHYSICAL_ADDRESS)(UINTN)data, datasize, sha1hash, 4);
+	tpm_log_pe((EFI_PHYSICAL_ADDRESS)(UINTN)data, datasize,
+		   (EFI_PHYSICAL_ADDRESS)(UINTN)context.ImageAddress,
+		   li->FilePath, sha1hash, 4);
 #ifdef REQUIRE_TPM
 	if (efi_status != EFI_SUCCESS) {
 		return efi_status;
@@ -1788,7 +1790,8 @@ EFI_STATUS shim_verify (void *buffer, UINT32 size)
 #ifdef REQUIRE_TPM
 	efi_status =
 #endif
-	tpm_log_pe((EFI_PHYSICAL_ADDRESS)(UINTN)buffer, size, sha1hash, 4);
+	tpm_log_pe((EFI_PHYSICAL_ADDRESS)(UINTN)buffer, size, 0, NULL,
+		   sha1hash, 4);
 #ifdef REQUIRE_TPM
 	if (EFI_ERROR(efi_status))
 		goto done;
diff --git a/tpm.c b/tpm.c
index 196b93c30f6..22ad148b35a 100644
--- a/tpm.c
+++ b/tpm.c
@@ -210,21 +210,39 @@ EFI_STATUS tpm_log_event(EFI_PHYSICAL_ADDRESS buf, UINTN size, UINT8 pcr,
 				 strlen(description) + 1, 0xd, NULL);
 }
 
-EFI_STATUS tpm_log_pe(EFI_PHYSICAL_ADDRESS buf, UINTN size, UINT8 *sha1hash,
-		      UINT8 pcr)
+EFI_STATUS tpm_log_pe(EFI_PHYSICAL_ADDRESS buf, UINTN size,
+		      EFI_PHYSICAL_ADDRESS addr, EFI_DEVICE_PATH *path,
+		      UINT8 *sha1hash, UINT8 pcr)
 {
-	EFI_IMAGE_LOAD_EVENT ImageLoad;
-
-	// All of this is informational and forces us to do more parsing before
-	// we can generate it, so let's just leave it out for now
-	ImageLoad.ImageLocationInMemory = 0;
-	ImageLoad.ImageLengthInMemory = 0;
-	ImageLoad.ImageLinkTimeAddress = 0;
-	ImageLoad.LengthOfDevicePath = 0;
-
-	return tpm_log_event_raw(buf, size, pcr, (CHAR8 *)&ImageLoad,
-				 sizeof(ImageLoad),
-				 EV_EFI_BOOT_SERVICES_APPLICATION, sha1hash);
+	EFI_IMAGE_LOAD_EVENT *ImageLoad = NULL;
+	EFI_STATUS efi_status;
+	UINTN path_size = 0;
+
+	if (path)
+		path_size = DevicePathSize(path);
+
+	ImageLoad = AllocateZeroPool(sizeof(*ImageLoad) + path_size);
+	if (!ImageLoad) {
+		perror(L"Unable to allocate image load event structure\n");
+		return EFI_OUT_OF_RESOURCES;
+	}
+
+	ImageLoad->ImageLocationInMemory = buf;
+	ImageLoad->ImageLengthInMemory = size;
+	ImageLoad->ImageLinkTimeAddress = addr;
+
+	if (path_size > 0) {
+		CopyMem(ImageLoad->DevicePath, path, path_size);
+		ImageLoad->LengthOfDevicePath = path_size;
+	}
+
+	efi_status = tpm_log_event_raw(buf, size, pcr, (CHAR8 *)ImageLoad,
+				       sizeof(*ImageLoad) + path_size,
+				       EV_EFI_BOOT_SERVICES_APPLICATION,
+				       sha1hash);
+	FreePool(ImageLoad);
+
+	return efi_status;
 }
 
 typedef struct {
diff --git a/include/tpm.h b/include/tpm.h
index 746e871ff22..a05c24949e5 100644
--- a/include/tpm.h
+++ b/include/tpm.h
@@ -10,8 +10,9 @@ EFI_STATUS tpm_log_event(EFI_PHYSICAL_ADDRESS buf, UINTN size, UINT8 pcr,
 			 const CHAR8 *description);
 EFI_STATUS fallback_should_prefer_reset(void);
 
-EFI_STATUS tpm_log_pe(EFI_PHYSICAL_ADDRESS buf, UINTN size, UINT8 *sha1hash,
-		      UINT8 pcr);
+EFI_STATUS tpm_log_pe(EFI_PHYSICAL_ADDRESS buf, UINTN size,
+		      EFI_PHYSICAL_ADDRESS addr, EFI_DEVICE_PATH *path,
+		      UINT8 *sha1hash, UINT8 pcr);
 
 EFI_STATUS tpm_measure_variable(CHAR16 *dbname, EFI_GUID guid, UINTN size, void *data);
 
-- 
2.26.2