From mboxrd@z Thu Jan 1 00:00:00 1970 Subject: Re: [edk2-devel] [PATCH] IntelFsp2Pkg/Library/BaseFspCommonLib: Fix OVERRUN Coverity issue To: Ranbir Singh ,devel@edk2.groups.io From: "Ranbir Singh" X-Originating-Location: Bengaluru, Karnataka, IN (122.172.85.38) X-Originating-Platform: Windows Chrome 108 User-Agent: GROUPS.IO Web Poster MIME-Version: 1.0 Date: Tue, 03 Jan 2023 20:39:30 -0800 References: In-Reply-To: Message-ID: <10036.1672807170137038569@groups.io> Content-Type: multipart/alternative; boundary="cdYWIWk7Ulsj3kqrxSew" --cdYWIWk7Ulsj3kqrxSew Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Copying patch contents directly in the message below ... >>From e0df5b0a34f1b96aa9610c5eef9161cbbca717d2 Mon Sep 17 00:00:00 2001 From: Ranbir Singh Date: Thu, 22 Dec 2022 10:57:21 +0530 Subject: [PATCH] IntelFsp2Pkg/Library/BaseFspCommonLib: Fix OVERRUN Coverit= y issue FspData->PerfIdx is getting increased for every call unconditionally in the function SetFspMeasurePoint and hence memory access can happen for out of bound FspData->PerfData[] array entries also. Example - FspData->PerfData is an array of 32 UINT64 entries. Assume a call is made to SetFspMeasurePoint function when the FspData->PerfIdx last value is 31. It gets incremented to 32 at line 400. Any subsequent call to SetFspMeasurePoint functions leads to FspData->PerfData[32] getting accessed which is out of the PerfData array as well as the FSP_GLOBAL_DATA structure boundary. Hence keep array access and index increment inside if block only and return invalid performance timestamp when PerfIdx is invalid. REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4200 Signed-off-by: Ranbir Singh --- IntelFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/IntelFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c b/IntelFs= p2Pkg/Library/BaseFspCommonLib/FspCommonLib.c index a22b0e7825..cda2a7b247 100644 --- a/IntelFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c +++ b/IntelFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c @@ -377,7 +377,8 @@ GetFspSiliconInitUpdDataPointer ( @param[in] Id=C2=A0 =C2=A0 =C2=A0 =C2=A0Measurement point ID. -=C2=A0 @return performance timestamp. +=C2=A0 @return performance timestamp if current PerfIdx is valid, +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 else return 0 as invalid performance ti= mestamp **/ UINT64 EFIAPI @@ -395,9 +396,10 @@ SetFspMeasurePoint ( if (FspData->PerfIdx < sizeof (FspData->PerfData) / sizeof (FspData->PerfDa= ta[0])) { FspData->PerfData[FspData->PerfIdx]=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =3D AsmReadTsc (); ((UINT8 *)(&FspData->PerfData[FspData->PerfIdx]))[7] =3D Id; +=C2=A0 =C2=A0 return FspData->PerfData[(FspData->PerfIdx)++]; } -=C2=A0 return FspData->PerfData[(FspData->PerfIdx)++]; +=C2=A0 return (UINT64)0x0000000000000000; } /** -- 2.36.1.windows.1 --cdYWIWk7Ulsj3kqrxSew Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Copying patch contents directly= in the message below ...

From e0df5b0a34f1b96aa9610c5eef9161cbbca717d2 Mon Sep 17 00:00:00 2001=
From: Ranbir Singh <Ranbir.Singh3@Dell.com>
Date: Thu, 22 Dec 2022 10:57:21 +0530
Subject: [PATCH] IntelFsp2Pkg/Library/BaseFspCommonLib: Fix OVERRUN Co= verity
 issue
 
FspData->PerfIdx is getting increased for every call unconditionall= y
in the function SetFspMeasurePoint and hence memory access can happen<= /div>
for out of bound FspData->PerfData[] array entries also.
 
Example -
   FspData->PerfData is an array of 32 UINT64 entries. As= sume a call
   is made to SetFspMeasurePoint function when the FspData-&= gt;PerfIdx
   last value is 31. It gets incremented to 32 at line 400.<= /div>
   Any subsequent call to SetFspMeasurePoint functions leads= to
   FspData->PerfData[32] getting accessed which is out of= the PerfData
   array as well as the FSP_GLOBAL_DATA structure boundary.<= /div>
 
Hence keep array access and index increment inside if block only and
return invalid performance timestamp when PerfIdx is invalid.
 
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4200
Signed-off-by: Ranbir Singh <Ranbir.Singh3@Dell.com>
---
 IntelFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c | 6 ++++--<= /div>
 1 file changed, 4 insertions(+), 2 deletions(-)
 
diff --git a/IntelFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c b/In= telFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c
index a22b0e7825..cda2a7b247 100644
--- a/IntelFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c
+++ b/IntelFsp2Pkg/Library/BaseFspCommonLib/FspCommonLib.c
@@ -377,7 +377,8 @@ GetFspSiliconInitUpdDataPointer (
 
   @param[in] Id       Measurement point= ID.
 
-  @return performance timestamp.
+  @return performance timestamp if current PerfIdx is valid,
+          else return 0 as invalid performan= ce timestamp
 **/
 UINT64
 EFIAPI
@@ -395,9 +396,10 @@ SetFspMeasurePoint (
   if (FspData->PerfIdx < sizeof (FspData->PerfData= ) / sizeof (FspData->PerfData[0])) {
     FspData->PerfData[FspData->PerfIdx]  &n= bsp;               =3D AsmReadTsc ();
     ((UINT8 *)(&FspData->PerfData[FspData->P= erfIdx]))[7] =3D Id;
+    return FspData->PerfData[(FspData->PerfIdx)++];
   }
 
-  return FspData->PerfData[(FspData->PerfIdx)++];
+  return (UINT64)0x0000000000000000;
 }
 
 /**
--
2.36.1.windows.1
--cdYWIWk7Ulsj3kqrxSew--