lkml.org 
[lkml]   [2022]   [Feb]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
SubjectRe: [PATCH 04/29] x86/livepatch: Validate __fentry__ location
On Fri, Feb 18, 2022 at 01:08:31PM -0800, Josh Poimboeuf wrote:
> On Fri, Feb 18, 2022 at 05:49:06PM +0100, Peter Zijlstra wrote:
> > Currently livepatch assumes __fentry__ lives at func+0, which is most
> > likely untrue with IBT on. Override the weak klp_get_ftrace_location()
> > function with an arch specific version that's IBT aware.
> >
> > Also make the weak fallback verify the location is an actual ftrace
> > location as a sanity check.
> >
> > Suggested-by: Miroslav Benes <mbenes@suse.cz>
> > Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> > ---
> > arch/x86/include/asm/livepatch.h | 9 +++++++++
> > kernel/livepatch/patch.c | 2 +-
> > 2 files changed, 10 insertions(+), 1 deletion(-)
> >
> > --- a/arch/x86/include/asm/livepatch.h
> > +++ b/arch/x86/include/asm/livepatch.h
> > @@ -17,4 +17,13 @@ static inline void klp_arch_set_pc(struc
> > ftrace_instruction_pointer_set(fregs, ip);
> > }
> >
> > +#define klp_get_ftrace_location klp_get_ftrace_location
> > +static inline unsigned long klp_get_ftrace_location(unsigned long faddr)
> > +{
> > + unsigned long addr = ftrace_location(faddr);
> > + if (!addr && IS_ENABLED(CONFIG_X86_IBT))
> > + addr = ftrace_location(faddr + 4);
> > + return addr;
>
> I'm kind of surprised this logic doesn't exist in ftrace itself. Is
> livepatch really the only user that needs to find the fentry for a given
> function?
>
> I had to do a double take for the ftrace_location() semantics, as I
> originally assumed that's what it did, based on its name and signature.
>
> Instead it apparently functions like a bool but returns its argument on
> success.
>
> Though the function comment tells a different story:
>
> /**
> * ftrace_location - return true if the ip giving is a traced location
>
> So it's all kinds of confusing...

Yes.. so yesterday, when making function-graph tracing not explode, I
ran into a similar issue. Steve suggested something along the lines of
.... this.

(modified from his actual suggestion to also cover this case)

Let me go try this...

--- 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(ip, &size, &offset, NULL, NULL))
+ goto out;
+
+ rec = lookup_rec(ip - offset, (ip - offset) + size);
+ }
+
+ if (rec)
+ return rec->ip;
+
+out:
+ return 0;
}

/**
@@ -5110,11 +5126,16 @@ int register_ftrace_direct(unsigned long
struct ftrace_func_entry *entry;
struct ftrace_hash *free_hash = NULL;
struct dyn_ftrace *rec;
- int ret = -EBUSY;
+ int ret = -ENODEV;

mutex_lock(&direct_mutex);

+ ip = ftrace_location(ip);
+ if (!ip)
+ goto out_unlock;
+
/* See if there's a direct function at @ip already */
+ ret = -EBUSY;
if (ftrace_find_rec_direct(ip))
goto out_unlock;

@@ -5222,6 +5243,10 @@ int unregister_ftrace_direct(unsigned lo

mutex_lock(&direct_mutex);

+ ip = ftrace_location(ip);
+ if (!ip)
+ goto out_unlock;
+
entry = find_direct_entry(&ip, NULL);
if (!entry)
goto out_unlock;
\
 
 \ /
  Last update: 2022-02-23 11:11    [W:0.091 / U:3.304 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site