From nobody Sat Apr 20 07:33:23 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+43650+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1562914530; cv=none; d=zoho.com; s=zohoarc; b=oGhHbtSPEjIGOp/4HMOL6uzSE+jMVHDRIOCoucAm7VWcnh1SOo6t0FKpSpApE8zQ0km7tGBJ+fQXQP3rcal7xzakXU7y/ZU7DNz3Vw75nQngA/HulGWxPSBy2H5Ll5nTM+/p+2oiL/yXI6EmzHBIQcUieQApiSQijlRQMZW4BmE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562914530; h=Content-Type:Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To:ARC-Authentication-Results; bh=Bx9ozCV56s8/LOlkCfoMJKEjY+nuoJ1RenSst8iKIW4=; b=FKFVJIYWIXxO1Jt/gewA8/s798FSyWlq8dUNTWvsoRFSAwZig8q7jsG/uZIAiWr6YMXJby8NJ79HQww5USBNXdH3WsTip6U84BtvcK+skEjSYxR5XUUwkZI8WT25cUyrNOl+uXgU7oYavcYc9MsqALkixkLGewD85mXwi5pnx/E= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+43650+1787277+3901457@groups.io Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1562914530727641.5518703790704; Thu, 11 Jul 2019 23:55:30 -0700 (PDT) Return-Path: X-Received: from EUR03-VE1-obe.outbound.protection.outlook.com (EUR03-VE1-obe.outbound.protection.outlook.com [40.107.5.80]) by groups.io with SMTP; Thu, 11 Jul 2019 23:55:29 -0700 X-Received: from VI1PR08CA0094.eurprd08.prod.outlook.com (2603:10a6:800:d3::20) by AM5PR0802MB2593.eurprd08.prod.outlook.com (2603:10a6:203:94::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2052.16; Fri, 12 Jul 2019 06:55:25 +0000 X-Received: from DB5EUR03FT060.eop-EUR03.prod.protection.outlook.com (2a01:111:f400:7e0a::203) by VI1PR08CA0094.outlook.office365.com (2603:10a6:800:d3::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.2073.10 via Frontend Transport; Fri, 12 Jul 2019 06:55:24 +0000 Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+43650+1787277+3901457@groups.io; helo=web01.groups.io; Received-SPF: TempError (protection.outlook.com: error in processing during lookup of arm.com: DNS Timeout) X-Received: from nebula.arm.com (40.67.248.234) by DB5EUR03FT060.mail.protection.outlook.com (10.152.21.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.20.2052.18 via Frontend Transport; Fri, 12 Jul 2019 06:55:23 +0000 X-Received: from AZ-NEU-EX03.Arm.com (10.251.24.31) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1415.2; Fri, 12 Jul 2019 06:53:23 +0000 X-Received: from E119924.Arm.com (10.37.8.167) by mail.arm.com (10.251.24.31) with Microsoft SMTP Server id 15.1.1415.2 via Frontend Transport; Fri, 12 Jul 2019 06:53:22 +0000 From: "Krzysztof Koch" To: CC: , , , , , Subject: [edk2-devel] [PATCH v1 10/11] ShellPkg: acpiview: GTDT: Add error-checking in the parsing logic Date: Fri, 12 Jul 2019 07:52:42 +0100 Message-ID: <20190712065243.3812-11-krzysztof.koch@arm.com> In-Reply-To: <20190712065243.3812-1-krzysztof.koch@arm.com> References: <20190712065243.3812-1-krzysztof.koch@arm.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1471643a-c935-4bd5-cfe1-08d70695e95a X-MS-TrafficTypeDiagnostic: AM5PR0802MB2593: X-MS-Exchange-PUrlCount: 1 X-Microsoft-Antispam-PRVS: NoDisclaimer: True X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: +R9nIMYsw8dCl6Sh9kkQJ/4UZj3v6g0ZeGe8fY3l2K3pBgQli4g8IscVuK5C0OmUyHLiHMFWBlwN0TYtKDQUp8zIOC7O8WnT87syLABdUJbHFtyKRA3G5z5WtS79AqIQBtnvtFdYxLbQNFZ9/SVzlJnlM5/7Btub4JUIqFICcc4FeEjYBR0hs5ye0aae1MFOLV9OjB+mN9GQB+9EdQM/IyNEMQsn5+ZHsrP0RdVOLCt9qwY0uZdzUTuQMM0itFVZMMo9cmJSURGMZhXmIN96tzOKOrJrjuAkkcZcd4k0mFOu45Qn7gse+BGKk812gQcCNWtCdPV6I9l2ax02oYfeBygM76kSLHCtePl3CbKG24n3miwGSEXDIHL1m3KfVIAfqrh4KgfANAOY66iOfoW5eZzcmOFWCAIP5naDBG/e9qI= X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Jul 2019 06:55:23.1020 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1471643a-c935-4bd5-cfe1-08d70695e95a X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d;Ip=[40.67.248.234];Helo=[nebula.arm.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0802MB2593 Precedence: Bulk List-Unsubscribe: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,krzysztof.koch@arm.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1562914530; bh=ajvCKV025QTRxpaHFw1pZStTtmxsgIEIZCzTVAXDakM=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=jZrCm58VXdA3H5c1OlYxV0j4+h5ZVz8iOzdBhvLK6eOrqktabbWljpoTPzOVUKDsypC ufLQpWuXdyB1D45wikvF/YNAU9BaXZITNe9TGXk6KUSJLfCjXm/tRFSFZjZtsyRehrifu ftMy/tLimD3tmYDG1FWg2aiLDf4SuSWmVNs= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 parsing logic. 2. Test against buffer overruns. 3. Allow silencing ACPI table content validation errors which do not cause table parsing to fail. 4. Remove redundant forward function declarations by repositioning blocks of code. 5. Convert a 'do-while' loop for parsing GTDT table body into a 'while' block for consistency with other table parsers. Signed-off-by: Krzysztof Koch Reviewed-by: Alexei Fedorov --- Changes can be seen at: https://github.com/KrzysztofKoch1/edk2/commit/8c2ed= 18c7f1c44620eb86e1c9117cbccee8938ce Notes: v1: - improve the logic in the GTDT parser [Krzysztof] ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Gtdt/GtdtParser.c | 2= 94 +++++++++++--------- 1 file changed, 170 insertions(+), 124 deletions(-) diff --git a/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Gtdt/Gtdt= Parser.c b/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Gtdt/GtdtPa= rser.c index 3b05ff3015d4a3af62dd9fab057c32369a456267..4e8e6f3eb50596823827d20dbb7= 2314a583d0931 100644 --- a/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Gtdt/GtdtParser.c +++ b/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Gtdt/GtdtParser.c @@ -12,6 +12,10 @@ #include #include "AcpiParser.h" #include "AcpiTableParser.h" +#include "AcpiView.h" + +// "The number of GT Block Timers must be less than or equal to 8" +#define GT_BLOCK_TIMER_COUNT_MAX 8 =20 // Local variables STATIC CONST UINT32* GtdtPlatformTimerCount; @@ -20,7 +24,6 @@ STATIC CONST UINT8* PlatformTimerType; STATIC CONST UINT16* PlatformTimerLength; STATIC CONST UINT32* GtBlockTimerCount; STATIC CONST UINT32* GtBlockTimerOffset; -STATIC CONST UINT16* GtBlockLength; STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo; =20 /** @@ -36,7 +39,21 @@ EFIAPI ValidateGtBlockTimerCount ( IN UINT8* Ptr, IN VOID* Context - ); + ) +{ + UINT32 BlockTimerCount; + + BlockTimerCount =3D *(UINT32*)Ptr; + + if (BlockTimerCount > GT_BLOCK_TIMER_COUNT_MAX) { + IncrementErrorCount (); + Print ( + L"\nERROR: Timer Count =3D %d. Max Timer Count is %d.", + BlockTimerCount, + GT_BLOCK_TIMER_COUNT_MAX + ); + } +} =20 /** This function validates the GT Frame Number. @@ -51,7 +68,21 @@ EFIAPI ValidateGtFrameNumber ( IN UINT8* Ptr, IN VOID* Context - ); + ) +{ + UINT8 FrameNumber; + + FrameNumber =3D *(UINT8*)Ptr; + + if (FrameNumber >=3D GT_BLOCK_TIMER_COUNT_MAX) { + IncrementErrorCount (); + Print ( + L"\nERROR: GT Frame Number =3D %d. GT Frame Number must be in range = 0-%d.", + FrameNumber, + GT_BLOCK_TIMER_COUNT_MAX - 1 + ); + } +} =20 /** An ACPI_PARSER array describing the ACPI GTDT Table. @@ -96,7 +127,7 @@ STATIC CONST ACPI_PARSER GtPlatformTimerHeaderParser[] = =3D { **/ STATIC CONST ACPI_PARSER GtBlockParser[] =3D { {L"Type", 1, 0, L"%d", NULL, NULL, NULL, NULL}, - {L"Length", 2, 1, L"%d", NULL, (VOID**)&GtBlockLength, NULL, NULL}, + {L"Length", 2, 1, L"%d", NULL, NULL, NULL, NULL}, {L"Reserved", 1, 3, L"%x", NULL, NULL, NULL, NULL}, {L"Physical address (CntCtlBase)", 8, 4, L"0x%lx", NULL, NULL, NULL, NUL= L}, {L"Timer Count", 4, 12, L"%d", NULL, (VOID**)&GtBlockTimerCount, @@ -134,115 +165,71 @@ STATIC CONST ACPI_PARSER SBSAGenericWatchdogParser[]= =3D { {L"Watchdog Timer Flags", 4, 24, L"0x%x", NULL, NULL, NULL, NULL} }; =20 -/** - This function validates the GT Block timer count. - - @param [in] Ptr Pointer to the start of the field data. - @param [in] Context Pointer to context specific information e.g. this - could be a pointer to the ACPI table header. -**/ -STATIC -VOID -EFIAPI -ValidateGtBlockTimerCount ( - IN UINT8* Ptr, - IN VOID* Context - ) -{ - UINT32 BlockTimerCount; - - BlockTimerCount =3D *(UINT32*)Ptr; - - if (BlockTimerCount > 8) { - IncrementErrorCount (); - Print ( - L"\nERROR: Timer Count =3D %d. Max Timer Count is 8.", - BlockTimerCount - ); - } -} - -/** - This function validates the GT Frame Number. - - @param [in] Ptr Pointer to the start of the field data. - @param [in] Context Pointer to context specific information e.g. this - could be a pointer to the ACPI table header. -**/ -STATIC -VOID -EFIAPI -ValidateGtFrameNumber ( - IN UINT8* Ptr, - IN VOID* Context - ) -{ - UINT8 FrameNumber; - - FrameNumber =3D *(UINT8*)Ptr; - - if (FrameNumber > 7) { - IncrementErrorCount (); - Print ( - L"\nERROR: GT Frame Number =3D %d. GT Frame Number must be in range = 0-7.", - FrameNumber - ); - } -} - /** This function parses the Platform GT Block. =20 - @param [in] Ptr Pointer to the start of the GT Block data. - @param [in] Length Length of the GT Block structure. + @param [in] Ptr Pointer to the start of the GT Block data. + @param [in] Length Length of the GT Block structure. **/ STATIC VOID DumpGTBlock ( IN UINT8* Ptr, - IN UINT32 Length + IN UINT16 Length ) { UINT32 Index; UINT32 Offset; - UINT32 GTBlockTimerLength; =20 - Offset =3D ParseAcpi ( - TRUE, - 2, - "GT Block", - Ptr, - Length, - PARSER_PARAMS (GtBlockParser) - ); - GTBlockTimerLength =3D (*GtBlockLength - Offset) / (*GtBlockTimerCount); - Length -=3D Offset; + ParseAcpi ( + TRUE, + 2, + "GT Block", + Ptr, + Length, + PARSER_PARAMS (GtBlockParser) + ); =20 - if (*GtBlockTimerCount !=3D 0) { - Ptr +=3D (*GtBlockTimerOffset); - Index =3D 0; - while ((Index < (*GtBlockTimerCount)) && (Length >=3D GTBlockTimerLeng= th)) { - Offset =3D ParseAcpi ( - TRUE, - 2, - "GT Block Timer", - Ptr, - GTBlockTimerLength, - PARSER_PARAMS (GtBlockTimerParser) - ); - // Increment by GT Block Timer structure size - Ptr +=3D Offset; - Length -=3D Offset; - Index++; - } + // Check if the values used to control the parsing logic have been + // successfully read. + if ((GtBlockTimerCount =3D=3D NULL) || + (GtBlockTimerOffset =3D=3D NULL)) { + IncrementErrorCount (); + Print ( + L"ERROR: Insufficient GT Block Structure length. Length =3D %d.\n", + Length + ); + return; + } =20 - if (Length !=3D 0) { - IncrementErrorCount (); - Print ( - L"ERROR:GT Block Timer length mismatch. Unparsed %d bytes.\n", - Length - ); - } + Offset =3D *GtBlockTimerOffset; + Index =3D 0; + + // Parse the specified number of GT Block Timer Structures or the GT Blo= ck + // Structure buffer length. Whichever is minimum. + while ((Index++ < *GtBlockTimerCount) && + (Offset < Length)) { + Offset +=3D ParseAcpi ( + TRUE, + 2, + "GT Block Timer", + Ptr + Offset, + Length - Offset, + PARSER_PARAMS (GtBlockTimerParser) + ); + } + + // Cross-check the substructure count with the length of the encapsulati= ng + // buffer + if (GetConsistencyChecking () && + (Index < *GtBlockTimerCount)) { + IncrementErrorCount (); + Print ( + L"ERROR: Invalid GT Block Timer count. GtBlockTimerCount =3D %d. " \ + L"GtBlockBufferSize =3D %d.\n", + *GtBlockTimerCount, + Length + ); } } =20 @@ -295,6 +282,7 @@ ParseAcpiGtdt ( ) { UINT32 Index; + UINT32 Offset; UINT8* TimerPtr; =20 if (!Trace) { @@ -310,36 +298,94 @@ ParseAcpiGtdt ( PARSER_PARAMS (GtdtParser) ); =20 - if (*GtdtPlatformTimerCount !=3D 0) { - TimerPtr =3D Ptr + (*GtdtPlatformTimerOffset); - Index =3D 0; - do { - // Parse the Platform Timer Header - ParseAcpi ( - FALSE, - 0, - NULL, - TimerPtr, - 4, // GT Platform Timer structure header length. - PARSER_PARAMS (GtPlatformTimerHeaderParser) + // Check if the values used to control the parsing logic have been + // successfully read. + if ((GtdtPlatformTimerCount =3D=3D NULL) || + (GtdtPlatformTimerOffset =3D=3D NULL)) { + IncrementErrorCount (); + Print ( + L"ERROR: Insufficient table length. AcpiTableLength =3D %d.\n", + AcpiTableLength + ); + return; + } + + TimerPtr =3D Ptr + *GtdtPlatformTimerOffset; + Offset =3D *GtdtPlatformTimerOffset; + Index =3D 0; + + // Parse the specified number of Platform Timer Structures or the GTDT + // buffer length. Whichever is minimum. + while ((Index++ < *GtdtPlatformTimerCount) && + (Offset < AcpiTableLength)) { + // Parse the Platform Timer Header to obtain Length and Type + ParseAcpi ( + FALSE, + 0, + NULL, + TimerPtr, + AcpiTableLength - Offset, + PARSER_PARAMS (GtPlatformTimerHeaderParser) + ); + + // Check if the values used to control the parsing logic have been + // successfully read. + if ((PlatformTimerType =3D=3D NULL) || + (PlatformTimerLength =3D=3D NULL)) { + IncrementErrorCount (); + Print ( + L"ERROR: Insufficient remaining table buffer length to read the " \ + L"Platform Timer Structure header. Length =3D %d.\n", + AcpiTableLength - Offset ); - switch (*PlatformTimerType) { - case EFI_ACPI_6_2_GTDT_GT_BLOCK: - DumpGTBlock (TimerPtr, *PlatformTimerLength); - break; - case EFI_ACPI_6_2_GTDT_SBSA_GENERIC_WATCHDOG: - DumpWatchdogTimer (TimerPtr, *PlatformTimerLength); - break; - default: + return; + } + + // Make sure the Platform Timer is inside the table. + if ((Offset + *PlatformTimerLength) > AcpiTableLength) { + IncrementErrorCount (); + Print ( + L"ERROR: Invalid Platform Timer Structure length. " \ + L"PlatformTimerLength =3D %d. RemainingTableBufferLength =3D %d.= " \ + L"GTDT parsing aborted.\n", + *PlatformTimerLength, + AcpiTableLength - Offset + ); + return; + } + + switch (*PlatformTimerType) { + case EFI_ACPI_6_3_GTDT_GT_BLOCK: + DumpGTBlock (TimerPtr, *PlatformTimerLength); + break; + case EFI_ACPI_6_3_GTDT_SBSA_GENERIC_WATCHDOG: + DumpWatchdogTimer (TimerPtr, *PlatformTimerLength); + break; + default: + if (GetConsistencyChecking ()) { IncrementErrorCount (); Print ( - L"ERROR: INVALID Platform Timer Type =3D %d\n", + L"ERROR: Invalid Platform Timer Type =3D %d\n", *PlatformTimerType ); - break; - } // switch - TimerPtr +=3D (*PlatformTimerLength); - Index++; - } while (Index < *GtdtPlatformTimerCount); + } + break; + } // switch + + TimerPtr +=3D *PlatformTimerLength; + Offset +=3D *PlatformTimerLength; + } // while + + // Cross-check the substructure count with the length of the encapsulati= ng + // buffer + if (GetConsistencyChecking () && + (Index < *GtdtPlatformTimerCount)) { + IncrementErrorCount (); + Print ( + L"ERROR: Invalid Platform Timer Structure count. " \ + L"GtdtPlatformTimerCount =3D %d. AcpiTableLength =3D %d.\n", + *GtdtPlatformTimerCount, + AcpiTableLength + ); } } -- 'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)' -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#43650): https://edk2.groups.io/g/devel/message/43650 Mute This Topic: https://groups.io/mt/32439515/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-