Skip to contentSkip to conversation

riscv: kprobes: fix placement in arch_prepare_ss_slot

PATCH Applied Ben Dooks 3 participants 3 messages Dec 16, 2024 – Jan 9, 2025 RISC-V

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

PATCH 1/1 riscv: kprobes: fix placement in arch_prepare_ss_slot

The second patch_text_nosync() in arch_prepare_ss_slot should be pointing after the first instruction, however p->ainsn.api.insn is a pointer to a probe_opcode_t which is 4 bytes... this means an instruction of length 2 moves the pointer by 8 bytes.

This means a kprobe may fail to work and recurse into the bad instruction handler (which then itself fails as the original lock still seems to be held?)

Fixes b1756750a397f36ddc857 ("riscv: kprobes: Use patch_text_nosync() for insn slots")
Cc Samuel Holland <[email protected]>
---
 arch/riscv/kernel/probes/kprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/probes/kprobes.c +1 -1
index 380a0e8cecc0..c0738d6c6498 100644
@@ -30,7 +30,7 @@ static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 	p->ainsn.api.restore = (unsigned long)p->addr + len;
 
 	patch_text_nosync(p->ainsn.api.insn, &p->opcode, len);
-	patch_text_nosync(p->ainsn.api.insn + len, &insn, GET_INSN_LENGTH(insn));
+	patch_text_nosync((void *)p->ainsn.api.insn + len, &insn, GET_INSN_LENGTH(insn));
 }
 
 static void __kprobes arch_prepare_simulate(struct kprobe *p)
Signature · 7 lines
2.37.2.352.g3c44437643


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

Hi Ben,

Quoted 21 lines · Ben Dooks

The second patch_text_nosync() in arch_prepare_ss_slot should be pointing after the first instruction, however p->ainsn.api.insn is a pointer to a probe_opcode_t which is 4 bytes... this means an instruction of length 2 moves the pointer by 8 bytes.

This means a kprobe may fail to work and recurse into the bad instruction handler (which then itself fails as the original lock still seems to be held?)

Fixes b1756750a397f36ddc857 ("riscv: kprobes: Use patch_text_nosync() for insn slots")
Cc Samuel Holland <[email protected]>
---
  arch/riscv/kernel/probes/kprobes.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/probes/kprobes.c +0 -0
index 380a0e8cecc0..c0738d6c6498 100644

arch/riscv/kernel/probes/kprobes.c static void __kprobes arch_prepare_ss_slot(struct kprobe *p) · line 36

Quoted 8 lines
arch/riscv/kernel/probes/kprobes.c +1 -1
@@ -30,7 +30,7 @@ static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
  	p->ainsn.api.restore = (unsigned long)p->addr + len;
 
  	patch_text_nosync(p->ainsn.api.insn, &p->opcode, len);
-	patch_text_nosync(p->ainsn.api.insn + len, &insn, GET_INSN_LENGTH(insn));
+	patch_text_nosync((void *)p->ainsn.api.insn + len, &insn, GET_INSN_LENGTH(insn));
  }
 
  static void __kprobes arch_prepare_simulate(struct kprobe *p)

Nam already came up with the same fix a few weeks ago: https://lore.kernel.org/linux-riscv/[email protected]/

Thanks anyway!

Alex

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#2 lore raw
In reply to Ben Dooks
This patch was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <[email protected]>:

Quoted 10 lines · On Mon, 16 Dec 2024 13:05:44 +0000 you wrote:

The second patch_text_nosync() in arch_prepare_ss_slot should be pointing after the first instruction, however p->ainsn.api.insn is a pointer to a probe_opcode_t which is 4 bytes... this means an instruction of length 2 moves the pointer by 8 bytes.

This means a kprobe may fail to work and recurse into the bad instruction handler (which then itself fails as the original lock still seems to be held?)

[...]

Here is the summary with links:
- riscv: kprobes: fix placement in arch_prepare_ss_slot

    https://git.kernel.org/riscv/c/13134cc94914

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv