From patchwork Mon Aug 15 11:02:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Zhongjin X-Patchwork-Id: 12943454 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 4E1CAC00140 for ; Mon, 15 Aug 2022 11:12:43 +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=grglRuDQVn9q3TTQ7C8d8v9gvCjlIo1PPdZtgliKvC4=; b=d6e6LygkpqL8Jq mQNt5Ffq5gQ6V0Y6wSDSu+M5e4aderroq9eSsgJiq35QGbwDidDneNr7YQDttq8gbGk76wNsvB6f3 aneaBH5bMF6pab87Bi2PTPqMpVME4EV/ySOdKCQAqKdvxHAOLgALVg5/BaGyjDpeo49EWqv+x7QoJ XECORFUnAJdqSTs4WcXVvzADpHW941Jlla3d+sdRONQ3VzGxFXQ+nJjD/aflMnOLjDzMF9LZqCuXo fTJl5+DWT/ZLGTbFzRhhn1shflzt1z7PDdcEFI884oLQoLHHWQ0kZSY94mQajDsNIROnbUVa3GsNY M6eoHKqk2hdclT4pzaRg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oNY0O-00FB2R-H7; Mon, 15 Aug 2022 11:11:36 +0000 Received: from szxga02-in.huawei.com ([45.249.212.188]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oNXup-00F7Ib-2Y for linux-arm-kernel@lists.infradead.org; Mon, 15 Aug 2022 11:05:55 +0000 Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4M5rwQ6lyszlVn7; Mon, 15 Aug 2022 19:02:46 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 15 Aug 2022 19:05:47 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 15 Aug 2022 19:05:47 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , Subject: [RESEND PATCH] ARM: Recover kretprobes return address for EABI stack unwinder Date: Mon, 15 Aug 2022 19:02:40 +0800 Message-ID: <20220815110240.18293-1-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220815_040551_689716_A0748FBC X-CRM114-Status: GOOD ( 15.40 ) 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 'fed240d9c974 ("ARM: Recover kretprobe modified return address in stacktrace")' has implemented kretprobes return address recovery for FP unwinder, this patch makes it works for EABI unwinder. It saves __kretprobe_trampoline address in LR on stack to identify and recover the correct return address in EABI unwinder. Since EABI doesn't use r11 as frame pointer, we need to use SP to identify different kretprobes addresses. Here the value of SP has fixed distance to conventional FP position so it's fine to use it. Passed kunit kprobes_test on QEMU. Signed-off-by: Chen Zhongjin Reviewed-by: Linus Walleij --- arch/arm/Kconfig | 2 +- arch/arm/kernel/unwind.c | 12 ++++++++++++ arch/arm/probes/kprobes/core.c | 20 +++++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 87badeae3181..7165e314c0de 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -3,7 +3,7 @@ config ARM bool default y select ARCH_32BIT_OFF_T - select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE if HAVE_KRETPROBES && FRAME_POINTER && !ARM_UNWIND + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE if HAVE_KRETPROBES select ARCH_HAS_BINFMT_FLAT select ARCH_HAS_CURRENT_STACK_POINTER select ARCH_HAS_DEBUG_VIRTUAL if MMU diff --git a/arch/arm/kernel/unwind.c b/arch/arm/kernel/unwind.c index a37ea6c772cd..51e34fa4a4b3 100644 --- a/arch/arm/kernel/unwind.c +++ b/arch/arm/kernel/unwind.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -482,6 +483,12 @@ int unwind_frame(struct stackframe *frame) frame->pc = ctrl.vrs[PC]; frame->lr_addr = ctrl.lr_addr; +#ifdef CONFIG_KRETPROBES + if (is_kretprobe_trampoline(frame->pc)) + frame->pc = kretprobe_find_ret_addr(frame->tsk, + (void *)frame->sp, &frame->kr_cur); +#endif + return URC_OK; } @@ -522,6 +529,11 @@ void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk, frame.pc = thread_saved_pc(tsk); } +#ifdef CONFIG_KRETPROBES + frame.kr_cur = NULL; + frame.tsk = tsk; +#endif + while (1) { int urc; unsigned long where = frame.pc; diff --git a/arch/arm/probes/kprobes/core.c b/arch/arm/probes/kprobes/core.c index 9090c3a74dcc..1435b508aa36 100644 --- a/arch/arm/probes/kprobes/core.c +++ b/arch/arm/probes/kprobes/core.c @@ -41,6 +41,16 @@ (unsigned long)(addr) + \ (size)) +/* + * Since EABI unwinder doesn't use ARM_fp as conventional fp + * use ARM_sp as hint register for kretprobes. + */ +#ifdef CONFIG_ARM_UNWIND +#define TRAMP_FP ARM_sp +#else /* CONFIG_FRAME_POINTER */ +#define TRAMP_FP ARM_fp +#endif + DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); @@ -376,8 +386,8 @@ int __kprobes kprobe_exceptions_notify(struct notifier_block *self, void __naked __kprobes __kretprobe_trampoline(void) { __asm__ __volatile__ ( -#ifdef CONFIG_FRAME_POINTER "ldr lr, =__kretprobe_trampoline \n\t" +#ifdef CONFIG_FRAME_POINTER /* __kretprobe_trampoline makes a framepointer on pt_regs. */ #ifdef CONFIG_CC_IS_CLANG "stmdb sp, {sp, lr, pc} \n\t" @@ -395,8 +405,12 @@ void __naked __kprobes __kretprobe_trampoline(void) "add fp, sp, #60 \n\t" #endif /* CONFIG_CC_IS_CLANG */ #else /* !CONFIG_FRAME_POINTER */ + /* store SP, LR on stack and add EABI unwind hint */ + "stmdb sp, {sp, lr, pc} \n\t" + ".save {sp, lr, pc} \n\t" "sub sp, sp, #16 \n\t" "stmdb sp!, {r0 - r11} \n\t" + ".pad #52 \n\t" #endif /* CONFIG_FRAME_POINTER */ "mov r0, sp \n\t" "bl trampoline_handler \n\t" @@ -414,14 +428,14 @@ void __naked __kprobes __kretprobe_trampoline(void) /* Called from __kretprobe_trampoline */ static __used __kprobes void *trampoline_handler(struct pt_regs *regs) { - return (void *)kretprobe_trampoline_handler(regs, (void *)regs->ARM_fp); + return (void *)kretprobe_trampoline_handler(regs, (void *)regs->TRAMP_FP); } void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs) { ri->ret_addr = (kprobe_opcode_t *)regs->ARM_lr; - ri->fp = (void *)regs->ARM_fp; + ri->fp = (void *)regs->TRAMP_FP; /* Replace the return addr with trampoline addr. */ regs->ARM_lr = (unsigned long)&__kretprobe_trampoline;