]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
Robustify patches from Fedora
authorRoland McGrath <roland@redhat.com>
Fri, 23 Jan 2009 08:48:38 +0000 (00:48 -0800)
committerRoland McGrath <roland@redhat.com>
Fri, 23 Jan 2009 08:48:38 +0000 (00:48 -0800)
32 files changed:
libelf/ChangeLog
libelf/elf32_getphdr.c
libelf/elf32_getshdr.c
libelf/elf32_newphdr.c
libelf/elf32_updatefile.c
libelf/elf_begin.c
libelf/elf_getarsym.c
libelf/elf_getshstrndx.c
libelf/elf_newscn.c
libelf/gelf_getdyn.c
libelf/gelf_getlib.c
libelf/gelf_getmove.c
libelf/gelf_getrel.c
libelf/gelf_getrela.c
libelf/gelf_getsym.c
libelf/gelf_getsyminfo.c
libelf/gelf_getsymshndx.c
libelf/gelf_getversym.c
libelf/gelf_update_dyn.c
libelf/gelf_update_lib.c
libelf/gelf_update_move.c
libelf/gelf_update_rel.c
libelf/gelf_update_rela.c
libelf/gelf_update_sym.c
libelf/gelf_update_syminfo.c
libelf/gelf_update_symshndx.c
libelf/gelf_update_versym.c
libelf/libelfP.h
src/ChangeLog
src/elflint.c
src/readelf.c
src/strip.c

index 9578f8bde9093fd740d9b28d96177bac226a8b2b..d850dceefb06a07e67d362778e44924a2a7029a0 100644 (file)
        If section content hasn't been read yet, do it before looking for the
        block size.  If no section data present, infer size of section header.
 
+2005-05-17  Jakub Jelinek  <jakub@redhat.com>
+
+       * elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header
+       table fits into object's bounds.
+       * elf_getshstrndx.c (elf_getshstrndx): Add elf->start_offset to
+       elf->map_address.  Check if first section header fits into object's
+       bounds.
+       * elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)):
+       Check if section header table fits into object's bounds.
+       * elf_begin.c (get_shnum): Ensure section headers fits into
+       object's bounds.
+       (file_read_elf): Make sure scncnt is small enough to allocate both
+       ElfXX_Shdr and Elf_Scn array.  Make sure section and program header
+       tables fit into object's bounds.  Avoid memory leak on failure.
+
+2005-05-14  Jakub Jelinek  <jakub@redhat.com>
+
+       * libelfP.h (INVALID_NDX): Define.
+       * gelf_getdyn.c (gelf_getdyn): Use it.  Remove ndx < 0 test if any.
+       * gelf_getlib.c (gelf_getlib): Likewise.
+       * gelf_getmove.c (gelf_getmove): Likewise.
+       * gelf_getrel.c (gelf_getrel): Likewise.
+       * gelf_getrela.c (gelf_getrela): Likewise.
+       * gelf_getsym.c (gelf_getsym): Likewise.
+       * gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
+       * gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
+       * gelf_getversym.c (gelf_getversym): Likewise.
+       * gelf_update_dyn.c (gelf_update_dyn): Likewise.
+       * gelf_update_lib.c (gelf_update_lib): Likewise.
+       * gelf_update_move.c (gelf_update_move): Likewise.
+       * gelf_update_rel.c (gelf_update_rel): Likewise.
+       * gelf_update_rela.c (gelf_update_rela): Likewise.
+       * gelf_update_sym.c (gelf_update_sym): Likewise.
+       * gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
+       * gelf_update_symshndx.c (gelf_update_symshndx): Likewise.
+       * gelf_update_versym.c (gelf_update_versym): Likewise.
+       * elf_newscn.c (elf_newscn): Check for overflow.
+       * elf32_updatefile.c (__elfw2(LIBELFBITS,updatemmap)): Likewise.
+       (__elfw2(LIBELFBITS,updatefile)): Likewise.
+       * elf_begin.c (file_read_elf): Likewise.
+       * elf32_newphdr.c (elfw2(LIBELFBITS,newphdr)): Likewise.
+       * elf_getarsym.c (elf_getarsym): Likewise.
+       * elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Likewise.
 2005-05-11  Ulrich Drepper  <drepper@redhat.com>
 
        * elf.h: Update again.
index c32c2827a2eee453607627df321ab51f015c0388..2a83dd5402f4d1c75e55667531cd4f435f9d971d 100644 (file)
@@ -105,6 +105,16 @@ __elfw2(LIBELFBITS,getphdr_wrlock) (elf)
 
       if (elf->map_address != NULL)
        {
+         /* First see whether the information in the ELF header is
+            valid and it does not ask for too much.  */
+         if (unlikely (ehdr->e_phoff >= elf->maximum_size)
+             || unlikely (ehdr->e_phoff + size > elf->maximum_size))
+           {
+             /* Something is wrong.  */
+             __libelf_seterrno (ELF_E_INVALID_PHDR);
+             goto out;
+           }
+
          /* All the data is already mapped.  Use it.  */
          void *file_phdr = ((char *) elf->map_address
                             + elf->start_offset + ehdr->e_phoff);
index 91f5b3a15783085e3a652d444cc5c3d978003ef7..5b206de9eb228b7f3a20161470ead97edea761a4 100644 (file)
@@ -1,5 +1,5 @@
 /* Return section header.
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2007 Red Hat, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2005, 2007, 2008 Red Hat, Inc.
    This file is part of Red Hat elfutils.
    Written by Ulrich Drepper <drepper@redhat.com>, 1998.
 
@@ -81,7 +81,8 @@ load_shdr_wrlock (Elf_Scn *scn)
     goto out;
 
   size_t shnum;
-  if (__elf_getshnum_rdlock (elf, &shnum) != 0)
+  if (__elf_getshnum_rdlock (elf, &shnum) != 0
+      || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
     goto out;
   size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
 
@@ -98,6 +99,16 @@ load_shdr_wrlock (Elf_Scn *scn)
 
   if (elf->map_address != NULL)
     {
+      /* First see whether the information in the ELF header is
+        valid and it does not ask for too much.  */
+      if (unlikely (ehdr->e_shoff >= elf->maximum_size)
+         || unlikely (ehdr->e_shoff + size > elf->maximum_size))
+       {
+         /* Something is wrong.  */
+         __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
+         goto free_and_out;
+       }
+
       ElfW2(LIBELFBITS,Shdr) *notcvt;
 
       /* All the data is already mapped.  If we could use it
index d1b16088dbdafff083debe83c1cdf084c427b3a6..589322294f0619eed0774c407c46e6fd3051eb9e 100644 (file)
@@ -124,6 +124,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
   else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
           || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
     {
+      if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
+       {
+         result = NULL;
+         goto out;
+       }
+
       /* Allocate a new program header with the appropriate number of
         elements.  */
       result = (ElfW2(LIBELFBITS,Phdr) *)
index e88f4a45e71c9ed384de6a897f524e6a787748ff..d49bf4efd617e06774a02a17a1f66e7f7b40b303 100644 (file)
@@ -212,6 +212,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum)
   /* Write all the sections.  Well, only those which are modified.  */
   if (shnum > 0)
     {
+      if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
+       return 1;
+
       Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
       Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
       char *const shdr_start = ((char *) elf->map_address + elf->start_offset
@@ -592,6 +595,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf, int change_bo, size_t shnum)
   /* Write all the sections.  Well, only those which are modified.  */
   if (shnum > 0)
     {
+      if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
+                                       + sizeof (ElfW2(LIBELFBITS,Shdr)))))
+       return 1;
+
       off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
 #if EV_NUM != 2
       xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
index 04670a4554296dafd64f4db6e41cdf44f27b6ed1..61bc0e05095f2d011f1e4f0df0fafa121458d4f8 100644 (file)
@@ -165,7 +165,8 @@ get_shnum (void *map_address, unsigned char *e_ident, int fildes, off_t offset,
 
       if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
        {
-         if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
+         if (unlikely (ehdr.e32->e_shoff >= maxsize)
+             || unlikely (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize))
            /* Cannot read the first section header.  */
            return 0;
 
@@ -213,7 +214,8 @@ get_shnum (void *map_address, unsigned char *e_ident, int fildes, off_t offset,
 
       if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
        {
-         if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
+         if (unlikely (ehdr.e64->e_shoff >= maxsize)
+             || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
            /* Cannot read the first section header.  */
            return 0;
 
@@ -285,6 +287,15 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
     /* Could not determine the number of sections.  */
     return NULL;
 
+  /* Check for too many sections.  */
+  if (e_ident[EI_CLASS] == ELFCLASS32)
+    {
+      if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
+       return NULL;
+    }
+  else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
+    return NULL;
+
   /* We can now allocate the memory.  */
   Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
                           ELF_K_ELF, scncnt * sizeof (Elf_Scn));
@@ -318,13 +329,31 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
        {
          /* We can use the mmapped memory.  */
          elf->state.elf32.ehdr = ehdr;
+
+         if (unlikely (ehdr->e_shoff >= maxsize)
+             || unlikely (ehdr->e_shoff
+                          + scncnt * sizeof (Elf32_Shdr) > maxsize))
+           {
+           free_and_out:
+             free (elf);
+             __libelf_seterrno (ELF_E_INVALID_FILE);
+             return NULL;
+           }
          elf->state.elf32.shdr
            = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
+
          if (ehdr->e_phnum > 0)
+           {
            /* Assign a value only if there really is a program
               header.  Otherwise the value remains NULL.  */
+             if (unlikely (ehdr->e_phoff >= maxsize)
+                 || unlikely (ehdr->e_phoff
+                              + ehdr->e_phnum
+                              * sizeof (Elf32_Phdr) > maxsize))
+               goto free_and_out;
            elf->state.elf32.phdr
              = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
+           }
 
          for (size_t cnt = 0; cnt < scncnt; ++cnt)
            {
@@ -406,13 +435,26 @@ file_read_elf (int fildes, void *map_address, unsigned char *e_ident,
        {
          /* We can use the mmapped memory.  */
          elf->state.elf64.ehdr = ehdr;
+
+         if (unlikely (ehdr->e_shoff >= maxsize)
+             || unlikely (ehdr->e_shoff
+                          + scncnt * sizeof (Elf32_Shdr) > maxsize))
+           goto free_and_out;
          elf->state.elf64.shdr
            = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
+
          if (ehdr->e_phnum > 0)
+           {
            /* Assign a value only if there really is a program
               header.  Otherwise the value remains NULL.  */
+             if (unlikely (ehdr->e_phoff >= maxsize)
+                 || unlikely (ehdr->e_phoff
+                              + ehdr->e_phnum
+                              * sizeof (Elf32_Phdr) > maxsize))
+               goto free_and_out;
            elf->state.elf64.phdr
              = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
+           }
 
          for (size_t cnt = 0; cnt < scncnt; ++cnt)
            {
index e6ecaadc724b9a8da1b507b83c30b3d5002b828d..9ff7f295e787f3a97f5bf0ab4de8769974340087 100644 (file)
@@ -179,6 +179,9 @@ elf_getarsym (elf, ptr)
       size_t index_size = atol (tmpbuf);
 
       if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
+#if SIZE_MAX <= 4294967295U
+         || n >= SIZE_MAX / sizeof (Elf_Arsym)
+#endif
          || n * sizeof (uint32_t) > index_size)
        {
          /* This index table cannot be right since it does not fit into
index 52516a8e0355d2f6c8620670d05271784ea782d9..5339ad7163613a619ff8d39fedf99364ca2dfbb4 100644 (file)
@@ -125,10 +125,25 @@ elf_getshstrndx (elf, dst)
              if (elf->map_address != NULL
                  && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
                  && (ALLOW_UNALIGNED
-                     || (((size_t) ((char *) elf->map_address + offset))
+                     || (((size_t) ((char *) elf->map_address
+                          + elf->start_offset + offset))
                          & (__alignof__ (Elf32_Shdr) - 1)) == 0))
+               {
+                 /* First see whether the information in the ELF header is
+                    valid and it does not ask for too much.  */
+                 if (unlikely (offset + sizeof (Elf32_Shdr)
+                               > elf->maximum_size))
+                   {
+                     /* Something is wrong.  */
+                     __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
+                     result = -1;
+                     goto out;
+                   }
+
                /* We can directly access the memory.  */
-               num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
+                 num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
+                                        + offset))->sh_link;
+               }
              else
                {
                  /* We avoid reading in all the section headers.  Just read
@@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
              if (elf->map_address != NULL
                  && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
                  && (ALLOW_UNALIGNED
-                     || (((size_t) ((char *) elf->map_address + offset))
+                     || (((size_t) ((char *) elf->map_address
+                          + elf->start_offset + offset))
                          & (__alignof__ (Elf64_Shdr) - 1)) == 0))
+               {
+                 /* First see whether the information in the ELF header is
+                    valid and it does not ask for too much.  */
+                 if (unlikely (offset + sizeof (Elf64_Shdr)
+                               > elf->maximum_size))
+                   {
+                     /* Something is wrong.  */
+                     __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
+                     result = -1;
+                     goto out;
+                   }
+
                /* We can directly access the memory.  */
-               num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
+                 num = ((Elf64_Shdr *) (elf->map_address
+                        + elf->start_offset + offset))->sh_link;
+               }
              else
                {
                  /* We avoid reading in all the section headers.  Just read
index aefab3345e3ac1621153a82af116c5c83516c552..8acbe19020b506596b6129f86f018289d29a80dc 100644 (file)
@@ -104,10 +104,18 @@ elf_newscn (elf)
   else
     {
       /* We must allocate a new element.  */
-      Elf_ScnList *newp;
+      Elf_ScnList *newp = NULL;
 
       assert (elf->state.elf.scnincr > 0);
 
+      if (
+#if SIZE_MAX <= 4294967295U
+         likely (elf->state.elf.scnincr
+                 < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
+#else
+         1
+#endif
+         )
       newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
                                     + ((elf->state.elf.scnincr *= 2)
                                        * sizeof (Elf_Scn)), 1);
index 82f8e3285205dfb60b0108e3e05daedbb7fdb3b3..bdf6476262a7f3ede137833cedae7fb71a54e4f8 100644 (file)
@@ -93,7 +93,8 @@ gelf_getdyn (data, ndx, dst)
         table entries has to be adopted.  The user better has provided
         a buffer where we can store the information.  While copying the
         data we are converting the format.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Dyn)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
 
       /* The data is already in the correct form.  Just make sure the
         index is OK.  */
-      if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, GElf_Dyn)
+         || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index aa91a73bdf3b9e0d5be3b1b5787d32a3f3f54593..eba1a3e9dcc2e6f936a3ee6b5a77695bd795aea1 100644 (file)
@@ -86,7 +86,8 @@ gelf_getlib (data, ndx, dst)
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
   GElf_Lib *result = NULL;
-  if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Lib)
+      || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
     __libelf_seterrno (ELF_E_INVALID_INDEX);
   else
     {
index 6e769484a051da5abdeb6f9272d64da615aa54bd..05f61b95c95bea5e382d5208b77301cb764cfa80 100644 (file)
@@ -83,7 +83,8 @@ gelf_getmove (data, ndx, dst)
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Move)
+      || unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       goto out;
index f3775bd9de940b7b9a1317cb68562225c761366b..c809337903dc6996403585ed74b716f98c775753 100644 (file)
@@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
   if (data_scn == NULL)
     return NULL;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return NULL;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
     {
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
@@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
   if (scn->elf->class == ELFCLASS32)
     {
       /* We have to convert the data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rel)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          result = NULL;
@@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
     {
       /* Simply copy the data after we made sure we are actually getting
         correct data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rel)
+         || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          result = NULL;
index 0275c375f172f6b16c35d6b7a030885864536045..5d945bc67cf7ebe3aa2e2ddbe6a0cc7a4004d963 100644 (file)
@@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
   if (data_scn == NULL)
     return NULL;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return NULL;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
     {
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
@@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
   if (scn->elf->class == ELFCLASS32)
     {
       /* We have to convert the data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rela)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          result = NULL;
@@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
     {
       /* Simply copy the data after we made sure we are actually getting
         correct data.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rela)
+         || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          result = NULL;
index 162061f7ddd6e682498db1f9287855798650ad97..1ec6321d62ad310b3ab37223be551346ee607f8f 100644 (file)
@@ -90,7 +90,8 @@ gelf_getsym (data, ndx, dst)
         table entries has to be adopted.  The user better has provided
         a buffer where we can store the information.  While copying the
         data we are converting the format.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
 
       /* The data is already in the correct form.  Just make sure the
         index is OK.  */
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
+      if (INVALID_NDX (ndx, GElf_Sym)
+         || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index 2c07526af97cccebe765f88439dc705c6fd80a61..f561cd451a1999a8c78107fc244e0140094f175b 100644 (file)
@@ -84,7 +84,8 @@ gelf_getsyminfo (data, ndx, dst)
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Syminfo)
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       goto out;
index a9cfc181e188aff2427c5c1e97718f20ab0b0a4b..33481c992bbb9ca6b1e2eadd00467488c985d701 100644 (file)
@@ -90,7 +90,9 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
      section index table.  */
   if (likely (shndxdata_scn != NULL))
     {
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Word)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Word)
+                      > shndxdata_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -110,7 +112,8 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
         table entries has to be adopted.  The user better has provided
         a buffer where we can store the information.  While copying the
         data we are converting the format.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, ndx, dst, dstshndx)
 
       /* The data is already in the correct form.  Just make sure the
         index is OK.  */
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
+      if (INVALID_NDX (ndx, GElf_Sym)
+         || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index 397b7edbf64483b919796b14ec28a1aa7b261ee7..7097ca52cf02ba99fcc6c0439a4d2abd6597bd33 100644 (file)
@@ -92,7 +92,8 @@ gelf_getversym (data, ndx, dst)
 
   /* The data is already in the correct form.  Just make sure the
      index is OK.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
+  if (INVALID_NDX (ndx, GElf_Versym)
+      || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       result = NULL;
index 10bfb6abeaef99c2851583fefd5a0781ee9e9524..38d1076867e7081f7e3232bcc1f360a9047d08e9 100644 (file)
@@ -71,12 +71,6 @@ gelf_update_dyn (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_DYN))
     {
       /* The type of the data better should match.  */
@@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Dyn)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Dyn)
+         || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index 9571016fbb95e13180b82c3ae901270de085138d..375d2381290737cdb9e63f490fe6c41286c0ad55 100644 (file)
@@ -68,12 +68,6 @@ gelf_update_lib (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
   if (unlikely (data_scn->d.d_type != ELF_T_LIB))
     {
@@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
 
   /* Check whether we have to resize the data buffer.  */
   int result = 0;
-  if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
+  if (INVALID_NDX (ndx, Elf64_Lib)
+      || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
     __libelf_seterrno (ELF_E_INVALID_INDEX);
   else
     {
index fd67be10f1c5d7696c08e7649dc1949c8ca639d6..faadb0ba9025b01f8e761ab573d6a3e4683e2672 100644 (file)
@@ -75,7 +75,7 @@ gelf_update_move (data, ndx, src)
   assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
 
   /* Check whether we have to resize the data buffer.  */
-  if (unlikely (ndx < 0)
+  if (INVALID_NDX (ndx, GElf_Move)
       || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
index 049b71a15bdf0629b1214f1bcfcbb5ee82875e08..1129185a0c422b12b1dcef42fdb2414978f7ab47 100644 (file)
@@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
   if (dst == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
     {
       /* The type of the data better should match.  */
@@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rel)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx, GElf_Rel *src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rel)
+         || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index dc99c6810bb46e235cf8c2bf987b966afb5cc2bf..b5c496569099f8c227582750b9abffa620cd419e 100644 (file)
@@ -68,12 +68,6 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
   if (dst == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
     {
       /* The type of the data better should match.  */
@@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Rela)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx, GElf_Rela *src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Rela)
+         || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index fff45fdb9024450a3dc95f3bf98cabd4fa2fe225..16a6736264e1ea6f0f35bc95d8a9d0becdf3ae3c 100644 (file)
@@ -72,12 +72,6 @@ gelf_update_sym (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_SYM))
     {
       /* The type of the data better should match.  */
@@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Sym)
+         || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index a0e176dc9d5633e57f1e727ace50a8c10a1b6222..659fa4f04439b4faf804f70b3b9846d93330b1bc 100644 (file)
@@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
   if (data == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
     {
       /* The type of the data better should match.  */
@@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
   rwlock_wrlock (scn->elf->lock);
 
   /* Check whether we have to resize the data buffer.  */
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
+  if (INVALID_NDX (ndx, GElf_Syminfo)
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
       goto out;
index 2d393c1fa0cb836dab5fc87b7047c50da1880143..e7d6fad71d15a1ea7a9fd61104e8ab35072ea4cb 100644 (file)
@@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx)
   if (symdata == NULL)
     return 0;
 
-  if (unlikely (ndx < 0))
-    {
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
-      return 0;
-    }
-
   if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
     {
       /* The type of the data better should match.  */
@@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx)
        }
 
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf32_Sym)
+         || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
@@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata, ndx, src, srcshndx)
   else
     {
       /* Check whether we have to resize the data buffer.  */
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
+      if (INVALID_NDX (ndx, Elf64_Sym)
+         || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
        {
          __libelf_seterrno (ELF_E_INVALID_INDEX);
          goto out;
index 905c8f7820821faa306899ced6e6f3b2d67d71cc..b69ec75b6f3bd05a49aa9a9d4d7101ae5e49901e 100644 (file)
@@ -75,7 +75,7 @@ gelf_update_versym (data, ndx, src)
   assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
 
   /* Check whether we have to resize the data buffer.  */
-  if (unlikely (ndx < 0)
+  if (INVALID_NDX (ndx, GElf_Versym)
       || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
     {
       __libelf_seterrno (ELF_E_INVALID_INDEX);
index e8100397d3bfee8ff3f381990b375b44f02bc305..54fcb16c884010926c4be80dcb8caa4174b8d14f 100644 (file)
@@ -611,4 +611,13 @@ extern uint32_t __libelf_crc32 (uint32_t crc, unsigned char *buf, size_t len)
 /* Align offset to 4 bytes as needed for note name and descriptor data.  */
 #define NOTE_ALIGN(n)  (((n) + 3) & -4U)
 
+/* Convenience macro.  Assumes int NDX and TYPE with size at least
+   2 bytes.  */
+#if SIZE_MAX > 4294967295U
+# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
+#else
+# define INVALID_NDX(ndx, type) \
+  unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
+#endif
+
 #endif  /* libelfP.h */
index b14d58bcc02c5207540e72030e8fd5cad5481579..8e9a2aac7ae6d80a1ac709feaeda3ecd0b60695a 100644 (file)
        object symbols or symbols with unknown type.
        (check_rel): Likewise.
 
+2005-06-09  Roland McGrath  <roland@redhat.com>
+
+       * readelf.c (handle_dynamic, handle_symtab): Check for bogus sh_link.
+       (handle_verneed, handle_verdef, handle_versym, handle_hash): Likewise.
+       (handle_scngrp): Check for bogus sh_info.
+
+       * strip.c (handle_elf): Check for bogus values in sh_link, sh_info,
+       st_shndx, e_shstrndx, and SHT_GROUP or SHT_SYMTAB_SHNDX data.
+       Don't use assert on input values, instead bail with "illformed" error.
+
 2005-06-08  Roland McGrath  <roland@redhat.com>
 
        * readelf.c (print_ops): Add consts.
 
        * readelf.c (dwarf_tag_string): Add new tags.
 
+2005-05-17  Jakub Jelinek  <jakub@redhat.com>
+
+       * elflint.c (check_hash): Don't check entries beyond end of section.
+       (check_note): Don't crash if gelf_rawchunk fails.
+       (section_name): Return <invalid> if gelf_getshdr returns NULL.
+
+2005-05-14  Jakub Jelinek  <jakub@redhat.com>
+
+       * elflint.c (section_name): Return "<invalid>" instead of
+       crashing on invalid section name.
+       (check_symtab, is_rel_dyn, check_rela, check_rel, check_dynamic,
+       check_symtab_shndx, check_hash, check_versym): Robustify.
+
 2005-05-08  Roland McGrath  <roland@redhat.com>
 
        * strip.c (handle_elf): Don't translate hash and versym data formats,
index bc4219b3e11640d9a75b568c59e99c50a97423d9..81350b155b608879e39292dfab3c967bdfdf27bb 100644 (file)
@@ -131,6 +131,9 @@ static uint32_t shstrndx;
 /* Array to count references in section groups.  */
 static int *scnref;
 
+/* Number of sections.  */
+static unsigned int shnum;
+
 
 int
 main (int argc, char *argv[])
@@ -320,10 +323,19 @@ section_name (Ebl *ebl, int idx)
 {
   GElf_Shdr shdr_mem;
   GElf_Shdr *shdr;
+  const char *ret;
+
+  if ((unsigned int) idx > shnum)
+    return "<invalid>";
 
   shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
+  if (shdr == NULL)
+    return "<invalid>";
 
-  return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
+  ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
+  if (ret == NULL)
+    return "<invalid>";
+  return ret;
 }
 
 
@@ -345,10 +357,6 @@ static const int valid_e_machine[] =
   (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
 
 
-/* Number of sections.  */
-static unsigned int shnum;
-
-
 static void
 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
 {
@@ -613,7 +621,8 @@ section [%2d] '%s': symbol table cannot have more than one extended index sectio
          }
       }
 
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
+  if (shdr->sh_entsize != sh_entsize)
     ERROR (gettext ("\
 section [%2u] '%s': entry size is does not match ElfXX_Sym\n"),
           idx, section_name (ebl, idx));
@@ -651,7 +660,7 @@ section [%2d] '%s': XINDEX for zeroth entry not zero\n"),
               xndxscnidx, section_name (ebl, xndxscnidx));
     }
 
-  for (size_t cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
+  for (size_t cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
     {
       sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
       if (sym == NULL)
@@ -671,7 +680,8 @@ section [%2d] '%s': symbol %zu: invalid name value\n"),
       else
        {
          name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
-         assert (name != NULL);
+         assert (name != NULL
+                 || strshdr->sh_type != SHT_STRTAB);
        }
 
       if (sym->st_shndx == SHN_XINDEX)
@@ -1001,9 +1011,11 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *ehdr, int idx, const GElf_Shdr *shdr,
     {
       GElf_Shdr rcshdr_mem;
       const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
-      assert (rcshdr != NULL);
 
-      if (rcshdr->sh_type == SHT_DYNAMIC)
+      if (rcshdr == NULL)
+       break;
+
+      if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
        {
          /* Found the dynamic section.  Look through it.  */
          Elf_Data *d = elf_getdata (scn, NULL);
@@ -1013,7 +1025,9 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *ehdr, int idx, const GElf_Shdr *shdr,
            {
              GElf_Dyn dyn_mem;
              GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
-             assert (dyn != NULL);
+
+             if (dyn == NULL)
+               break;
 
              if (dyn->d_tag == DT_RELCOUNT)
                {
@@ -1027,7 +1041,9 @@ section [%2d] '%s': DT_RELCOUNT used for this RELA section\n"),
                      /* Does the number specified number of relative
                         relocations exceed the total number of
                         relocations?  */
-                     if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
+                     if (shdr->sh_entsize != 0
+                         && dyn->d_un.d_val > (shdr->sh_size
+                                               / shdr->sh_entsize))
                        ERROR (gettext ("\
 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
                               idx, section_name (ebl, idx),
@@ -1187,7 +1203,8 @@ section [%2d] '%s': no relocations for merge-able sections possible\n"),
        }
     }
 
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
+  size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
+  if (shdr->sh_entsize != sh_entsize)
     ERROR (gettext (reltype == ELF_T_RELA ? "\
 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
@@ -1410,7 +1427,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, GElf_Shdr *shdr, int idx)
   Elf_Data *symdata = elf_getdata (symscn, NULL);
   enum load_state state = state_undecided;
 
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
     {
       GElf_Rela rela_mem;
       GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
@@ -1460,7 +1478,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GElf_Shdr *shdr, int idx)
   Elf_Data *symdata = elf_getdata (symscn, NULL);
   enum load_state state = state_undecided;
 
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
     {
       GElf_Rel rel_mem;
       GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
@@ -1563,7 +1582,8 @@ section [%2d] '%s': referenced as string table for section [%2d] '%s' but type i
           shdr->sh_link, section_name (ebl, shdr->sh_link),
           idx, section_name (ebl, idx));
 
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
+  if (shdr->sh_entsize != sh_entsize)
     ERROR (gettext ("\
 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
           idx, section_name (ebl, idx));
@@ -1573,7 +1593,7 @@ section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
           idx, section_name (ebl, idx));
 
   bool non_null_warned = false;
-  for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
+  for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
     {
       GElf_Dyn dyn_mem;
       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
@@ -1854,6 +1874,8 @@ section [%2d] '%s': entry size does not match Elf32_Word\n"),
           idx, section_name (ebl, idx));
 
   if (symshdr != NULL
+      && shdr->sh_entsize
+      && symshdr->sh_entsize
       && (shdr->sh_size / shdr->sh_entsize
          < symshdr->sh_size / symshdr->sh_entsize))
     ERROR (gettext ("\
@@ -1880,6 +1902,12 @@ section [%2d] '%s': extended section index in section [%2zu] '%s' refers to same
     }
 
   Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
+  if (data == NULL)
+    {
+      ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
+            idx, section_name (ebl, idx));
+      return;
+    }
 
   if (*((Elf32_Word *) data->d_buf) != 0)
     ERROR (gettext ("symbol 0 should have zero extended section index\n"));
@@ -1922,7 +1950,7 @@ section [%2d] '%s': hash table section is too small (is %ld, expected %ld)\n"),
 
   size_t maxidx = nchain;
 
-  if (symshdr != NULL)
+  if (symshdr != NULL && symshdr->sh_entsize != 0)
     {
       size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
 
@@ -1933,18 +1961,28 @@ section [%2d] '%s': hash table section is too small (is %ld, expected %ld)\n"),
       maxidx = symsize;
     }
 
+  Elf32_Word *buf = (Elf32_Word *) data->d_buf;
+  Elf32_Word *end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
   size_t cnt;
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
+    {
+      if (buf + cnt >= end)
+       break;
+      else if (buf[cnt] >= maxidx)
       ERROR (gettext ("\
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
             idx, section_name (ebl, idx), cnt - 2);
+    }
 
   for (; cnt < 2 + nbucket + nchain; ++cnt)
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
+    {
+      if (buf + cnt >= end)
+       break;
+      else if (buf[cnt] >= maxidx)
       ERROR (gettext ("\
 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
             idx, section_name (ebl, idx), cnt - 2 - nbucket);
+    }
 }
 
 
@@ -1974,18 +2012,28 @@ section [%2d] '%s': hash table section is too small (is %ld, expected %ld)\n"),
       maxidx = symsize;
     }
 
+  Elf64_Xword *buf = (Elf64_Xword *) data->d_buf;
+  Elf64_Xword *end = (Elf64_Xword *) ((char *) data->d_buf + shdr->sh_size);
   size_t cnt;
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
+    {
+      if (buf + cnt >= end)
+       break;
+      else if (buf[cnt] >= maxidx)
       ERROR (gettext ("\
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
             idx, section_name (ebl, idx), cnt - 2);
+    }
 
   for (; cnt < 2 + nbucket + nchain; ++cnt)
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
+    {
+      if (buf + cnt >= end)
+       break;
+      else if (buf[cnt] >= maxidx)
       ERROR (gettext ("\
 section [%2d] '%s': hash chain reference %" PRIu64 " out of bounds\n"),
-            idx, section_name (ebl, idx), (uint64_t) (cnt - 2 - nbucket));
+              idx, section_name (ebl, idx), (uint64_t) cnt - 2 - nbucket);
+    }
 }
 
 
@@ -2010,7 +2058,7 @@ section [%2d] '%s': bitmask size not power of 2: %u\n"),
   if (shdr->sh_size < (4 + bitmask_words + nbuckets) * sizeof (Elf32_Word))
     {
       ERROR (gettext ("\
-section [%2d] '%s': hash table section is too small (is %ld, expected at least%ld)\n"),
+section [%2d] '%s': hash table section is too small (is %ld, expected at least %ld)\n"),
             idx, section_name (ebl, idx), (long int) shdr->sh_size,
             (long int) ((4 + bitmask_words + nbuckets) * sizeof (Elf32_Word)));
       return;
@@ -2682,8 +2730,9 @@ section [%2d] '%s' refers in sh_link to section [%2d] '%s' which is no dynamic s
 
   /* The number of elements in the version symbol table must be the
      same as the number of symbols.  */
-  if (shdr->sh_size / shdr->sh_entsize
-      != symshdr->sh_size / symshdr->sh_entsize)
+  if (shdr->sh_entsize && symshdr->sh_entsize
+      && (shdr->sh_size / shdr->sh_entsize
+         != symshdr->sh_size / symshdr->sh_entsize))
     ERROR (gettext ("\
 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
           idx, section_name (ebl, idx),
index 2797a8496a4a6a2382d520bc1f95764eb7482b6d..03eec292ae1a8ba0c99b15949404d64a925782aa 100644 (file)
@@ -1116,6 +1116,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
   Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
 
   GElf_Sym sym_mem;
+  GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
+
   printf ((grpref[0] & GRP_COMDAT)
          ? ngettext ("\
 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
@@ -1128,8 +1130,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
                      data->d_size / sizeof (Elf32_Word) - 1),
          elf_ndxscn (scn),
          elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
-         elf_strptr (ebl->elf, symshdr->sh_link,
-                     gelf_getsym (symdata, shdr->sh_info, &sym_mem)->st_name)
+         (sym == NULL ? NULL
+          : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
          ?: gettext ("<INVALID SYMBOL>"),
          data->d_size / sizeof (Elf32_Word) - 1);
 
@@ -1280,7 +1282,8 @@ static void
 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
 {
   int class = gelf_getclass (ebl->elf);
-  GElf_Shdr glink;
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink;
   Elf_Data *data;
   size_t cnt;
   size_t shstrndx;
@@ -1295,6 +1298,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
     error (EXIT_FAILURE, 0,
           gettext ("cannot get section header string table index"));
 
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+          elf_ndxscn (scn));
+
   printf (ngettext ("\
 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
                    "\
@@ -1304,9 +1312,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
          class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
          shdr->sh_offset,
          (int) shdr->sh_link,
-         elf_strptr (ebl->elf, shstrndx,
-                     gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-                                   &glink)->sh_name));
+         elf_strptr (ebl->elf, shstrndx, glink->sh_name));
   fputs_unlocked (gettext ("  Type              Value\n"), stdout);
 
   for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
@@ -1806,6 +1812,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
     error (EXIT_FAILURE, 0,
           gettext ("cannot get section header string table index"));
 
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
+                                  &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+          elf_ndxscn (scn));
+
   /* Now we can compute the number of entries in the section.  */
   unsigned int nsyms = data->d_size / (class == ELFCLASS32
                                       ? sizeof (Elf32_Sym)
@@ -1816,15 +1829,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
                    nsyms),
          (unsigned int) elf_ndxscn (scn),
          elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
-  GElf_Shdr glink;
   printf (ngettext (" %lu local symbol  String table: [%2u] '%s'\n",
                    " %lu local symbols  String table: [%2u] '%s'\n",
                    shdr->sh_info),
          (unsigned long int) shdr->sh_info,
          (unsigned int) shdr->sh_link,
-         elf_strptr (ebl->elf, shstrndx,
-                     gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-                                   &glink)->sh_name));
+         elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   fputs_unlocked (class == ELFCLASS32
                  ? gettext ("\
@@ -2060,7 +2070,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
     error (EXIT_FAILURE, 0,
           gettext ("cannot get section header string table index"));
 
-  GElf_Shdr glink;
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
+                                  &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+          elf_ndxscn (scn));
+
   printf (ngettext ("\
 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
                    "\
@@ -2071,9 +2087,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
          class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
          shdr->sh_offset,
          (unsigned int) shdr->sh_link,
-         elf_strptr (ebl->elf, shstrndx,
-                     gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-                                   &glink)->sh_name));
+         elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   unsigned int offset = 0;
   for (int cnt = shdr->sh_info; --cnt >= 0; )
@@ -2126,8 +2140,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
     error (EXIT_FAILURE, 0,
           gettext ("cannot get section header string table index"));
 
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
+                                  &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+          elf_ndxscn (scn));
+
   int class = gelf_getclass (ebl->elf);
-  GElf_Shdr glink;
   printf (ngettext ("\
 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
                    "\
@@ -2139,9 +2159,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
          class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
          shdr->sh_offset,
          (unsigned int) shdr->sh_link,
-         elf_strptr (ebl->elf, shstrndx,
-                     gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-                                   &glink)->sh_name));
+         elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   unsigned int offset = 0;
   for (int cnt = shdr->sh_info; --cnt >= 0; )
@@ -2403,8 +2421,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
       filename = NULL;
     }
 
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
+                                  &glink_mem);
+  if (glink == NULL)
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
+          elf_ndxscn (scn));
+
   /* Print the header.  */
-  GElf_Shdr glink;
   printf (ngettext ("\
 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
                    "\
@@ -2416,9 +2440,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
          class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
          shdr->sh_offset,
          (unsigned int) shdr->sh_link,
-         elf_strptr (ebl->elf, shstrndx,
-                     gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-                                   &glink)->sh_name));
+         elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   /* Now we can finally look at the actual contents of this section.  */
   for (unsigned int cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
@@ -2470,7 +2492,17 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
     ++counts[lengths[cnt]];
 
-  GElf_Shdr glink;
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
+                                              shdr->sh_link),
+                                  &glink_mem);
+  if (glink == NULL)
+    {
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
+            elf_ndxscn (scn));
+      return;
+    }
+
   printf (ngettext ("\
 \nHistogram for bucket list length in section [%2u] '%s' (total of %d bucket):\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
                    "\
@@ -2483,9 +2515,7 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
          shdr->sh_addr,
          shdr->sh_offset,
          (unsigned int) shdr->sh_link,
-         elf_strptr (ebl->elf, shstrndx,
-                     gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
-                                   &glink)->sh_name));
+         elf_strptr (ebl->elf, shstrndx, glink->sh_name));
 
   if (extrastr != NULL)
     fputs (extrastr, stdout);
@@ -4044,6 +4074,16 @@ print_debug_aranges_section (Dwfl_Module *dwflmod __attribute__ ((unused)),
       return;
     }
 
+  GElf_Shdr glink_mem;
+  GElf_Shdr *glink;
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
+  if (glink == NULL)
+    {
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
+            elf_ndxscn (scn));
+      return;
+    }
+
   printf (ngettext ("\
 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
                    "\
index 0f83e61d2a2694a48a054e79823d0d5c3a870453..ffa91441e805f0b72822067ffb12c22e15673887 100644 (file)
@@ -544,6 +544,11 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
       goto fail_close;
     }
 
+  if (shstrndx >= shnum)
+    goto illformed;
+
+#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
+
   /* Storage for section information.  We leave room for two more
      entries since we unconditionally create a section header string
      table.  Maybe some weird tool created an ELF file without one.
@@ -565,7 +570,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
     {
       /* This should always be true (i.e., there should not be any
         holes in the numbering).  */
-      assert (elf_ndxscn (scn) == cnt);
+      elf_assert (elf_ndxscn (scn) == cnt);
 
       shdr_info[cnt].scn = scn;
 
@@ -578,6 +583,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                                        shdr_info[cnt].shdr.sh_name);
       if (shdr_info[cnt].name == NULL)
        {
+       illformed:
          error (0, 0, gettext ("illformed file '%s'"), fname);
          goto fail_close;
        }
@@ -587,6 +593,8 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
 
       /* Remember the shdr.sh_link value.  */
       shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
+      if (shdr_info[cnt].old_sh_link >= shnum)
+       goto illformed;
 
       /* Sections in files other than relocatable object files which
         are not loaded can be freely moved by us.  In relocatable
@@ -599,7 +607,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
         appropriate reference.  */
       if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
        {
-         assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
+         elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
          shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
        }
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
@@ -616,7 +624,12 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
          for (inner = 1;
               inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
               ++inner)
+           {
+             if (grpref[inner] < shnum)
            shdr_info[grpref[inner]].group_idx = cnt;
+             else
+               goto illformed;
+           }
 
          if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
            /* If the section group contains only one element and this
@@ -627,7 +640,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
        }
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
        {
-         assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
+         elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
          shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
        }
 
@@ -635,7 +648,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
         discarded right away.  */
       if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
        {
-         assert (shdr_info[cnt].group_idx != 0);
+         elf_assert (shdr_info[cnt].group_idx != 0);
 
          if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
            {
@@ -710,11 +723,15 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
            {
              /* If a relocation section is marked as being removed make
                 sure the section it is relocating is removed, too.  */
-             if ((shdr_info[cnt].shdr.sh_type == SHT_REL
+             if (shdr_info[cnt].shdr.sh_type == SHT_REL
                   || shdr_info[cnt].shdr.sh_type == SHT_RELA)
-                 && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
+               {
+                 if (shdr_info[cnt].shdr.sh_info >= shnum)
+                   goto illformed;
+                 else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
                shdr_info[cnt].idx = 1;
            }
+           }
 
          if (shdr_info[cnt].idx == 1)
            {
@@ -741,7 +758,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                  if (shdr_info[cnt].symtab_idx != 0
                      && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
                    {
-                     assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
+                     elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
 
                      shdr_info[shdr_info[cnt].symtab_idx].data
                        = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
@@ -781,6 +798,9 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                      else if (scnidx == SHN_XINDEX)
                        scnidx = xndx;
 
+                     if (scnidx >= shnum)
+                       goto illformed;
+
                      if (shdr_info[scnidx].idx == 0)
                        /* This symbol table has a real symbol in
                           a discarded section.  So preserve the
@@ -811,12 +831,16 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                }
 
              /* Handle references through sh_info.  */
-             if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
-                 && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
+             if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
+               {
+                 if (shdr_info[cnt].shdr.sh_info >= shnum)
+                   goto illformed;
+                 else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
                {
                  shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
                  changes |= shdr_info[cnt].shdr.sh_info < cnt;
                }
+               }
 
              /* Mark the section as investigated.  */
              shdr_info[cnt].idx = 2;
@@ -954,7 +978,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
          error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
                 elf_errmsg (-1));
 
-       assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
+       elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
 
        /* Add this name to the section header string table.  */
        shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
@@ -991,7 +1015,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
        error (EXIT_FAILURE, 0,
               gettext ("while create section header section: %s"),
               elf_errmsg (-1));
-      assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
+      elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
 
       shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
       if (shdr_info[cnt].data == NULL)
@@ -1047,7 +1071,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
     error (EXIT_FAILURE, 0,
           gettext ("while create section header section: %s"),
           elf_errmsg (-1));
-  assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
+  elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
 
   /* Finalize the string table and fill in the correct indices in the
      section headers.  */
@@ -1137,20 +1161,20 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                    shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
                                             NULL);
 
-                   assert ((versiondata->d_size / sizeof (Elf32_Word))
+                   elf_assert ((versiondata->d_size / sizeof (Elf32_Word))
                            >= shdr_info[cnt].data->d_size / elsize);
                  }
 
                if (shdr_info[cnt].version_idx != 0)
                  {
-                   assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
+                   elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
                    /* This section has associated version
                       information.  We have to modify that
                       information, too.  */
                    versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
                                               NULL);
 
-                   assert ((versiondata->d_size / sizeof (GElf_Versym))
+                   elf_assert ((versiondata->d_size / sizeof (GElf_Versym))
                            >= shdr_info[cnt].data->d_size / elsize);
                  }
 
@@ -1205,7 +1229,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                      sec = shdr_info[sym->st_shndx].idx;
                    else
                      {
-                       assert (shndxdata != NULL);
+                       elf_assert (shndxdata != NULL);
 
                        sec = shdr_info[xshndx].idx;
                      }
@@ -1226,7 +1250,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                            nxshndx = sec;
                          }
 
-                       assert (sec < SHN_LORESERVE || shndxdata != NULL);
+                       elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
 
                        if ((inner != destidx || nshndx != sym->st_shndx
                             || (shndxdata != NULL && nxshndx != xshndx))
@@ -1250,7 +1274,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                             || shdr_info[cnt].debug_data == NULL)
                      /* This is a section symbol for a section which has
                         been removed.  */
-                     assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
+                     elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
                  }
 
                if (destidx != inner)
@@ -1437,11 +1461,11 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                  {
                    GElf_Sym sym_mem;
                    GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
-                   assert (sym != NULL);
+                   elf_assert (sym != NULL);
 
                    const char *name = elf_strptr (elf, strshndx,
                                                   sym->st_name);
-                   assert (name != NULL);
+                   elf_assert (name != NULL);
                    size_t hidx = elf_hash (name) % nbucket;
 
                    if (bucket[hidx] == 0)
@@ -1460,7 +1484,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
            else
              {
                /* Alpha and S390 64-bit use 64-bit SHT_HASH entries.  */
-               assert (shdr_info[cnt].shdr.sh_entsize
+               elf_assert (shdr_info[cnt].shdr.sh_entsize
                        == sizeof (Elf64_Xword));
 
                Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
@@ -1491,11 +1515,11 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
                  {
                    GElf_Sym sym_mem;
                    GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
-                   assert (sym != NULL);
+                   elf_assert (sym != NULL);
 
                    const char *name = elf_strptr (elf, strshndx,
                                                   sym->st_name);
-                   assert (name != NULL);
+                   elf_assert (name != NULL);
                    size_t hidx = elf_hash (name) % nbucket;
 
                    if (bucket[hidx] == 0)