From 532691c8ba90e5022174503ab0781322c6f79cab Mon Sep 17 00:00:00 2001 From: jcarsey Date: Fri, 14 Oct 2011 19:21:13 +0000 Subject: ShellPkg: Add checking for memory allocation and pointer returns from functions. signed-off-by: jcarsey reviewed-by: geekboy15a git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12540 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Application/Shell/Shell.c | 29 ++++++++++++++++------ ShellPkg/Application/Shell/ShellManParser.c | 16 ++++++++++++ .../Application/Shell/ShellParametersProtocol.c | 24 +++++++++++------- ShellPkg/Application/Shell/ShellProtocol.c | 25 +++++++++++++------ 4 files changed, 70 insertions(+), 24 deletions(-) (limited to 'ShellPkg/Application') diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index 614c1e9595..d266c826e9 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -857,15 +857,19 @@ DoStartupScript( FileStringPath = NULL; NewSize = 0; FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0); - TempSpot = StrStr(FileStringPath, L";"); - if (TempSpot != NULL) { - *TempSpot = CHAR_NULL; + if (FileStringPath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + TempSpot = StrStr(FileStringPath, L";"); + if (TempSpot != NULL) { + *TempSpot = CHAR_NULL; + } + FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0); + PathRemoveLastItem(FileStringPath); + FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0); + Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ); + FreePool(FileStringPath); } - FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0); - PathRemoveLastItem(FileStringPath); - FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0); - Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ); - FreePool(FileStringPath); } if (EFI_ERROR(Status)) { NamePath = FileDevicePath (NULL, mStartupScript); @@ -1215,6 +1219,12 @@ RunSplitCommand( NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0); OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine); + + if (NextCommandLine == NULL || OurCommandLine == NULL) { + SHELL_FREE_NON_NULL(OurCommandLine); + SHELL_FREE_NON_NULL(NextCommandLine); + return (EFI_OUT_OF_RESOURCES); + } if (NextCommandLine[0] != CHAR_NULL && NextCommandLine[0] == L'a' && NextCommandLine[1] == L' ' @@ -1334,6 +1344,9 @@ RunCommand( Split = NULL; CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0); + if (CleanOriginal == NULL) { + return (EFI_OUT_OF_RESOURCES); + } while (CleanOriginal[StrLen(CleanOriginal)-1] == L' ') { CleanOriginal[StrLen(CleanOriginal)-1] = CHAR_NULL; } diff --git a/ShellPkg/Application/Shell/ShellManParser.c b/ShellPkg/Application/Shell/ShellManParser.c index bd2efcb423..470f51a8ea 100644 --- a/ShellPkg/Application/Shell/ShellManParser.c +++ b/ShellPkg/Application/Shell/ShellManParser.c @@ -180,6 +180,10 @@ ManBufferFindSections( TempString2 = MIN(TempString2, StrStr(CurrentLocation, L"\n")); ASSERT(TempString == NULL); TempString = StrnCatGrow(&TempString, NULL, CurrentLocation, TempString2==NULL?0:TempString2 - CurrentLocation); + if (TempString == NULL) { + Status = EFI_OUT_OF_RESOURCES; + break; + } SectionName = TempString; SectionLen = StrLen(SectionName); SectionName = StrStr(Sections, SectionName); @@ -197,12 +201,24 @@ ManBufferFindSections( TempString2 = MIN(TempString2, StrStr(CurrentLocation, L"\n")); ASSERT(TempString == NULL); TempString = StrnCatGrow(&TempString, NULL, CurrentLocation, TempString2==NULL?0:TempString2 - CurrentLocation); + if (TempString == NULL) { + Status = EFI_OUT_OF_RESOURCES; + break; + } // // copy and save the current line. // ASSERT((*HelpText == NULL && *HelpSize == 0) || (*HelpText != NULL)); StrnCatGrow (HelpText, HelpSize, TempString, 0); + if (HelpText == NULL) { + Status = EFI_OUT_OF_RESOURCES; + break; + } StrnCatGrow (HelpText, HelpSize, L"\r\n", 0); + if (HelpText == NULL) { + Status = EFI_OUT_OF_RESOURCES; + break; + } } } SHELL_FREE_NON_NULL(TempString); diff --git a/ShellPkg/Application/Shell/ShellParametersProtocol.c b/ShellPkg/Application/Shell/ShellParametersProtocol.c index 5beaadab2e..55cf3c279e 100644 --- a/ShellPkg/Application/Shell/ShellParametersProtocol.c +++ b/ShellPkg/Application/Shell/ShellParametersProtocol.c @@ -577,6 +577,9 @@ UpdateStdInStdOutStdErr( } CommandLineCopy = StrnCatGrow(&CommandLineCopy, NULL, NewCommandLine, 0); + if (CommandLineCopy == NULL) { + return (EFI_OUT_OF_RESOURCES); + } Status = EFI_SUCCESS; Split = NULL; FirstLocation = CommandLineCopy + StrLen(CommandLineCopy); @@ -1030,16 +1033,19 @@ UpdateStdInStdOutStdErr( // if (!EFI_ERROR(Status) && StdInVarName != NULL) { TempHandle = CreateFileInterfaceEnv(StdInVarName); - if (!InUnicode) { - TempHandle = CreateFileInterfaceFile(TempHandle, FALSE); - } - Size = 0; - ASSERT(TempHandle != NULL); - if (((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) { - Status = EFI_INVALID_PARAMETER; + if (TempHandle == NULL) { + Status = EFI_OUT_OF_RESOURCES; } else { - ShellParameters->StdIn = TempHandle; - gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle); + if (!InUnicode) { + TempHandle = CreateFileInterfaceFile(TempHandle, FALSE); + } + Size = 0; + if (((EFI_FILE_PROTOCOL*)TempHandle)->Read(TempHandle, &Size, NULL) != EFI_BUFFER_TOO_SMALL) { + Status = EFI_INVALID_PARAMETER; + } else { + ShellParameters->StdIn = TempHandle; + gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle); + } } } diff --git a/ShellPkg/Application/Shell/ShellProtocol.c b/ShellPkg/Application/Shell/ShellProtocol.c index de7dc3cad9..6b81918101 100644 --- a/ShellPkg/Application/Shell/ShellProtocol.c +++ b/ShellPkg/Application/Shell/ShellProtocol.c @@ -488,10 +488,12 @@ EfiShellGetFilePathFromDevicePath( This function converts a file system style name to a device path, by replacing any mapping references to the associated device path. - @param Path the pointer to the path + @param[in] Path The pointer to the path. - @return all The pointer of the file path. The file path is callee + @return The pointer of the file path. The file path is callee allocated and should be freed by the caller. + @retval NULL The path could not be found. + @retval NULL There was not enough available memory. **/ EFI_DEVICE_PATH_PROTOCOL * EFIAPI @@ -525,7 +527,9 @@ EfiShellGetDevicePathFromFilePath( Size = StrSize(Cwd); Size += StrSize(Path); NewPath = AllocateZeroPool(Size); - ASSERT(NewPath != NULL); + if (NewPath == NULL) { + return (NULL); + } StrCpy(NewPath, Cwd); if (*Path == L'\\') { Path++; @@ -543,8 +547,7 @@ EfiShellGetDevicePathFromFilePath( // ASSERT((MapName == NULL && Size == 0) || (MapName != NULL)); MapName = StrnCatGrow(&MapName, &Size, Path, (StrStr(Path, L":")-Path+1)); - if (MapName[StrLen(MapName)-1] != L':') { - ASSERT(FALSE); + if (MapName == NULL || MapName[StrLen(MapName)-1] != L':') { return (NULL); } @@ -564,7 +567,6 @@ EfiShellGetDevicePathFromFilePath( // DevicePathCopyForFree = DevicePathCopy = DuplicateDevicePath(DevicePath); if (DevicePathCopy == NULL) { - ASSERT(FALSE); FreePool(MapName); return (NULL); } @@ -1888,6 +1890,9 @@ EfiShellFindFilesInDir( TempString = NULL; Size = 0; TempString = StrnCatGrow(&TempString, &Size, ShellFileHandleGetPath(FileDirHandle), 0); + if (TempString == NULL) { + return (EFI_OUT_OF_RESOURCES); + } TempSpot = StrStr(TempString, L";"); if (TempSpot != NULL) { @@ -1895,6 +1900,9 @@ EfiShellFindFilesInDir( } TempString = StrnCatGrow(&TempString, &Size, BasePath, 0); + if (TempString == NULL) { + return (EFI_OUT_OF_RESOURCES); + } BasePath = TempString; } @@ -2239,6 +2247,9 @@ EfiShellFindFiles( ASSERT(MapName == NULL); MapName = StrnCatGrow(&MapName, NULL, PatternCopy, Count); + if (MapName == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } if (!EFI_ERROR(Status)) { RootDevicePath = EfiShellGetDevicePathFromFilePath(PatternCopy); @@ -2759,7 +2770,7 @@ EfiShellSetCurDir( MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0); ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL)); MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0); - if (MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') { + if (MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') { ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL)); MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0); } -- cgit v1.2.3