X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FDxeMemoryAllocationLib%2FMemoryAllocationLib.c;h=ef643eed4296d2252506098a5bc5b18922425dc6;hb=b6bd81d45f85be8e74abb5b0a4c0e1dfea803e71;hp=a9bdf60aa1e0e1f2351e31e1684182533823c39a;hpb=878ddf1fc3540a715f63594ed22b6929e881afb4;p=mirror_edk2.git diff --git a/MdePkg/Library/DxeMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/DxeMemoryAllocationLib/MemoryAllocationLib.c index a9bdf60aa1..ef643eed42 100644 --- a/MdePkg/Library/DxeMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/DxeMemoryAllocationLib/MemoryAllocationLib.c @@ -168,6 +168,13 @@ InternalAllocateAlignedPages ( // AlignmentMask = Alignment - 1; RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment); + if (RealPages <= Pages) { + // + // This extra checking is to make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. + // + return NULL; + } + Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory); if (EFI_ERROR (Status)) { return NULL; @@ -576,8 +583,8 @@ InternalAllocateAlignedPool ( UINTN AlignedAddress; UINTN AlignmentMask; UINTN OverAllocationSize; + UINTN RealAllocationSize; VOID **FreePointer; - EFI_STATUS Status; // // Alignment must be a power of two or zero. @@ -593,8 +600,15 @@ InternalAllocateAlignedPool ( // Calculate the extra memory size, over-allocate memory pool and get the aligned memory address. // OverAllocationSize = sizeof (RawAddress) + AlignmentMask; - Status = gBS->AllocatePool (PoolType, AllocationSize + OverAllocationSize, &RawAddress); - if (EFI_ERROR (Status)) { + RealAllocationSize = AllocationSize + OverAllocationSize; + if (RealAllocationSize <= AllocationSize ) { + // + // This extra checking is to make sure that AllocationSize plus OverAllocationSize does not overflow. + // + return NULL; + } + RawAddress = InternalAllocatePool (PoolType, RealAllocationSize); + if (RawAddress == NULL) { return NULL; } AlignedAddress = ((UINTN) RawAddress + OverAllocationSize) & ~AlignmentMask;