From nobody Thu Sep 18 21:42:47 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E87CC4332F for ; Fri, 2 Dec 2022 02:18:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232025AbiLBCSr (ORCPT ); Thu, 1 Dec 2022 21:18:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231990AbiLBCSj (ORCPT ); Thu, 1 Dec 2022 21:18:39 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7715ECAF96 for ; Thu, 1 Dec 2022 18:18:38 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id EE6C9621EF for ; Fri, 2 Dec 2022 02:18:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A6393C433D6; Fri, 2 Dec 2022 02:18:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669947517; bh=d/lXGG0C2aiD02jOJY4ptnK9ZuIklkzqbOFkMk8yr0Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nIcOtysO/1w3lc+kLQuqC0ib2g7qPgkTgrCEhLuYzBJpk/gtzaibZPU4g2BgqOhRf mDc07gKkTVFokUSPi/PCJBp6rerTt+MmmmR1Cx8dUD/qfKSdvUOikLBrtp/R/VtQid fRv9cybKiXw4Nfgjbb9hVipdUBiWS2kdrTtUPPkqo4KQ6lNl+aTRmHxt3KqAPMk6jN Mplg13O9zyrFyUNYQI3JiAJgntKbbRH+gZqNO/pEIMOZYvZRzUg497vpeVCxsUYn7i pUhJDFtfRX/5/DgCW7aGnfvPeaYG4ECIwh41hqpPObpsFZDK56a+XEZn0ipoHUNd8D sWxn78mF86hdQ== From: "Masami Hiramatsu (Google)" To: Catalin Marinas , Will Deacon Cc: Mark Rutland , Mark Brown , Kalesh Singh , Masami Hiramatsu , Marc Zyngier , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Sandeepa Prabhu Subject: [PATCH v2 1/3] arm64: Prohibit instrumentation on arch_stack_walk() Date: Fri, 2 Dec 2022 11:18:33 +0900 Message-Id: <166994751368.439920.3236636557520824664.stgit@devnote3> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <166994750386.439920.1754385804350980158.stgit@devnote3> References: <166994750386.439920.1754385804350980158.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu (Google) Mark arch_stack_walk() as noinstr instead of notrace and inline functions called from arch_stack_walk() as __always_inline so that user does not put any instrumentations on it, because this function can be used from return_address() which is used by lockdep. Without this, if the kernel built with CONFIG_LOCKDEP=3Dy, just probing arch_stack_walk() via /kprobe_events will crash the kernel on arm64. # echo p arch_stack_walk >> ${TRACEFS}/kprobe_events # echo 1 > ${TRACEFS}/events/kprobes/enable kprobes: Failed to recover from reentered kprobes. kprobes: Dump kprobe: .symbol_name =3D arch_stack_walk, .offset =3D 0, .addr =3D arch_stack_wal= k+0x0/0x1c0 ------------[ cut here ]------------ kernel BUG at arch/arm64/kernel/probes/kprobes.c:241! kprobes: Failed to recover from reentered kprobes. kprobes: Dump kprobe: .symbol_name =3D arch_stack_walk, .offset =3D 0, .addr =3D arch_stack_wal= k+0x0/0x1c0 ------------[ cut here ]------------ kernel BUG at arch/arm64/kernel/probes/kprobes.c:241! PREEMPT SMP Modules linked in: CPU: 0 PID: 17 Comm: migration/0 Tainted: G N 6.1.0-rc5+ = #6 Hardware name: linux,dummy-virt (DT) Stopper: 0x0 <- 0x0 pstate: 600003c5 (nZCv DAIF -PAN -UAO -TCO -DIT -SSBS BTYPE=3D--) pc : kprobe_breakpoint_handler+0x178/0x17c lr : kprobe_breakpoint_handler+0x178/0x17c sp : ffff8000080d3090 x29: ffff8000080d3090 x28: ffff0df5845798c0 x27: ffffc4f59057a774 x26: ffff0df5ffbba770 x25: ffff0df58f420f18 x24: ffff49006f641000 x23: ffffc4f590579768 x22: ffff0df58f420f18 x21: ffff8000080d31c0 x20: ffffc4f590579768 x19: ffffc4f590579770 x18: 0000000000000006 x17: 5f6b636174735f68 x16: 637261203d207264 x15: 64612e202c30203d x14: 2074657366666f2e x13: 30633178302f3078 x12: 302b6b6c61775f6b x11: 636174735f686372 x10: ffffc4f590dc5bd8 x9 : ffffc4f58eb31958 x8 : 00000000ffffefff x7 : ffffc4f590dc5bd8 x6 : 80000000fffff000 x5 : 000000000000bff4 x4 : 0000000000000000 x3 : 0000000000000000 x2 : 0000000000000000 x1 : ffff0df5845798c0 x0 : 0000000000000064 Call trace: kprobes: Failed to recover from reentered kprobes. kprobes: Dump kprobe: .symbol_name =3D arch_stack_walk, .offset =3D 0, .addr =3D arch_stack_wal= k+0x0/0x1c0 ------------[ cut here ]------------ kernel BUG at arch/arm64/kernel/probes/kprobes.c:241! Fixes: 39ef362d2d45 ("arm64: Make return_address() use arch_stack_walk()") Cc: stable@vger.kernel.org Signed-off-by: Masami Hiramatsu (Google) Acked-by: Mark Rutland --- Changes in v2: - Use noinstr instead of NOKPROBE_SYMBOL() - Use __always_inline because nokprobe_inline will be changed if CONFIG_KPROBES=3Dn. - Fix indentation. --- arch/arm64/kernel/stacktrace.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c index 634279b3b03d..117e2c180f3c 100644 --- a/arch/arm64/kernel/stacktrace.c +++ b/arch/arm64/kernel/stacktrace.c @@ -23,8 +23,8 @@ * * The regs must be on a stack currently owned by the calling task. */ -static inline void unwind_init_from_regs(struct unwind_state *state, - struct pt_regs *regs) +static __always_inline void unwind_init_from_regs(struct unwind_state *sta= te, + struct pt_regs *regs) { unwind_init_common(state, current); =20 @@ -58,8 +58,8 @@ static __always_inline void unwind_init_from_caller(struc= t unwind_state *state) * duration of the unwind, or the unwind will be bogus. It is never valid = to * call this for the current task. */ -static inline void unwind_init_from_task(struct unwind_state *state, - struct task_struct *task) +static __always_inline void unwind_init_from_task(struct unwind_state *sta= te, + struct task_struct *task) { unwind_init_common(state, task); =20 @@ -186,7 +186,7 @@ void show_stack(struct task_struct *tsk, unsigned long = *sp, const char *loglvl) : stackinfo_get_unknown(); \ }) =20 -noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, +noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, struct task_struct *task, struct pt_regs *regs) { From nobody Thu Sep 18 21:42:47 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EADCFC4332F for ; Fri, 2 Dec 2022 02:18:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232047AbiLBCS5 (ORCPT ); Thu, 1 Dec 2022 21:18:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231989AbiLBCSt (ORCPT ); Thu, 1 Dec 2022 21:18:49 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B196CAF8A for ; Thu, 1 Dec 2022 18:18:48 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id DFE21620DF for ; Fri, 2 Dec 2022 02:18:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4B914C433D6; Fri, 2 Dec 2022 02:18:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669947527; bh=l+zSv5Pd3wyhqUmwoKQIAVoeZtKtlu6AJ8Bvwt0QiX4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zii/5dPf7Jv4cEPjUQA2jGVbRF1TX6Q20gJvpJPtsZ7BYH8G7nNgi7RcAYXTs9O2W 42eQmwYyhdB9miTrO9IVQve1gUnMi9vyD7xZOdQPsOPcbVIrcJRoXnsesxyGPjnSFR VMmi1WME5mYK5YaxnFORjAEp5hsm/FQ5rBBNylZaRq8BKDeXTzJ+ozojWnzpVn9ry/ OrmAQHUXvtvwo6mhuvCtoOQEt4s2qJ5ge36PdYyhOIpCJF3fG04uO9Ejhnw+V4KsI+ 6mrCVeNVyRTWq9wGLL7j5Z6SfHrLB7Csjq+ZfUFRWrWoLxtO5MiHURjGNZjoP85lkY pSGk+ZKaIAG9A== From: "Masami Hiramatsu (Google)" To: Catalin Marinas , Will Deacon Cc: Mark Rutland , Mark Brown , Kalesh Singh , Masami Hiramatsu , Marc Zyngier , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Sandeepa Prabhu Subject: [PATCH v2 2/3] arm64: kprobes: Let arch do_page_fault() fix up page fault in user handler Date: Fri, 2 Dec 2022 11:18:42 +0900 Message-Id: <166994752269.439920.4801339965959400456.stgit@devnote3> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <166994750386.439920.1754385804350980158.stgit@devnote3> References: <166994750386.439920.1754385804350980158.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu (Google) Since arm64's do_page_fault() can handle the page fault correctly than kprobe_fault_handler() according to the context, let it handle the page fault instead of simply call fixup_exception() in the kprobe_fault_handler(). Signed-off-by: Masami Hiramatsu (Google) Acked-by: Mark Rutland --- arch/arm64/kernel/probes/kprobes.c | 8 -------- 1 file changed, 8 deletions(-) diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/= kprobes.c index c9e4d0720285..d2ae37f89774 100644 --- a/arch/arm64/kernel/probes/kprobes.c +++ b/arch/arm64/kernel/probes/kprobes.c @@ -294,14 +294,6 @@ int __kprobes kprobe_fault_handler(struct pt_regs *reg= s, unsigned int fsr) } =20 break; - case KPROBE_HIT_ACTIVE: - case KPROBE_HIT_SSDONE: - /* - * In case the user-specified fault handler returned - * zero, try to fix up. - */ - if (fixup_exception(regs)) - return 1; } return 0; } From nobody Thu Sep 18 21:42:47 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1614C4332F for ; Fri, 2 Dec 2022 02:19:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232058AbiLBCTN (ORCPT ); Thu, 1 Dec 2022 21:19:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231990AbiLBCTD (ORCPT ); Thu, 1 Dec 2022 21:19:03 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2A35D678D for ; Thu, 1 Dec 2022 18:18:59 -0800 (PST) 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 766BAB82032 for ; Fri, 2 Dec 2022 02:18:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25870C433C1; Fri, 2 Dec 2022 02:18:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669947537; bh=R9kOaJyETkU6/vr64DTuIeuT6pjd+1zjqb/XRu6SYXI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q/+XSbpX8LbQtmkoSF88IcMiq2vXBlzkel817bNeZn1zXpn0qv5C1HcJVyzkk0n0W ScAB6ot6ZIaTErzIXVnd/zJSEpkNuzuzQ33nT11zATwVO+hjZuNrzkkjjVOKMzwMgH znjNoUF947eQLb9W8c3WYzleXKkasbaQDjHWABqQI+198sqdvRnPsUoQGssdIGesyA 538c4h8iPppXIj3oQvX58WgYFlFBgjnUHkBVegYVT8yTBU6pqkLnL/klRKr3+mDpzo qdNiH4sChPThlNmk+MQ0EdoLepgkFQkygqV2rM3UphEfoK5E4Ohny9O6nB9qDtvEJ9 0+lP9HJZec2YA== From: "Masami Hiramatsu (Google)" To: Catalin Marinas , Will Deacon Cc: Mark Rutland , Mark Brown , Kalesh Singh , Masami Hiramatsu , Marc Zyngier , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Sandeepa Prabhu Subject: [PATCH v2 3/3] arm64: kprobes: Return DBG_HOOK_ERROR if kprobes can not handle a BRK Date: Fri, 2 Dec 2022 11:18:52 +0900 Message-Id: <166994753273.439920.6629626290560350760.stgit@devnote3> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog In-Reply-To: <166994750386.439920.1754385804350980158.stgit@devnote3> References: <166994750386.439920.1754385804350980158.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu (Google) Return DBG_HOOK_ERROR if kprobes can not handle a BRK because it fails to find a kprobe corresponding to the address. Since arm64 kprobes uses stop_machine based text patching for removing BRK, it ensures all running kprobe_break_handler() is done at that point. And after removing the BRK, it removes the kprobe from its hash list. Thus, if the kprobe_break_handler() fails to find kprobe from hash list, there is a bug. Signed-off-by: Masami Hiramatsu (Google) Acked-by: Mark Rutland --- Changes in v2: - Update comments in the code. --- arch/arm64/kernel/probes/kprobes.c | 78 +++++++++++++++++---------------= ---- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/= kprobes.c index d2ae37f89774..f35d059a9a36 100644 --- a/arch/arm64/kernel/probes/kprobes.c +++ b/arch/arm64/kernel/probes/kprobes.c @@ -298,7 +298,8 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs= , unsigned int fsr) return 0; } =20 -static void __kprobes kprobe_handler(struct pt_regs *regs) +static int __kprobes +kprobe_breakpoint_handler(struct pt_regs *regs, unsigned long esr) { struct kprobe *p, *cur_kprobe; struct kprobe_ctlblk *kcb; @@ -308,39 +309,44 @@ static void __kprobes kprobe_handler(struct pt_regs *= regs) cur_kprobe =3D kprobe_running(); =20 p =3D get_kprobe((kprobe_opcode_t *) addr); + if (WARN_ON_ONCE(!p)) { + /* + * Something went wrong. This BRK used an immediate reserved + * for kprobes, but we couldn't find any corresponding probe. + */ + return DBG_HOOK_ERROR; + } =20 - if (p) { - if (cur_kprobe) { - if (reenter_kprobe(p, regs, kcb)) - return; - } else { - /* Probe hit */ - set_current_kprobe(p); - kcb->kprobe_status =3D KPROBE_HIT_ACTIVE; - - /* - * If we have no pre-handler or it returned 0, we - * continue with normal processing. If we have a - * pre-handler and it returned non-zero, it will - * modify the execution path and no need to single - * stepping. Let's just reset current kprobe and exit. - */ - if (!p->pre_handler || !p->pre_handler(p, regs)) { - setup_singlestep(p, regs, kcb, 0); - } else - reset_current_kprobe(); - } + if (cur_kprobe) { + /* Hit a kprobe inside another kprobe */ + if (!reenter_kprobe(p, regs, kcb)) + return DBG_HOOK_ERROR; + } else { + /* Probe hit */ + set_current_kprobe(p); + kcb->kprobe_status =3D KPROBE_HIT_ACTIVE; + + /* + * If we have no pre-handler or it returned 0, we + * continue with normal processing. If we have a + * pre-handler and it returned non-zero, it will + * modify the execution path and not need to single-step + * Let's just reset current kprobe and exit. + */ + if (!p->pre_handler || !p->pre_handler(p, regs)) + setup_singlestep(p, regs, kcb, 0); + else + reset_current_kprobe(); } - /* - * The breakpoint instruction was removed right - * after we hit it. Another cpu has removed - * either a probepoint or a debugger breakpoint - * at this address. In either case, no further - * handling of this interrupt is appropriate. - * Return back to original instruction, and continue. - */ + + return DBG_HOOK_HANDLED; } =20 +static struct break_hook kprobes_break_hook =3D { + .imm =3D KPROBES_BRK_IMM, + .fn =3D kprobe_breakpoint_handler, +}; + static int __kprobes kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned long esr) { @@ -365,18 +371,6 @@ static struct break_hook kprobes_break_ss_hook =3D { .fn =3D kprobe_breakpoint_ss_handler, }; =20 -static int __kprobes -kprobe_breakpoint_handler(struct pt_regs *regs, unsigned long esr) -{ - kprobe_handler(regs); - return DBG_HOOK_HANDLED; -} - -static struct break_hook kprobes_break_hook =3D { - .imm =3D KPROBES_BRK_IMM, - .fn =3D kprobe_breakpoint_handler, -}; - /* * Provide a blacklist of symbols identifying ranges which cannot be kprob= ed. * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).