arrfab / rpms / shim

Forked from rpms/shim 4 years ago
Clone

Blame SOURCES/0024-Kees-patch-missed-the-offset-adjustment-to-PEHdr.patch

e97c83
From a876037a0d4b91638fcb6274bd7a976f8318a7e2 Mon Sep 17 00:00:00 2001
e97c83
From: Peter Jones <pjones@redhat.com>
e97c83
Date: Fri, 11 Apr 2014 15:05:24 -0400
e97c83
Subject: [PATCH 24/74] Kees' patch missed the offset adjustment to PEHdr.
e97c83
e97c83
In read_header, we adjust context->PEHdr's address by doshdr->e_lfanew.
e97c83
If we're going to recompute that address, we have to adjust it here
e97c83
too.
e97c83
e97c83
Signed-off-by: Peter Jones <pjones@redhat.com>
e97c83
---
e97c83
 shim.c | 26 +++++++++++++++++++-------
e97c83
 1 file changed, 19 insertions(+), 7 deletions(-)
e97c83
e97c83
diff --git a/shim.c b/shim.c
e97c83
index 8c583a4..d06bd02 100644
e97c83
--- a/shim.c
e97c83
+++ b/shim.c
e97c83
@@ -511,12 +511,8 @@ static EFI_STATUS generate_hash (char *data, int datasize_in,
e97c83
 	EFI_IMAGE_SECTION_HEADER  *SectionHeader = NULL;
e97c83
 	EFI_IMAGE_SECTION_HEADER  *SectionCache;
e97c83
 	EFI_STATUS status = EFI_SUCCESS;
e97c83
-
e97c83
-	sha256ctxsize = Sha256GetContextSize();
e97c83
-	sha256ctx = AllocatePool(sha256ctxsize);
e97c83
-
e97c83
-	sha1ctxsize = Sha1GetContextSize();
e97c83
-	sha1ctx = AllocatePool(sha1ctxsize);
e97c83
+	EFI_IMAGE_DOS_HEADER *DosHdr = (void *)data;
e97c83
+	unsigned int PEHdr_offset = 0;
e97c83
 
e97c83
 	if (datasize_in < 0) {
e97c83
 		Print(L"Invalid data size\n");
e97c83
@@ -524,6 +520,19 @@ static EFI_STATUS generate_hash (char *data, int datasize_in,
e97c83
 	}
e97c83
 	size = datasize = (unsigned int)datasize_in;
e97c83
 
e97c83
+	if (datasize <= sizeof (*DosHdr) ||
e97c83
+	    DosHdr->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
e97c83
+		Print(L"Invalid signature\n");
e97c83
+		return EFI_INVALID_PARAMETER;
e97c83
+	}
e97c83
+	PEHdr_offset = DosHdr->e_lfanew;
e97c83
+
e97c83
+	sha256ctxsize = Sha256GetContextSize();
e97c83
+	sha256ctx = AllocatePool(sha256ctxsize);
e97c83
+
e97c83
+	sha1ctxsize = Sha1GetContextSize();
e97c83
+	sha1ctx = AllocatePool(sha1ctxsize);
e97c83
+
e97c83
 	if (!sha256ctx || !sha1ctx) {
e97c83
 		Print(L"Unable to allocate memory for hash context\n");
e97c83
 		return EFI_OUT_OF_RESOURCES;
e97c83
@@ -590,6 +599,7 @@ static EFI_STATUS generate_hash (char *data, int datasize_in,
e97c83
 
e97c83
 		/* Validate SectionPtr is within image */
e97c83
 		SectionPtr = ImageAddress(data, datasize,
e97c83
+			PEHdr_offset +
e97c83
 			sizeof (UINT32) +
e97c83
 			sizeof (EFI_IMAGE_FILE_HEADER) +
e97c83
 			context->PEHdr->Pe32.FileHeader.SizeOfOptionalHeader +
e97c83
@@ -617,7 +627,9 @@ static EFI_STATUS generate_hash (char *data, int datasize_in,
e97c83
 	}
e97c83
 
e97c83
 	/* Already validated above */
e97c83
-	Section = ImageAddress(data, datasize, sizeof (UINT32) +
e97c83
+	Section = ImageAddress(data, datasize,
e97c83
+		PEHdr_offset +
e97c83
+		sizeof (UINT32) +
e97c83
 		sizeof (EFI_IMAGE_FILE_HEADER) +
e97c83
 		context->PEHdr->Pe32.FileHeader.SizeOfOptionalHeader);
e97c83
 
e97c83
-- 
e97c83
1.9.3
e97c83