From patchwork Mon Nov 28 09:49:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13057208 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 B256CC43217 for ; Mon, 28 Nov 2022 09:50:51 +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: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:In-Reply-To:References: List-Owner; bh=aenfZu/lOOlvu/K8r4qOppXtgx1m5ZZHyhkBf4hXiEU=; b=uFS2wOBrvLVItC caSA+uEeMLBzorZNM/0kAoVBEHe5bKPluU5WRddKQIJiQNln9B1SDZ5NQyYQAFPLRiEAsJMfQhP0m 5hfdkWzOAnf4YnkAmVGImtJCEMgVqJ4g3b0qe5YVXbTresW/YcPh1KIzEcFRA7wejB4N2FBF4cXtS nyr0iHc++UEw2YucPNxbLmp1pfj6HQKIf/+7CS09TM9pySdq8DDu/fatQh6ypO35Qlsel36S8vokn MDf958Est4ACxacmzOq7e2/UgSE2jf4hqa6arPJ+bszGEUtEKiPTaHYa7BebnblYUPfpkFMNURSA6 LK8d8Mv46jzL71f/zp6Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ozalv-000oGv-So; Mon, 28 Nov 2022 09:49:55 +0000 Received: from ams.source.kernel.org ([145.40.68.75]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ozalr-000oEg-Mk for linux-arm-kernel@lists.infradead.org; Mon, 28 Nov 2022 09:49:53 +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 56DC8B80C90; Mon, 28 Nov 2022 09:49:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E09AC433C1; Mon, 28 Nov 2022 09:49:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669628988; bh=IYgx8aYCHMJXqPUUbXfcldTTpG8Zc+8llafQDmuZ0BM=; h=From:To:Cc:Subject:Date:From; b=Rl/d3h7LhtZqMCuxP0LaAfra5dcIj0cr7GgBxOXw+f65+ZCTI7Zq1nGb+5ugYjobM 5h6NWdnaKarC0w1xLpPyNWmahaURCwit2kV0qfugS33dM7gx38QVwa+nO6jQMD7kli WYOxRdm0nsERe77S1n9ryRWhLAH6Q+x7FHU8VFf888BQzzDUrJgFS+oHNS8LqSk2Oj OzZvtbwNr2aS9fWcXMkWP1bfAIc/jKmr0UMvyR2U1U/bkqogNCXILYTYRzQWdgjdxn XmEO6PyAexs9Pb5UU1FEuQmA9+tN21drsxDW8xJhNnw678LtWsOztYG75TIfNCdu0a ea+tu09eDXtZw== From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com, Ard Biesheuvel , Sami Tolvanen , Kees Cook Subject: [PATCH] arm64: efi: Make runtime service wrapper more robust Date: Mon, 28 Nov 2022 10:49:39 +0100 Message-Id: <20221128094939.801232-1-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2846; i=ardb@kernel.org; h=from:subject; bh=IYgx8aYCHMJXqPUUbXfcldTTpG8Zc+8llafQDmuZ0BM=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjhIQy5KUt+463PJ61h9EDy8Z6Tn0PzpB7nPv4STEH vzqoQLeJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY4SEMgAKCRDDTyI5ktmPJE81C/ 9JFizrIyQvMOlqntS5kSRv6ubuJ0JGeaHelmY6ZT72o9qHVucvCkKip9I7lNtW41QbjfJihQfYIlmp x6bkrtyq4cBd8kROczFj9a9TqCfc1WyqaO5Qi+pyHoBwcWSL+RVjyHULc63fRLMaVM8ov/jAF00fXz Bz0YPwQhfkSEIxzo3jFET0hLtK4On7b0r+Su0fjtRkC9mVnXWYh+rB+vGxcVUoIXKFVxaPnq1ep1OB zY/rql6ch1AtI4VViTSIyyqPnr012eHL+sqqxh921CZQhRYAEQ1pwMWD6Uha3aVIKqTcKq4ojg/Sv1 MLD/C39XxMUkLdc9DoXhtX9P1pYrfLml0fDF5qYOfrPbz5se7t3Dk95zPqi468//eBWcBnc6k28Y8N YZHcbd3QkqhZn7wC8hwdngqgVSUvLqS8Qm5MtS3QK/XGf/QlKzC5gp236V1jX2yzAlqjq7lNLX6MJT KqH4EMxnDkxOXJI4H+6TwxW8Fx0PhuKwrdLRrwnPfSlak= 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-20221128_014952_075336_ADD99A1B X-CRM114-Status: GOOD ( 12.04 ) 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 Prevent abuse of the runtime service wrapper code by avoiding restoring the shadow call stack pointer from the ordinary stack, or the stack pointer itself from a GPR. Also, given that the exception recovery routine is never called in an ordinary way, it doesn't need BTI landing pads so it can be SYM_CODE rather than SYM_FUNC. Cc: Sami Tolvanen Cc: Kees Cook Signed-off-by: Ard Biesheuvel --- arch/arm64/kernel/efi-rt-wrapper.S | 16 +++++++++------- arch/arm64/kernel/efi.c | 6 +++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kernel/efi-rt-wrapper.S b/arch/arm64/kernel/efi-rt-wrapper.S index 67babd5f04c27c7a..afd3e81e1b627b87 100644 --- a/arch/arm64/kernel/efi-rt-wrapper.S +++ b/arch/arm64/kernel/efi-rt-wrapper.S @@ -28,7 +28,7 @@ SYM_FUNC_START(__efi_rt_asm_wrapper) stp x27, x28, [sp, #96] adr_this_cpu x8, __efi_rt_asm_recover_sp, x9 - str x29, [x8] + stp x29, x18, [x8] /* * We are lucky enough that no EFI runtime services take more than @@ -56,15 +56,17 @@ SYM_FUNC_START(__efi_rt_asm_wrapper) * called with preemption disabled and a separate shadow stack is used * for interrupts. */ - mov x18, x2 +#ifdef CONFIG_SHADOW_CALL_STACK + ldr_this_cpu x18, __efi_rt_asm_recover_sp + 8, x9 +#endif + b efi_handle_corrupted_x18 // tail call SYM_FUNC_END(__efi_rt_asm_wrapper) -SYM_FUNC_START(__efi_rt_asm_recover) - ldr_this_cpu x8, __efi_rt_asm_recover_sp, x9 - mov sp, x8 +SYM_CODE_START(__efi_rt_asm_recover) + mov sp, x30 - ldp x0, x18, [sp, #16] + ldr x0, [sp, #16] ldp x19, x20, [sp, #32] ldp x21, x22, [sp, #48] ldp x23, x24, [sp, #64] @@ -73,4 +75,4 @@ SYM_FUNC_START(__efi_rt_asm_recover) ldp x29, x30, [sp], #112 b efi_handle_runtime_exception -SYM_FUNC_END(__efi_rt_asm_recover) +SYM_CODE_END(__efi_rt_asm_recover) diff --git a/arch/arm64/kernel/efi.c b/arch/arm64/kernel/efi.c index 8d36e66a6e64cdaa..db7bdce1c7da578b 100644 --- a/arch/arm64/kernel/efi.c +++ b/arch/arm64/kernel/efi.c @@ -130,7 +130,7 @@ asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f) return s; } -asmlinkage DEFINE_PER_CPU(u64, __efi_rt_asm_recover_sp); +asmlinkage DEFINE_PER_CPU(u64[2], __efi_rt_asm_recover_sp); asmlinkage efi_status_t __efi_rt_asm_recover(void); @@ -151,6 +151,10 @@ bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg) add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); dump_stack(); + regs->regs[30] = __this_cpu_read(__efi_rt_asm_recover_sp[0]); +#ifdef CONFIG_SHADOW_CALL_STACK + regs->regs[18] = __this_cpu_read(__efi_rt_asm_recover_sp[1]); +#endif regs->pc = (u64)__efi_rt_asm_recover; return true; }