Re: [PATCH] kprobes/x86: Return correct length in __copy_instruction()

From: Masami Hiramatsu
Date: Mon Mar 09 2015 - 22:59:46 EST


(2015/03/09 20:19), Eugene Shatokhin wrote:
> On x86-64, __copy_instruction() always returns 0 (error) if the
> instruction uses %rip-relative addressing. This is because
> kernel_insn_init() is called the second time for 'insn' instance
> in such cases and sets all its fields to 0.
>
> Because of this, trying to place a Kprobe on such instruction will
> fail, register_kprobe() will return -EINVAL.

Oops, good catch!

Acked-by: Masami Hiramatsu <masami.hiramatsu.pt@xxxxxxxxxxx>

Thank you!

>
> This patch fixes the problem.
>
> Signed-off-by: Eugene Shatokhin <eugene.shatokhin@xxxxxxxxxx>
> ---
> arch/x86/kernel/kprobes/core.c | 9 ++++++---
> 1 file changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
> index 4e3d5a9..03189d8 100644
> --- a/arch/x86/kernel/kprobes/core.c
> +++ b/arch/x86/kernel/kprobes/core.c
> @@ -354,6 +354,7 @@ int __copy_instruction(u8 *dest, u8 *src)
> {
> struct insn insn;
> kprobe_opcode_t buf[MAX_INSN_SIZE];
> + int length;
> unsigned long recovered_insn =
> recover_probed_instruction(buf, (unsigned long)src);
>
> @@ -361,16 +362,18 @@ int __copy_instruction(u8 *dest, u8 *src)
> return 0;
> kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
> insn_get_length(&insn);
> + length = insn.length;
> +
> /* Another subsystem puts a breakpoint, failed to recover */
> if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
> return 0;
> - memcpy(dest, insn.kaddr, insn.length);
> + memcpy(dest, insn.kaddr, length);
>
> #ifdef CONFIG_X86_64
> if (insn_rip_relative(&insn)) {
> s64 newdisp;
> u8 *disp;
> - kernel_insn_init(&insn, dest, insn.length);
> + kernel_insn_init(&insn, dest, length);
> insn_get_displacement(&insn);
> /*
> * The copied instruction uses the %rip-relative addressing
> @@ -394,7 +397,7 @@ int __copy_instruction(u8 *dest, u8 *src)
> *(s32 *) disp = (s32) newdisp;
> }
> #endif
> - return insn.length;
> + return length;
> }
>
> static int arch_copy_kprobe(struct kprobe *p)
>


--
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt@xxxxxxxxxxx


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/