MdePkg: Use SafeIntLib to handle overflow

Used SafeIntLib to handle the overflow check in
PeCoffLoaderRelocateImage

Signed-off-by: Sachin Ganesh <sachinganesh@ami.com>
This commit is contained in:
INDIA\sachinganesh 2025-01-23 18:35:35 +05:30 committed by mergify[bot]
parent aedcaa3df8
commit 6278bbb898

View File

@ -976,6 +976,7 @@ PeCoffLoaderRelocateImage (
PHYSICAL_ADDRESS BaseAddress;
UINT32 NumberOfRvaAndSizes;
UINT32 TeStrippedOffset;
UINT32 EndAddress;
ASSERT (ImageContext != NULL);
@ -1055,15 +1056,20 @@ PeCoffLoaderRelocateImage (
RelocDir = &Hdr.Te->DataDirectory[0];
}
if ((RelocDir != NULL) && (RelocDir->Size > 0) && ((RelocDir->Size - 1) < (MAX_UINT32 - RelocDir->VirtualAddress))) {
RelocBase = (EFI_IMAGE_BASE_RELOCATION *)PeCoffLoaderImageAddress (ImageContext, RelocDir->VirtualAddress, TeStrippedOffset);
RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)PeCoffLoaderImageAddress (
ImageContext,
RelocDir->VirtualAddress + RelocDir->Size - 1,
TeStrippedOffset
);
if ((RelocDir != NULL) && (RelocDir->Size > 0)) {
Status = SafeUint32Add (RelocDir->VirtualAddress, (RelocDir->Size - 1), &EndAddress);
if (!RETURN_ERROR (Status)) {
RelocBase = (EFI_IMAGE_BASE_RELOCATION *)PeCoffLoaderImageAddress (ImageContext, RelocDir->VirtualAddress, TeStrippedOffset);
RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)PeCoffLoaderImageAddress (
ImageContext,
EndAddress,
TeStrippedOffset
);
}
if ((RelocBase == NULL) || (RelocBaseEnd == NULL) || ((UINTN)RelocBaseEnd < (UINTN)RelocBase)) {
ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
DEBUG ((DEBUG_ERROR, "Relocation block is not valid\n"));
return RETURN_LOAD_ERROR;
}
} else {