Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp204085imw; Fri, 15 Jul 2022 01:09:59 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vyADJm4ls5/JktV7t7Eb3Nbj858mVeEdXdkX68FuQdlClSaT4eXDUm+aPIbnrD9DYCPcd2 X-Received: by 2002:a63:515f:0:b0:419:b668:4f09 with SMTP id r31-20020a63515f000000b00419b6684f09mr5558708pgl.347.1657872599175; Fri, 15 Jul 2022 01:09:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657872599; cv=none; d=google.com; s=arc-20160816; b=IZVQn0HMFYPx0w2u4Y0rbXOKftDfuG4Gqsk+4eUn8LE9ppTBDb3G/KoFX8E4tPwZTM NQLABGifdC5d4432PoDD2YDl+ZyDAaCLypLkhfqMYIL6ctrBGGFhFusMBMR9ldWdb7Wk ZcwSjRnrk7/2oWzO4oDgbsM+CJhMSzy0BKE0C0ZHohk1c6O4+mOgzPrQQGjRYB57qsBA FDTbVVd37o5QabqgubbxVnzT8b1TBKBV7UoPmKlErSLFZ4hgykviFc2+i3PsOS5Yli15 n8uZghlRGZ8YxEHQMQxkS7t83BF027er5QMgi1s9WazNFqxOCFQWxuEFy46asJBrW68D kENA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=+/AfyCDlQNl/fw4MKgMA4QQ9fkKDnLnTxjyqPXdp3aA=; b=QUjObxBi7CjcXJFCQcjtFykgkoMpj+383Bc/fYtgvdlpEBxNW6IytStjFx5X45Fxd6 qU/fYZA9gjzfhz8r8nLYufwh5hIQELXZVvnJHdUaci6DwP91DVyunUfTM5Xl+dOJZUPa 0ogbMSYJRc3X4mP6EuqKUtOFtVpJ1Pe5VbjKHKJlN6zch1XYwPF2s7kRiGQhKbQ8F9fH SA94DttR7sleKjullNvAE9XjAhbbgxlEJpn32SPecqHDoheBMDd4YZDvVxEUg0o4RXIC q1/KdT+3xn6wcg+nVw9bAfP14jjibUq6HpXJfU/hjADubhy7aieQ6U+6UBfsWW4npvZi SOIw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u4-20020a170902bf4400b0015d0c53ae0esi4230770pls.491.2022.07.15.01.09.32; Fri, 15 Jul 2022 01:09:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232446AbiGOIBo (ORCPT + 99 others); Fri, 15 Jul 2022 04:01:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229782AbiGOIBn (ORCPT ); Fri, 15 Jul 2022 04:01:43 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A87877E00F; Fri, 15 Jul 2022 01:01:42 -0700 (PDT) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4LkkHS3sP7zVg3M; Fri, 15 Jul 2022 15:57:56 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Fri, 15 Jul 2022 16:01:11 +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; Fri, 15 Jul 2022 16:01:11 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , Subject: [PATCH] ARM: Recover kretprobes return address for EABI stack unwinder Date: Fri, 15 Jul 2022 15:58:04 +0800 Message-ID: <20220715075804.93756-1-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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 --- 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 7630ba9cb6cc..2bd987aa938d 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; -- 2.17.1