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

Change DumpIortNodeIdMappings() function's signature and implementation
to simplify buffer overrun prevention. Update all calls to this
function accordingly.

Modify the parser for each type of IORT node such that the offset from
the start of the node's buffer is tracked as the parsing function is
executed. Again, this change helps prevent buffer overruns.

Test that the IORT node buffer fits in the table buffer before the
node's buffer contents are dumped.

References:
- IO Remapping Table (Issue D), Platform Design Document, March 2018

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/Iort/IortParser.c

index 7c850b3813d5204775e2cc247cabf42358b25769..8912d415a755c7f892b5cd2edc532aae8964a42c 100644 (file)
@@ -247,42 +247,41 @@ STATIC CONST ACPI_PARSER IortNodePmcgParser[] = {
 /**\r
   This function parses the IORT Node Id Mapping array.\r
 \r
-  @param [in] Ptr            Pointer to the start of the IORT Table.\r
+  @param [in] Ptr            Pointer to the start of the ID mapping array.\r
+  @param [in] Length         Length of the buffer.\r
   @param [in] MappingCount   The ID Mapping count.\r
-  @param [in] MappingOffset  The offset of the ID Mapping array\r
-                             from the start of the IORT table.\r
 **/\r
 STATIC\r
 VOID\r
 DumpIortNodeIdMappings (\r
   IN UINT8* Ptr,\r
-  IN UINT32 MappingCount,\r
-  IN UINT32 MappingOffset\r
+  IN UINT32 Length,\r
+  IN UINT32 MappingCount\r
   )\r
 {\r
-  UINT8* IdMappingPtr;\r
   UINT32 Index;\r
   UINT32 Offset;\r
   CHAR8  Buffer[40];  // Used for AsciiName param of ParseAcpi\r
 \r
-  IdMappingPtr = Ptr + MappingOffset;\r
   Index = 0;\r
-  while (Index < MappingCount) {\r
+  Offset = 0;\r
+\r
+  while ((Index < MappingCount) &&\r
+         (Offset < Length)) {\r
     AsciiSPrint (\r
       Buffer,\r
       sizeof (Buffer),\r
       "ID Mapping [%d]",\r
       Index\r
       );\r
-    Offset = ParseAcpi (\r
-               TRUE,\r
-               4,\r
-               Buffer,\r
-               IdMappingPtr,\r
-               20,\r
-               PARSER_PARAMS (IortNodeIdMappingParser)\r
-               );\r
-    IdMappingPtr += Offset;\r
+    Offset += ParseAcpi (\r
+                TRUE,\r
+                4,\r
+                Buffer,\r
+                Ptr + Offset,\r
+                Length - Offset,\r
+                PARSER_PARAMS (IortNodeIdMappingParser)\r
+                );\r
     Index++;\r
   }\r
 }\r
@@ -309,8 +308,6 @@ DumpIortNodeSmmuV1V2 (
   UINT32 Offset;\r
   CHAR8  Buffer[50];  // Used for AsciiName param of ParseAcpi\r
 \r
-  UINT8* ArrayPtr;\r
-\r
   ParseAcpi (\r
     TRUE,\r
     2,\r
@@ -320,51 +317,55 @@ DumpIortNodeSmmuV1V2 (
     PARSER_PARAMS (IortNodeSmmuV1V2Parser)\r
     );\r
 \r
-  ArrayPtr = Ptr + *InterruptContextOffset;\r
+  Offset = *InterruptContextOffset;\r
   Index = 0;\r
-  while (Index < *InterruptContextCount) {\r
+\r
+  while ((Index < *InterruptContextCount) &&\r
+         (Offset < Length)) {\r
     AsciiSPrint (\r
       Buffer,\r
       sizeof (Buffer),\r
       "Context Interrupts Array [%d]",\r
       Index\r
       );\r
-    Offset = ParseAcpi (\r
-               TRUE,\r
-               4,\r
-               Buffer,\r
-               ArrayPtr,\r
-               8,\r
-               PARSER_PARAMS (InterruptArrayParser)\r
-               );\r
-    ArrayPtr += Offset;\r
+    Offset += ParseAcpi (\r
+                TRUE,\r
+                4,\r
+                Buffer,\r
+                Ptr + Offset,\r
+                Length - Offset,\r
+                PARSER_PARAMS (InterruptArrayParser)\r
+                );\r
     Index++;\r
   }\r
 \r
-  ArrayPtr = Ptr + *PmuInterruptOffset;\r
+  Offset = *PmuInterruptOffset;\r
   Index = 0;\r
-  while (Index < *PmuInterruptCount) {\r
+\r
+  while ((Index < *PmuInterruptCount) &&\r
+         (Offset < Length)) {\r
     AsciiSPrint (\r
       Buffer,\r
       sizeof (Buffer),\r
       "PMU Interrupts Array [%d]",\r
       Index\r
       );\r
-    Offset = ParseAcpi (\r
-               TRUE,\r
-               4,\r
-               Buffer,\r
-               ArrayPtr,\r
-               8,\r
-               PARSER_PARAMS (InterruptArrayParser)\r
-               );\r
-    ArrayPtr += Offset;\r
+    Offset += ParseAcpi (\r
+                TRUE,\r
+                4,\r
+                Buffer,\r
+                Ptr + Offset,\r
+                Length - Offset,\r
+                PARSER_PARAMS (InterruptArrayParser)\r
+                );\r
     Index++;\r
   }\r
 \r
-  if (*IortIdMappingCount != 0) {\r
-    DumpIortNodeIdMappings (Ptr, MappingCount, MappingOffset);\r
-  }\r
+  DumpIortNodeIdMappings (\r
+    Ptr + MappingOffset,\r
+    Length - MappingOffset,\r
+    MappingCount\r
+    );\r
 }\r
 \r
 /**\r
@@ -394,9 +395,11 @@ DumpIortNodeSmmuV3 (
     PARSER_PARAMS (IortNodeSmmuV3Parser)\r
     );\r
 \r
-  if (*IortIdMappingCount != 0) {\r
-    DumpIortNodeIdMappings (Ptr, MappingCount, MappingOffset);\r
-  }\r
+  DumpIortNodeIdMappings (\r
+    Ptr + MappingOffset,\r
+    Length - MappingOffset,\r
+    MappingCount\r
+    );\r
 }\r
 \r
 /**\r
@@ -414,40 +417,40 @@ DumpIortNodeIts (
 {\r
   UINT32 Offset;\r
   UINT32 Index;\r
-  UINT8* ItsIdPtr;\r
   CHAR8  Buffer[80];  // Used for AsciiName param of ParseAcpi\r
 \r
   Offset = ParseAcpi (\r
-             TRUE,\r
-             2,\r
-             "ITS Node",\r
-             Ptr,\r
-             Length,\r
-             PARSER_PARAMS (IortNodeItsParser)\r
-             );\r
+            TRUE,\r
+            2,\r
+            "ITS Node",\r
+            Ptr,\r
+            Length,\r
+            PARSER_PARAMS (IortNodeItsParser)\r
+            );\r
 \r
-  ItsIdPtr = Ptr + Offset;\r
   Index = 0;\r
-  while (Index < *ItsCount) {\r
+\r
+  while ((Index < *ItsCount) &&\r
+         (Offset < Length)) {\r
     AsciiSPrint (\r
       Buffer,\r
       sizeof (Buffer),\r
       "GIC ITS Identifier Array [%d]",\r
       Index\r
       );\r
-    Offset = ParseAcpi (\r
-               TRUE,\r
-               4,\r
-               Buffer,\r
-               ItsIdPtr,\r
-               4,\r
-               PARSER_PARAMS (ItsIdParser)\r
-               );\r
-    ItsIdPtr += Offset;\r
+    Offset += ParseAcpi (\r
+                TRUE,\r
+                4,\r
+                Buffer,\r
+                Ptr + Offset,\r
+                Length - Offset,\r
+                PARSER_PARAMS (ItsIdParser)\r
+                );\r
     Index++;\r
   }\r
 \r
   // Note: ITS does not have the ID Mappings Array\r
+\r
 }\r
 \r
 /**\r
@@ -470,8 +473,6 @@ DumpIortNodeNamedComponent (
 {\r
   UINT32 Offset;\r
   UINT32 Index;\r
-  UINT8* DeviceNamePtr;\r
-  UINT32 DeviceNameLength;\r
 \r
   Offset = ParseAcpi (\r
              TRUE,\r
@@ -482,19 +483,22 @@ DumpIortNodeNamedComponent (
              PARSER_PARAMS (IortNodeNamedComponentParser)\r
              );\r
 \r
-  DeviceNamePtr = Ptr + Offset;\r
   // Estimate the Device Name length\r
-  DeviceNameLength = Length - Offset - (MappingCount * 20);\r
   PrintFieldName (2, L"Device Object Name");\r
   Index = 0;\r
-  while ((Index < DeviceNameLength) && (DeviceNamePtr[Index] != 0)) {\r
-    Print (L"%c", DeviceNamePtr[Index++]);\r
+\r
+  while ((*(Ptr + Offset) != 0) &&\r
+         (Offset < Length)) {\r
+    Print (L"%c", *(Ptr + Offset));\r
+    Offset++;\r
   }\r
   Print (L"\n");\r
 \r
-  if (*IortIdMappingCount != 0) {\r
-    DumpIortNodeIdMappings (Ptr, MappingCount, MappingOffset);\r
-  }\r
+  DumpIortNodeIdMappings (\r
+    Ptr + MappingOffset,\r
+    Length - MappingOffset,\r
+    MappingCount\r
+    );\r
 }\r
 \r
 /**\r
@@ -524,9 +528,11 @@ DumpIortNodeRootComplex (
     PARSER_PARAMS (IortNodeRootComplexParser)\r
     );\r
 \r
-  if (*IortIdMappingCount != 0) {\r
-    DumpIortNodeIdMappings (Ptr, MappingCount, MappingOffset);\r
-  }\r
+  DumpIortNodeIdMappings (\r
+    Ptr + MappingOffset,\r
+    Length - MappingOffset,\r
+    MappingCount\r
+    );\r
 }\r
 \r
 /**\r
@@ -554,11 +560,13 @@ DumpIortNodePmcg (
     Ptr,\r
     Length,\r
     PARSER_PARAMS (IortNodePmcgParser)\r
-  );\r
+    );\r
 \r
-  if (*IortIdMappingCount != 0) {\r
-    DumpIortNodeIdMappings (Ptr, MappingCount, MappingOffset);\r
-  }\r
+  DumpIortNodeIdMappings (\r
+    Ptr + MappingOffset,\r
+    Length - MappingOffset,\r
+    MappingCount\r
+    );\r
 }\r
 \r
 /**\r
@@ -605,23 +613,34 @@ ParseAcpiIort (
     AcpiTableLength,\r
     PARSER_PARAMS (IortParser)\r
     );\r
+\r
   Offset = *IortNodeOffset;\r
   NodePtr = Ptr + Offset;\r
   Index = 0;\r
 \r
-  while ((Index < *IortNodeCount) && (Offset < AcpiTableLength)) {\r
+  // Parse the specified number of IORT nodes or the IORT table buffer length.\r
+  // Whichever is minimum.\r
+  while ((Index++ < *IortNodeCount) &&\r
+         (Offset < AcpiTableLength)) {\r
     // Parse the IORT Node Header\r
     ParseAcpi (\r
       FALSE,\r
       0,\r
       "IORT Node Header",\r
       NodePtr,\r
-      16,\r
+      AcpiTableLength - Offset,\r
       PARSER_PARAMS (IortNodeHeaderParser)\r
       );\r
-    if (*IortNodeLength == 0) {\r
+\r
+    // Make sure the IORT Node is inside the table\r
+    if ((Offset + (*IortNodeLength)) > AcpiTableLength) {\r
       IncrementErrorCount ();\r
-      Print (L"ERROR: Parser error. Invalid table data.\n");\r
+      Print (\r
+        L"ERROR: Invalid IORT node length. IortNodeLength = %d. " \\r
+          L"RemainingTableBufferLength = %d. IORT parsing aborted.\n",\r
+        *IortNodeLength,\r
+        AcpiTableLength - Offset\r
+        );\r
       return;\r
     }\r
 \r