]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/PlatformDriOverrideDxe/PlatDriOverrideLib.c
Add check to make sure the data be valid.
[mirror_edk2.git] / MdeModulePkg / Universal / PlatformDriOverrideDxe / PlatDriOverrideLib.c
index 2391ade211d242a0cb5cb54aa2b0af561fea9b96..d07f62ab4ac0914a84ec0c3dfdc5b425eb794b4c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Implementation of the shared functions to do the platform driver vverride mapping.\r
 \r
-  Copyright (c) 2007 - 2009, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2007 - 2014, 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
@@ -691,12 +691,17 @@ InitOverridesMapping (
   //\r
   VariableNum = 1;\r
   Corrupted = FALSE;\r
+  NotEnd = 0;\r
   do {\r
     VariableIndex = VariableBuffer;\r
-    //\r
-    // End flag\r
-    //\r
-    NotEnd = *(UINT32*) VariableIndex;\r
+    if (VariableIndex + sizeof (UINT32) > (UINT8 *) VariableBuffer + BufferSize) {\r
+      Corrupted = TRUE;\r
+    } else {\r
+      //\r
+      // End flag\r
+      //\r
+      NotEnd = *(UINT32*) VariableIndex;\r
+    }\r
     //\r
     // Traverse the entries containing the mapping that Controller Device Path\r
     // to a set of Driver Device Paths within this variable.\r
@@ -706,6 +711,10 @@ InitOverridesMapping (
       //\r
       // Check signature of this entry\r
       //\r
+      if (VariableIndex + sizeof (UINT32) > (UINT8 *) VariableBuffer + BufferSize) {\r
+        Corrupted = TRUE;\r
+        break;\r
+      }\r
       Signature = *(UINT32 *) VariableIndex;\r
       if (Signature != PLATFORM_OVERRIDE_ITEM_SIGNATURE) {\r
         Corrupted = TRUE;\r
@@ -722,6 +731,10 @@ InitOverridesMapping (
       //\r
       // Get DriverNum\r
       //\r
+      if (VariableIndex + sizeof (UINT32) >= (UINT8 *) VariableBuffer + BufferSize) {\r
+        Corrupted = TRUE;\r
+        break;\r
+      }\r
       DriverNumber = *(UINT32*) VariableIndex;\r
       OverrideItem->DriverInfoNum = DriverNumber;\r
       VariableIndex = VariableIndex + sizeof (UINT32);\r
@@ -735,6 +748,14 @@ InitOverridesMapping (
       // Align the VariableIndex since the controller device path may not be aligned, refer to the SaveOverridesMapping()\r
       //\r
       VariableIndex += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));\r
+      //\r
+      // Check buffer overflow.\r
+      //\r
+      if ((OverrideItem->ControllerDevicePath == NULL) || (VariableIndex < (UINT8 *) ControllerDevicePath) || \r
+          (VariableIndex > (UINT8 *) VariableBuffer + BufferSize)) {\r
+        Corrupted = TRUE;\r
+        break;\r
+      }\r
 \r
       //\r
       // Get all DriverImageDevicePath[]\r
@@ -756,8 +777,20 @@ InitOverridesMapping (
         VariableIndex += ((sizeof(UINT32) - ((UINTN) (VariableIndex))) & (sizeof(UINT32) - 1));\r
 \r
         InsertTailList (&OverrideItem->DriverInfoList, &DriverImageInfo->Link);\r
+\r
+        //\r
+        // Check buffer overflow\r
+        //\r
+        if ((DriverImageInfo->DriverImagePath == NULL) || (VariableIndex < (UINT8 *) DriverDevicePath) || \r
+            (VariableIndex < (UINT8 *) VariableBuffer + BufferSize)) {\r
+          Corrupted = TRUE;\r
+          break;\r
+        }\r
       }\r
       InsertTailList (MappingDataBase, &OverrideItem->Link);\r
+      if (Corrupted) {\r
+        break;\r
+      }\r
     }\r
 \r
     FreePool (VariableBuffer);\r
@@ -866,11 +899,11 @@ DeleteOverridesVariables (
   //\r
   // Check NotEnd to get all PlatDriOverX variable(s)\r
   //\r
-  while ((*(UINT32*)VariableBuffer) != 0) {\r
+  while ((VariableBuffer != NULL) && ((*(UINT32*)VariableBuffer) != 0)) {\r
+    FreePool (VariableBuffer);\r
     UnicodeSPrint (OverrideVariableName, sizeof (OverrideVariableName), L"PlatDriOver%d", VariableNum);\r
     VariableBuffer = GetVariableAndSize (OverrideVariableName, &gEfiCallerIdGuid, &BufferSize);\r
     VariableNum++;\r
-    ASSERT (VariableBuffer != NULL);\r
   }\r
 \r
   //\r
@@ -1057,10 +1090,19 @@ SaveOverridesMapping (
                     VariableNeededSize,\r
                     VariableBuffer\r
                     );\r
-    ASSERT (!EFI_ERROR(Status));\r
+    FreePool (VariableBuffer);\r
+\r
+    if (EFI_ERROR (Status)) {\r
+      if (NumIndex > 0) {\r
+        //\r
+        // Delete all PlatDriOver variables when full mapping can't be set.  \r
+        //\r
+        DeleteOverridesVariables ();\r
+      }\r
+      return Status;\r
+    }\r
 \r
     NumIndex ++;\r
-    FreePool (VariableBuffer);\r
   }\r
 \r
   return EFI_SUCCESS;\r