From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.24, mailfrom: zhichao.gao@intel.com) Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by groups.io with SMTP; Thu, 18 Jul 2019 22:39:23 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Jul 2019 22:39:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,280,1559545200"; d="scan'208";a="319869963" Received: from fmsmsx106.amr.corp.intel.com ([10.18.124.204]) by orsmga004.jf.intel.com with ESMTP; 18 Jul 2019 22:39:21 -0700 Received: from FMSMSX110.amr.corp.intel.com (10.18.116.10) by FMSMSX106.amr.corp.intel.com (10.18.124.204) with Microsoft SMTP Server (TLS) id 14.3.439.0; Thu, 18 Jul 2019 22:39:21 -0700 Received: from shsmsx108.ccr.corp.intel.com (10.239.4.97) by fmsmsx110.amr.corp.intel.com (10.18.116.10) with Microsoft SMTP Server (TLS) id 14.3.439.0; Thu, 18 Jul 2019 22:39:21 -0700 Received: from shsmsx101.ccr.corp.intel.com ([169.254.1.134]) by SHSMSX108.ccr.corp.intel.com ([169.254.8.174]) with mapi id 14.03.0439.000; Fri, 19 Jul 2019 13:39:19 +0800 From: "Gao, Zhichao" To: Krzysztof Koch , "devel@edk2.groups.io" CC: "Carsey, Jaben" , "Ni, Ray" , "Sami.Mujawar@arm.com" , "Matteo.Carlini@arm.com" , "nd@arm.com" Subject: Re: [PATCH v1 11/11] ShellPkg: acpiview: DBG2: Add error-checking in the parsing logic Thread-Topic: [PATCH v1 11/11] ShellPkg: acpiview: DBG2: Add error-checking in the parsing logic Thread-Index: AQHVOH7Uk/Q7M+nrUESKVdFWQuaPbabRdBDg Date: Fri, 19 Jul 2019 05:39:18 +0000 Message-ID: <3CE959C139B4C44DBEA1810E3AA6F9000B808F1E@SHSMSX101.ccr.corp.intel.com> References: <20190712065243.3812-1-krzysztof.koch@arm.com> <20190712065243.3812-12-krzysztof.koch@arm.com> In-Reply-To: <20190712065243.3812-12-krzysztof.koch@arm.com> 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: zhichao.gao@intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Two comments with this patch. > -----Original Message----- > From: Krzysztof Koch [mailto:krzysztof.koch@arm.com] > Sent: Friday, July 12, 2019 2:53 PM > To: devel@edk2.groups.io > Cc: Carsey, Jaben ; Ni, Ray ; > Gao, Zhichao ; Sami.Mujawar@arm.com; > Matteo.Carlini@arm.com; nd@arm.com > Subject: [PATCH v1 11/11] ShellPkg: acpiview: DBG2: Add error-checking in > the parsing logic >=20 > 1. Check if the global pointers (in the scope of this ACPI table parser) = have > been successfully updated before they are later used to control the parsi= ng > logic. >=20 > 2. Remove redundant forward function declarations by repositioning blocks > of code. >=20 > 3. Test against buffer overruns. >=20 > 4. Introduce a ACPI_PARSER array for parsing the header of the debug devi= ce > information structure. This way, the length of the buffer storing a debug > device information structure instance can be passed to > DumpDbgDeviceInfo(). Consequently, the parsing logic becomes consistent > with other ACPI table parsers and tests against buffer overrruns are simp= ler > to implement. >=20 > 5. Modify the signature of DumpGasStruct() function inside AcpiParser.c t= o > facilitate protection against buffer overruns in the DBG2 parser. >=20 > Signed-off-by: Krzysztof Koch > --- >=20 > Changes can be seen at: > https://github.com/KrzysztofKoch1/edk2/commit/530b059a9fe4aa9f1df36b4 > 07f97d76acaab8b74 >=20 > Notes: > v1: > - improve the logic in the DBG2 parser [Krzysztof] >=20 > ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.c |= 26 +- > ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.h |= 8 +- >=20 > ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Dbg2/Dbg2Parser.c > | 298 ++++++++++++++------ > 3 files changed, 225 insertions(+), 107 deletions(-) >=20 > diff --git a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.c > b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.c > index > 8b3153516d2b7d9b920ab2de0344c17798ac572c..2d6ff80e299eebe7853061d3 > db89332197c0dc0e 100644 > --- a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.c > +++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.c > @@ -589,23 +589,27 @@ STATIC CONST ACPI_PARSER GasParser[] =3D { >=20 > @param [in] Ptr Pointer to the start of the buffer. > @param [in] Indent Number of spaces to indent the output. > + @param [in] Length Length of the GAS structure buffer. > + > + @retval Number of bytes parsed. > **/ > -VOID > +UINT32 > EFIAPI > DumpGasStruct ( > IN UINT8* Ptr, > - IN UINT32 Indent > + IN UINT32 Indent, > + IN UINT32 Length > ) > { > Print (L"\n"); > - ParseAcpi ( > - TRUE, > - Indent, > - NULL, > - Ptr, > - GAS_LENGTH, > - PARSER_PARAMS (GasParser) > - ); > + return ParseAcpi ( > + TRUE, > + Indent, > + NULL, > + Ptr, > + Length, > + PARSER_PARAMS (GasParser) > + ); > } >=20 > /** > @@ -621,7 +625,7 @@ DumpGas ( > IN UINT8* Ptr > ) > { > - DumpGasStruct (Ptr, 2); > + DumpGasStruct (Ptr, 2, GAS_LENGTH); > } >=20 > /** > diff --git a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.h > b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.h > index > 7657892d9fd2e2e14c6578611ff0cf1b6f6cd750..20ca358bddfa5953bfb1d1beba > ebbf3079eaba01 100644 > --- a/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.h > +++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/AcpiParser.h > @@ -405,12 +405,16 @@ ParseAcpi ( >=20 > @param [in] Ptr Pointer to the start of the buffer. > @param [in] Indent Number of spaces to indent the output. > + @param [in] Length Length of the GAS structure buffer. > + > + @retval Number of bytes parsed. > **/ > -VOID > +UINT32 > EFIAPI > DumpGasStruct ( > IN UINT8* Ptr, > - IN UINT32 Indent > + IN UINT32 Indent, > + IN UINT32 Length > ); >=20 > /** > diff --git > a/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Dbg2/Dbg2Parse > r.c > b/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Dbg2/Dbg2Parse > r.c > index > 8de5ebf74775bab8e765849cba6ef4eb6f659a5a..2bbd622ffb7cec0a340de3e10 > bdcd01ba4d330df 100644 > --- > a/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Dbg2/Dbg2Parse > r.c > +++ > b/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Dbg2/Dbg2Pars > +++ er.c > @@ -12,6 +12,7 @@ > #include > #include "AcpiParser.h" > #include "AcpiTableParser.h" > +#include "AcpiView.h" >=20 > // Local variables pointing to the table fields STATIC CONST UINT32* > OffsetDbgDeviceInfo; @@ -27,7 +28,7 @@ STATIC CONST UINT16* > AddrSizeOffset; STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo; >=20 > /** > - This function Validates the NameSpace string length. > + This function validates the NameSpace string length. >=20 > @param [in] Ptr Pointer to the start of the buffer. > @param [in] Context Pointer to context specific information e.g. this = @@ - > 37,24 +38,23 @@ STATIC VOID EFIAPI ValidateNameSpaceStrLen ( > - IN UINT8* Ptr, > - IN VOID* Context > - ); > + IN UINT8* Ptr, > + IN VOID* Context > + ) > +{ > + UINT16 NameSpaceStrLen; >=20 > -/** > - This function parses the debug device information structure. > + NameSpaceStrLen =3D *(UINT16*)Ptr; >=20 > - @param [in] Ptr Pointer to the start of the buffer. > - @param [out] Length Pointer in which the length of the debug > - device information is returned. > -**/ > -STATIC > -VOID > -EFIAPI > -DumpDbgDeviceInfo ( > - IN UINT8* Ptr, > - OUT UINT32* Length > - ); > + if (NameSpaceStrLen < 2) { > + IncrementErrorCount (); > + Print ( > + L"\nERROR: NamespaceString Length =3D %d. If no Namespace device > exists, " \ > + L"NamespaceString[] must contain a period '.'", > + NameSpaceStrLen > + ); > + } > +} >=20 > /// An ACPI_PARSER array describing the ACPI DBG2 table. > STATIC CONST ACPI_PARSER Dbg2Parser[] =3D { @@ -65,10 +65,17 @@ STATIC > CONST ACPI_PARSER Dbg2Parser[] =3D { > (VOID**)&NumberDbgDeviceInfo, NULL, NULL} }; >=20 > +/// An ACPI_PARSER array describing the debug device information > +structure /// header. > +STATIC CONST ACPI_PARSER DbgDevInfoHeaderParser[] =3D { > + {L"Revision", 1, 0, L"0x%x", NULL, NULL, NULL, NULL}, > + {L"Length", 2, 1, L"%d", NULL, (VOID**)&DbgDevInfoLen, NULL, NULL} }; > + > /// An ACPI_PARSER array describing the debug device information. > STATIC CONST ACPI_PARSER DbgDevInfoParser[] =3D { > {L"Revision", 1, 0, L"0x%x", NULL, NULL, NULL, NULL}, > - {L"Length", 2, 1, L"%d", NULL, (VOID**)&DbgDevInfoLen, NULL, NULL}, > + {L"Length", 2, 1, L"%d", NULL, NULL, NULL, NULL}, >=20 > {L"Generic Address Registers Count", 1, 3, L"0x%x", NULL, > (VOID**)&GasCount, NULL, NULL}, > @@ -91,108 +98,152 @@ STATIC CONST ACPI_PARSER DbgDevInfoParser[] =3D > { > (VOID**)&AddrSizeOffset, NULL, NULL} }; >=20 > -/** > - This function validates the NameSpace string length. > - > - @param [in] Ptr Pointer to the start of the buffer. > - @param [in] Context Pointer to context specific information e.g. this > - could be a pointer to the ACPI table header. > -**/ > -STATIC > -VOID > -EFIAPI > -ValidateNameSpaceStrLen ( > - IN UINT8* Ptr, > - IN VOID* Context > - ) > -{ > - UINT16 NameSpaceStrLen; > - > - NameSpaceStrLen =3D *(UINT16*)Ptr; > - > - if (NameSpaceStrLen < 2) { > - IncrementErrorCount (); > - Print ( > - L"\nERROR: NamespaceString Length =3D %d. If no Namespace device > exists,\n" > - L" then NamespaceString[] must contain a period '.'", > - NameSpaceStrLen > - ); > - } > -} > - > /** > This function parses the debug device information structure. >=20 > - @param [in] Ptr Pointer to the start of the buffer. > - @param [out] Length Pointer in which the length of the debug > - device information is returned. > + @param [in] Ptr Pointer to the start of the buffer. > + @param [in] Length Length of the debug device information structure. > **/ > STATIC > VOID > EFIAPI > DumpDbgDeviceInfo ( > - IN UINT8* Ptr, > - OUT UINT32* Length > + IN UINT8* Ptr, > + IN UINT16 Length > ) > { > UINT16 Index; > - UINT8* DataPtr; > - UINT32* AddrSize; > - > - // Parse the debug device info to get the Length > - ParseAcpi ( > - FALSE, > - 0, > - "Debug Device Info", > - Ptr, > - 3, // Length is 2 bytes starting at offset 1 > - PARSER_PARAMS (DbgDevInfoParser) > - ); > + UINT16 Offset; >=20 > ParseAcpi ( > TRUE, > 2, > "Debug Device Info", > Ptr, > - *DbgDevInfoLen, > + Length, > PARSER_PARAMS (DbgDevInfoParser) > ); >=20 > - // GAS and Address Size > + // Check if the values used to control the parsing logic have been > + // successfully read. > + if ((GasCount =3D=3D NULL) || > + (NameSpaceStringLength =3D=3D NULL) || > + (NameSpaceStringOffset =3D=3D NULL) || > + (OEMDataLength =3D=3D NULL) || > + (OEMDataOffset =3D=3D NULL) || > + (BaseAddrRegOffset =3D=3D NULL) || > + (AddrSizeOffset =3D=3D NULL)) { > + IncrementErrorCount (); > + Print ( > + L"ERROR: Insufficient Debug Device Information Structure length. "= \ > + L"Length =3D %d.\n", > + Length > + ); > + return; > + } > + > + // GAS > Index =3D 0; > - DataPtr =3D Ptr + (*BaseAddrRegOffset); > - AddrSize =3D (UINT32*)(Ptr + (*AddrSizeOffset)); > - while (Index < (*GasCount)) { > + Offset =3D *BaseAddrRegOffset; > + while ((Index++ < *GasCount) && > + (Offset < Length)) { > PrintFieldName (4, L"BaseAddressRegister"); > - DumpGasStruct (DataPtr, 4); > + Offset +=3D (UINT16)DumpGasStruct ( > + Ptr + Offset, > + 4, > + Length - Offset > + ); > + } > + > + // Cross-check the substructure count with the length of the > + encapsulating // buffer if (GetConsistencyChecking () && > + (Index < *GasCount)) { > + IncrementErrorCount (); > + Print ( > + L"ERROR: Invalid GAS count. GasCount =3D %d. DbgDevInfoLen =3D %d.= \n", > + *GasCount, > + Length > + ); > + return; > + } > + > + // Make sure the array of address sizes corresponding to each GAS fit > + in the // Debug Device Information structure if ((*AddrSizeOffset + > + (*GasCount * sizeof (UINT32))) > Length) { > + IncrementErrorCount (); > + Print ( > + L"ERROR: Invalid GAS count. GasCount =3D %d. RemainingBufferLength > =3D %d. " \ > + L"Parsing of the Debug Device Information structure aborted.\n", > + *GasCount, > + Length - *AddrSizeOffset > + ); > + return; > + } > + > + // Address Size > + Index =3D 0; > + Offset =3D *AddrSizeOffset; > + while ((Index++ < *GasCount) && > + (Offset < Length)) { > PrintFieldName (4, L"Address Size"); > - Print (L"0x%x\n", AddrSize[Index]); > - DataPtr +=3D GAS_LENGTH; > - Index++; > + Print (L"0x%x\n", *((UINT32*)(Ptr + Offset))); > + Offset +=3D sizeof (UINT32); 1. Why use sizof (UINT32) to replace GAS_LENGTH? The MACRO make a good view= and meaningful. > } >=20 > // NameSpace String > Index =3D 0; > - DataPtr =3D Ptr + (*NameSpaceStringOffset); > + Offset =3D *NameSpaceStringOffset; > PrintFieldName (4, L"NameSpace String"); > - while (Index < (*NameSpaceStringLength)) { > - Print (L"%c", DataPtr[Index++]); > + while ((Index++ < *NameSpaceStringLength) && > + (Offset < Length)) { > + Print (L"%c", *(Ptr + Offset)); > + Offset++; > } > Print (L"\n"); >=20 > + // Cross-check the string length with the size of the encapsulating > + // buffer if (GetConsistencyChecking () && > + (Index < *NameSpaceStringLength)) { > + IncrementErrorCount (); > + Print ( > + L"ERROR: Invalid NameSpaceString length. NameSpaceStringLength =3D= %d. > " \ > + L"DbgDevInfoLen =3D %d.\n", > + *NameSpaceStringLength, > + Length > + ); > + return; > + } > + > // OEM Data > - Index =3D 0; > - DataPtr =3D Ptr + (*OEMDataOffset); > - PrintFieldName (4, L"OEM Data"); > - while (Index < (*OEMDataLength)) { > - Print (L"%x ", DataPtr[Index++]); > - if ((Index & 7) =3D=3D 0) { > - Print (L"\n%-*s ", OUTPUT_FIELD_COLUMN_WIDTH, L""); > + if (*OEMDataOffset !=3D 0) { > + Index =3D 0; > + Offset =3D *OEMDataOffset; > + PrintFieldName (4, L"OEM Data"); > + while ((Index++ < *OEMDataLength) && > + (Offset < Length)) { > + Print (L"%x ", *(Ptr + Offset)); > + if ((Index & 7) =3D=3D 0) { > + Print (L"\n%-*s ", OUTPUT_FIELD_COLUMN_WIDTH, L""); > + } > + Offset++; > } > + Print (L"\n"); > } > - Print (L"\n"); >=20 > - *Length =3D *DbgDevInfoLen; > + // Cross-check the OEM data length with the size of the encapsulating > + // buffer if (GetConsistencyChecking () && > + (Index < *OEMDataLength)) { > + IncrementErrorCount (); > + Print ( > + L"ERROR: Invalid OEM Data size. OEMDataLength =3D %d. " \ > + L"DbgDevInfoLen =3D %d.\n", > + *OEMDataLength, > + Length > + ); > + } > } >=20 > /** > @@ -217,8 +268,7 @@ ParseAcpiDbg2 ( > ) > { > UINT32 Offset; > - UINT32 DbgDeviceInfoLength; > - UINT8* DevInfoPtr; > + UINT32 Index; >=20 > if (!Trace) { > return; > @@ -232,14 +282,74 @@ ParseAcpiDbg2 ( > AcpiTableLength, > PARSER_PARAMS (Dbg2Parser) > ); > - DevInfoPtr =3D Ptr + Offset; >=20 > - while (Offset < AcpiTableLength) { > - DumpDbgDeviceInfo ( > - DevInfoPtr, > - &DbgDeviceInfoLength > + // Check if the values used to control the parsing logic have been > + // successfully read. > + if ((OffsetDbgDeviceInfo =3D=3D NULL) || > + (NumberDbgDeviceInfo =3D=3D NULL)) { > + IncrementErrorCount (); > + Print ( > + L"ERROR: Insufficient table length. AcpiTableLength =3D %d\n", > + AcpiTableLength > + ); > + return; > + } > + > + Offset =3D *OffsetDbgDeviceInfo; > + Index =3D 0; > + > + while (Index++ < *NumberDbgDeviceInfo) { > + > + // Parse the Debug Device Information Structure header to obtain Len= gth > + ParseAcpi ( > + FALSE, > + 0, > + NULL, > + Ptr + Offset, > + AcpiTableLength - Offset, > + PARSER_PARAMS (DbgDevInfoHeaderParser) > + ); 2. Here adding the DbgDevInfoHeaderParser to get the DbgDevInfoLen. This ma= y be conflict with 4/10. 4/10 removes the get header section. I think it is= fine to get the whole devinfo if the Length is valid. And the DbgDevInfoHe= aderParser isn't required to add. Thanks, Zhichao > + > + // Check if the values used to control the parsing logic have been > + // successfully read. > + if (DbgDevInfoLen =3D=3D NULL) { > + IncrementErrorCount (); > + Print ( > + L"ERROR: Insufficient remaining table buffer length to read the = " \ > + L"Debug Device Information structure's 'Length' field. " \ > + L"RemainingTableBufferLength =3D %d.\n", > + AcpiTableLength - Offset > + ); > + return; > + } > + > + // Make sure the Debug Device Information structure lies inside the = table. > + if ((Offset + *DbgDevInfoLen) > AcpiTableLength) { > + IncrementErrorCount (); > + Print ( > + L"ERROR: Invalid Debug Device Information structure length. " \ > + L"DbgDevInfoLen =3D %d. RemainingTableBufferLength =3D %d. " \ > + L"DBG2 parsing aborted.\n", > + *DbgDevInfoLen, > + AcpiTableLength - Offset > + ); > + return; > + } > + > + DumpDbgDeviceInfo (Ptr + Offset, (*DbgDevInfoLen)); > + Offset +=3D (*DbgDevInfoLen); > + } > + > + // Cross-check the substructure count with the length of the > + encapsulating // buffer if (GetConsistencyChecking () && > + (Index < *NumberDbgDeviceInfo)) { > + IncrementErrorCount (); > + Print ( > + L"ERROR: Invalid Debug Device Information structure count. " \ > + L"NumberDbgDeviceInfo =3D %d. AcpiTableLength =3D %d.\n", > + *NumberDbgDeviceInfo, > + AcpiTableLength > ); > - Offset +=3D DbgDeviceInfoLength; > - DevInfoPtr +=3D DbgDeviceInfoLength; > } > } > -- > 'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)' >=20