Blame SOURCES/edk2-MdePkg-Add-more-checker-in-UefiDecompressLib-to-acce.patch

cc9195
From 41129e136b621728eb5cb1c81aafcc5fedc53a12 Mon Sep 17 00:00:00 2001
cc9195
From: Laszlo Ersek <lersek@redhat.com>
cc9195
Date: Wed, 24 Oct 2018 21:03:43 +0200
cc9195
Subject: [PATCH 2/4] MdePkg: Add more checker in UefiDecompressLib to access
cc9195
 the valid buffer only (CVE FIX)
cc9195
cc9195
Message-id: <20181024190345.15288-3-lersek@redhat.com>
cc9195
Patchwork-id: 82883
cc9195
O-Subject:  [RHEL8 edk2 PATCH 2/4] MdePkg: Add more checker in UefiDecompressLib
cc9195
	to access the valid buffer only (CVE FIX)
cc9195
Bugzilla: 1641449
cc9195
1641453
cc9195
1641464
cc9195
1641469
cc9195
Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
cc9195
Acked-by: Thomas Huth <thuth@redhat.com>
cc9195
cc9195
From: Liming Gao <liming.gao@intel.com>
cc9195
cc9195
--v-- RHEL8 note start --v--
cc9195
cc9195
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1641449
cc9195
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1641453
cc9195
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1641464
cc9195
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1641469
cc9195
cc9195
Unfortunately, the upstream patch series was not structured according to
cc9195
the CVE reports. This patch contributes to fixing:
cc9195
cc9195
- CVE-2017-5732
cc9195
- CVE-2017-5733
cc9195
- CVE-2017-5734
cc9195
- CVE-2017-5735
cc9195
cc9195
but not CVE-2017-5731 (contrarily to the upstream commit message). The
cc9195
best I could achieve up-stream was to get the "CVE FIX" expression into
cc9195
the subject, and a whole-sale dump of the CVEs into the body. I had not
cc9195
been invited to the original (off-list, embargoed) analysis and review.
cc9195
cc9195
The trivial context difference (whitespace) is due to RHEL8 lacking
cc9195
upstream commit 9095d37b8fe5 ("MdePkg: Clean up source files",
cc9195
2018-06-28). I've considered backporting that (since it only cleans up
cc9195
whitespace). However, the diffstat on that commit convinced me otherwise:
cc9195
"729 files changed, 15667 insertions(+), 15667 deletions(-)". I've decided
cc9195
not to do a partial backport of that (i.e. just for
cc9195
"BaseUefiDecompressLib.c").
cc9195
cc9195
--^-- RHEL8 note end --^--
cc9195
cc9195
Fix CVE-2017-5731,CVE-2017-5732,CVE-2017-5733,CVE-2017-5734,CVE-2017-5735
cc9195
https://bugzilla.tianocore.org/show_bug.cgi?id=686
cc9195
cc9195
Contributed-under: TianoCore Contribution Agreement 1.1
cc9195
Signed-off-by: Holtsclaw Brent <brent.holtsclaw@intel.com>
cc9195
Signed-off-by: Liming Gao <liming.gao@intel.com>
cc9195
Reviewed-by: Star Zeng <star.zeng@intel.com>
cc9195
Acked-by: Laszlo Ersek <lersek@redhat.com>
cc9195
(cherry picked from commit 2ec7953d49677142c5f7552e9e3d96fb406ba0c4)
cc9195
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
cc9195
---
cc9195
 .../BaseUefiDecompressLib/BaseUefiDecompressLib.c       | 17 +++++++++++++++--
cc9195
 1 file changed, 15 insertions(+), 2 deletions(-)
cc9195
cc9195
diff --git a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c
cc9195
index e818543..0c6b1fe 100644
cc9195
--- a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c
cc9195
+++ b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c
cc9195
@@ -152,6 +152,7 @@ MakeTable (
cc9195
   UINT16  Mask;
cc9195
   UINT16  WordOfStart;
cc9195
   UINT16  WordOfCount;
cc9195
+  UINT16  MaxTableLength;
cc9195
 
cc9195
   //
cc9195
   // The maximum mapping table width supported by this internal
cc9195
@@ -164,6 +165,9 @@ MakeTable (
cc9195
   }
cc9195
 
cc9195
   for (Index = 0; Index < NumOfChar; Index++) {
cc9195
+    if (BitLen[Index] > 16) {
cc9195
+      return (UINT16) BAD_TABLE;
cc9195
+    }
cc9195
     Count[BitLen[Index]]++;
cc9195
   }
cc9195
   
cc9195
@@ -205,6 +209,7 @@ MakeTable (
cc9195
 
cc9195
   Avail = NumOfChar;
cc9195
   Mask  = (UINT16) (1U << (15 - TableBits));
cc9195
+  MaxTableLength = (UINT16) (1U << TableBits);
cc9195
 
cc9195
   for (Char = 0; Char < NumOfChar; Char++) {
cc9195
 
cc9195
@@ -218,6 +223,9 @@ MakeTable (
cc9195
     if (Len <= TableBits) {
cc9195
 
cc9195
       for (Index = Start[Len]; Index < NextCode; Index++) {
cc9195
+        if (Index >= MaxTableLength) {
cc9195
+          return (UINT16) BAD_TABLE;
cc9195
+        }
cc9195
         Table[Index] = Char;
cc9195
       }
cc9195
 
cc9195
@@ -620,11 +628,16 @@ Decode (
cc9195
       // Write BytesRemain of bytes into mDstBase
cc9195
       //
cc9195
       BytesRemain--;
cc9195
+
cc9195
       while ((INT16) (BytesRemain) >= 0) {
cc9195
-        Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];
cc9195
         if (Sd->mOutBuf >= Sd->mOrigSize) {
cc9195
           goto Done;
cc9195
         }
cc9195
+        if (DataIdx >= Sd->mOrigSize) {
cc9195
+          Sd->mBadTableFlag = (UINT16) BAD_TABLE;
cc9195
+          goto Done;
cc9195
+        }
cc9195
+        Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];
cc9195
 
cc9195
         BytesRemain--;
cc9195
       }
cc9195
@@ -694,7 +707,7 @@ UefiDecompressGetInfo (
cc9195
   }
cc9195
 
cc9195
   CompressedSize   = ReadUnaligned32 ((UINT32 *)Source);
cc9195
-  if (SourceSize < (CompressedSize + 8)) {
cc9195
+  if (SourceSize < (CompressedSize + 8) || (CompressedSize + 8) < 8) {
cc9195
     return RETURN_INVALID_PARAMETER;
cc9195
   }
cc9195
 
cc9195
-- 
cc9195
1.8.3.1
cc9195