Skip to content

Commit 9e68bd8

Browse files
VogtinatorPaul Walmsley
authored andcommitted
riscv: kprobes: Fix probe address validation
When adding a kprobe such as "p:probe/tcp_sendmsg _text+15392192", arch_check_kprobe would start iterating all instructions starting from _text until the probed address. Not only is this very inefficient, but literal values in there (e.g. left by function patching) are misinterpreted in a way that causes a desync. Fix this by doing it like x86: start the iteration at the closest preceding symbol instead of the given starting point. Fixes: 87f48c7 ("riscv: kprobe: Fixup kernel panic when probing an illegal position") Signed-off-by: Fabian Vogt <fvogt@suse.de> Signed-off-by: Marvin Friedrich <marvin.friedrich@suse.com> Acked-by: Guo Ren <guoren@kernel.org> Link: https://lore.kernel.org/r/6191817.lOV4Wx5bFT@fvogt-thinkpad Signed-off-by: Paul Walmsley <pjw@kernel.org>
1 parent c199745 commit 9e68bd8

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

arch/riscv/kernel/probes/kprobes.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
4949
post_kprobe_handler(p, kcb, regs);
5050
}
5151

52-
static bool __kprobes arch_check_kprobe(struct kprobe *p)
52+
static bool __kprobes arch_check_kprobe(unsigned long addr)
5353
{
54-
unsigned long tmp = (unsigned long)p->addr - p->offset;
55-
unsigned long addr = (unsigned long)p->addr;
54+
unsigned long tmp, offset;
55+
56+
/* start iterating at the closest preceding symbol */
57+
if (!kallsyms_lookup_size_offset(addr, NULL, &offset))
58+
return false;
59+
60+
tmp = addr - offset;
5661

5762
while (tmp <= addr) {
5863
if (tmp == addr)
@@ -71,7 +76,7 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
7176
if ((unsigned long)insn & 0x1)
7277
return -EILSEQ;
7378

74-
if (!arch_check_kprobe(p))
79+
if (!arch_check_kprobe((unsigned long)p->addr))
7580
return -EILSEQ;
7681

7782
/* copy instruction */

0 commit comments

Comments
 (0)