]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/GenFw: Avoid possible NULL pointer dereference
authorHao Wu <hao.a.wu@intel.com>
Tue, 11 Oct 2016 02:20:14 +0000 (10:20 +0800)
committerHao Wu <hao.a.wu@intel.com>
Tue, 8 Nov 2016 08:36:21 +0000 (16:36 +0800)
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/C/GenFw/Elf32Convert.c
BaseTools/Source/C/GenFw/Elf64Convert.c
BaseTools/Source/C/GenFw/ElfConvert.c
BaseTools/Source/C/GenFw/GenFw.c

index 8fca7fba2a255852c0b20367f42c9dbd7ca4dedc..f420bc89da44e1d943a45c5fd8d15ca9bd98181b 100644 (file)
@@ -167,6 +167,10 @@ InitializeElf32 (
   // Create COFF Section offset buffer and zero.\r
   //\r
   mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32));\r
+  if (mCoffSectionsOffset == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return FALSE;\r
+  }\r
   memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32));\r
 \r
   //\r
@@ -526,6 +530,10 @@ ScanSections32 (
   // Allocate base Coff file.  Will be expanded later for relocations.\r
   //\r
   mCoffFile = (UINT8 *)malloc(mCoffOffset);\r
+  if (mCoffFile == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+  }\r
+  assert (mCoffFile != NULL);\r
   memset(mCoffFile, 0, mCoffOffset);\r
 \r
   //\r
index 9b409b615e4085c5f7785d70865f0489e104b299..acf021667eab494ccf2def0d2a811a6198e25e07 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Elf64 convert solution\r
 \r
-Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
 Portions copyright (c) 2013-2014, ARM Ltd. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials are licensed and made available\r
@@ -172,6 +172,10 @@ InitializeElf64 (
   //\r
   VerboseMsg ("Create COFF Section Offset Buffer");\r
   mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32));\r
+  if (mCoffSectionsOffset == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return FALSE;\r
+  }\r
   memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32));\r
 \r
   //\r
@@ -518,6 +522,10 @@ ScanSections64 (
   // Allocate base Coff file.  Will be expanded later for relocations.\r
   //\r
   mCoffFile = (UINT8 *)malloc(mCoffOffset);\r
+  if (mCoffFile == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+  }\r
+  assert (mCoffFile != NULL);\r
   memset(mCoffFile, 0, mCoffOffset);\r
 \r
   //\r
index 6211389ba1695458ec9e82faee470e1108910b68..17913ff2dfbe118664ad06a0971143923da42090 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Elf convert solution\r
 \r
-Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials are licensed and made available \r
 under the terms and conditions of the BSD License which accompanies this \r
@@ -24,6 +24,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <string.h>\r
 #include <time.h>\r
 #include <ctype.h>\r
+#include <assert.h>\r
 \r
 #include <Common/UefiBaseTypes.h>\r
 #include <IndustryStandard/PeImage.h>\r
@@ -98,6 +99,10 @@ CoffAddFixup(
       mCoffFile,\r
       mCoffOffset + sizeof(EFI_IMAGE_BASE_RELOCATION) + 2 * MAX_COFF_ALIGNMENT\r
       );\r
+    if (mCoffFile == NULL) {\r
+      Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    }\r
+    assert (mCoffFile != NULL);\r
     memset (\r
       mCoffFile + mCoffOffset, 0,\r
       sizeof(EFI_IMAGE_BASE_RELOCATION) + 2 * MAX_COFF_ALIGNMENT\r
index b62756a49212c0b8bf2e6b02d80b01114164c391..9ffc4c5b0155800e3e0a927c25f2fc93cdad0f51 100644 (file)
@@ -625,6 +625,10 @@ PeCoffConvertImageToXip (
   // Allocate the extra space that we need to grow the image\r
   //\r
   XipFile = malloc (XipLength);\r
+  if (XipFile == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return;\r
+  }\r
   memset (XipFile, 0, XipLength);\r
 \r
   //\r
@@ -701,6 +705,10 @@ Returns:
                           + 3 * (sizeof (UINT16) + 3 * sizeof (CHAR16)) \r
                           + sizeof (EFI_IMAGE_RESOURCE_DATA_ENTRY);\r
   HiiSectionHeader = malloc (HiiSectionHeaderSize);\r
+  if (HiiSectionHeader == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return NULL;\r
+  }\r
   memset (HiiSectionHeader, 0, HiiSectionHeaderSize);\r
 \r
   HiiSectionOffset = 0;\r
@@ -1693,6 +1701,10 @@ Returns:
       // Create the resource section header\r
       //\r
       HiiSectionHeader = CreateHiiResouceSectionHeader (&HiiSectionHeaderSize, HiiPackageListHeader.PackageLength);\r
+      if (HiiSectionHeader == NULL) {\r
+        free (HiiPackageListBuffer);\r
+        goto Finish;\r
+      }\r
       //\r
       // Wrtie section header and HiiData into File.\r
       //\r
@@ -3028,8 +3040,10 @@ Returns:
   }\r
 \r
   ptime = localtime (&newtime);\r
-  DebugMsg (NULL, 0, 9, "New Image Time Stamp", "%04d-%02d-%02d %02d:%02d:%02d",\r
-            ptime->tm_year + 1900, ptime->tm_mon + 1, ptime->tm_mday, ptime->tm_hour, ptime->tm_min, ptime->tm_sec);\r
+  if (ptime != NULL) {\r
+    DebugMsg (NULL, 0, 9, "New Image Time Stamp", "%04d-%02d-%02d %02d:%02d:%02d",\r
+              ptime->tm_year + 1900, ptime->tm_mon + 1, ptime->tm_mday, ptime->tm_hour, ptime->tm_min, ptime->tm_sec);\r
+  }\r
   //\r
   // Set new time and data into PeImage.\r
   //\r