[PATCH 1/3] perf symbols: Fix use-after-free in get_plt_got_name()

From: Adrian Hunter
Date: Thu Mar 16 2023 - 15:42:20 EST


Fix use-after-free in get_plt_got_name().

Discovered using EXTRA_CFLAGS="-fsanitize=undefined -fsanitize=address".

Reported-by: kernel test robot <yujie.liu@xxxxxxxxx>
Link: https://lore.kernel.org/oe-lkp/202303061424.6ad43294-yujie.liu@xxxxxxxxx
Fixes: ce4c8e7966f3 ("perf symbols: Get symbols for .plt.got for x86-64")
Signed-off-by: Adrian Hunter <adrian.hunter@xxxxxxxxx>
---
tools/perf/util/symbol-elf.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index c0a2de42c51b..7ef5f6d7d415 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -557,6 +557,7 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i,
const char *sym_name;
char *demangled;
GElf_Sym sym;
+ bool result;
u32 disp;

if (!di->sorted)
@@ -583,9 +584,11 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i,

snprintf(buf, buf_sz, "%s@plt", sym_name);

+ result = *sym_name;
+
free(demangled);

- return *sym_name;
+ return result;
}

static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
--
2.34.1