Message ID | 166990556124.253128.2968612748605960211.stgit@devnote3 |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp304201wrr; Thu, 1 Dec 2022 06:53:46 -0800 (PST) X-Google-Smtp-Source: AA0mqf5s1EsDIrHnLq61whGkca3okW34TOzaxXpZhi0sSIMKXCjv1ZmZLauxAjGPjc5ZU8iFjQSD X-Received: by 2002:a17:906:34d2:b0:7bf:1b9d:c0a8 with SMTP id h18-20020a17090634d200b007bf1b9dc0a8mr18589992ejb.551.1669906426591; Thu, 01 Dec 2022 06:53:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669906426; cv=none; d=google.com; s=arc-20160816; b=BmbPVsbcnPcitPLMe46yAlSoz4kCQ7RZRVVETUy+82Vq0y/8BPj2S2OeJaPJ7zeawY J84D1PW6Ruws4BoxMRippzlN1/pUV+jcnc6S5iYrDJIfQXu3nl5zUV/ZUfF/nrACcAl3 D17OibxdJ1tDvNp/saugDkyppHxdJUALJEpRdWcVQN78sJF8/QL6y0q1/fl4ZGUCCxPl jekiVBHOfbOxVvM4phxi8LEGEFSEITwo4LnbP0QSjI75CDxjllg5PRiYVoyvpVMqcLzD kPFi8sVKEK8ADYhUWOXPYtsbQIrj5rgvvuR08+bbiXwQyvdqH773RpQCLQgpdTJ0Bm12 ZjvQ== 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=aQsxL2Fwdl8CrLbO0RZzkRSQviLhgUe+EpPAqXNlAWM=; b=aJUzSKWMTsb/EUlpwn8iC8ZzQi4nJLvhZk/6ALl1AK19LXXDEqg8y1JiZZFjZgcECj dKp8d9bw6i1taLVSBqEGFpnQbOzGVM3g2gLKRje7FQ19rLNoahX8dmd+E8PcsRqTS9GN eGcCFUGOgy1Z9ZNt+kYDZmP+ZFkJdvHZfH7jhbPSUlzJMw0igEtc8nNLaw2p/hAVLH5E QP1FKVxxHYgpXmAGWxPQFLpE8DfFnps/7+vsxC82pJtwC8M1+AnHp0xJin/uwnWIw/q4 NIb/lCjMh3xNvQVTF1ucRBOyZjGHnLCbAfyRrh1TwGH7+Jp9mPpCNUgIlgCDA8BB2Sy0 BCpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="JNTeI/BK"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cw13-20020a056402228d00b0046b2f820e6esi4031789edb.124.2022.12.01.06.53.23; Thu, 01 Dec 2022 06:53:46 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="JNTeI/BK"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231415AbiLAOjd (ORCPT <rfc822;heyuhang3455@gmail.com> + 99 others); Thu, 1 Dec 2022 09:39:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231879AbiLAOj2 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 1 Dec 2022 09:39:28 -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 1A8ECA8FE1 for <linux-kernel@vger.kernel.org>; Thu, 1 Dec 2022 06:39:26 -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 AE9FD62029 for <linux-kernel@vger.kernel.org>; Thu, 1 Dec 2022 14:39:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E6BFC433D6; Thu, 1 Dec 2022 14:39:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669905565; bh=s6ICiOAJJFwO6u7uP13cxjjC82jHSuTLzzoIQZoxdr0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JNTeI/BKZGk6hBCF/yAwVMC1CHBMGqNL+rxW+yTg8twEdGmtg0ijvJ405PVCnq2SJ FURQug7F6k7EfTflm0OzMHKkb36xU4UKDrQ11EhvYz3/Xw/pq1wKkF0o+Teh2oe3S4 uYl0NOVcgqoNChE4HoQ2d7TANB32cMpWsUwdR+ggmlh7ePihjB7QlczQDvz6MDsuuz 74iLg40BxdaZrMQeutfbYIKIsIdmZ4eQ8Cgh+kYjuW2hlTYRwpmNX4TKYjrBTSavgk c/tKSjHhAXSR06djwoo82kxV5GOukSHcniabmY/vep4mGiDwd8sTOtImg0rR0iHg9V 1HbrEBQ7GqGgg== From: "Masami Hiramatsu (Google)" <mhiramat@kernel.org> To: Catalin Marinas <catalin.marinas@arm.com>, Will Deacon <will@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com>, Mark Brown <broonie@kernel.org>, Kalesh Singh <kaleshsingh@google.com>, Masami Hiramatsu <mhiramat@kernel.org>, Marc Zyngier <maz@kernel.org>, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Sandeepa Prabhu <sandeepa.s.prabhu@gmail.com> Subject: [PATCH 3/3] arm64: kprobes: Return DBG_HOOK_ERROR if kprobes can not handle a BRK Date: Thu, 1 Dec 2022 23:39:21 +0900 Message-Id: <166990556124.253128.2968612748605960211.stgit@devnote3> X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog In-Reply-To: <166990553243.253128.13594802750635478633.stgit@devnote3> References: <166990553243.253128.13594802750635478633.stgit@devnote3> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751023800966913610?= X-GMAIL-MSGID: =?utf-8?q?1751023800966913610?= |
Series |
arm64: kprobes: Fix bugs in kprobes for arm64
|
|
Commit Message
Masami Hiramatsu (Google)
Dec. 1, 2022, 2:39 p.m. UTC
From: Masami Hiramatsu (Google) <mhiramat@kernel.org> 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) <mhiramat@kernel.org> --- arch/arm64/kernel/probes/kprobes.c | 79 +++++++++++++++++------------------- 1 file changed, 37 insertions(+), 42 deletions(-)
Comments
On Thu, Dec 01, 2022 at 11:39:21PM +0900, Masami Hiramatsu (Google) wrote: > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > 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. IIUC this relies on BRK handling not being preemptible, which is something we've repeatedly considered changing along with a bunch of other debug exception handling. In case we do try to change that in future, it would be good to have a comment somewhere to that effect. I think there are other ways we could synchronise against that (e.g. using RCU tasks rude) if we ever do that, and this patch looks good to me. > > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > --- > arch/arm64/kernel/probes/kprobes.c | 79 +++++++++++++++++------------------- > 1 file changed, 37 insertions(+), 42 deletions(-) > > diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c > index d2ae37f89774..ea56b22d4da8 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; > } > > -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,45 @@ static void __kprobes kprobe_handler(struct pt_regs *regs) > cur_kprobe = kprobe_running(); > > p = get_kprobe((kprobe_opcode_t *) addr); > + if (WARN_ON_ONCE(!p)) { > + /* > + * Something went wrong. This must be put by kprobe, but we > + * could not find corresponding kprobes. Let the kernel handle > + * this error case. > + */ Could we make this: /* * Something went wrong. This BRK used an immediate reserved * for kprobes, but we couldn't find any corresponding probe. */ > + return DBG_HOOK_ERROR; > + } > > - if (p) { > - if (cur_kprobe) { > - if (reenter_kprobe(p, regs, kcb)) > - return; > - } else { > - /* Probe hit */ > - set_current_kprobe(p); > - kcb->kprobe_status = 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 = 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. > + */ Minor wording nit: could we replace: no need to single stepping. With: not need to single-step. Thanks, Mark. > + 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; > } > > +static struct break_hook kprobes_break_hook = { > + .imm = KPROBES_BRK_IMM, > + .fn = kprobe_breakpoint_handler, > +}; > + > static int __kprobes > kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned long esr) > { > @@ -365,18 +372,6 @@ static struct break_hook kprobes_break_ss_hook = { > .fn = kprobe_breakpoint_ss_handler, > }; > > -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 = { > - .imm = KPROBES_BRK_IMM, > - .fn = kprobe_breakpoint_handler, > -}; > - > /* > * Provide a blacklist of symbols identifying ranges which cannot be kprobed. > * This blacklist is exposed to userspace via debugfs (kprobes/blacklist). >
On Thu, 1 Dec 2022 15:08:52 +0000 Mark Rutland <mark.rutland@arm.com> wrote: > On Thu, Dec 01, 2022 at 11:39:21PM +0900, Masami Hiramatsu (Google) wrote: > > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > > 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. > > IIUC this relies on BRK handling not being preemptible, which is something > we've repeatedly considered changing along with a bunch of other debug > exception handling. Interesting idea... and it also need many changes in kprobe itself. > > In case we do try to change that in future, it would be good to have a comment > somewhere to that effect. Hmm, it would fundamentally change the assumptions that kprobes relies on, and would require a lot of thought again. (e.g. current running kprobe is stored in per-cpu variable, it should be per-task. etc.) > > I think there are other ways we could synchronise against that (e.g. using RCU > tasks rude) if we ever do that, and this patch looks good to me. > > > > > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > --- > > arch/arm64/kernel/probes/kprobes.c | 79 +++++++++++++++++------------------- > > 1 file changed, 37 insertions(+), 42 deletions(-) > > > > diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c > > index d2ae37f89774..ea56b22d4da8 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; > > } > > > > -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,45 @@ static void __kprobes kprobe_handler(struct pt_regs *regs) > > cur_kprobe = kprobe_running(); > > > > p = get_kprobe((kprobe_opcode_t *) addr); > > + if (WARN_ON_ONCE(!p)) { > > + /* > > + * Something went wrong. This must be put by kprobe, but we > > + * could not find corresponding kprobes. Let the kernel handle > > + * this error case. > > + */ > > Could we make this: > > /* > * Something went wrong. This BRK used an immediate reserved > * for kprobes, but we couldn't find any corresponding probe. > */ OK. > > > + return DBG_HOOK_ERROR; > > + } > > > > - if (p) { > > - if (cur_kprobe) { > > - if (reenter_kprobe(p, regs, kcb)) > > - return; > > - } else { > > - /* Probe hit */ > > - set_current_kprobe(p); > > - kcb->kprobe_status = 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 = 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. > > + */ > > Minor wording nit: could we replace: > > no need to single stepping. > > With: > > not need to single-step. OK, I'll update both in v2. Thank you! > > Thanks, > Mark. > > > + 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; > > } > > > > +static struct break_hook kprobes_break_hook = { > > + .imm = KPROBES_BRK_IMM, > > + .fn = kprobe_breakpoint_handler, > > +}; > > + > > static int __kprobes > > kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned long esr) > > { > > @@ -365,18 +372,6 @@ static struct break_hook kprobes_break_ss_hook = { > > .fn = kprobe_breakpoint_ss_handler, > > }; > > > > -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 = { > > - .imm = KPROBES_BRK_IMM, > > - .fn = kprobe_breakpoint_handler, > > -}; > > - > > /* > > * Provide a blacklist of symbols identifying ranges which cannot be kprobed. > > * This blacklist is exposed to userspace via debugfs (kprobes/blacklist). > >
On Fri, Dec 02, 2022 at 01:07:13AM +0900, Masami Hiramatsu wrote: > On Thu, 1 Dec 2022 15:08:52 +0000 > Mark Rutland <mark.rutland@arm.com> wrote: > > > On Thu, Dec 01, 2022 at 11:39:21PM +0900, Masami Hiramatsu (Google) wrote: > > > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > > > > 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. > > > > IIUC this relies on BRK handling not being preemptible, which is something > > we've repeatedly considered changing along with a bunch of other debug > > exception handling. > > Interesting idea... and it also need many changes in kprobe itself. > > > > > In case we do try to change that in future, it would be good to have a comment > > somewhere to that effect. > > Hmm, it would fundamentally change the assumptions that kprobes relies on, > and would require a lot of thought again. (e.g. current running kprobe is > stored in per-cpu variable, it should be per-task. etc.) Ah; I had not considered that. Feel free to ignore the above; with the comments as below: Acked-by: Mark Rutland <mark.rutland@arm.com> Thanks, Mark. > > > > > I think there are other ways we could synchronise against that (e.g. using RCU > > tasks rude) if we ever do that, and this patch looks good to me. > > > > > > > > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > --- > > > arch/arm64/kernel/probes/kprobes.c | 79 +++++++++++++++++------------------- > > > 1 file changed, 37 insertions(+), 42 deletions(-) > > > > > > diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c > > > index d2ae37f89774..ea56b22d4da8 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; > > > } > > > > > > -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,45 @@ static void __kprobes kprobe_handler(struct pt_regs *regs) > > > cur_kprobe = kprobe_running(); > > > > > > p = get_kprobe((kprobe_opcode_t *) addr); > > > + if (WARN_ON_ONCE(!p)) { > > > + /* > > > + * Something went wrong. This must be put by kprobe, but we > > > + * could not find corresponding kprobes. Let the kernel handle > > > + * this error case. > > > + */ > > > > Could we make this: > > > > /* > > * Something went wrong. This BRK used an immediate reserved > > * for kprobes, but we couldn't find any corresponding probe. > > */ > > OK. > > > > > > + return DBG_HOOK_ERROR; > > > + } > > > > > > - if (p) { > > > - if (cur_kprobe) { > > > - if (reenter_kprobe(p, regs, kcb)) > > > - return; > > > - } else { > > > - /* Probe hit */ > > > - set_current_kprobe(p); > > > - kcb->kprobe_status = 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 = 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. > > > + */ > > > > Minor wording nit: could we replace: > > > > no need to single stepping. > > > > With: > > > > not need to single-step. > > OK, I'll update both in v2. > > Thank you! > > > > > Thanks, > > Mark. > > > > > + 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; > > > } > > > > > > +static struct break_hook kprobes_break_hook = { > > > + .imm = KPROBES_BRK_IMM, > > > + .fn = kprobe_breakpoint_handler, > > > +}; > > > + > > > static int __kprobes > > > kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned long esr) > > > { > > > @@ -365,18 +372,6 @@ static struct break_hook kprobes_break_ss_hook = { > > > .fn = kprobe_breakpoint_ss_handler, > > > }; > > > > > > -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 = { > > > - .imm = KPROBES_BRK_IMM, > > > - .fn = kprobe_breakpoint_handler, > > > -}; > > > - > > > /* > > > * Provide a blacklist of symbols identifying ranges which cannot be kprobed. > > > * This blacklist is exposed to userspace via debugfs (kprobes/blacklist). > > > > > > -- > Masami Hiramatsu (Google) <mhiramat@kernel.org>
On Thu, 1 Dec 2022 17:21:55 +0000 Mark Rutland <mark.rutland@arm.com> wrote: > On Fri, Dec 02, 2022 at 01:07:13AM +0900, Masami Hiramatsu wrote: > > On Thu, 1 Dec 2022 15:08:52 +0000 > > Mark Rutland <mark.rutland@arm.com> wrote: > > > > > On Thu, Dec 01, 2022 at 11:39:21PM +0900, Masami Hiramatsu (Google) wrote: > > > > From: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > > > > > > 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. > > > > > > IIUC this relies on BRK handling not being preemptible, which is something > > > we've repeatedly considered changing along with a bunch of other debug > > > exception handling. > > > > Interesting idea... and it also need many changes in kprobe itself. > > > > > > > > In case we do try to change that in future, it would be good to have a comment > > > somewhere to that effect. > > > > Hmm, it would fundamentally change the assumptions that kprobes relies on, > > and would require a lot of thought again. (e.g. current running kprobe is > > stored in per-cpu variable, it should be per-task. etc.) > > Ah; I had not considered that. > > Feel free to ignore the above; with the comments as below: > > Acked-by: Mark Rutland <mark.rutland@arm.com> OK, Thanks! > > Thanks, > Mark. > > > > > > > > > I think there are other ways we could synchronise against that (e.g. using RCU > > > tasks rude) if we ever do that, and this patch looks good to me. > > > > > > > > > > > Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> > > > > --- > > > > arch/arm64/kernel/probes/kprobes.c | 79 +++++++++++++++++------------------- > > > > 1 file changed, 37 insertions(+), 42 deletions(-) > > > > > > > > diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c > > > > index d2ae37f89774..ea56b22d4da8 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; > > > > } > > > > > > > > -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,45 @@ static void __kprobes kprobe_handler(struct pt_regs *regs) > > > > cur_kprobe = kprobe_running(); > > > > > > > > p = get_kprobe((kprobe_opcode_t *) addr); > > > > + if (WARN_ON_ONCE(!p)) { > > > > + /* > > > > + * Something went wrong. This must be put by kprobe, but we > > > > + * could not find corresponding kprobes. Let the kernel handle > > > > + * this error case. > > > > + */ > > > > > > Could we make this: > > > > > > /* > > > * Something went wrong. This BRK used an immediate reserved > > > * for kprobes, but we couldn't find any corresponding probe. > > > */ > > > > OK. > > > > > > > > > + return DBG_HOOK_ERROR; > > > > + } > > > > > > > > - if (p) { > > > > - if (cur_kprobe) { > > > > - if (reenter_kprobe(p, regs, kcb)) > > > > - return; > > > > - } else { > > > > - /* Probe hit */ > > > > - set_current_kprobe(p); > > > > - kcb->kprobe_status = 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 = 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. > > > > + */ > > > > > > Minor wording nit: could we replace: > > > > > > no need to single stepping. > > > > > > With: > > > > > > not need to single-step. > > > > OK, I'll update both in v2. > > > > Thank you! > > > > > > > > Thanks, > > > Mark. > > > > > > > + 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; > > > > } > > > > > > > > +static struct break_hook kprobes_break_hook = { > > > > + .imm = KPROBES_BRK_IMM, > > > > + .fn = kprobe_breakpoint_handler, > > > > +}; > > > > + > > > > static int __kprobes > > > > kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned long esr) > > > > { > > > > @@ -365,18 +372,6 @@ static struct break_hook kprobes_break_ss_hook = { > > > > .fn = kprobe_breakpoint_ss_handler, > > > > }; > > > > > > > > -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 = { > > > > - .imm = KPROBES_BRK_IMM, > > > > - .fn = kprobe_breakpoint_handler, > > > > -}; > > > > - > > > > /* > > > > * Provide a blacklist of symbols identifying ranges which cannot be kprobed. > > > > * This blacklist is exposed to userspace via debugfs (kprobes/blacklist). > > > > > > > > > > -- > > Masami Hiramatsu (Google) <mhiramat@kernel.org>
diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c index d2ae37f89774..ea56b22d4da8 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; } -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,45 @@ static void __kprobes kprobe_handler(struct pt_regs *regs) cur_kprobe = kprobe_running(); p = get_kprobe((kprobe_opcode_t *) addr); + if (WARN_ON_ONCE(!p)) { + /* + * Something went wrong. This must be put by kprobe, but we + * could not find corresponding kprobes. Let the kernel handle + * this error case. + */ + return DBG_HOOK_ERROR; + } - if (p) { - if (cur_kprobe) { - if (reenter_kprobe(p, regs, kcb)) - return; - } else { - /* Probe hit */ - set_current_kprobe(p); - kcb->kprobe_status = 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 = 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(); } - /* - * 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; } +static struct break_hook kprobes_break_hook = { + .imm = KPROBES_BRK_IMM, + .fn = kprobe_breakpoint_handler, +}; + static int __kprobes kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned long esr) { @@ -365,18 +372,6 @@ static struct break_hook kprobes_break_ss_hook = { .fn = kprobe_breakpoint_ss_handler, }; -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 = { - .imm = KPROBES_BRK_IMM, - .fn = kprobe_breakpoint_handler, -}; - /* * Provide a blacklist of symbols identifying ranges which cannot be kprobed. * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).