mirror of https://github.com/acidanthera/audk.git
MdePkg/BasePeCoffLib: Add more check for relocation data
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1426 V2: (1) Add NULL pointer check for the input parameters (2) Add check for the "Adjust" value before applying fix ups. In function PeCoffLoaderRelocateImageForRuntime, it doesn't do much check when do relocation. For API level consideration, it's not safe enough. So this patch is to replace the same code logic with function PeCoffLoaderImageAddress which will cover more validation. Cc: Michael D Kinney <michael.d.kinney@intel.com> Cc: Liming Gao <liming.gao@intel.com> Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Dandan Bi <dandan.bi@intel.com> Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
parent
9e90fb097c
commit
2f4a5a9f4c
|
@ -15,7 +15,7 @@
|
|||
PeCoffLoaderGetPeHeader() routine will do basic check for PE/COFF header.
|
||||
PeCoffLoaderGetImageInfo() routine will do basic check for whole PE/COFF image.
|
||||
|
||||
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
|
||||
Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
|
||||
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
|
||||
This program and the accompanying materials
|
||||
are licensed and made available under the terms and conditions of the BSD License
|
||||
|
@ -1671,6 +1671,7 @@ PeCoffLoaderRelocateImageForRuntime (
|
|||
EFI_IMAGE_DATA_DIRECTORY *RelocDir;
|
||||
EFI_IMAGE_BASE_RELOCATION *RelocBase;
|
||||
EFI_IMAGE_BASE_RELOCATION *RelocBaseEnd;
|
||||
EFI_IMAGE_BASE_RELOCATION *RelocBaseOrig;
|
||||
UINT16 *Reloc;
|
||||
UINT16 *RelocEnd;
|
||||
CHAR8 *Fixup;
|
||||
|
@ -1681,11 +1682,19 @@ PeCoffLoaderRelocateImageForRuntime (
|
|||
CHAR8 *FixupData;
|
||||
UINTN Adjust;
|
||||
RETURN_STATUS Status;
|
||||
PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
|
||||
|
||||
if (RelocationData == NULL || ImageBase == 0x0 || VirtImageBase == 0x0) {
|
||||
return;
|
||||
}
|
||||
|
||||
OldBase = (CHAR8 *)((UINTN)ImageBase);
|
||||
NewBase = (CHAR8 *)((UINTN)VirtImageBase);
|
||||
Adjust = (UINTN) NewBase - (UINTN) OldBase;
|
||||
|
||||
ImageContext.ImageAddress = ImageBase;
|
||||
ImageContext.ImageSize = ImageSize;
|
||||
|
||||
//
|
||||
// Find the image's relocate dir info
|
||||
//
|
||||
|
@ -1732,8 +1741,11 @@ PeCoffLoaderRelocateImageForRuntime (
|
|||
//
|
||||
if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
|
||||
RelocDir = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC;
|
||||
RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress);
|
||||
RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress + RelocDir->Size);
|
||||
RelocBase = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext, RelocDir->VirtualAddress, 0);
|
||||
RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext,
|
||||
RelocDir->VirtualAddress + RelocDir->Size,
|
||||
0
|
||||
);
|
||||
} else {
|
||||
//
|
||||
// Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.
|
||||
|
@ -1747,6 +1759,7 @@ PeCoffLoaderRelocateImageForRuntime (
|
|||
//
|
||||
ASSERT (RelocBase != NULL && RelocBaseEnd != NULL);
|
||||
|
||||
if (Adjust != 0) {
|
||||
//
|
||||
// Run the whole relocation block. And re-fixup data that has not been
|
||||
// modified. The FixupData is used to see if the image has been modified
|
||||
|
@ -1755,6 +1768,7 @@ PeCoffLoaderRelocateImageForRuntime (
|
|||
// defaults.
|
||||
//
|
||||
FixupData = RelocationData;
|
||||
RelocBaseOrig = RelocBase;
|
||||
while (RelocBase < RelocBaseEnd) {
|
||||
//
|
||||
// Add check for RelocBase->SizeOfBlock field.
|
||||
|
@ -1768,14 +1782,24 @@ PeCoffLoaderRelocateImageForRuntime (
|
|||
|
||||
Reloc = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));
|
||||
RelocEnd = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock);
|
||||
FixupBase = (CHAR8 *) ((UINTN)ImageBase) + RelocBase->VirtualAddress;
|
||||
if ((UINTN)RelocEnd > (UINTN)RelocBaseOrig + RelocDir->Size) {
|
||||
return;
|
||||
}
|
||||
|
||||
FixupBase = PeCoffLoaderImageAddress (&ImageContext, RelocBase->VirtualAddress, 0);
|
||||
if (FixupBase == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Run this relocation record
|
||||
//
|
||||
while (Reloc < RelocEnd) {
|
||||
|
||||
Fixup = FixupBase + (*Reloc & 0xFFF);
|
||||
Fixup = PeCoffLoaderImageAddress (&ImageContext, RelocBase->VirtualAddress + (*Reloc & 0xFFF), 0);
|
||||
if (Fixup == NULL) {
|
||||
return;
|
||||
}
|
||||
switch ((*Reloc) >> 12) {
|
||||
|
||||
case EFI_IMAGE_REL_BASED_ABSOLUTE:
|
||||
|
@ -1838,6 +1862,7 @@ PeCoffLoaderRelocateImageForRuntime (
|
|||
//
|
||||
RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue