public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: David Malcolm <dmalcolm@redhat.com>, Mark Wielaard <mark@klomp.org>
Subject: [PATCH 5/7] src: Check ebl_openbackend result before using ebl handle.
Date: Sun, 10 May 2020 21:53:38 +0200	[thread overview]
Message-ID: <20200510195339.37191-6-mark@klomp.org> (raw)
In-Reply-To: <20200510195339.37191-1-mark@klomp.org>

GCC10 -fanalyzer plus -flto sees that ebl_openbackend can fail and
return NULL. Most of the time we will get a dummy ebl, but in case
of out of memory or corrupt ELF header it might return NULL. Make
sure that we report a (fatal) error in that case in all tools.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog |  7 +++++++
 src/elflint.c |  9 ++++++++-
 src/nm.c      | 16 +++++++++-------
 src/objdump.c |  3 +++
 4 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 83d58607..8c72e7d1 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,10 @@
+2020-05-09  Mark Wielaard  <mark@klomp.org>
+
+	* elflint.c (process_elf_file): Error out if ebl_openbackend fails.
+	* objdump.c (handle_elf): Likewise.
+	* nm.c (handle_elf): Likewise. Move full name string construction
+	forward, so it can be used in the error message.
+
 2020-04-17  Mark Wielaard  <mark@klomp.org>
 
 	* readelf.c (print_debug): Check .gnu.debuglto_ prefix.
diff --git a/src/elflint.c b/src/elflint.c
index 0ef43236..6ad9bc42 100644
--- a/src/elflint.c
+++ b/src/elflint.c
@@ -4775,7 +4775,14 @@ process_elf_file (Elf *elf, const char *prefix, const char *suffix,
   ebl = ebl_openbackend (elf);
   /* If there is no appropriate backend library we cannot test
      architecture and OS specific features.  Any encountered extension
-     is an error.  */
+     is an error.  Often we'll get a "dummy" ebl, except if something
+     really bad happen, like a totally corrupted ELF file or out of
+     memory situation.  */
+  if (ebl == NULL)
+    {
+      ERROR (gettext ("cannot create backend for ELF file\n"));
+      return;
+    }
 
   /* Go straight by the gABI, check all the parts in turn.  */
   check_elf_header (ebl, ehdr, size);
diff --git a/src/nm.c b/src/nm.c
index b7c2aed6..f6ca3b0a 100644
--- a/src/nm.c
+++ b/src/nm.c
@@ -1510,8 +1510,17 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
   GElf_Ehdr *ehdr;
   Ebl *ebl;
 
+  /* Create the full name of the file.  */
+  if (prefix != NULL)
+    cp = mempcpy (cp, prefix, prefix_len);
+  cp = mempcpy (cp, fname, fname_len);
+  if (suffix != NULL)
+    memcpy (cp - 1, suffix, suffix_len + 1);
+
   /* Get the backend for this object file type.  */
   ebl = ebl_openbackend (elf);
+  if (ebl == NULL)
+    INTERNAL_ERROR (fullname);
 
   /* We need the ELF header in a few places.  */
   ehdr = gelf_getehdr (elf, &ehdr_mem);
@@ -1530,13 +1539,6 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
       goto out;
     }
 
-  /* Create the full name of the file.  */
-  if (prefix != NULL)
-    cp = mempcpy (cp, prefix, prefix_len);
-  cp = mempcpy (cp, fname, fname_len);
-  if (suffix != NULL)
-    memcpy (cp - 1, suffix, suffix_len + 1);
-
   /* Find the symbol table.
 
      XXX Can there be more than one?  Do we print all?  Currently we do.  */
diff --git a/src/objdump.c b/src/objdump.c
index a619674f..82d7bcf6 100644
--- a/src/objdump.c
+++ b/src/objdump.c
@@ -755,6 +755,9 @@ handle_elf (Elf *elf, const char *prefix, const char *fname,
 
   /* Get the backend for this object file type.  */
   Ebl *ebl = ebl_openbackend (elf);
+  if (ebl == NULL)
+    error (EXIT_FAILURE, 0,
+	   gettext ("cannot create backend for elf file"));
 
   printf ("%s: elf%d-%s\n\n",
 	  fname, gelf_getclass (elf) == ELFCLASS32 ? 32 : 64,
-- 
2.20.1


  parent reply	other threads:[~2020-05-10 19:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-10 19:53 Some more GCC10 -fanalyzer inspired patches Mark Wielaard
2020-05-10 19:53 ` [PATCH 1/7] libdwfl: Cleanup user_core resources on failure in dwfl_core_file_report Mark Wielaard
2020-05-10 19:53 ` [PATCH 2/7] tests: Make sure to not call memcmp with NULL arguments Mark Wielaard
2020-05-10 19:53 ` [PATCH 3/7] libelf: Check __gelf_getehdr_rdlock call doesn't fail in elf_getdata Mark Wielaard
2020-05-10 19:53 ` [PATCH 4/7] libelf: Check for NULL shdr in elf_strptr Mark Wielaard
2020-05-10 19:53 ` Mark Wielaard [this message]
2020-05-10 19:53 ` [PATCH 6/7] libdwfl: Return failure from dwfl_standard_find_debuginfo for NULL module Mark Wielaard
2020-05-10 19:53 ` [PATCH 7/7] libcpu: Free unused new bitfield on error in i386_parse.y new_bitfield Mark Wielaard
2020-05-14 12:44 ` Some more GCC10 -fanalyzer inspired patches 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=20200510195339.37191-6-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=dmalcolm@redhat.com \
    --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).