Blame SOURCES/0046-tpm-Include-information-about-PE-COFF-images-in-the-.patch

6a35ff
From 0a8f7ade76ff3eede486027eaa638181e6bed3b8 Mon Sep 17 00:00:00 2001
6a35ff
From: Javier Martinez Canillas <javierm@redhat.com>
6a35ff
Date: Tue, 18 Feb 2020 12:03:17 +0100
6a35ff
Subject: [PATCH 46/62] tpm: Include information about PE/COFF images in the
6a35ff
 TPM Event Log
6a35ff
6a35ff
The "TCG PC Client Specific Platform Firmware Profile Specification" says
6a35ff
that when measuring a PE/COFF image, the TCG_PCR_EVENT2 structure Event
6a35ff
field MUST contain a UEFI_IMAGE_LOAD_EVENT structure.
6a35ff
6a35ff
Currently an empty UEFI_IMAGE_LOAD_EVENT structure is passed so users only
6a35ff
have the hash of the PE/COFF image, but not information such the file path
6a35ff
of the binary.
6a35ff
6a35ff
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
6a35ff
Upstream-commit-id: c252b9ee94c
6a35ff
---
6a35ff
 shim.c        |  7 +++++--
6a35ff
 tpm.c         | 46 ++++++++++++++++++++++++++++++++--------------
6a35ff
 include/tpm.h |  5 +++--
6a35ff
 3 files changed, 40 insertions(+), 18 deletions(-)
6a35ff
6a35ff
diff --git a/shim.c b/shim.c
6a35ff
index a4f7769b38b..b35b0ad90cc 100644
6a35ff
--- a/shim.c
6a35ff
+++ b/shim.c
6a35ff
@@ -1274,7 +1274,9 @@ static EFI_STATUS handle_image (void *data, unsigned int datasize,
6a35ff
 #ifdef REQUIRE_TPM
6a35ff
 	efi_status =
6a35ff
 #endif
6a35ff
-	tpm_log_pe((EFI_PHYSICAL_ADDRESS)(UINTN)data, datasize, sha1hash, 4);
6a35ff
+	tpm_log_pe((EFI_PHYSICAL_ADDRESS)(UINTN)data, datasize,
6a35ff
+		   (EFI_PHYSICAL_ADDRESS)(UINTN)context.ImageAddress,
6a35ff
+		   li->FilePath, sha1hash, 4);
6a35ff
 #ifdef REQUIRE_TPM
6a35ff
 	if (efi_status != EFI_SUCCESS) {
6a35ff
 		return efi_status;
6a35ff
@@ -1788,7 +1790,8 @@ EFI_STATUS shim_verify (void *buffer, UINT32 size)
6a35ff
 #ifdef REQUIRE_TPM
6a35ff
 	efi_status =
6a35ff
 #endif
6a35ff
-	tpm_log_pe((EFI_PHYSICAL_ADDRESS)(UINTN)buffer, size, sha1hash, 4);
6a35ff
+	tpm_log_pe((EFI_PHYSICAL_ADDRESS)(UINTN)buffer, size, 0, NULL,
6a35ff
+		   sha1hash, 4);
6a35ff
 #ifdef REQUIRE_TPM
6a35ff
 	if (EFI_ERROR(efi_status))
6a35ff
 		goto done;
6a35ff
diff --git a/tpm.c b/tpm.c
6a35ff
index 196b93c30f6..22ad148b35a 100644
6a35ff
--- a/tpm.c
6a35ff
+++ b/tpm.c
6a35ff
@@ -210,21 +210,39 @@ EFI_STATUS tpm_log_event(EFI_PHYSICAL_ADDRESS buf, UINTN size, UINT8 pcr,
6a35ff
 				 strlen(description) + 1, 0xd, NULL);
6a35ff
 }
6a35ff
 
6a35ff
-EFI_STATUS tpm_log_pe(EFI_PHYSICAL_ADDRESS buf, UINTN size, UINT8 *sha1hash,
6a35ff
-		      UINT8 pcr)
6a35ff
+EFI_STATUS tpm_log_pe(EFI_PHYSICAL_ADDRESS buf, UINTN size,
6a35ff
+		      EFI_PHYSICAL_ADDRESS addr, EFI_DEVICE_PATH *path,
6a35ff
+		      UINT8 *sha1hash, UINT8 pcr)
6a35ff
 {
6a35ff
-	EFI_IMAGE_LOAD_EVENT ImageLoad;
6a35ff
-
6a35ff
-	// All of this is informational and forces us to do more parsing before
6a35ff
-	// we can generate it, so let's just leave it out for now
6a35ff
-	ImageLoad.ImageLocationInMemory = 0;
6a35ff
-	ImageLoad.ImageLengthInMemory = 0;
6a35ff
-	ImageLoad.ImageLinkTimeAddress = 0;
6a35ff
-	ImageLoad.LengthOfDevicePath = 0;
6a35ff
-
6a35ff
-	return tpm_log_event_raw(buf, size, pcr, (CHAR8 *)&ImageLoad,
6a35ff
-				 sizeof(ImageLoad),
6a35ff
-				 EV_EFI_BOOT_SERVICES_APPLICATION, sha1hash);
6a35ff
+	EFI_IMAGE_LOAD_EVENT *ImageLoad = NULL;
6a35ff
+	EFI_STATUS efi_status;
6a35ff
+	UINTN path_size = 0;
6a35ff
+
6a35ff
+	if (path)
6a35ff
+		path_size = DevicePathSize(path);
6a35ff
+
6a35ff
+	ImageLoad = AllocateZeroPool(sizeof(*ImageLoad) + path_size);
6a35ff
+	if (!ImageLoad) {
6a35ff
+		perror(L"Unable to allocate image load event structure\n");
6a35ff
+		return EFI_OUT_OF_RESOURCES;
6a35ff
+	}
6a35ff
+
6a35ff
+	ImageLoad->ImageLocationInMemory = buf;
6a35ff
+	ImageLoad->ImageLengthInMemory = size;
6a35ff
+	ImageLoad->ImageLinkTimeAddress = addr;
6a35ff
+
6a35ff
+	if (path_size > 0) {
6a35ff
+		CopyMem(ImageLoad->DevicePath, path, path_size);
6a35ff
+		ImageLoad->LengthOfDevicePath = path_size;
6a35ff
+	}
6a35ff
+
6a35ff
+	efi_status = tpm_log_event_raw(buf, size, pcr, (CHAR8 *)ImageLoad,
6a35ff
+				       sizeof(*ImageLoad) + path_size,
6a35ff
+				       EV_EFI_BOOT_SERVICES_APPLICATION,
6a35ff
+				       sha1hash);
6a35ff
+	FreePool(ImageLoad);
6a35ff
+
6a35ff
+	return efi_status;
6a35ff
 }
6a35ff
 
6a35ff
 typedef struct {
6a35ff
diff --git a/include/tpm.h b/include/tpm.h
6a35ff
index 746e871ff22..a05c24949e5 100644
6a35ff
--- a/include/tpm.h
6a35ff
+++ b/include/tpm.h
6a35ff
@@ -10,8 +10,9 @@ EFI_STATUS tpm_log_event(EFI_PHYSICAL_ADDRESS buf, UINTN size, UINT8 pcr,
6a35ff
 			 const CHAR8 *description);
6a35ff
 EFI_STATUS fallback_should_prefer_reset(void);
6a35ff
 
6a35ff
-EFI_STATUS tpm_log_pe(EFI_PHYSICAL_ADDRESS buf, UINTN size, UINT8 *sha1hash,
6a35ff
-		      UINT8 pcr);
6a35ff
+EFI_STATUS tpm_log_pe(EFI_PHYSICAL_ADDRESS buf, UINTN size,
6a35ff
+		      EFI_PHYSICAL_ADDRESS addr, EFI_DEVICE_PATH *path,
6a35ff
+		      UINT8 *sha1hash, UINT8 pcr);
6a35ff
 
6a35ff
 EFI_STATUS tpm_measure_variable(CHAR16 *dbname, EFI_GUID guid, UINTN size, void *data);
6a35ff
 
6a35ff
-- 
6a35ff
2.26.2
6a35ff