Re: [PATCH v2 07/17] kallsyms: strip ThinLTO hashes from static functions

From: Sami Tolvanen
Date: Thu Mar 18 2021 - 17:42:42 EST


On Thu, Mar 18, 2021 at 12:00 PM Nick Desaulniers
<ndesaulniers@xxxxxxxxxx> wrote:
>
> On Thu, Mar 18, 2021 at 10:11 AM Sami Tolvanen <samitolvanen@xxxxxxxxxx> wrote:
> >
> > With CONFIG_CFI_CLANG and ThinLTO, Clang appends a hash to the names
> > of all static functions not marked __used. This can break userspace
> > tools that don't expect the function name to change, so strip out the
> > hash from the output.
> >
> > Suggested-by: Jack Pham <jackp@xxxxxxxxxxxxxx>
> > Signed-off-by: Sami Tolvanen <samitolvanen@xxxxxxxxxx>
> > Reviewed-by: Kees Cook <keescook@xxxxxxxxxxxx>
> > ---
> > kernel/kallsyms.c | 54 ++++++++++++++++++++++++++++++++++++++++++-----
> > 1 file changed, 49 insertions(+), 5 deletions(-)
> >
> > diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
> > index 8043a90aa50e..17d3a704bafa 100644
> > --- a/kernel/kallsyms.c
> > +++ b/kernel/kallsyms.c
> > @@ -161,6 +161,26 @@ static unsigned long kallsyms_sym_address(int idx)
> > return kallsyms_relative_base - 1 - kallsyms_offsets[idx];
> > }
> >
> > +#if defined(CONFIG_CFI_CLANG) && defined(CONFIG_LTO_CLANG_THIN)
> > +/*
> > + * LLVM appends a hash to static function names when ThinLTO and CFI are
> > + * both enabled, which causes confusion and potentially breaks user space
>
> Might be nice to add an example, something along the lines of:
> ie. foo() becomes foo$asfdasdfasdfasdf()

Agreed, I'll update the comment in v3.

>
> > + * tools, so we will strip the postfix from expanded symbol names.
>
> s/postfix/suffix/ ?

Ack.

>
> > + */
> > +static inline char *cleanup_symbol_name(char *s)
> > +{
> > + char *res = NULL;
> > +
> > + res = strrchr(s, '$');
> > + if (res)
> > + *res = '\0';
> > +
> > + return res;
> > +}
> > +#else
> > +static inline char *cleanup_symbol_name(char *s) { return NULL; }
> > +#endif
>
> Might be nicer to return a `bool` and have the larger definition
> `return res != NULL`). Not sure what a caller would do with `res` if
> it was not `NULL`?

Sure, I'll change this to bool.

Sami