From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E0EAC83004 for ; Wed, 29 Apr 2020 16:02:34 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1E99F206D9 for ; Wed, 29 Apr 2020 16:02:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="ZD582pfK" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1E99F206D9 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender:Content-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date:Message-ID:From: References:To:Subject:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=miFaptp4FXQw4R5AIKvhZpFcoArgGJdv+1dJtMfmitE=; b=ZD582pfKEIvmPIghHzIXVKVBR +IJGBa4FLfg3IeEZ/T6waHGBUk6+MJPGsRH3rzxZyZPJWTkd/8wAaJuFuuVLpHSq65dpVPRko1r7f lh4nVaKPrmyR6ko2Sut03N1NITuoQLeZNorIemhvRlb7k2bLmMrozACPyIMHhA541xdmZmgpZ2j+e 9f3dHDEn4r3YhnMy8sLByOwjImePyog2zxhWalzuDjw9zuXoLvSguCk956eiHhC8QLIDWwxsdy6kG zwU5h4+hyiZqGeaYP6cc7YdvpH3uTgBpuZNfom5PvgYadEhI4hGXknW0/Q+gG37IU6jetQ9dkjY1j CRBvObUXA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jTpAL-0002ma-D3; Wed, 29 Apr 2020 16:02:29 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jTpAH-0002mB-A1 for linux-arm-kernel@lists.infradead.org; Wed, 29 Apr 2020 16:02:27 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 73C6E1045; Wed, 29 Apr 2020 09:02:24 -0700 (PDT) Received: from [10.57.22.235] (unknown [10.57.22.235]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 777933F73D; Wed, 29 Apr 2020 09:02:21 -0700 (PDT) Subject: Re: [PATCH v2] arm64: Optimize ptrauth by enabling it for non-leaf functions To: Mark Rutland References: <1588149371-20310-1-git-send-email-amit.kachhap@arm.com> <20200429101839.GB28631@C02TD0UTHF1T.local> From: Amit Kachhap Message-ID: <996f1dfb-7df8-df15-7957-3e9d7994348e@arm.com> Date: Wed, 29 Apr 2020 21:31:59 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20200429101839.GB28631@C02TD0UTHF1T.local> Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200429_090225_437394_7CAFC4FE X-CRM114-Status: GOOD ( 28.45 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Catalin Marinas , Mark Brown , James Morse , Vincenzo Frascino , Will Deacon , linux-arm-kernel@lists.infradead.org, Daniel Kiss Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Mark, On 4/29/20 3:48 PM, Mark Rutland wrote: > Hi Amit, > > On Wed, Apr 29, 2020 at 02:06:10PM +0530, Amit Daniel Kachhap wrote: >> Compilers are optimized to not create the frame record for the leaf >> function and hence lr is not signed and stored in the stack. Thus the leaf >> functions cannot be used for ROP gadget attack. > > IIUC Will's point on the last posting was that leaf functions can be > used as a restricted ROP gadget, where the LR isn't controlled via the > stack. > > e.g. you might have a gadget that does something like: > > : > LDP x0, x1, [SP], #16 > STR x0, [x1] > RET // LR == > > ... and if the LR had previously been set up to point to gadget, it > would return recursively, performing a sequence of arbitrary stores. > With an AUT, it would fail after the first store. > > That does rely on already subverting control flow (probably via a > forward-edge BR where we don't have BTI), and so maybe we've already > lost in practical terms, but there is at least some possibility of a > gadget that AUT would catch here. There's some nuance to capture in the > commit message for that. ok sure. It makes sense to write some details for this scenario. Thanks for the details. > >> This patch selects pointer authentication only for non-leaf function >> and the compiler option is modified to -mbranch-protection=pac-ret and >> -msign-return-address=non-leaf. >> >> As there are no PAC instructions(PACIASP and AUTIASP) inserted in the leaf >> functions so the kernel code size reduces by ~0.01%. > > Do we expect this to matter? The size difference isn't that large, so is > there a performance issue? > > Are there any leaf functions which we consider critical to performance? > > I know that one concern is that PACIASP acts as an implicit landing pad, > and so its use everywhere potentially weakens BTI. Do we have any data > to indicate that would be a concern here? e.g. with and without this, > how many instances of PACIASP and BTI *C exist? I don't have any dynamic performance numbers now. I have some static numbers from 5.7-rc1 kernel with a simple grep. Total functions(leaf+non leaf) = 55682(PACIASP instructions). Total non-leaf functions = 47425. so leaf functions = 8257(14.8% of total). Leaf functions used for indirect calls = 1345 (extra "bti c" instructions if -mbranch-protection=standard used). Leaf functions used for direct calls = 6192. So statically there should be performance improvement with this patch. Reduction of extra instructions with ptrauth = 14.8% Reduction of extra instructions with both ptrauth + bti = 12.32% Cheers, Amit > > Thanks, > Mark. > >> Note, As PACIASP instruction is also used for Armv8.5 BTI branching so the >> compiler may insert BTI instructions in case of leaf functions which are >> candidate of JOP gadget for the upcoming BTI in-kernel support. >> >> Reported-by: Daniel Kiss >> Signed-off-by: Amit Daniel Kachhap >> --- >> Changes since v1: >> * Updated the commit logs as per the comments from Will and Mark[1]. >> >> [1]: https://www.spinics.net/lists/arm-kernel/msg798518.html >> >> >> arch/arm64/Kconfig | 4 ++-- >> arch/arm64/Makefile | 4 ++-- >> 2 files changed, 4 insertions(+), 4 deletions(-) >> >> diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig >> index 40fb05d..29cfe05 100644 >> --- a/arch/arm64/Kconfig >> +++ b/arch/arm64/Kconfig >> @@ -1541,11 +1541,11 @@ config ARM64_PTR_AUTH >> >> config CC_HAS_BRANCH_PROT_PAC_RET >> # GCC 9 or later, clang 8 or later >> - def_bool $(cc-option,-mbranch-protection=pac-ret+leaf) >> + def_bool $(cc-option,-mbranch-protection=pac-ret) >> >> config CC_HAS_SIGN_RETURN_ADDRESS >> # GCC 7, 8 >> - def_bool $(cc-option,-msign-return-address=all) >> + def_bool $(cc-option,-msign-return-address=non-leaf) >> >> config AS_HAS_PAC >> def_bool $(as-option,-Wa$(comma)-march=armv8.3-a) >> diff --git a/arch/arm64/Makefile b/arch/arm64/Makefile >> index 85e4149..895f506 100644 >> --- a/arch/arm64/Makefile >> +++ b/arch/arm64/Makefile >> @@ -70,8 +70,8 @@ endif >> branch-prot-flags-y += $(call cc-option,-mbranch-protection=none) >> >> ifeq ($(CONFIG_ARM64_PTR_AUTH),y) >> -branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=all >> -branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pac-ret+leaf >> +branch-prot-flags-$(CONFIG_CC_HAS_SIGN_RETURN_ADDRESS) := -msign-return-address=non-leaf >> +branch-prot-flags-$(CONFIG_CC_HAS_BRANCH_PROT_PAC_RET) := -mbranch-protection=pac-ret >> # -march=armv8.3-a enables the non-nops instructions for PAC, to avoid the >> # compiler to generate them and consequently to break the single image contract >> # we pass it only to the assembler. This option is utilized only in case of non >> -- >> 2.7.4 >> _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel