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
840723d
--- elfutils-0.132/src/elflint.c.robustify
840723d
+++ elfutils-0.132/src/elflint.c
840723d
@@ -126,6 +126,9 @@ static uint32_t shstrndx;
840723d
 /* Array to count references in section groups.  */
840723d
 static int *scnref;
840723d
 
840723d
+/* Number of sections.  */
840723d
+static unsigned int shnum;
840723d
+
840723d
 
840723d
 int
840723d
 main (int argc, char *argv[])
840723d
@@ -315,10 +318,19 @@ section_name (Ebl *ebl, int idx)
840723d
 {
840723d
   GElf_Shdr shdr_mem;
840723d
   GElf_Shdr *shdr;
840723d
+  const char *ret;
840723d
+
840723d
+  if ((unsigned int) idx > shnum)
840723d
+    return "<invalid>";
840723d
 
840723d
   shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
840723d
+  if (shdr == NULL)
840723d
+    return "<invalid>";
840723d
 
840723d
-  return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
840723d
+  ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
840723d
+  if (ret == NULL)
840723d
+    return "<invalid>";
840723d
+  return ret;
840723d
 }
840723d
 
840723d
 
840723d
@@ -340,10 +352,6 @@ static const int valid_e_machine[] =
840723d
   (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
840723d
 
840723d
 
840723d
-/* Number of sections.  */
840723d
-static unsigned int shnum;
840723d
-
840723d
-
840723d
 static void
840723d
 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
840723d
 {
840723d
@@ -605,7 +613,8 @@ section [%2d] '%s': symbol table cannot 
840723d
 	  }
840723d
       }
840723d
 
840723d
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
840723d
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
840723d
+  if (shdr->sh_entsize != sh_entsize)
840723d
     ERROR (gettext ("\
840723d
 section [%2u] '%s': entry size is does not match ElfXX_Sym\n"),
840723d
 	   idx, section_name (ebl, idx));
840723d
@@ -643,7 +652,7 @@ section [%2d] '%s': XINDEX for zeroth en
840723d
 	       xndxscnidx, section_name (ebl, xndxscnidx));
840723d
     }
840723d
 
840723d
-  for (size_t cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
840723d
+  for (size_t cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
840723d
     {
840723d
       sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
840723d
       if (sym == NULL)
840723d
@@ -661,7 +670,8 @@ section [%2d] '%s': symbol %zu: invalid 
840723d
       else
840723d
 	{
840723d
 	  name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
840723d
-	  assert (name != NULL);
840723d
+	  assert (name != NULL
840723d
+		  || strshdr->sh_type != SHT_STRTAB);
840723d
 	}
840723d
 
840723d
       if (sym->st_shndx == SHN_XINDEX)
840723d
@@ -991,9 +1001,11 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
840723d
     {
840723d
       GElf_Shdr rcshdr_mem;
840723d
       const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
840723d
-      assert (rcshdr != NULL);
840723d
 
840723d
-      if (rcshdr->sh_type == SHT_DYNAMIC)
840723d
+      if (rcshdr == NULL)
840723d
+	break;
840723d
+
840723d
+      if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
840723d
 	{
840723d
 	  /* Found the dynamic section.  Look through it.  */
840723d
 	  Elf_Data *d = elf_getdata (scn, NULL);
840723d
@@ -1003,7 +1015,9 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
840723d
 	    {
840723d
 	      GElf_Dyn dyn_mem;
840723d
 	      GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
840723d
-	      assert (dyn != NULL);
840723d
+
840723d
+	      if (dyn == NULL)
840723d
+		break;
840723d
 
840723d
 	      if (dyn->d_tag == DT_RELCOUNT)
840723d
 		{
840723d
@@ -1017,7 +1031,9 @@ section [%2d] '%s': DT_RELCOUNT used for
840723d
 		      /* Does the number specified number of relative
840723d
 			 relocations exceed the total number of
840723d
 			 relocations?  */
840723d
-		      if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
840723d
+		      if (shdr->sh_entsize != 0
840723d
+			  && dyn->d_un.d_val > (shdr->sh_size
840723d
+						/ shdr->sh_entsize))
840723d
 			ERROR (gettext ("\
840723d
 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
840723d
 			       idx, section_name (ebl, idx),
840723d
@@ -1177,7 +1193,8 @@ section [%2d] '%s': no relocations for m
840723d
 	}
840723d
     }
840723d
 
840723d
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
840723d
+  size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
840723d
+  if (shdr->sh_entsize != sh_entsize)
840723d
     ERROR (gettext (reltype == ELF_T_RELA ? "\
840723d
 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
840723d
 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
840723d
@@ -1400,7 +1417,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
840723d
   Elf_Data *symdata = elf_getdata (symscn, NULL);
840723d
   enum load_state state = state_undecided;
840723d
 
840723d
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
840723d
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
840723d
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
840723d
     {
840723d
       GElf_Rela rela_mem;
840723d
       GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
840723d
@@ -1450,7 +1468,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
840723d
   Elf_Data *symdata = elf_getdata (symscn, NULL);
840723d
   enum load_state state = state_undecided;
840723d
 
840723d
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
840723d
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
840723d
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
840723d
     {
840723d
       GElf_Rel rel_mem;
840723d
       GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
840723d
@@ -1553,7 +1572,8 @@ section [%2d] '%s': referenced as string
840723d
 	   shdr->sh_link, section_name (ebl, shdr->sh_link),
840723d
 	   idx, section_name (ebl, idx));
840723d
 
840723d
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
840723d
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
840723d
+  if (shdr->sh_entsize != sh_entsize)
840723d
     ERROR (gettext ("\
840723d
 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
840723d
 	   idx, section_name (ebl, idx));
840723d
@@ -1563,7 +1583,7 @@ section [%2d] '%s': section entry size d
840723d
 	   idx, section_name (ebl, idx));
840723d
 
840723d
   bool non_null_warned = false;
840723d
-  for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
840723d
+  for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
840723d
     {
840723d
       GElf_Dyn dyn_mem;
840723d
       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
840723d
@@ -1844,6 +1864,8 @@ section [%2d] '%s': entry size does not 
840723d
 	   idx, section_name (ebl, idx));
840723d
 
840723d
   if (symshdr != NULL
840723d
+      && shdr->sh_entsize
840723d
+      && symshdr->sh_entsize
840723d
       && (shdr->sh_size / shdr->sh_entsize
840723d
 	  < symshdr->sh_size / symshdr->sh_entsize))
840723d
     ERROR (gettext ("\
840723d
@@ -1870,6 +1892,12 @@ section [%2d] '%s': extended section ind
840723d
     }
840723d
 
840723d
   Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
840723d
+  if (data == NULL)
840723d
+    {
840723d
+      ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
840723d
+ 	     idx, section_name (ebl, idx));
840723d
+      return;
840723d
+    }
840723d
 
840723d
   if (*((Elf32_Word *) data->d_buf) != 0)
840723d
     ERROR (gettext ("symbol 0 should have zero extended section index\n"));
840723d
@@ -1912,7 +1940,7 @@ section [%2d] '%s': hash table section i
840723d
 
840723d
   size_t maxidx = nchain;
840723d
 
840723d
-  if (symshdr != NULL)
840723d
+  if (symshdr != NULL && symshdr->sh_entsize != 0)
840723d
     {
840723d
       size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
840723d
 
840723d
@@ -1923,18 +1951,28 @@ section [%2d] '%s': hash table section i
840723d
       maxidx = symsize;
840723d
     }
840723d
 
840723d
+  Elf32_Word *buf = (Elf32_Word *) data->d_buf;
840723d
+  Elf32_Word *end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
840723d
   size_t cnt;
840723d
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
840723d
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
840723d
+    {
840723d
+      if (buf + cnt >= end)
840723d
+	break;
840723d
+      else if (buf[cnt] >= maxidx)
840723d
       ERROR (gettext ("\
840723d
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
840723d
 	     idx, section_name (ebl, idx), cnt - 2);
840723d
+    }
840723d
 
840723d
   for (; cnt < 2 + nbucket + nchain; ++cnt)
840723d
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
840723d
+    {
840723d
+      if (buf + cnt >= end)
840723d
+	break;
840723d
+      else if (buf[cnt] >= maxidx)
840723d
       ERROR (gettext ("\
840723d
 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
840723d
 	     idx, section_name (ebl, idx), cnt - 2 - nbucket);
840723d
+    }
840723d
 }
840723d
 
840723d
 
840723d
@@ -1964,18 +2002,28 @@ section [%2d] '%s': hash table section i
840723d
       maxidx = symsize;
840723d
     }
840723d
 
840723d
+  Elf64_Xword *buf = (Elf64_Xword *) data->d_buf;
840723d
+  Elf64_Xword *end = (Elf64_Xword *) ((char *) data->d_buf + shdr->sh_size);
840723d
   size_t cnt;
840723d
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
840723d
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
840723d
+    {
840723d
+      if (buf + cnt >= end)
840723d
+	break;
840723d
+      else if (buf[cnt] >= maxidx)
840723d
       ERROR (gettext ("\
840723d
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
840723d
 	     idx, section_name (ebl, idx), cnt - 2);
840723d
+    }
840723d
 
840723d
   for (; cnt < 2 + nbucket + nchain; ++cnt)
840723d
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
840723d
+    {
840723d
+      if (buf + cnt >= end)
840723d
+	break;
840723d
+      else if (buf[cnt] >= maxidx)
840723d
       ERROR (gettext ("\
840723d
 section [%2d] '%s': hash chain reference %" PRIu64 " out of bounds\n"),
840723d
-	     idx, section_name (ebl, idx), (uint64_t) (cnt - 2 - nbucket));
840723d
+	       idx, section_name (ebl, idx), (uint64_t) cnt - 2 - nbucket);
840723d
+    }
840723d
 }
840723d
 
840723d
 
840723d
@@ -2000,7 +2048,7 @@ section [%2d] '%s': bitmask size not pow
840723d
   if (shdr->sh_size < (4 + bitmask_words + nbuckets) * sizeof (Elf32_Word))
840723d
     {
840723d
       ERROR (gettext ("\
840723d
-section [%2d] '%s': hash table section is too small (is %ld, expected at least%ld)\n"),
840723d
+section [%2d] '%s': hash table section is too small (is %ld, expected at least %ld)\n"),
840723d
 	     idx, section_name (ebl, idx), (long int) shdr->sh_size,
840723d
 	     (long int) ((4 + bitmask_words + nbuckets) * sizeof (Elf32_Word)));
840723d
       return;
840723d
@@ -2657,8 +2705,9 @@ section [%2d] '%s' refers in sh_link to 
840723d
 
840723d
   /* The number of elements in the version symbol table must be the
840723d
      same as the number of symbols.  */
840723d
-  if (shdr->sh_size / shdr->sh_entsize
840723d
-      != symshdr->sh_size / symshdr->sh_entsize)
840723d
+  if (shdr->sh_entsize && symshdr->sh_entsize
840723d
+      && (shdr->sh_size / shdr->sh_entsize
840723d
+	  != symshdr->sh_size / symshdr->sh_entsize))
840723d
     ERROR (gettext ("\
840723d
 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
840723d
 	   idx, section_name (ebl, idx),
840723d
--- elfutils-0.132/src/readelf.c.robustify
840723d
+++ elfutils-0.132/src/readelf.c
840723d
@@ -1107,6 +1107,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",
840723d
@@ -1119,8 +1121,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
 
840723d
@@ -1271,7 +1273,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;
840723d
@@ -1286,6 +1289,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
 		    "\
840723d
@@ -1295,9 +1303,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)
840723d
@@ -1797,6 +1803,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)
840723d
@@ -1807,15 +1820,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 ("\
840723d
@@ -2051,7 +2061,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
 		    "\
840723d
@@ -2062,9 +2078,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; )
840723d
@@ -2117,8 +2131,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
 		    "\
840723d
@@ -2130,9 +2150,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; )
840723d
@@ -2394,8 +2412,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
 		    "\
840723d
@@ -2407,9 +2431,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)
840723d
@@ -2461,7 +2483,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
 		    "\
840723d
@@ -2474,9 +2506,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);
840723d
@@ -3875,6 +3905,16 @@ print_debug_aranges_section (Dwfl_Module
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
 		    "\
840723d
--- elfutils-0.132/src/strip.c.robustify
840723d
+++ elfutils-0.132/src/strip.c
840723d
@@ -543,6 +543,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.
840723d
@@ -564,7 +569,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
 
840723d
@@ -577,6 +582,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
 	}
840723d
@@ -586,6 +592,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
840723d
@@ -598,7 +606,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))
840723d
@@ -615,7 +623,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
840723d
@@ -626,7 +639,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
 
840723d
@@ -634,7 +647,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
 	    {
840723d
@@ -709,11 +722,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
 	    {
840723d
@@ -738,7 +755,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,
840723d
@@ -778,6 +795,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.  */
840723d
@@ -809,12 +829,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;
840723d
@@ -914,7 +938,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);
840723d
@@ -951,7 +975,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)
840723d
@@ -1007,7 +1031,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.  */
840723d
@@ -1097,20 +1121,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
 
840723d
@@ -1165,7 +1189,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
 		      }
840723d
@@ -1186,7 +1210,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))
840723d
@@ -1209,7 +1233,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)
840723d
@@ -1388,11 +1412,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)
840723d
@@ -1411,7 +1435,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;
840723d
@@ -1442,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);
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)
840723d
--- elfutils-0.132/libelf/gelf_getdyn.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_getdyn.c
840723d
@@ -93,7 +93,8 @@ gelf_getdyn (data, ndx, dst)
840723d
 	 table entries has to be adopted.  The user better has provided
840723d
 	 a buffer where we can store the information.  While copying the
840723d
 	 data we are converting the format.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf32_Dyn)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
bf5b25b
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  goto out;
840723d
@@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
bf5b25b
 
840723d
       /* The data is already in the correct form.  Just make sure the
840723d
 	 index is OK.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, GElf_Dyn)
840723d
+	  || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
bf5b25b
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  goto out;
840723d
--- elfutils-0.132/libelf/gelf_getrel.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_getrel.c
840723d
@@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
840723d
   if (data_scn == NULL)
840723d
     return NULL;
bf5b25b
 
840723d
-  if (unlikely (ndx < 0))
840723d
-    {
840723d
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
-      return NULL;
840723d
-    }
840723d
-
840723d
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
bf5b25b
     {
840723d
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
840723d
@@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
840723d
   if (scn->elf->class == ELFCLASS32)
bf5b25b
     {
840723d
       /* We have to convert the data.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf32_Rel)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
840723d
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  result = NULL;
840723d
@@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
bf5b25b
     {
840723d
       /* Simply copy the data after we made sure we are actually getting
840723d
 	 correct data.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf64_Rel)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
840723d
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  result = NULL;
840723d
--- elfutils-0.132/libelf/gelf_getversym.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_getversym.c
840723d
@@ -92,7 +92,8 @@ gelf_getversym (data, ndx, dst)
bf5b25b
 
840723d
   /* The data is already in the correct form.  Just make sure the
840723d
      index is OK.  */
840723d
-  if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
840723d
+  if (INVALID_NDX (ndx, GElf_Versym)
840723d
+      || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
bf5b25b
     {
840723d
       __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
       result = NULL;
840723d
--- elfutils-0.132/libelf/elf_newscn.c.robustify
840723d
+++ elfutils-0.132/libelf/elf_newscn.c
840723d
@@ -104,10 +104,18 @@ elf_newscn (elf)
840723d
   else
e76067d
     {
840723d
       /* We must allocate a new element.  */
840723d
-      Elf_ScnList *newp;
840723d
+      Elf_ScnList *newp = NULL;
bf5b25b
 
840723d
       assert (elf->state.elf.scnincr > 0);
bf5b25b
 
840723d
+      if (
840723d
+#if SIZE_MAX <= 4294967295U
840723d
+	  likely (elf->state.elf.scnincr
840723d
+		  < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
840723d
+#else
840723d
+	  1
840723d
+#endif
840723d
+	  )
840723d
       newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
840723d
 				     + ((elf->state.elf.scnincr *= 2)
840723d
 					* sizeof (Elf_Scn)), 1);
840723d
--- elfutils-0.132/libelf/elf32_getshdr.c.robustify
840723d
+++ elfutils-0.132/libelf/elf32_getshdr.c
840723d
@@ -101,7 +101,8 @@ elfw2(LIBELFBITS,getshdr) (scn)
840723d
 	goto out;
bf5b25b
 
840723d
       size_t shnum;
840723d
-      if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
840723d
+      if (INTUSE (elf_getshnum) (elf, &shnum) != 0
840723d
+	  || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
840723d
 	goto out;
840723d
       size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
bf5b25b
 
840723d
@@ -118,6 +119,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
840723d
 
840723d
       if (elf->map_address != NULL)
840723d
 	{
840723d
+	  /* First see whether the information in the ELF header is
840723d
+	     valid and it does not ask for too much.  */
840723d
+	  if (unlikely (ehdr->e_shoff >= elf->maximum_size)
840723d
+	      || unlikely (ehdr->e_shoff + size > elf->maximum_size))
840723d
+	    {
840723d
+	      /* Something is wrong.  */
840723d
+	      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
840723d
+	      goto free_and_out;
840723d
+	    }
bf5b25b
+
840723d
 	  ElfW2(LIBELFBITS,Shdr) *notcvt;
bf5b25b
 
840723d
 	  /* All the data is already mapped.  If we could use it
840723d
--- elfutils-0.132/libelf/gelf_getsymshndx.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_getsymshndx.c
840723d
@@ -90,7 +90,9 @@ gelf_getsymshndx (symdata, shndxdata, nd
840723d
      section index table.  */
840723d
   if (likely (shndxdata_scn != NULL))
bf5b25b
     {
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf32_Word)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Word)
840723d
+		       > shndxdata_scn->d.d_size))
840723d
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  goto out;
840723d
@@ -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.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
6b412c1
+      if (INVALID_NDX (ndx, Elf32_Sym)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
6b412c1
 	{
6b412c1
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
6b412c1
 	  goto out;
840723d
@@ -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.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
6b412c1
+      if (INVALID_NDX (ndx, GElf_Sym)
840723d
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
6b412c1
 	{
6b412c1
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
6b412c1
 	  goto out;
840723d
--- elfutils-0.132/libelf/gelf_update_versym.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_update_versym.c
840723d
@@ -75,7 +75,7 @@ gelf_update_versym (data, ndx, src)
840723d
   assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
bf5b25b
 
840723d
   /* Check whether we have to resize the data buffer.  */
840723d
-  if (unlikely (ndx < 0)
840723d
+  if (INVALID_NDX (ndx, GElf_Versym)
840723d
       || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
6b412c1
     {
840723d
       __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
--- elfutils-0.132/libelf/elf_getshstrndx.c.robustify
840723d
+++ elfutils-0.132/libelf/elf_getshstrndx.c
840723d
@@ -125,10 +125,25 @@ elf_getshstrndx (elf, dst)
840723d
 	      if (elf->map_address != NULL
840723d
 		  && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
840723d
 		  && (ALLOW_UNALIGNED
840723d
-		      || (((size_t) ((char *) elf->map_address + offset))
840723d
+		      || (((size_t) ((char *) elf->map_address
840723d
+			   + elf->start_offset + offset))
840723d
 			  & (__alignof__ (Elf32_Shdr) - 1)) == 0))
840723d
+		{
840723d
+		  /* First see whether the information in the ELF header is
840723d
+		     valid and it does not ask for too much.  */
840723d
+		  if (unlikely (offset + sizeof (Elf32_Shdr)
840723d
+				> elf->maximum_size))
840723d
+		    {
840723d
+		      /* Something is wrong.  */
840723d
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
840723d
+		      result = -1;
840723d
+		      goto out;
840723d
+		    }
840723d
+
840723d
 		/* We can directly access the memory.  */
840723d
-		num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
840723d
+		  num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
840723d
+					 + offset))->sh_link;
840723d
+		}
840723d
 	      else
840723d
 		{
840723d
 		  /* We avoid reading in all the section headers.  Just read
840723d
@@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
840723d
 	      if (elf->map_address != NULL
840723d
 		  && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
840723d
 		  && (ALLOW_UNALIGNED
840723d
-		      || (((size_t) ((char *) elf->map_address + offset))
840723d
+		      || (((size_t) ((char *) elf->map_address
840723d
+			   + elf->start_offset + offset))
840723d
 			  & (__alignof__ (Elf64_Shdr) - 1)) == 0))
840723d
+		{
840723d
+		  /* First see whether the information in the ELF header is
840723d
+		     valid and it does not ask for too much.  */
840723d
+		  if (unlikely (offset + sizeof (Elf64_Shdr)
840723d
+				> elf->maximum_size))
840723d
+		    {
840723d
+		      /* Something is wrong.  */
840723d
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
840723d
+		      result = -1;
840723d
+		      goto out;
840723d
+		    }
840723d
+
840723d
 		/* We can directly access the memory.  */
840723d
-		num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
840723d
+		  num = ((Elf64_Shdr *) (elf->map_address
840723d
+			 + elf->start_offset + offset))->sh_link;
840723d
+		}
840723d
 	      else
840723d
 		{
840723d
 		  /* We avoid reading in all the section headers.  Just read
840723d
--- elfutils-0.132/libelf/libelfP.h.robustify
840723d
+++ elfutils-0.132/libelf/libelfP.h
840723d
@@ -596,4 +596,13 @@ extern uint32_t __libelf_crc32 (uint32_t
840723d
 /* Align offset to 4 bytes as needed for note name and descriptor data.  */
840723d
 #define NOTE_ALIGN(n)	(((n) + 3) & -4U)
840723d
 
840723d
+/* Convenience macro.  Assumes int NDX and TYPE with size at least
840723d
+   2 bytes.  */
840723d
+#if SIZE_MAX > 4294967295U
840723d
+# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
840723d
+#else
840723d
+# define INVALID_NDX(ndx, type) \
840723d
+  unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
840723d
+#endif
840723d
+
840723d
 #endif  /* libelfP.h */
840723d
--- elfutils-0.132/libelf/gelf_getmove.c.robustify
840723d
+++ elfutils-0.132/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;
840723d
--- elfutils-0.132/libelf/elf32_updatefile.c.robustify
840723d
+++ elfutils-0.132/libelf/elf32_updatefile.c
840723d
@@ -212,6 +212,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf
840723d
   /* Write all the sections.  Well, only those which are modified.  */
840723d
   if (shnum > 0)
6b412c1
     {
840723d
+      if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
840723d
+ 	return 1;
840723d
+
840723d
       Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
840723d
       Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
840723d
       char *const shdr_start = ((char *) elf->map_address + elf->start_offset
840723d
@@ -582,6 +585,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
840723d
   /* Write all the sections.  Well, only those which are modified.  */
840723d
   if (shnum > 0)
6b412c1
     {
840723d
+      if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
840723d
+					+ sizeof (ElfW2(LIBELFBITS,Shdr)))))
840723d
+	return 1;
840723d
+
840723d
       off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
840723d
 #if EV_NUM != 2
840723d
       xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
840723d
--- elfutils-0.132/libelf/gelf_update_lib.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_update_lib.c
840723d
@@ -68,12 +68,6 @@ gelf_update_lib (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
-
840723d
   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
840723d
   if (unlikely (data_scn->d.d_type != ELF_T_LIB))
6b412c1
     {
840723d
@@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
bf5b25b
 
840723d
   /* Check whether we have to resize the data buffer.  */
840723d
   int result = 0;
840723d
-  if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
840723d
+  if (INVALID_NDX (ndx, Elf64_Lib)
840723d
+      || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
840723d
     __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
   else
840723d
     {
840723d
--- elfutils-0.132/libelf/elf32_getphdr.c.robustify
840723d
+++ elfutils-0.132/libelf/elf32_getphdr.c
840723d
@@ -116,6 +116,16 @@ elfw2(LIBELFBITS,getphdr) (elf)
840723d
 
840723d
       if (elf->map_address != NULL)
bf5b25b
 	{
840723d
+	  /* First see whether the information in the ELF header is
840723d
+	     valid and it does not ask for too much.  */
840723d
+	  if (unlikely (ehdr->e_phoff >= elf->maximum_size)
840723d
+	      || unlikely (ehdr->e_phoff + size > elf->maximum_size))
840723d
+	    {
840723d
+	      /* Something is wrong.  */
840723d
+	      __libelf_seterrno (ELF_E_INVALID_PHDR);
840723d
+	      goto out;
840723d
+	    }
840723d
+
840723d
 	  /* All the data is already mapped.  Use it.  */
840723d
 	  void *file_phdr = ((char *) elf->map_address
840723d
 			     + elf->start_offset + ehdr->e_phoff);
840723d
--- elfutils-0.132/libelf/gelf_getlib.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_getlib.c
840723d
@@ -86,7 +86,8 @@ gelf_getlib (data, ndx, dst)
840723d
   /* The data is already in the correct form.  Just make sure the
840723d
      index is OK.  */
840723d
   GElf_Lib *result = NULL;
840723d
-  if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
840723d
+  if (INVALID_NDX (ndx, GElf_Lib)
840723d
+      || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
840723d
     __libelf_seterrno (ELF_E_INVALID_INDEX);
6b412c1
   else
6b412c1
     {
840723d
--- elfutils-0.132/libelf/elf32_newphdr.c.robustify
840723d
+++ elfutils-0.132/libelf/elf32_newphdr.c
840723d
@@ -124,6 +124,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
840723d
   else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
840723d
 	   || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
840723d
     {
840723d
+      if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
840723d
+	{
840723d
+	  result = NULL;
840723d
+	  goto out;
840723d
+	}
840723d
+
840723d
       /* Allocate a new program header with the appropriate number of
840723d
 	 elements.  */
840723d
       result = (ElfW2(LIBELFBITS,Phdr) *)
840723d
--- elfutils-0.132/libelf/gelf_update_rela.c.robustify
840723d
+++ elfutils-0.132/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;
840723d
--- elfutils-0.132/libelf/gelf_update_syminfo.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_update_syminfo.c
840723d
@@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
840723d
   if (data == NULL)
840723d
     return 0;
840723d
 
840723d
-  if (unlikely (ndx < 0))
840723d
-    {
840723d
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
-      return 0;
840723d
-    }
840723d
-
840723d
   if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
6b412c1
     {
840723d
       /* The type of the data better should match.  */
840723d
@@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
840723d
   rwlock_wrlock (scn->elf->lock);
bf5b25b
 
840723d
   /* Check whether we have to resize the data buffer.  */
840723d
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
840723d
+  if (INVALID_NDX (ndx, GElf_Syminfo)
840723d
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
6b412c1
     {
840723d
       __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
       goto out;
840723d
--- elfutils-0.132/libelf/gelf_update_sym.c.robustify
840723d
+++ elfutils-0.132/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;
840723d
--- elfutils-0.132/libelf/gelf_getsyminfo.c.robustify
840723d
+++ elfutils-0.132/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;
840723d
--- elfutils-0.132/libelf/gelf_update_symshndx.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_update_symshndx.c
840723d
@@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata
840723d
   if (symdata == NULL)
840723d
     return 0;
840723d
 
840723d
-  if (unlikely (ndx < 0))
840723d
-    {
840723d
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
-      return 0;
840723d
-    }
840723d
-
840723d
   if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
6b412c1
     {
840723d
       /* The type of the data better should match.  */
840723d
@@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
840723d
 	}
bf5b25b
 
840723d
       /* Check whether we have to resize the data buffer.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf32_Sym)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
840723d
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  goto out;
840723d
@@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
840723d
   else
6b412c1
     {
840723d
       /* Check whether we have to resize the data buffer.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf64_Sym)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
840723d
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  goto out;
840723d
--- elfutils-0.132/libelf/elf_getarsym.c.robustify
840723d
+++ elfutils-0.132/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
840723d
--- elfutils-0.132/libelf/gelf_getsym.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_getsym.c
840723d
@@ -90,7 +90,8 @@ gelf_getsym (data, ndx, dst)
840723d
 	 table entries has to be adopted.  The user better has provided
840723d
 	 a buffer where we can store the information.  While copying the
840723d
 	 data we are converting the format.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
840723d
+      if (INVALID_NDX (ndx, Elf32_Sym)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
840723d
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  goto out;
840723d
@@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
840723d
 
840723d
       /* The data is already in the correct form.  Just make sure the
840723d
 	 index is OK.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
840723d
+      if (INVALID_NDX (ndx, GElf_Sym)
840723d
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
840723d
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  goto out;
840723d
--- elfutils-0.132/libelf/elf_begin.c.robustify
840723d
+++ elfutils-0.132/libelf/elf_begin.c
840723d
@@ -155,7 +155,8 @@ get_shnum (void *map_address, unsigned c
840723d
 
840723d
       if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
840723d
 	{
840723d
-	  if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
840723d
+	  if (unlikely (ehdr.e32->e_shoff >= maxsize)
840723d
+	      || unlikely (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize))
840723d
 	    /* Cannot read the first section header.  */
840723d
 	    return 0;
840723d
 
840723d
@@ -203,7 +204,8 @@ get_shnum (void *map_address, unsigned c
840723d
 
840723d
       if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
840723d
 	{
840723d
-	  if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
840723d
+	  if (unlikely (ehdr.e64->e_shoff >= maxsize)
840723d
+	      || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
840723d
 	    /* Cannot read the first section header.  */
840723d
 	    return 0;
840723d
 
840723d
@@ -275,6 +277,15 @@ file_read_elf (int fildes, void *map_add
840723d
     /* Could not determine the number of sections.  */
6b412c1
     return NULL;
bf5b25b
 
840723d
+  /* Check for too many sections.  */
840723d
+  if (e_ident[EI_CLASS] == ELFCLASS32)
840723d
+    {
840723d
+      if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
840723d
+	return NULL;
840723d
+    }
840723d
+  else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
840723d
+    return NULL;
840723d
+
840723d
   /* We can now allocate the memory.  */
840723d
   Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
840723d
 			   ELF_K_ELF, scncnt * sizeof (Elf_Scn));
840723d
@@ -308,13 +319,31 @@ file_read_elf (int fildes, void *map_add
840723d
 	{
840723d
 	  /* We can use the mmapped memory.  */
840723d
 	  elf->state.elf32.ehdr = ehdr;
840723d
+
840723d
+	  if (unlikely (ehdr->e_shoff >= maxsize)
840723d
+	      || unlikely (ehdr->e_shoff
840723d
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
840723d
+	    {
840723d
+	    free_and_out:
840723d
+	      free (elf);
840723d
+	      __libelf_seterrno (ELF_E_INVALID_FILE);
840723d
+	      return NULL;
840723d
+	    }
840723d
 	  elf->state.elf32.shdr
840723d
 	    = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
840723d
+
840723d
 	  if (ehdr->e_phnum > 0)
840723d
+	    {
840723d
 	    /* Assign a value only if there really is a program
840723d
 	       header.  Otherwise the value remains NULL.  */
840723d
+	      if (unlikely (ehdr->e_phoff >= maxsize)
840723d
+		  || unlikely (ehdr->e_phoff
840723d
+			       + ehdr->e_phnum
840723d
+			       * sizeof (Elf32_Phdr) > maxsize))
840723d
+		goto free_and_out;
840723d
 	    elf->state.elf32.phdr
840723d
 	      = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
840723d
+	    }
840723d
 
840723d
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
840723d
 	    {
840723d
@@ -396,13 +425,26 @@ file_read_elf (int fildes, void *map_add
840723d
 	{
840723d
 	  /* We can use the mmapped memory.  */
840723d
 	  elf->state.elf64.ehdr = ehdr;
840723d
+
840723d
+	  if (unlikely (ehdr->e_shoff >= maxsize)
840723d
+	      || unlikely (ehdr->e_shoff
840723d
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
840723d
+	    goto free_and_out;
840723d
 	  elf->state.elf64.shdr
840723d
 	    = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
840723d
+
840723d
 	  if (ehdr->e_phnum > 0)
840723d
+	    {
840723d
 	    /* Assign a value only if there really is a program
840723d
 	       header.  Otherwise the value remains NULL.  */
840723d
+	      if (unlikely (ehdr->e_phoff >= maxsize)
840723d
+		  || unlikely (ehdr->e_phoff
840723d
+			       + ehdr->e_phnum
840723d
+			       * sizeof (Elf32_Phdr) > maxsize))
840723d
+		goto free_and_out;
840723d
 	    elf->state.elf64.phdr
840723d
 	      = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
840723d
+	    }
840723d
 
840723d
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
840723d
 	    {
840723d
--- elfutils-0.132/libelf/gelf_update_rel.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_update_rel.c
840723d
@@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx,
840723d
   if (dst == NULL)
840723d
     return 0;
840723d
 
6b412c1
-  if (unlikely (ndx < 0))
6b412c1
-    {
6b412c1
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
-      return 0;
6b412c1
-    }
6b412c1
-
6b412c1
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
6b412c1
     {
840723d
       /* The type of the data better should match.  */
840723d
@@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
840723d
 	}
840723d
 
840723d
       /* 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);
840723d
 	  goto out;
840723d
@@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
840723d
   else
6b412c1
     {
840723d
       /* 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);
840723d
 	  goto out;
840723d
--- elfutils-0.132/libelf/gelf_update_move.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_update_move.c
840723d
@@ -75,7 +75,7 @@ gelf_update_move (data, ndx, src)
840723d
   assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
bf5b25b
 
6b412c1
   /* Check whether we have to resize the data buffer.  */
6b412c1
-  if (unlikely (ndx < 0)
840723d
+  if (INVALID_NDX (ndx, GElf_Move)
840723d
       || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
6b412c1
     {
6b412c1
       __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
--- elfutils-0.132/libelf/gelf_update_dyn.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_update_dyn.c
840723d
@@ -71,12 +71,6 @@ gelf_update_dyn (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
-
840723d
   if (unlikely (data_scn->d.d_type != ELF_T_DYN))
6b412c1
     {
6b412c1
       /* The type of the data better should match.  */
840723d
@@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
6b412c1
 	}
bf5b25b
 
6b412c1
       /* Check whether we have to resize the data buffer.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf32_Dyn)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
6b412c1
 	{
6b412c1
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
6b412c1
 	  goto out;
840723d
@@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
6b412c1
   else
6b412c1
     {
6b412c1
       /* Check whether we have to resize the data buffer.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf64_Dyn)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
6b412c1
 	{
6b412c1
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
6b412c1
 	  goto out;
840723d
--- elfutils-0.132/libelf/gelf_getrela.c.robustify
840723d
+++ elfutils-0.132/libelf/gelf_getrela.c
840723d
@@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
840723d
   if (data_scn == NULL)
840723d
     return NULL;
bf5b25b
 
6b412c1
-  if (unlikely (ndx < 0))
6b412c1
-    {
6b412c1
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
-      return NULL;
6b412c1
-    }
6b412c1
-
840723d
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
6b412c1
     {
840723d
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
840723d
@@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
840723d
   if (scn->elf->class == ELFCLASS32)
6b412c1
     {
840723d
       /* We have to convert the data.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf32_Rela)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
6b412c1
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  result = NULL;
840723d
@@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
840723d
     {
840723d
       /* Simply copy the data after we made sure we are actually getting
840723d
 	 correct data.  */
840723d
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
840723d
+      if (INVALID_NDX (ndx, Elf64_Rela)
840723d
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
6b412c1
 	{
840723d
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
840723d
 	  result = NULL;