]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: acpiview: Prevent infinite loop if structure length is 0
authorKrzysztof Koch <krzysztof.koch@arm.com>
Wed, 19 Feb 2020 10:23:38 +0000 (18:23 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Thu, 20 Feb 2020 09:27:00 +0000 (09:27 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2534

Extend validation of ACPI structure lengths which are read from the
ACPI table being parsed. Additionally check if the structure 'Length'
field value is positive. If not, stop parsing the faulting table.

Some ACPI tables define internal structures of variable size. The
'Length' field inside the substructure is used to update a pointer used
for table traversal. If the byte-length of the structure is equal to 0,
acpiview can enter an infinite loop. This condition can occur if, for
example, the zero-allocated ACPI table buffer is not fully populated.
This is typically a bug on the ACPI table writer side.

In short, this method helps acpiview recover gracefully from a
zero-valued ACPI structure length.

Signed-off-by: Krzysztof Koch <krzysztof.koch@arm.com>
Reviewed-by: Sami Mujawar <sami.mujawar@arm.com>
Reviewed-by: Zhichao Gao <zhichao.gao@intel.com>
ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Dbg2/Dbg2Parser.c
ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Gtdt/GtdtParser.c
ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Iort/IortParser.c
ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Madt/MadtParser.c
ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Pptt/PpttParser.c
ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Srat/SratParser.c

index 0f730a306a94329a23fbaf54b59f1833b44616ba..9df111ecaa7d7a703a13a39c243ed78b9f12ee97 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   DBG2 table parser\r
 \r
-  Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.\r
+  Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.\r
   SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
   @par Reference(s):\r
@@ -282,15 +282,16 @@ ParseAcpiDbg2 (
       return;\r
     }\r
 \r
-    // Make sure the Debug Device Information structure lies inside the table.\r
-    if ((Offset + *DbgDevInfoLen) > AcpiTableLength) {\r
+    // Validate Debug Device Information Structure length\r
+    if ((*DbgDevInfoLen == 0) ||\r
+        ((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
+        L"ERROR: Invalid Debug Device Information Structure length. " \\r
+          L"Length = %d. Offset = %d. AcpiTableLength = %d.\n",\r
         *DbgDevInfoLen,\r
-        AcpiTableLength - Offset\r
+        Offset,\r
+        AcpiTableLength\r
         );\r
       return;\r
     }\r
index 699a55b549ec3fa61bbd156898821055dc019199..bdd30ff45c61142c071ead63a27babab8998721b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   GTDT table parser\r
 \r
-  Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.\r
+  Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.\r
   SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
   @par Reference(s):\r
@@ -327,15 +327,16 @@ ParseAcpiGtdt (
       return;\r
     }\r
 \r
-    // Make sure the Platform Timer is inside the table.\r
-    if ((Offset + *PlatformTimerLength) > AcpiTableLength) {\r
+    // Validate Platform Timer Structure length\r
+    if ((*PlatformTimerLength == 0) ||\r
+        ((Offset + (*PlatformTimerLength)) > AcpiTableLength)) {\r
       IncrementErrorCount ();\r
       Print (\r
         L"ERROR: Invalid Platform Timer Structure length. " \\r
-          L"PlatformTimerLength = %d. RemainingTableBufferLength = %d. " \\r
-          L"GTDT parsing aborted.\n",\r
+          L"Length = %d. Offset = %d. AcpiTableLength = %d.\n",\r
         *PlatformTimerLength,\r
-        AcpiTableLength - Offset\r
+        Offset,\r
+        AcpiTableLength\r
         );\r
       return;\r
     }\r
index 9d5d937c7b2c19945ca2ad3eba644bdfc09cc3f6..9a006a01448b897865cd7cd85651c816933acf05 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   IORT table parser\r
 \r
-  Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.\r
+  Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.\r
   SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
   @par Reference(s):\r
@@ -687,14 +687,16 @@ ParseAcpiIort (
       return;\r
     }\r
 \r
-    // Make sure the IORT Node is inside the table\r
-    if ((Offset + (*IortNodeLength)) > AcpiTableLength) {\r
+    // Validate IORT Node length\r
+    if ((*IortNodeLength == 0) ||\r
+        ((Offset + (*IortNodeLength)) > AcpiTableLength)) {\r
       IncrementErrorCount ();\r
       Print (\r
-        L"ERROR: Invalid IORT node length. IortNodeLength = %d. " \\r
-          L"RemainingTableBufferLength = %d. IORT parsing aborted.\n",\r
+        L"ERROR: Invalid IORT Node length. " \\r
+          L"Length = %d. Offset = %d. AcpiTableLength = %d.\n",\r
         *IortNodeLength,\r
-        AcpiTableLength - Offset\r
+        Offset,\r
+        AcpiTableLength\r
         );\r
       return;\r
     }\r
index 438905cb24f58b8b82e8fe61280e72f765d578d8..f85d2b36532cfc5db36fe7bef9830cccc64969cc 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   MADT table parser\r
 \r
-  Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.\r
+  Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.\r
   SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
   @par Reference(s):\r
@@ -273,28 +273,16 @@ ParseAcpiMadt (
       return;\r
     }\r
 \r
-    // Make sure forward progress is made.\r
-    if (*MadtInterruptControllerLength < 2) {\r
+    // Validate Interrupt Controller Structure length\r
+    if ((*MadtInterruptControllerLength == 0) ||\r
+        ((Offset + (*MadtInterruptControllerLength)) > AcpiTableLength)) {\r
       IncrementErrorCount ();\r
       Print (\r
-        L"ERROR: Structure length is too small: " \\r
-          L"MadtInterruptControllerLength = %d. " \\r
-          L"MadtInterruptControllerType = %d. MADT parsing aborted.\n",\r
+        L"ERROR: Invalid Interrupt Controller Structure length. " \\r
+          L"Length = %d. Offset = %d. AcpiTableLength = %d.\n",\r
         *MadtInterruptControllerLength,\r
-        *MadtInterruptControllerType\r
-        );\r
-      return;\r
-    }\r
-\r
-    // Make sure the MADT structure lies inside the table\r
-    if ((Offset + *MadtInterruptControllerLength) > AcpiTableLength) {\r
-      IncrementErrorCount ();\r
-      Print (\r
-        L"ERROR: Invalid MADT structure length. " \\r
-          L"MadtInterruptControllerLength = %d. " \\r
-          L"RemainingTableBufferLength = %d. MADT parsing aborted.\n",\r
-        *MadtInterruptControllerLength,\r
-        AcpiTableLength - Offset\r
+        Offset,\r
+        AcpiTableLength\r
         );\r
       return;\r
     }\r
index 675ba75f02b367cd5ad9f2ac23c30ed0ab58f286..0db272c16af0ad8824c8da4c88dd409c8550112a 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   PPTT table parser\r
 \r
-  Copyright (c) 2019, ARM Limited. All rights reserved.\r
+  Copyright (c) 2019 - 2020, ARM Limited. All rights reserved.\r
   SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
   @par Reference(s):\r
@@ -425,15 +425,16 @@ ParseAcpiPptt (
       return;\r
     }\r
 \r
-    // Make sure the PPTT structure lies inside the table\r
-    if ((Offset + *ProcessorTopologyStructureLength) > AcpiTableLength) {\r
+    // Validate Processor Topology Structure length\r
+    if ((*ProcessorTopologyStructureLength == 0) ||\r
+        ((Offset + (*ProcessorTopologyStructureLength)) > AcpiTableLength)) {\r
       IncrementErrorCount ();\r
       Print (\r
-        L"ERROR: Invalid PPTT structure length. " \\r
-          L"ProcessorTopologyStructureLength = %d. " \\r
-          L"RemainingTableBufferLength = %d. PPTT parsing aborted.\n",\r
+        L"ERROR: Invalid Processor Topology Structure length. " \\r
+          L"Length = %d. Offset = %d. AcpiTableLength = %d.\n",\r
         *ProcessorTopologyStructureLength,\r
-        AcpiTableLength - Offset\r
+        Offset,\r
+        AcpiTableLength\r
         );\r
       return;\r
     }\r
index 3613900ae322483fdd3d3383de4e22ba75b2128b..6f66be68cc0bed14811a0432c61a79fd47c54890 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   SRAT table parser\r
 \r
-  Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.\r
+  Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.\r
   SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
   @par Reference(s):\r
@@ -412,14 +412,16 @@ ParseAcpiSrat (
       return;\r
     }\r
 \r
-    // Make sure the SRAT structure lies inside the table\r
-    if ((Offset + *SratRALength) > AcpiTableLength) {\r
+    // Validate Static Resource Allocation Structure length\r
+    if ((*SratRALength == 0) ||\r
+        ((Offset + (*SratRALength)) > AcpiTableLength)) {\r
       IncrementErrorCount ();\r
       Print (\r
-        L"ERROR: Invalid SRAT structure length. SratRALength = %d. " \\r
-          L"RemainingTableBufferLength = %d. SRAT parsing aborted.\n",\r
+        L"ERROR: Invalid Static Resource Allocation Structure length. " \\r
+          L"Length = %d. Offset = %d. AcpiTableLength = %d.\n",\r
         *SratRALength,\r
-        AcpiTableLength - Offset\r
+        Offset,\r
+        AcpiTableLength\r
         );\r
       return;\r
     }\r