]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/DxeCore: Ensure FfsFileHeader 8 bytes aligned
authorStar Zeng <star.zeng@intel.com>
Fri, 5 Jan 2018 05:46:22 +0000 (13:46 +0800)
committerJian J Wang <jian.j.wang@intel.com>
Thu, 28 Feb 2019 10:22:53 +0000 (18:22 +0800)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=864
REF: CVE-2018-3630

To follow PI spec, ensure FfsFileHeader 8 bytes aligned.

For the integrity of FV(especially non-MemoryMapped FV) layout,
let CachedFv point to FV beginning, but not (FV + FV header).

And current code only handles (FwVolHeader->ExtHeaderOffset != 0) path,
update code to also handle (FwVolHeader->ExtHeaderOffset == 0) path.

Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao Wu <hao.a.wu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Jian J Wang <jian.j.wang@intel.com>
MdeModulePkg/Core/Dxe/FwVol/FwVol.c

index 93ddcc35913badc81ae3b141dc6c78fad5a3166a..28fce46a956fd430d1c2b9f1ce760e6b4a500e82 100644 (file)
@@ -3,7 +3,7 @@
   Layers on top of Firmware Block protocol to produce a file abstraction\r
   of FV based files.\r
 \r
-Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2019, 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
@@ -329,8 +329,6 @@ FvCheck (
   FFS_FILE_LIST_ENTRY                   *FfsFileEntry;\r
   EFI_FFS_FILE_HEADER                   *FfsHeader;\r
   UINT8                                 *CacheLocation;\r
-  UINTN                                 LbaOffset;\r
-  UINTN                                 HeaderSize;\r
   UINTN                                 Index;\r
   EFI_LBA                               LbaIndex;\r
   UINTN                                 Size;\r
@@ -353,11 +351,7 @@ FvCheck (
     return Status;\r
   }\r
 \r
-  //\r
-  // Size is the size of the FV minus the head. We have already allocated\r
-  // the header to check to make sure the volume is valid\r
-  //\r
-  Size = (UINTN)(FwVolHeader->FvLength - FwVolHeader->HeaderLength);\r
+  Size = (UINTN) FwVolHeader->FvLength;\r
   if ((FvbAttributes & EFI_FVB2_MEMORY_MAPPED) != 0) {\r
     FvDevice->IsMemoryMapped = TRUE;\r
 \r
@@ -369,7 +363,7 @@ FvCheck (
     //\r
     // Don't cache memory mapped FV really.\r
     //\r
-    FvDevice->CachedFv = (UINT8 *) (UINTN) (PhysicalAddress + FwVolHeader->HeaderLength);\r
+    FvDevice->CachedFv = (UINT8 *) (UINTN) PhysicalAddress;\r
   } else {\r
     FvDevice->IsMemoryMapped = FALSE;\r
     FvDevice->CachedFv = AllocatePool (Size);\r
@@ -380,52 +374,27 @@ FvCheck (
   }\r
 \r
   //\r
-  // Remember a pointer to the end fo the CachedFv\r
+  // Remember a pointer to the end of the CachedFv\r
   //\r
   FvDevice->EndOfCachedFv = FvDevice->CachedFv + Size;\r
 \r
   if (!FvDevice->IsMemoryMapped) {\r
     //\r
-    // Copy FV minus header into memory using the block map we have all ready\r
-    // read into memory.\r
+    // Copy FV into memory using the block map.\r
     //\r
     BlockMap = FwVolHeader->BlockMap;\r
     CacheLocation = FvDevice->CachedFv;\r
     LbaIndex = 0;\r
-    LbaOffset = 0;\r
-    HeaderSize = FwVolHeader->HeaderLength;\r
     while ((BlockMap->NumBlocks != 0) || (BlockMap->Length != 0)) {\r
-      Index = 0;\r
-      Size  = BlockMap->Length;\r
-      if (HeaderSize > 0) {\r
-        //\r
-        // Skip header size\r
-        //\r
-        for (; Index < BlockMap->NumBlocks && HeaderSize >= BlockMap->Length; Index ++) {\r
-          HeaderSize -= BlockMap->Length;\r
-          LbaIndex ++;\r
-        }\r
-\r
-        //\r
-        // Check whether FvHeader is crossing the multi block range.\r
-        //\r
-        if (Index >= BlockMap->NumBlocks) {\r
-          BlockMap++;\r
-          continue;\r
-        } else if (HeaderSize > 0) {\r
-          LbaOffset = HeaderSize;\r
-          Size = BlockMap->Length - HeaderSize;\r
-          HeaderSize = 0;\r
-        }\r
-      }\r
-\r
       //\r
       // read the FV data\r
       //\r
-      for (; Index < BlockMap->NumBlocks; Index ++) {\r
-        Status = Fvb->Read (Fvb,\r
+      Size = BlockMap->Length;\r
+      for (Index = 0; Index < BlockMap->NumBlocks; Index++) {\r
+        Status = Fvb->Read (\r
+                        Fvb,\r
                         LbaIndex,\r
-                        LbaOffset,\r
+                        0,\r
                         &Size,\r
                         CacheLocation\r
                         );\r
@@ -438,13 +407,7 @@ FvCheck (
         }\r
 \r
         LbaIndex++;\r
-        CacheLocation += Size;\r
-\r
-        //\r
-        // After we skip Fv Header always read from start of block\r
-        //\r
-        LbaOffset = 0;\r
-        Size  = BlockMap->Length;\r
+        CacheLocation += BlockMap->Length;\r
       }\r
 \r
       BlockMap++;\r
@@ -475,12 +438,12 @@ FvCheck (
     //\r
     // Searching for files starts on an 8 byte aligned boundary after the end of the Extended Header if it exists.\r
     //\r
-    FwVolExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) (FvDevice->CachedFv + (FwVolHeader->ExtHeaderOffset - FwVolHeader->HeaderLength));\r
+    FwVolExtHeader = (EFI_FIRMWARE_VOLUME_EXT_HEADER *) (FvDevice->CachedFv + FwVolHeader->ExtHeaderOffset);\r
     FfsHeader = (EFI_FFS_FILE_HEADER *) ((UINT8 *) FwVolExtHeader + FwVolExtHeader->ExtHeaderSize);\r
-    FfsHeader = (EFI_FFS_FILE_HEADER *) ALIGN_POINTER (FfsHeader, 8);\r
   } else {\r
-    FfsHeader = (EFI_FFS_FILE_HEADER *) (FvDevice->CachedFv);\r
+    FfsHeader = (EFI_FFS_FILE_HEADER *) (FvDevice->CachedFv + FwVolHeader->HeaderLength);\r
   }\r
+  FfsHeader = (EFI_FFS_FILE_HEADER *) ALIGN_POINTER (FfsHeader, 8);\r
   TopFvAddress = FvDevice->EndOfCachedFv;\r
   while (((UINTN) FfsHeader >= (UINTN) FvDevice->CachedFv) && ((UINTN) FfsHeader <= (UINTN) ((UINTN) TopFvAddress - sizeof (EFI_FFS_FILE_HEADER)))) {\r
 \r