Skip to contentSkip to conversation

x86/kprobes: Fix 1 byte conditional jump target

PATCH In review Nadav Amit 3 participants 3 messages Feb 8, 2023 – Feb 9, 2023 x86

lore
Share & tools
lore

Nadav Amit

author
#0 lore raw

PATCH v2 1/1 x86/kprobes: Fix 1 byte conditional jump target

From: Nadav Amit <[email protected]>

Commit 3bc753c06dd0 ("kbuild: treat char as always unsigned") broke kprobes. Setting a probe-point on 1 byte conditional jump can cause the kernel to crash, as the branch target is not sign-extended and instead zero-extended.

In fact, there is no need for any casting of immediate.value since sign extension is already done during its decoding in insn_get_immediate().

Fix by removing the casting of the 1 byte conditional jump target. Future patches can also remove the casting (and sign extension) in other cases in which immediate.value is being used.

Fixes 3bc753c06dd0 ("kbuild: treat char as always unsigned")
Cc Peter Zijlstra <[email protected]>

---

v1->v2: Removing the casting completely [Dave]

---
 arch/x86/kernel/kprobes/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/kprobes/core.c +1 -1
index b36f3c367cb2..695873c0f50b 100644
@@ -625,7 +625,7 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
 		/* 1 byte conditional jump */
 		p->ainsn.emulate_op = kprobe_emulate_jcc;
 		p->ainsn.jcc.type = opcode & 0xf;
-		p->ainsn.rel32 = *(char *)insn->immediate.bytes;
+		p->ainsn.rel32 = insn->immediate.value;
 		break;
 	case 0x0f:
 		opcode = insn->opcode.bytes[1];
Signature · 1 line
2.34.1
Acked-by on v2 1/1
#1 lore raw
Quoted 14 lines · Nadav Amit

From: Nadav Amit <[email protected]>

Commit 3bc753c06dd0 ("kbuild: treat char as always unsigned") broke kprobes. Setting a probe-point on 1 byte conditional jump can cause the kernel to crash, as the branch target is not sign-extended and instead zero-extended.

In fact, there is no need for any casting of immediate.value since sign extension is already done during its decoding in insn_get_immediate().

Fix by removing the casting of the 1 byte conditional jump target. Future patches can also remove the casting (and sign extension) in other cases in which immediate.value is being used.

Quoted 5 lines
Fixes 3bc753c06dd0 ("kbuild: treat char as always unsigned")
Cc Peter Zijlstra <[email protected]>
Acked-by on v2 1/1
#2 lore raw
In reply to Nadav Amit
Quoted 14 lines · On Wed, 8 Feb 2023 07:17:08 +0000 Nadav Amit <[email protected]> wrote:

From: Nadav Amit <[email protected]>

Commit 3bc753c06dd0 ("kbuild: treat char as always unsigned") broke kprobes. Setting a probe-point on 1 byte conditional jump can cause the kernel to crash, as the branch target is not sign-extended and instead zero-extended.

In fact, there is no need for any casting of immediate.value since sign extension is already done during its decoding in insn_get_immediate().

Fix by removing the casting of the 1 byte conditional jump target. Future patches can also remove the casting (and sign extension) in other cases in which immediate.value is being used.

Quoted 5 lines
Fixes 3bc753c06dd0 ("kbuild: treat char as always unsigned")
Cc Peter Zijlstra <[email protected]>

Thanks, looks good to me.

Quoted 24 lines

---

v1->v2: Removing the casting completely [Dave]

---
 arch/x86/kernel/kprobes/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/kprobes/core.c +1 -1
index b36f3c367cb2..695873c0f50b 100644
@@ -625,7 +625,7 @@ static int prepare_emulation(struct kprobe *p, struct insn *insn)
 		/* 1 byte conditional jump */
 		p->ainsn.emulate_op = kprobe_emulate_jcc;
 		p->ainsn.jcc.type = opcode & 0xf;
-		p->ainsn.rel32 = *(char *)insn->immediate.bytes;
+		p->ainsn.rel32 = insn->immediate.value;
 		break;
 	case 0x0f:
 		opcode = insn->opcode.bytes[1];
Signature · 1 line
2.34.1

Nadav

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>