* [PATCH 0/3] Fix misuses of AllocateCopyPool @ 2017-11-03 4:57 Jian J Wang 2017-11-03 4:57 ` [PATCH 1/3] MdeModulePkg: " Jian J Wang ` (2 more replies) 0 siblings, 3 replies; 10+ messages in thread From: Jian J Wang @ 2017-11-03 4:57 UTC (permalink / raw) To: edk2-devel 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. The solution is to allocate pool first then copy the necessary bytes to new memory. This can avoid copying extra bytes from unknown memory range. Jian J Wang (3): MdeModulePkg: Fix misuses of AllocateCopyPool ShellPkg: Fix misuses of AllocateCopyPool IntelFrameworkModulePkg: Fix misuses of AllocateCopyPool .../Universal/BdsDxe/DeviceMngr/DeviceManager.c | 4 +++- MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c | 3 ++- .../BootMaintenanceManagerCustomizedUiSupport.c | 3 ++- MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c | 4 +++- MdeModulePkg/Library/UefiHiiLib/HiiLib.c | 10 ++++++---- .../Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c | 3 ++- MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c | 3 ++- ShellPkg/Application/Shell/Shell.c | 4 +++- .../Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c | 6 ++++-- 9 files changed, 27 insertions(+), 13 deletions(-) -- 2.14.1.windows.1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 1/3] MdeModulePkg: Fix misuses of AllocateCopyPool 2017-11-03 4:57 [PATCH 0/3] Fix misuses of AllocateCopyPool Jian J Wang @ 2017-11-03 4:57 ` Jian J Wang 2017-11-03 9:14 ` Zeng, Star 2017-11-03 4:57 ` [PATCH 2/3] ShellPkg: " Jian J Wang 2017-11-03 4:57 ` [PATCH 3/3] IntelFrameworkModulePkg: " Jian J Wang 2 siblings, 1 reply; 10+ messages in thread From: Jian J Wang @ 2017-11-03 4:57 UTC (permalink / raw) To: edk2-devel; +Cc: Star Zeng, Eric Dong, Bi Dandan 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. The solution is to allocate pool first then copy the necessary bytes to new memory. This can avoid copying extra bytes from unknown memory range. Cc: Star Zeng <star.zeng@intel.com> Cc: Eric Dong <eric.dong@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> --- MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c | 3 ++- .../BootMaintenanceManagerCustomizedUiSupport.c | 3 ++- MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c | 4 +++- MdeModulePkg/Library/UefiHiiLib/HiiLib.c | 10 ++++++---- .../Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c | 3 ++- MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c | 3 ++- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c b/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c index 1505ef9319..74d890441b 100644 --- a/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c +++ b/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c @@ -639,8 +639,9 @@ UiListThirdPartyDrivers ( Count++; if (Count >= CurrentSize) { - DriverListPtr = AllocateCopyPool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE), gHiiDriverList); + DriverListPtr = AllocatePool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE)); ASSERT (DriverListPtr != NULL); + CopyMem (DriverListPtr, gHiiDriverList, CurrentSize * sizeof (UI_HII_DRIVER_INSTANCE)); FreePool (gHiiDriverList); gHiiDriverList = DriverListPtr; CurrentSize += UI_HII_DRIVER_LIST_SIZE; diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c index b25bc67c06..512c340741 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c @@ -435,8 +435,9 @@ BmmListThirdPartyDrivers ( Count++; if (Count >= CurrentSize) { - DriverListPtr = AllocateCopyPool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE), gHiiDriverList); + DriverListPtr = AllocatePool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE)); ASSERT (DriverListPtr != NULL); + CopyMem (DriverListPtr, gHiiDriverList, CurrentSize * sizeof (UI_HII_DRIVER_INSTANCE)); FreePool (gHiiDriverList); gHiiDriverList = DriverListPtr; CurrentSize += UI_HII_DRIVER_LIST_SIZE; diff --git a/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c b/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c index 23ae6c5392..8be3e53edf 100644 --- a/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c +++ b/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c @@ -240,7 +240,9 @@ AddIdToMacDeviceList ( } else { mMacDeviceList.MaxListLen += MAX_MAC_ADDRESS_NODE_LIST_LEN; if (mMacDeviceList.CurListLen != 0) { - TempDeviceList = (MENU_INFO_ITEM *)AllocateCopyPool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen, (VOID *)mMacDeviceList.NodeList); + TempDeviceList = (MENU_INFO_ITEM *)AllocatePool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen); + ASSERT (TempDeviceList != NULL); + CopyMem (TempDeviceList, mMacDeviceList.NodeList, sizeof (MENU_INFO_ITEM) * mMacDeviceList.CurListLen); } else { TempDeviceList = (MENU_INFO_ITEM *)AllocatePool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen); } diff --git a/MdeModulePkg/Library/UefiHiiLib/HiiLib.c b/MdeModulePkg/Library/UefiHiiLib/HiiLib.c index ce894c08b5..d88614fea5 100644 --- a/MdeModulePkg/Library/UefiHiiLib/HiiLib.c +++ b/MdeModulePkg/Library/UefiHiiLib/HiiLib.c @@ -464,20 +464,22 @@ HiiGetFormSetFromHiiHandle( } if (FormSetBuffer != NULL){ - TempBuffer = AllocateCopyPool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length, FormSetBuffer); - FreePool(FormSetBuffer); - FormSetBuffer = NULL; + TempBuffer = AllocatePool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length); if (TempBuffer == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Done; } + CopyMem (TempBuffer, FormSetBuffer, TempSize); CopyMem (TempBuffer + TempSize, OpCodeData, ((EFI_IFR_OP_HEADER *) OpCodeData)->Length); + FreePool(FormSetBuffer); + FormSetBuffer = NULL; } else { - TempBuffer = AllocateCopyPool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length, OpCodeData); + TempBuffer = AllocatePool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length); if (TempBuffer == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Done; } + CopyMem (TempBuffer, OpCodeData, ((EFI_IFR_OP_HEADER *) OpCodeData)->Length); } TempSize += ((EFI_IFR_OP_HEADER *) OpCodeData)->Length; FormSetBuffer = TempBuffer; diff --git a/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c b/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c index b81110ff98..5bd4c246bc 100644 --- a/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c +++ b/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c @@ -562,7 +562,8 @@ FvSimpleFileSystemOpen ( // No, there was no extension. So add one and search again for the file // NewFileNameLength = FileNameLength + 1 + 4 = (Number of non-null character) + (file extension) + (a null character) NewFileNameLength = FileNameLength + 1 + 4; - FileNameWithExtension = AllocateCopyPool (NewFileNameLength * 2, FileName); + FileNameWithExtension = AllocatePool (NewFileNameLength * 2); + StrCpyS (FileNameWithExtension, FileNameLength, FileName); StrCatS (FileNameWithExtension, NewFileNameLength, L".EFI"); for (FvFileInfoLink = GetFirstNode (&Instance->FileInfoHead); diff --git a/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c b/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c index 1b48c1cebe..47173b9c17 100644 --- a/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c +++ b/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c @@ -2543,10 +2543,11 @@ MergeToMultiKeywordResp ( MultiKeywordRespLen = (StrLen (*MultiKeywordResp) + 1 + StrLen (*KeywordResp) + 1) * sizeof (CHAR16); - StringPtr = AllocateCopyPool (MultiKeywordRespLen, *MultiKeywordResp); + StringPtr = AllocatePool (MultiKeywordRespLen); if (StringPtr == NULL) { return EFI_OUT_OF_RESOURCES; } + CopyMem (StringPtr, *MultiKeywordResp, StrSize (*MultiKeywordResp)); FreePool (*MultiKeywordResp); *MultiKeywordResp = StringPtr; -- 2.14.1.windows.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH 1/3] MdeModulePkg: Fix misuses of AllocateCopyPool 2017-11-03 4:57 ` [PATCH 1/3] MdeModulePkg: " Jian J Wang @ 2017-11-03 9:14 ` Zeng, Star 2017-11-06 0:49 ` Wang, Jian J 0 siblings, 1 reply; 10+ messages in thread From: Zeng, Star @ 2017-11-03 9:14 UTC (permalink / raw) To: Wang, Jian J, edk2-devel@lists.01.org; +Cc: Dong, Eric, Bi, Dandan, Zeng, Star Some of the change can reuse ReallocatePool() to make the code more simpler. For example, the change in FrontPageCustomizedUiSupport.c. Count++; if (Count >= CurrentSize) { DriverListPtr = AllocatePool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE)); ASSERT (DriverListPtr != NULL); CopyMem (DriverListPtr, gHiiDriverList, CurrentSize * sizeof (UI_HII_DRIVER_INSTANCE)); FreePool (gHiiDriverList); gHiiDriverList = DriverListPtr; CurrentSize += UI_HII_DRIVER_LIST_SIZE; Could be Count++; if (Count >= CurrentSize) { gHiiDriverList = ReallocatePool ( CurrentSize * sizeof (UI_HII_DRIVER_INSTANCE), (CurrentSize + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE), gHiiDriverList ); ASSERT (gHiiDriverList != NULL); CurrentSize += UI_HII_DRIVER_LIST_SIZE; Thanks, Star -----Original Message----- From: Wang, Jian J Sent: Friday, November 3, 2017 12:58 PM To: edk2-devel@lists.01.org Cc: Zeng, Star <star.zeng@intel.com>; Dong, Eric <eric.dong@intel.com>; Bi, Dandan <dandan.bi@intel.com> Subject: [PATCH 1/3] MdeModulePkg: Fix misuses of AllocateCopyPool 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. The solution is to allocate pool first then copy the necessary bytes to new memory. This can avoid copying extra bytes from unknown memory range. Cc: Star Zeng <star.zeng@intel.com> Cc: Eric Dong <eric.dong@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> --- MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c | 3 ++- .../BootMaintenanceManagerCustomizedUiSupport.c | 3 ++- MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c | 4 +++- MdeModulePkg/Library/UefiHiiLib/HiiLib.c | 10 ++++++---- .../Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c | 3 ++- MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c | 3 ++- 6 files changed, 17 insertions(+), 9 deletions(-) diff --git a/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c b/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c index 1505ef9319..74d890441b 100644 --- a/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c +++ b/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c @@ -639,8 +639,9 @@ UiListThirdPartyDrivers ( Count++; if (Count >= CurrentSize) { - DriverListPtr = AllocateCopyPool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE), gHiiDriverList); + DriverListPtr = AllocatePool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE)); ASSERT (DriverListPtr != NULL); + CopyMem (DriverListPtr, gHiiDriverList, CurrentSize * sizeof (UI_HII_DRIVER_INSTANCE)); FreePool (gHiiDriverList); gHiiDriverList = DriverListPtr; CurrentSize += UI_HII_DRIVER_LIST_SIZE; diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c index b25bc67c06..512c340741 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c @@ -435,8 +435,9 @@ BmmListThirdPartyDrivers ( Count++; if (Count >= CurrentSize) { - DriverListPtr = AllocateCopyPool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE), gHiiDriverList); + DriverListPtr = AllocatePool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof (UI_HII_DRIVER_INSTANCE)); ASSERT (DriverListPtr != NULL); + CopyMem (DriverListPtr, gHiiDriverList, CurrentSize * sizeof (UI_HII_DRIVER_INSTANCE)); FreePool (gHiiDriverList); gHiiDriverList = DriverListPtr; CurrentSize += UI_HII_DRIVER_LIST_SIZE; diff --git a/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c b/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c index 23ae6c5392..8be3e53edf 100644 --- a/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c +++ b/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c @@ -240,7 +240,9 @@ AddIdToMacDeviceList ( } else { mMacDeviceList.MaxListLen += MAX_MAC_ADDRESS_NODE_LIST_LEN; if (mMacDeviceList.CurListLen != 0) { - TempDeviceList = (MENU_INFO_ITEM *)AllocateCopyPool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen, (VOID *)mMacDeviceList.NodeList); + TempDeviceList = (MENU_INFO_ITEM *)AllocatePool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen); + ASSERT (TempDeviceList != NULL); + CopyMem (TempDeviceList, mMacDeviceList.NodeList, sizeof (MENU_INFO_ITEM) * mMacDeviceList.CurListLen); } else { TempDeviceList = (MENU_INFO_ITEM *)AllocatePool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen); } diff --git a/MdeModulePkg/Library/UefiHiiLib/HiiLib.c b/MdeModulePkg/Library/UefiHiiLib/HiiLib.c index ce894c08b5..d88614fea5 100644 --- a/MdeModulePkg/Library/UefiHiiLib/HiiLib.c +++ b/MdeModulePkg/Library/UefiHiiLib/HiiLib.c @@ -464,20 +464,22 @@ HiiGetFormSetFromHiiHandle( } if (FormSetBuffer != NULL){ - TempBuffer = AllocateCopyPool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length, FormSetBuffer); - FreePool(FormSetBuffer); - FormSetBuffer = NULL; + TempBuffer = AllocatePool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length); if (TempBuffer == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Done; } + CopyMem (TempBuffer, FormSetBuffer, TempSize); CopyMem (TempBuffer + TempSize, OpCodeData, ((EFI_IFR_OP_HEADER *) OpCodeData)->Length); + FreePool(FormSetBuffer); + FormSetBuffer = NULL; } else { - TempBuffer = AllocateCopyPool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length, OpCodeData); + TempBuffer = AllocatePool (TempSize + ((EFI_IFR_OP_HEADER *) OpCodeData)->Length); if (TempBuffer == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Done; } + CopyMem (TempBuffer, OpCodeData, ((EFI_IFR_OP_HEADER *) OpCodeData)->Length); } TempSize += ((EFI_IFR_OP_HEADER *) OpCodeData)->Length; FormSetBuffer = TempBuffer; diff --git a/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c b/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c index b81110ff98..5bd4c246bc 100644 --- a/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c +++ b/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c @@ -562,7 +562,8 @@ FvSimpleFileSystemOpen ( // No, there was no extension. So add one and search again for the file // NewFileNameLength = FileNameLength + 1 + 4 = (Number of non-null character) + (file extension) + (a null character) NewFileNameLength = FileNameLength + 1 + 4; - FileNameWithExtension = AllocateCopyPool (NewFileNameLength * 2, FileName); + FileNameWithExtension = AllocatePool (NewFileNameLength * 2); + StrCpyS (FileNameWithExtension, FileNameLength, FileName); StrCatS (FileNameWithExtension, NewFileNameLength, L".EFI"); for (FvFileInfoLink = GetFirstNode (&Instance->FileInfoHead); diff --git a/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c b/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c index 1b48c1cebe..47173b9c17 100644 --- a/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c +++ b/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c @@ -2543,10 +2543,11 @@ MergeToMultiKeywordResp ( MultiKeywordRespLen = (StrLen (*MultiKeywordResp) + 1 + StrLen (*KeywordResp) + 1) * sizeof (CHAR16); - StringPtr = AllocateCopyPool (MultiKeywordRespLen, *MultiKeywordResp); + StringPtr = AllocatePool (MultiKeywordRespLen); if (StringPtr == NULL) { return EFI_OUT_OF_RESOURCES; } + CopyMem (StringPtr, *MultiKeywordResp, StrSize (*MultiKeywordResp)); FreePool (*MultiKeywordResp); *MultiKeywordResp = StringPtr; -- 2.14.1.windows.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH 1/3] MdeModulePkg: Fix misuses of AllocateCopyPool 2017-11-03 9:14 ` Zeng, Star @ 2017-11-06 0:49 ` Wang, Jian J 0 siblings, 0 replies; 10+ messages in thread From: Wang, Jian J @ 2017-11-06 0:49 UTC (permalink / raw) To: Zeng, Star, edk2-devel@lists.01.org; +Cc: Dong, Eric, Bi, Dandan Star, Thanks for the comments. It's a good suggestion. I didn't know ReallocatePool() can do that. Thanks Jian > -----Original Message----- > From: Zeng, Star > Sent: Friday, November 03, 2017 5:14 PM > To: Wang, Jian J <jian.j.wang@intel.com>; edk2-devel@lists.01.org > Cc: Dong, Eric <eric.dong@intel.com>; Bi, Dandan <dandan.bi@intel.com>; > Zeng, Star <star.zeng@intel.com> > Subject: RE: [PATCH 1/3] MdeModulePkg: Fix misuses of AllocateCopyPool > > Some of the change can reuse ReallocatePool() to make the code more simpler. > > For example, the change in FrontPageCustomizedUiSupport.c. > > Count++; > if (Count >= CurrentSize) { > DriverListPtr = AllocatePool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof > (UI_HII_DRIVER_INSTANCE)); > ASSERT (DriverListPtr != NULL); > CopyMem (DriverListPtr, gHiiDriverList, CurrentSize * sizeof > (UI_HII_DRIVER_INSTANCE)); > FreePool (gHiiDriverList); > gHiiDriverList = DriverListPtr; > CurrentSize += UI_HII_DRIVER_LIST_SIZE; > > Could be > > Count++; > if (Count >= CurrentSize) { > gHiiDriverList = ReallocatePool ( > CurrentSize * sizeof (UI_HII_DRIVER_INSTANCE), > (CurrentSize + UI_HII_DRIVER_LIST_SIZE) * sizeof > (UI_HII_DRIVER_INSTANCE), > gHiiDriverList > ); > ASSERT (gHiiDriverList != NULL); > CurrentSize += UI_HII_DRIVER_LIST_SIZE; > > Thanks, > Star > -----Original Message----- > From: Wang, Jian J > Sent: Friday, November 3, 2017 12:58 PM > To: edk2-devel@lists.01.org > Cc: Zeng, Star <star.zeng@intel.com>; Dong, Eric <eric.dong@intel.com>; Bi, > Dandan <dandan.bi@intel.com> > Subject: [PATCH 1/3] MdeModulePkg: Fix misuses of AllocateCopyPool > > 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. > > The solution is to allocate pool first then copy the necessary bytes to new > memory. This can avoid copying extra bytes from unknown memory range. > > Cc: Star Zeng <star.zeng@intel.com> > Cc: Eric Dong <eric.dong@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> > --- > MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c | 3 ++- > .../BootMaintenanceManagerCustomizedUiSupport.c | 3 ++- > MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c | 4 +++- > MdeModulePkg/Library/UefiHiiLib/HiiLib.c | 10 ++++++---- > .../Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c | 3 ++- > MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c | 3 ++- > 6 files changed, 17 insertions(+), 9 deletions(-) > > diff --git a/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c > b/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c > index 1505ef9319..74d890441b 100644 > --- a/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c > +++ b/MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c > @@ -639,8 +639,9 @@ UiListThirdPartyDrivers ( > > Count++; > if (Count >= CurrentSize) { > - DriverListPtr = AllocateCopyPool ((Count + UI_HII_DRIVER_LIST_SIZE) * > sizeof (UI_HII_DRIVER_INSTANCE), gHiiDriverList); > + DriverListPtr = AllocatePool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof > (UI_HII_DRIVER_INSTANCE)); > ASSERT (DriverListPtr != NULL); > + CopyMem (DriverListPtr, gHiiDriverList, CurrentSize * sizeof > (UI_HII_DRIVER_INSTANCE)); > FreePool (gHiiDriverList); > gHiiDriverList = DriverListPtr; > CurrentSize += UI_HII_DRIVER_LIST_SIZE; > diff --git > a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceM > anagerCustomizedUiSupport.c > b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceM > anagerCustomizedUiSupport.c > index b25bc67c06..512c340741 100644 > --- > a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceM > anagerCustomizedUiSupport.c > +++ > b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceM > anagerCustomizedUiSupport.c > @@ -435,8 +435,9 @@ BmmListThirdPartyDrivers ( > > Count++; > if (Count >= CurrentSize) { > - DriverListPtr = AllocateCopyPool ((Count + UI_HII_DRIVER_LIST_SIZE) * > sizeof (UI_HII_DRIVER_INSTANCE), gHiiDriverList); > + DriverListPtr = AllocatePool ((Count + UI_HII_DRIVER_LIST_SIZE) * sizeof > (UI_HII_DRIVER_INSTANCE)); > ASSERT (DriverListPtr != NULL); > + CopyMem (DriverListPtr, gHiiDriverList, CurrentSize * sizeof > (UI_HII_DRIVER_INSTANCE)); > FreePool (gHiiDriverList); > gHiiDriverList = DriverListPtr; > CurrentSize += UI_HII_DRIVER_LIST_SIZE; > diff --git a/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c > b/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c > index 23ae6c5392..8be3e53edf 100644 > --- a/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c > +++ b/MdeModulePkg/Library/DeviceManagerUiLib/DeviceManager.c > @@ -240,7 +240,9 @@ AddIdToMacDeviceList ( > } else { > mMacDeviceList.MaxListLen += MAX_MAC_ADDRESS_NODE_LIST_LEN; > if (mMacDeviceList.CurListLen != 0) { > - TempDeviceList = (MENU_INFO_ITEM *)AllocateCopyPool (sizeof > (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen, (VOID > *)mMacDeviceList.NodeList); > + TempDeviceList = (MENU_INFO_ITEM *)AllocatePool (sizeof > (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen); > + ASSERT (TempDeviceList != NULL); > + CopyMem (TempDeviceList, mMacDeviceList.NodeList, sizeof > (MENU_INFO_ITEM) * mMacDeviceList.CurListLen); > } else { > TempDeviceList = (MENU_INFO_ITEM *)AllocatePool (sizeof > (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen); > } > diff --git a/MdeModulePkg/Library/UefiHiiLib/HiiLib.c > b/MdeModulePkg/Library/UefiHiiLib/HiiLib.c > index ce894c08b5..d88614fea5 100644 > --- a/MdeModulePkg/Library/UefiHiiLib/HiiLib.c > +++ b/MdeModulePkg/Library/UefiHiiLib/HiiLib.c > @@ -464,20 +464,22 @@ HiiGetFormSetFromHiiHandle( > } > > if (FormSetBuffer != NULL){ > - TempBuffer = AllocateCopyPool (TempSize + ((EFI_IFR_OP_HEADER *) > OpCodeData)->Length, FormSetBuffer); > - FreePool(FormSetBuffer); > - FormSetBuffer = NULL; > + TempBuffer = AllocatePool (TempSize + ((EFI_IFR_OP_HEADER *) > OpCodeData)->Length); > if (TempBuffer == NULL) { > Status = EFI_OUT_OF_RESOURCES; > goto Done; > } > + CopyMem (TempBuffer, FormSetBuffer, TempSize); > CopyMem (TempBuffer + TempSize, OpCodeData, ((EFI_IFR_OP_HEADER *) > OpCodeData)->Length); > + FreePool(FormSetBuffer); > + FormSetBuffer = NULL; > } else { > - TempBuffer = AllocateCopyPool (TempSize + ((EFI_IFR_OP_HEADER *) > OpCodeData)->Length, OpCodeData); > + TempBuffer = AllocatePool (TempSize + ((EFI_IFR_OP_HEADER *) > OpCodeData)->Length); > if (TempBuffer == NULL) { > Status = EFI_OUT_OF_RESOURCES; > goto Done; > } > + CopyMem (TempBuffer, OpCodeData, ((EFI_IFR_OP_HEADER *) > OpCodeData)->Length); > } > TempSize += ((EFI_IFR_OP_HEADER *) OpCodeData)->Length; > FormSetBuffer = TempBuffer; > diff --git > a/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c > b/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c > index b81110ff98..5bd4c246bc 100644 > --- a/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c > +++ b/MdeModulePkg/Universal/FvSimpleFileSystemDxe/FvSimpleFileSystem.c > @@ -562,7 +562,8 @@ FvSimpleFileSystemOpen ( > // No, there was no extension. So add one and search again for the file > // NewFileNameLength = FileNameLength + 1 + 4 = (Number of non-null > character) + (file extension) + (a null character) > NewFileNameLength = FileNameLength + 1 + 4; > - FileNameWithExtension = AllocateCopyPool (NewFileNameLength * 2, > FileName); > + FileNameWithExtension = AllocatePool (NewFileNameLength * 2); > + StrCpyS (FileNameWithExtension, FileNameLength, FileName); > StrCatS (FileNameWithExtension, NewFileNameLength, L".EFI"); > > for (FvFileInfoLink = GetFirstNode (&Instance->FileInfoHead); > diff --git a/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c > b/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c > index 1b48c1cebe..47173b9c17 100644 > --- a/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c > +++ b/MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c > @@ -2543,10 +2543,11 @@ MergeToMultiKeywordResp ( > > MultiKeywordRespLen = (StrLen (*MultiKeywordResp) + 1 + StrLen > (*KeywordResp) + 1) * sizeof (CHAR16); > > - StringPtr = AllocateCopyPool (MultiKeywordRespLen, *MultiKeywordResp); > + StringPtr = AllocatePool (MultiKeywordRespLen); > if (StringPtr == NULL) { > return EFI_OUT_OF_RESOURCES; > } > + CopyMem (StringPtr, *MultiKeywordResp, StrSize (*MultiKeywordResp)); > > FreePool (*MultiKeywordResp); > *MultiKeywordResp = StringPtr; > -- > 2.14.1.windows.1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool 2017-11-03 4:57 [PATCH 0/3] Fix misuses of AllocateCopyPool Jian J Wang 2017-11-03 4:57 ` [PATCH 1/3] MdeModulePkg: " Jian J Wang @ 2017-11-03 4:57 ` Jian J Wang 2017-11-03 8:22 ` Ni, Ruiyu 2017-11-03 4:57 ` [PATCH 3/3] IntelFrameworkModulePkg: " Jian J Wang 2 siblings, 1 reply; 10+ messages in thread From: Jian J Wang @ 2017-11-03 4:57 UTC (permalink / raw) To: edk2-devel; +Cc: Jaben Carsey, Ruiyu Ni, Bi Dandan 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. The solution is to allocate pool first then copy the necessary bytes to new memory. This can avoid copying extra bytes from unknown memory range. 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> --- ShellPkg/Application/Shell/Shell.c | 4 +++- ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index 5471930ba1..24a04ca323 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -1646,7 +1646,9 @@ ShellConvertVariables ( // // now do the replacements... // - NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine); + NewCommandLine1 = AllocatePool(NewSize); + ASSERT (NewCommandLine1 != NULL); + CopyMem (NewCommandLine1, OriginalCommandLine, StrSize (OriginalCommandLine)); NewCommandLine2 = AllocateZeroPool(NewSize); ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16))); if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) { diff --git a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c index 1122c89b8b..5de62219b3 100644 --- a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c +++ b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c @@ -143,10 +143,11 @@ UpdateOptionalData( OriginalOptionDataSize += (*(UINT16*)(OriginalData + sizeof(UINT32))); OriginalOptionDataSize -= OriginalSize; NewSize = OriginalSize - OriginalOptionDataSize + DataSize; - NewData = AllocateCopyPool(NewSize, OriginalData); + NewData = AllocatePool(NewSize); if (NewData == NULL) { Status = EFI_OUT_OF_RESOURCES; } else { + CopyMem (NewData, OriginalData, OriginalSize - OriginalOptionDataSize); CopyMem(NewData + OriginalSize - OriginalOptionDataSize, Data, DataSize); } } @@ -1120,11 +1121,12 @@ BcfgAddOpt( // Now we know how many EFI_INPUT_KEY structs we need to attach to the end of the EFI_KEY_OPTION struct. // Re-allocate with the added information. // - KeyOptionBuffer = AllocateCopyPool(sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount), &NewKeyOption); + KeyOptionBuffer = AllocatePool (sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount)); if (KeyOptionBuffer == NULL) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellBcfgHiiHandle, L"bcfg"); ShellStatus = SHELL_OUT_OF_RESOURCES; } + CopyMem (KeyOptionBuffer, &NewKeyOption, sizeof(EFI_KEY_OPTION)); } for (LoopCounter = 0 ; ShellStatus == SHELL_SUCCESS && LoopCounter < NewKeyOption.KeyData.Options.InputKeyCount; LoopCounter++) { // -- 2.14.1.windows.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool 2017-11-03 4:57 ` [PATCH 2/3] ShellPkg: " Jian J Wang @ 2017-11-03 8:22 ` Ni, Ruiyu 2017-11-03 11:43 ` Jim.Dailey 2017-11-06 0:35 ` Wang, Jian J 0 siblings, 2 replies; 10+ messages in thread From: Ni, Ruiyu @ 2017-11-03 8:22 UTC (permalink / raw) To: Wang, Jian J, edk2-devel@lists.01.org; +Cc: Carsey, Jaben, Bi, Dandan 2 comments below. -----Original Message----- From: Wang, Jian J Sent: Friday, November 3, 2017 12:58 PM To: edk2-devel@lists.01.org Cc: Carsey, Jaben <jaben.carsey@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Bi, Dandan <dandan.bi@intel.com> Subject: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool 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. The solution is to allocate pool first then copy the necessary bytes to new memory. This can avoid copying extra bytes from unknown memory range. 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> --- ShellPkg/Application/Shell/Shell.c | 4 +++- ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index 5471930ba1..24a04ca323 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -1646,7 +1646,9 @@ ShellConvertVariables ( // // now do the replacements... // - NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine); + NewCommandLine1 = AllocatePool(NewSize); + ASSERT (NewCommandLine1 != NULL); [Ray] 1. Please do not use assertion because there is NULL check in the below if-statement. The rule in ShellPkg is avoid using assertion. + CopyMem (NewCommandLine1, OriginalCommandLine, StrSize (OriginalCommandLine)); NewCommandLine2 = AllocateZeroPool(NewSize); ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16))); if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) { diff --git a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c index 1122c89b8b..5de62219b3 100644 --- a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c +++ b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c @@ -143,10 +143,11 @@ UpdateOptionalData( OriginalOptionDataSize += (*(UINT16*)(OriginalData + sizeof(UINT32))); OriginalOptionDataSize -= OriginalSize; NewSize = OriginalSize - OriginalOptionDataSize + DataSize; - NewData = AllocateCopyPool(NewSize, OriginalData); + NewData = AllocatePool(NewSize); if (NewData == NULL) { Status = EFI_OUT_OF_RESOURCES; } else { + CopyMem (NewData, OriginalData, OriginalSize - OriginalOptionDataSize); CopyMem(NewData + OriginalSize - OriginalOptionDataSize, Data, DataSize); } } @@ -1120,11 +1121,12 @@ BcfgAddOpt( // Now we know how many EFI_INPUT_KEY structs we need to attach to the end of the EFI_KEY_OPTION struct. // Re-allocate with the added information. // - KeyOptionBuffer = AllocateCopyPool(sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount), &NewKeyOption); + KeyOptionBuffer = AllocatePool (sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount)); if (KeyOptionBuffer == NULL) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellBcfgHiiHandle, L"bcfg"); ShellStatus = SHELL_OUT_OF_RESOURCES; } [Ray] 2. Should the above NULL check return? + CopyMem (KeyOptionBuffer, &NewKeyOption, sizeof(EFI_KEY_OPTION)); } for (LoopCounter = 0 ; ShellStatus == SHELL_SUCCESS && LoopCounter < NewKeyOption.KeyData.Options.InputKeyCount; LoopCounter++) { // -- 2.14.1.windows.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool 2017-11-03 8:22 ` Ni, Ruiyu @ 2017-11-03 11:43 ` Jim.Dailey 2017-11-06 0:55 ` Wang, Jian J 2017-11-06 0:35 ` Wang, Jian J 1 sibling, 1 reply; 10+ messages in thread From: Jim.Dailey @ 2017-11-03 11:43 UTC (permalink / raw) To: ruiyu.ni, jian.j.wang, edk2-devel; +Cc: jaben.carsey, dandan.bi Isn't ReallocatePool is the correct function to use in these cases? For example: NewCommandLine1 = ReallocatePool(NewSize, StrSize(OriginalCommandLine), OriginalCommandLine; Regards, Jim -----Original Message----- From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ni, Ruiyu Sent: Friday, November 3, 2017 3:23 AM To: Wang, Jian J <jian.j.wang@intel.com>; edk2-devel@lists.01.org Cc: Carsey, Jaben <jaben.carsey@intel.com>; Bi, Dandan <dandan.bi@intel.com> Subject: Re: [edk2] [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool 2 comments below. -----Original Message----- From: Wang, Jian J Sent: Friday, November 3, 2017 12:58 PM To: edk2-devel@lists.01.org Cc: Carsey, Jaben <jaben.carsey@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Bi, Dandan <dandan.bi@intel.com> Subject: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool 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. The solution is to allocate pool first then copy the necessary bytes to new memory. This can avoid copying extra bytes from unknown memory range. 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> --- ShellPkg/Application/Shell/Shell.c | 4 +++- ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index 5471930ba1..24a04ca323 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -1646,7 +1646,9 @@ ShellConvertVariables ( // // now do the replacements... // - NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine); + NewCommandLine1 = AllocatePool(NewSize); + ASSERT (NewCommandLine1 != NULL); [Ray] 1. Please do not use assertion because there is NULL check in the below if-statement. The rule in ShellPkg is avoid using assertion. + CopyMem (NewCommandLine1, OriginalCommandLine, StrSize (OriginalCommandLine)); NewCommandLine2 = AllocateZeroPool(NewSize); ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16))); if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) { diff --git a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c index 1122c89b8b..5de62219b3 100644 --- a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c +++ b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c @@ -143,10 +143,11 @@ UpdateOptionalData( OriginalOptionDataSize += (*(UINT16*)(OriginalData + sizeof(UINT32))); OriginalOptionDataSize -= OriginalSize; NewSize = OriginalSize - OriginalOptionDataSize + DataSize; - NewData = AllocateCopyPool(NewSize, OriginalData); + NewData = AllocatePool(NewSize); if (NewData == NULL) { Status = EFI_OUT_OF_RESOURCES; } else { + CopyMem (NewData, OriginalData, OriginalSize - OriginalOptionDataSize); CopyMem(NewData + OriginalSize - OriginalOptionDataSize, Data, DataSize); } } @@ -1120,11 +1121,12 @@ BcfgAddOpt( // Now we know how many EFI_INPUT_KEY structs we need to attach to the end of the EFI_KEY_OPTION struct. // Re-allocate with the added information. // - KeyOptionBuffer = AllocateCopyPool(sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount), &NewKeyOption); + KeyOptionBuffer = AllocatePool (sizeof(EFI_KEY_OPTION) + (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount)); if (KeyOptionBuffer == NULL) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellBcfgHiiHandle, L"bcfg"); ShellStatus = SHELL_OUT_OF_RESOURCES; } [Ray] 2. Should the above NULL check return? + CopyMem (KeyOptionBuffer, &NewKeyOption, sizeof(EFI_KEY_OPTION)); } for (LoopCounter = 0 ; ShellStatus == SHELL_SUCCESS && LoopCounter < NewKeyOption.KeyData.Options.InputKeyCount; LoopCounter++) { // -- 2.14.1.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel ^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool 2017-11-03 11:43 ` Jim.Dailey @ 2017-11-06 0:55 ` Wang, Jian J 0 siblings, 0 replies; 10+ messages in thread From: Wang, Jian J @ 2017-11-06 0:55 UTC (permalink / raw) To: Jim.Dailey@dell.com, Ni, Ruiyu, edk2-devel@lists.01.org Cc: Carsey, Jaben, Bi, Dandan Jim, RellocatePool() will free old buffer but AllocateCopyPool() will not. So not all cases in which they can be replaced for each other. Thanks, Jian > -----Original Message----- > From: Jim.Dailey@dell.com [mailto:Jim.Dailey@dell.com] > Sent: Friday, November 03, 2017 7:44 PM > To: Ni, Ruiyu <ruiyu.ni@intel.com>; Wang, Jian J <jian.j.wang@intel.com>; > edk2-devel@lists.01.org > Cc: Carsey, Jaben <jaben.carsey@intel.com>; Bi, Dandan <dandan.bi@intel.com> > Subject: RE: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool > > Isn't ReallocatePool is the correct function to use in these cases? > For example: > > NewCommandLine1 = ReallocatePool(NewSize, StrSize(OriginalCommandLine), > OriginalCommandLine; > > Regards, > Jim > > -----Original Message----- > From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Ni, > Ruiyu > Sent: Friday, November 3, 2017 3:23 AM > To: Wang, Jian J <jian.j.wang@intel.com>; edk2-devel@lists.01.org > Cc: Carsey, Jaben <jaben.carsey@intel.com>; Bi, Dandan <dandan.bi@intel.com> > Subject: Re: [edk2] [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool > > 2 comments below. > > -----Original Message----- > From: Wang, Jian J > Sent: Friday, November 3, 2017 12:58 PM > To: edk2-devel@lists.01.org > Cc: Carsey, Jaben <jaben.carsey@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Bi, > Dandan <dandan.bi@intel.com> > Subject: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool > > 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. > > The solution is to allocate pool first then copy the necessary bytes to new > memory. This can avoid copying extra bytes from unknown memory range. > > 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> > --- > ShellPkg/Application/Shell/Shell.c | 4 +++- > ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c | 6 > ++++-- > 2 files changed, 7 insertions(+), 3 deletions(-) > > diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c > index 5471930ba1..24a04ca323 100644 > --- a/ShellPkg/Application/Shell/Shell.c > +++ b/ShellPkg/Application/Shell/Shell.c > @@ -1646,7 +1646,9 @@ ShellConvertVariables ( > // > // now do the replacements... > // > - NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine); > + NewCommandLine1 = AllocatePool(NewSize); > + ASSERT (NewCommandLine1 != NULL); > [Ray] 1. Please do not use assertion because there is NULL check in the below if- > statement. > The rule in ShellPkg is avoid using assertion. > > + CopyMem (NewCommandLine1, OriginalCommandLine, StrSize > (OriginalCommandLine)); > NewCommandLine2 = AllocateZeroPool(NewSize); > ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16))); > if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp > == NULL) { > diff --git > a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c > b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c > index 1122c89b8b..5de62219b3 100644 > --- a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c > +++ b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c > @@ -143,10 +143,11 @@ UpdateOptionalData( > OriginalOptionDataSize += (*(UINT16*)(OriginalData + sizeof(UINT32))); > OriginalOptionDataSize -= OriginalSize; > NewSize = OriginalSize - OriginalOptionDataSize + DataSize; > - NewData = AllocateCopyPool(NewSize, OriginalData); > + NewData = AllocatePool(NewSize); > if (NewData == NULL) { > Status = EFI_OUT_OF_RESOURCES; > } else { > + CopyMem (NewData, OriginalData, OriginalSize - OriginalOptionDataSize); > CopyMem(NewData + OriginalSize - OriginalOptionDataSize, Data, DataSize); > } > } > @@ -1120,11 +1121,12 @@ BcfgAddOpt( > // Now we know how many EFI_INPUT_KEY structs we need to attach to > the end of the EFI_KEY_OPTION struct. > // Re-allocate with the added information. > // > - KeyOptionBuffer = AllocateCopyPool(sizeof(EFI_KEY_OPTION) + > (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount), > &NewKeyOption); > + KeyOptionBuffer = AllocatePool (sizeof(EFI_KEY_OPTION) + > (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount)); > if (KeyOptionBuffer == NULL) { > ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), > gShellBcfgHiiHandle, L"bcfg"); > ShellStatus = SHELL_OUT_OF_RESOURCES; > } > [Ray] 2. Should the above NULL check return? > + CopyMem (KeyOptionBuffer, &NewKeyOption, sizeof(EFI_KEY_OPTION)); > } > for (LoopCounter = 0 ; ShellStatus == SHELL_SUCCESS && LoopCounter < > NewKeyOption.KeyData.Options.InputKeyCount; LoopCounter++) { > // > -- > 2.14.1.windows.1 > > _______________________________________________ > edk2-devel mailing list > edk2-devel@lists.01.org > https://lists.01.org/mailman/listinfo/edk2-devel ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool 2017-11-03 8:22 ` Ni, Ruiyu 2017-11-03 11:43 ` Jim.Dailey @ 2017-11-06 0:35 ` Wang, Jian J 1 sibling, 0 replies; 10+ messages in thread From: Wang, Jian J @ 2017-11-06 0:35 UTC (permalink / raw) To: Ni, Ruiyu, edk2-devel@lists.01.org; +Cc: Carsey, Jaben, Bi, Dandan Ruiyu, Thanks for the comments. > -----Original Message----- > From: Ni, Ruiyu > Sent: Friday, November 03, 2017 4:23 PM > To: Wang, Jian J <jian.j.wang@intel.com>; edk2-devel@lists.01.org > Cc: Carsey, Jaben <jaben.carsey@intel.com>; Bi, Dandan <dandan.bi@intel.com> > Subject: RE: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool > > 2 comments below. > > -----Original Message----- > From: Wang, Jian J > Sent: Friday, November 3, 2017 12:58 PM > To: edk2-devel@lists.01.org > Cc: Carsey, Jaben <jaben.carsey@intel.com>; Ni, Ruiyu <ruiyu.ni@intel.com>; Bi, > Dandan <dandan.bi@intel.com> > Subject: [PATCH 2/3] ShellPkg: Fix misuses of AllocateCopyPool > > 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. > > The solution is to allocate pool first then copy the necessary bytes to new > memory. This can avoid copying extra bytes from unknown memory range. > > 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> > --- > ShellPkg/Application/Shell/Shell.c | 4 +++- > ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c | 6 > ++++-- > 2 files changed, 7 insertions(+), 3 deletions(-) > > diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c > index 5471930ba1..24a04ca323 100644 > --- a/ShellPkg/Application/Shell/Shell.c > +++ b/ShellPkg/Application/Shell/Shell.c > @@ -1646,7 +1646,9 @@ ShellConvertVariables ( > // > // now do the replacements... > // > - NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine); > + NewCommandLine1 = AllocatePool(NewSize); > + ASSERT (NewCommandLine1 != NULL); > [Ray] 1. Please do not use assertion because there is NULL check in the below if- > statement. > The rule in ShellPkg is avoid using assertion. > Got it. It'll be removed. > + CopyMem (NewCommandLine1, OriginalCommandLine, StrSize > (OriginalCommandLine)); > NewCommandLine2 = AllocateZeroPool(NewSize); > ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16))); > if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp > == NULL) { > diff --git > a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c > b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c > index 1122c89b8b..5de62219b3 100644 > --- a/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c > +++ b/ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c > @@ -143,10 +143,11 @@ UpdateOptionalData( > OriginalOptionDataSize += (*(UINT16*)(OriginalData + sizeof(UINT32))); > OriginalOptionDataSize -= OriginalSize; > NewSize = OriginalSize - OriginalOptionDataSize + DataSize; > - NewData = AllocateCopyPool(NewSize, OriginalData); > + NewData = AllocatePool(NewSize); > if (NewData == NULL) { > Status = EFI_OUT_OF_RESOURCES; > } else { > + CopyMem (NewData, OriginalData, OriginalSize - OriginalOptionDataSize); > CopyMem(NewData + OriginalSize - OriginalOptionDataSize, Data, DataSize); > } > } > @@ -1120,11 +1121,12 @@ BcfgAddOpt( > // Now we know how many EFI_INPUT_KEY structs we need to attach to > the end of the EFI_KEY_OPTION struct. > // Re-allocate with the added information. > // > - KeyOptionBuffer = AllocateCopyPool(sizeof(EFI_KEY_OPTION) + > (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount), > &NewKeyOption); > + KeyOptionBuffer = AllocatePool (sizeof(EFI_KEY_OPTION) + > (sizeof(EFI_INPUT_KEY) * NewKeyOption.KeyData.Options.InputKeyCount)); > if (KeyOptionBuffer == NULL) { > ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), > gShellBcfgHiiHandle, L"bcfg"); > ShellStatus = SHELL_OUT_OF_RESOURCES; > } > [Ray] 2. Should the above NULL check return? It's original code not my change. But I think it should return if NULL is got. > + CopyMem (KeyOptionBuffer, &NewKeyOption, sizeof(EFI_KEY_OPTION)); > } > for (LoopCounter = 0 ; ShellStatus == SHELL_SUCCESS && LoopCounter < > NewKeyOption.KeyData.Options.InputKeyCount; LoopCounter++) { > // > -- > 2.14.1.windows.1 ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 3/3] IntelFrameworkModulePkg: Fix misuses of AllocateCopyPool 2017-11-03 4:57 [PATCH 0/3] Fix misuses of AllocateCopyPool Jian J Wang 2017-11-03 4:57 ` [PATCH 1/3] MdeModulePkg: " Jian J Wang 2017-11-03 4:57 ` [PATCH 2/3] ShellPkg: " Jian J Wang @ 2017-11-03 4:57 ` Jian J Wang 2 siblings, 0 replies; 10+ messages in thread From: Jian J Wang @ 2017-11-03 4:57 UTC (permalink / raw) To: edk2-devel; +Cc: Liming Gao, Bi Dandan 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. The solution is to allocate pool first then copy the necessary bytes to new memory. This can avoid copying extra bytes from unknown memory range. Cc: Liming Gao <liming.gao@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> --- IntelFrameworkModulePkg/Universal/BdsDxe/DeviceMngr/DeviceManager.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/IntelFrameworkModulePkg/Universal/BdsDxe/DeviceMngr/DeviceManager.c b/IntelFrameworkModulePkg/Universal/BdsDxe/DeviceMngr/DeviceManager.c index 125c49db5e..b844224068 100644 --- a/IntelFrameworkModulePkg/Universal/BdsDxe/DeviceMngr/DeviceManager.c +++ b/IntelFrameworkModulePkg/Universal/BdsDxe/DeviceMngr/DeviceManager.c @@ -480,7 +480,9 @@ AddIdToMacDeviceList ( } else { mMacDeviceList.MaxListLen += MAX_MAC_ADDRESS_NODE_LIST_LEN; if (mMacDeviceList.CurListLen != 0) { - TempDeviceList = (MENU_INFO_ITEM *)AllocateCopyPool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen, (VOID *)mMacDeviceList.NodeList); + TempDeviceList = (MENU_INFO_ITEM *)AllocatePool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen); + ASSERT (TempDeviceList != NULL); + CopyMem (TempDeviceList, mMacDeviceList.NodeList, sizeof (MENU_INFO_ITEM) * mMacDeviceList.CurListLen); } else { TempDeviceList = (MENU_INFO_ITEM *)AllocatePool (sizeof (MENU_INFO_ITEM) * mMacDeviceList.MaxListLen); } -- 2.14.1.windows.1 ^ permalink raw reply related [flat|nested] 10+ messages in thread
end of thread, other threads:[~2017-11-06 0:51 UTC | newest] Thread overview: 10+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2017-11-03 4:57 [PATCH 0/3] Fix misuses of AllocateCopyPool Jian J Wang 2017-11-03 4:57 ` [PATCH 1/3] MdeModulePkg: " Jian J Wang 2017-11-03 9:14 ` Zeng, Star 2017-11-06 0:49 ` Wang, Jian J 2017-11-03 4:57 ` [PATCH 2/3] ShellPkg: " Jian J Wang 2017-11-03 8:22 ` Ni, Ruiyu 2017-11-03 11:43 ` Jim.Dailey 2017-11-06 0:55 ` Wang, Jian J 2017-11-06 0:35 ` Wang, Jian J 2017-11-03 4:57 ` [PATCH 3/3] IntelFrameworkModulePkg: " Jian J Wang
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox