From: Zhang Lubo Date: Wed, 8 Jul 2015 06:35:37 +0000 (+0000) Subject: MdeModulePkg: Replace unsafe string functions. X-Git-Tag: edk2-stable201903~9393 X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=commitdiff_plain;h=206b5f51beb15a22417c42e846678425de60c556 MdeModulePkg: Replace unsafe string functions. Replace unsafe string functions with new added safe string functions. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Zhang Lubo Reviewed-by: Fu Siyuan Reviewed-by: Qiu Shumin Reviewed-by: Jaben Carsey git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@17881 6f19259b-4bc3-4df7-8a09-765794883524 --- diff --git a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c index 2a2bac1279..ce26b322bc 100644 --- a/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c +++ b/MdeModulePkg/Library/DxeNetLib/DxeNetLib.c @@ -3230,7 +3230,7 @@ NetLibIp6ToStr ( return EFI_BUFFER_TOO_SMALL; } - StrCpy (String, Buffer); + StrCpyS (String, StringSize / sizeof (CHAR16), Buffer); return EFI_SUCCESS; } diff --git a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiConfig.c b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiConfig.c index e263f0e306..7b77fd386b 100644 --- a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiConfig.c +++ b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiConfig.c @@ -1,7 +1,7 @@ /** @file Helper functions for configuring or getting the parameters relating to iSCSI. -Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -778,7 +778,7 @@ IScsiFormCallback ( if (EFI_ERROR (Status)) { CreatePopUp (EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE, &Key, L"Invalid iSCSI Name!", NULL); } else { - AsciiStrCpy (Private->Current->SessionConfigData.TargetName, IScsiName); + AsciiStrCpyS (Private->Current->SessionConfigData.TargetName, ISCSI_NAME_MAX_SIZE, IScsiName); } break; diff --git a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiDhcp.c b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiDhcp.c index 79e70934e6..d574ce21dd 100644 --- a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiDhcp.c +++ b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiDhcp.c @@ -1,7 +1,7 @@ /** @file iSCSI DHCP related configuration routines. -Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.
+Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -158,7 +158,7 @@ IScsiDhcpExtractRootPath ( goto ON_EXIT; } - AsciiStrCpy (ConfigNvData->TargetName, Field->Str); + AsciiStrCpyS (ConfigNvData->TargetName, ISCSI_NAME_MAX_SIZE, Field->Str); ON_EXIT: diff --git a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiExtScsiPassThru.c b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiExtScsiPassThru.c index da14105542..e9e37b7e37 100644 --- a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiExtScsiPassThru.c +++ b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiExtScsiPassThru.c @@ -1,7 +1,7 @@ /** @file The IScsi's EFI_EXT_SCSI_PASS_THRU_PROTOCOL driver. -Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -244,7 +244,7 @@ IScsiExtScsiPassThruBuildDevicePath ( CopyMem (&Node->Iscsi.Lun, ConfigNvData->BootLun, sizeof (UINT64)); Node->Iscsi.TargetPortalGroupTag = Session->TargetPortalGroupTag; - AsciiStrCpy ((CHAR8 *) Node + sizeof (ISCSI_DEVICE_PATH), ConfigNvData->TargetName); + AsciiStrCpyS ((CHAR8 *) Node + sizeof (ISCSI_DEVICE_PATH), AsciiStrLen (ConfigNvData->TargetName) + 1, ConfigNvData->TargetName); *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Node; diff --git a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c index 1cb1fb89d9..61c407e6ff 100644 --- a/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c +++ b/MdeModulePkg/Universal/Network/IScsiDxe/IScsiMisc.c @@ -1,7 +1,7 @@ /** @file Miscellaneous routines for iSCSI driver. -Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -204,7 +204,7 @@ IScsiLunToUnicodeStr ( for (Index = 0; Index < 4; Index++) { if ((Lun[2 * Index] | Lun[2 * Index + 1]) == 0) { - StrCpy (TempStr, L"0-"); + CopyMem(TempStr, L"0-", sizeof (L"0-")); } else { TempStr[0] = (CHAR16) IScsiHexString[Lun[2 * Index] >> 4]; TempStr[1] = (CHAR16) IScsiHexString[Lun[2 * Index] & 0x0F]; diff --git a/MdeModulePkg/Universal/Network/Ip4ConfigDxe/Ip4ConfigNv.c b/MdeModulePkg/Universal/Network/Ip4ConfigDxe/Ip4ConfigNv.c index 89e1ddaf39..794bb79949 100644 --- a/MdeModulePkg/Universal/Network/Ip4ConfigDxe/Ip4ConfigNv.c +++ b/MdeModulePkg/Universal/Network/Ip4ConfigDxe/Ip4ConfigNv.c @@ -1,7 +1,7 @@ /** @file Helper functions for configuring or getting the parameters relating to Ip4. -Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -487,10 +487,10 @@ Ip4DeviceExtractConfig ( *Results = AllocateZeroPool (Size * sizeof (CHAR16)); ASSERT (*Results != NULL); StrPointer = *Results; - StrCpy (StrPointer, DeviceResult); + StrCpyS (StrPointer, Size, DeviceResult); StrPointer = StrPointer + StrLen (StrPointer); *StrPointer = L'&'; - StrCpy (StrPointer + 1, FormResult); + StrCpyS (StrPointer + 1, StrLen (FormResult) + 1, FormResult); FreePool (DeviceResult); FreePool (FormResult); } else if (HiiIsConfigHdrMatch (Request, &gEfiNicIp4ConfigVariableGuid, EFI_NIC_IP4_CONFIG_VARIABLE)) { diff --git a/MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Support.c b/MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Support.c index df79c85fa0..cfb4b26df2 100644 --- a/MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Support.c +++ b/MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Support.c @@ -1,7 +1,7 @@ /** @file Support routines for Mtftp. -Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -319,17 +319,20 @@ Mtftp4SendRequest ( Packet->OpCode = HTONS (Instance->Operation); Cur = Packet->Rrq.Filename; - Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Token->Filename); + Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2, (CHAR8 *) Token->Filename); Cur += AsciiStrLen ((CHAR8 *) Token->Filename) + 1; - Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Mode); + Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len - 2 - (AsciiStrLen ((CHAR8 *) Token->Filename) + 1), (CHAR8 *) Mode); Cur += AsciiStrLen ((CHAR8 *) Mode) + 1; + Len -= ((UINT32) AsciiStrLen ((CHAR8 *) Token->Filename) + (UINT32) AsciiStrLen ((CHAR8 *) Mode) + 4); for (Index = 0; Index < Token->OptionCount; ++Index) { - Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Options[Index].OptionStr); + Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].OptionStr); Cur += AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1; + Len -= (AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1); - Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Options[Index].ValueStr); + Cur = (UINT8 *) AsciiStrCpyS ((CHAR8 *) Cur, Len, (CHAR8 *) Options[Index].ValueStr); Cur += AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1; + Len -= (AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1); } return Mtftp4SendPacket (Instance, Nbuf); @@ -371,7 +374,7 @@ Mtftp4SendError ( TftpError->OpCode = HTONS (EFI_MTFTP4_OPCODE_ERROR); TftpError->Error.ErrorCode = HTONS (ErrCode); - AsciiStrCpy ((CHAR8 *) TftpError->Error.ErrorMessage, (CHAR8 *) ErrInfo); + AsciiStrCpyS ((CHAR8 *) TftpError->Error.ErrorMessage, Len, (CHAR8 *) ErrInfo); return Mtftp4SendPacket (Instance, Packet); } diff --git a/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcMtftp.c b/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcMtftp.c index aa73132cb5..8017b73dd7 100644 --- a/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcMtftp.c +++ b/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcMtftp.c @@ -1,7 +1,7 @@ /** @file PxeBc MTFTP functions. -Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -60,7 +60,7 @@ PxeBcCheckPacket ( if (Packet->OpCode == EFI_MTFTP4_OPCODE_ERROR) { Private->Mode.TftpErrorReceived = TRUE; Private->Mode.TftpError.ErrorCode = (UINT8) Packet->Error.ErrorCode; - AsciiStrnCpy (Private->Mode.TftpError.ErrorString, (CHAR8 *) Packet->Error.ErrorMessage, PXE_MTFTP_ERROR_STRING_LENGTH); + AsciiStrnCpyS (Private->Mode.TftpError.ErrorString, PXE_MTFTP_ERROR_STRING_LENGTH, (CHAR8 *) Packet->Error.ErrorMessage, PXE_MTFTP_ERROR_STRING_LENGTH - 1); Private->Mode.TftpError.ErrorString[PXE_MTFTP_ERROR_STRING_LENGTH - 1] = '\0'; } @@ -135,13 +135,13 @@ PxeBcTftpGetFileSize ( } ReqOpt[0].OptionStr = (UINT8*)mMtftpOptions[PXE_MTFTP_OPTION_TSIZE_INDEX]; - UtoA10 (0, (CHAR8 *) OptBuf); + UtoA10 (0, (CHAR8 *) OptBuf, PXE_MTFTP_OPTBUF_MAXNUM_INDEX); ReqOpt[0].ValueStr = OptBuf; if (BlockSize != NULL) { ReqOpt[1].OptionStr = (UINT8*)mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; ReqOpt[1].ValueStr = ReqOpt[0].ValueStr + AsciiStrLen ((CHAR8 *) ReqOpt[0].ValueStr) + 1; - UtoA10 (*BlockSize, (CHAR8 *) ReqOpt[1].ValueStr); + UtoA10 (*BlockSize, (CHAR8 *) ReqOpt[1].ValueStr, PXE_MTFTP_OPTBUF_MAXNUM_INDEX - (AsciiStrLen ((CHAR8 *) ReqOpt[0].ValueStr) + 1)); OptCnt++; } @@ -160,10 +160,11 @@ PxeBcTftpGetFileSize ( if (Status == EFI_TFTP_ERROR) { Private->Mode.TftpErrorReceived = TRUE; Private->Mode.TftpError.ErrorCode = (UINT8) Packet->Error.ErrorCode; - AsciiStrnCpy ( - Private->Mode.TftpError.ErrorString, - (CHAR8 *) Packet->Error.ErrorMessage, - PXE_MTFTP_ERROR_STRING_LENGTH + AsciiStrnCpyS ( + Private->Mode.TftpError.ErrorString, + PXE_MTFTP_ERROR_STRING_LENGTH, + (CHAR8 *) Packet->Error.ErrorMessage, + PXE_MTFTP_ERROR_STRING_LENGTH - 1 ); Private->Mode.TftpError.ErrorString[PXE_MTFTP_ERROR_STRING_LENGTH - 1] = '\0'; } @@ -261,7 +262,7 @@ PxeBcTftpReadFile ( ReqOpt[0].OptionStr = (UINT8*) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; ReqOpt[0].ValueStr = OptBuf; - UtoA10 (*BlockSize, (CHAR8 *) ReqOpt[0].ValueStr); + UtoA10 (*BlockSize, (CHAR8 *) ReqOpt[0].ValueStr, PXE_MTFTP_OPTBUF_MAXNUM_INDEX); OptCnt++; } @@ -344,7 +345,7 @@ PxeBcTftpWriteFile ( ReqOpt[0].OptionStr = (UINT8*) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; ReqOpt[0].ValueStr = OptBuf; - UtoA10 (*BlockSize, (CHAR8 *) ReqOpt[0].ValueStr); + UtoA10 (*BlockSize, (CHAR8 *) ReqOpt[0].ValueStr, PXE_MTFTP_OPTBUF_MAXNUM_INDEX); OptCnt++; } @@ -418,7 +419,7 @@ PxeBcTftpReadDirectory ( ReqOpt[0].OptionStr = (UINT8*) mMtftpOptions[PXE_MTFTP_OPTION_BLKSIZE_INDEX]; ReqOpt[0].ValueStr = OptBuf; - UtoA10 (*BlockSize, (CHAR8 *) ReqOpt[0].ValueStr); + UtoA10 (*BlockSize, (CHAR8 *) ReqOpt[0].ValueStr, PXE_MTFTP_OPTBUF_MAXNUM_INDEX); OptCnt++; } diff --git a/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcMtftp.h b/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcMtftp.h index 9920aff8b0..241b079d72 100644 --- a/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcMtftp.h +++ b/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcMtftp.h @@ -1,7 +1,7 @@ /** @file Mtftp routines for PxeBc. -Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -22,6 +22,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #define PXE_MTFTP_OPTION_MAXIMUM_INDEX 4 #define PXE_MTFTP_ERROR_STRING_LENGTH 127 +#define PXE_MTFTP_OPTBUF_MAXNUM_INDEX 128 /** diff --git a/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c b/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c index 327e4a26d8..3016da7324 100644 --- a/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c +++ b/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.c @@ -1,7 +1,7 @@ /** @file Support routines for PxeBc. -Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.
+Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -140,16 +140,18 @@ CvtNum ( /** Convert unsigned int number to decimal number. - @param Number The unsigned int number will be converted. - @param Buffer Pointer to the buffer to store the decimal number after transform. - + @param Number The unsigned int number will be converted. + @param Buffer Pointer to the buffer to store the decimal number after transform. + @param[in] BufferSize The maxsize of the buffer. + @return the length of the number after transform. **/ UINTN UtoA10 ( IN UINTN Number, - IN CHAR8 *Buffer + IN CHAR8 *Buffer, + IN UINTN BufferSize ) { UINTN Index; @@ -164,7 +166,7 @@ UtoA10 ( Number = Number / 10; } while (Number != 0); - AsciiStrCpy (Buffer, &TempStr[Index]); + AsciiStrCpyS (Buffer, BufferSize, &TempStr[Index]); return AsciiStrLen (Buffer); } diff --git a/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.h b/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.h index 96f48f56de..6f41aa671d 100644 --- a/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.h +++ b/MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcSupport.h @@ -1,6 +1,6 @@ /** @file Support routines for PxeBc. -Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.
+Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -82,16 +82,19 @@ CvtNum ( /** Convert unsigned int number to decimal number. - @param Number The unsigned int number will be converted. - @param Buffer Pointer to the buffer to store the decimal number after transform. - + @param Number The unsigned int number will be converted. + @param Buffer Pointer to the buffer to store the decimal number after transform. + @param[in] BufferSize The maxsize of the buffer. + @return the length of the number after transform. **/ UINTN UtoA10 ( IN UINTN Number, - IN CHAR8 *Buffer + IN CHAR8 *Buffer, + IN UINTN BufferSize + ); diff --git a/MdeModulePkg/Universal/Network/VlanConfigDxe/VlanConfigImpl.c b/MdeModulePkg/Universal/Network/VlanConfigDxe/VlanConfigImpl.c index fd8555e30e..5e0fe42a88 100644 --- a/MdeModulePkg/Universal/Network/VlanConfigDxe/VlanConfigImpl.c +++ b/MdeModulePkg/Universal/Network/VlanConfigDxe/VlanConfigImpl.c @@ -1,7 +1,7 @@ /** @file HII Config Access protocol implementation of VLAN configuration module. -Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full @@ -412,17 +412,17 @@ VlanUpdateForm ( for (Index = 0; Index < NumberOfVlan; Index++) { String = VlanStr; - StrCpy (String, L" VLAN ID:"); + StrCpyS (String, (sizeof (VlanStr) /sizeof (CHAR16)), L" VLAN ID:"); String += 10; // // Pad VlanId string up to 4 characters with space // DigitalCount = UnicodeValueToString (VlanIdStr, 0, VlanData[Index].VlanId, 5); SetMem16 (String, (4 - DigitalCount) * sizeof (CHAR16), L' '); - StrCpy (String + 4 - DigitalCount, VlanIdStr); + StrCpyS (String + 4 - DigitalCount, (sizeof (VlanStr) /sizeof (CHAR16)) - 10 - (4 - DigitalCount), VlanIdStr); String += 4; - StrCpy (String, L", Priority:"); + StrCpyS (String, (sizeof (VlanStr) /sizeof (CHAR16)) - 10 - (4 - DigitalCount) - 4, L", Priority:"); String += 11; String += UnicodeValueToString (String, 0, VlanData[Index].Priority, 4); *String = 0; @@ -559,9 +559,9 @@ InstallVlanConfigForm ( } PrivateData->MacString = MacString; - StrCpy (Str, L"VLAN Configuration (MAC:"); - StrnCat (Str, MacString, sizeof (EFI_MAC_ADDRESS) * 2); - StrCat (Str, L")"); + StrCpyS (Str, sizeof (Str) / sizeof (CHAR16), L"VLAN Configuration (MAC:"); + StrCatS (Str, sizeof (Str) / sizeof (CHAR16), MacString); + StrCatS (Str, sizeof (Str) / sizeof (CHAR16), L")"); HiiSetString ( HiiHandle, STRING_TOKEN (STR_VLAN_FORM_SET_TITLE_HELP),