[PATCH] nm: Handle corrupt symbol name table.
Mark Wielaard
mark@klomp.org
Sun Jun 7 14:51:24 GMT 2020
We try to sort symbol by name (when neither -n nor -p are given).
This could crash if the symbol name table was corrupt. Use elf_strptr
to get the symbol name and use the empty string in case a name couldn't
be found.
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
src/ChangeLog | 8 ++++++++
src/nm.c | 15 +++++++++------
2 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/src/ChangeLog b/src/ChangeLog
index 4684d332..512d7b54 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,11 @@
+2020-06-07 Mark Wielaard <mark@klomp.org>
+
+ * nm.c (sort_by_name_strtab): Replace by...
+ (sort_by_name_elf): New static Elf pointer and ...
+ (sort_by_name_ndx): New static size_t index.
+ (sort_by_name): Use elf_strptr to get symbol string.
+ (show_symbols): Set sort_by_name_elf and sort_by_name_ndx.
+
2020-06-06 Mark Wielaard <mark@klomp.org>
* nm.c (show_symbols_sysv): Skip no name and STT_FILE symbols.
diff --git a/src/nm.c b/src/nm.c
index 8302a98c..a6334743 100644
--- a/src/nm.c
+++ b/src/nm.c
@@ -1,5 +1,5 @@
/* Print symbol information from ELF file in human-readable form.
- Copyright (C) 2000-2008, 2009, 2011, 2012, 2014, 2015 Red Hat, Inc.
+ Copyright (C) 2000-2008, 2009, 2011, 2012, 2014, 2015, 2020 Red Hat, Inc.
This file is part of elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 2000.
@@ -1153,7 +1153,8 @@ sort_by_address (const void *p1, const void *p2)
return reverse_sort ? -result : result;
}
-static Elf_Data *sort_by_name_strtab;
+static Elf *sort_by_name_elf;
+static size_t sort_by_name_ndx;
static int
sort_by_name (const void *p1, const void *p2)
@@ -1161,8 +1162,10 @@ sort_by_name (const void *p1, const void *p2)
GElf_SymX *s1 = (GElf_SymX *) p1;
GElf_SymX *s2 = (GElf_SymX *) p2;
- const char *n1 = sort_by_name_strtab->d_buf + s1->sym.st_name;
- const char *n2 = sort_by_name_strtab->d_buf + s2->sym.st_name;
+ const char *n1 = elf_strptr (sort_by_name_elf, sort_by_name_ndx,
+ s1->sym.st_name) ?: "";
+ const char *n2 = elf_strptr (sort_by_name_elf, sort_by_name_ndx,
+ s2->sym.st_name) ?: "";
int result = strcmp (n1, n2);
@@ -1475,8 +1478,8 @@ show_symbols (int fd, Ebl *ebl, GElf_Ehdr *ehdr,
/* Sort the entries according to the users wishes. */
if (sort == sort_name)
{
- sort_by_name_strtab = elf_getdata (elf_getscn (ebl->elf, shdr->sh_link),
- NULL);
+ sort_by_name_elf = ebl->elf;
+ sort_by_name_ndx = shdr->sh_link;
qsort (sym_mem, nentries, sizeof (GElf_SymX), sort_by_name);
}
else if (sort == sort_numeric)
--
2.18.4
More information about the Elfutils-devel
mailing list