All of lore.kernel.org
 help / color / mirror / Atom feed
* [BUGFIX PATCH -tip] x86/kprobes: Fix to call previous kprobe's fault handler
@ 2021-05-24  3:25 Masami Hiramatsu
  2021-05-24  5:30 ` Masami Hiramatsu
  0 siblings, 1 reply; 3+ messages in thread
From: Masami Hiramatsu @ 2021-05-24  3:25 UTC (permalink / raw)
  To: Ingo Molnar, Steven Rostedt
  Cc: Naveen N . Rao, Ananth N Mavinakayanahalli, linux-kernel, mhiramat, x86

Fix to call the previous kprobe's fault handler when a page fault
occurred in reentered kprobes.
This may happen if kprobes handler calls a function which can cause
a page fault (e.g. access user page) and another kprobe probes that
instruction.
Without the 2nd kprobe, the 1st kprobe can handle the page fault,
but with the 2nd kprobe, the 1st one can not handle it.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
 arch/x86/kernel/kprobes/core.c |   19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 7c4d0736a998..ac2514f1e195 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -1098,12 +1098,21 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
 		 */
 		regs->flags |= kcb->kprobe_old_flags;
 
-		if (kcb->kprobe_status == KPROBE_REENTER)
-			restore_previous_kprobe(kcb);
-		else
+		if (kcb->kprobe_status != KPROBE_REENTER) {
 			reset_current_kprobe();
-	} else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
-		   kcb->kprobe_status == KPROBE_HIT_SSDONE) {
+			return 0;
+		}
+		restore_previous_kprobe(kcb);
+		/*
+		 * If reentered kprobes caused a page fault, it must be
+		 * handled by the previous kprobe too. But we don't bother
+		 * checking KPROBE_HIT_SS again because kprobes can not
+		 * probe another kprobe's single stepping buffer.
+		 */
+	}
+
+	if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
+	    kcb->kprobe_status == KPROBE_HIT_SSDONE) {
 		/*
 		 * We increment the nmissed count for accounting,
 		 * we can also use npre/npostfault count for accounting


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [BUGFIX PATCH -tip] x86/kprobes: Fix to call previous kprobe's fault handler
  2021-05-24  3:25 [BUGFIX PATCH -tip] x86/kprobes: Fix to call previous kprobe's fault handler Masami Hiramatsu
@ 2021-05-24  5:30 ` Masami Hiramatsu
  2021-05-25  7:24   ` Peter Zijlstra
  0 siblings, 1 reply; 3+ messages in thread
From: Masami Hiramatsu @ 2021-05-24  5:30 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Ingo Molnar, Steven Rostedt, Naveen N . Rao,
	Ananth N Mavinakayanahalli, linux-kernel, x86

On Mon, 24 May 2021 12:25:36 +0900
Masami Hiramatsu <mhiramat@kernel.org> wrote:

> Fix to call the previous kprobe's fault handler when a page fault
> occurred in reentered kprobes.
> This may happen if kprobes handler calls a function which can cause
> a page fault (e.g. access user page) and another kprobe probes that
> instruction.
> Without the 2nd kprobe, the 1st kprobe can handle the page fault,
> but with the 2nd kprobe, the 1st one can not handle it.
> 
> Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>

BTW, this is a kind of a rare case bug, but exists from very early day.

E.g. reenter case has been decoupled in the kprobe_fault_handler()
by commit b4026513b88e ("[PATCH] kprobes: fix broken fault handling 
for i386"), but even without that commit, kprobes never call previous
kprobe fault handler.

So it seems that this bug has been there from the commit 417c8da6511b
 ("[PATCH] kprobes: Temporary disarming of reentrant probe for i386")
which introduces the "reenter" kprobes.

Fixes: 417c8da6511b ("[PATCH] kprobes: Temporary disarming of reentrant probe for i386")
Cc: stable@vger.kernel.org

Note that this patch itself can be cleanly applied from commit
2bbda764d720 ("kprobes/x86: Do not disable preempt on int3 path")

and before commit 6381c24cd6d5 ("kprobes/x86: Fix page-fault handling logic")
this needs the above commit.

BTW, there is another discussion to remove user fault_handler from
kprobes. In that case, this patch is not needed anymore.

Thank you,

> ---
>  arch/x86/kernel/kprobes/core.c |   19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)
> 
> diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
> index 7c4d0736a998..ac2514f1e195 100644
> --- a/arch/x86/kernel/kprobes/core.c
> +++ b/arch/x86/kernel/kprobes/core.c
> @@ -1098,12 +1098,21 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
>  		 */
>  		regs->flags |= kcb->kprobe_old_flags;
>  
> -		if (kcb->kprobe_status == KPROBE_REENTER)
> -			restore_previous_kprobe(kcb);
> -		else
> +		if (kcb->kprobe_status != KPROBE_REENTER) {
>  			reset_current_kprobe();
> -	} else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
> -		   kcb->kprobe_status == KPROBE_HIT_SSDONE) {
> +			return 0;
> +		}
> +		restore_previous_kprobe(kcb);
> +		/*
> +		 * If reentered kprobes caused a page fault, it must be
> +		 * handled by the previous kprobe too. But we don't bother
> +		 * checking KPROBE_HIT_SS again because kprobes can not
> +		 * probe another kprobe's single stepping buffer.
> +		 */
> +	}
> +
> +	if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
> +	    kcb->kprobe_status == KPROBE_HIT_SSDONE) {
>  		/*
>  		 * We increment the nmissed count for accounting,
>  		 * we can also use npre/npostfault count for accounting
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [BUGFIX PATCH -tip] x86/kprobes: Fix to call previous kprobe's fault handler
  2021-05-24  5:30 ` Masami Hiramatsu
@ 2021-05-25  7:24   ` Peter Zijlstra
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Zijlstra @ 2021-05-25  7:24 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Ingo Molnar, Steven Rostedt, Naveen N . Rao,
	Ananth N Mavinakayanahalli, linux-kernel, x86

On Mon, May 24, 2021 at 02:30:45PM +0900, Masami Hiramatsu wrote:
> BTW, there is another discussion to remove user fault_handler from
> kprobes. In that case, this patch is not needed anymore.

Thanks for reminding me; I'm indeed still sitting on those patches, let
me post them again, they've been in my tree for months now without a
single robot complaint, I've just been to busy with other things to
pursue them :/


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-25  7:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-24  3:25 [BUGFIX PATCH -tip] x86/kprobes: Fix to call previous kprobe's fault handler Masami Hiramatsu
2021-05-24  5:30 ` Masami Hiramatsu
2021-05-25  7:24   ` Peter Zijlstra

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.