From patchwork Fri Dec 9 15:20:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13069739 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id E24FEC4332F for ; Fri, 9 Dec 2022 15:22:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=VULxvmgKwE6WMd8cu5XYAnmw9jm/ai6jiS3+luA8Hvc=; b=nCdauOFV5vmSfu ilLFyfM4GphS+aZ4zWCmXhvs+RbdjetXLCcKJVuZhYjYdanKdcBbc6PLKVWeokm4mX6N9i1xUvBsm lM9vSnR+hqUUF+Jimdzoc1Es4tH3thh/GqO9teFwCMBpJhZkS3ZzJsf4btlBCG8v992rnW9PIst2E nwKvsz4xPbUZjXNMYvBJ9ZPorWNV2d8fBazY0VmQ1EBrwxWWvHcPwqVXvAZBalfVa5obAXeLsoXR7 mziklY/zcaxZtjLuxgshYEz3nmapnPM+uAlYMiTpBcbX5m969ccNpEENHVqmH4VJXyVsD3VVKz/nD cszhk4xd3mEx/SjW9sDw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1p3fCE-008wRx-3M; Fri, 09 Dec 2022 15:21:54 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1p3fBW-008wEd-39 for linux-arm-kernel@lists.infradead.org; Fri, 09 Dec 2022 15:21:11 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id C9F95B82887; Fri, 9 Dec 2022 15:21:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C619FC43396; Fri, 9 Dec 2022 15:21:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1670599267; bh=MqT2F3MLZTaJHBNFjcvmSzJ/Q5zR07h4UcC/7suziHk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bZmY44djnv+67qd7HuP08y7uJgcIZRXUAXskzSF/lxll8y92BVk0XZ6kPkcPZ7Wvm XDjl4Pdc9UcfWUN7ee7oBXudBtTgYVmNDNBflyCQq0O2cCNrDPI8bFMNbzoCKYswnJ CNYDNN+Sl8vZZAfQ1djoyF3Oy82EbwN/wuZvfn/H1czLfR2B7tlHVMQ2h2Tw2fxxh/ PqSpPb6eBGvzAZ/+k0ytn1FFFc873IFTq2p80ntbi9zcyTbTJf4XuPERIWDbbmySJw DdveUckNzLHfLUdabYrptD85ZQiTDHb7LTUFeea8aJKhG+6gtkh6HLRA0Eo4dKcd4S eEzbW0of4cvaA== From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Marc Zyngier , Will Deacon , Mark Rutland , Kees Cook , Catalin Marinas , Mark Brown Subject: [PATCH v3 4/4] arm64: ftrace: Add return address protection Date: Fri, 9 Dec 2022 16:20:48 +0100 Message-Id: <20221209152048.3517080-5-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221209152048.3517080-1-ardb@kernel.org> References: <20221209152048.3517080-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2660; i=ardb@kernel.org; h=from:subject; bh=MqT2F3MLZTaJHBNFjcvmSzJ/Q5zR07h4UcC/7suziHk=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjk1JPubSdmkIXyFkN5IX9YAlnXJvDRKqy4Q41y0k+ C/K8E4CJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY5NSTwAKCRDDTyI5ktmPJN5FC/ 0ecKxBXTUetdMfWxGaGjfSyNRG4ERcKriULQRMe2mNskxKenepj1erbLGHRZvkzil84aveI8K4eoH8 2lIxEVZXtpzINRyql4ZCB0f0IuSglC7fwqLulml6/Lkeo9YShh1RazEkwPIRiA0/aHP/OudNJNSFo3 U+wW9nmViArbMdn80lVL23/zXVS+7n6fGgmldIz0MvyqwIBBtlV80GNfg6rLyil76T533u6coHApW5 yN+3Ebj+oiZXbdb4tcpv4BdKTJP9ANjWPVFm1Lmg1XfE9vpbduQC6FquSnf5ANeiu6hTF98vHpOAhh 40bAdZzOz/K2uWyq44dAil2DK+NrYg7s1WGcRkRCD+Q1SayDyZhJAgi+Hh3tJ54Yu0Nimcnf7QCbvR 8rEHSwXd4dQaIfdYbvTFoVFbki1Y+d0BQQ4WZGa3+e8lmFkzvQeWfpk8yRMxsCHvjY/eROs4/UvT5A oy0Wcn4NigpAGXJmh3FgBpwIqwPa4uvZ6rzOQKRZ0+zFM= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221209_072110_449955_5C9474B7 X-CRM114-Status: GOOD ( 16.41 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Use the newly added asm macros to protect and restore the return address in the ftrace call wrappers, based on whichever method is active (PAC and/or shadow call stack). If the graph tracer is in use, this covers both the return address *to* the ftrace call site as well as the return address *at* the call site, and the latter will either be restored in return_to_handler(), or before returning to the call site. Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/entry-ftrace.S | 17 ++++++++++++++++- arch/arm64/kernel/stacktrace.c | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kernel/entry-ftrace.S b/arch/arm64/kernel/entry-ftrace.S index bccd525241ab615d..4acfe12ac594da58 100644 --- a/arch/arm64/kernel/entry-ftrace.S +++ b/arch/arm64/kernel/entry-ftrace.S @@ -33,9 +33,13 @@ * record, its caller is missing from the LR and existing chain of frame * records. */ + SYM_CODE_START(ftrace_caller) bti c + protect_return_address x9 + protect_return_address x30 + /* Save original SP */ mov x10, sp @@ -65,6 +69,9 @@ SYM_CODE_START(ftrace_caller) stp x29, x30, [sp, #FREGS_SIZE] add x29, sp, #FREGS_SIZE + alternative_insn nop, "xpaci x30", ARM64_HAS_ADDRESS_AUTH, \ + IS_ENABLED(CONFIG_ARM64_PTR_AUTH_KERNEL) + sub x0, x30, #AARCH64_INSN_SIZE // ip (callsite's BL insn) mov x1, x9 // parent_ip (callsite's LR) ldr_l x2, function_trace_op // op @@ -93,7 +100,14 @@ SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL) /* Restore the callsite's SP */ add sp, sp, #FREGS_SIZE + 32 - ret x10 + restore_return_address x10 +#ifdef CONFIG_FUNCTION_GRAPH_TRACER + // Check whether the callsite's LR has been overridden + cmp x9, x30 + b.ne 0f +#endif + restore_return_address x30 +0: ret x10 SYM_CODE_END(ftrace_caller) #else /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */ @@ -265,6 +279,7 @@ SYM_CODE_START(return_to_handler) ldp x6, x7, [sp, #48] add sp, sp, #64 + restore_return_address x30 ret SYM_CODE_END(return_to_handler) #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 634279b3b03d1b07..e323a8ac50168261 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -102,6 +102,7 @@ static int notrace unwind_next(struct unwind_state *state) */ orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc, (void *)state->fp); + orig_pc = ptrauth_strip_insn_pac(orig_pc); if (WARN_ON_ONCE(state->pc == orig_pc)) return -EINVAL; state->pc = orig_pc;