From ecae51177e83db0d99f8b4888ae4b866c18651b6 Mon Sep 17 00:00:00 2001 From: jcarsey Date: Mon, 10 Oct 2011 20:32:17 +0000 Subject: ShellPkg: Add checks for NULL pointers. This adds lots of pointer verification with ASSERTs only used when the condition should be impossible and never for memory allocation. signed-off-by: jcarsey reviewed-by: geekboy15a git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12523 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Application/Shell/FileHandleWrappers.c | 6 +++++- ShellPkg/Application/Shell/ShellManParser.c | 6 +++++- .../UefiHandleParsingLib/UefiHandleParsingLib.c | 8 +++++--- ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c | 17 +++++++++++------ .../Library/UefiShellDebug1CommandsLib/Compress.c | 19 +++++++++++++++---- .../Library/UefiShellDebug1CommandsLib/Edit/Edit.c | 5 ++++- .../Library/UefiShellDebug1CommandsLib/EfiCompress.c | 7 +++++-- .../UefiShellDebug1CommandsLib/EfiDecompress.c | 7 +++++-- .../UefiShellDebug1CommandsLib/HexEdit/FileImage.c | 2 +- 9 files changed, 56 insertions(+), 21 deletions(-) (limited to 'ShellPkg') diff --git a/ShellPkg/Application/Shell/FileHandleWrappers.c b/ShellPkg/Application/Shell/FileHandleWrappers.c index 2e1ce08ef3..2ca13cb3f3 100644 --- a/ShellPkg/Application/Shell/FileHandleWrappers.c +++ b/ShellPkg/Application/Shell/FileHandleWrappers.c @@ -1327,7 +1327,8 @@ FileInterfaceMemGetPosition( @param[in, out] BufferSize Size in bytes of Buffer. @param[in] Buffer The pointer to the buffer to write. - @retval EFI_SUCCESS The data was written. + @retval EFI_OUT_OF_RESOURCES The operation failed due to lack of resources. + @retval EFI_SUCCESS The data was written. **/ EFI_STATUS EFIAPI @@ -1354,6 +1355,9 @@ FileInterfaceMemWrite( // Ascii // AsciiBuffer = AllocateZeroPool(*BufferSize); + if (AsciiBuffer == NULL) { + return (EFI_OUT_OF_RESOURCES); + } AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer); if ((UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->Position + AsciiStrSize(AsciiBuffer)) > (UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->BufferSize)) { ((EFI_FILE_PROTOCOL_MEM*)This)->Buffer = ReallocatePool((UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->BufferSize), (UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->BufferSize) + AsciiStrSize(AsciiBuffer) + 10, ((EFI_FILE_PROTOCOL_MEM*)This)->Buffer); diff --git a/ShellPkg/Application/Shell/ShellManParser.c b/ShellPkg/Application/Shell/ShellManParser.c index bcc1f8e0a3..bd2efcb423 100644 --- a/ShellPkg/Application/Shell/ShellManParser.c +++ b/ShellPkg/Application/Shell/ShellManParser.c @@ -548,7 +548,8 @@ ManFileFindTitleSection( @retval EFI_SUCCESS The help text was returned. @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the returned help text. - @retval EFI_INVALID_PARAMETER HelpText is NULL + @retval EFI_INVALID_PARAMETER HelpText is NULL. + @retval EFI_INVALID_PARAMETER ManFileName is invalid. @retval EFI_NOT_FOUND There is no help text available for Command. **/ EFI_STATUS @@ -594,6 +595,9 @@ ProcessManFile( } else { FileHandle = NULL; TempString = GetManFileName(ManFileName); + if (TempString == NULL) { + return (EFI_INVALID_PARAMETER); + } Status = SearchPathForFile(TempString, &FileHandle); if (EFI_ERROR(Status)) { diff --git a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c index 77f7756dc7..9e51648217 100644 --- a/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c +++ b/ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c @@ -206,8 +206,10 @@ TxtOutProtocolDumpInformation( RetVal = AllocateZeroPool(Size); Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_TXT_OUT_DUMP_HEADER), NULL); - UnicodeSPrint(RetVal, Size, Temp, Dev, Dev->Mode->Attribute); - FreePool(Temp); + if (Temp != NULL) { + UnicodeSPrint(RetVal, Size, Temp, Dev, Dev->Mode->Attribute); + FreePool(Temp); + } // // Dump TextOut Info @@ -219,7 +221,7 @@ TxtOutProtocolDumpInformation( UnicodeSPrint( RetVal + StrLen(RetVal), NewSize, - Temp, + Temp == NULL?L"":Temp, Index == Dev->Mode->Mode ? L'*' : L' ', Index, !EFI_ERROR(Status)?Col:-1, diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c index b59ef31682..01ef1997b2 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c @@ -49,6 +49,7 @@ ShellCommandRunComp ( UINTN DataSizeFromFile2; CHAR16 *FileName1; CHAR16 *FileName2; + CONST CHAR16 *TempParam; ErrorCount = 0; ShellStatus = SHELL_SUCCESS; @@ -88,25 +89,29 @@ ShellCommandRunComp ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle); ShellStatus = SHELL_INVALID_PARAMETER; } else { - FileName1 = ShellFindFilePath(ShellCommandLineGetRawValue(Package, 1)); + TempParam = ShellCommandLineGetRawValue(Package, 1); + ASSERT(TempParam != NULL); + FileName1 = ShellFindFilePath(TempParam); if (FileName1 == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 1)); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, TempParam); ShellStatus = SHELL_NOT_FOUND; } else { Status = ShellOpenFileByName(FileName1, &FileHandle1, EFI_FILE_MODE_READ, 0); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_OPEN_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 1), Status); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_OPEN_FAIL), gShellDebug1HiiHandle, TempParam, Status); ShellStatus = SHELL_NOT_FOUND; } } - FileName2 = ShellFindFilePath(ShellCommandLineGetRawValue(Package, 2)); + TempParam = ShellCommandLineGetRawValue(Package, 2); + ASSERT(TempParam != NULL); + FileName2 = ShellFindFilePath(TempParam); if (FileName2 == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 2)); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, TempParam); ShellStatus = SHELL_NOT_FOUND; } else { Status = ShellOpenFileByName(FileName2, &FileHandle2, EFI_FILE_MODE_READ, 0); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_OPEN_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 2), Status); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_OPEN_FAIL), gShellDebug1HiiHandle, TempParam, Status); ShellStatus = SHELL_NOT_FOUND; } } diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c index 06a3210ea0..b1f8327f4a 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c @@ -607,8 +607,10 @@ FreadCrc ( Advance the current position (read in new data if needed). Delete outdated string info. Find a match string for current position. + @retval TRUE The operation was successful. + @retval FALSE The operation failed due to insufficient memory. **/ -VOID +BOOLEAN EFIAPI GetNextMatch ( VOID @@ -621,6 +623,9 @@ GetNextMatch ( mPos++; if (mPos == WNDSIZ * 2) { Temp = AllocateZeroPool (WNDSIZ + MAXMATCH); + if (Temp == NULL) { + return (FALSE); + } CopyMem (Temp, &mText[WNDSIZ], WNDSIZ + MAXMATCH); CopyMem (&mText[0], Temp, WNDSIZ + MAXMATCH); FreePool (Temp); @@ -631,6 +636,8 @@ GetNextMatch ( DeleteNode (); InsertNode (); + + return (TRUE); } /** @@ -1286,7 +1293,9 @@ Encode ( while (mRemainder > 0) { LastMatchLen = mMatchLen; LastMatchPos = mMatchPos; - GetNextMatch (); + if (!GetNextMatch ()) { + Status = EFI_OUT_OF_RESOURCES; + } if (mMatchLen > mRemainder) { mMatchLen = mRemainder; } @@ -1306,7 +1315,9 @@ Encode ( (mPos - LastMatchPos - 2) & (WNDSIZ - 1)); LastMatchLen--; while (LastMatchLen > 0) { - GetNextMatch (); + if (!GetNextMatch ()) { + Status = EFI_OUT_OF_RESOURCES; + } LastMatchLen--; } @@ -1318,7 +1329,7 @@ Encode ( HufEncodeEnd (); FreeMemory (); - return EFI_SUCCESS; + return (Status); } /** diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c index c4dbf80c96..c28f9bc591 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c @@ -36,6 +36,7 @@ ShellCommandRunEdit ( CONST CHAR16 *Cwd; CHAR16 *Nfs; CHAR16 *Spot; + CONST CHAR16 *TempParam; // SHELL_FILE_HANDLE TempHandle; Buffer = NULL; @@ -101,7 +102,9 @@ ShellCommandRunEdit ( // if editor launched with file named // if (ShellCommandLineGetCount(Package) == 2) { - FileBufferSetFileName (ShellCommandLineGetRawValue(Package, 1)); + TempParam = ShellCommandLineGetRawValue(Package, 1); + ASSERT(TempParam != NULL); + FileBufferSetFileName (TempParam); // if (EFI_ERROR(ShellFileExists(MainEditor.FileBuffer->FileName))) { // Status = ShellOpenFileByName(MainEditor.FileBuffer->FileName, &TempHandle, EFI_FILE_MODE_CREATE|EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0); // if (!EFI_ERROR(Status)) { diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c index d4bb6217b0..25e8718980 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c @@ -42,6 +42,7 @@ ShellCommandRunEfiCompress ( VOID *InBuffer; CHAR16 *InFileName; CONST CHAR16 *OutFileName; + CONST CHAR16 *TempParam; InFileName = NULL; OutFileName = NULL; @@ -82,10 +83,12 @@ ShellCommandRunEfiCompress ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle); ShellStatus = SHELL_INVALID_PARAMETER; } else { - InFileName = ShellFindFilePath(ShellCommandLineGetRawValue(Package, 1)); + TempParam = ShellCommandLineGetRawValue(Package, 1); + ASSERT(TempParam != NULL); + InFileName = ShellFindFilePath(TempParam); OutFileName = ShellCommandLineGetRawValue(Package, 2); if (InFileName == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 1)); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, TempParam); ShellStatus = SHELL_NOT_FOUND; } else { if (ShellIsDirectory(InFileName) == EFI_SUCCESS){ diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c index 4b4671b455..35a32f1f3b 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c @@ -46,6 +46,7 @@ ShellCommandRunEfiDecompress ( UINT32 ScratchSize; VOID *ScratchBuffer; EFI_DECOMPRESS_PROTOCOL *Decompress; + CONST CHAR16 *TempParam; InFileName = NULL; OutFileName = NULL; @@ -87,10 +88,12 @@ ShellCommandRunEfiDecompress ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle); ShellStatus = SHELL_INVALID_PARAMETER; } else { - InFileName = ShellFindFilePath(ShellCommandLineGetRawValue(Package, 1)); + TempParam = ShellCommandLineGetRawValue(Package, 1); + ASSERT(TempParam != NULL); + InFileName = ShellFindFilePath(TempParam); OutFileName = ShellCommandLineGetRawValue(Package, 2); if (InFileName == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 1)); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, TempParam); ShellStatus = SHELL_NOT_FOUND; } else { if (ShellIsDirectory(InFileName) == EFI_SUCCESS){ diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c index 8562662638..f5fb7d262a 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c @@ -169,7 +169,7 @@ HFileImageRead ( // you should set the status string // Status = ReadFileIntoBuffer (FileName, (VOID**)&Buffer, &HFileImage.Size, &HFileImage.ReadOnly); - if (EFI_ERROR(Status)) { + if (EFI_ERROR(Status) || Buffer == NULL) { UnicodeBuffer = CatSPrint(NULL, L"Read error on file &s: %r", FileName, Status); if (UnicodeBuffer == NULL) { SHELL_FREE_NON_NULL(Buffer); -- cgit v1.2.3