From 8358e53013fc62c9556598ad842d233906de00ef Mon Sep 17 00:00:00 2001 From: Laszlo Ersek Date: Wed, 24 Oct 2018 21:03:44 +0200 Subject: [PATCH 3/4] IntelFrameworkModulePkg: Add more checker in UefiTianoDecompressLib (CVE FIX) Message-id: <20181024190345.15288-4-lersek@redhat.com> Patchwork-id: 82885 O-Subject: [RHEL8 edk2 PATCH 3/4] IntelFrameworkModulePkg: Add more checker in UefiTianoDecompressLib (CVE FIX) Bugzilla: 1641453 1641464 1641469 Acked-by: Vitaly Kuznetsov Acked-by: Thomas Huth From: Liming Gao --v-- RHEL8 note start --v-- Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1641453 Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1641464 Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1641469 Unfortunately, the upstream patch series was not structured according to the CVE reports. This patch contributes to fixing: - CVE-2017-5733 - CVE-2017-5734 - CVE-2017-5735 but not CVE-2017-5731 or CVE-2017-5732 (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 0a6f48249a60 ("IntelFrameworkModulePkg: 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: "246 files changed, 4067 insertions(+), 4067 deletions(-)". I've decided not to do a partial backport of that (i.e. just for "BaseUefiTianoCustomDecompressLib.c"). --^-- RHEL8 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 To make sure the valid buffer be accessed only. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Holtsclaw Brent Signed-off-by: Liming Gao Reviewed-by: Star Zeng Acked-by: Laszlo Ersek (cherry picked from commit 684db6da64bc7b5faee4e1174e801c245f563b5c) Signed-off-by: Laszlo Ersek --- .../BaseUefiTianoCustomDecompressLib.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/IntelFrameworkModulePkg/Library/BaseUefiTianoCustomDecompressLib/BaseUefiTianoCustomDecompressLib.c b/IntelFrameworkModulePkg/Library/BaseUefiTianoCustomDecompressLib/BaseUefiTianoCustomDecompressLib.c index cb009e7..9b00166 100644 --- a/IntelFrameworkModulePkg/Library/BaseUefiTianoCustomDecompressLib/BaseUefiTianoCustomDecompressLib.c +++ b/IntelFrameworkModulePkg/Library/BaseUefiTianoCustomDecompressLib/BaseUefiTianoCustomDecompressLib.c @@ -143,6 +143,7 @@ MakeTable ( UINT16 Mask; UINT16 WordOfStart; UINT16 WordOfCount; + UINT16 MaxTableLength; // // The maximum mapping table width supported by this internal @@ -155,6 +156,9 @@ MakeTable ( } for (Index = 0; Index < NumOfChar; Index++) { + if (BitLen[Index] > 16) { + return (UINT16) BAD_TABLE; + } Count[BitLen[Index]]++; } @@ -196,6 +200,7 @@ MakeTable ( Avail = NumOfChar; Mask = (UINT16) (1U << (15 - TableBits)); + MaxTableLength = (UINT16) (1U << TableBits); for (Char = 0; Char < NumOfChar; Char++) { @@ -209,6 +214,9 @@ MakeTable ( if (Len <= TableBits) { for (Index = Start[Len]; Index < NextCode; Index++) { + if (Index >= MaxTableLength) { + return (UINT16) BAD_TABLE; + } Table[Index] = Char; } @@ -615,10 +623,14 @@ Decode ( // 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--; } @@ -688,7 +700,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