From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web10.3657.1576479519715299614 for ; Sun, 15 Dec 2019 22:58:39 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.43, mailfrom: shenglei.zhang@intel.com) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Dec 2019 22:58:39 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.69,320,1571727600"; d="scan'208";a="212132574" Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by fmsmga008.fm.intel.com with ESMTP; 15 Dec 2019 22:58:39 -0800 Received: from fmsmsx117.amr.corp.intel.com (10.18.116.17) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.439.0; Sun, 15 Dec 2019 22:58:39 -0800 Received: from shsmsx103.ccr.corp.intel.com (10.239.4.69) by fmsmsx117.amr.corp.intel.com (10.18.116.17) with Microsoft SMTP Server (TLS) id 14.3.439.0; Sun, 15 Dec 2019 22:58:38 -0800 Received: from shsmsx104.ccr.corp.intel.com ([169.254.5.90]) by SHSMSX103.ccr.corp.intel.com ([169.254.4.29]) with mapi id 14.03.0439.000; Mon, 16 Dec 2019 14:58:37 +0800 From: "Zhang, Shenglei" To: "devel@edk2.groups.io" , "lersek@redhat.com" CC: "Yao, Jiewen" , "Wang, Jian J" , "Zhang, Chao B" Subject: Re: [edk2-devel] [PATCH] SecurityPkg/Tpm2Help.c: Add boundary check for array Thread-Topic: [edk2-devel] [PATCH] SecurityPkg/Tpm2Help.c: Add boundary check for array Thread-Index: AQHVrBdWGlDy3TORwEGt1AU3cWORJ6e8ZApw Date: Mon, 16 Dec 2019 06:58:37 +0000 Message-ID: References: <20191206014933.36648-1-shenglei.zhang@intel.com> In-Reply-To: Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Return-Path: shenglei.zhang@intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable > -----Original Message----- > From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of > Laszlo Ersek > Sent: Friday, December 6, 2019 5:27 PM > To: devel@edk2.groups.io; Zhang, Shenglei > Cc: Yao, Jiewen ; Wang, Jian J > ; Zhang, Chao B > Subject: Re: [edk2-devel] [PATCH] SecurityPkg/Tpm2Help.c: Add boundary > check for array >=20 > On 12/06/19 02:49, Zhang, Shenglei wrote: > > Add 'Index < HASH_COUNT' to ensure things out of boundary > > of digests[] can not be visited. > > > > Cc: Jiewen Yao > > Cc: Jian J Wang > > Cc: Chao Zhang > > Signed-off-by: Shenglei Zhang > > --- > > SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c > b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c > > index 36c240d1221c..a7d4e3ab5373 100644 > > --- a/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c > > +++ b/SecurityPkg/Library/Tpm2CommandLib/Tpm2Help.c > > @@ -299,7 +299,7 @@ GetDigestListSize ( > > UINT32 TotalSize; > > > > TotalSize =3D sizeof(DigestList->count); > > - for (Index =3D 0; Index < DigestList->count; Index++) { > > + for (Index =3D 0; Index < DigestList->count, Index < HASH_COUNT; In= dex++) > { > > DigestSize =3D GetHashSizeFromAlgo (DigestList->digests[Index].ha= shAlg); > > TotalSize +=3D sizeof(DigestList->digests[Index].hashAlg) + Diges= tSize; > > } > > >=20 > Nacked-by: Laszlo Ersek >=20 > The comma operator is either functionally wrong in this context, or > stylistically wrong. From the C standard: >=20 > The left operand of a comma operator is evaluated as a void > expression; there is a sequence point after its evaluation. Then the > right operand is evaluated; the result has its type and value. [...] >=20 > In case we *only* need to check (Index < HASH_COUNT), then the patch is > stylistically incorrect: the (Index < DigestList->count) condition > should simply be removed. >=20 > In case we need to check *both* conditions, then the patch is > functionally wrong: we should either use the logical AND (&&) operator, > instead of the comma: >=20 > Index < DigestList->count && Index < HASH_COUNT >=20 Hi Laszlo, You are right. I'll change the statement to include both conditions. Thanks, Shenglei > or invoke the MIN() function-like macro: >=20 > Index < MIN ((UINTN)DigestList->count, (UINTN)HASH_COUNT) >=20 > Thanks > Laszlo >=20 >=20 >=20