Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp223056pxb; Fri, 15 Oct 2021 04:21:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzW2ssHiXfNoKKGsgB+uV4PyOmpfHUGQHrVi2xwg8JZfw3fO1hseROy97ryO+/TMIkvUjgW X-Received: by 2002:a17:902:e144:b0:13f:4b7:68c0 with SMTP id d4-20020a170902e14400b0013f04b768c0mr10330511pla.77.1634296873580; Fri, 15 Oct 2021 04:21:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634296873; cv=none; d=google.com; s=arc-20160816; b=HgVA9sTIAeV1ZqR81X1jYYryU1g/FtpPPfW6NvQ0UYJu7tpcrWOhH2CPCh+t/yT/6q JjGL3oyQ0w8fpFgSCc/N2NUSERC4SxHGRQzbT0y2U8OUEe0id5xiYXOuRfRUctU0LM4u zhip76KrWVB8KiPh5MMoXvS+Lb/T/Mwz5+mbTTbcHbU7rYOBHXygBAGPGgluLFMtZauv EIs0YW+9/GxsPVh3RMRZ/Xtdf5bHoki757/Vula5JhFOUr4/uk+0cHByIJ7Z6e3SBX2F 2XV16RIZ75GCpug/OjK2BLlqFqQfAub91qVBxdIXd3BSrntKqr6QrfiFpXIPpGOWplmJ FmNA== 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 :references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dkim-filter; bh=LkJybEj9FMuSQymj/zVROIuX/DLBwHm7LAFTUmAbltw=; b=uHNVD2pCWS5rHW7IDoBwqB7idxNmWMyKLJePKFUbAlhCODiO6CKDUfr8Ybj/XvOfS2 IY0q5iqt2uMH09ls/1+RALqWo4+Dsxd4HjXC79Y2doda2haWZnSAatb9ewDY5rjneQ0/ ht4Y8DC+J0thQub0K4PuzFgWA2VPFcC+sHpO4msrza0FynkQBDkFBNVwpI2v0K2wsI4L A+nZGyfgpTMpg8BlTqv1xjlI3PeRZfFvrcQXr2lntwSJn3gLulJ7Wa9rq18VBHPGmXMF ev9FRZl5Ze+p+EibAwUrohRGkG/6cmn/ZNSA3COcyRgoFUoB7DE9F00shFBfDUGVQhPS gDvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.microsoft.com header.s=default header.b=fiU+VTml; 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=linux.microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v4si1109609plo.132.2021.10.15.04.20.59; Fri, 15 Oct 2021 04:21:13 -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=@linux.microsoft.com header.s=default header.b=fiU+VTml; 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=linux.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235226AbhJODBl (ORCPT + 99 others); Thu, 14 Oct 2021 23:01:41 -0400 Received: from linux.microsoft.com ([13.77.154.182]:58286 "EHLO linux.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235197AbhJODBT (ORCPT ); Thu, 14 Oct 2021 23:01:19 -0400 Received: from x64host.home (unknown [47.187.212.181]) by linux.microsoft.com (Postfix) with ESMTPSA id 17D0320B9D20; Thu, 14 Oct 2021 19:59:13 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 17D0320B9D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1634266753; bh=LkJybEj9FMuSQymj/zVROIuX/DLBwHm7LAFTUmAbltw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=fiU+VTmlZc3oj8fNbn/VLSXOafDgTgM2g3gjFOVMBgLoGMcF5WazypfITO5sleyHT m/y2RZd/AGZJ+ZQg0qAOzNkS30600q8rJfSkHniScrPfTvC36yiOnj82qKU9+XFaiT IEHTjde1HsUcUWHrfK89UrMl3TQXgO95Qouo0EE0= From: madvenka@linux.microsoft.com To: mark.rutland@arm.com, broonie@kernel.org, jpoimboe@redhat.com, ardb@kernel.org, nobuta.keiya@fujitsu.com, sjitindarsingh@gmail.com, catalin.marinas@arm.com, will@kernel.org, jmorris@namei.org, linux-arm-kernel@lists.infradead.org, live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, madvenka@linux.microsoft.com Subject: [PATCH v10 10/11] arm64: Introduce stack trace reliability checks in the unwinder Date: Thu, 14 Oct 2021 21:58:46 -0500 Message-Id: <20211015025847.17694-11-madvenka@linux.microsoft.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211015025847.17694-1-madvenka@linux.microsoft.com> References: <20211015025847.17694-1-madvenka@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Madhavan T. Venkataraman" There are some kernel features and conditions that make a stack trace unreliable. Callers may require the unwinder to detect these cases. E.g., livepatch. Introduce a new function called unwind_check_reliability() that will detect these cases and set a flag in the stack frame. Call unwind_check_reliability() for every frame, that is, in unwind_start() and unwind_next(). Introduce the first reliability check in unwind_check_reliability() - If a return PC is not a valid kernel text address, consider the stack trace unreliable. It could be some generated code. Other reliability checks will be added in the future. Let unwind() return a boolean to indicate if the stack trace is reliable. Introduce arch_stack_walk_reliable() for ARM64. This works like arch_stack_walk() except that it returns -EINVAL if the stack trace is not reliable. Until all the reliability checks are in place, arch_stack_walk_reliable() may not be used by livepatch. But it may be used by debug and test code. Signed-off-by: Madhavan T. Venkataraman --- arch/arm64/include/asm/stacktrace.h | 3 ++ arch/arm64/kernel/stacktrace.c | 48 ++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/stacktrace.h b/arch/arm64/include/asm/stacktrace.h index ba2180c7d5cd..ce0710fa3037 100644 --- a/arch/arm64/include/asm/stacktrace.h +++ b/arch/arm64/include/asm/stacktrace.h @@ -51,6 +51,8 @@ struct stack_info { * replacement lr value in the ftrace graph stack. * * @failed: Unwind failed. + * + * @reliable: Stack trace is reliable. */ struct stackframe { unsigned long fp; @@ -62,6 +64,7 @@ struct stackframe { int graph; #endif bool failed; + bool reliable; }; extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk, diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 8e9e6f38c975..142f08ae515f 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -18,6 +18,22 @@ #include #include +/* + * Check the stack frame for conditions that make further unwinding unreliable. + */ +static void notrace unwind_check_reliability(struct stackframe *frame) +{ + /* + * If the PC is not a known kernel text address, then we cannot + * be sure that a subsequent unwind will be reliable, as we + * don't know that the code follows our unwind requirements. + */ + if (!__kernel_text_address(frame->pc)) + frame->reliable = false; +} + +NOKPROBE_SYMBOL(unwind_check_reliability); + /* * AArch64 PCS assigns the frame pointer to x29. * @@ -55,6 +71,8 @@ static void notrace unwind_start(struct stackframe *frame, unsigned long fp, frame->prev_fp = 0; frame->prev_type = STACK_TYPE_UNKNOWN; frame->failed = false; + frame->reliable = true; + unwind_check_reliability(frame); } NOKPROBE_SYMBOL(unwind_start); @@ -138,6 +156,7 @@ static void notrace unwind_next(struct task_struct *tsk, #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ frame->pc = ptrauth_strip_insn_pac(frame->pc); + unwind_check_reliability(frame); } NOKPROBE_SYMBOL(unwind_next); @@ -167,7 +186,7 @@ static bool notrace unwind_continue(struct task_struct *task, NOKPROBE_SYMBOL(unwind_continue); -static void notrace unwind(struct task_struct *tsk, +static bool notrace unwind(struct task_struct *tsk, unsigned long fp, unsigned long pc, bool (*fn)(void *, unsigned long), void *data) @@ -177,6 +196,7 @@ static void notrace unwind(struct task_struct *tsk, unwind_start(&frame, fp, pc); while (unwind_continue(tsk, &frame, fn, data)) unwind_next(tsk, &frame); + return frame.reliable; } NOKPROBE_SYMBOL(unwind); @@ -238,4 +258,30 @@ noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, } +/* + * arch_stack_walk_reliable() may not be used for livepatch until all of + * the reliability checks are in place in unwind_consume(). However, + * debug and test code can choose to use it even if all the checks are not + * in place. + */ +noinline int notrace arch_stack_walk_reliable(stack_trace_consume_fn consume_fn, + void *cookie, + struct task_struct *task) +{ + unsigned long fp, pc; + + if (task == current) { + /* Skip arch_stack_walk_reliable() in the stack trace. */ + fp = (unsigned long)__builtin_frame_address(1); + pc = (unsigned long)__builtin_return_address(0); + } else { + /* Caller guarantees that the task is not running. */ + fp = thread_saved_fp(task); + pc = thread_saved_pc(task); + } + if (unwind(task, fp, pc, consume_fn, cookie)) + return 0; + return -EINVAL; +} + #endif -- 2.25.1