Skip to content

Commit 3d2d2ba

Browse files
sm00thgregkh
authored andcommitted
objtool: Fix segfault in .cold detection with -ffunction-sections
[ Upstream commit 22566c1 ] Because find_symbol_by_name() traverses the same lists as read_symbols(), changing sym->name in place without copying it affects the result of find_symbol_by_name(). In the case where a ".cold" function precedes its parent in sec->symbol_list, it can result in a function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS. Fix by copying the search string instead of modifying it in place. Signed-off-by: Artem Savkov <asavkov@redhat.com> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Fixes: 1381043 ("objtool: Support GCC 8's cold subfunctions") Link: http://lkml.kernel.org/r/910abd6b5a4945130fd44f787c24e07b9e07c8da.1542736240.git.jpoimboe@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 79cd7b0 commit 3d2d2ba

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

‎tools/objtool/elf.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "elf.h"
3232
#include "warn.h"
3333

34+
#define MAX_NAME_LEN 128
35+
3436
struct section *find_section_by_name(struct elf *elf, const char *name)
3537
{
3638
struct section *sec;
@@ -298,16 +300,25 @@ static int read_symbols(struct elf *elf)
298300
/* Create parent/child links for any cold subfunctions */
299301
list_for_each_entry(sec, &elf->sections, list) {
300302
list_for_each_entry(sym, &sec->symbol_list, list) {
303+
char pname[MAX_NAME_LEN + 1];
304+
size_t pnamelen;
301305
if (sym->type != STT_FUNC)
302306
continue;
303307
sym->pfunc = sym->cfunc = sym;
304308
coldstr = strstr(sym->name, ".cold.");
305309
if (!coldstr)
306310
continue;
307311

308-
coldstr[0] = '\0';
309-
pfunc = find_symbol_by_name(elf, sym->name);
310-
coldstr[0] = '.';
312+
pnamelen = coldstr - sym->name;
313+
if (pnamelen > MAX_NAME_LEN) {
314+
WARN("%s(): parent function name exceeds maximum length of %d characters",
315+
sym->name, MAX_NAME_LEN);
316+
return -1;
317+
}
318+
319+
strncpy(pname, sym->name, pnamelen);
320+
pname[pnamelen] = '\0';
321+
pfunc = find_symbol_by_name(elf, pname);
311322

312323
if (!pfunc) {
313324
WARN("%s(): can't find parent function",

0 commit comments

Comments
 (0)