linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86/kprobes: fix JNG/JNLE emulation
@ 2022-08-13 22:59 Nadav Amit
  2022-08-15  7:29 ` Peter Zijlstra
  2022-08-15  8:48 ` [tip: perf/urgent] x86/kprobes: Fix " tip-bot2 for Nadav Amit
  0 siblings, 2 replies; 3+ messages in thread
From: Nadav Amit @ 2022-08-13 22:59 UTC (permalink / raw)
  To: Thomas Gleixner
  Cc: Ingo Molnar, Borislav Petkov, Dave Hansen, x86, linux-kernel,
	Nadav Amit, Masami Hiramatsu, Peter Zijlstra, Andy Lutomirski,
	stable

From: Nadav Amit <namit@vmware.com>

When kprobes emulates JNG/JNLE instructions on x86 it uses the wrong
condition. For JNG (opcode: 0F 8E), according to Intel SDM, the jump is
performed if (ZF == 1 or SF != OF). However the kernel emulation
currently uses 'and' instead of 'or'.

As a result, setting a kprobe on JNG/JNLE might cause the kernel to
behave incorrectly whenever the kprobe is hit.

Fix by changing the 'and' to 'or'.

Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: stable@vger.kernel.org
Fixes: 6256e668b7af ("x86/kprobes: Use int3 instead of debug trap for single-step")
Signed-off-by: Nadav Amit <namit@vmware.com>
---
 arch/x86/kernel/kprobes/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 74167dc5f55e..4c3c27b6aea3 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -505,7 +505,7 @@ static void kprobe_emulate_jcc(struct kprobe *p, struct pt_regs *regs)
 		match = ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^
 			((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT);
 		if (p->ainsn.jcc.type >= 0xe)
-			match = match && (regs->flags & X86_EFLAGS_ZF);
+			match = match || (regs->flags & X86_EFLAGS_ZF);
 	}
 	__kprobe_emulate_jmp(p, regs, (match && !invert) || (!match && invert));
 }
-- 
2.25.1


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

* Re: [PATCH] x86/kprobes: fix JNG/JNLE emulation
  2022-08-13 22:59 [PATCH] x86/kprobes: fix JNG/JNLE emulation Nadav Amit
@ 2022-08-15  7:29 ` Peter Zijlstra
  2022-08-15  8:48 ` [tip: perf/urgent] x86/kprobes: Fix " tip-bot2 for Nadav Amit
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Zijlstra @ 2022-08-15  7:29 UTC (permalink / raw)
  To: Nadav Amit
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
	linux-kernel, Nadav Amit, Masami Hiramatsu, Andy Lutomirski,
	stable

On Sat, Aug 13, 2022 at 03:59:43PM -0700, Nadav Amit wrote:
> From: Nadav Amit <namit@vmware.com>
> 
> When kprobes emulates JNG/JNLE instructions on x86 it uses the wrong
> condition. For JNG (opcode: 0F 8E), according to Intel SDM, the jump is
> performed if (ZF == 1 or SF != OF). However the kernel emulation
> currently uses 'and' instead of 'or'.
> 
> As a result, setting a kprobe on JNG/JNLE might cause the kernel to
> behave incorrectly whenever the kprobe is hit.
> 
> Fix by changing the 'and' to 'or'.
> 
> Cc: Masami Hiramatsu <mhiramat@kernel.org>
> Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
> Cc: Andy Lutomirski <luto@kernel.org>
> Cc: stable@vger.kernel.org
> Fixes: 6256e668b7af ("x86/kprobes: Use int3 instead of debug trap for single-step")
> Signed-off-by: Nadav Amit <namit@vmware.com>

Urgghh..

Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>

> ---
>  arch/x86/kernel/kprobes/core.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
> index 74167dc5f55e..4c3c27b6aea3 100644
> --- a/arch/x86/kernel/kprobes/core.c
> +++ b/arch/x86/kernel/kprobes/core.c
> @@ -505,7 +505,7 @@ static void kprobe_emulate_jcc(struct kprobe *p, struct pt_regs *regs)
>  		match = ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^
>  			((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT);
>  		if (p->ainsn.jcc.type >= 0xe)
> -			match = match && (regs->flags & X86_EFLAGS_ZF);
> +			match = match || (regs->flags & X86_EFLAGS_ZF);
>  	}
>  	__kprobe_emulate_jmp(p, regs, (match && !invert) || (!match && invert));
>  }
> -- 
> 2.25.1
> 

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

* [tip: perf/urgent] x86/kprobes: Fix JNG/JNLE emulation
  2022-08-13 22:59 [PATCH] x86/kprobes: fix JNG/JNLE emulation Nadav Amit
  2022-08-15  7:29 ` Peter Zijlstra
@ 2022-08-15  8:48 ` tip-bot2 for Nadav Amit
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot2 for Nadav Amit @ 2022-08-15  8:48 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Nadav Amit, Ingo Molnar, stable, x86, linux-kernel

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     8924779df820c53875abaeb10c648e9cb75b46d4
Gitweb:        https://git.kernel.org/tip/8924779df820c53875abaeb10c648e9cb75b46d4
Author:        Nadav Amit <namit@vmware.com>
AuthorDate:    Sat, 13 Aug 2022 15:59:43 -07:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Sun, 14 Aug 2022 11:27:17 +02:00

x86/kprobes: Fix JNG/JNLE emulation

When kprobes emulates JNG/JNLE instructions on x86 it uses the wrong
condition. For JNG (opcode: 0F 8E), according to Intel SDM, the jump is
performed if (ZF == 1 or SF != OF). However the kernel emulation
currently uses 'and' instead of 'or'.

As a result, setting a kprobe on JNG/JNLE might cause the kernel to
behave incorrectly whenever the kprobe is hit.

Fix by changing the 'and' to 'or'.

Fixes: 6256e668b7af ("x86/kprobes: Use int3 instead of debug trap for single-step")
Signed-off-by: Nadav Amit <namit@vmware.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/20220813225943.143767-1-namit@vmware.com
---
 arch/x86/kernel/kprobes/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 74167dc..4c3c27b 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -505,7 +505,7 @@ static void kprobe_emulate_jcc(struct kprobe *p, struct pt_regs *regs)
 		match = ((regs->flags & X86_EFLAGS_SF) >> X86_EFLAGS_SF_BIT) ^
 			((regs->flags & X86_EFLAGS_OF) >> X86_EFLAGS_OF_BIT);
 		if (p->ainsn.jcc.type >= 0xe)
-			match = match && (regs->flags & X86_EFLAGS_ZF);
+			match = match || (regs->flags & X86_EFLAGS_ZF);
 	}
 	__kprobe_emulate_jmp(p, regs, (match && !invert) || (!match && invert));
 }

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

end of thread, other threads:[~2022-08-15  8:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-13 22:59 [PATCH] x86/kprobes: fix JNG/JNLE emulation Nadav Amit
2022-08-15  7:29 ` Peter Zijlstra
2022-08-15  8:48 ` [tip: perf/urgent] x86/kprobes: Fix " tip-bot2 for Nadav Amit

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).