UefiCpuPkg/MpInitLibUp: Fix #UD exception due to NULL pointer deref

In MpInitLibUp, MpInitLibGetNumberOfProcessors() unconditionally
dereferences the NumberOfProcessors and NumberOfEnabledProcessors
parameters. However, the API specification marks both as OPTIONAL.

When callers (like InitializeExceptionStackSwitchHandlers in
UefiCpuPkg/CpuMpPei/CpuMpPei.c) pass NULL for NumberOfEnabledProcessors,
GCC's Link-Time Optimization (LTO) detects an unconditional NULL pointer
dereference. Since this is Undefined Behavior, GCC emits a 'ud2'
(Invalid Opcode) instruction at the dereference site. This leads to an
unexpected #UD exception during the boot process of uniprocessor guests
like TDX VMs.

This patch fixes the issue by adding appropriate NULL checks before
dereferencing the pointers. It also returns EFI_INVALID_PARAMETER if
both arguments are NULL, ensuring compliance with the MpInitLib
specification.

Signed-off-by: Changyuan Lyu <changyuanl@google.com>
This commit is contained in:
Changyuan Lyu
2026-03-02 02:05:57 +00:00
committed by mergify[bot]
parent 58c9ba24d4
commit c422b3b97b
+12 -2
View File
@@ -68,8 +68,18 @@ MpInitLibGetNumberOfProcessors (
OUT UINTN *NumberOfEnabledProcessors OPTIONAL
)
{
*NumberOfProcessors = 1;
*NumberOfEnabledProcessors = 1;
if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {
return EFI_INVALID_PARAMETER;
}
if (NumberOfProcessors != NULL) {
*NumberOfProcessors = 1;
}
if (NumberOfEnabledProcessors != NULL) {
*NumberOfEnabledProcessors = 1;
}
return EFI_SUCCESS;
}