From patchwork Fri Oct 5 08:47:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Kristina_Mart=C5=A1enko?= X-Patchwork-Id: 10627757 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CF4F81515 for ; Fri, 5 Oct 2018 09:49:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CE4DB29186 for ; Fri, 5 Oct 2018 09:49:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C25F929190; Fri, 5 Oct 2018 09:49:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4D8FE29186 for ; Fri, 5 Oct 2018 09:49:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Gs6O1CXJm0uIMlVPPEAD7VwFGPsirfrVUefsyepi34U=; b=LljvOEtR+Hmrcg1gHD4dps1S3r CjRSSkHXTc0qNAa+gHEhIdT5G6YgnQRcpMsYRiOuDMG3ATdxv13aMY/FdxElMA4QhVInE41rsd+SU rloNZxApOr4xLmR4JZNbYe3dFoAt3RRHFQvhgemASd3ix3C8Fa7sXOe+0oNxUKZlBjeIY+ALMYn5a yVAPIBED/vtRYoikeFI5wJvnYEj+l839QFzdrF9O4eYCSQFhDtA7o/8TMEPDqfsu8Bfbni2BRYdrT xxCoveXcsI/LPrv839Xp5AhZ5crlox4i1m1P9mTCPPSRHe26/pDTmYcpsg+GG/SB/CqHFNaheDPTd t+KeYdGw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1g8Mjj-0003HH-RZ; Fri, 05 Oct 2018 09:49:31 +0000 Received: from merlin.infradead.org ([2001:8b0:10b:1231::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1g8MhI-0002Cw-Ox for linux-arm-kernel@bombadil.infradead.org; Fri, 05 Oct 2018 09:47:00 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=4xCflmaAlUPnxxWkkcErOh7EaiZVAXVSRTTeGTPyqyU=; b=AHn16I9lwXqFg/DEHtFKuSrDS 98PkKWWmb4CxYaAYvN6Zb3YJxsrv6Z+uKEkajoANB3mRe9fbDw3TITrVgzDIeiElaTm9s7F1ZNxJq gmBbFLYklpwbR3ffqfnAPi8+1S6jseu34h8de1v/mVSCPGmaavg5Jz+u+P5JeVY6+ONFcCHHWtXsC +kxeM7x12qpwikScHtjF/kn9MAMzeR4BgGji2QvtcBFBJjnsOn953m0Nj8sDeBIiM2NUBKdj/Lf+1 LIOCemAbWgP00wD3bNnxf364dP7yCv7c+QOWO+YJ4hxk79H9rHJ9P+d+2RwsdHcQcNw6U5fCPN4ip ebnwmd2xw==; Received: from foss.arm.com ([217.140.101.70]) by merlin.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1g8Lp1-00029y-5f for linux-arm-kernel@lists.infradead.org; Fri, 05 Oct 2018 08:50:56 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 881E21650; Fri, 5 Oct 2018 01:50:46 -0700 (PDT) Received: from moonbear.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 52E713F5B3; Fri, 5 Oct 2018 01:50:43 -0700 (PDT) From: Kristina Martsenko To: linux-arm-kernel@lists.infradead.org Subject: [RFC 14/17] arm64: unwind: strip PAC from kernel addresses Date: Fri, 5 Oct 2018 09:47:51 +0100 Message-Id: <20181005084754.20950-15-kristina.martsenko@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181005084754.20950-1-kristina.martsenko@arm.com> References: <20181005084754.20950-1-kristina.martsenko@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181005_045055_390772_DD82109C X-CRM114-Status: GOOD ( 17.83 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , linux-arch@vger.kernel.org, Andrew Jones , Jacob Bramley , Arnd Bergmann , Ard Biesheuvel , Marc Zyngier , Catalin Marinas , Adam Wallis , "Suzuki K . Poulose" , Will Deacon , Christoffer Dall , kvmarm@lists.cs.columbia.edu, Ramana Radhakrishnan , Amit Kachhap , Dave P Martin , linux-kernel@vger.kernel.org, Kees Cook MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Mark Rutland When we enable pointer authentication in the kernel, LR values saved to the stack will have a PAC which we must strip in order to retrieve the real return address. Strip PACs when unwinding the stack in order to account for this. Signed-off-by: Mark Rutland Signed-off-by: Kristina Martsenko --- arch/arm64/include/asm/pointer_auth.h | 10 +++++++--- arch/arm64/kernel/ptrace.c | 2 +- arch/arm64/kernel/stacktrace.c | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/pointer_auth.h b/arch/arm64/include/asm/pointer_auth.h index 5e40533f4ea2..e60f225d9fa2 100644 --- a/arch/arm64/include/asm/pointer_auth.h +++ b/arch/arm64/include/asm/pointer_auth.h @@ -55,12 +55,16 @@ static inline void ptrauth_keys_switch(struct ptrauth_keys *keys) * The EL0 pointer bits used by a pointer authentication code. * This is dependent on TBI0 being enabled, or bits 63:56 would also apply. */ -#define ptrauth_pac_mask() GENMASK(54, VA_BITS) +#define ptrauth_pac_mask_ttbr0() GENMASK(54, VA_BITS) + +#define ptrauth_pac_mask_ttbr1() (GENMASK(63, 56) | GENMASK(54, VA_BITS)) -/* Only valid for EL0 TTBR0 instruction pointers */ static inline unsigned long ptrauth_strip_insn_pac(unsigned long ptr) { - return ptr & ~ptrauth_pac_mask(); + if (ptr & BIT_ULL(55)) + return ptr | ptrauth_pac_mask_ttbr1(); + else + return ptr & ~ptrauth_pac_mask_ttbr0(); } #define ptrauth_task_init_user(tsk) \ diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c index cb8246f8c603..bf4d6d384e4f 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -970,7 +970,7 @@ static int pac_mask_get(struct task_struct *target, * depending on TCR_EL1.TBID*, which we may make use of in future, so * we expose separate masks. */ - unsigned long mask = ptrauth_pac_mask(); + unsigned long mask = ptrauth_pac_mask_ttbr0(); struct user_pac_mask uregs = { .data_mask = mask, .insn_mask = mask, diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 4989f7ea1e59..44f6a64a8006 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -56,6 +57,8 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp)); frame->pc = READ_ONCE_NOCHECK(*(unsigned long *)(fp + 8)); + frame->pc = ptrauth_strip_insn_pac(frame->pc); + #ifdef CONFIG_FUNCTION_GRAPH_TRACER if (tsk->ret_stack && (frame->pc == (unsigned long)return_to_handler)) {