Blame SOURCES/edk2-MdeModulePkg-LzmaCustomDecompressLib-catch-4GB-uncom.patch

6d4e4f
From 158bc30e7cefe14ae690e0b8da1e8803127b813e Mon Sep 17 00:00:00 2001
6d4e4f
From: Laszlo Ersek <lersek@redhat.com>
6d4e4f
Date: Thu, 19 Nov 2020 12:50:34 +0100
6d4e4f
Subject: [PATCH 1/2] MdeModulePkg/LzmaCustomDecompressLib: catch 4GB+
6d4e4f
 uncompressed buffer sizes
6d4e4f
MIME-Version: 1.0
6d4e4f
Content-Type: text/plain; charset=UTF-8
6d4e4f
Content-Transfer-Encoding: 8bit
6d4e4f
6d4e4f
RH-Author: Laszlo Ersek (lersek)
6d4e4f
RH-MergeRequest: 2: prevent integer overflow / heap corruption in LZMA decompression [rhel-8.5.0]
6d4e4f
RH-Commit: [1/1] 97de3eab2b9fdf86195fe329c4391f5b3b98b6f8
6d4e4f
RH-Bugzilla: 1892318
6d4e4f
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
6d4e4f
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
6d4e4f
6d4e4f
The LzmaUefiDecompressGetInfo() function
6d4e4f
[MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c] currently
6d4e4f
silently truncates the UINT64 "DecodedSize" property of the compressed
6d4e4f
blob to the UINT32 "DestinationSize" output parameter.
6d4e4f
6d4e4f
If "DecodedSize" is 0x1_0000_0100, for example, then the subsequent memory
6d4e4f
allocation (for decompression) will likely succeed (allocating 0x100 bytes
6d4e4f
only), but then the LzmaUefiDecompress() function (which re-fetches the
6d4e4f
uncompressed buffer size from the same LZMA header into a "SizeT"
6d4e4f
variable) will overwrite the buffer.
6d4e4f
6d4e4f
Catch (DecodedSize > MAX_UINT32) in LzmaUefiDecompressGetInfo() at once.
6d4e4f
This should not be a practical limitation. (The issue cannot be fixed for
6d4e4f
32-bit systems without spec modifications anyway, given that the
6d4e4f
"OutputSize" output parameter of
6d4e4f
EFI_GUIDED_SECTION_EXTRACTION_PROTOCOL.ExtractSection() has type UINTN,
6d4e4f
not UINT64.)
6d4e4f
6d4e4f
Cc: Dandan Bi <dandan.bi@intel.com>
6d4e4f
Cc: Hao A Wu <hao.a.wu@intel.com>
6d4e4f
Cc: Jian J Wang <jian.j.wang@intel.com>
6d4e4f
Cc: Liming Gao <gaoliming@byosoft.com.cn>
6d4e4f
Cc: Philippe Mathieu-Daud <philmd@redhat.com>
6d4e4f
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=1816
6d4e4f
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
6d4e4f
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
6d4e4f
Reviewed-by: Philippe Mathieu-Daud <philmd@redhat.com>
6d4e4f
Message-Id: <20201119115034.12897-2-lersek@redhat.com>
6d4e4f
(cherry picked from commit e7bd0dd26db7e56aa8ca70132d6ea916ee6f3db0)
6d4e4f
---
6d4e4f
 .../Library/LzmaCustomDecompressLib/LzmaDecompress.c       | 7 +++++++
6d4e4f
 .../LzmaCustomDecompressLib/LzmaDecompressLibInternal.h    | 5 +++++
6d4e4f
 2 files changed, 12 insertions(+)
6d4e4f
6d4e4f
diff --git a/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c b/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
6d4e4f
index c58912eb6a..8f7c242dca 100644
6d4e4f
--- a/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
6d4e4f
+++ b/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompress.c
6d4e4f
@@ -127,6 +127,10 @@ GetDecodedSizeOfBuf(
6d4e4f
                           in DestinationSize and the size of the scratch
6d4e4f
                           buffer was returned in ScratchSize.
6d4e4f
 
6d4e4f
+  @retval RETURN_UNSUPPORTED  DestinationSize cannot be output because the
6d4e4f
+                              uncompressed buffer size (in bytes) does not fit
6d4e4f
+                              in a UINT32. Output parameters have not been
6d4e4f
+                              modified.
6d4e4f
 **/
6d4e4f
 RETURN_STATUS
6d4e4f
 EFIAPI
6d4e4f
@@ -142,6 +146,9 @@ LzmaUefiDecompressGetInfo (
6d4e4f
   ASSERT(SourceSize >= LZMA_HEADER_SIZE);
6d4e4f
 
6d4e4f
   DecodedSize = GetDecodedSizeOfBuf((UINT8*)Source);
6d4e4f
+  if (DecodedSize > MAX_UINT32) {
6d4e4f
+    return RETURN_UNSUPPORTED;
6d4e4f
+  }
6d4e4f
 
6d4e4f
   *DestinationSize = (UINT32)DecodedSize;
6d4e4f
   *ScratchSize = SCRATCH_BUFFER_REQUEST_SIZE;
6d4e4f
diff --git a/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompressLibInternal.h b/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompressLibInternal.h
6d4e4f
index 26f110ba2a..fbafd5f100 100644
6d4e4f
--- a/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompressLibInternal.h
6d4e4f
+++ b/MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaDecompressLibInternal.h
6d4e4f
@@ -9,6 +9,7 @@
6d4e4f
 #ifndef __LZMADECOMPRESSLIB_INTERNAL_H__
6d4e4f
 #define __LZMADECOMPRESSLIB_INTERNAL_H__
6d4e4f
 
6d4e4f
+#include <Base.h>
6d4e4f
 #include <PiPei.h>
6d4e4f
 #include <Library/BaseLib.h>
6d4e4f
 #include <Library/BaseMemoryLib.h>
6d4e4f
@@ -45,6 +46,10 @@
6d4e4f
                           in DestinationSize and the size of the scratch
6d4e4f
                           buffer was returned in ScratchSize.
6d4e4f
 
6d4e4f
+  @retval RETURN_UNSUPPORTED  DestinationSize cannot be output because the
6d4e4f
+                              uncompressed buffer size (in bytes) does not fit
6d4e4f
+                              in a UINT32. Output parameters have not been
6d4e4f
+                              modified.
6d4e4f
 **/
6d4e4f
 RETURN_STATUS
6d4e4f
 EFIAPI
6d4e4f
-- 
6d4e4f
2.27.0
6d4e4f