mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] riscv: kprobes: Fix incorrect address calculation
@ 2024-11-19 11:10 Nam Cao
  2024-11-28 13:02 ` Alexandre Ghiti
  2025-01-09 16:16 ` patchwork-bot+linux-riscv
  0 siblings, 2 replies; 4+ messages in thread
From: Nam Cao @ 2024-11-19 11:10 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Albert Ou, Samuel Holland,
	Björn Töpel, linux-riscv, linux-kernel
  Cc: John Ogness, Nam Cao, stable

p->ainsn.api.insn is a pointer to u32, therefore arithmetic operations are
multiplied by four. This is clearly undesirable for this case.

Cast it to (void *) first before any calculation.

Below is a sample before/after. The dumped memory is two kprobe slots, the
first slot has

  - c.addiw a0, 0x1c (0x7125)
  - ebreak           (0x00100073)

and the second slot has:

  - c.addiw a0, -4   (0x7135)
  - ebreak           (0x00100073)

Before this patch:

(gdb) x/16xh 0xff20000000135000
0xff20000000135000:	0x7125	0x0000	0x0000	0x0000	0x7135	0x0010	0x0000	0x0000
0xff20000000135010:	0x0073	0x0010	0x0000	0x0000	0x0000	0x0000	0x0000	0x0000

After this patch:

(gdb) x/16xh 0xff20000000125000
0xff20000000125000:	0x7125	0x0073	0x0010	0x0000	0x7135	0x0073	0x0010	0x0000
0xff20000000125010:	0x0000	0x0000	0x0000	0x0000	0x0000	0x0000	0x0000	0x0000

Fixes: b1756750a397 ("riscv: kprobes: Use patch_text_nosync() for insn slots")
Signed-off-by: Nam Cao <namcao@linutronix.de>
Cc: stable@vger.kernel.org
---
 arch/riscv/kernel/probes/kprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
index 474a65213657..d2dacea1aedd 100644
--- a/arch/riscv/kernel/probes/kprobes.c
+++ b/arch/riscv/kernel/probes/kprobes.c
@@ -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)
-- 
2.39.5


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

* Re: [PATCH] riscv: kprobes: Fix incorrect address calculation
  2024-11-19 11:10 [PATCH] riscv: kprobes: Fix incorrect address calculation Nam Cao
@ 2024-11-28 13:02 ` Alexandre Ghiti
  2024-11-29 12:19   ` Nam Cao
  2025-01-09 16:16 ` patchwork-bot+linux-riscv
  1 sibling, 1 reply; 4+ messages in thread
From: Alexandre Ghiti @ 2024-11-28 13:02 UTC (permalink / raw)
  To: Nam Cao, Paul Walmsley, Palmer Dabbelt, Albert Ou,
	Samuel Holland, Björn Töpel, linux-riscv, linux-kernel
  Cc: John Ogness, stable

Hi Nam,

On 19/11/2024 12:10, Nam Cao wrote:
> p->ainsn.api.insn is a pointer to u32, therefore arithmetic operations are
> multiplied by four. This is clearly undesirable for this case.
>
> Cast it to (void *) first before any calculation.
>
> Below is a sample before/after. The dumped memory is two kprobe slots, the
> first slot has
>
>    - c.addiw a0, 0x1c (0x7125)
>    - ebreak           (0x00100073)
>
> and the second slot has:
>
>    - c.addiw a0, -4   (0x7135)
>    - ebreak           (0x00100073)
>
> Before this patch:
>
> (gdb) x/16xh 0xff20000000135000
> 0xff20000000135000:	0x7125	0x0000	0x0000	0x0000	0x7135	0x0010	0x0000	0x0000
> 0xff20000000135010:	0x0073	0x0010	0x0000	0x0000	0x0000	0x0000	0x0000	0x0000
>
> After this patch:
>
> (gdb) x/16xh 0xff20000000125000
> 0xff20000000125000:	0x7125	0x0073	0x0010	0x0000	0x7135	0x0073	0x0010	0x0000
> 0xff20000000125010:	0x0000	0x0000	0x0000	0x0000	0x0000	0x0000	0x0000	0x0000
>
> Fixes: b1756750a397 ("riscv: kprobes: Use patch_text_nosync() for insn slots")
> Signed-off-by: Nam Cao <namcao@linutronix.de>
> Cc: stable@vger.kernel.org
> ---
>   arch/riscv/kernel/probes/kprobes.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/riscv/kernel/probes/kprobes.c b/arch/riscv/kernel/probes/kprobes.c
> index 474a65213657..d2dacea1aedd 100644
> --- a/arch/riscv/kernel/probes/kprobes.c
> +++ b/arch/riscv/kernel/probes/kprobes.c
> @@ -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)

This looks good to me, how did you find this issue?

You can add:

Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>

Thanks,

Alex


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

* Re: [PATCH] riscv: kprobes: Fix incorrect address calculation
  2024-11-28 13:02 ` Alexandre Ghiti
@ 2024-11-29 12:19   ` Nam Cao
  0 siblings, 0 replies; 4+ messages in thread
From: Nam Cao @ 2024-11-29 12:19 UTC (permalink / raw)
  To: Alexandre Ghiti
  Cc: Paul Walmsley, Palmer Dabbelt, Albert Ou, Samuel Holland,
	Björn Töpel, linux-riscv, linux-kernel, John Ogness,
	stable

Hi Alex,

On Thu, Nov 28, 2024 at 02:02:40PM +0100, Alexandre Ghiti wrote:
> On 19/11/2024 12:10, Nam Cao wrote:
> > p->ainsn.api.insn is a pointer to u32, therefore arithmetic operations are
> > multiplied by four. This is clearly undesirable for this case.
> > 
> > Cast it to (void *) first before any calculation.
...
> This looks good to me, how did you find this issue?

I found it while working on RV monitors (Documentation/trace/rv) which
use kprobes. The monitors exploded on riscv.

Best regards,
Nam

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

* Re: [PATCH] riscv: kprobes: Fix incorrect address calculation
  2024-11-19 11:10 [PATCH] riscv: kprobes: Fix incorrect address calculation Nam Cao
  2024-11-28 13:02 ` Alexandre Ghiti
@ 2025-01-09 16:16 ` patchwork-bot+linux-riscv
  1 sibling, 0 replies; 4+ messages in thread
From: patchwork-bot+linux-riscv @ 2025-01-09 16:16 UTC (permalink / raw)
  To: Nam Cao
  Cc: linux-riscv, paul.walmsley, palmer, aou, samuel.holland, bjorn,
	linux-kernel, john.ogness, stable

Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <palmer@rivosinc.com>:

On Tue, 19 Nov 2024 12:10:56 +0100 you wrote:
> p->ainsn.api.insn is a pointer to u32, therefore arithmetic operations are
> multiplied by four. This is clearly undesirable for this case.
> 
> Cast it to (void *) first before any calculation.
> 
> Below is a sample before/after. The dumped memory is two kprobe slots, the
> first slot has
> 
> [...]

Here is the summary with links:
  - riscv: kprobes: Fix incorrect address calculation
    https://git.kernel.org/riscv/c/13134cc94914

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2025-01-09 16:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-19 11:10 [PATCH] riscv: kprobes: Fix incorrect address calculation Nam Cao
2024-11-28 13:02 ` Alexandre Ghiti
2024-11-29 12:19   ` Nam Cao
2025-01-09 16:16 ` patchwork-bot+linux-riscv

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®