Skip to contentSkip to conversation

perf symbol: Avoid use after free

PATCH In review Ian Rogers 3 participants 4 messages Mar 28, 2023 – Mar 29, 2023 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf symbol: Avoid use after free

If demangling succeeds then sym_name is set to the demangled string that is freed. Rather than test if sym_name is empty and possibly use-after-free on the return path, expand out the alternatives.

---
 tools/perf/util/symbol-elf.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
tools/perf/util/symbol-elf.c +10 -8
index c0a2de42c51b..b7e3e492bff3 100644
@@ -577,15 +577,17 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i,
 	/* Get the associated symbol */
 	gelf_getsym(di->dynsym_data, vr->sym_idx, &sym);
 	sym_name = elf_sym__name(&sym, di->dynstr_data);
-	demangled = demangle_sym(di->dso, 0, sym_name);
-	if (demangled != NULL)
-		sym_name = demangled;
-
-	snprintf(buf, buf_sz, "%s@plt", sym_name);
-
-	free(demangled);
+	if (*sym_name == '\0')
+		return false;
 
-	return *sym_name;
+	demangled = demangle_sym(di->dso, 0, sym_name);
+	if (demangled != NULL) {
+		snprintf(buf, buf_sz, "%s@plt", demangled);
+		free(demangled);
+	} else {
+		snprintf(buf, buf_sz, "%s@plt", sym_name);
+	}
+	return true;
 }
 
 static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
Signature · 1 line
2.40.0.348.gf938b09366-goog
reviewer
#1 lore raw
Quoted 3 lines · Ian Rogers

If demangling succeeds then sym_name is set to the demangled string that is freed. Rather than test if sym_name is empty and possibly use-after-free on the return path, expand out the alternatives.

Looks the same as:

https://lore.kernel.org/linux-perf-users/[email protected]/

Quoted 36 lines
---
 tools/perf/util/symbol-elf.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
tools/perf/util/symbol-elf.c +10 -8
index c0a2de42c51b..b7e3e492bff3 100644
@@ -577,15 +577,17 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i,
 	/* Get the associated symbol */
 	gelf_getsym(di->dynsym_data, vr->sym_idx, &sym);
 	sym_name = elf_sym__name(&sym, di->dynstr_data);
-	demangled = demangle_sym(di->dso, 0, sym_name);
-	if (demangled != NULL)
-		sym_name = demangled;
-
-	snprintf(buf, buf_sz, "%s@plt", sym_name);
-
-	free(demangled);
+	if (*sym_name == '\0')
+		return false;
 
-	return *sym_name;
+	demangled = demangle_sym(di->dso, 0, sym_name);
+	if (demangled != NULL) {
+		snprintf(buf, buf_sz, "%s@plt", demangled);
+		free(demangled);
+	} else {
+		snprintf(buf, buf_sz, "%s@plt", sym_name);
+	}
+	return true;
 }
 
 static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
author reviewer
#2 lore raw
Quoted 9 lines · Adrian Hunter
Quoted 3 lines · Ian Rogers

If demangling succeeds then sym_name is set to the demangled string that is freed. Rather than test if sym_name is empty and possibly use-after-free on the return path, expand out the alternatives.

Looks the same as:

https://lore.kernel.org/linux-perf-users/[email protected]/

Sorry for missing that. In the case it returns false, this avoids an unnecessary snprintf.

Thanks,
Ian

Quoted 37 lines
Quoted 36 lines
---
 tools/perf/util/symbol-elf.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
tools/perf/util/symbol-elf.c +10 -8
index c0a2de42c51b..b7e3e492bff3 100644
@@ -577,15 +577,17 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i,
      /* Get the associated symbol */
      gelf_getsym(di->dynsym_data, vr->sym_idx, &sym);
      sym_name = elf_sym__name(&sym, di->dynstr_data);
-     demangled = demangle_sym(di->dso, 0, sym_name);
-     if (demangled != NULL)
-             sym_name = demangled;
-
-     snprintf(buf, buf_sz, "%s@plt", sym_name);
-
-     free(demangled);
+     if (*sym_name == '\0')
+             return false;
 
-     return *sym_name;
+     demangled = demangle_sym(di->dso, 0, sym_name);
+     if (demangled != NULL) {
+             snprintf(buf, buf_sz, "%s@plt", demangled);
+             free(demangled);
+     } else {
+             snprintf(buf, buf_sz, "%s@plt", sym_name);
+     }
+     return true;
 }
 
 static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
#3 lore raw

Em Tue, Mar 28, 2023 at 10:35:07PM -0700, Ian Rogers escreveu:

Quoted 7 lines · Adrian Hunter
Quoted 6 lines · Ian Rogers
Quoted 3 lines

If demangling succeeds then sym_name is set to the demangled string that is freed. Rather than test if sym_name is empty and possibly use-after-free on the return path, expand out the alternatives.

Quoted 6 lines
Quoted 3 lines

Looks the same as:

https://lore.kernel.org/linux-perf-users/[email protected]/

Sorry for missing that. In the case it returns false, this avoids an unnecessary snprintf.

I applied Adrian's fixes,

- Arnaldo

Quoted 40 lines

Thanks,
Ian

Quoted 37 lines
Quoted 36 lines
---
 tools/perf/util/symbol-elf.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
tools/perf/util/symbol-elf.c +10 -8
index c0a2de42c51b..b7e3e492bff3 100644
@@ -577,15 +577,17 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i,
      /* Get the associated symbol */
      gelf_getsym(di->dynsym_data, vr->sym_idx, &sym);
      sym_name = elf_sym__name(&sym, di->dynstr_data);
-     demangled = demangle_sym(di->dso, 0, sym_name);
-     if (demangled != NULL)
-             sym_name = demangled;
-
-     snprintf(buf, buf_sz, "%s@plt", sym_name);
-
-     free(demangled);
+     if (*sym_name == '\0')
+             return false;
 
-     return *sym_name;
+     demangled = demangle_sym(di->dso, 0, sym_name);
+     if (demangled != NULL) {
+             snprintf(buf, buf_sz, "%s@plt", demangled);
+             free(demangled);
+     } else {
+             snprintf(buf, buf_sz, "%s@plt", sym_name);
+     }
+     return true;
 }
 
 static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf,
Signature · 1 line
- Arnaldo