Blob Blame History Raw
From 6e3079460fa075f4b44c1031b1e20709979d9424 Mon Sep 17 00:00:00 2001
From: Philippe Mathieu-Daude <philmd@redhat.com>
Date: Wed, 13 Feb 2019 09:50:45 +0100
Subject: [PATCH 02/13] MdePkg: Add more checker in UefiDecompressLib to access
 the valid buffer only (CVE FIX)

Message-id: <20190213085050.20766-3-philmd@redhat.com>
Patchwork-id: 84480
O-Subject:  [RHEL-7.7 ovmf PATCH v3 2/7] MdePkg: Add more checker in
	UefiDecompressLib to access the valid buffer only (CVE FIX)
Bugzilla: 1666586
Acked-by: Laszlo Ersek <lersek@redhat.com>
Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>

From: Laszlo Ersek <lersek@redhat.com>

From: Liming Gao <liming.gao@intel.com>

--v-- RHEL7 note start --v--
Unfortunately, the upstream patch series was not structured according to
the CVE reports. This patch contributes to fixing:

- CVE-2017-5732
- CVE-2017-5733
- CVE-2017-5734
- CVE-2017-5735

but not CVE-2017-5731 (contrarily to the upstream commit message). The
best I could achieve up-stream was to get the "CVE FIX" expression into
the subject, and a whole-sale dump of the CVEs into the body. I had not
been invited to the original (off-list, embargoed) analysis and review.

The trivial context difference (whitespace) is due to RHEL8 lacking
upstream commit 9095d37b8fe5 ("MdePkg: Clean up source files",
2018-06-28). I've considered backporting that (since it only cleans up
whitespace). However, the diffstat on that commit convinced me otherwise:
"729 files changed, 15667 insertions(+), 15667 deletions(-)". I've decided
not to do a partial backport of that (i.e. just for
"BaseUefiDecompressLib.c").

--^-- RHEL7 note end --^--

Fix CVE-2017-5731,CVE-2017-5732,CVE-2017-5733,CVE-2017-5734,CVE-2017-5735
https://bugzilla.tianocore.org/show_bug.cgi?id=686

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Holtsclaw Brent <brent.holtsclaw@intel.com>
Signed-off-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Star Zeng <star.zeng@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
(cherry picked from commit 2ec7953d49677142c5f7552e9e3d96fb406ba0c4)
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
(cherry picked from commit 41129e136b621728eb5cb1c81aafcc5fedc53a12)
Signed-off-by: Philippe Mathieu-Daude <philmd@redhat.com>
---
 .../BaseUefiDecompressLib/BaseUefiDecompressLib.c       | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c
index e818543..0c6b1fe 100644
--- a/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c
+++ b/MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompressLib.c
@@ -152,6 +152,7 @@ MakeTable (
   UINT16  Mask;
   UINT16  WordOfStart;
   UINT16  WordOfCount;
+  UINT16  MaxTableLength;
 
   //
   // The maximum mapping table width supported by this internal
@@ -164,6 +165,9 @@ MakeTable (
   }
 
   for (Index = 0; Index < NumOfChar; Index++) {
+    if (BitLen[Index] > 16) {
+      return (UINT16) BAD_TABLE;
+    }
     Count[BitLen[Index]]++;
   }
   
@@ -205,6 +209,7 @@ MakeTable (
 
   Avail = NumOfChar;
   Mask  = (UINT16) (1U << (15 - TableBits));
+  MaxTableLength = (UINT16) (1U << TableBits);
 
   for (Char = 0; Char < NumOfChar; Char++) {
 
@@ -218,6 +223,9 @@ MakeTable (
     if (Len <= TableBits) {
 
       for (Index = Start[Len]; Index < NextCode; Index++) {
+        if (Index >= MaxTableLength) {
+          return (UINT16) BAD_TABLE;
+        }
         Table[Index] = Char;
       }
 
@@ -620,11 +628,16 @@ Decode (
       // Write BytesRemain of bytes into mDstBase
       //
       BytesRemain--;
+
       while ((INT16) (BytesRemain) >= 0) {
-        Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];
         if (Sd->mOutBuf >= Sd->mOrigSize) {
           goto Done;
         }
+        if (DataIdx >= Sd->mOrigSize) {
+          Sd->mBadTableFlag = (UINT16) BAD_TABLE;
+          goto Done;
+        }
+        Sd->mDstBase[Sd->mOutBuf++] = Sd->mDstBase[DataIdx++];
 
         BytesRemain--;
       }
@@ -694,7 +707,7 @@ UefiDecompressGetInfo (
   }
 
   CompressedSize   = ReadUnaligned32 ((UINT32 *)Source);
-  if (SourceSize < (CompressedSize + 8)) {
+  if (SourceSize < (CompressedSize + 8) || (CompressedSize + 8) < 8) {
     return RETURN_INVALID_PARAMETER;
   }
 
-- 
1.8.3.1