]> www.infradead.org Git - users/jedix/linux-maple.git/commit
riscv: kprobes: Fix incorrect address calculation
authorNam Cao <namcao@linutronix.de>
Tue, 19 Nov 2024 11:10:56 +0000 (12:10 +0100)
committerPalmer Dabbelt <palmer@rivosinc.com>
Wed, 8 Jan 2025 18:39:39 +0000 (10:39 -0800)
commit13134cc949148e1dfa540a0fe5dc73569bc62155
treefe3ebd2c2ac507a9fa9f103e1d6b4445455bf958
parent6a97f4118ac07cfdc316433f385dbdc12af5025e
riscv: kprobes: Fix incorrect address calculation

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
Reviewed-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20241119111056.2554419-1-namcao@linutronix.de
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
arch/riscv/kernel/probes/kprobes.c