Skip to content

Commit

Permalink
MdeModulePkg: More CodeQL fixes (#319)
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 d859369 commit bf3dfbe
Show file tree
Hide file tree
Showing 30 changed files with 938 additions and 118 deletions.
29 changes: 29 additions & 0 deletions MdeModulePkg/Bus/Pci/PciBusDxe/PciResourceSupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,35 @@ CreateResourceMap (
PciResUsageTypical
);

// MU_CHANGE [BEGIN] - CodeQL change
if ((IoBridge == NULL) || (Mem32Bridge == NULL) || (PMem32Bridge == NULL) ||
(Mem64Bridge == NULL) || (PMem64Bridge == NULL))
{
if (IoBridge != NULL) {
FreePool (IoBridge);
}

if (Mem32Bridge != NULL) {
FreePool (Mem32Bridge);
}

if (PMem32Bridge != NULL) {
FreePool (PMem32Bridge);
}

if (Mem64Bridge != NULL) {
FreePool (Mem64Bridge);
}

if (PMem64Bridge != NULL) {
FreePool (PMem64Bridge);
}

return;
}

// MU_CHANGE [END] - CodeQL change

//
// Recursively create resource map on this bridge
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ USBMouseAbsolutePointerDriverBindingStart (
}

UsbMouseAbsolutePointerDevice = AllocateZeroPool (sizeof (USB_MOUSE_ABSOLUTE_POINTER_DEV));
ASSERT (UsbMouseAbsolutePointerDevice != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (UsbMouseAbsolutePointerDevice == NULL) {
ASSERT (UsbMouseAbsolutePointerDevice != NULL);
Status = EFI_OUT_OF_RESOURCES;
goto ErrorExit;
}

// MU_CHANGE [END] - CodeQL change

UsbMouseAbsolutePointerDevice->UsbIo = UsbIo;
UsbMouseAbsolutePointerDevice->Signature = USB_MOUSE_ABSOLUTE_POINTER_DEV_SIGNATURE;
Expand Down Expand Up @@ -631,7 +638,14 @@ InitializeUsbMouseDevice (
}

ReportDesc = AllocateZeroPool (MouseHidDesc->HidClassDesc[0].DescriptorLength);
ASSERT (ReportDesc != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (ReportDesc == NULL) {
ASSERT (ReportDesc != NULL);
FreePool (Buf);
return EFI_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change

Status = UsbGetReportDescriptor (
UsbIo,
Expand Down
17 changes: 15 additions & 2 deletions MdeModulePkg/Core/Dxe/Dispatcher/Dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,13 @@ FvIsBeingProcessed (
}

KnownHandle = AllocateZeroPool (sizeof (KNOWN_HANDLE));
ASSERT (KnownHandle != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (KnownHandle == NULL) {
ASSERT (KnownHandle != NULL);
return NULL;
}

// MU_CHANGE [END] - CodeQL change

KnownHandle->Signature = KNOWN_HANDLE_SIGNATURE;
KnownHandle->Handle = FvHandle;
Expand Down Expand Up @@ -852,6 +858,7 @@ CoreFvToDevicePath (
@retval EFI_ALREADY_STARTED The driver has already been started. Only one
DriverName may be active in the system at any one
time.
@retval EFI_OUT_OF_RESOURCES If memory could not be allocated for the DriverEntry. // MU_CHANGE - CodeQL change
**/
EFI_STATUS
Expand All @@ -869,7 +876,13 @@ CoreAddToDriverList (
// NULL or FALSE.
//
DriverEntry = AllocateZeroPool (sizeof (EFI_CORE_DRIVER_ENTRY));
ASSERT (DriverEntry != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (DriverEntry == NULL) {
ASSERT (DriverEntry != NULL);
return EFI_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change
if (Type == EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE) {
DriverEntry->IsFvImage = TRUE;
}
Expand Down
7 changes: 6 additions & 1 deletion MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,14 @@ PeCoffEmuProtocolNotify (
continue;
}

// MU_CHANGE [BEGIN] - CodeQL change
Entry = AllocateZeroPool (sizeof (*Entry));
ASSERT (Entry != NULL);
if (Entry == NULL) {
ASSERT (Entry != NULL);
break;
}

// MU_CHANGE [END] - CodeQL change
Entry->Emulator = Emulator;
Entry->MachineType = Entry->Emulator->MachineType;

Expand Down
17 changes: 15 additions & 2 deletions MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ InstallMemoryAttributesTable (

do {
MemoryMap = AllocatePool (MemoryMapSize);
ASSERT (MemoryMap != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (MemoryMap == NULL) {
ASSERT (MemoryMap != NULL);
return;
}

// MU_CHANGE [END] - CodeQL change

Status = CoreGetMemoryMapWithSeparatedImageSection (
&MemoryMapSize,
Expand Down Expand Up @@ -178,7 +184,14 @@ InstallMemoryAttributesTable (
// Allocate MemoryAttributesTable
//
MemoryAttributesTable = AllocatePool (sizeof (EFI_MEMORY_ATTRIBUTES_TABLE) + DescriptorSize * RuntimeEntryCount);
ASSERT (MemoryAttributesTable != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (MemoryAttributesTable == NULL) {
ASSERT (MemoryAttributesTable != NULL);
FreePool (MemoryMapStart);
return;
}

// MU_CHANGE [END] - CodeQL change
MemoryAttributesTable->Version = EFI_MEMORY_ATTRIBUTES_TABLE_VERSION;
MemoryAttributesTable->NumberOfEntries = RuntimeEntryCount;
MemoryAttributesTable->DescriptorSize = (UINT32)DescriptorSize;
Expand Down
8 changes: 7 additions & 1 deletion MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,13 @@ InitializeDxeNxMemoryProtectionPolicy (
ASSERT (Status == EFI_BUFFER_TOO_SMALL);
do {
MemoryMap = (EFI_MEMORY_DESCRIPTOR *)AllocatePool (MemoryMapSize);
ASSERT (MemoryMap != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (MemoryMap == NULL) {
ASSERT (MemoryMap != NULL);
return;
}

// MU_CHANGE [END] - CodeQL change
Status = gBS->GetMemoryMap (
&MemoryMapSize,
MemoryMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,13 @@ CreateGuidedExtractionRpnEvent (
// Allocate new event structure and context
//
Context = AllocatePool (sizeof (RPN_EVENT_CONTEXT));
ASSERT (Context != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (Context == NULL) {
ASSERT (Context != NULL);
return;
}

// MU_CHANGE [END] - CodeQL change

Context->ChildNode = ChildNode;
Context->ParentStream = ParentStream;
Expand Down
3 changes: 2 additions & 1 deletion MdeModulePkg/Library/DxeCapsuleLibFmp/CapsuleOnDisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ GetBootOptionInOrder (
// Second get BootOption from "BootOrder"
//
BootOrderOptionBuf = EfiBootManagerGetLoadOptions (&BootOrderCount, LoadOptionTypeBoot);
if ((BootNextCount == 0) && (BootOrderCount == 0)) {
// MU_CHANGE - CodeQL change
if (((BootNextCount == 0) && (BootOrderCount == 0)) || (BootOrderOptionBuf == NULL)) {
return EFI_NOT_FOUND;
}

Expand Down
32 changes: 28 additions & 4 deletions MdeModulePkg/Library/UefiBootManagerLib/BmBootDescription.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,13 @@ BmGetDescriptionFromDiskInfo (
);
if (!EFI_ERROR (Status)) {
Description = AllocateZeroPool ((ModelNameLength + SerialNumberLength + 2) * sizeof (CHAR16));
ASSERT (Description != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (Description == NULL) {
ASSERT (Description != NULL);
return NULL;
}

// MU_CHANGE [END] - CodeQL change
for (Index = 0; Index + 1 < ModelNameLength; Index += 2) {
Description[Index] = (CHAR16)IdentifyData.ModelName[Index + 1];
Description[Index + 1] = (CHAR16)IdentifyData.ModelName[Index];
Expand Down Expand Up @@ -206,7 +212,13 @@ BmGetDescriptionFromDiskInfo (
);
if (!EFI_ERROR (Status)) {
Description = AllocateZeroPool ((VENDOR_IDENTIFICATION_LENGTH + PRODUCT_IDENTIFICATION_LENGTH + 2) * sizeof (CHAR16));
ASSERT (Description != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (Description == NULL) {
ASSERT (Description != NULL);
return NULL;
}

// MU_CHANGE [END] - CodeQL change

//
// Per SCSI spec, EFI_SCSI_INQUIRY_DATA.Reserved_5_95[3 - 10] save the Verdor identification
Expand Down Expand Up @@ -336,7 +348,13 @@ BmGetUsbDescription (

DescMaxSize = StrSize (Manufacturer) + StrSize (Product) + StrSize (SerialNumber);
Description = AllocateZeroPool (DescMaxSize);
ASSERT (Description != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (Description == NULL) {
ASSERT (Description != NULL);
return NULL;
}

// MU_CHANGE [END] - CodeQL change
StrCatS (Description, DescMaxSize/sizeof (CHAR16), Manufacturer);
StrCatS (Description, DescMaxSize/sizeof (CHAR16), L" ");

Expand Down Expand Up @@ -890,7 +908,13 @@ BmMakeBootOptionDescriptionUnique (
}

Visited = AllocateZeroPool (sizeof (BOOLEAN) * BootOptionCount);
ASSERT (Visited != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (Visited == NULL) {
ASSERT (Visited != NULL);
return;
}

// MU_CHANGE [END] - CodeQL change

for (Base = 0; Base < BootOptionCount; Base++) {
if (!Visited[Base]) {
Expand Down
5 changes: 5 additions & 0 deletions MdeModulePkg/Library/UefiBootManagerLib/BmDriverHealth.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ BmDisplayMessages (
DriverHealthInfo->ControllerHandle,
DriverHealthInfo->ChildHandle
);
// MU_CHANGE [BEGIN] - CodeQL change
if (ControllerName == NULL) {
return;
}

// MU_CHANGE [END] - CodeQL change
DEBUG ((DEBUG_INFO, "Controller: %s\n", ControllerName));
Print (L"Controller: %s\n", ControllerName);
for (Index = 0; DriverHealthInfo->MessageList[Index].HiiHandle != NULL; Index++) {
Expand Down
14 changes: 13 additions & 1 deletion MdeModulePkg/Library/UefiBootManagerLib/BmHotkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,19 @@ BmProcessKeyOption (

for (Index = 0; Index < KeyShiftStateCount; Index++) {
Hotkey = AllocateZeroPool (sizeof (BM_HOTKEY));
ASSERT (Hotkey != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (Hotkey == NULL) {
ASSERT (Hotkey != NULL);
if (Handles != NULL) {
FreePool (Handles);
}

EfiReleaseLock (&mBmHotkeyLock);

return EFI_OUT_OF_RESOURCES;
}

// MU_CHANGE [END] - CodeQL change

Hotkey->Signature = BM_HOTKEY_SIGNATURE;
Hotkey->BootOption = KeyOption->BootOption;
Expand Down
8 changes: 7 additions & 1 deletion MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ BmForEachVariable (

NameSize = sizeof (CHAR16);
Name = AllocateZeroPool (NameSize);
ASSERT (Name != NULL);
// MU_CHANGE [BEGIN] - CodeQL change
if (Name == NULL) {
ASSERT (Name != NULL);
return;
}

// MU_CHANGE [END] - CodeQL change
while (TRUE) {
NewNameSize = NameSize;
Status = gRT->GetNextVariableName (&NewNameSize, Name, &Guid);
Expand Down
Loading

0 comments on commit bf3dfbe

Please sign in to comment.