]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
src: Check ebl_openbackend result before using ebl handle.
authorMark Wielaard <mark@klomp.org>
Sat, 9 May 2020 19:05:31 +0000 (21:05 +0200)
committerMark Wielaard <mark@klomp.org>
Thu, 14 May 2020 12:30:57 +0000 (14:30 +0200)
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
src/elflint.c
src/nm.c
src/objdump.c

index 83d5860715159c146df83973d6b4a7fc34c867c4..8c72e7d1a3a82b61a468041324ab857cac1d62e7 100644 (file)
@@ -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.
index 0ef43236605d8261bb7412f631d5409130dec2d1..6ad9bc42114c15363fe2faee6f06b4a5ba316069 100644 (file)
@@ -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);
index b7c2aed6c360b66c384e04ff2e5273ed15c1ade6..f6ca3b0aff6b3e758b4176fd85dacdf846150a8f 100644 (file)
--- 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.  */
index a619674f4c5fd09b55928f3b194db4ab5fef0935..82d7bcf6a74437527335c354a4373bf469e84e29 100644 (file)
@@ -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,