Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp118289pxb; Wed, 20 Oct 2021 17:57:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOWDU67zdRWppYCgOe9WPmi2wjx2EfS4oFJKpoPuf7uZUYqLyfwSkjbAhdN7XPE2QDUN4Z X-Received: by 2002:a17:906:d0ca:: with SMTP id bq10mr3274364ejb.25.1634777873140; Wed, 20 Oct 2021 17:57:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634777873; cv=none; d=google.com; s=arc-20160816; b=qyOKTIzWOjc7JFr53Kj8najZHKD5VML5/L/YosfxjDi3LZkQtYfDOR0hdeNvMHJXnY uuTu3YQ1P57VlpVWdPJDe2cmvskOHWFnFB2VTIXciE+FME3TNynw5KpO5ExQD2dO2ikQ dYK8Hu4ZzMPGQH+C/a2Gm1oBVeM+krJ++Ar2yCBkaXdBgJEDUvlhOV/raG/SO2BlUFiZ kH4Lv2VqJLel6qdbLK8vOxYucreHzqpddeiuOjIggnrsb4mGt4xlsDu1MTlNMd2oPLX/ iqpxR9vxESzAHxm0evi0FbX/qsHApwzIX6HcmguMCTUAG17R/AO5Ul4rDPxUANLFmLpC dLww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=xqYa2audKBsv+C1qbnRqq2B3j9m2Qckz1iFW0jTSgqQ=; b=MKz7ZxO8IEgbuv2uhu6/0dJgm3pvElxZ2DhYEekcGifQtPeHs7KgacZB/ny9+6yPiw OIepYf3ome/nfzSae9sVAedopQNjIIakf/fng/qfJUV2mFG3GnKPHsjooarB/qqFxuqt LkFyrTM8S3Sj6PauQl/efn8AeIiZ0/+NJVfuTZMLuOCDQEOBbEr5d7juFlk5fT+twAeZ mJl6zLUKqHCbGxKS7MerXUdgasXeQMOl2AMYfeBCT1Uhi1NYfNvQ0O1PfNb56hgFP97Z wF4n1Nsbz0ys7dRMAuSR8wOIS/co1wZYNvt4YpqtVRbVfMHu649Yr25895EVSeeq06mT GCCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uQTTlRIg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dr12si6980538ejc.214.2021.10.20.17.57.30; Wed, 20 Oct 2021 17:57:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=uQTTlRIg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231444AbhJUA5i (ORCPT + 99 others); Wed, 20 Oct 2021 20:57:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:51594 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231523AbhJUA51 (ORCPT ); Wed, 20 Oct 2021 20:57:27 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E7680611C7; Thu, 21 Oct 2021 00:55:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1634777712; bh=nvZy1sVKYSK6zZvA82X0Qwm/erFQUaJsuPthlqjB5TY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uQTTlRIg7wvs0J2F3vfJYET6CB+rhWaAmsJkiPbCBRnVrOaK4XDeEnv/5Z0FuY+dZ jWqDkyeL5FUiU+gfIxe895VEFl6yzXNzLC//tzwFW7CC7QauWEvaO9SFAYtgpfNNnm 3HotPrIDgeVACCBn9qww0PmXS7a8GEwyr0jnQDbbmOdZhusAL/pZRShgzZ5V9Iblkt jxER6wFs2esN02Y9jp8O6TBVjPB/mrdv611l3McaLm6Wizc1gNcDvFfH/0FMPmXhMG emelmNk9kvib7ugVHbfuXDysgDRUxLo6eCMZHa4JkJFUJU538w4z7pZI86pvIDfYxQ Yjjg5TeosTXGA== From: Masami Hiramatsu To: Steven Rostedt Cc: "Naveen N . Rao" , Ananth N Mavinakayanahalli , Ingo Molnar , linux-kernel@vger.kernel.org, mhiramat@kernel.org, Sven Schnelle , Catalin Marinas , Will Deacon , Russell King , Nathan Chancellor , Nick Desaulniers , linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 6/9] arm64: Recover kretprobe modified return address in stacktrace Date: Thu, 21 Oct 2021 09:55:09 +0900 Message-Id: <163477770935.264901.1772964361191833681.stgit@devnote2> X-Mailer: git-send-email 2.25.1 In-Reply-To: <163477765570.264901.3851692300287671122.stgit@devnote2> References: <163477765570.264901.3851692300287671122.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Since the kretprobe replaces the function return address with the kretprobe_trampoline on the stack, stack unwinder shows it instead of the correct return address. This checks whether the next return address is the __kretprobe_trampoline(), and if so, try to find the correct return address from the kretprobe instance list. For this purpose this adds 'kr_cur' loop cursor to memorize the current kretprobe instance. With this fix, now arm64 can enable CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE, and pass the kprobe self tests. Signed-off-by: Masami Hiramatsu --- Changes in v2: - Add comment for kr_cur. - Make the kretprobe related code depends on CONFIG_KRETPROBES. - Initialize "kr_cur" directly in start_backtrace() instead of clearing "frame" data structure by memset(). --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/stacktrace.h | 4 ++++ arch/arm64/kernel/stacktrace.c | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 5c7ae4c3954b..edde5171ffb2 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -11,6 +11,7 @@ config ARM64 select ACPI_PPTT if ACPI select ARCH_HAS_DEBUG_WX select ARCH_BINFMT_ELF_STATE + select ARCH_CORRECT_STACKTRACE_ON_KRETPROBE select ARCH_ENABLE_HUGEPAGE_MIGRATION if HUGETLB_PAGE && MIGRATION select ARCH_ENABLE_MEMORY_HOTPLUG select ARCH_ENABLE_MEMORY_HOTREMOVE diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index 8aebc00c1718..a4e046ef4568 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -59,6 +60,9 @@ struct stackframe { #ifdef CONFIG_FUNCTION_GRAPH_TRACER int graph; #endif +#ifdef CONFIG_KRETPROBES + struct llist_node *kr_cur; +#endif }; extern int unwind_frame(struct task_struct *tsk, struct stackframe *frame); diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 8982a2b78acf..c30624fff6ac 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -41,6 +41,9 @@ void start_backtrace(struct stackframe *frame, unsigned long fp, #ifdef CONFIG_FUNCTION_GRAPH_TRACER frame->graph = 0; #endif +#ifdef CONFIG_KRETPROBES + frame->kr_cur = NULL; +#endif /* * Prime the first unwind. @@ -129,6 +132,10 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame) frame->pc = ret_stack->ret; } #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ +#ifdef CONFIG_KRETPROBES + if (is_kretprobe_trampoline(frame->pc)) + frame->pc = kretprobe_find_ret_addr(tsk, (void *)frame->fp, &frame->kr_cur); +#endif frame->pc = ptrauth_strip_insn_pac(frame->pc);