[edk2-devel] [PATCH v3 1/3] CryptoPkg: Fix memoryleak in BaseMemAllocation

Judah Vang judah.vang at intel.com
Thu Jul 14 22:04:14 UTC 2022


REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3990

Replace AllocatePool() with AllocatePages() and FreePool() with
FreePages() because FreePool() is not supported in PEI phase.
FreePool() does not free the allocated pool in PEI phase causing
a memory leak.

Cc: Jiewen Yao <jiewen.yao at intel.com>
Cc: Jian J Wang <jian.j.wang at intel.com>
Cc: Xiaoyu Lu <xiaoyux.lu at intel.com>
Cc: Guomin Jiang <guomin.jiang at intel.com>
Cc: Nishant C Mistry <nishant.c.mistry at intel.com>
Signed-off-by: Jian J Wang <jian.j.wang at intel.com>
Signed-off-by: Nishant C Mistry <nishant.c.mistry at intel.com>
Signed-off-by: Judah Vang <judah.vang at intel.com>
---
 CryptoPkg/Library/BaseCryptLib/SysCall/BaseMemAllocation.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/CryptoPkg/Library/BaseCryptLib/SysCall/BaseMemAllocation.c b/CryptoPkg/Library/BaseCryptLib/SysCall/BaseMemAllocation.c
index b7bed15c18df..d77e1f7de5e3 100644
--- a/CryptoPkg/Library/BaseCryptLib/SysCall/BaseMemAllocation.c
+++ b/CryptoPkg/Library/BaseCryptLib/SysCall/BaseMemAllocation.c
@@ -2,13 +2,14 @@
   Base Memory Allocation Routines Wrapper for Crypto library over OpenSSL
   during PEI & DXE phases.
 
-Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2009 - 2022, Intel Corporation. All rights reserved.<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
 
 #include <CrtLibSupport.h>
 #include <Library/MemoryAllocationLib.h>
+#include <Library/BaseCryptLib.h>
 
 //
 // Extra header to record the memory buffer size from malloc routine.
@@ -41,7 +42,7 @@ malloc (
   //
   NewSize = (UINTN)(size) + CRYPTMEM_OVERHEAD;
 
-  Data = AllocatePool (NewSize);
+  Data = AllocatePages (EFI_SIZE_TO_PAGES (NewSize));
   if (Data != NULL) {
     PoolHdr = (CRYPTMEM_HEAD *)Data;
     //
@@ -73,7 +74,7 @@ realloc (
   VOID           *Data;
 
   NewSize = (UINTN)size + CRYPTMEM_OVERHEAD;
-  Data    = AllocatePool (NewSize);
+  Data    = AllocatePages (EFI_SIZE_TO_PAGES (NewSize));
   if (Data != NULL) {
     NewPoolHdr            = (CRYPTMEM_HEAD *)Data;
     NewPoolHdr->Signature = CRYPTMEM_HEAD_SIGNATURE;
@@ -90,7 +91,7 @@ realloc (
       // Duplicate the buffer content.
       //
       CopyMem ((VOID *)(NewPoolHdr + 1), ptr, MIN (OldSize, size));
-      FreePool ((VOID *)OldPoolHdr);
+      FreePages (((VOID *)OldPoolHdr), EFI_SIZE_TO_PAGES (OldSize));
     }
 
     return (VOID *)(NewPoolHdr + 1);
@@ -117,6 +118,6 @@ free (
   if (ptr != NULL) {
     PoolHdr = (CRYPTMEM_HEAD *)ptr - 1;
     ASSERT (PoolHdr->Signature == CRYPTMEM_HEAD_SIGNATURE);
-    FreePool (PoolHdr);
+    FreePages (((VOID *)PoolHdr), EFI_SIZE_TO_PAGES (PoolHdr->Size));
   }
 }
-- 
2.35.1.windows.2



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#91385): https://edk2.groups.io/g/devel/message/91385
Mute This Topic: https://groups.io/mt/92389738/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-




More information about the edk2-devel-archive mailing list