e39c7b
src/
e39c7b
2005-06-09  Roland McGrath  <roland@redhat.com>
e39c7b
e39c7b
	* readelf.c (handle_dynamic, handle_symtab): Check for bogus sh_link.
e39c7b
	(handle_verneed, handle_verdef, handle_versym, handle_hash): Likewise.
e39c7b
	(handle_scngrp): Check for bogus sh_info.
e39c7b
e39c7b
	* strip.c (handle_elf): Check for bogus values in sh_link, sh_info,
e39c7b
	st_shndx, e_shstrndx, and SHT_GROUP or SHT_SYMTAB_SHNDX data.
e39c7b
	Don't use assert on input values, instead bail with "illformed" error.
e39c7b
e39c7b
2005-05-17  Jakub Jelinek  <jakub@redhat.com>
e39c7b
e39c7b
libelf/
e39c7b
	* elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header
e39c7b
	table fits into object's bounds.
e39c7b
	* elf_getshstrndx.c (elf_getshstrndx): Add elf->start_offset to
e39c7b
	elf->map_address.  Check if first section header fits into object's
e39c7b
	bounds.
233557
	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)):
e39c7b
	Check if section header table fits into object's bounds.
4af02c
	* elf_begin.c (get_shnum): Ensure section headers fits into
4af02c
	object's bounds.
e39c7b
	(file_read_elf): Make sure scncnt is small enough to allocate both
e39c7b
	ElfXX_Shdr and Elf_Scn array.  Make sure section and program header
e39c7b
	tables fit into object's bounds.  Avoid memory leak on failure.
e39c7b
e39c7b
src/
e39c7b
	* elflint.c (check_hash): Don't check entries beyond end of section.
e39c7b
	(check_note): Don't crash if gelf_rawchunk fails.
e39c7b
	(section_name): Return <invalid> if gelf_getshdr returns NULL.
e39c7b
e39c7b
2005-05-14  Jakub Jelinek  <jakub@redhat.com>
e39c7b
e39c7b
libelf/
e39c7b
	* libelfP.h (INVALID_NDX): Define.
e39c7b
	* gelf_getdyn.c (gelf_getdyn): Use it.  Remove ndx < 0 test if any.
e39c7b
	* gelf_getlib.c (gelf_getlib): Likewise.
e39c7b
	* gelf_getmove.c (gelf_getmove): Likewise.
e39c7b
	* gelf_getrel.c (gelf_getrel): Likewise.
e39c7b
	* gelf_getrela.c (gelf_getrela): Likewise.
e39c7b
	* gelf_getsym.c (gelf_getsym): Likewise.
e39c7b
	* gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
e39c7b
	* gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
e39c7b
	* gelf_getversym.c (gelf_getversym): Likewise.
e39c7b
	* gelf_update_dyn.c (gelf_update_dyn): Likewise.
e39c7b
	* gelf_update_lib.c (gelf_update_lib): Likewise.
e39c7b
	* gelf_update_move.c (gelf_update_move): Likewise.
e39c7b
	* gelf_update_rel.c (gelf_update_rel): Likewise.
e39c7b
	* gelf_update_rela.c (gelf_update_rela): Likewise.
e39c7b
	* gelf_update_sym.c (gelf_update_sym): Likewise.
e39c7b
	* gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
e39c7b
	* gelf_update_symshndx.c (gelf_update_symshndx): Likewise.
e39c7b
	* gelf_update_versym.c (gelf_update_versym): Likewise.
e39c7b
	* elf_newscn.c (elf_newscn): Check for overflow.
e39c7b
	* elf32_updatefile.c (__elfw2(LIBELFBITS,updatemmap)): Likewise.
e39c7b
	(__elfw2(LIBELFBITS,updatefile)): Likewise.
e39c7b
	* elf_begin.c (file_read_elf): Likewise.
e39c7b
	* elf32_newphdr.c (elfw2(LIBELFBITS,newphdr)): Likewise.
e39c7b
	* elf_getarsym.c (elf_getarsym): Likewise.
e39c7b
	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Likewise.
e39c7b
src/
e39c7b
	* elflint.c (section_name): Return "<invalid>" instead of
e39c7b
	crashing on invalid section name.
e39c7b
	(check_symtab, is_rel_dyn, check_rela, check_rel, check_dynamic,
e39c7b
	check_symtab_shndx, check_hash, check_versym): Robustify.
e39c7b
2fa04c
--- elfutils-0.127/src/readelf.c.robustify
2fa04c
+++ elfutils-0.127/src/readelf.c
2fa04c
@@ -958,6 +958,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
2fa04c
   Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
e39c7b
 
2fa04c
   GElf_Sym sym_mem;
2fa04c
+  GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
e39c7b
+
2fa04c
   printf ((grpref[0] & GRP_COMDAT)
2fa04c
 	  ? ngettext ("\
2fa04c
 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
2fa04c
@@ -970,8 +972,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
2fa04c
 		      data->d_size / sizeof (Elf32_Word) - 1),
2fa04c
 	  elf_ndxscn (scn),
2fa04c
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2fa04c
-	  elf_strptr (ebl->elf, symshdr->sh_link,
2fa04c
-		      gelf_getsym (symdata, shdr->sh_info, &sym_mem)->st_name)
2fa04c
+	  (sym == NULL ? NULL
2fa04c
+	   : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
2fa04c
 	  ?: gettext ("<INVALID SYMBOL>"),
2fa04c
 	  data->d_size / sizeof (Elf32_Word) - 1);
e39c7b
 
2fa04c
@@ -1122,7 +1124,8 @@ static void
2fa04c
 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2fa04c
 {
2fa04c
   int class = gelf_getclass (ebl->elf);
2fa04c
-  GElf_Shdr glink;
2fa04c
+  GElf_Shdr glink_mem;
2fa04c
+  GElf_Shdr *glink;
2fa04c
   Elf_Data *data;
2fa04c
   size_t cnt;
2fa04c
   size_t shstrndx;
2fa04c
@@ -1137,6 +1140,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
2fa04c
     error (EXIT_FAILURE, 0,
2fa04c
 	   gettext ("cannot get section header string table index"));
e39c7b
 
2fa04c
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
2fa04c
+  if (glink == NULL)
2fa04c
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c
+	   elf_ndxscn (scn));
e39c7b
+
2fa04c
   printf (ngettext ("\
2fa04c
 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
2fa04c
 		    "\
2fa04c
@@ -1146,9 +1154,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
2fa04c
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2fa04c
 	  shdr->sh_offset,
2fa04c
 	  (int) shdr->sh_link,
2fa04c
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
-				    &glink)->sh_name));
2fa04c
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2fa04c
   fputs_unlocked (gettext ("  Type              Value\n"), stdout);
233557
 
2fa04c
   for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
2fa04c
@@ -1666,6 +1672,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
2fa04c
     error (EXIT_FAILURE, 0,
2fa04c
 	   gettext ("cannot get section header string table index"));
2fa04c
 
2fa04c
+  GElf_Shdr glink_mem;
2fa04c
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
+				   &glink_mem);
2fa04c
+  if (glink == NULL)
2fa04c
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c
+	   elf_ndxscn (scn));
e39c7b
+
2fa04c
   /* Now we can compute the number of entries in the section.  */
2fa04c
   unsigned int nsyms = data->d_size / (class == ELFCLASS32
2fa04c
 				       ? sizeof (Elf32_Sym)
2fa04c
@@ -1676,15 +1689,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
2fa04c
 		    nsyms),
2fa04c
 	  (unsigned int) elf_ndxscn (scn),
2fa04c
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2fa04c
-  GElf_Shdr glink;
2fa04c
   printf (ngettext (" %lu local symbol  String table: [%2u] '%s'\n",
2fa04c
 		    " %lu local symbols  String table: [%2u] '%s'\n",
2fa04c
 		    shdr->sh_info),
2fa04c
 	  (unsigned long int) shdr->sh_info,
2fa04c
 	  (unsigned int) shdr->sh_link,
2fa04c
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
-				    &glink)->sh_name));
2fa04c
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2fa04c
 
2fa04c
   fputs_unlocked (class == ELFCLASS32
2fa04c
 		  ? gettext ("\
2fa04c
@@ -1920,7 +1930,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
2fa04c
     error (EXIT_FAILURE, 0,
2fa04c
 	   gettext ("cannot get section header string table index"));
2fa04c
 
2fa04c
-  GElf_Shdr glink;
2fa04c
+  GElf_Shdr glink_mem;
2fa04c
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
+				   &glink_mem);
2fa04c
+  if (glink == NULL)
2fa04c
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c
+	   elf_ndxscn (scn));
e39c7b
+
2fa04c
   printf (ngettext ("\
2fa04c
 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
2fa04c
 		    "\
2fa04c
@@ -1931,9 +1947,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
2fa04c
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2fa04c
 	  shdr->sh_offset,
2fa04c
 	  (unsigned int) shdr->sh_link,
2fa04c
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
-				    &glink)->sh_name));
2fa04c
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
e39c7b
 
2fa04c
   unsigned int offset = 0;
2fa04c
   for (int cnt = shdr->sh_info; --cnt >= 0; )
2fa04c
@@ -1986,8 +2000,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
2fa04c
     error (EXIT_FAILURE, 0,
2fa04c
 	   gettext ("cannot get section header string table index"));
2fa04c
 
2fa04c
+  GElf_Shdr glink_mem;
2fa04c
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
+				   &glink_mem);
2fa04c
+  if (glink == NULL)
2fa04c
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c
+	   elf_ndxscn (scn));
2fa04c
+
2fa04c
   int class = gelf_getclass (ebl->elf);
2fa04c
-  GElf_Shdr glink;
2fa04c
   printf (ngettext ("\
2fa04c
 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
2fa04c
 		    "\
2fa04c
@@ -1999,9 +2019,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
2fa04c
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2fa04c
 	  shdr->sh_offset,
2fa04c
 	  (unsigned int) shdr->sh_link,
2fa04c
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
-				    &glink)->sh_name));
2fa04c
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
e39c7b
 
2fa04c
   unsigned int offset = 0;
2fa04c
   for (int cnt = shdr->sh_info; --cnt >= 0; )
2fa04c
@@ -2263,8 +2281,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
2fa04c
       filename = NULL;
2fa04c
     }
e39c7b
 
2fa04c
+  GElf_Shdr glink_mem;
2fa04c
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
+				   &glink_mem);
2fa04c
+  if (glink == NULL)
2fa04c
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c
+	   elf_ndxscn (scn));
2fa04c
+
2fa04c
   /* Print the header.  */
2fa04c
-  GElf_Shdr glink;
2fa04c
   printf (ngettext ("\
2fa04c
 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
2fa04c
 		    "\
2fa04c
@@ -2276,9 +2300,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
2fa04c
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2fa04c
 	  shdr->sh_offset,
2fa04c
 	  (unsigned int) shdr->sh_link,
2fa04c
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
-				    &glink)->sh_name));
2fa04c
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
e39c7b
 
2fa04c
   /* Now we can finally look at the actual contents of this section.  */
2fa04c
   for (unsigned int cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
2fa04c
@@ -2330,7 +2352,17 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
2fa04c
   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
2fa04c
     ++counts[lengths[cnt]];
e39c7b
 
2fa04c
-  GElf_Shdr glink;
2fa04c
+  GElf_Shdr glink_mem;
2fa04c
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
2fa04c
+					       shdr->sh_link),
2fa04c
+				   &glink_mem);
2fa04c
+  if (glink == NULL)
e39c7b
+    {
2fa04c
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c
+	     elf_ndxscn (scn));
2fa04c
+      return;
e39c7b
+    }
e39c7b
+
2fa04c
   printf (ngettext ("\
2fa04c
 \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",
2fa04c
 		    "\
2fa04c
@@ -2343,9 +2375,7 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
2fa04c
 	  shdr->sh_addr,
2fa04c
 	  shdr->sh_offset,
2fa04c
 	  (unsigned int) shdr->sh_link,
2fa04c
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c
-				    &glink)->sh_name));
2fa04c
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
e39c7b
 
2fa04c
   if (extrastr != NULL)
2fa04c
     fputs (extrastr, stdout);
2fa04c
@@ -3655,6 +3685,16 @@ print_debug_aranges_section (Ebl *ebl __
2fa04c
       return;
2fa04c
     }
2fa04c
 
2fa04c
+  GElf_Shdr glink_mem;
2fa04c
+  GElf_Shdr *glink;
2fa04c
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
2fa04c
+  if (glink == NULL)
2fa04c
+    {
2fa04c
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c
+	     elf_ndxscn (scn));
2fa04c
+      return;
2fa04c
+    }
4af02c
+
2fa04c
   printf (ngettext ("\
2fa04c
 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
2fa04c
 		    "\
2d93f1
--- elfutils-0.128/src/strip.c.orig
2d93f1
+++ elfutils-0.128/src/strip.c
2d93f1
@@ -413,6 +413,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
   Elf_Data debuglink_crc_data;
2fa04c
   bool any_symtab_changes = false;
2fa04c
   Elf_Data *shstrtab_data = NULL;
2fa04c
+  size_t shdridx = 0;
e39c7b
 
2fa04c
   /* Create the full name of the file.  */
2fa04c
   if (prefix != NULL)
2d93f1
@@ -543,6 +544,11 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
       goto fail_close;
2fa04c
     }
e39c7b
 
2fa04c
+  if (shstrndx >= shnum)
2fa04c
+    goto illformed;
e39c7b
+
2fa04c
+#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
e39c7b
+
2fa04c
   /* Storage for section information.  We leave room for two more
2fa04c
      entries since we unconditionally create a section header string
2fa04c
      table.  Maybe some weird tool created an ELF file without one.
2d93f1
@@ -564,7 +570,7 @@ handle_elf (int fd, Elf *elf, const char
e39c7b
     {
2fa04c
       /* This should always be true (i.e., there should not be any
2fa04c
 	 holes in the numbering).  */
2fa04c
-      assert (elf_ndxscn (scn) == cnt);
2fa04c
+      elf_assert (elf_ndxscn (scn) == cnt);
e39c7b
 
2fa04c
       shdr_info[cnt].scn = scn;
e39c7b
 
2d93f1
@@ -577,6 +583,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 					shdr_info[cnt].shdr.sh_name);
2fa04c
       if (shdr_info[cnt].name == NULL)
e39c7b
 	{
2fa04c
+	illformed:
2fa04c
 	  error (0, 0, gettext ("illformed file '%s'"), fname);
2fa04c
 	  goto fail_close;
2fa04c
 	}
2d93f1
@@ -586,6 +593,8 @@ handle_elf (int fd, Elf *elf, const char
e39c7b
 
2fa04c
       /* Remember the shdr.sh_link value.  */
2fa04c
       shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
2fa04c
+      if (shdr_info[cnt].old_sh_link >= shnum)
2fa04c
+	goto illformed;
e39c7b
 
2fa04c
       /* Sections in files other than relocatable object files which
2fa04c
 	 are not loaded can be freely moved by us.  In relocatable
2d93f1
@@ -598,7 +607,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 	 appropriate reference.  */
2fa04c
       if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
e39c7b
 	{
2fa04c
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
2fa04c
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
2fa04c
 	  shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
2fa04c
 	}
2fa04c
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
2d93f1
@@ -615,7 +624,12 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 	  for (inner = 1;
2fa04c
 	       inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
2fa04c
 	       ++inner)
2fa04c
+	    {
2fa04c
+	      if (grpref[inner] < shnum)
2fa04c
 	    shdr_info[grpref[inner]].group_idx = cnt;
2fa04c
+	      else
2fa04c
+		goto illformed;
2fa04c
+	    }
e39c7b
 
2fa04c
 	  if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
2fa04c
 	    /* If the section group contains only one element and this
2d93f1
@@ -626,7 +640,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 	}
2fa04c
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
e39c7b
 	{
2fa04c
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
2fa04c
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
2fa04c
 	  shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
2fa04c
 	}
e39c7b
 
2d93f1
@@ -634,7 +648,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 	 discarded right away.  */
2fa04c
       if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
e39c7b
 	{
2fa04c
-	  assert (shdr_info[cnt].group_idx != 0);
2fa04c
+	  elf_assert (shdr_info[cnt].group_idx != 0);
e39c7b
 
2fa04c
 	  if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
2fa04c
 	    {
2d93f1
@@ -709,11 +723,15 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 	    {
2fa04c
 	      /* If a relocation section is marked as being removed make
2fa04c
 		 sure the section it is relocating is removed, too.  */
2fa04c
-	      if ((shdr_info[cnt].shdr.sh_type == SHT_REL
2fa04c
+	      if (shdr_info[cnt].shdr.sh_type == SHT_REL
2fa04c
 		   || shdr_info[cnt].shdr.sh_type == SHT_RELA)
2fa04c
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
2fa04c
+		{
2fa04c
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
2fa04c
+		    goto illformed;
2fa04c
+		  else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
2fa04c
 		shdr_info[cnt].idx = 1;
2fa04c
 	    }
2fa04c
+	    }
e39c7b
 
2fa04c
 	  if (shdr_info[cnt].idx == 1)
2fa04c
 	    {
2d93f1
@@ -738,7 +756,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 		  if (shdr_info[cnt].symtab_idx != 0
2fa04c
 		      && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
2fa04c
 		    {
2fa04c
-		      assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
2fa04c
+		      elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
e39c7b
 
2fa04c
 		      shdr_info[shdr_info[cnt].symtab_idx].data
2fa04c
 			= elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
2d93f1
@@ -778,6 +796,9 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 		      else if (scnidx == SHN_XINDEX)
2fa04c
 			scnidx = xndx;
e39c7b
 
2fa04c
+		      if (scnidx >= shnum)
2fa04c
+			goto illformed;
2fa04c
+
2fa04c
 		      if (shdr_info[scnidx].idx == 0)
2fa04c
 			{
2fa04c
 			  /* Mark this section as used.  */
2d93f1
@@ -809,12 +830,16 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 		}
e39c7b
 
2fa04c
 	      /* Handle references through sh_info.  */
2fa04c
-	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
2fa04c
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
2fa04c
+	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
2fa04c
+		{
2fa04c
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
2fa04c
+		    goto illformed;
2fa04c
+		  else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
2fa04c
 		{
2fa04c
 		  shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
2fa04c
 		  changes |= shdr_info[cnt].shdr.sh_info < cnt;
2fa04c
 		}
2fa04c
+		}
e39c7b
 
2fa04c
 	      /* Mark the section as investigated.  */
2fa04c
 	      shdr_info[cnt].idx = 2;
2d93f1
@@ -953,7 +978,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 	  error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
2fa04c
 		 elf_errmsg (-1));
e39c7b
 
2fa04c
-	assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
2fa04c
+	elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
e39c7b
 
2fa04c
 	/* Add this name to the section header string table.  */
2fa04c
 	shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
2d93f1
@@ -990,7 +1015,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 	error (EXIT_FAILURE, 0,
2fa04c
 	       gettext ("while create section header section: %s"),
2fa04c
 	       elf_errmsg (-1));
2fa04c
-      assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
2fa04c
+      elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
e39c7b
 
2fa04c
       shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
2fa04c
       if (shdr_info[cnt].data == NULL)
2d93f1
@@ -1021,7 +1046,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
     }
e39c7b
 
2fa04c
   /* Index of the section header table in the shdr_info array.  */
2fa04c
-  size_t shdridx = cnt;
2fa04c
+  shdridx = cnt;
e39c7b
 
2fa04c
   /* Add the section header string table section name.  */
2fa04c
   shdr_info[cnt].se = ebl_strtabadd (shst, ".shstrtab", 10);
2d93f1
@@ -1046,7 +1071,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
     error (EXIT_FAILURE, 0,
2fa04c
 	   gettext ("while create section header section: %s"),
2fa04c
 	   elf_errmsg (-1));
2fa04c
-  assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
2fa04c
+  elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
e39c7b
 
2fa04c
   /* Finalize the string table and fill in the correct indices in the
2fa04c
      section headers.  */
2d93f1
@@ -1136,20 +1161,20 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 		    shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
2fa04c
 					     NULL);
e39c7b
 
2fa04c
-		    assert ((versiondata->d_size / sizeof (Elf32_Word))
2fa04c
+		    elf_assert ((versiondata->d_size / sizeof (Elf32_Word))
2fa04c
 			    >= shdr_info[cnt].data->d_size / elsize);
2fa04c
 		  }
e39c7b
 
2fa04c
 		if (shdr_info[cnt].version_idx != 0)
2fa04c
 		  {
2fa04c
-		    assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
2fa04c
+		    elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
2fa04c
 		    /* This section has associated version
2fa04c
 		       information.  We have to modify that
2fa04c
 		       information, too.  */
2fa04c
 		    versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
2fa04c
 					       NULL);
e39c7b
 
2fa04c
-		    assert ((versiondata->d_size / sizeof (GElf_Versym))
2fa04c
+		    elf_assert ((versiondata->d_size / sizeof (GElf_Versym))
2fa04c
 			    >= shdr_info[cnt].data->d_size / elsize);
2fa04c
 		  }
e39c7b
 
2d93f1
@@ -1204,7 +1229,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 		      sec = shdr_info[sym->st_shndx].idx;
2fa04c
 		    else
2fa04c
 		      {
2fa04c
-			assert (shndxdata != NULL);
2fa04c
+			elf_assert (shndxdata != NULL);
e39c7b
 
2fa04c
 			sec = shdr_info[xshndx].idx;
2fa04c
 		      }
2d93f1
@@ -1225,7 +1250,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 			    nxshndx = sec;
2fa04c
 			  }
e39c7b
 
2fa04c
-			assert (sec < SHN_LORESERVE || shndxdata != NULL);
2fa04c
+			elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
e39c7b
 
2fa04c
 			if ((inner != destidx || nshndx != sym->st_shndx
2fa04c
 			     || (shndxdata != NULL && nxshndx != xshndx))
2d93f1
@@ -1248,7 +1273,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 		    else
2fa04c
 		      /* This is a section symbol for a section which has
2fa04c
 			 been removed.  */
2fa04c
-		      assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
2fa04c
+		      elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
2fa04c
 		  }
e39c7b
 
2fa04c
 		if (destidx != inner)
2d93f1
@@ -1441,11 +1466,11 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 		    {
2fa04c
 		      GElf_Sym sym_mem;
2fa04c
 		      GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
2fa04c
-		      assert (sym != NULL);
2fa04c
+		      elf_assert (sym != NULL);
e39c7b
 
2fa04c
 		      const char *name = elf_strptr (elf, strshndx,
2fa04c
 						     sym->st_name);
2fa04c
-		      assert (name != NULL);
2fa04c
+		      elf_assert (name != NULL);
2fa04c
 		      size_t hidx = elf_hash (name) % nbucket;
e39c7b
 
2fa04c
 		      if (bucket[hidx] == 0)
2d93f1
@@ -1464,7 +1489,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 	      else
2fa04c
 		{
2fa04c
 		  /* Alpha and S390 64-bit use 64-bit SHT_HASH entries.  */
2fa04c
-		  assert (shdr_info[cnt].shdr.sh_entsize
2fa04c
+		  elf_assert (shdr_info[cnt].shdr.sh_entsize
2fa04c
 			  == sizeof (Elf64_Xword));
2fa04c
 
2fa04c
 		  Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
2d93f1
@@ -1495,11 +1520,11 @@ handle_elf (int fd, Elf *elf, const char
2fa04c
 		    {
2fa04c
 		      GElf_Sym sym_mem;
2fa04c
 		      GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
2fa04c
-		      assert (sym != NULL);
2fa04c
+		      elf_assert (sym != NULL);
2fa04c
 
2fa04c
 		      const char *name = elf_strptr (elf, strshndx,
2fa04c
 						     sym->st_name);
2fa04c
-		      assert (name != NULL);
2fa04c
+		      elf_assert (name != NULL);
2fa04c
 		      size_t hidx = elf_hash (name) % nbucket;
2fa04c
 
2fa04c
 		      if (bucket[hidx] == 0)
2fa04c
--- elfutils-0.127/src/elflint.c.robustify
2fa04c
+++ elfutils-0.127/src/elflint.c
2fa04c
@@ -123,6 +123,9 @@ static uint32_t shstrndx;
2fa04c
 /* Array to count references in section groups.  */
2fa04c
 static int *scnref;
2fa04c
 
2fa04c
+/* Number of sections.  */
2fa04c
+static unsigned int shnum;
2fa04c
+
2fa04c
 
2fa04c
 int
2fa04c
 main (int argc, char *argv[])
2fa04c
@@ -312,10 +315,19 @@ section_name (Ebl *ebl, int idx)
2fa04c
 {
2fa04c
   GElf_Shdr shdr_mem;
2fa04c
   GElf_Shdr *shdr;
e39c7b
+  const char *ret;
e39c7b
+
e39c7b
+  if ((unsigned int) idx > shnum)
e39c7b
+    return "<invalid>";
e39c7b
 
e39c7b
   shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
e39c7b
+  if (shdr == NULL)
e39c7b
+    return "<invalid>";
e39c7b
 
e39c7b
-  return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
e39c7b
+  ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
e39c7b
+  if (ret == NULL)
e39c7b
+    return "<invalid>";
e39c7b
+  return ret;
e39c7b
 }
e39c7b
 
e39c7b
 
26b409
@@ -337,10 +349,6 @@ static const int valid_e_machine[] =
e39c7b
   (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
e39c7b
 
e39c7b
 
e39c7b
-/* Number of sections.  */
e39c7b
-static unsigned int shnum;
e39c7b
-
e39c7b
-
e39c7b
 static void
e39c7b
 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
e39c7b
 {
26b409
@@ -603,7 +611,8 @@ section [%2d] '%s': symbol table cannot 
e39c7b
 	  }
e39c7b
       }
e39c7b
 
e39c7b
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
e39c7b
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
e39c7b
+  if (shdr->sh_entsize != sh_entsize)
e39c7b
     ERROR (gettext ("\
e39c7b
 section [%2zu] '%s': entry size is does not match ElfXX_Sym\n"),
e39c7b
 	   cnt, section_name (ebl, cnt));
26b409
@@ -641,7 +650,7 @@ section [%2d] '%s': XINDEX for zeroth en
e39c7b
 	       xndxscnidx, section_name (ebl, xndxscnidx));
e39c7b
     }
e39c7b
 
e39c7b
-  for (cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
e39c7b
+  for (cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
e39c7b
     {
e39c7b
       sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
e39c7b
       if (sym == NULL)
26b409
@@ -659,7 +668,8 @@ section [%2d] '%s': symbol %zu: invalid 
e39c7b
       else
e39c7b
 	{
e39c7b
 	  name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
e39c7b
-	  assert (name != NULL);
e39c7b
+	  assert (name != NULL
e39c7b
+		  || strshdr->sh_type != SHT_STRTAB);
e39c7b
 	}
e39c7b
 
e39c7b
       if (sym->st_shndx == SHN_XINDEX)
2fa04c
@@ -981,9 +991,11 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
acf77d
     {
acf77d
       GElf_Shdr rcshdr_mem;
e39c7b
       const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
acf77d
-      assert (rcshdr != NULL);
e39c7b
 
e39c7b
-      if (rcshdr->sh_type == SHT_DYNAMIC)
acf77d
+      if (rcshdr == NULL)
acf77d
+	break;
acf77d
+
e39c7b
+      if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
e39c7b
 	{
e39c7b
 	  /* Found the dynamic section.  Look through it.  */
e39c7b
 	  Elf_Data *d = elf_getdata (scn, NULL);
2fa04c
@@ -993,7 +1005,9 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
e39c7b
 	    {
e39c7b
 	      GElf_Dyn dyn_mem;
e39c7b
 	      GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
e39c7b
-	      assert (dyn != NULL);
e39c7b
+
e39c7b
+	      if (dyn == NULL)
e39c7b
+		break;
e39c7b
 
e39c7b
 	      if (dyn->d_tag == DT_RELCOUNT)
e39c7b
 		{
2fa04c
@@ -1007,7 +1021,9 @@ section [%2d] '%s': DT_RELCOUNT used for
acf77d
 		      /* Does the number specified number of relative
acf77d
 			 relocations exceed the total number of
acf77d
 			 relocations?  */
acf77d
-		      if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
acf77d
+		      if (shdr->sh_entsize != 0
acf77d
+			  && dyn->d_un.d_val > (shdr->sh_size
acf77d
+						/ shdr->sh_entsize))
acf77d
 			ERROR (gettext ("\
e39c7b
 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
acf77d
 			       idx, section_name (ebl, idx),
2fa04c
@@ -1167,7 +1183,8 @@ section [%2d] '%s': no relocations for m
e39c7b
 	}
e39c7b
     }
e39c7b
 
e39c7b
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
e39c7b
+  size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
e39c7b
+  if (shdr->sh_entsize != sh_entsize)
e39c7b
     ERROR (gettext (reltype == ELF_T_RELA ? "\
e39c7b
 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
e39c7b
 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
2fa04c
@@ -1389,7 +1406,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
e39c7b
   Elf_Data *symdata = elf_getdata (symscn, NULL);
e39c7b
   enum load_state state = state_undecided;
e39c7b
 
e39c7b
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
e39c7b
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
e39c7b
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
e39c7b
     {
e39c7b
       GElf_Rela rela_mem;
e39c7b
       GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
2fa04c
@@ -1439,7 +1457,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
e39c7b
   Elf_Data *symdata = elf_getdata (symscn, NULL);
e39c7b
   enum load_state state = state_undecided;
e39c7b
 
e39c7b
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
e39c7b
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
e39c7b
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
e39c7b
     {
e39c7b
       GElf_Rel rel_mem;
e39c7b
       GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
2fa04c
@@ -1543,7 +1562,8 @@ section [%2d] '%s': referenced as string
e39c7b
 	   shdr->sh_link, section_name (ebl, shdr->sh_link),
e39c7b
 	   idx, section_name (ebl, idx));
e39c7b
 
e39c7b
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
e39c7b
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
e39c7b
+  if (shdr->sh_entsize != sh_entsize)
e39c7b
     ERROR (gettext ("\
e39c7b
 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
e39c7b
 	   idx, section_name (ebl, idx));
2fa04c
@@ -1553,7 +1573,7 @@ section [%2d] '%s': section entry size d
e39c7b
 	   idx, section_name (ebl, idx));
e39c7b
 
e39c7b
   bool non_null_warned = false;
e39c7b
-  for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
e39c7b
+  for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
e39c7b
     {
e39c7b
       GElf_Dyn dyn_mem;
e39c7b
       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
2fa04c
@@ -1834,6 +1854,8 @@ section [%2d] '%s': entry size does not 
e39c7b
 	   idx, section_name (ebl, idx));
e39c7b
 
e39c7b
   if (symshdr != NULL
e39c7b
+      && shdr->sh_entsize
e39c7b
+      && symshdr->sh_entsize
e39c7b
       && (shdr->sh_size / shdr->sh_entsize
e39c7b
 	  < symshdr->sh_size / symshdr->sh_entsize))
e39c7b
     ERROR (gettext ("\
2fa04c
@@ -1860,6 +1882,12 @@ section [%2d] '%s': extended section ind
e39c7b
     }
e39c7b
 
e39c7b
   Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
e39c7b
+  if (data == NULL)
e39c7b
+    {
e39c7b
+      ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
e39c7b
+ 	     idx, section_name (ebl, idx));
e39c7b
+      return;
e39c7b
+    }
e39c7b
 
e39c7b
   if (*((Elf32_Word *) data->d_buf) != 0)
e39c7b
     ERROR (gettext ("symbol 0 should have zero extended section index\n"));
2fa04c
@@ -1902,7 +1930,7 @@ section [%2d] '%s': hash table section i
acf77d
 
acf77d
   size_t maxidx = nchain;
e39c7b
 
e39c7b
-  if (symshdr != NULL)
acf77d
+  if (symshdr != NULL && symshdr->sh_entsize != 0)
e39c7b
     {
e39c7b
       size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
acf77d
 
2fa04c
@@ -1913,18 +1941,28 @@ section [%2d] '%s': hash table section i
acf77d
       maxidx = symsize;
acf77d
     }
acf77d
 
acf77d
+  Elf32_Word *buf = (Elf32_Word *) data->d_buf;
acf77d
+  Elf32_Word *end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
acf77d
   size_t cnt;
acf77d
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
acf77d
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
acf77d
+    {
acf77d
+      if (buf + cnt >= end)
acf77d
+	break;
acf77d
+      else if (buf[cnt] >= maxidx)
2fa04c
       ERROR (gettext ("\
e39c7b
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
2fa04c
 	     idx, section_name (ebl, idx), cnt - 2);
acf77d
+    }
acf77d
 
acf77d
   for (; cnt < 2 + nbucket + nchain; ++cnt)
acf77d
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
acf77d
+    {
acf77d
+      if (buf + cnt >= end)
acf77d
+	break;
acf77d
+      else if (buf[cnt] >= maxidx)
2fa04c
       ERROR (gettext ("\
e39c7b
 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
2fa04c
 	     idx, section_name (ebl, idx), cnt - 2 - nbucket);
acf77d
+    }
acf77d
 }
acf77d
 
acf77d
 
2fa04c
@@ -1954,18 +1992,28 @@ section [%2d] '%s': hash table section i
acf77d
       maxidx = symsize;
acf77d
     }
acf77d
 
acf77d
+  Elf64_Xword *buf = (Elf64_Xword *) data->d_buf;
acf77d
+  Elf64_Xword *end = (Elf64_Xword *) ((char *) data->d_buf + shdr->sh_size);
acf77d
   size_t cnt;
acf77d
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
acf77d
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
acf77d
+    {
acf77d
+      if (buf + cnt >= end)
acf77d
+	break;
acf77d
+      else if (buf[cnt] >= maxidx)
2fa04c
       ERROR (gettext ("\
acf77d
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
2fa04c
 	     idx, section_name (ebl, idx), cnt - 2);
acf77d
+    }
acf77d
 
acf77d
   for (; cnt < 2 + nbucket + nchain; ++cnt)
acf77d
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
acf77d
+    {
acf77d
+      if (buf + cnt >= end)
acf77d
+	break;
acf77d
+      else if (buf[cnt] >= maxidx)
2fa04c
       ERROR (gettext ("\
acf77d
 section [%2d] '%s': hash chain reference %" PRIu64 " out of bounds\n"),
acf77d
-	     idx, section_name (ebl, idx), (uint64_t) (cnt - 2 - nbucket));
acf77d
+	       idx, section_name (ebl, idx), (uint64_t) cnt - 2 - nbucket);
acf77d
+    }
acf77d
 }
acf77d
 
acf77d
 
2fa04c
@@ -1990,7 +2038,7 @@ section [%2d] '%s': bitmask size not pow
acf77d
   if (shdr->sh_size < (4 + bitmask_words + nbuckets) * sizeof (Elf32_Word))
acf77d
     {
acf77d
       ERROR (gettext ("\
acf77d
-section [%2d] '%s': hash table section is too small (is %ld, expected at least%ld)\n"),
acf77d
+section [%2d] '%s': hash table section is too small (is %ld, expected at least %ld)\n"),
acf77d
 	     idx, section_name (ebl, idx), (long int) shdr->sh_size,
acf77d
 	     (long int) ((4 + bitmask_words + nbuckets) * sizeof (Elf32_Word)));
acf77d
       return;
2fa04c
@@ -2644,8 +2692,9 @@ section [%2d] '%s' refers in sh_link to 
e39c7b
 
e39c7b
   /* The number of elements in the version symbol table must be the
e39c7b
      same as the number of symbols.  */
e39c7b
-  if (shdr->sh_size / shdr->sh_entsize
e39c7b
-      != symshdr->sh_size / symshdr->sh_entsize)
e39c7b
+  if (shdr->sh_entsize && symshdr->sh_entsize
e39c7b
+      && (shdr->sh_size / shdr->sh_entsize
e39c7b
+	  != symshdr->sh_size / symshdr->sh_entsize))
e39c7b
     ERROR (gettext ("\
e39c7b
 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
e39c7b
 	   idx, section_name (ebl, idx),
2fa04c
@@ -3574,6 +3623,8 @@ phdr[%d]: no note entries defined for th
e39c7b
     return;
e39c7b
 
e39c7b
   char *notemem = gelf_rawchunk (ebl->elf, phdr->p_offset, phdr->p_filesz);
e39c7b
+  if (notemem == NULL)
e39c7b
+    return;
e39c7b
 
e39c7b
   /* ELF64 files often use note section entries in the 32-bit format.
e39c7b
      The p_align field is set to 8 in case the 64-bit format is used.
2fa04c
--- elfutils-0.127/libelf/elf_begin.c.robustify
2fa04c
+++ elfutils-0.127/libelf/elf_begin.c
2fa04c
@@ -155,7 +155,8 @@ get_shnum (void *map_address, unsigned c
e39c7b
 
2fa04c
       if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
2fa04c
 	{
2fa04c
-	  if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
2fa04c
+	  if (unlikely (ehdr.e32->e_shoff >= maxsize)
2fa04c
+	      || unlikely (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize))
2fa04c
 	    /* Cannot read the first section header.  */
2fa04c
 	    return 0;
e39c7b
 
2fa04c
@@ -203,7 +204,8 @@ get_shnum (void *map_address, unsigned c
e39c7b
 
2fa04c
       if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
2fa04c
 	{
2fa04c
-	  if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
2fa04c
+	  if (unlikely (ehdr.e64->e_shoff >= maxsize)
2fa04c
+	      || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
2fa04c
 	    /* Cannot read the first section header.  */
2fa04c
 	    return 0;
e39c7b
 
2fa04c
@@ -275,6 +277,15 @@ file_read_elf (int fildes, void *map_add
2fa04c
     /* Could not determine the number of sections.  */
2fa04c
     return NULL;
e39c7b
 
2fa04c
+  /* Check for too many sections.  */
2fa04c
+  if (e_ident[EI_CLASS] == ELFCLASS32)
2fa04c
+    {
2fa04c
+      if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
2fa04c
+	return NULL;
2fa04c
+    }
2fa04c
+  else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
2fa04c
+    return NULL;
e39c7b
+
2fa04c
   /* We can now allocate the memory.  */
2fa04c
   Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
2fa04c
 			   ELF_K_ELF, scncnt * sizeof (Elf_Scn));
2fa04c
@@ -308,13 +319,31 @@ file_read_elf (int fildes, void *map_add
2fa04c
 	{
2fa04c
 	  /* We can use the mmapped memory.  */
2fa04c
 	  elf->state.elf32.ehdr = ehdr;
2fa04c
+
2fa04c
+	  if (unlikely (ehdr->e_shoff >= maxsize)
2fa04c
+	      || unlikely (ehdr->e_shoff
2fa04c
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
2fa04c
+	    {
2fa04c
+	    free_and_out:
2fa04c
+	      free (elf);
2fa04c
+	      __libelf_seterrno (ELF_E_INVALID_FILE);
2fa04c
+	      return NULL;
2fa04c
+	    }
2fa04c
 	  elf->state.elf32.shdr
2fa04c
 	    = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
2fa04c
+
2fa04c
 	  if (ehdr->e_phnum > 0)
2fa04c
+	    {
2fa04c
 	    /* Assign a value only if there really is a program
2fa04c
 	       header.  Otherwise the value remains NULL.  */
2fa04c
+	      if (unlikely (ehdr->e_phoff >= maxsize)
2fa04c
+		  || unlikely (ehdr->e_phoff
2fa04c
+			       + ehdr->e_phnum
2fa04c
+			       * sizeof (Elf32_Phdr) > maxsize))
2fa04c
+		goto free_and_out;
2fa04c
 	    elf->state.elf32.phdr
2fa04c
 	      = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
2fa04c
+	    }
e39c7b
 
2fa04c
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
2fa04c
 	    {
2fa04c
@@ -383,13 +412,26 @@ file_read_elf (int fildes, void *map_add
2fa04c
 	{
2fa04c
 	  /* We can use the mmapped memory.  */
2fa04c
 	  elf->state.elf64.ehdr = ehdr;
e39c7b
+
2fa04c
+	  if (unlikely (ehdr->e_shoff >= maxsize)
2fa04c
+	      || unlikely (ehdr->e_shoff
2fa04c
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
2fa04c
+	    goto free_and_out;
2fa04c
 	  elf->state.elf64.shdr
2fa04c
 	    = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
2fa04c
+
2fa04c
 	  if (ehdr->e_phnum > 0)
2fa04c
+	    {
2fa04c
 	    /* Assign a value only if there really is a program
2fa04c
 	       header.  Otherwise the value remains NULL.  */
2fa04c
+	      if (unlikely (ehdr->e_phoff >= maxsize)
2fa04c
+		  || unlikely (ehdr->e_phoff
2fa04c
+			       + ehdr->e_phnum
2fa04c
+			       * sizeof (Elf32_Phdr) > maxsize))
2fa04c
+		goto free_and_out;
2fa04c
 	    elf->state.elf64.phdr
2fa04c
 	      = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
2fa04c
+	    }
e39c7b
 
2fa04c
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
2fa04c
 	    {
2fa04c
--- elfutils-0.127/libelf/libelfP.h.robustify
2fa04c
+++ elfutils-0.127/libelf/libelfP.h
2fa04c
@@ -574,4 +574,13 @@ extern uint32_t __libelf_crc32 (uint32_t
2fa04c
       }									      \
2fa04c
   } while (0)
e39c7b
 
2fa04c
+/* Convenience macro.  Assumes int NDX and TYPE with size at least
2fa04c
+   2 bytes.  */
2fa04c
+#if SIZE_MAX > 4294967295U
2fa04c
+# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
2fa04c
+#else
2fa04c
+# define INVALID_NDX(ndx, type) \
2fa04c
+  unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
2fa04c
+#endif
e39c7b
+
2fa04c
 #endif  /* libelfP.h */
2fa04c
--- elfutils-0.127/libelf/gelf_update_move.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_update_move.c
2fa04c
@@ -75,7 +75,7 @@ gelf_update_move (data, ndx, src)
2fa04c
   assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
e39c7b
 
2fa04c
   /* Check whether we have to resize the data buffer.  */
2fa04c
-  if (unlikely (ndx < 0)
2fa04c
+  if (INVALID_NDX (ndx, GElf_Move)
2fa04c
       || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
e39c7b
     {
2fa04c
       __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
--- elfutils-0.127/libelf/gelf_getsym.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_getsym.c
2fa04c
@@ -90,7 +90,8 @@ gelf_getsym (data, ndx, dst)
2fa04c
 	 table entries has to be adopted.  The user better has provided
2fa04c
 	 a buffer where we can store the information.  While copying the
2fa04c
 	 data we are converting the format.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Sym)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
@@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
e39c7b
 
2fa04c
       /* The data is already in the correct form.  Just make sure the
2fa04c
 	 index is OK.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
2fa04c
+      if (INVALID_NDX (ndx, GElf_Sym)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
--- elfutils-0.127/libelf/gelf_getrela.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_getrela.c
2fa04c
@@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
2fa04c
   if (data_scn == NULL)
2fa04c
     return NULL;
e39c7b
 
2fa04c
-  if (unlikely (ndx < 0))
2fa04c
-    {
2fa04c
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
-      return NULL;
2fa04c
-    }
2fa04c
-
2fa04c
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
2fa04c
     {
2fa04c
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
2fa04c
@@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
2fa04c
   if (scn->elf->class == ELFCLASS32)
2fa04c
     {
2fa04c
       /* We have to convert the data.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Rela)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
e39c7b
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  result = NULL;
2fa04c
@@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
2fa04c
     {
2fa04c
       /* Simply copy the data after we made sure we are actually getting
2fa04c
 	 correct data.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf64_Rela)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  result = NULL;
2fa04c
--- elfutils-0.127/libelf/gelf_getmove.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_getmove.c
2fa04c
@@ -83,7 +83,8 @@ gelf_getmove (data, ndx, dst)
e39c7b
 
2fa04c
   /* The data is already in the correct form.  Just make sure the
2fa04c
      index is OK.  */
2fa04c
-  if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
2fa04c
+  if (INVALID_NDX (ndx, GElf_Move)
2fa04c
+      || unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
2fa04c
     {
2fa04c
       __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
       goto out;
2fa04c
--- elfutils-0.127/libelf/gelf_update_symshndx.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_update_symshndx.c
2fa04c
@@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata
2fa04c
   if (symdata == NULL)
2fa04c
     return 0;
e39c7b
 
2fa04c
-  if (unlikely (ndx < 0))
2fa04c
-    {
2fa04c
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
-      return 0;
2fa04c
-    }
2fa04c
-
2fa04c
   if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
2fa04c
     {
2fa04c
       /* The type of the data better should match.  */
2fa04c
@@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
e39c7b
 	}
e39c7b
 
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Sym)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
e39c7b
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
@@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
2fa04c
   else
2fa04c
     {
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf64_Sym)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
--- elfutils-0.127/libelf/gelf_update_dyn.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_update_dyn.c
2fa04c
@@ -71,12 +71,6 @@ gelf_update_dyn (data, ndx, src)
2fa04c
   if (data == NULL)
2fa04c
     return 0;
2fa04c
 
2fa04c
-  if (unlikely (ndx < 0))
2fa04c
-    {
2fa04c
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
-      return 0;
2fa04c
-    }
2fa04c
-
2fa04c
   if (unlikely (data_scn->d.d_type != ELF_T_DYN))
2fa04c
     {
2fa04c
       /* The type of the data better should match.  */
2fa04c
@@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
e39c7b
 	}
e39c7b
 
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Dyn)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
e39c7b
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
@@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
2fa04c
   else
2fa04c
     {
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf64_Dyn)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
--- elfutils-0.127/libelf/gelf_update_rela.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_update_rela.c
2fa04c
@@ -68,12 +68,6 @@ gelf_update_rela (Elf_Data *dst, int ndx
2fa04c
   if (dst == NULL)
2fa04c
     return 0;
e39c7b
 
2fa04c
-  if (unlikely (ndx < 0))
2fa04c
-    {
2fa04c
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
-      return 0;
2fa04c
-    }
2fa04c
-
2fa04c
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
2fa04c
     {
2fa04c
       /* The type of the data better should match.  */
2fa04c
@@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
2fa04c
 	}
e39c7b
 
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Rela)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
@@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
2fa04c
   else
2fa04c
     {
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf64_Rela)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
--- elfutils-0.127/libelf/gelf_getsymshndx.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_getsymshndx.c
2fa04c
@@ -90,7 +90,9 @@ gelf_getsymshndx (symdata, shndxdata, nd
2fa04c
      section index table.  */
2fa04c
   if (likely (shndxdata_scn != NULL))
2fa04c
     {
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Word)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Word)
2fa04c
+		       > shndxdata_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
@@ -110,7 +112,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
2fa04c
 	 table entries has to be adopted.  The user better has provided
2fa04c
 	 a buffer where we can store the information.  While copying the
2fa04c
 	 data we are converting the format.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Sym)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
@@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
e39c7b
 
2fa04c
       /* The data is already in the correct form.  Just make sure the
2fa04c
 	 index is OK.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
2fa04c
+      if (INVALID_NDX (ndx, GElf_Sym)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
--- elfutils-0.127/libelf/elf32_newphdr.c.robustify
2fa04c
+++ elfutils-0.127/libelf/elf32_newphdr.c
2fa04c
@@ -124,6 +124,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
2fa04c
   else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
2fa04c
 	   || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
2fa04c
     {
2fa04c
+      if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
2fa04c
+	{
2fa04c
+	  result = NULL;
2fa04c
+	  goto out;
2fa04c
+	}
e39c7b
+
2fa04c
       /* Allocate a new program header with the appropriate number of
2fa04c
 	 elements.  */
2fa04c
       result = (ElfW2(LIBELFBITS,Phdr) *)
2fa04c
--- elfutils-0.127/libelf/gelf_update_sym.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_update_sym.c
2fa04c
@@ -72,12 +72,6 @@ gelf_update_sym (data, ndx, src)
2fa04c
   if (data == NULL)
2fa04c
     return 0;
e39c7b
 
2fa04c
-  if (unlikely (ndx < 0))
2fa04c
-    {
2fa04c
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
-      return 0;
2fa04c
-    }
2fa04c
-
2fa04c
   if (unlikely (data_scn->d.d_type != ELF_T_SYM))
2fa04c
     {
2fa04c
       /* The type of the data better should match.  */
2fa04c
@@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
2fa04c
 	}
e39c7b
 
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Sym)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
@@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
2fa04c
   else
2fa04c
     {
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf64_Sym)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
--- elfutils-0.127/libelf/gelf_getsyminfo.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_getsyminfo.c
2fa04c
@@ -84,7 +84,8 @@ gelf_getsyminfo (data, ndx, dst)
e39c7b
 
2fa04c
   /* The data is already in the correct form.  Just make sure the
2fa04c
      index is OK.  */
2fa04c
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
2fa04c
+  if (INVALID_NDX (ndx, GElf_Syminfo)
2fa04c
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
2fa04c
     {
2fa04c
       __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
       goto out;
2fa04c
--- elfutils-0.127/libelf/gelf_getlib.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_getlib.c
2fa04c
@@ -86,7 +86,8 @@ gelf_getlib (data, ndx, dst)
2fa04c
   /* The data is already in the correct form.  Just make sure the
2fa04c
      index is OK.  */
2fa04c
   GElf_Lib *result = NULL;
2fa04c
-  if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
2fa04c
+  if (INVALID_NDX (ndx, GElf_Lib)
2fa04c
+      || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
2fa04c
     __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
   else
2fa04c
     {
2fa04c
--- elfutils-0.127/libelf/elf32_updatefile.c.robustify
2fa04c
+++ elfutils-0.127/libelf/elf32_updatefile.c
2fa04c
@@ -201,6 +201,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf
2fa04c
   /* Write all the sections.  Well, only those which are modified.  */
2fa04c
   if (shnum > 0)
2fa04c
     {
2fa04c
+      if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
2fa04c
+ 	return 1;
2fa04c
+
2fa04c
       Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
2fa04c
       Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
2fa04c
       char *const shdr_start = ((char *) elf->map_address + elf->start_offset
2fa04c
@@ -571,6 +574,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
2fa04c
   /* Write all the sections.  Well, only those which are modified.  */
2fa04c
   if (shnum > 0)
2fa04c
     {
2fa04c
+      if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
2fa04c
+					+ sizeof (ElfW2(LIBELFBITS,Shdr)))))
2fa04c
+	return 1;
2fa04c
+
2fa04c
       off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
2fa04c
 #if EV_NUM != 2
2fa04c
       xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
2fa04c
--- elfutils-0.127/libelf/gelf_getversym.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_getversym.c
2fa04c
@@ -92,7 +92,8 @@ gelf_getversym (data, ndx, dst)
e39c7b
 
2fa04c
   /* The data is already in the correct form.  Just make sure the
2fa04c
      index is OK.  */
2fa04c
-  if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
2fa04c
+  if (INVALID_NDX (ndx, GElf_Versym)
2fa04c
+      || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
2fa04c
     {
2fa04c
       __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
       result = NULL;
2fa04c
--- elfutils-0.127/libelf/elf_getarsym.c.robustify
2fa04c
+++ elfutils-0.127/libelf/elf_getarsym.c
2fa04c
@@ -179,6 +179,9 @@ elf_getarsym (elf, ptr)
2fa04c
       size_t index_size = atol (tmpbuf);
e39c7b
 
2fa04c
       if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
2fa04c
+#if SIZE_MAX <= 4294967295U
2fa04c
+	  || n >= SIZE_MAX / sizeof (Elf_Arsym)
2fa04c
+#endif
2fa04c
 	  || n * sizeof (uint32_t) > index_size)
2fa04c
 	{
2fa04c
 	  /* This index table cannot be right since it does not fit into
2fa04c
--- elfutils-0.127/libelf/gelf_getrel.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_getrel.c
2fa04c
@@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
2fa04c
   if (data_scn == NULL)
2fa04c
     return NULL;
e39c7b
 
2fa04c
-  if (unlikely (ndx < 0))
2fa04c
-    {
2fa04c
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
-      return NULL;
2fa04c
-    }
2fa04c
-
2fa04c
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
2fa04c
     {
2fa04c
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
2fa04c
@@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
2fa04c
   if (scn->elf->class == ELFCLASS32)
2fa04c
     {
2fa04c
       /* We have to convert the data.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Rel)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  result = NULL;
2fa04c
@@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
2fa04c
     {
2fa04c
       /* Simply copy the data after we made sure we are actually getting
2fa04c
 	 correct data.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf64_Rel)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  result = NULL;
2fa04c
--- elfutils-0.127/libelf/gelf_update_versym.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_update_versym.c
2fa04c
@@ -75,7 +75,7 @@ gelf_update_versym (data, ndx, src)
2fa04c
   assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
e39c7b
 
2fa04c
   /* Check whether we have to resize the data buffer.  */
2fa04c
-  if (unlikely (ndx < 0)
2fa04c
+  if (INVALID_NDX (ndx, GElf_Versym)
2fa04c
       || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
2fa04c
     {
2fa04c
       __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
--- elfutils-0.127/libelf/gelf_getdyn.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_getdyn.c
2fa04c
@@ -93,7 +93,8 @@ gelf_getdyn (data, ndx, dst)
2fa04c
 	 table entries has to be adopted.  The user better has provided
2fa04c
 	 a buffer where we can store the information.  While copying the
2fa04c
 	 data we are converting the format.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Dyn)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
@@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
e39c7b
 
2fa04c
       /* The data is already in the correct form.  Just make sure the
2fa04c
 	 index is OK.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, GElf_Dyn)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
--- elfutils-0.127/libelf/gelf_update_syminfo.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_update_syminfo.c
2fa04c
@@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
2fa04c
   if (data == NULL)
2fa04c
     return 0;
e39c7b
 
2fa04c
-  if (unlikely (ndx < 0))
2fa04c
-    {
2fa04c
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
-      return 0;
2fa04c
-    }
2fa04c
-
2fa04c
   if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
2fa04c
     {
2fa04c
       /* The type of the data better should match.  */
2fa04c
@@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
2fa04c
   rwlock_wrlock (scn->elf->lock);
e39c7b
 
2fa04c
   /* Check whether we have to resize the data buffer.  */
2fa04c
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
2fa04c
+  if (INVALID_NDX (ndx, GElf_Syminfo)
2fa04c
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
2fa04c
     {
2fa04c
       __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
       goto out;
2fa04c
--- elfutils-0.127/libelf/gelf_update_rel.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_update_rel.c
2fa04c
@@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx,
2fa04c
   if (dst == NULL)
2fa04c
     return 0;
e39c7b
 
2fa04c
-  if (unlikely (ndx < 0))
2fa04c
-    {
2fa04c
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
-      return 0;
2fa04c
-    }
2fa04c
-
2fa04c
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
2fa04c
     {
2fa04c
       /* The type of the data better should match.  */
2fa04c
@@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
2fa04c
 	}
e39c7b
 
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf32_Rel)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
@@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
2fa04c
   else
2fa04c
     {
2fa04c
       /* Check whether we have to resize the data buffer.  */
2fa04c
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
2fa04c
+      if (INVALID_NDX (ndx, Elf64_Rel)
2fa04c
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
2fa04c
 	{
2fa04c
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
 	  goto out;
2fa04c
--- elfutils-0.127/libelf/elf_newscn.c.robustify
2fa04c
+++ elfutils-0.127/libelf/elf_newscn.c
2fa04c
@@ -104,10 +104,18 @@ elf_newscn (elf)
2fa04c
   else
2fa04c
     {
2fa04c
       /* We must allocate a new element.  */
2fa04c
-      Elf_ScnList *newp;
2fa04c
+      Elf_ScnList *newp = NULL;
e39c7b
 
2fa04c
       assert (elf->state.elf.scnincr > 0);
e39c7b
 
2fa04c
+      if (
2fa04c
+#if SIZE_MAX <= 4294967295U
2fa04c
+	  likely (elf->state.elf.scnincr
2fa04c
+		  < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
2fa04c
+#else
2fa04c
+	  1
2fa04c
+#endif
2fa04c
+	  )
2fa04c
       newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
2fa04c
 				     + ((elf->state.elf.scnincr *= 2)
2fa04c
 					* sizeof (Elf_Scn)), 1);
2fa04c
--- elfutils-0.127/libelf/gelf_update_lib.c.robustify
2fa04c
+++ elfutils-0.127/libelf/gelf_update_lib.c
2fa04c
@@ -68,12 +68,6 @@ gelf_update_lib (data, ndx, src)
2fa04c
   if (data == NULL)
2fa04c
     return 0;
e39c7b
 
2fa04c
-  if (unlikely (ndx < 0))
2fa04c
-    {
2fa04c
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
-      return 0;
2fa04c
-    }
2fa04c
-
2fa04c
   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
2fa04c
   if (unlikely (data_scn->d.d_type != ELF_T_LIB))
2fa04c
     {
2fa04c
@@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
2fa04c
 
2fa04c
   /* Check whether we have to resize the data buffer.  */
2fa04c
   int result = 0;
2fa04c
-  if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
2fa04c
+  if (INVALID_NDX (ndx, Elf64_Lib)
2fa04c
+      || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
2fa04c
     __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c
   else
2fa04c
     {
2fa04c
--- elfutils-0.127/libelf/elf_getshstrndx.c.robustify
2fa04c
+++ elfutils-0.127/libelf/elf_getshstrndx.c
2fa04c
@@ -125,10 +125,25 @@ elf_getshstrndx (elf, dst)
2fa04c
 	      if (elf->map_address != NULL
2fa04c
 		  && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
2fa04c
 		  && (ALLOW_UNALIGNED
2fa04c
-		      || (((size_t) ((char *) elf->map_address + offset))
2fa04c
+		      || (((size_t) ((char *) elf->map_address
2fa04c
+			   + elf->start_offset + offset))
2fa04c
 			  & (__alignof__ (Elf32_Shdr) - 1)) == 0))
2fa04c
+		{
2fa04c
+		  /* First see whether the information in the ELF header is
2fa04c
+		     valid and it does not ask for too much.  */
2fa04c
+		  if (unlikely (offset + sizeof (Elf32_Shdr)
2fa04c
+				> elf->maximum_size))
2fa04c
+		    {
2fa04c
+		      /* Something is wrong.  */
2fa04c
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
2fa04c
+		      result = -1;
2fa04c
+		      goto out;
2fa04c
+		    }
2fa04c
+
2fa04c
 		/* We can directly access the memory.  */
2fa04c
-		num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
2fa04c
+		  num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
2fa04c
+					 + offset))->sh_link;
2fa04c
+		}
e39c7b
 	      else
e39c7b
 		{
2fa04c
 		  /* We avoid reading in all the section headers.  Just read
2fa04c
@@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
2fa04c
 	      if (elf->map_address != NULL
2fa04c
 		  && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
2fa04c
 		  && (ALLOW_UNALIGNED
2fa04c
-		      || (((size_t) ((char *) elf->map_address + offset))
2fa04c
+		      || (((size_t) ((char *) elf->map_address
2fa04c
+			   + elf->start_offset + offset))
2fa04c
 			  & (__alignof__ (Elf64_Shdr) - 1)) == 0))
2fa04c
+		{
2fa04c
+		  /* First see whether the information in the ELF header is
2fa04c
+		     valid and it does not ask for too much.  */
2fa04c
+		  if (unlikely (offset + sizeof (Elf64_Shdr)
2fa04c
+				> elf->maximum_size))
2fa04c
+		    {
2fa04c
+		      /* Something is wrong.  */
2fa04c
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
2fa04c
+		      result = -1;
2fa04c
+		      goto out;
2fa04c
+		    }
2fa04c
+
2fa04c
 		/* We can directly access the memory.  */
2fa04c
-		num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
2fa04c
+		  num = ((Elf64_Shdr *) (elf->map_address
2fa04c
+			 + elf->start_offset + offset))->sh_link;
2fa04c
+		}
2fa04c
 	      else
2fa04c
 		{
2fa04c
 		  /* We avoid reading in all the section headers.  Just read
2fa04c
--- elfutils-0.127/libelf/elf32_getshdr.c.robustify
2fa04c
+++ elfutils-0.127/libelf/elf32_getshdr.c
2fa04c
@@ -101,7 +101,8 @@ elfw2(LIBELFBITS,getshdr) (scn)
2fa04c
 	goto out;
e39c7b
 
2fa04c
       size_t shnum;
2fa04c
-      if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
2fa04c
+      if (INTUSE (elf_getshnum) (elf, &shnum) != 0
2fa04c
+	  || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
2fa04c
 	goto out;
2fa04c
       size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
e39c7b
 
2fa04c
@@ -118,6 +119,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
e39c7b
 
2fa04c
       if (elf->map_address != NULL)
2fa04c
 	{
2fa04c
+	  /* First see whether the information in the ELF header is
2fa04c
+	     valid and it does not ask for too much.  */
2fa04c
+	  if (unlikely (ehdr->e_shoff >= elf->maximum_size)
2fa04c
+	      || unlikely (ehdr->e_shoff + size > elf->maximum_size))
2fa04c
+	    {
2fa04c
+	      /* Something is wrong.  */
2fa04c
+	      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
2fa04c
+	      goto free_and_out;
2fa04c
+	    }
2fa04c
+
2fa04c
 	  ElfW2(LIBELFBITS,Shdr) *notcvt;
2fa04c
 
2fa04c
 	  /* All the data is already mapped.  If we could use it
2fa04c
--- elfutils-0.127/libelf/elf32_getphdr.c.robustify
2fa04c
+++ elfutils-0.127/libelf/elf32_getphdr.c
2fa04c
@@ -116,6 +116,16 @@ elfw2(LIBELFBITS,getphdr) (elf)
2fa04c
 
2fa04c
       if (elf->map_address != NULL)
2fa04c
 	{
2fa04c
+	  /* First see whether the information in the ELF header is
2fa04c
+	     valid and it does not ask for too much.  */
2fa04c
+	  if (unlikely (ehdr->e_phoff >= elf->maximum_size)
2fa04c
+	      || unlikely (ehdr->e_phoff + size > elf->maximum_size))
2fa04c
+	    {
2fa04c
+	      /* Something is wrong.  */
2fa04c
+	      __libelf_seterrno (ELF_E_INVALID_PHDR);
2fa04c
+	      goto out;
2fa04c
+	    }
2fa04c
+
2fa04c
 	  /* All the data is already mapped.  Use it.  */
2fa04c
 	  void *file_phdr = ((char *) elf->map_address
2fa04c
 			     + elf->start_offset + ehdr->e_phoff);