Skip to contentSkip to conversation

x86/kprobes: fix JNG/JNLE emulation

PATCH Applied Nadav Amit 3 participants 3 messages Aug 14, 2022 – Aug 15, 2022

Subsystems (2)
lore
Share & tools
lore

Nadav Amit

author
#0 lore raw

PATCH 1/1 x86/kprobes: fix JNG/JNLE emulation

From: Nadav Amit <[email protected]>

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 <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Cc Andy Lutomirski <[email protected]>
Fixes 6256e668b7af ("x86/kprobes: Use int3 instead of debug trap for single-step")
---
 arch/x86/kernel/kprobes/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/kprobes/core.c +1 -1
index 74167dc5f55e..4c3c27b6aea3 100644
@@ -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));
 }
Signature · 1 line
2.25.1
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Nadav Amit

From: Nadav Amit <[email protected]>

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.

Quoted 8 lines

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

Cc Masami Hiramatsu <[email protected]>
Cc Peter Zijlstra (Intel) <[email protected]>
Cc Andy Lutomirski <[email protected]>
Fixes 6256e668b7af ("x86/kprobes: Use int3 instead of debug trap for single-step")

Urgghh..

Quoted 20 lines
---
 arch/x86/kernel/kprobes/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/kprobes/core.c +1 -1
index 74167dc5f55e..4c3c27b6aea3 100644
@@ -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));
 }
Signature · 1 line
2.25.1
merge bot Applied
#2 lore raw

[tip: perf/urgent] x86/kprobes: Fix JNG/JNLE emulation

In reply to Nadav Amit
The following commit has been merged into the perf/urgent branch of tip:

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 <[email protected]>
AuthorDate:    Sat, 13 Aug 2022 15:59:43 -07:00
Committer:     Ingo Molnar <[email protected]>
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")
---
 arch/x86/kernel/kprobes/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/kprobes/core.c +1 -1
index 74167dc..4c3c27b 100644
@@ -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));
 }