]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: acpiview: DBG2: Prevent buffer overruns
authorKrzysztof Koch <krzysztof.koch@arm.com>
Thu, 1 Aug 2019 23:44:02 +0000 (16:44 -0700)
committerJaben Carsey <jaben.carsey@intel.com>
Mon, 12 Aug 2019 17:13:41 +0000 (10:13 -0700)
Modify the DBG2 table parsing logic to prevent reading past the ACPI
buffer lengths provided.

Modify the signature of the DumpDbgDeviceInfo() function to make it
consistent with the ACPI structure processing functions in other
acpiview parsers. Now, the length of the Debug Device Information
Structure is read before the entire structure is dumped.

This refactoring change makes it easier to stop reading beyond the
DBG2 table buffer if the Debug Device Information Structure Buffer
does not fit in the DBG2 buffer.

For processing the first two fields of the Debug Device Information
Structure (to get the length) a new ACPI_PARSER array is defined.

References:
- Microsoft Debug Port Table 2 (DBG2), December 10, 2015

Signed-off-by: Krzysztof Koch <krzysztof.koch@arm.com>
Reviewed-by: Alexei Fedorov <Alexei.Fedorov@arm.com>
Reviewed-by: Zhichao Gao <zhichao.gao@inte.com>
Reviewed-by: Sami Mujawar <sami.mujawar@arm.com>
ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Dbg2/Dbg2Parser.c

index c6929695a1032c57761ef85002d6c51b7800ce23..869e700b9beda4886bf7bc5ae4ced3ab9a59efa3 100644 (file)
@@ -64,10 +64,17 @@ STATIC CONST ACPI_PARSER Dbg2Parser[] = {
    (VOID**)&NumberDbgDeviceInfo, NULL, NULL}\r
 };\r
 \r
+/// An ACPI_PARSER array describing the debug device information structure\r
+/// header.\r
+STATIC CONST ACPI_PARSER DbgDevInfoHeaderParser[] = {\r
+  {L"Revision", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},\r
+  {L"Length", 2, 1, L"%d", NULL, (VOID**)&DbgDevInfoLen, NULL, NULL}\r
+};\r
+\r
 /// An ACPI_PARSER array describing the debug device information.\r
 STATIC CONST ACPI_PARSER DbgDevInfoParser[] = {\r
   {L"Revision", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},\r
-  {L"Length", 2, 1, L"%d", NULL, (VOID**)&DbgDevInfoLen, NULL, NULL},\r
+  {L"Length", 2, 1, L"%d", NULL, NULL, NULL, NULL},\r
 \r
   {L"Generic Address Registers Count", 1, 3, L"0x%x", NULL,\r
    (VOID**)&GasCount, NULL, NULL},\r
@@ -93,76 +100,91 @@ STATIC CONST ACPI_PARSER DbgDevInfoParser[] = {
 /**\r
   This function parses the debug device information structure.\r
 \r
-  @param [in]  Ptr     Pointer to the start of the buffer.\r
-  @param [out] Length  Pointer in which the length of the debug\r
-                       device information is returned.\r
+  @param [in] Ptr     Pointer to the start of the buffer.\r
+  @param [in] Length  Length of the debug device information structure.\r
 **/\r
 STATIC\r
 VOID\r
 EFIAPI\r
 DumpDbgDeviceInfo (\r
-  IN  UINT8*  Ptr,\r
-  OUT UINT32* Length\r
+  IN UINT8* Ptr,\r
+  IN UINT16 Length\r
   )\r
 {\r
   UINT16  Index;\r
-  UINT8*  DataPtr;\r
-  UINT32* AddrSize;\r
-\r
-  // Parse the debug device info to get the Length\r
-  ParseAcpi (\r
-    FALSE,\r
-    0,\r
-    "Debug Device Info",\r
-    Ptr,\r
-    3,  // Length is 2 bytes starting at offset 1\r
-    PARSER_PARAMS (DbgDevInfoParser)\r
-    );\r
+  UINT16  Offset;\r
 \r
   ParseAcpi (\r
     TRUE,\r
     2,\r
     "Debug Device Info",\r
     Ptr,\r
-    *DbgDevInfoLen,\r
+    Length,\r
     PARSER_PARAMS (DbgDevInfoParser)\r
     );\r
 \r
-  // GAS and Address Size\r
+  // GAS\r
   Index = 0;\r
-  DataPtr = Ptr + (*BaseAddrRegOffset);\r
-  AddrSize = (UINT32*)(Ptr + (*AddrSizeOffset));\r
-  while (Index < (*GasCount)) {\r
+  Offset = *BaseAddrRegOffset;\r
+  while ((Index++ < *GasCount) &&\r
+         (Offset < Length)) {\r
     PrintFieldName (4, L"BaseAddressRegister");\r
-    DumpGasStruct (DataPtr, 4, GAS_LENGTH);\r
+    Offset += (UINT16)DumpGasStruct (\r
+                        Ptr + Offset,\r
+                        4,\r
+                        Length - Offset\r
+                        );\r
+  }\r
+\r
+  // Make sure the array of address sizes corresponding to each GAS fit in the\r
+  // Debug Device Information structure\r
+  if ((*AddrSizeOffset + (*GasCount * sizeof (UINT32))) > Length) {\r
+    IncrementErrorCount ();\r
+    Print (\r
+      L"ERROR: Invalid GAS count. GasCount = %d. RemainingBufferLength = %d. " \\r
+        L"Parsing of the Debug Device Information structure aborted.\n",\r
+      *GasCount,\r
+      Length - *AddrSizeOffset\r
+      );\r
+    return;\r
+  }\r
+\r
+  // Address Size\r
+  Index = 0;\r
+  Offset = *AddrSizeOffset;\r
+  while ((Index++ < *GasCount) &&\r
+         (Offset < Length)) {\r
     PrintFieldName (4, L"Address Size");\r
-    Print (L"0x%x\n", AddrSize[Index]);\r
-    DataPtr += GAS_LENGTH;\r
-    Index++;\r
+    Print (L"0x%x\n", *((UINT32*)(Ptr + Offset)));\r
+    Offset += sizeof (UINT32);\r
   }\r
 \r
   // NameSpace String\r
   Index = 0;\r
-  DataPtr = Ptr + (*NameSpaceStringOffset);\r
+  Offset = *NameSpaceStringOffset;\r
   PrintFieldName (4, L"NameSpace String");\r
-  while (Index < (*NameSpaceStringLength)) {\r
-    Print (L"%c", DataPtr[Index++]);\r
+  while ((Index++ < *NameSpaceStringLength) &&\r
+         (Offset < Length)) {\r
+    Print (L"%c", *(Ptr + Offset));\r
+    Offset++;\r
   }\r
   Print (L"\n");\r
 \r
   // OEM Data\r
-  Index = 0;\r
-  DataPtr = Ptr + (*OEMDataOffset);\r
-  PrintFieldName (4, L"OEM Data");\r
-  while (Index < (*OEMDataLength)) {\r
-    Print (L"%x ", DataPtr[Index++]);\r
-    if ((Index & 7) == 0) {\r
-      Print (L"\n%-*s   ", OUTPUT_FIELD_COLUMN_WIDTH, L"");\r
+  if (*OEMDataOffset != 0) {\r
+    Index = 0;\r
+    Offset = *OEMDataOffset;\r
+    PrintFieldName (4, L"OEM Data");\r
+    while ((Index++ < *OEMDataLength) &&\r
+           (Offset < Length)) {\r
+      Print (L"%x ", *(Ptr + Offset));\r
+      if ((Index & 7) == 0) {\r
+        Print (L"\n%-*s   ", OUTPUT_FIELD_COLUMN_WIDTH, L"");\r
+      }\r
+      Offset++;\r
     }\r
+    Print (L"\n");\r
   }\r
-  Print (L"\n");\r
-\r
-  *Length = *DbgDevInfoLen;\r
 }\r
 \r
 /**\r
@@ -187,8 +209,7 @@ ParseAcpiDbg2 (
   )\r
 {\r
   UINT32 Offset;\r
-  UINT32 DbgDeviceInfoLength;\r
-  UINT8* DevInfoPtr;\r
+  UINT32 Index;\r
 \r
   if (!Trace) {\r
     return;\r
@@ -202,14 +223,36 @@ ParseAcpiDbg2 (
              AcpiTableLength,\r
              PARSER_PARAMS (Dbg2Parser)\r
              );\r
-  DevInfoPtr = Ptr + Offset;\r
 \r
-  while (Offset < AcpiTableLength) {\r
-    DumpDbgDeviceInfo (\r
-      DevInfoPtr,\r
-      &DbgDeviceInfoLength\r
+  Offset = *OffsetDbgDeviceInfo;\r
+  Index = 0;\r
+\r
+  while (Index++ < *NumberDbgDeviceInfo) {\r
+\r
+    // Parse the Debug Device Information Structure header to obtain Length\r
+    ParseAcpi (\r
+      FALSE,\r
+      0,\r
+      NULL,\r
+      Ptr + Offset,\r
+      AcpiTableLength - Offset,\r
+      PARSER_PARAMS (DbgDevInfoHeaderParser)\r
       );\r
-    Offset += DbgDeviceInfoLength;\r
-    DevInfoPtr += DbgDeviceInfoLength;\r
+\r
+    // Make sure the Debug Device Information structure lies inside the table.\r
+    if ((Offset + *DbgDevInfoLen) > AcpiTableLength) {\r
+      IncrementErrorCount ();\r
+      Print (\r
+        L"ERROR: Invalid Debug Device Information structure length. " \\r
+          L"DbgDevInfoLen = %d. RemainingTableBufferLength = %d. " \\r
+          L"DBG2 parsing aborted.\n",\r
+        *DbgDevInfoLen,\r
+        AcpiTableLength - Offset\r
+        );\r
+      return;\r
+    }\r
+\r
+    DumpDbgDeviceInfo (Ptr + Offset, (*DbgDevInfoLen));\r
+    Offset += (*DbgDevInfoLen);\r
   }\r
 }\r