public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] nm: Handle corrupt symbol name table.
Date: Sun,  7 Jun 2020 16:51:24 +0200	[thread overview]
Message-ID: <20200607145124.32214-1-mark@klomp.org> (raw)

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


             reply	other threads:[~2020-06-07 14:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-07 14:51 Mark Wielaard [this message]
2020-06-10 10:18 ` Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200607145124.32214-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).