Commit e8c7264d authored by Nam Cao's avatar Nam Cao Committed by Wen Zhiwei
Browse files

riscv: kprobes: Fix incorrect address calculation

stable inclusion
from stable-v6.6.72
commit 03753bfacbc6039fce7cc7585287589b87895199
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/IBQN9L

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=03753bfacbc6039fce7cc7585287589b87895199



--------------------------------

commit 13134cc949148e1dfa540a0fe5dc73569bc62155 upstream.

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: default avatarNam Cao <namcao@linutronix.de>
Cc: stable@vger.kernel.org
Reviewed-by: default avatarAlexandre Ghiti <alexghiti@rivosinc.com>
Link: https://lore.kernel.org/r/20241119111056.2554419-1-namcao@linutronix.de


Signed-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
[rebase to v6.6]
Signed-off-by: default avatarNam Cao <namcao@linutronix.de>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarWen Zhiwei <wenzhiwei@kylinos.cn>
parent 765b596f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
	p->ainsn.api.restore = (unsigned long)p->addr + offset;

	patch_text_nosync(p->ainsn.api.insn, &p->opcode, 1);
	patch_text_nosync(p->ainsn.api.insn + offset, &insn, 1);
	patch_text_nosync((void *)p->ainsn.api.insn + offset, &insn, 1);
}

static void __kprobes arch_prepare_simulate(struct kprobe *p)