From patchwork Tue Aug 5 09:49:13 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Olivier Martin X-Patchwork-Id: 34903 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-pd0-f199.google.com (mail-pd0-f199.google.com [209.85.192.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 7DADE20523 for ; Tue, 5 Aug 2014 09:49:44 +0000 (UTC) Received: by mail-pd0-f199.google.com with SMTP id ft15sf4293333pdb.10 for ; Tue, 05 Aug 2014 02:49:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:thread-topic :thread-index:message-id:references:in-reply-to:accept-language :acceptlanguage:mime-version:cc:subject:precedence:reply-to:list-id :list-unsubscribe:list-archive:list-post:list-help:list-subscribe :errors-to:x-original-sender:x-original-authentication-results :mailing-list:content-language:content-type; bh=qdBdSaWaqzKi7hBezcGd8UHqmApBMPCmPGUlFzkUduc=; b=h0GQb/8iVBWZjLAuR5V9un9V3FcS1bUIrFqjwqPHCszThMLs6NgFvEJKv2vDgHRaot UpDgIp+XPvfNHmEPOpeixwMOD5oa1XLyY/pjVXq67sh7CubGaFf2KDMccJwpaa+//zvd bqEMpGfcGS5g9j7xvTzF5vWI0eSwWgV0NaYSMMb2tXuUlivZ79VvIi/cQguv0+jBddST KNP0y63jNARN4UGTPyLLdH9Pu3uaG7O8OzIdmAJe6rtggUaXBZJ7EB3gR093qBt/HAuW x7Dz+OYECIstUGBLzc0ipNw2mXlf5C/STbn+k8/6veuKOpWFERLaDqt8LouVxx//jAe2 wb9g== X-Gm-Message-State: ALoCoQkhUGVpWC29Ea0tZcgpjRLUYVuIzXDZTZ2Lg4P7IUoFVNMiD0SaC38IobvTKz428Z4ftIP0 X-Received: by 10.66.168.197 with SMTP id zy5mr1397028pab.7.1407232183801; Tue, 05 Aug 2014 02:49:43 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.101.231 with SMTP id u94ls209673qge.62.gmail; Tue, 05 Aug 2014 02:49:43 -0700 (PDT) X-Received: by 10.52.64.140 with SMTP id o12mr474021vds.70.1407232183588; Tue, 05 Aug 2014 02:49:43 -0700 (PDT) Received: from mail-vc0-f182.google.com (mail-vc0-f182.google.com [209.85.220.182]) by mx.google.com with ESMTPS id h6si704563vdw.73.2014.08.05.02.49.43 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 05 Aug 2014 02:49:43 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.182 as permitted sender) client-ip=209.85.220.182; Received: by mail-vc0-f182.google.com with SMTP id hy4so1026405vcb.13 for ; Tue, 05 Aug 2014 02:49:43 -0700 (PDT) X-Received: by 10.52.234.228 with SMTP id uh4mr1833499vdc.34.1407232183465; Tue, 05 Aug 2014 02:49:43 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp370896vcb; Tue, 5 Aug 2014 02:49:42 -0700 (PDT) X-Received: by 10.42.16.2 with SMTP id n2mr4013495ica.93.1407232182397; Tue, 05 Aug 2014 02:49:42 -0700 (PDT) Received: from lists.sourceforge.net (lists.sourceforge.net. [216.34.181.88]) by mx.google.com with ESMTPS id nh10si2824046icc.92.2014.08.05.02.49.41 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 05 Aug 2014 02:49:42 -0700 (PDT) Received-SPF: pass (google.com: domain of edk2-devel-bounces@lists.sourceforge.net designates 216.34.181.88 as permitted sender) client-ip=216.34.181.88; Received: from localhost ([127.0.0.1] helo=sfs-ml-3.v29.ch3.sourceforge.com) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1XEbN5-0003nE-0a; Tue, 05 Aug 2014 09:49:31 +0000 Received: from sog-mx-4.v43.ch3.sourceforge.com ([172.29.43.194] helo=mx.sourceforge.net) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1XEbN3-0003n2-ES for edk2-devel@lists.sourceforge.net; Tue, 05 Aug 2014 09:49:29 +0000 Received-SPF: pass (sog-mx-4.v43.ch3.sourceforge.com: domain of arm.com designates 195.130.217.12 as permitted sender) client-ip=195.130.217.12; envelope-from=olivier.martin@arm.com; helo=service88.mimecast.com; Received: from service88.mimecast.com ([195.130.217.12]) by sog-mx-4.v43.ch3.sourceforge.com with esmtp (Exim 4.76) id 1XEbMx-0005Pz-Db for edk2-devel@lists.sourceforge.net; Tue, 05 Aug 2014 09:49:29 +0000 Received: from emea-cam-gw2.Emea.Arm.com (fw-tnat.cambridge.arm.com [217.140.96.21]) (Using TLS) by service88.mimecast.com; Tue, 05 Aug 2014 10:49:17 +0100 Received: from george.Emea.Arm.com ([fe80::4c19:a8f:5c9a:76df]) by emea-cam-gw2.Emea.Arm.com ([::1]) with mapi; Tue, 5 Aug 2014 10:49:14 +0100 From: Olivier Martin To: "Gao, Liming" , 'Andrew Fish' Date: Tue, 5 Aug 2014 10:49:13 +0100 Thread-Topic: [edk2-buildtools] [PATCH 2/3] MdePkg: Introduced BaseStackCheckLib Thread-Index: AQHPmfqXOYjyhNDU2UGe1rBoG/OXF5uVVTfg//+EogCAAMyEgIABY5+ggCm4DtT//3uUgIABM6AAgAB/tKA= Message-ID: <0877601216922E4B83A7129715B5DA2BAC50CFA7A6@GEORGE.Emea.Arm.com> References: <1404747833-653-1-git-send-email-olivier.martin@arm.com> <1404747833-653-3-git-send-email-olivier.martin@arm.com> <4A89E2EF3DFEDB4C8BFDE51014F606A1118B41D7@shsmsx102.ccr.corp.intel.com> <74DC89F9-E911-430A-B3D0-4238BAFB4425@apple.com> <"004d01cf9ab0$382145e0$a863d1a0$@martin"@arm.com> <4A89E2EF3DFEDB4C8BFDE51014F606A1118B4AD0@shsmsx102.ccr.corp.intel.com> <"006f01cfaff9$15b7d380$41277a80$@martin"@arm.com> <007401cfaffb$d879d970$896d8c50$@martin@arm.com> <4A89E2EF3DFEDB4C8BFDE51014F606A1118DD113@shsmsx102.ccr.corp.intel.com> In-Reply-To: <4A89E2EF3DFEDB4C8BFDE51014F606A1118DD113@shsmsx102.ccr.corp.intel.com> Accept-Language: en-US, en-GB X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US, en-GB MIME-Version: 1.0 X-MC-Unique: 114080510491703902 X-Spam-Score: -0.5 (/) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for sender-domain -0.0 SPF_PASS SPF: sender matches SPF record 1.0 HTML_MESSAGE BODY: HTML included in message X-Headers-End: 1XEbMx-0005Pz-Db Cc: "edk2-devel@lists.sourceforge.net" , "edk2-buildtools-devel@lists.sourceforge.net" Subject: Re: [edk2] [edk2-buildtools] [PATCH 2/3] MdePkg: Introduced BaseStackCheckLib X-BeenThere: edk2-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list Reply-To: edk2-devel@lists.sourceforge.net List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Errors-To: edk2-devel-bounces@lists.sourceforge.net X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: olivier.martin@arm.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.182 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Content-Language: en-US I have just sent it -see '[PATCH v3 1/3] MdePkg: Introduced BaseStackCheckLib'. Thanks, Olivier From: Gao, Liming [mailto:liming.gao@intel.com] Sent: 05 August 2014 03:13 To: Olivier Martin; 'Andrew Fish' Cc: Kinney, Michael D; edk2-buildtools-devel@lists.sourceforge.net; edk2-devel@lists.sourceforge.net Subject: RE: [edk2-buildtools] [PATCH 2/3] MdePkg: Introduced BaseStackCheckLib Martin: I am fine to use FixedPcd for value 0x0AFF. Have you plan to add this PCD in your patch? Thanks Liming From: Olivier Martin [mailto:olivier.martin@arm.com] Sent: Monday, August 04, 2014 11:51 PM To: 'Andrew Fish' Cc: Gao, Liming; Kinney, Michael D; edk2-buildtools-devel@lists.sourceforge.net; edk2-devel@lists.sourceforge.net Subject: RE: [edk2-buildtools] [PATCH 2/3] MdePkg: Introduced BaseStackCheckLib The benefit of the FixedPcd was to be an easy alternative between the recommended constant value and the random number. A Firmware vendor might want to use a different canary value as the open source project. Olivier From: Andrew Fish [mailto:afish@apple.com] Sent: 04 August 2014 16:44 To: Olivier Martin Cc: Gao, Liming; Mike Kinney; edk2-buildtools-devel@lists.sourceforge.net; edk2-devel@lists.sourceforge.net Subject: Re: [edk2-buildtools] [PATCH 2/3] MdePkg: Introduced BaseStackCheckLib On Aug 4, 2014, at 8:30 AM, Olivier Martin > wrote: Recommended value does not mean unique value. Anyway, no one has commented whether this value should be kept in the code or be replaced by a PCD. So, I will keep the original code. I will send a new patchset in the next couple of minutes to define this class instance as NULL instead of BASE. The recommendation is to use a constant like the one in the code, or a random number per boot. So if there was a PCD it would be to use the constant vs. a random number per boot. I don't think we have a BaseLib compatible way to get a random number? The per boot random number makes it harder for attacking code to figure out where the "canary" is so it can be defeated. It is like ASLR, and a constant PCD value is not going to help with that. Thanks, Andrew Fish Olivier From: Gao, Liming [mailto:liming.gao@intel.com] Sent: 09 July 2014 03:42 To: Olivier Martin; 'Andrew Fish' Cc: Kinney, Michael D; edk2-buildtools-devel@lists.sourceforge.net; edk2-devel@lists.sourceforge.net Subject: RE: [edk2-buildtools] [PATCH 2/3] MdePkg: Introduced BaseStackCheckLib Martin: Per Andrew comment, this value is the recommended constant. If so, we don't need to add one PCD for it unless someone has the strong request to configure it. Thanks Liming From: Olivier Martin [mailto:olivier.martin@arm.com] Sent: Tuesday, July 08, 2014 9:26 PM To: 'Andrew Fish'; Gao, Liming Cc: Kinney, Michael D; edk2-buildtools-devel@lists.sourceforge.net; edk2-devel@lists.sourceforge.net Subject: RE: [edk2-buildtools] [PATCH 2/3] MdePkg: Introduced BaseStackCheckLib Actually, I was thinking to replace this canary value by a FixedPcd but I do not remember why I have not done it. I might have just forgot it. Olivier From: Andrew Fish [mailto:afish@apple.com] Sent: 08 July 2014 02:14 To: Gao, Liming Cc: Olivier Martin; Mike Kinney; edk2-buildtools-devel@lists.sourceforge.net; edk2-devel@lists.sourceforge.net Subject: Re: [edk2-buildtools] [PATCH 2/3] MdePkg: Introduced BaseStackCheckLib On Jul 7, 2014, at 5:40 PM, Gao, Liming > wrote: Martin: What is 0x0AFF for? Is it an address or a value? This value is the recommended constant if you can not generate a real random number for the "canary" value. It has NULL (will terminate strings), LF, and -1. http://wiki.osdev.org/GCC_Stack_Smashing_Protector. So it helps contains read based overruns. __stack_chk_guard is the "canary" placed on the stack by the compiler. __stack_check_fail() is called if the "canary" has been over written. These are both compiler intrinsics. Thanks, Andrew Fish ~/work/Compiler>cat stack.c void test (int i, char v) { char test[0x100]; test[i] = v; return; } ~/work/Compiler>clang -S stack.c ~/work/Compiler>cat stack.S .section __TEXT,__text,regular,pure_instructions .globl _test .align 4, 0x90 _test: ## @test .cfi_startproc ## BB#0: pushq %rbp Ltmp2: .cfi_def_cfa_offset 16 Ltmp3: .cfi_offset %rbp, -16 movq %rsp, %rbp Ltmp4: .cfi_def_cfa_register %rbp subq $272, %rsp ## imm = 0x110 movb %sil, %al movq ___stack_chk_guard@GOTPCREL(%rip), %rcx movq (%rcx), %rcx movq %rcx, -8(%rbp) movq ___stack_chk_guard@GOTPCREL(%rip), %rcx movl %edi, -12(%rbp) movb %al, -13(%rbp) movb -13(%rbp), %al movslq -12(%rbp), %rdx movb %al, -272(%rbp,%rdx) movq (%rcx), %rcx movq -8(%rbp), %rdx cmpq %rdx, %rcx jne LBB0_2 ## BB#1: ## %SP_return addq $272, %rsp ## imm = 0x110 popq %rbp ret LBB0_2: ## %CallStackCheckFailBlk callq ___stack_chk_fail .cfi_endproc .subsections_via_symbols +/// "canary" value that is inserted by the compiler into the stack frame. +VOID *__stack_chk_guard = (VOID*)0x0AFF; And, this library instance is used as NULL class instance. Its library class should be NULL. Thanks Liming -----Original Message----- From: Olivier Martin [mailto:olivier.martin@arm.com] Sent: Monday, July 07, 2014 11:44 PM To: Kinney, Michael D; edk2-buildtools-devel@lists.sourceforge.net Cc: andrew.fish@apple.com; edk2-devel@lists.sourceforge.net Subject: [edk2-buildtools] [PATCH 2/3] MdePkg: Introduced BaseStackCheckLib This library only support GCC and XCode for now. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Andrew Fish > Signed-off-by: Olivier Martin Change-Id: Ib51239b7d315637fd22c34a9a78a7a830f2ffdc7 --- .../Library/BaseStackCheckLib/BaseStackCheckGcc.c | 62 ++++++++++++++++++++++ .../BaseStackCheckLib/BaseStackCheckLib.inf | 41 ++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 MdePkg/Library/BaseStackCheckLib/BaseStackCheckGcc.c create mode 100644 MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf ------------------------------------------------------------------------------ Open source business process management suite built on Java and Eclipse Turn processes into business applications with Bonita BPM Community Edition Quickly connect people, data, and systems into organized workflows Winner of BOSSIE, CODIE, OW2 and Gartner awards http://p.sf.net/sfu/Bonitasoft diff --git a/MdePkg/Library/BaseStackCheckLib/BaseStackCheckGcc.c b/MdePkg/Library/BaseStackCheckLib/BaseStackCheckGcc.c new file mode 100644 index 0000000..9ec76d8 --- /dev/null +++ b/MdePkg/Library/BaseStackCheckLib/BaseStackCheckGcc.c @@ -0,0 +1,62 @@ +/** @file + Base Stack Check library for GCC/clang. + + Use -fstack-protector-all compiler flag to make the compiler insert + the __stack_chk_guard "canary" value into the stack and check the + value prior to exiting the function. If the "canary" is overwritten + __stack_chk_fail() is called. This is GCC specific code. + + Copyright (c) 2012, Apple Inc. All rights reserved.
This program + and the accompanying materials are licensed and made available under + the terms and conditions of the BSD License which accompanies this + distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php. + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include +#include +#include +#include + +VOID +__stack_chk_fail ( + VOID + ); + + +/// "canary" value that is inserted by the compiler into the stack frame. +VOID *__stack_chk_guard = (VOID*)0x0AFF; + +// If ASLR was enabled we could use +//void (*__stack_chk_guard)(void) = __stack_chk_fail; + +/** + Error path for compiler generated stack "canary" value check code. If +the stack canary has been overwritten this function gets called on +exit of the function. +**/ +VOID +__stack_chk_fail ( + VOID + ) +{ + UINT8 DebugPropertyMask; + + DEBUG ((DEBUG_ERROR, "STACK FAULT: Buffer Overflow in function + %a.\n", __builtin_return_address(0))); + + // + // Generate a Breakpoint, DeadLoop, or NOP based on PCD settings even +if + // BaseDebugLibNull is in use. + // + DebugPropertyMask = PcdGet8 (PcdDebugPropertyMask); + if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_BREAKPOINT_ENABLED) != 0) { + CpuBreakpoint (); + } else if ((DebugPropertyMask & DEBUG_PROPERTY_ASSERT_DEADLOOP_ENABLED) != 0) { + CpuDeadLoop (); + } +} + diff --git a/MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf b/MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf new file mode 100644 index 0000000..4e2285d --- /dev/null +++ b/MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf @@ -0,0 +1,41 @@ +## @file +# Stack Check Library +# +# Copyright (c) 2014, ARM Ltd. All rights reserved.
# # This +program and the accompanying materials # are licensed and made +available under the terms and conditions of the BSD License # which +accompanies this distribution. The full text of the license may be +found at # http://opensource.org/licenses/bsd-license.php. +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" +BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = BaseStackCheckLib + FILE_GUID = 5f6579f7-b648-4fdb-9f19-4c17e27e8eff + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = BaseStackCheckLib + + +# +# VALID_ARCHITECTURES = ARM AARCH64 +# + +[Sources] + BaseStackCheckGcc.c | GCC + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib + DebugLib + +[Pcd] + gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask + -- 1.8.5