Skip to content

Commit

Permalink
ShellPkg: More CodeQL fixes (#321)
Browse files Browse the repository at this point in the history
## Description

Various fixes

- [x] Impacts functionality?
- **Functionality** - Does the change ultimately impact how firmware
functions?
- Examples: Add a new library, publish a new PPI, update an algorithm,
...
- [x] Impacts security?
- **Security** - Does the change have a direct security impact on an
application,
    flow, or firmware?
  - Examples: Crypto algorithm change, buffer overflow fix, parameter
    validation improvement, ...
- [ ] Breaking change?
- **Breaking change** - Will anyone consuming this change experience a
break
    in build or boot behavior?
- Examples: Add a new library class, move a module to a different repo,
call
    a function in a new library class in a pre-existing module, ...
- [ ] Includes tests?
  - **Tests** - Does the change include any explicit test code?
  - Examples: Unit tests, integration tests, robot tests, ...
- [ ] Includes documentation?
- **Documentation** - Does the change contain explicit documentation
additions
    outside direct code modifications (and comments)?
- Examples: Update readme file, add feature readme file, link to
documentation
    on an a separate Web page, ...

## How This Was Tested

Build and boot changes on QemuQ35Pkg to EFI shell.

## Integration Instructions

N/A
  • Loading branch information
TaylorBeebe authored and kenlautner committed May 9, 2023
1 parent 5b2d935 commit a05d819
Show file tree
Hide file tree
Showing 27 changed files with 384 additions and 59 deletions.
10 changes: 8 additions & 2 deletions ShellPkg/Application/Shell/FileHandleWrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2144,8 +2144,14 @@ FileInterfaceFileWrite (
// Ascii
//
AsciiBuffer = AllocateZeroPool (*BufferSize);
Size = AsciiSPrint (AsciiBuffer, *BufferSize, "%S", Buffer); // MU_CHANGE BZ3232
Status = (((EFI_FILE_PROTOCOL_FILE *)This)->Orig->Write (((EFI_FILE_PROTOCOL_FILE *)This)->Orig, &Size, AsciiBuffer));
// MU_CHANGE [BEGIN] - CodeQL change
if (AsciiBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change
Size = AsciiSPrint (AsciiBuffer, *BufferSize, "%S", Buffer); // MU_CHANGE BZ3232
Status = (((EFI_FILE_PROTOCOL_FILE *)This)->Orig->Write (((EFI_FILE_PROTOCOL_FILE *)This)->Orig, &Size, AsciiBuffer));
FreePool (AsciiBuffer);
return (Status);
}
Expand Down
7 changes: 7 additions & 0 deletions ShellPkg/Application/Shell/Shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,13 @@ UefiMain (

Size = 100;
TempString = AllocateZeroPool (Size);
// MU_CHANGE [BEGIN] - CodeQL change
if (TempString == NULL) {
ASSERT (TempString != NULL);
return EFI_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change

UnicodeSPrint (TempString, Size, L"%d", PcdGet8 (PcdShellSupportLevel));
Status = InternalEfiShellSetEnv (L"uefishellsupport", TempString, TRUE);
Expand Down
8 changes: 7 additions & 1 deletion ShellPkg/Application/Shell/ShellParametersProtocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,13 @@ CreatePopulateInstallShellParametersProtocol (
Status = SHELL_GET_ENVIRONMENT_VARIABLE (L"ShellOpt", &Size, FullCommandLine);
if (Status == EFI_BUFFER_TOO_SMALL) {
FullCommandLine = AllocateZeroPool (Size + LoadedImage->LoadOptionsSize);
Status = SHELL_GET_ENVIRONMENT_VARIABLE (L"ShellOpt", &Size, FullCommandLine);
// MU_CHANGE [BEGIN] - CodeQL change
if (FullCommandLine == NULL) {
return EFI_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change
Status = SHELL_GET_ENVIRONMENT_VARIABLE (L"ShellOpt", &Size, FullCommandLine);
}

if (Status == EFI_NOT_FOUND) {
Expand Down
13 changes: 13 additions & 0 deletions ShellPkg/Application/Shell/ShellProtocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,12 @@ EfiShellGetEnvEx (
// Allocate the space and recall the get function
//
Buffer = AllocateZeroPool (Size);
// MU_CHANGE [BEGIN] - CodeQL change
if (Buffer == NULL) {
return NULL;
}

// MU_CHANGE [END] - CodeQL change
Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES (Name, Attributes, &Size, Buffer);
}

Expand Down Expand Up @@ -3562,6 +3568,13 @@ EfiShellGetAlias (
Status = gRT->GetVariable (AliasLower, &gShellAliasGuid, &Attribs, &RetSize, RetVal);
if (Status == EFI_BUFFER_TOO_SMALL) {
RetVal = AllocateZeroPool (RetSize);
// MU_CHANGE [BEGIN] - CodeQL change
if (RetVal == NULL) {
FreePool (AliasLower);
return NULL;
}

// MU_CHANGE [END] - CodeQL change
Status = gRT->GetVariable (AliasLower, &gShellAliasGuid, &Attribs, &RetSize, RetVal);
}

Expand Down
57 changes: 39 additions & 18 deletions ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,25 +315,28 @@ LoadedImageProtocolDumpInformation (
PdbFileName = PeCoffLoaderGetPdbPointer (LoadedImage->ImageBase);
DataType = ConvertMemoryType (LoadedImage->ImageDataType);
CodeType = ConvertMemoryType (LoadedImage->ImageCodeType);
// MU_CHANGE [BEGIN] - CodeQL change
if ((PdbFileName != NULL) && (DataType != NULL) && (CodeType != NULL) && (FilePath != NULL)) {
RetVal = CatSPrint (
RetVal,
Temp,
LoadedImage->Revision,
LoadedImage->ParentHandle,
LoadedImage->SystemTable,
LoadedImage->DeviceHandle,
FilePath,
PdbFileName,
LoadedImage->LoadOptionsSize,
LoadedImage->LoadOptions,
LoadedImage->ImageBase,
LoadedImage->ImageSize,
CodeType,
DataType,
LoadedImage->Unload
);
}

RetVal = CatSPrint (
RetVal,
Temp,
LoadedImage->Revision,
LoadedImage->ParentHandle,
LoadedImage->SystemTable,
LoadedImage->DeviceHandle,
FilePath,
PdbFileName,
LoadedImage->LoadOptionsSize,
LoadedImage->LoadOptions,
LoadedImage->ImageBase,
LoadedImage->ImageSize,
CodeType,
DataType,
LoadedImage->Unload
);

// MU_CHANGE [END] - CodeQL change
SHELL_FREE_NON_NULL (Temp);
SHELL_FREE_NON_NULL (FilePath);
SHELL_FREE_NON_NULL (CodeType);
Expand Down Expand Up @@ -395,7 +398,13 @@ GraphicsOutputProtocolDumpInformation (
}

Fmt = ConvertPixelFormat (GraphicsOutput->Mode->Info->PixelFormat);
// MU_CHANGE [BEGIN] - CodeQL change
if (Fmt == NULL) {
SHELL_FREE_NON_NULL (Temp);
return NULL;
}

// MU_CHANGE [END] - CodeQL change
RetVal = CatSPrint (
NULL,
Temp,
Expand Down Expand Up @@ -813,7 +822,12 @@ TxtOutProtocolDumpInformation (

Size = (Dev->Mode->MaxMode + 1) * 80;
RetVal = AllocateZeroPool (Size);
// MU_CHANGE [BEGIN] - CodeQL change
if (RetVal == NULL) {
return NULL;
}

// MU_CHANGE END] - CodeQL change
Temp = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN (STR_TXT_OUT_DUMP_HEADER), NULL);
if (Temp != NULL) {
UnicodeSPrint (RetVal, Size, Temp, Dev, Dev->Mode->Attribute);
Expand All @@ -824,6 +838,13 @@ TxtOutProtocolDumpInformation (
// Dump TextOut Info
//
Temp = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN (STR_TXT_OUT_DUMP_LINE), NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (Temp == NULL) {
FreePool (RetVal);
return NULL;
}

// MU_CHANGE [END] - CodeQL change
for (Index = 0; Index < Dev->Mode->MaxMode; Index++) {
Status = Dev->QueryMode (Dev, Index, &Col, &Row);
NewSize = Size - StrSize (RetVal);
Expand Down
14 changes: 14 additions & 0 deletions ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ GetBootOptionCrc (
);
if (Status == EFI_BUFFER_TOO_SMALL) {
Buffer = AllocateZeroPool (BufferSize);
// MU_CHANGE [BEGIN] - CodeQL change
if (Buffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change
Status = gRT->GetVariable (
VariableName,
(EFI_GUID *)&gEfiGlobalVariableGuid,
Expand Down Expand Up @@ -1388,6 +1394,14 @@ BcfgDisplayDump (
);
if (Status == EFI_BUFFER_TOO_SMALL) {
Buffer = AllocateZeroPool (BufferSize);
// MU_CHANGE [BEGIN] - CodeQL change
if (Buffer == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellBcfgHiiHandle, L"bcfg");
++Errors;
goto Cleanup;
}

// MU_CHANGE [END] - CodeQL change
Status = gRT->GetVariable (
VariableName,
(EFI_GUID *)&gEfiGlobalVariableGuid,
Expand Down
6 changes: 6 additions & 0 deletions ShellPkg/Library/UefiShellCommandLib/ConsistMapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,12 @@ GetHIDevicePath (
NonHIDevicePathNodeCount = 0;

HIDevicePath = AllocateZeroPool (sizeof (EFI_DEVICE_PATH_PROTOCOL));
// MU_CHANGE [BEGIN] - CodeQL change
if (HIDevicePath == NULL) {
return NULL;
}

// MU_CHANGE [END] - CodeQL change
SetDevicePathEndNode (HIDevicePath);

Node.DevPath.Type = END_DEVICE_PATH_TYPE;
Expand Down
25 changes: 23 additions & 2 deletions ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,12 @@ ShellCommandCreateNewMappingName (
String = NULL;

String = AllocateZeroPool (PcdGet8 (PcdShellMapNameLength) * sizeof (String[0]));
// MU_CHANGE [BEGIN] - CodeQL change
if (String == NULL) {
return (NULL);
}

// MU_CHANGE [END] - CodeQL change
UnicodeSPrint (
String,
PcdGet8 (PcdShellMapNameLength) * sizeof (String[0]),
Expand Down Expand Up @@ -1475,7 +1481,14 @@ ShellCommandCreateInitialMappingsAndPaths (
// Get default name first
//
NewDefaultName = ShellCommandCreateNewMappingName (MappingTypeFileSystem);
ASSERT (NewDefaultName != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (NewDefaultName == NULL) {
ASSERT (NewDefaultName != NULL);
Status = EFI_OUT_OF_RESOURCES;
break;
}

// MU_CHANGE [END] - CodeQL change
Status = ShellCommandAddMapItemAndUpdatePath (NewDefaultName, DevicePathList[Count], 0, TRUE);
ASSERT_EFI_ERROR (Status);
FreePool (NewDefaultName);
Expand Down Expand Up @@ -1565,7 +1578,15 @@ ShellCommandCreateInitialMappingsAndPaths (
// Get default name first
//
NewDefaultName = ShellCommandCreateNewMappingName (MappingTypeBlockIo);
ASSERT (NewDefaultName != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (NewDefaultName == NULL) {
ASSERT (NewDefaultName != NULL);
SHELL_FREE_NON_NULL (HandleList);
SHELL_FREE_NON_NULL (DevicePathList);
return EFI_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change
Status = ShellCommandAddMapItemAndUpdatePath (NewDefaultName, DevicePathList[Count], 0, FALSE);
ASSERT_EFI_ERROR (Status);
FreePool (NewDefaultName);
Expand Down
14 changes: 13 additions & 1 deletion ShellPkg/Library/UefiShellDebug1CommandsLib/DmpStore.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,13 @@ CascadeProcessVariables (
StrnCatGrow (&FoundVarName, &NameSize, PrevName, 0);
} else {
FoundVarName = AllocateZeroPool (sizeof (CHAR16));
NameSize = sizeof (CHAR16);
// MU_CHANGE [BEGIN] - CodeQL change
if (FoundVarName == NULL) {
return (SHELL_OUT_OF_RESOURCES);
}

// MU_CHANGE [END] - CodeQL change
NameSize = sizeof (CHAR16);
}

Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);
Expand Down Expand Up @@ -817,6 +823,12 @@ ShellCommandRunDmpStore (
// Get the Name of the variable to find
//
Name = ShellCommandLineGetRawValue (Package, 1);
// MU_CHANGE [BEGIN] - CodeQL change
if (Name == NULL) {
return (SHELL_INVALID_PARAMETER);
}

// MU_CHANGE [END] - CodeQL change

if (ShellStatus == SHELL_SUCCESS) {
if (ShellCommandLineGetFlag (Package, L"-s")) {
Expand Down
11 changes: 11 additions & 0 deletions ShellPkg/Library/UefiShellDebug1CommandsLib/EditMenuBar.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ MenuBarRefresh (
//
for (Item = MenuItems; Item != NULL && Item->Function != NULL; Item++) {
NameString = HiiGetString (gShellDebug1HiiHandle, Item->NameToken, NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (NameString == NULL) {
return EFI_INVALID_PARAMETER;
}

// MU_CHANGE [END] - CodeQL change

Width = MAX ((StrLen (NameString) + 6), 20);
if (((Col + Width) > LastCol)) {
Expand All @@ -115,7 +121,12 @@ MenuBarRefresh (
}

FunctionKeyString = HiiGetString (gShellDebug1HiiHandle, Item->FunctionKeyToken, NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (FunctionKeyString == NULL) {
return EFI_INVALID_PARAMETER;
}

// MU_CHANGE [END] - CodeQL change
ShellPrintEx ((INT32)(Col) - 1, (INT32)(Row) - 1, L"%E%s%N %H%s%N ", FunctionKeyString, NameString);

FreePool (NameString);
Expand Down
7 changes: 7 additions & 0 deletions ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ LoadEfiDriversFromRomImage (
);
if (!EFI_ERROR (Status)) {
DecompressedImageBuffer = AllocateZeroPool (DestinationSize);
// MU_CHANGE [BEGIN] - CodeQL change
if (DecompressedImageBuffer == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"loadpcirom");
return EFI_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change
if (ImageBuffer != NULL) {
Scratch = AllocateZeroPool (ScratchSize);
if (Scratch != NULL) {
Expand Down
9 changes: 8 additions & 1 deletion ShellPkg/Library/UefiShellDebug1CommandsLib/MemMap.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,14 @@ ShellCommandRunMemMap (
if (Status == EFI_BUFFER_TOO_SMALL) {
Size += SIZE_1KB;
Descriptors = AllocateZeroPool (Size);
Status = gBS->GetMemoryMap (&Size, Descriptors, &MapKey, &ItemSize, &Version);
// MU_CHANGE [BEGIN] - CodeQL change
if (Descriptors == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"memmap");
ShellStatus = SHELL_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change
Status = gBS->GetMemoryMap (&Size, Descriptors, &MapKey, &ItemSize, &Version);
}

if (EFI_ERROR (Status)) {
Expand Down
15 changes: 15 additions & 0 deletions ShellPkg/Library/UefiShellDebug1CommandsLib/SetVar.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ ShellCommandRunSetVar (
if (StringGuid != NULL) {
RStatus = StrToGuid (StringGuid, &Guid);
} else {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"setvar", StringGuid);
return SHELL_INVALID_PARAMETER;
}

Expand All @@ -438,6 +439,13 @@ ShellCommandRunSetVar (
Status = gRT->GetVariable ((CHAR16 *)VariableName, &Guid, &Attributes, &Size, Buffer);
if (Status == EFI_BUFFER_TOO_SMALL) {
Buffer = AllocateZeroPool (Size);
// MU_CHANGE [START] - CodeQL change
if (Buffer == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"setvar");
return SHELL_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change
Status = gRT->GetVariable ((CHAR16 *)VariableName, &Guid, &Attributes, &Size, Buffer);
}

Expand All @@ -459,6 +467,13 @@ ShellCommandRunSetVar (
Status = gRT->GetVariable ((CHAR16 *)VariableName, &Guid, &Attributes, &Size, Buffer);
if (Status == EFI_BUFFER_TOO_SMALL) {
Buffer = AllocateZeroPool (Size);
// MU_CHANGE [START] - CodeQL change
if (Buffer == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"setvar");
return SHELL_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change
Status = gRT->GetVariable ((CHAR16 *)VariableName, &Guid, &Attributes, &Size, Buffer);
}

Expand Down
18 changes: 18 additions & 0 deletions ShellPkg/Library/UefiShellDriver1CommandsLib/DevTree.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ ShellCommandRunDevTree (
Lang = ShellCommandLineGetValue (Package, L"-l");
if (Lang != NULL) {
Language = AllocateZeroPool (StrSize (Lang));
// MU_CHANGE [BEGIN] - CodeQL change
if (Language == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDriver1HiiHandle, L"devtree");
ShellCommandLineFreeVarList (Package);
return (SHELL_OUT_OF_RESOURCES);
}

// MU_CHANGE [END] - CodeQL change
AsciiSPrint (Language, StrSize (Lang), "%S", Lang);
} else if (!ShellCommandLineGetFlag (Package, L"-l")) {
ASSERT (Language == NULL);
Expand All @@ -212,6 +220,16 @@ ShellCommandRunDevTree (
Lang = ShellCommandLineGetRawValue (Package, 1);
HiiString = HiiGetString (gShellDriver1HiiHandle, STRING_TOKEN (STR_DEV_TREE_OUTPUT), Language);

// MU_CHANGE [BEGIN] - CodeQL change
if (HiiString == NULL) {
ASSERT (HiiString != NULL);
SHELL_FREE_NON_NULL (Language);
ShellCommandLineFreeVarList (Package);
return (SHELL_INVALID_PARAMETER);
}

// MU_CHANGE [END] - CodeQL change

if (Lang == NULL) {
for (LoopVar = 1; ; LoopVar++) {
TheHandle = ConvertHandleIndexToHandle (LoopVar);
Expand Down
Loading

0 comments on commit a05d819

Please sign in to comment.