From 1cc0af9d6de69b803189c3f86c1f80fe86caf8a6 Mon Sep 17 00:00:00 2001 From: Aaron Pop Date: Thu, 23 Oct 2025 12:57:49 -0700 Subject: [PATCH] MdeModulePkg: Fix missing NULL tests https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Critical/MissingNullTest.qhelp For items which allocate memory, or get a pointer from another structure, it is important to validate that the pointers are not null before they are dereferenced. Signed-off-by: Aaron Pop --- .../BootMaintenance.c | 12 ++++++++++-- .../BootMaintenanceManagerCustomizedUiSupport.c | 12 ++++++++++-- .../BootMaintenanceManagerUiLib/BootOption.c | 9 ++++++++- .../BootMaintenanceManagerUiLib/ConsoleOption.c | 10 +++++++++- .../BootMaintenanceManagerUiLib/UpdatePage.c | 17 +++++++++++++---- .../BootMaintenanceManagerUiLib/Variable.c | 6 +++++- 6 files changed, 55 insertions(+), 11 deletions(-) diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenance.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenance.c index 773c978c9f..8c2bf870ba 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenance.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenance.c @@ -1451,10 +1451,17 @@ CustomizeMenus ( // Allocate space for creation of UpdateData Buffer // StartOpCodeHandle = HiiAllocateOpCodeHandle (); - ASSERT (StartOpCodeHandle != NULL); + if (StartOpCodeHandle == NULL) { + ASSERT (StartOpCodeHandle != NULL); + return; + } EndOpCodeHandle = HiiAllocateOpCodeHandle (); - ASSERT (EndOpCodeHandle != NULL); + if (EndOpCodeHandle == NULL) { + ASSERT (EndOpCodeHandle != NULL); + goto Exit; + } + // // Create Hii Extend Label OpCode as the start opcode // @@ -1485,6 +1492,7 @@ CustomizeMenus ( ); HiiFreeOpCodeHandle (StartOpCodeHandle); +Exit: HiiFreeOpCodeHandle (EndOpCodeHandle); } diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c index bbc86dea18..7cfbf5aac0 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c @@ -378,10 +378,18 @@ BmmListThirdPartyDrivers ( } HiiHandles = HiiGetHiiHandles (NULL); - ASSERT (HiiHandles != NULL); + if (HiiHandles == NULL) { + ASSERT (HiiHandles != NULL); + return EFI_OUT_OF_RESOURCES; + } gHiiDriverList = AllocateZeroPool (UI_HII_DRIVER_LIST_SIZE * sizeof (UI_HII_DRIVER_INSTANCE)); - ASSERT (gHiiDriverList != NULL); + if (gHiiDriverList == NULL) { + ASSERT (gHiiDriverList != NULL); + FreePool (HiiHandles); + return EFI_OUT_OF_RESOURCES; + } + DriverListPtr = gHiiDriverList; CurrentSize = UI_HII_DRIVER_LIST_SIZE; diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootOption.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootOption.c index 73c985c169..aea573b979 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootOption.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootOption.c @@ -340,6 +340,10 @@ BOpt_GetBootOptions ( BootOption = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot); for (Index = 0; Index < BootOrderListSize / sizeof (UINT16); Index++) { + if (BootOption == NULL) { + continue; + } + // // Don't display the hidden/inactive boot option // @@ -363,7 +367,10 @@ BOpt_GetBootOptions ( } NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT); - ASSERT (NULL != NewMenuEntry); + if (NewMenuEntry == NULL) { + ASSERT (NULL != NewMenuEntry); + return EFI_OUT_OF_RESOURCES; + } NewLoadContext = (BM_LOAD_CONTEXT *)NewMenuEntry->VariableContext; diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/ConsoleOption.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/ConsoleOption.c index cc84f46592..c97c657db4 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/ConsoleOption.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/ConsoleOption.c @@ -803,6 +803,10 @@ GetConsoleMenu ( Index2 = 0; for (Index = 0; Index < AllCount; Index++) { DevicePathInst = GetNextDevicePathInstance (&MultiDevicePath, &Size); + if (DevicePathInst == NULL) { + ASSERT (DevicePathInst != NULL); + continue; + } NewMenuEntry = BOpt_CreateMenuEntry (BM_CONSOLE_CONTEXT_SELECT); if (NULL == NewMenuEntry) { @@ -813,7 +817,11 @@ GetConsoleMenu ( NewMenuEntry->OptionNumber = Index2; NewConsoleContext->DevicePath = DuplicateDevicePath (DevicePathInst); - ASSERT (NewConsoleContext->DevicePath != NULL); + if (NewConsoleContext->DevicePath == NULL) { + ASSERT (NewConsoleContext->DevicePath != NULL); + return EFI_OUT_OF_RESOURCES; + } + NewMenuEntry->DisplayString = EfiLibStrFromDatahub (NewConsoleContext->DevicePath); if (NULL == NewMenuEntry->DisplayString) { NewMenuEntry->DisplayString = UiDevicePathToStr (NewConsoleContext->DevicePath); diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/UpdatePage.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/UpdatePage.c index 84becdfd84..0cf53104b2 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/UpdatePage.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/UpdatePage.c @@ -486,7 +486,10 @@ UpdateConsolePage ( break; } - ASSERT (ConsoleCheck != NULL); + if (ConsoleCheck == NULL) { + ASSERT (ConsoleCheck != NULL); + return; + } for (Index = 0; ((Index < ConsoleMenu->MenuNumber) && \ (Index < MAX_MENU_NUMBER)); Index++) @@ -619,10 +622,16 @@ UpdateOrderPage ( break; } - ASSERT (OptionOrder != NULL); + if (OptionOrder == NULL ) { + ASSERT (OptionOrder != NULL); + return; + } OptionsOpCodeHandle = HiiAllocateOpCodeHandle (); - ASSERT (OptionsOpCodeHandle != NULL); + if (OptionsOpCodeHandle == NULL) { + ASSERT (OptionsOpCodeHandle != NULL); + return; + } NewMenuEntry = NULL; for (OptionIndex = 0; (OptionIndex < MAX_MENU_NUMBER && OptionOrder[OptionIndex] != 0); OptionIndex++) { @@ -635,7 +644,7 @@ UpdateOrderPage ( } } - if (BootOptionFound) { + if (BootOptionFound && (NewMenuEntry != NULL)) { HiiCreateOneOfOptionOpCode ( OptionsOpCodeHandle, NewMenuEntry->DisplayStringToken, diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/Variable.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/Variable.c index 25158f99d6..2003f65620 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/Variable.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/Variable.c @@ -197,7 +197,11 @@ Var_UpdateConsoleOption ( NewTerminalContext->DevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&Vendor ); - ASSERT (TerminalDevicePath != NULL); + if (TerminalDevicePath == NULL) { + ASSERT (TerminalDevicePath != NULL); + return EFI_OUT_OF_RESOURCES; + } + ChangeTerminalDevicePath (TerminalDevicePath, TRUE); ConDevicePath = AppendDevicePathInstance ( ConDevicePath,