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

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