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