]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: Fix use-after-free error in InstallConfigurationTable()
authorShi, Steven <steven.shi@intel.com>
Sat, 20 May 2017 09:05:17 +0000 (17:05 +0800)
committerStar Zeng <star.zeng@intel.com>
Tue, 20 Jun 2017 08:55:10 +0000 (16:55 +0800)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=601

When installing configuration table and the original
gDxeCoreST->ConfigurationTable[] buffer happen to be not big enough to
add a new table, the CoreInstallConfigurationTable() enter the branch
of line 113 in InstallConfigurationTable.c to free the old
gDxeCoreST->ConfigurationTable[] buffer and allocate a new bigger one.
The problem happens at line 139 CoreFreePool(), which is to free the
old gDxeCoreST->ConfigurationTable[] buffer. The CoreFreePool()'s
behavior is to free the buffer firstly, then call the
InstallMemoryAttributesTableOnMemoryAllocation (PoolType) to update
the EfiRuntimeServices type memory info, the
CoreInstallConfigurationTable() will be re-entered by CoreFreePool()
in its calling stack, then use-after-free read error will happen at
line 59 of InstallConfigurationTable.c and use-after-free write error
will happen at line 151 and 152 of InstallConfigurationTable.c.

The patch is to update System table to the new table pointer before
calling CoreFreePool() to free the old table.

The case above is in DxeCore, but not in PiSmmCore.
The change in PiSmmCore is to be consistent with DxeCore.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Steven Shi <steven.shi@intel.com>
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Steven Shi <steven.shi@intel.com>
MdeModulePkg/Core/Dxe/Misc/InstallConfigurationTable.c [changed mode: 0644->0755]
MdeModulePkg/Core/PiSmmCore/InstallConfigurationTable.c

old mode 100644 (file)
new mode 100755 (executable)
index e4735db..dcdeb7f
@@ -1,7 +1,7 @@
 /** @file\r
   UEFI Miscellaneous boot Services InstallConfigurationTable service\r
 \r
-Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -42,6 +42,7 @@ CoreInstallConfigurationTable (
 {\r
   UINTN                   Index;\r
   EFI_CONFIGURATION_TABLE *EfiConfigurationTable;\r
+  EFI_CONFIGURATION_TABLE *OldTable;\r
 \r
   //\r
   // If Guid is NULL, then this operation cannot be performed\r
@@ -68,7 +69,7 @@ CoreInstallConfigurationTable (
     if (Table != NULL) {\r
       //\r
       // If Table is not NULL, then this is a modify operation.\r
-      // Modify the table enty and return.\r
+      // Modify the table entry and return.\r
       //\r
       gDxeCoreST->ConfigurationTable[Index].VendorTable = Table;\r
 \r
@@ -134,15 +135,30 @@ CoreInstallConfigurationTable (
           );\r
 \r
         //\r
-        // Free Old Table\r
+        // Record the old table pointer.\r
         //\r
-        CoreFreePool (gDxeCoreST->ConfigurationTable);\r
-      }\r
+        OldTable = gDxeCoreST->ConfigurationTable;\r
 \r
-      //\r
-      // Update System Table\r
-      //\r
-      gDxeCoreST->ConfigurationTable = EfiConfigurationTable;\r
+        //\r
+        // As the CoreInstallConfigurationTable() may be re-entered by CoreFreePool()\r
+        // in its calling stack, updating System table to the new table pointer must\r
+        // be done before calling CoreFreePool() to free the old table.\r
+        // It can make sure the gDxeCoreST->ConfigurationTable point to the new table\r
+        // and avoid the errors of use-after-free to the old table by the reenter of\r
+        // CoreInstallConfigurationTable() in CoreFreePool()'s calling stack.\r
+        //\r
+        gDxeCoreST->ConfigurationTable = EfiConfigurationTable;\r
+\r
+        //\r
+        // Free the old table after updating System Table to the new table pointer.\r
+        //\r
+        CoreFreePool (OldTable);\r
+      } else {\r
+        //\r
+        // Update System Table\r
+        //\r
+        gDxeCoreST->ConfigurationTable = EfiConfigurationTable;\r
+      }\r
     }\r
 \r
     //\r
index b2f6769c109fda016ca0609efea123684f0ca2ae..867d1ac4b13a0e488511b2c8447535f0ec619075 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   System Management System Table Services SmmInstallConfigurationTable service\r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2017, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials are licensed and made available \r
   under the terms and conditions of the BSD License which accompanies this \r
   distribution.  The full text of the license may be found at        \r
@@ -46,6 +46,7 @@ SmmInstallConfigurationTable (
 {\r
   UINTN                    Index;\r
   EFI_CONFIGURATION_TABLE  *ConfigurationTable;\r
+  EFI_CONFIGURATION_TABLE  *OldTable;\r
 \r
   //\r
   // If Guid is NULL, then this operation cannot be performed\r
@@ -72,7 +73,7 @@ SmmInstallConfigurationTable (
     if (Table != NULL) {\r
       //\r
       // If Table is not NULL, then this is a modify operation.\r
-      // Modify the table enty and return.\r
+      // Modify the table entry and return.\r
       //\r
       ConfigurationTable[Index].VendorTable = Table;\r
       return EFI_SUCCESS;\r
@@ -130,15 +131,30 @@ SmmInstallConfigurationTable (
           );\r
 \r
         //\r
-        // Free Old Table\r
+        // Record the old table pointer.\r
         //\r
-        FreePool (gSmmCoreSmst.SmmConfigurationTable);\r
-      }\r
+        OldTable = gSmmCoreSmst.SmmConfigurationTable;\r
 \r
-      //\r
-      // Update System Table\r
-      //\r
-      gSmmCoreSmst.SmmConfigurationTable = ConfigurationTable;\r
+        //\r
+        // As the SmmInstallConfigurationTable() may be re-entered by FreePool() in\r
+        // its calling stack, updating System table to the new table pointer must\r
+        // be done before calling FreePool() to free the old table.\r
+        // It can make sure the gSmmCoreSmst.SmmConfigurationTable point to the new\r
+        // table and avoid the errors of use-after-free to the old table by the\r
+        // reenter of SmmInstallConfigurationTable() in FreePool()'s calling stack.\r
+        //\r
+        gSmmCoreSmst.SmmConfigurationTable = ConfigurationTable;\r
+\r
+        //\r
+        // Free the old table after updating System Table to the new table pointer.\r
+        //\r
+        FreePool (OldTable);\r
+      } else {\r
+        //\r
+        // Update System Table\r
+        //\r
+        gSmmCoreSmst.SmmConfigurationTable = ConfigurationTable;\r
+      }\r
     }\r
 \r
     //\r