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

From: Kees Cook
Date: Thu Oct 05 2017 - 20:06:17 EST


On Thu, Oct 5, 2017 at 4:58 PM, 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.

Ah-ha! Perfect, thank you! I'll give this a shot.

-Kees

--
Kees Cook
Pixel Security