Re: [PATCH 04/29] x86/livepatch: Validate __fentry__ location

From: Steven Rostedt
Date: Wed Feb 23 2022 - 09:16:47 EST


On Wed, 23 Feb 2022 15:05:42 +0100
Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:

> On Wed, Feb 23, 2022 at 07:41:39AM -0500, Steven Rostedt wrote:
> > On Wed, 23 Feb 2022 11:57:26 +0100
> > Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> >
> > > --- a/kernel/trace/ftrace.c
> > > +++ b/kernel/trace/ftrace.c
> > > @@ -1578,7 +1578,23 @@ unsigned long ftrace_location_range(unsi
> > > */
> > > unsigned long ftrace_location(unsigned long ip)
> > > {
> > > - return ftrace_location_range(ip, ip);
> > > + struct dyn_ftrace *rec;
> > > + unsigned long offset;
> > > + unsigned long size;
> > > +
> > > + rec = lookup_rec(ip, ip);
> > > + if (!rec) {
> > > + if (!kallsyms_lookup_size_offset(ip, &size, &offset))
> > > + goto out;
> > > +
> > > + rec = lookup_rec(ip - offset, (ip - offset) + size);
> > > + }
> > > +
> >
> > Please create a new function for this. Perhaps find_ftrace_location().
> >
> > ftrace_location() is used to see if the address given is a ftrace
> > nop or not. This change will make it always return true.
> >
>
> # git grep ftrace_location
> arch/powerpc/include/asm/livepatch.h:#define klp_get_ftrace_location klp_get_ftrace_location
> arch/powerpc/include/asm/livepatch.h:static inline unsigned long klp_get_ftrace_location(unsigned long faddr)
> arch/powerpc/include/asm/livepatch.h: return ftrace_location_range(faddr, faddr + 16);
> arch/powerpc/kernel/kprobes.c: faddr = ftrace_location_range((unsigned long)addr,
> arch/x86/kernel/kprobes/core.c: faddr = ftrace_location(addr);
> arch/x86/kernel/kprobes/core.c: * arch_check_ftrace_location(). Something went terribly wrong
> include/linux/ftrace.h:unsigned long ftrace_location(unsigned long ip);
> include/linux/ftrace.h:unsigned long ftrace_location_range(unsigned long start, unsigned long end);
> include/linux/ftrace.h:static inline unsigned long ftrace_location(unsigned long ip)
> kernel/bpf/trampoline.c:static int is_ftrace_location(void *ip)
> kernel/bpf/trampoline.c: addr = ftrace_location((long)ip);
> kernel/bpf/trampoline.c: ret = is_ftrace_location(ip);
> kernel/kprobes.c: unsigned long faddr = ftrace_location((unsigned long)addr);
> kernel/kprobes.c:static int check_ftrace_location(struct kprobe *p)
> kernel/kprobes.c: ftrace_addr = ftrace_location((unsigned long)p->addr);
> kernel/kprobes.c: ret = check_ftrace_location(p);
> kernel/livepatch/patch.c:#ifndef klp_get_ftrace_location
> kernel/livepatch/patch.c:static unsigned long klp_get_ftrace_location(unsigned long faddr)
> kernel/livepatch/patch.c: return ftrace_location(faddr);
> kernel/livepatch/patch.c: klp_get_ftrace_location((unsigned long)func->old_func);
> kernel/livepatch/patch.c: klp_get_ftrace_location((unsigned long)func->old_func);
> kernel/trace/ftrace.c: * ftrace_location_range - return the first address of a traced location
> kernel/trace/ftrace.c:unsigned long ftrace_location_range(unsigned long start, unsigned long end)
> kernel/trace/ftrace.c: * ftrace_location - return true if the ip giving is a traced location
> kernel/trace/ftrace.c:unsigned long ftrace_location(unsigned long ip)
> kernel/trace/ftrace.c: ret = ftrace_location_range((unsigned long)start,
> kernel/trace/ftrace.c: if (!ftrace_location(ip))
> kernel/trace/ftrace.c: ip = ftrace_location(ip);
> kernel/trace/ftrace.c: ip = ftrace_location(ip);
> kernel/trace/trace_kprobe.c: * Since ftrace_location_range() does inclusive range check, we need
> kernel/trace/trace_kprobe.c: return !ftrace_location_range(addr, addr + size - 1);
>
> and yet almost every caller takes the address it returns...

And they check if the returned value is 0 or not. If it is zero, it lets
them know it isn't an ftrace location.

-- Steve