From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=34.238.86.106; helo=mail.paulo.ac; envelope-from=paulo@paulo.ac; receiver=edk2-devel@lists.01.org Received: from mail.paulo.ac (mail.paulo.ac [34.238.86.106]) (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 205C1222DDBEF for ; Sun, 14 Jan 2018 16:18:54 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.paulo.ac (Postfix) with ESMTP id 3AA6BC0888F; Mon, 15 Jan 2018 00:24:11 +0000 (UTC) X-Virus-Scanned: amavisd-new at paulo.ac X-Spam-Flag: NO X-Spam-Score: -1.099 X-Spam-Level: X-Spam-Status: No, score=-1.099 tagged_above=-999 required=6.31 tests=[ALL_TRUSTED=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no Authentication-Results: mail.paulo.ac (amavisd-new); dkim=pass (1024-bit key) header.d=paulo.ac Received: from mail.paulo.ac ([127.0.0.1]) by localhost (mail.paulo.ac [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KOqW0UjV4Uny; Mon, 15 Jan 2018 00:24:08 +0000 (UTC) Received: from thor.domain.name (177.204.15.215.dynamic.adsl.gvt.net.br [177.204.15.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.paulo.ac (Postfix) with ESMTPSA id 656BCC08891; Mon, 15 Jan 2018 00:24:04 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.paulo.ac 656BCC08891 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=paulo.ac; s=default; t=1515975846; bh=Q+mwrfhWLb6d1pSB8tAKdl9SZZTuiGaQs78zX3ALDbc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:In-Reply-To: References:From; b=Zrps696/zD2RbCgiRkcjrzdeSULR6tAXFJJ6OYcHjQJaP5YUHwA8MmL+GyxRSKkQK rr1FUyG9Ccb6k+hqqwENZbQ1Jl9sIqSW0Fa58KVPBLQCQoymTw3FIv27OrdyKQyF8K 7EUUYWZO0nbFQVFK44wu1JpUsm6XvghkmNYcRzIo= From: Paulo Alcantara To: edk2-devel@lists.01.org Cc: Paulo Alcantara , Eric Dong , Laszlo Ersek Date: Sun, 14 Jan 2018 22:23:35 -0200 Message-Id: X-Mailer: git-send-email 2.14.3 In-Reply-To: References: In-Reply-To: References: Subject: [RFC v5 7/8] UefiCpuPkg/CpuExceptionHandlerLib: Validate memory address ranges X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 15 Jan 2018 00:18:54 -0000 Introduce a new IsLinearAddressRangeValid() function to validate a given address range and check whether or not it is valid. This function is useful for validating ranges of memory addresses during stack traces in X64. Contributed-under: TianoCore Contribution Agreement 1.1 Cc: Eric Dong Cc: Laszlo Ersek Requested-by: Brian Johnson Signed-off-by: Paulo Alcantara --- UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c | 40 ++++++++++++++++++++ UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h | 18 +++++++++ UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c | 40 ++++++++++++-------- 3 files changed, 83 insertions(+), 15 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c index 7ac13640de..e1dd054259 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.c @@ -589,3 +589,43 @@ IsLinearAddressValid ( return AddressValid; } + +/** + Check if a linear address range is valid. + + @param[in] Cr0 CR0 control register. + @param[in] Cr3 CR3 control register. + @param[in] Cr4 CR4 control register. + @param[in] LinearAddressStart Linear address start. + @param[in] LinearAddressEnd Linear address end. +**/ +BOOLEAN +IsLinearAddressRangeValid ( + IN UINTN Cr0, + IN UINTN Cr3, + IN UINTN Cr4, + IN UINTN LinearAddressStart, + IN UINTN LinearAddressEnd + ) +{ + // + // Check for valid input parameters + // + if (LinearAddressStart == 0 || LinearAddressEnd == 0 || + LinearAddressStart > LinearAddressEnd) { + return FALSE; + } + + // + // Validate all linear addresses within the given range + // + for (LinearAddressStart &= ~(SIZE_4KB - 1); + LinearAddressStart <= LinearAddressEnd; + LinearAddressStart += SIZE_4KB) { + if (!IsLinearAddressValid (Cr0, Cr3, Cr4, LinearAddressStart)) { + return FALSE; + } + } + + return TRUE; +} diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h index 1b51034c25..075f668290 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/CpuExceptionCommon.h @@ -346,5 +346,23 @@ IsLinearAddressValid ( IN UINTN LinearAddress ); +/** + Check if a linear address range is valid. + + @param[in] Cr0 CR0 control register. + @param[in] Cr3 CR3 control register. + @param[in] Cr4 CR4 control register. + @param[in] LinearAddressStart Linear address start. + @param[in] LinearAddressEnd Linear address end. +**/ +BOOLEAN +IsLinearAddressRangeValid ( + IN UINTN Cr0, + IN UINTN Cr3, + IN UINTN Cr4, + IN UINTN LinearAddressStart, + IN UINTN LinearAddressEnd + ); + #endif diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c index 71d2d2f5d4..4d8c9b0a89 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c @@ -415,6 +415,8 @@ DumpStackContents ( UINTN Cr0; UINTN Cr3; UINTN Cr4; + UINTN RspAddressStart; + UINTN RspAddressEnd; // // Get current stack pointer @@ -436,21 +438,29 @@ DumpStackContents ( Cr3 = SystemContext.SystemContextX64->Cr3; Cr4 = SystemContext.SystemContextX64->Cr4; + // + // Calculate address range of the stack pointers + // + RspAddressStart = (UINTN)CurrentRsp; + RspAddressEnd = + RspAddressStart + (UINTN)UnwoundStacksCount * CPU_STACK_ALIGNMENT; + + // + // Validate address range of stack pointers + // + if (!IsLinearAddressRangeValid (Cr0, Cr3, Cr4, RspAddressStart, + RspAddressEnd)) { + InternalPrintMessage ("%a: attempted to dereference an invalid stack " + "pointer at 0x%016lx - 0x%016lx\n", __FUNCTION__, + RspAddressStart, RspAddressEnd); + return; + } + // // Dump out stack contents // InternalPrintMessage ("\nStack dump:\n"); while (UnwoundStacksCount-- > 0) { - // - // Check for a valid stack pointer address - // - if (!IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)CurrentRsp) || - !IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)CurrentRsp + 8)) { - InternalPrintMessage ("%a: attempted to dereference an invalid stack " - "pointer at 0x%016lx\n", __FUNCTION__, CurrentRsp); - break; - } - InternalPrintMessage ( "0x%016lx: %016lx %016lx\n", CurrentRsp, @@ -459,7 +469,7 @@ DumpStackContents ( ); // - // Point to next stack + // Point to next stack pointer // CurrentRsp += CPU_STACK_ALIGNMENT; } @@ -571,8 +581,8 @@ DumpImageModuleNames ( // // Check for a valid frame pointer // - if (!IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp + 8) || - !IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp)) { + if (!IsLinearAddressRangeValid (Cr0, Cr3, Cr4, (UINTN)Rbp, + (UINTN)Rbp + 8)) { InternalPrintMessage ("%a: attempted to dereference an invalid frame " "pointer at 0x%016lx\n", __FUNCTION__, Rbp); break; @@ -722,8 +732,8 @@ DumpStacktrace ( // // Check for valid frame pointer // - if (!IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp + 8) || - !IsLinearAddressValid (Cr0, Cr3, Cr4, (UINTN)Rbp)) { + if (!IsLinearAddressRangeValid (Cr0, Cr3, Cr4, (UINTN)Rbp, + (UINTN)Rbp + 8)) { InternalPrintMessage ("%a: attempted to dereference an invalid frame " "pointer at 0x%016lx\n", __FUNCTION__, Rbp); break; -- 2.14.3