Re: [RFC PATCH -tip 0/5] kprobes: Abolish jprobe APIs

From: Masami Hiramatsu
Date: Fri Oct 06 2017 - 00:50:08 EST


On Thu, 5 Oct 2017 19:58:08 -0400
Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:

> On Thu, 5 Oct 2017 16:35:22 -0700
> Kees Cook <keescook@xxxxxxxxxxxx> wrote:
>
> > > As far as I can see, tcp probe, dccp probe, sctp probe and lkdtm
> > > are using jprobe to probe function. Please consider to migrate.
> >
> > I'm happy to do so, but I'm quite unfamiliar with how to do this (I
> > didn't write lkdtm's jprobe code originally). lkdtm just wants to hook
> > function entry and call it's own function before.
>
> That can be done with ftrace. That's how live kernel patching works. It
> registers a callback via register_ftrace_function(), and with fentry
> (gcc 4.6 and later on x86), you can "hijack" the function. If you don't
> modify the regs->ip, then the function you hooked to will be called.
>
> >
> > It uses struct jprobe like this:
> >
> > .jprobe = { \
> > .kp.symbol_name = _symbol, \
> > .entry = (kprobe_opcode_t *)_entry, \
> > }, \
> >
> > and defines a bunch of handlers like this for the _symbol and _entry pairs:
> >
> > "do_IRQ", jp_do_irq),
> > ...
> > "tasklet_action", jp_tasklet_action),
> >
> > where all the handlers look exactly the same (and don't care about arguments):
>
> Hell, this is really easy then!
>
> >
> > static unsigned int jp_do_irq(unsigned int irq)
> > {
> > lkdtm_handler();
> > jprobe_return();
> > return 0;
> > }
> >
> > What's the right way to migrate away from jprobe for lkdtm?
>
> Perhaps something like:
>
> #include <linux/ftrace.h>
>
> static void lkdtm_callback(unsigned long ip, unsigned long parent_ip,
> struct ftrace_ops *ops, struct pt_regs *regs)
> {
> lkdt_handler();
> }
>
>
> static struct ftrace_ops ops = {
> .func = lkdtm_callback,
> };
>
> [..]
> ftrace_set_filter(&ops, "do_IRQ", strlen("do_IRQ"), 0);
> ftrace_set_filter(&ops, "tasklet_action", strlen("tasklet_action"), 0);
> [..]
>
> /* to add the hook */
>
> register_ftrace_function(&ops);
>
> Now all functions you set the filter for will be traced.
>
> Oh you may want to check the return status of ftrace_set_filter()
> otherwise, if they all fail, you will be tracing all functions.

Steve, could you write a documentation how to use ftrace callback?
I think I should update the Documentation/kprobes.txt so that jprobe
user can easily migrate on that.

Thank you,


--
Masami Hiramatsu <mhiramat@xxxxxxxxxx>