Blame SOURCES/ovmf-MdeModulePkg-UdfDxe-Add-boundary-check-the-read-of-F.patch

3c2ede
From 8a7cd4ba31848171f596a1eb1df0bc06633d3276 Mon Sep 17 00:00:00 2001
3c2ede
From: Laszlo Ersek <lersek@redhat.com>
3c2ede
Date: Fri, 22 Mar 2019 21:53:21 +0100
3c2ede
Subject: [PATCH 5/8] MdeModulePkg/UdfDxe: Add boundary check the read of
3c2ede
 FE/EFE
3c2ede
MIME-Version: 1.0
3c2ede
Content-Type: text/plain; charset=UTF-8
3c2ede
Content-Transfer-Encoding: 8bit
3c2ede
3c2ede
Message-id: <20190322205323.17693-4-lersek@redhat.com>
3c2ede
Patchwork-id: 85130
3c2ede
O-Subject:  [RHEL-7.7 ovmf PATCH 3/5] MdeModulePkg/UdfDxe: Add boundary check the
3c2ede
	read of FE/EFE
3c2ede
Bugzilla: 1691647
3c2ede
Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
3c2ede
Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
3c2ede
3c2ede
From: Hao Wu <hao.a.wu@intel.com>
3c2ede
3c2ede
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=828
3c2ede
3c2ede
Within ReadFile():
3c2ede
3c2ede
Add checks to ensure that when getting the raw data or the Allocation
3c2ede
Descriptors' data from a FE/EFE, it will not consume data beyond the
3c2ede
size of a FE/EFE.
3c2ede
3c2ede
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
3c2ede
Cc: Jiewen Yao <jiewen.yao@intel.com>
3c2ede
Contributed-under: TianoCore Contribution Agreement 1.1
3c2ede
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
3c2ede
Reviewed-by: Paulo Alcantara <palcantara@suse.de>
3c2ede
Acked-by: Star Zeng <star.zeng@intel.com>
3c2ede
(cherry picked from commit 5c0748f43f4e1cc15fdd0be64a764eacd7df92f6)
3c2ede
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
3c2ede
---
3c2ede
 .../Universal/Disk/UdfDxe/FileSystemOperations.c   | 54 ++++++++++++++++++++--
3c2ede
 1 file changed, 50 insertions(+), 4 deletions(-)
3c2ede
3c2ede
diff --git a/MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c b/MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c
3c2ede
index 424f41c..0012075 100644
3c2ede
--- a/MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c
3c2ede
+++ b/MdeModulePkg/Universal/Disk/UdfDxe/FileSystemOperations.c
3c2ede
@@ -504,15 +504,27 @@ DuplicateFe (
3c2ede
 
3c2ede
   NOTE: The FE/EFE can be thought it was an inode.
3c2ede
 
3c2ede
+  @attention This is boundary function that may receive untrusted input.
3c2ede
+  @attention The input is from FileSystem.
3c2ede
+
3c2ede
+  The (Extended) File Entry is external input, so this routine will do basic
3c2ede
+  validation for (Extended) File Entry and report status.
3c2ede
+
3c2ede
   @param[in]  FileEntryData       (Extended) File Entry pointer.
3c2ede
+  @param[in]  FileEntrySize       Size of the (Extended) File Entry specified
3c2ede
+                                  by FileEntryData.
3c2ede
   @param[out] Data                Buffer contains the raw data of a given
3c2ede
                                   (Extended) File Entry.
3c2ede
   @param[out] Length              Length of the data in Buffer.
3c2ede
 
3c2ede
+  @retval EFI_SUCCESS             Raw data and size of the FE/EFE was read.
3c2ede
+  @retval EFI_VOLUME_CORRUPTED    The file system structures are corrupted.
3c2ede
+
3c2ede
 **/
3c2ede
-VOID
3c2ede
+EFI_STATUS
3c2ede
 GetFileEntryData (
3c2ede
   IN   VOID    *FileEntryData,
3c2ede
+  IN   UINTN   FileEntrySize,
3c2ede
   OUT  VOID    **Data,
3c2ede
   OUT  UINT64  *Length
3c2ede
   )
3c2ede
@@ -536,20 +548,40 @@ GetFileEntryData (
3c2ede
     *Data    = (VOID *)((UINT8 *)FileEntry->Data +
3c2ede
                         FileEntry->LengthOfExtendedAttributes);
3c2ede
   }
3c2ede
+
3c2ede
+  if ((*Length > FileEntrySize) ||
3c2ede
+      ((UINTN)FileEntryData > (UINTN)(*Data)) ||
3c2ede
+      ((UINTN)(*Data) - (UINTN)FileEntryData > FileEntrySize - *Length)) {
3c2ede
+    return EFI_VOLUME_CORRUPTED;
3c2ede
+  }
3c2ede
+  return EFI_SUCCESS;
3c2ede
 }
3c2ede
 
3c2ede
 /**
3c2ede
   Get Allocation Descriptors' data information from a given FE/EFE.
3c2ede
 
3c2ede
+  @attention This is boundary function that may receive untrusted input.
3c2ede
+  @attention The input is from FileSystem.
3c2ede
+
3c2ede
+  The (Extended) File Entry is external input, so this routine will do basic
3c2ede
+  validation for (Extended) File Entry and report status.
3c2ede
+
3c2ede
   @param[in]  FileEntryData       (Extended) File Entry pointer.
3c2ede
+  @param[in]  FileEntrySize       Size of the (Extended) File Entry specified
3c2ede
+                                  by FileEntryData.
3c2ede
   @param[out] AdsData             Buffer contains the Allocation Descriptors'
3c2ede
                                   data from a given FE/EFE.
3c2ede
   @param[out] Length              Length of the data in AdsData.
3c2ede
 
3c2ede
+  @retval EFI_SUCCESS             The data and size of Allocation Descriptors
3c2ede
+                                  were read from the FE/EFE.
3c2ede
+  @retval EFI_VOLUME_CORRUPTED    The file system structures are corrupted.
3c2ede
+
3c2ede
 **/
3c2ede
-VOID
3c2ede
+EFI_STATUS
3c2ede
 GetAdsInformation (
3c2ede
   IN   VOID    *FileEntryData,
3c2ede
+  IN   UINTN   FileEntrySize,
3c2ede
   OUT  VOID    **AdsData,
3c2ede
   OUT  UINT64  *Length
3c2ede
   )
3c2ede
@@ -573,6 +605,13 @@ GetAdsInformation (
3c2ede
     *AdsData = (VOID *)((UINT8 *)FileEntry->Data +
3c2ede
                         FileEntry->LengthOfExtendedAttributes);
3c2ede
   }
3c2ede
+
3c2ede
+  if ((*Length > FileEntrySize) ||
3c2ede
+      ((UINTN)FileEntryData > (UINTN)(*AdsData)) ||
3c2ede
+      ((UINTN)(*AdsData) - (UINTN)FileEntryData > FileEntrySize - *Length)) {
3c2ede
+    return EFI_VOLUME_CORRUPTED;
3c2ede
+  }
3c2ede
+  return EFI_SUCCESS;
3c2ede
 }
3c2ede
 
3c2ede
 /**
3c2ede
@@ -1066,7 +1105,10 @@ ReadFile (
3c2ede
     //
3c2ede
     // There are no extents for this FE/EFE. All data is inline.
3c2ede
     //
3c2ede
-    GetFileEntryData (FileEntryData, &Data, &Length);
3c2ede
+    Status = GetFileEntryData (FileEntryData, Volume->FileEntrySize, &Data, &Length);
3c2ede
+    if (EFI_ERROR (Status)) {
3c2ede
+      return Status;
3c2ede
+    }
3c2ede
 
3c2ede
     if (ReadFileInfo->Flags == ReadFileGetFileSize) {
3c2ede
       ReadFileInfo->ReadLength = Length;
3c2ede
@@ -1110,7 +1152,11 @@ ReadFile (
3c2ede
     // This FE/EFE contains a run of Allocation Descriptors. Get data + size
3c2ede
     // for start reading them out.
3c2ede
     //
3c2ede
-    GetAdsInformation (FileEntryData, &Data, &Length);
3c2ede
+    Status = GetAdsInformation (FileEntryData, Volume->FileEntrySize, &Data, &Length);
3c2ede
+    if (EFI_ERROR (Status)) {
3c2ede
+      return Status;
3c2ede
+    }
3c2ede
+
3c2ede
     AdOffset = 0;
3c2ede
 
3c2ede
     for (;;) {
3c2ede
-- 
3c2ede
1.8.3.1
3c2ede