From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.93; helo=mga11.intel.com; envelope-from=yonghong.zhu@intel.com; receiver=edk2-devel@lists.01.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 862742194D387 for ; Thu, 18 Oct 2018 05:28:41 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Oct 2018 05:28:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,396,1534834800"; d="scan'208";a="79725302" Received: from fmsmsx104.amr.corp.intel.com ([10.18.124.202]) by fmsmga008.fm.intel.com with ESMTP; 18 Oct 2018 05:28:41 -0700 Received: from fmsmsx101.amr.corp.intel.com (10.18.124.199) by fmsmsx104.amr.corp.intel.com (10.18.124.202) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 18 Oct 2018 05:28:40 -0700 Received: from shsmsx104.ccr.corp.intel.com (10.239.4.70) by fmsmsx101.amr.corp.intel.com (10.18.124.199) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 18 Oct 2018 05:28:40 -0700 Received: from shsmsx103.ccr.corp.intel.com ([169.254.4.111]) by SHSMSX104.ccr.corp.intel.com ([169.254.5.183]) with mapi id 14.03.0319.002; Thu, 18 Oct 2018 20:28:36 +0800 From: "Zhu, Yonghong" To: "Gao, Liming" , "edk2-devel@lists.01.org" CC: "Holtsclaw, Brent" , "Zhu, Yonghong" Thread-Topic: [edk2] [Patch 3/3] BaseTools: Add more checker in Decompress algorithm to access the valid buffer Thread-Index: AQHUZPUzuwKuTJ54nkOcXSXocmrFXqUk8iAg Date: Thu, 18 Oct 2018 12:28:35 +0000 Message-ID: References: <1539655573-5456-1-git-send-email-liming.gao@intel.com> <1539655573-5456-4-git-send-email-liming.gao@intel.com> In-Reply-To: <1539655573-5456-4-git-send-email-liming.gao@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [Patch 3/3] BaseTools: Add more checker in Decompress algorithm to access the valid buffer X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Oct 2018 12:28:41 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Yonghong Zhu =20 Best Regards, Zhu Yonghong -----Original Message----- From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Limi= ng Gao Sent: Tuesday, October 16, 2018 10:06 AM To: edk2-devel@lists.01.org Cc: Holtsclaw, Brent Subject: [edk2] [Patch 3/3] BaseTools: Add more checker in Decompress algor= ithm to access the valid buffer https://bugzilla.tianocore.org/show_bug.cgi?id=3D686 Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Holtsclaw Brent Signed-off-by: Liming Gao --- BaseTools/Source/C/Common/Decompress.c | 23 +++++++++++++++++++-= - BaseTools/Source/C/TianoCompress/TianoCompress.c | 26 ++++++++++++++++++++= +++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/BaseTools/Source/C/Common/Decompress.c b/BaseTools/Source/C/Co= mmon/Decompress.c index 9906888..71313b1 100644 --- a/BaseTools/Source/C/Common/Decompress.c +++ b/BaseTools/Source/C/Common/Decompress.c @@ -194,12 +194,16 @@ Returns: UINT16 Avail; UINT16 NextCode; UINT16 Mask; + UINT16 MaxTableLength; =20 for (Index =3D 1; Index <=3D 16; Index++) { Count[Index] =3D 0; } =20 for (Index =3D 0; Index < NumOfChar; Index++) { + if (BitLen[Index] > 16) { + return (UINT16) BAD_TABLE; + } Count[BitLen[Index]]++; } =20 @@ -237,6 +241,7 @@ Returns: =20 Avail =3D NumOfChar; Mask =3D (UINT16) (1U << (15 - TableBits)); + MaxTableLength =3D (UINT16) (1U << TableBits); =20 for (Char =3D 0; Char < NumOfChar; Char++) { =20 @@ -250,6 +255,9 @@ Returns: if (Len <=3D TableBits) { =20 for (Index =3D Start[Len]; Index < NextCode; Index++) { + if (Index >=3D MaxTableLength) { + return (UINT16) BAD_TABLE; + } Table[Index] =3D Char; } =20 @@ -643,10 +651,14 @@ Returns: (VOID) =20 BytesRemain--; while ((INT16) (BytesRemain) >=3D 0) { - Sd->mDstBase[Sd->mOutBuf++] =3D Sd->mDstBase[DataIdx++]; if (Sd->mOutBuf >=3D Sd->mOrigSize) { return ; } + if (DataIdx >=3D Sd->mOrigSize) { + Sd->mBadTableFlag =3D (UINT16) BAD_TABLE; + return ; + } + Sd->mDstBase[Sd->mOutBuf++] =3D Sd->mDstBase[DataIdx++]; =20 BytesRemain--; } @@ -684,6 +696,7 @@ Returns: --*/ { UINT8 *Src; + UINT32 CompSize; =20 *ScratchSize =3D sizeof (SCRATCH_DATA); =20 @@ -692,7 +705,13 @@ Returns: return EFI_INVALID_PARAMETER; } =20 + CompSize =3D Src[0] + (Src[1] << 8) + (Src[2] << 16) + (Src[3] << 24); *DstSize =3D Src[4] + (Src[5] << 8) + (Src[6] << 16) + (Src[7] << 24); + + if (SrcSize < CompSize + 8 || (CompSize + 8) < 8) { + return EFI_INVALID_PARAMETER; + } + return EFI_SUCCESS; } =20 @@ -752,7 +771,7 @@ Returns: CompSize =3D Src[0] + (Src[1] << 8) + (Src[2] << 16) + (Src[3] << 24); OrigSize =3D Src[4] + (Src[5] << 8) + (Src[6] << 16) + (Src[7] << 24); =20 - if (SrcSize < CompSize + 8) { + if (SrcSize < CompSize + 8 || (CompSize + 8) < 8) { return EFI_INVALID_PARAMETER; } =20 diff --git a/BaseTools/Source/C/TianoCompress/TianoCompress.c b/BaseTools/S= ource/C/TianoCompress/TianoCompress.c index b88d7da..2d6fc4c 100644 --- a/BaseTools/Source/C/TianoCompress/TianoCompress.c +++ b/BaseTools/Source/C/TianoCompress/TianoCompress.c @@ -1757,6 +1757,7 @@ Returns: SCRATCH_DATA *Scratch; UINT8 *Src; UINT32 OrigSize; + UINT32 CompSize; =20 SetUtilityName(UTILITY_NAME); =20 @@ -1765,6 +1766,7 @@ Returns: OutBuffer =3D NULL; Scratch =3D NULL; OrigSize =3D 0; + CompSize =3D 0; InputLength =3D 0; InputFileName =3D NULL; OutputFileName =3D NULL; @@ -2006,15 +2008,24 @@ Returns: } fwrite(OutBuffer, (size_t)(DstSize), 1, OutputFile); } else { + if (InputLength < 8){ + Error (NULL, 0, 3000, "Invalid", "The input file %s is too small.", = InputFileName); + goto ERROR; + } // // Get Compressed file original size // Src =3D (UINT8 *)FileBuffer; OrigSize =3D Src[4] + (Src[5] << 8) + (Src[6] << 16) + (Src[7] << 24)= ; + CompSize =3D Src[0] + (Src[1] << 8) + (Src[2] <<16) + (Src[3] <<24); =20 // // Allocate OutputBuffer // + if (InputLength < CompSize + 8 || (CompSize + 8) < 8) { + Error (NULL, 0, 3000, "Invalid", "The input file %s data is invalid.= ", InputFileName); + goto ERROR; + } OutBuffer =3D (UINT8 *)malloc(OrigSize); if (OutBuffer =3D=3D NULL) { Error (NULL, 0, 4001, "Resource:", "Memory cannot be allocated!"); @= @ -2204,12 +2215,16 @@ Returns: UINT16 Mask; UINT16 WordOfStart; UINT16 WordOfCount; + UINT16 MaxTableLength; =20 for (Index =3D 0; Index <=3D 16; Index++) { Count[Index] =3D 0; } =20 for (Index =3D 0; Index < NumOfChar; Index++) { + if (BitLen[Index] > 16) { + return (UINT16) BAD_TABLE; + } Count[BitLen[Index]]++; } =20 @@ -2253,6 +2268,7 @@ Returns: =20 Avail =3D NumOfChar; Mask =3D (UINT16) (1U << (15 - TableBits)); + MaxTableLength =3D (UINT16) (1U << TableBits); =20 for (Char =3D 0; Char < NumOfChar; Char++) { =20 @@ -2266,6 +2282,9 @@ Returns: if (Len <=3D TableBits) { =20 for (Index =3D Start[Len]; Index < NextCode; Index++) { + if (Index >=3D MaxTableLength) { + return (UINT16) BAD_TABLE; + } Table[Index] =3D Char; } =20 @@ -2650,11 +2669,16 @@ Returns: (VOID) DataIdx =3D Sd->mOutBuf - DecodeP (Sd) - 1; =20 BytesRemain--; + while ((INT16) (BytesRemain) >=3D 0) { - Sd->mDstBase[Sd->mOutBuf++] =3D Sd->mDstBase[DataIdx++]; if (Sd->mOutBuf >=3D Sd->mOrigSize) { goto Done ; } + if (DataIdx >=3D Sd->mOrigSize) { + Sd->mBadTableFlag =3D (UINT16) BAD_TABLE; + goto Done ; + } + Sd->mDstBase[Sd->mOutBuf++] =3D Sd->mDstBase[DataIdx++]; =20 BytesRemain--; } -- 2.10.0.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel