ArmPkg/Library: Fix for coverity issue OVERRUN

RootCause: SectionSize, SectionLength and FileLength are declared as
UINTN, UINTN and UINT32 but are typecast to UINT32 and masked
with 0x00FFFFFF to store only the lower 24 bits.
Although this approach yields the correct result,
it introduces a potential security vulnerability due to
unsafe typecasting and dereferencing.

Solution: Using the predefined macro FFS_FILE_SIZE()
from MdePkg\Include\Pi\PiFirmwareFile.h,
which safely performs the same operation by reconstruct
the size using individual byte access.

Cc: Sachin Ganesh <sachinganesh@ami.com>
Signed-off-by: Gowtham M <gowthamm@ami.com>
This commit is contained in:
Gowtham M
2025-10-28 13:04:13 +05:30
committed by mergify[bot]
parent 05b677c9de
commit 059332bda3
@@ -141,7 +141,7 @@ GetFfsFile (
return EFI_SUCCESS;
}
FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
FileLength = FFS_FILE_SIZE (FfsFileHeader);
FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
FileOffset += FileOccupiedSize;
@@ -149,7 +149,7 @@ GetFfsFile (
break;
case EFI_FILE_DELETED:
FileLength = *(UINT32 *)(FfsFileHeader->Size) & 0x00FFFFFF;
FileLength = FFS_FILE_SIZE (FfsFileHeader);
FileOccupiedSize = GET_OCCUPIED_SIZE (FileLength, 8);
FileOffset += FileOccupiedSize;
FfsFileHeader = (EFI_FFS_FILE_HEADER *)((UINT8 *)FfsFileHeader + FileOccupiedSize);
@@ -180,7 +180,7 @@ GetImageContext (
VOID *CodeViewEntryPointer;
Section = (EFI_COMMON_SECTION_HEADER *)(FfsHeader + 1);
SectionSize = *(UINT32 *)(FfsHeader->Size) & 0x00FFFFFF;
SectionSize = FFS_FILE_SIZE (FfsHeader);
SectionSize -= sizeof (EFI_FFS_FILE_HEADER);
ParsedLength = 0;
EfiImage = NULL;
@@ -196,7 +196,7 @@ GetImageContext (
// SectionLength is adjusted it is 4 byte aligned.
// Go to the next section
//
SectionLength = *(UINT32 *)Section->Size & 0x00FFFFFF;
SectionLength = FFS_FILE_SIZE (FfsHeader);
SectionLength = GET_OCCUPIED_SIZE (SectionLength, 4);
ASSERT (SectionLength != 0);
ParsedLength += SectionLength;