Skip to content

Commit

Permalink
kallsyms: strip ThinLTO hashes from static functions
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Sami Tolvanen <[email protected]>
Reviewed-by: Kees Cook <[email protected]>
Tested-by: Nathan Chancellor <[email protected]>
Signed-off-by: Kees Cook <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
  • Loading branch information
samitolvanen authored and kees committed Apr 8, 2021
1 parent 0a5b412 commit 8b8e6b5
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions kernel/kallsyms.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ 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, i.e. foo() becomes foo$707af9a22804d33c81801f27dcfe489b.
* This causes confusion and potentially breaks user space tools, so we
* strip the suffix from expanded symbol names.
*/
static inline bool cleanup_symbol_name(char *s)
{
char *res;

res = strrchr(s, '$');
if (res)
*res = '\0';

return res != NULL;
}
#else
static inline bool cleanup_symbol_name(char *s) { return false; }
#endif

/* Lookup the address for this symbol. Returns 0 if not found. */
unsigned long kallsyms_lookup_name(const char *name)
{
Expand All @@ -173,6 +194,9 @@ unsigned long kallsyms_lookup_name(const char *name)

if (strcmp(namebuf, name) == 0)
return kallsyms_sym_address(i);

if (cleanup_symbol_name(namebuf) && strcmp(namebuf, name) == 0)
return kallsyms_sym_address(i);
}
return module_kallsyms_lookup_name(name);
}
Expand Down Expand Up @@ -303,7 +327,9 @@ const char *kallsyms_lookup(unsigned long addr,
namebuf, KSYM_NAME_LEN);
if (modname)
*modname = NULL;
return namebuf;

ret = namebuf;
goto found;
}

/* See if it's in a module or a BPF JITed image. */
Expand All @@ -316,11 +342,16 @@ const char *kallsyms_lookup(unsigned long addr,
if (!ret)
ret = ftrace_mod_address_lookup(addr, symbolsize,
offset, modname, namebuf);

found:
cleanup_symbol_name(namebuf);
return ret;
}

int lookup_symbol_name(unsigned long addr, char *symname)
{
int res;

symname[0] = '\0';
symname[KSYM_NAME_LEN - 1] = '\0';

Expand All @@ -331,15 +362,23 @@ int lookup_symbol_name(unsigned long addr, char *symname)
/* Grab name */
kallsyms_expand_symbol(get_symbol_offset(pos),
symname, KSYM_NAME_LEN);
return 0;
goto found;
}
/* See if it's in a module. */
return lookup_module_symbol_name(addr, symname);
res = lookup_module_symbol_name(addr, symname);
if (res)
return res;

found:
cleanup_symbol_name(symname);
return 0;
}

int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
unsigned long *offset, char *modname, char *name)
{
int res;

name[0] = '\0';
name[KSYM_NAME_LEN - 1] = '\0';

Expand All @@ -351,10 +390,16 @@ int lookup_symbol_attrs(unsigned long addr, unsigned long *size,
kallsyms_expand_symbol(get_symbol_offset(pos),
name, KSYM_NAME_LEN);
modname[0] = '\0';
return 0;
goto found;
}
/* See if it's in a module. */
return lookup_module_symbol_attrs(addr, size, offset, modname, name);
res = lookup_module_symbol_attrs(addr, size, offset, modname, name);
if (res)
return res;

found:
cleanup_symbol_name(name);
return 0;
}

/* Look up a kernel symbol and return it in a text buffer. */
Expand Down

0 comments on commit 8b8e6b5

Please sign in to comment.