MdePkg/UefiDevicePathLib: Fix AcpiEx print logic

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4555

Add logic that checks if the code doesn't overflow
ACPI_EXTENDED_HID_DEVICE_PATH node when searching for optional
strings. If the string is not provided in the device path node
default value of "\0" is used.

Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@bysoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Signed-off-by: Mateusz Albecki <mateusz.albecki@intel.com>
Reviewed-by: Michael D Kinney <Michael.d.kinney@intel.com>
This commit is contained in:
Mateusz Albecki 2023-09-26 11:11:49 +02:00 committed by mergify[bot]
parent 8abbf6d87e
commit 96ed60dfd7
1 changed files with 44 additions and 26 deletions

View File

@ -418,23 +418,41 @@ DevPathToTextAcpiEx (
) )
{ {
ACPI_EXTENDED_HID_DEVICE_PATH *AcpiEx; ACPI_EXTENDED_HID_DEVICE_PATH *AcpiEx;
CHAR8 *HIDStr;
CHAR8 *UIDStr;
CHAR8 *CIDStr;
CHAR16 HIDText[11]; CHAR16 HIDText[11];
CHAR16 CIDText[11]; CHAR16 CIDText[11];
UINTN CurrentLength;
CHAR8 *CurrentPos;
UINTN NextStringOffset;
CHAR8 *Strings[3];
UINT8 HidStrIndex;
UINT8 UidStrIndex;
UINT8 CidStrIndex;
UINT8 StrIndex;
AcpiEx = DevPath; HidStrIndex = 0;
HIDStr = (CHAR8 *)(((UINT8 *)AcpiEx) + sizeof (ACPI_EXTENDED_HID_DEVICE_PATH)); UidStrIndex = 1;
UIDStr = HIDStr + AsciiStrLen (HIDStr) + 1; CidStrIndex = 2;
CIDStr = UIDStr + AsciiStrLen (UIDStr) + 1; AcpiEx = DevPath;
Strings[HidStrIndex] = NULL;
Strings[UidStrIndex] = NULL;
Strings[CidStrIndex] = NULL;
CurrentLength = sizeof (ACPI_EXTENDED_HID_DEVICE_PATH);
CurrentPos = (CHAR8 *)(((UINT8 *)AcpiEx) + sizeof (ACPI_EXTENDED_HID_DEVICE_PATH));
StrIndex = 0;
while (CurrentLength < AcpiEx->Header.Length[0] && StrIndex < ARRAY_SIZE (Strings)) {
Strings[StrIndex] = CurrentPos;
NextStringOffset = AsciiStrLen (CurrentPos) + 1;
CurrentLength += NextStringOffset;
CurrentPos += NextStringOffset;
StrIndex++;
}
if (DisplayOnly) { if (DisplayOnly) {
if ((EISA_ID_TO_NUM (AcpiEx->HID) == 0x0A03) || if ((EISA_ID_TO_NUM (AcpiEx->HID) == 0x0A03) ||
((EISA_ID_TO_NUM (AcpiEx->CID) == 0x0A03) && (EISA_ID_TO_NUM (AcpiEx->HID) != 0x0A08))) ((EISA_ID_TO_NUM (AcpiEx->CID) == 0x0A03) && (EISA_ID_TO_NUM (AcpiEx->HID) != 0x0A08)))
{ {
if (AcpiEx->UID == 0) { if (Strings[UidStrIndex] != NULL) {
UefiDevicePathLibCatPrint (Str, L"PciRoot(%a)", UIDStr); UefiDevicePathLibCatPrint (Str, L"PciRoot(%a)", Strings[UidStrIndex]);
} else { } else {
UefiDevicePathLibCatPrint (Str, L"PciRoot(0x%x)", AcpiEx->UID); UefiDevicePathLibCatPrint (Str, L"PciRoot(0x%x)", AcpiEx->UID);
} }
@ -443,8 +461,8 @@ DevPathToTextAcpiEx (
} }
if ((EISA_ID_TO_NUM (AcpiEx->HID) == 0x0A08) || (EISA_ID_TO_NUM (AcpiEx->CID) == 0x0A08)) { if ((EISA_ID_TO_NUM (AcpiEx->HID) == 0x0A08) || (EISA_ID_TO_NUM (AcpiEx->CID) == 0x0A08)) {
if (AcpiEx->UID == 0) { if (Strings[UidStrIndex] != NULL) {
UefiDevicePathLibCatPrint (Str, L"PcieRoot(%a)", UIDStr); UefiDevicePathLibCatPrint (Str, L"PcieRoot(%a)", Strings[UidStrIndex]);
} else { } else {
UefiDevicePathLibCatPrint (Str, L"PcieRoot(0x%x)", AcpiEx->UID); UefiDevicePathLibCatPrint (Str, L"PcieRoot(0x%x)", AcpiEx->UID);
} }
@ -475,7 +493,10 @@ DevPathToTextAcpiEx (
(AcpiEx->CID >> 16) & 0xFFFF (AcpiEx->CID >> 16) & 0xFFFF
); );
if ((*HIDStr == '\0') && (*CIDStr == '\0') && (*UIDStr != '\0')) { if (((Strings[HidStrIndex] != NULL) && (*Strings[HidStrIndex] == '\0')) &&
((Strings[CidStrIndex] != NULL) && (*Strings[CidStrIndex] == '\0')) &&
((Strings[UidStrIndex] != NULL) && (*Strings[UidStrIndex] != '\0')))
{
// //
// use AcpiExp() // use AcpiExp()
// //
@ -484,7 +505,7 @@ DevPathToTextAcpiEx (
Str, Str,
L"AcpiExp(%s,0,%a)", L"AcpiExp(%s,0,%a)",
HIDText, HIDText,
UIDStr Strings[UidStrIndex]
); );
} else { } else {
UefiDevicePathLibCatPrint ( UefiDevicePathLibCatPrint (
@ -492,28 +513,25 @@ DevPathToTextAcpiEx (
L"AcpiExp(%s,%s,%a)", L"AcpiExp(%s,%s,%a)",
HIDText, HIDText,
CIDText, CIDText,
UIDStr Strings[UidStrIndex]
); );
} }
} else { } else {
if (DisplayOnly) { if (DisplayOnly) {
// if (Strings[HidStrIndex] != NULL) {
// display only UefiDevicePathLibCatPrint (Str, L"AcpiEx(%a,", Strings[HidStrIndex]);
//
if (AcpiEx->HID == 0) {
UefiDevicePathLibCatPrint (Str, L"AcpiEx(%a,", HIDStr);
} else { } else {
UefiDevicePathLibCatPrint (Str, L"AcpiEx(%s,", HIDText); UefiDevicePathLibCatPrint (Str, L"AcpiEx(%s,", HIDText);
} }
if (AcpiEx->CID == 0) { if (Strings[CidStrIndex] != NULL) {
UefiDevicePathLibCatPrint (Str, L"%a,", CIDStr); UefiDevicePathLibCatPrint (Str, L"%a,", Strings[CidStrIndex]);
} else { } else {
UefiDevicePathLibCatPrint (Str, L"%s,", CIDText); UefiDevicePathLibCatPrint (Str, L"%s,", CIDText);
} }
if (AcpiEx->UID == 0) { if (Strings[UidStrIndex] != NULL) {
UefiDevicePathLibCatPrint (Str, L"%a)", UIDStr); UefiDevicePathLibCatPrint (Str, L"%a)", Strings[UidStrIndex]);
} else { } else {
UefiDevicePathLibCatPrint (Str, L"0x%x)", AcpiEx->UID); UefiDevicePathLibCatPrint (Str, L"0x%x)", AcpiEx->UID);
} }
@ -524,9 +542,9 @@ DevPathToTextAcpiEx (
HIDText, HIDText,
CIDText, CIDText,
AcpiEx->UID, AcpiEx->UID,
HIDStr, Strings[HidStrIndex] != NULL ? Strings[HidStrIndex] : '\0',
CIDStr, Strings[CidStrIndex] != NULL ? Strings[CidStrIndex] : '\0',
UIDStr Strings[UidStrIndex] != NULL ? Strings[UidStrIndex] : '\0'
); );
} }
} }