]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: Fix misuses of AllocateCopyPool
authorJian J Wang <jian.j.wang@intel.com>
Wed, 8 Nov 2017 02:09:19 +0000 (10:09 +0800)
committerStar Zeng <star.zeng@intel.com>
Wed, 8 Nov 2017 09:13:04 +0000 (17:13 +0800)
AllocateCopyPool(AllocationSize, *Buffer) will copy "AllocationSize" bytes of
memory from old "Buffer" to new allocated one. If "AllocationSize" is bigger
than size of "Buffer", heap memory overflow occurs during copy.

One solution is to allocate pool first then copy the necessary bytes to new
memory. Another is using ReallocatePool instead if old buffer will be freed
on spot.

Cc: Jaben Carsey <jaben.carsey@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Bi Dandan <dandan.bi@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jian J Wang <jian.j.wang@intel.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
ShellPkg/Application/Shell/Shell.c
ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c

index 5471930ba19140c42ec1ec9877a83e6c53c30fbe..656206fdce2ade4c69d0258eafad126e96728159 100644 (file)
@@ -1646,7 +1646,7 @@ ShellConvertVariables (
   //\r
   // now do the replacements...\r
   //\r
-  NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine);\r
+  NewCommandLine1 = AllocateZeroPool (NewSize);\r
   NewCommandLine2 = AllocateZeroPool(NewSize);\r
   ItemTemp        = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));\r
   if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {\r
@@ -1655,6 +1655,8 @@ ShellConvertVariables (
     SHELL_FREE_NON_NULL(ItemTemp);\r
     return (NULL);\r
   }\r
+  CopyMem (NewCommandLine1, OriginalCommandLine, StrSize (OriginalCommandLine));\r
+\r
   for (MasterEnvList = EfiShellGetEnv(NULL)\r
     ;  MasterEnvList != NULL && *MasterEnvList != CHAR_NULL\r
     ;  MasterEnvList += StrLen(MasterEnvList) + 1\r
index 1122c89b8bfe5eec4a16306855b564b200481a8f..ee3db6335871456d29bd36e38446016fea4297b6 100644 (file)
@@ -143,10 +143,11 @@ UpdateOptionalData(
     OriginalOptionDataSize += (*(UINT16*)(OriginalData + sizeof(UINT32)));\r
     OriginalOptionDataSize -= OriginalSize;\r
     NewSize = OriginalSize - OriginalOptionDataSize + DataSize;\r
-    NewData = AllocateCopyPool(NewSize, OriginalData);\r
+    NewData = AllocatePool(NewSize);\r
     if (NewData == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
     } else {\r
+      CopyMem (NewData, OriginalData, OriginalSize - OriginalOptionDataSize);\r
       CopyMem(NewData + OriginalSize - OriginalOptionDataSize, Data, DataSize);\r
     }\r
   }\r
@@ -1120,11 +1121,13 @@ BcfgAddOpt(
         // Now we know how many EFI_INPUT_KEY structs we need to attach to the end of the EFI_KEY_OPTION struct.  \r
         // Re-allocate with the added information.\r
         //\r
-        KeyOptionBuffer = AllocateCopyPool(sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount), &NewKeyOption);\r
+        KeyOptionBuffer = AllocatePool (sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount));\r
         if (KeyOptionBuffer == NULL) {\r
           ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellBcfgHiiHandle, L"bcfg");  \r
           ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+          return ShellStatus;\r
         }\r
+        CopyMem (KeyOptionBuffer, &NewKeyOption, sizeof(EFI_KEY_OPTION));\r
       }\r
       for (LoopCounter = 0 ; ShellStatus == SHELL_SUCCESS && LoopCounter < NewKeyOption.KeyData.Options.InputKeyCount; LoopCounter++) {\r
         //\r