e39c7bc
src/
e39c7bc
2005-06-09  Roland McGrath  <roland@redhat.com>
e39c7bc
e39c7bc
	* readelf.c (handle_dynamic, handle_symtab): Check for bogus sh_link.
e39c7bc
	(handle_verneed, handle_verdef, handle_versym, handle_hash): Likewise.
e39c7bc
	(handle_scngrp): Check for bogus sh_info.
e39c7bc
e39c7bc
	* strip.c (handle_elf): Check for bogus values in sh_link, sh_info,
e39c7bc
	st_shndx, e_shstrndx, and SHT_GROUP or SHT_SYMTAB_SHNDX data.
e39c7bc
	Don't use assert on input values, instead bail with "illformed" error.
e39c7bc
e39c7bc
2005-05-17  Jakub Jelinek  <jakub@redhat.com>
e39c7bc
e39c7bc
libelf/
e39c7bc
	* elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header
e39c7bc
	table fits into object's bounds.
e39c7bc
	* elf_getshstrndx.c (elf_getshstrndx): Add elf->start_offset to
e39c7bc
	elf->map_address.  Check if first section header fits into object's
e39c7bc
	bounds.
2335578
	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)):
e39c7bc
	Check if section header table fits into object's bounds.
4af02cf
	* elf_begin.c (get_shnum): Ensure section headers fits into
4af02cf
	object's bounds.
e39c7bc
	(file_read_elf): Make sure scncnt is small enough to allocate both
e39c7bc
	ElfXX_Shdr and Elf_Scn array.  Make sure section and program header
e39c7bc
	tables fit into object's bounds.  Avoid memory leak on failure.
e39c7bc
e39c7bc
src/
e39c7bc
	* elflint.c (check_hash): Don't check entries beyond end of section.
e39c7bc
	(check_note): Don't crash if gelf_rawchunk fails.
e39c7bc
	(section_name): Return <invalid> if gelf_getshdr returns NULL.
e39c7bc
e39c7bc
2005-05-14  Jakub Jelinek  <jakub@redhat.com>
e39c7bc
e39c7bc
libelf/
e39c7bc
	* libelfP.h (INVALID_NDX): Define.
e39c7bc
	* gelf_getdyn.c (gelf_getdyn): Use it.  Remove ndx < 0 test if any.
e39c7bc
	* gelf_getlib.c (gelf_getlib): Likewise.
e39c7bc
	* gelf_getmove.c (gelf_getmove): Likewise.
e39c7bc
	* gelf_getrel.c (gelf_getrel): Likewise.
e39c7bc
	* gelf_getrela.c (gelf_getrela): Likewise.
e39c7bc
	* gelf_getsym.c (gelf_getsym): Likewise.
e39c7bc
	* gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
e39c7bc
	* gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
e39c7bc
	* gelf_getversym.c (gelf_getversym): Likewise.
e39c7bc
	* gelf_update_dyn.c (gelf_update_dyn): Likewise.
e39c7bc
	* gelf_update_lib.c (gelf_update_lib): Likewise.
e39c7bc
	* gelf_update_move.c (gelf_update_move): Likewise.
e39c7bc
	* gelf_update_rel.c (gelf_update_rel): Likewise.
e39c7bc
	* gelf_update_rela.c (gelf_update_rela): Likewise.
e39c7bc
	* gelf_update_sym.c (gelf_update_sym): Likewise.
e39c7bc
	* gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
e39c7bc
	* gelf_update_symshndx.c (gelf_update_symshndx): Likewise.
e39c7bc
	* gelf_update_versym.c (gelf_update_versym): Likewise.
e39c7bc
	* elf_newscn.c (elf_newscn): Check for overflow.
e39c7bc
	* elf32_updatefile.c (__elfw2(LIBELFBITS,updatemmap)): Likewise.
e39c7bc
	(__elfw2(LIBELFBITS,updatefile)): Likewise.
e39c7bc
	* elf_begin.c (file_read_elf): Likewise.
e39c7bc
	* elf32_newphdr.c (elfw2(LIBELFBITS,newphdr)): Likewise.
e39c7bc
	* elf_getarsym.c (elf_getarsym): Likewise.
e39c7bc
	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Likewise.
e39c7bc
src/
e39c7bc
	* elflint.c (section_name): Return "<invalid>" instead of
e39c7bc
	crashing on invalid section name.
e39c7bc
	(check_symtab, is_rel_dyn, check_rela, check_rel, check_dynamic,
e39c7bc
	check_symtab_shndx, check_hash, check_versym): Robustify.
e39c7bc
a6ac629
--- elfutils-0.135/src/elflint.c.robustify
a6ac629
+++ elfutils-0.135/src/elflint.c
a6ac629
@@ -131,6 +131,9 @@ static uint32_t shstrndx;
f6dcaea
 /* Array to count references in section groups.  */
f6dcaea
 static int *scnref;
f6dcaea
 
f6dcaea
+/* Number of sections.  */
f6dcaea
+static unsigned int shnum;
f6dcaea
+
f6dcaea
 
f6dcaea
 int
f6dcaea
 main (int argc, char *argv[])
a6ac629
@@ -320,10 +323,19 @@ section_name (Ebl *ebl, int idx)
f6dcaea
 {
f6dcaea
   GElf_Shdr shdr_mem;
f6dcaea
   GElf_Shdr *shdr;
f6dcaea
+  const char *ret;
f6dcaea
+
f6dcaea
+  if ((unsigned int) idx > shnum)
f6dcaea
+    return "<invalid>";
f6dcaea
 
f6dcaea
   shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
f6dcaea
+  if (shdr == NULL)
f6dcaea
+    return "<invalid>";
f6dcaea
 
f6dcaea
-  return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
f6dcaea
+  ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
f6dcaea
+  if (ret == NULL)
f6dcaea
+    return "<invalid>";
f6dcaea
+  return ret;
f6dcaea
 }
f6dcaea
 
f6dcaea
 
a6ac629
@@ -345,10 +357,6 @@ static const int valid_e_machine[] =
f6dcaea
   (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
f6dcaea
 
f6dcaea
 
f6dcaea
-/* Number of sections.  */
f6dcaea
-static unsigned int shnum;
f6dcaea
-
f6dcaea
-
f6dcaea
 static void
f6dcaea
 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
f6dcaea
 {
a6ac629
@@ -613,7 +621,8 @@ section [%2d] '%s': symbol table cannot 
f6dcaea
 	  }
f6dcaea
       }
f6dcaea
 
f6dcaea
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
f6dcaea
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
f6dcaea
+  if (shdr->sh_entsize != sh_entsize)
f6dcaea
     ERROR (gettext ("\
f6dcaea
 section [%2u] '%s': entry size is does not match ElfXX_Sym\n"),
f6dcaea
 	   idx, section_name (ebl, idx));
a6ac629
@@ -651,7 +660,7 @@ section [%2d] '%s': XINDEX for zeroth en
f6dcaea
 	       xndxscnidx, section_name (ebl, xndxscnidx));
f6dcaea
     }
f6dcaea
 
f6dcaea
-  for (size_t cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
f6dcaea
+  for (size_t cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
f6dcaea
     {
f6dcaea
       sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
f6dcaea
       if (sym == NULL)
a6ac629
@@ -671,7 +680,8 @@ section [%2d] '%s': symbol %zu: invalid 
f6dcaea
       else
f6dcaea
 	{
f6dcaea
 	  name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
f6dcaea
-	  assert (name != NULL);
f6dcaea
+	  assert (name != NULL
f6dcaea
+		  || strshdr->sh_type != SHT_STRTAB);
f6dcaea
 	}
f6dcaea
 
f6dcaea
       if (sym->st_shndx == SHN_XINDEX)
a6ac629
@@ -1001,9 +1011,11 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
f6dcaea
     {
f6dcaea
       GElf_Shdr rcshdr_mem;
f6dcaea
       const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
f6dcaea
-      assert (rcshdr != NULL);
f6dcaea
 
f6dcaea
-      if (rcshdr->sh_type == SHT_DYNAMIC)
f6dcaea
+      if (rcshdr == NULL)
f6dcaea
+	break;
f6dcaea
+
f6dcaea
+      if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
f6dcaea
 	{
f6dcaea
 	  /* Found the dynamic section.  Look through it.  */
f6dcaea
 	  Elf_Data *d = elf_getdata (scn, NULL);
a6ac629
@@ -1013,7 +1025,9 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
f6dcaea
 	    {
f6dcaea
 	      GElf_Dyn dyn_mem;
f6dcaea
 	      GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
f6dcaea
-	      assert (dyn != NULL);
f6dcaea
+
f6dcaea
+	      if (dyn == NULL)
f6dcaea
+		break;
f6dcaea
 
f6dcaea
 	      if (dyn->d_tag == DT_RELCOUNT)
f6dcaea
 		{
a6ac629
@@ -1027,7 +1041,9 @@ section [%2d] '%s': DT_RELCOUNT used for
f6dcaea
 		      /* Does the number specified number of relative
f6dcaea
 			 relocations exceed the total number of
f6dcaea
 			 relocations?  */
f6dcaea
-		      if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
f6dcaea
+		      if (shdr->sh_entsize != 0
f6dcaea
+			  && dyn->d_un.d_val > (shdr->sh_size
f6dcaea
+						/ shdr->sh_entsize))
f6dcaea
 			ERROR (gettext ("\
f6dcaea
 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
f6dcaea
 			       idx, section_name (ebl, idx),
a6ac629
@@ -1187,7 +1203,8 @@ section [%2d] '%s': no relocations for m
f6dcaea
 	}
f6dcaea
     }
f6dcaea
 
f6dcaea
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
f6dcaea
+  size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
f6dcaea
+  if (shdr->sh_entsize != sh_entsize)
f6dcaea
     ERROR (gettext (reltype == ELF_T_RELA ? "\
f6dcaea
 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
f6dcaea
 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
a6ac629
@@ -1410,7 +1427,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
f6dcaea
   Elf_Data *symdata = elf_getdata (symscn, NULL);
f6dcaea
   enum load_state state = state_undecided;
f6dcaea
 
f6dcaea
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
f6dcaea
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
f6dcaea
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
f6dcaea
     {
f6dcaea
       GElf_Rela rela_mem;
f6dcaea
       GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
a6ac629
@@ -1460,7 +1478,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
f6dcaea
   Elf_Data *symdata = elf_getdata (symscn, NULL);
f6dcaea
   enum load_state state = state_undecided;
f6dcaea
 
f6dcaea
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
f6dcaea
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
f6dcaea
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
f6dcaea
     {
f6dcaea
       GElf_Rel rel_mem;
f6dcaea
       GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
a6ac629
@@ -1563,7 +1582,8 @@ section [%2d] '%s': referenced as string
f6dcaea
 	   shdr->sh_link, section_name (ebl, shdr->sh_link),
f6dcaea
 	   idx, section_name (ebl, idx));
f6dcaea
 
f6dcaea
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
f6dcaea
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
f6dcaea
+  if (shdr->sh_entsize != sh_entsize)
f6dcaea
     ERROR (gettext ("\
f6dcaea
 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
f6dcaea
 	   idx, section_name (ebl, idx));
a6ac629
@@ -1573,7 +1593,7 @@ section [%2d] '%s': section entry size d
f6dcaea
 	   idx, section_name (ebl, idx));
f6dcaea
 
f6dcaea
   bool non_null_warned = false;
f6dcaea
-  for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
f6dcaea
+  for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
f6dcaea
     {
f6dcaea
       GElf_Dyn dyn_mem;
f6dcaea
       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
a6ac629
@@ -1854,6 +1874,8 @@ section [%2d] '%s': entry size does not 
f6dcaea
 	   idx, section_name (ebl, idx));
f6dcaea
 
f6dcaea
   if (symshdr != NULL
f6dcaea
+      && shdr->sh_entsize
f6dcaea
+      && symshdr->sh_entsize
f6dcaea
       && (shdr->sh_size / shdr->sh_entsize
f6dcaea
 	  < symshdr->sh_size / symshdr->sh_entsize))
f6dcaea
     ERROR (gettext ("\
a6ac629
@@ -1880,6 +1902,12 @@ section [%2d] '%s': extended section ind
f6dcaea
     }
f6dcaea
 
f6dcaea
   Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
f6dcaea
+  if (data == NULL)
f6dcaea
+    {
f6dcaea
+      ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
f6dcaea
+ 	     idx, section_name (ebl, idx));
f6dcaea
+      return;
f6dcaea
+    }
f6dcaea
 
f6dcaea
   if (*((Elf32_Word *) data->d_buf) != 0)
f6dcaea
     ERROR (gettext ("symbol 0 should have zero extended section index\n"));
a6ac629
@@ -1922,7 +1950,7 @@ section [%2d] '%s': hash table section i
f6dcaea
 
f6dcaea
   size_t maxidx = nchain;
f6dcaea
 
f6dcaea
-  if (symshdr != NULL)
f6dcaea
+  if (symshdr != NULL && symshdr->sh_entsize != 0)
f6dcaea
     {
f6dcaea
       size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
f6dcaea
 
a6ac629
@@ -1933,18 +1961,28 @@ section [%2d] '%s': hash table section i
f6dcaea
       maxidx = symsize;
f6dcaea
     }
f6dcaea
 
f6dcaea
+  Elf32_Word *buf = (Elf32_Word *) data->d_buf;
f6dcaea
+  Elf32_Word *end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
f6dcaea
   size_t cnt;
f6dcaea
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
f6dcaea
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
f6dcaea
+    {
f6dcaea
+      if (buf + cnt >= end)
f6dcaea
+	break;
f6dcaea
+      else if (buf[cnt] >= maxidx)
f6dcaea
       ERROR (gettext ("\
f6dcaea
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
f6dcaea
 	     idx, section_name (ebl, idx), cnt - 2);
f6dcaea
+    }
f6dcaea
 
f6dcaea
   for (; cnt < 2 + nbucket + nchain; ++cnt)
f6dcaea
-    if (((Elf32_Word *) data->d_buf)[cnt] >= maxidx)
f6dcaea
+    {
f6dcaea
+      if (buf + cnt >= end)
f6dcaea
+	break;
f6dcaea
+      else if (buf[cnt] >= maxidx)
f6dcaea
       ERROR (gettext ("\
f6dcaea
 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
f6dcaea
 	     idx, section_name (ebl, idx), cnt - 2 - nbucket);
f6dcaea
+    }
f6dcaea
 }
f6dcaea
 
f6dcaea
 
a6ac629
@@ -1974,18 +2012,28 @@ section [%2d] '%s': hash table section i
f6dcaea
       maxidx = symsize;
f6dcaea
     }
f6dcaea
 
f6dcaea
+  Elf64_Xword *buf = (Elf64_Xword *) data->d_buf;
f6dcaea
+  Elf64_Xword *end = (Elf64_Xword *) ((char *) data->d_buf + shdr->sh_size);
f6dcaea
   size_t cnt;
f6dcaea
   for (cnt = 2; cnt < 2 + nbucket; ++cnt)
f6dcaea
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
f6dcaea
+    {
f6dcaea
+      if (buf + cnt >= end)
f6dcaea
+	break;
f6dcaea
+      else if (buf[cnt] >= maxidx)
f6dcaea
       ERROR (gettext ("\
f6dcaea
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
f6dcaea
 	     idx, section_name (ebl, idx), cnt - 2);
f6dcaea
+    }
f6dcaea
 
f6dcaea
   for (; cnt < 2 + nbucket + nchain; ++cnt)
f6dcaea
-    if (((Elf64_Xword *) data->d_buf)[cnt] >= maxidx)
f6dcaea
+    {
f6dcaea
+      if (buf + cnt >= end)
f6dcaea
+	break;
f6dcaea
+      else if (buf[cnt] >= maxidx)
f6dcaea
       ERROR (gettext ("\
f6dcaea
 section [%2d] '%s': hash chain reference %" PRIu64 " out of bounds\n"),
f6dcaea
-	     idx, section_name (ebl, idx), (uint64_t) (cnt - 2 - nbucket));
f6dcaea
+	       idx, section_name (ebl, idx), (uint64_t) cnt - 2 - nbucket);
f6dcaea
+    }
f6dcaea
 }
f6dcaea
 
f6dcaea
 
a6ac629
@@ -2010,7 +2058,7 @@ section [%2d] '%s': bitmask size not pow
f6dcaea
   if (shdr->sh_size < (4 + bitmask_words + nbuckets) * sizeof (Elf32_Word))
f6dcaea
     {
f6dcaea
       ERROR (gettext ("\
f6dcaea
-section [%2d] '%s': hash table section is too small (is %ld, expected at least%ld)\n"),
f6dcaea
+section [%2d] '%s': hash table section is too small (is %ld, expected at least %ld)\n"),
f6dcaea
 	     idx, section_name (ebl, idx), (long int) shdr->sh_size,
f6dcaea
 	     (long int) ((4 + bitmask_words + nbuckets) * sizeof (Elf32_Word)));
f6dcaea
       return;
a6ac629
@@ -2682,8 +2730,9 @@ section [%2d] '%s' refers in sh_link to 
f6dcaea
 
f6dcaea
   /* The number of elements in the version symbol table must be the
f6dcaea
      same as the number of symbols.  */
f6dcaea
-  if (shdr->sh_size / shdr->sh_entsize
f6dcaea
-      != symshdr->sh_size / symshdr->sh_entsize)
f6dcaea
+  if (shdr->sh_entsize && symshdr->sh_entsize
f6dcaea
+      && (shdr->sh_size / shdr->sh_entsize
f6dcaea
+	  != symshdr->sh_size / symshdr->sh_entsize))
f6dcaea
     ERROR (gettext ("\
f6dcaea
 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
f6dcaea
 	   idx, section_name (ebl, idx),
a6ac629
diff -up elfutils-0.135/src/readelf.c.robustify elfutils-0.135/src/readelf.c
a6ac629
--- elfutils-0.135/src/readelf.c.robustify
a6ac629
+++ elfutils-0.135/src/readelf.c
a6ac629
@@ -1111,6 +1111,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
2fa04c0
   Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
e39c7bc
 
2fa04c0
   GElf_Sym sym_mem;
2fa04c0
+  GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
e39c7bc
+
2fa04c0
   printf ((grpref[0] & GRP_COMDAT)
2fa04c0
 	  ? ngettext ("\
2fa04c0
 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
a6ac629
@@ -1123,8 +1125,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
2fa04c0
 		      data->d_size / sizeof (Elf32_Word) - 1),
2fa04c0
 	  elf_ndxscn (scn),
2fa04c0
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
2fa04c0
-	  elf_strptr (ebl->elf, symshdr->sh_link,
2fa04c0
-		      gelf_getsym (symdata, shdr->sh_info, &sym_mem)->st_name)
2fa04c0
+	  (sym == NULL ? NULL
2fa04c0
+	   : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
2fa04c0
 	  ?: gettext ("<INVALID SYMBOL>"),
2fa04c0
 	  data->d_size / sizeof (Elf32_Word) - 1);
e39c7bc
 
a6ac629
@@ -1275,7 +1277,8 @@ static void
2fa04c0
 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
2fa04c0
 {
2fa04c0
   int class = gelf_getclass (ebl->elf);
2fa04c0
-  GElf_Shdr glink;
2fa04c0
+  GElf_Shdr glink_mem;
2fa04c0
+  GElf_Shdr *glink;
2fa04c0
   Elf_Data *data;
2fa04c0
   size_t cnt;
2fa04c0
   size_t shstrndx;
a6ac629
@@ -1290,6 +1293,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
2fa04c0
     error (EXIT_FAILURE, 0,
2fa04c0
 	   gettext ("cannot get section header string table index"));
e39c7bc
 
2fa04c0
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
2fa04c0
+  if (glink == NULL)
2fa04c0
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c0
+	   elf_ndxscn (scn));
e39c7bc
+
2fa04c0
   printf (ngettext ("\
2fa04c0
 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
2fa04c0
 		    "\
a6ac629
@@ -1299,9 +1307,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
2fa04c0
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2fa04c0
 	  shdr->sh_offset,
2fa04c0
 	  (int) shdr->sh_link,
2fa04c0
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c0
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
-				    &glink)->sh_name));
2fa04c0
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2fa04c0
   fputs_unlocked (gettext ("  Type              Value\n"), stdout);
2335578
 
2fa04c0
   for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
a6ac629
@@ -1801,6 +1807,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
2fa04c0
     error (EXIT_FAILURE, 0,
2fa04c0
 	   gettext ("cannot get section header string table index"));
2fa04c0
 
2fa04c0
+  GElf_Shdr glink_mem;
2fa04c0
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
+				   &glink_mem);
2fa04c0
+  if (glink == NULL)
2fa04c0
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c0
+	   elf_ndxscn (scn));
e39c7bc
+
2fa04c0
   /* Now we can compute the number of entries in the section.  */
2fa04c0
   unsigned int nsyms = data->d_size / (class == ELFCLASS32
2fa04c0
 				       ? sizeof (Elf32_Sym)
a6ac629
@@ -1811,15 +1824,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
2fa04c0
 		    nsyms),
2fa04c0
 	  (unsigned int) elf_ndxscn (scn),
2fa04c0
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
2fa04c0
-  GElf_Shdr glink;
2fa04c0
   printf (ngettext (" %lu local symbol  String table: [%2u] '%s'\n",
2fa04c0
 		    " %lu local symbols  String table: [%2u] '%s'\n",
2fa04c0
 		    shdr->sh_info),
2fa04c0
 	  (unsigned long int) shdr->sh_info,
2fa04c0
 	  (unsigned int) shdr->sh_link,
2fa04c0
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c0
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
-				    &glink)->sh_name));
2fa04c0
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
2fa04c0
 
2fa04c0
   fputs_unlocked (class == ELFCLASS32
2fa04c0
 		  ? gettext ("\
a6ac629
@@ -2055,7 +2065,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
2fa04c0
     error (EXIT_FAILURE, 0,
2fa04c0
 	   gettext ("cannot get section header string table index"));
2fa04c0
 
2fa04c0
-  GElf_Shdr glink;
2fa04c0
+  GElf_Shdr glink_mem;
2fa04c0
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
+				   &glink_mem);
2fa04c0
+  if (glink == NULL)
2fa04c0
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c0
+	   elf_ndxscn (scn));
e39c7bc
+
2fa04c0
   printf (ngettext ("\
2fa04c0
 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
2fa04c0
 		    "\
a6ac629
@@ -2066,9 +2082,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
2fa04c0
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2fa04c0
 	  shdr->sh_offset,
2fa04c0
 	  (unsigned int) shdr->sh_link,
2fa04c0
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c0
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
-				    &glink)->sh_name));
2fa04c0
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
e39c7bc
 
2fa04c0
   unsigned int offset = 0;
2fa04c0
   for (int cnt = shdr->sh_info; --cnt >= 0; )
a6ac629
@@ -2121,8 +2135,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
2fa04c0
     error (EXIT_FAILURE, 0,
2fa04c0
 	   gettext ("cannot get section header string table index"));
2fa04c0
 
2fa04c0
+  GElf_Shdr glink_mem;
2fa04c0
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
+				   &glink_mem);
2fa04c0
+  if (glink == NULL)
2fa04c0
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c0
+	   elf_ndxscn (scn));
2fa04c0
+
2fa04c0
   int class = gelf_getclass (ebl->elf);
2fa04c0
-  GElf_Shdr glink;
2fa04c0
   printf (ngettext ("\
2fa04c0
 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
2fa04c0
 		    "\
a6ac629
@@ -2134,9 +2154,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
2fa04c0
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2fa04c0
 	  shdr->sh_offset,
2fa04c0
 	  (unsigned int) shdr->sh_link,
2fa04c0
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c0
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
-				    &glink)->sh_name));
2fa04c0
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
e39c7bc
 
2fa04c0
   unsigned int offset = 0;
2fa04c0
   for (int cnt = shdr->sh_info; --cnt >= 0; )
a6ac629
@@ -2398,8 +2416,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
2fa04c0
       filename = NULL;
2fa04c0
     }
e39c7bc
 
2fa04c0
+  GElf_Shdr glink_mem;
2fa04c0
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
+				   &glink_mem);
2fa04c0
+  if (glink == NULL)
2fa04c0
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c0
+	   elf_ndxscn (scn));
2fa04c0
+
2fa04c0
   /* Print the header.  */
2fa04c0
-  GElf_Shdr glink;
2fa04c0
   printf (ngettext ("\
2fa04c0
 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
2fa04c0
 		    "\
a6ac629
@@ -2411,9 +2435,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
2fa04c0
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
2fa04c0
 	  shdr->sh_offset,
2fa04c0
 	  (unsigned int) shdr->sh_link,
2fa04c0
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c0
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
-				    &glink)->sh_name));
2fa04c0
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
e39c7bc
 
2fa04c0
   /* Now we can finally look at the actual contents of this section.  */
2fa04c0
   for (unsigned int cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
a6ac629
@@ -2465,7 +2487,17 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
2fa04c0
   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
2fa04c0
     ++counts[lengths[cnt]];
e39c7bc
 
2fa04c0
-  GElf_Shdr glink;
2fa04c0
+  GElf_Shdr glink_mem;
2fa04c0
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
2fa04c0
+					       shdr->sh_link),
2fa04c0
+				   &glink_mem);
2fa04c0
+  if (glink == NULL)
e39c7bc
+    {
2fa04c0
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c0
+	     elf_ndxscn (scn));
2fa04c0
+      return;
e39c7bc
+    }
e39c7bc
+
2fa04c0
   printf (ngettext ("\
2fa04c0
 \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",
2fa04c0
 		    "\
a6ac629
@@ -2478,9 +2510,7 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn,
2fa04c0
 	  shdr->sh_addr,
2fa04c0
 	  shdr->sh_offset,
2fa04c0
 	  (unsigned int) shdr->sh_link,
2fa04c0
-	  elf_strptr (ebl->elf, shstrndx,
2fa04c0
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
2fa04c0
-				    &glink)->sh_name));
2fa04c0
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
e39c7bc
 
2fa04c0
   if (extrastr != NULL)
2fa04c0
     fputs (extrastr, stdout);
a6ac629
@@ -4039,6 +4069,16 @@ print_debug_aranges_section (Dwfl_Module
2fa04c0
       return;
2fa04c0
     }
2fa04c0
 
2fa04c0
+  GElf_Shdr glink_mem;
2fa04c0
+  GElf_Shdr *glink;
2fa04c0
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
2fa04c0
+  if (glink == NULL)
2fa04c0
+    {
2fa04c0
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
2fa04c0
+	     elf_ndxscn (scn));
2fa04c0
+      return;
2fa04c0
+    }
4af02cf
+
2fa04c0
   printf (ngettext ("\
2fa04c0
 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
2fa04c0
 		    "\
a6ac629
diff -up elfutils-0.135/src/strip.c.robustify elfutils-0.135/src/strip.c
a6ac629
--- elfutils-0.135/src/strip.c.robustify
a6ac629
+++ elfutils-0.135/src/strip.c
a6ac629
@@ -544,6 +544,11 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
       goto fail_close;
2fa04c0
     }
e39c7bc
 
2fa04c0
+  if (shstrndx >= shnum)
2fa04c0
+    goto illformed;
e39c7bc
+
2fa04c0
+#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
e39c7bc
+
2fa04c0
   /* Storage for section information.  We leave room for two more
2fa04c0
      entries since we unconditionally create a section header string
2fa04c0
      table.  Maybe some weird tool created an ELF file without one.
a6ac629
@@ -565,7 +570,7 @@ handle_elf (int fd, Elf *elf, const char
e39c7bc
     {
2fa04c0
       /* This should always be true (i.e., there should not be any
2fa04c0
 	 holes in the numbering).  */
2fa04c0
-      assert (elf_ndxscn (scn) == cnt);
2fa04c0
+      elf_assert (elf_ndxscn (scn) == cnt);
e39c7bc
 
2fa04c0
       shdr_info[cnt].scn = scn;
e39c7bc
 
a6ac629
@@ -578,6 +583,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 					shdr_info[cnt].shdr.sh_name);
2fa04c0
       if (shdr_info[cnt].name == NULL)
e39c7bc
 	{
2fa04c0
+	illformed:
2fa04c0
 	  error (0, 0, gettext ("illformed file '%s'"), fname);
2fa04c0
 	  goto fail_close;
2fa04c0
 	}
a6ac629
@@ -587,6 +593,8 @@ handle_elf (int fd, Elf *elf, const char
e39c7bc
 
2fa04c0
       /* Remember the shdr.sh_link value.  */
2fa04c0
       shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
2fa04c0
+      if (shdr_info[cnt].old_sh_link >= shnum)
2fa04c0
+	goto illformed;
e39c7bc
 
2fa04c0
       /* Sections in files other than relocatable object files which
2fa04c0
 	 are not loaded can be freely moved by us.  In relocatable
a6ac629
@@ -599,7 +607,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 	 appropriate reference.  */
2fa04c0
       if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
e39c7bc
 	{
2fa04c0
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
2fa04c0
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
2fa04c0
 	  shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
2fa04c0
 	}
2fa04c0
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
a6ac629
@@ -616,7 +624,12 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 	  for (inner = 1;
2fa04c0
 	       inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
2fa04c0
 	       ++inner)
2fa04c0
+	    {
2fa04c0
+	      if (grpref[inner] < shnum)
2fa04c0
 	    shdr_info[grpref[inner]].group_idx = cnt;
2fa04c0
+	      else
2fa04c0
+		goto illformed;
2fa04c0
+	    }
e39c7bc
 
2fa04c0
 	  if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
2fa04c0
 	    /* If the section group contains only one element and this
a6ac629
@@ -627,7 +640,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 	}
2fa04c0
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
e39c7bc
 	{
2fa04c0
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
2fa04c0
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
2fa04c0
 	  shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
2fa04c0
 	}
e39c7bc
 
a6ac629
@@ -635,7 +648,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 	 discarded right away.  */
2fa04c0
       if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
e39c7bc
 	{
2fa04c0
-	  assert (shdr_info[cnt].group_idx != 0);
2fa04c0
+	  elf_assert (shdr_info[cnt].group_idx != 0);
e39c7bc
 
2fa04c0
 	  if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
2fa04c0
 	    {
a6ac629
@@ -710,11 +723,15 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 	    {
2fa04c0
 	      /* If a relocation section is marked as being removed make
2fa04c0
 		 sure the section it is relocating is removed, too.  */
2fa04c0
-	      if ((shdr_info[cnt].shdr.sh_type == SHT_REL
2fa04c0
+	      if (shdr_info[cnt].shdr.sh_type == SHT_REL
2fa04c0
 		   || shdr_info[cnt].shdr.sh_type == SHT_RELA)
2fa04c0
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
2fa04c0
+		{
2fa04c0
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
2fa04c0
+		    goto illformed;
2fa04c0
+		  else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
2fa04c0
 		shdr_info[cnt].idx = 1;
2fa04c0
 	    }
2fa04c0
+	    }
e39c7bc
 
2fa04c0
 	  if (shdr_info[cnt].idx == 1)
2fa04c0
 	    {
a6ac629
@@ -741,7 +758,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 		  if (shdr_info[cnt].symtab_idx != 0
2fa04c0
 		      && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
2fa04c0
 		    {
2fa04c0
-		      assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
2fa04c0
+		      elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
e39c7bc
 
2fa04c0
 		      shdr_info[shdr_info[cnt].symtab_idx].data
2fa04c0
 			= elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
a6ac629
@@ -781,6 +798,9 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 		      else if (scnidx == SHN_XINDEX)
2fa04c0
 			scnidx = xndx;
e39c7bc
 
2fa04c0
+		      if (scnidx >= shnum)
2fa04c0
+			goto illformed;
2fa04c0
+
2fa04c0
 		      if (shdr_info[scnidx].idx == 0)
a6ac629
 			/* This symbol table has a real symbol in
a6ac629
 			   a discarded section.  So preserve the
a6ac629
@@ -811,12 +831,16 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 		}
e39c7bc
 
2fa04c0
 	      /* Handle references through sh_info.  */
2fa04c0
-	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
2fa04c0
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
2fa04c0
+	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
2fa04c0
+		{
2fa04c0
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
2fa04c0
+		    goto illformed;
2fa04c0
+		  else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
2fa04c0
 		{
2fa04c0
 		  shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
2fa04c0
 		  changes |= shdr_info[cnt].shdr.sh_info < cnt;
2fa04c0
 		}
2fa04c0
+		}
e39c7bc
 
2fa04c0
 	      /* Mark the section as investigated.  */
2fa04c0
 	      shdr_info[cnt].idx = 2;
a6ac629
@@ -954,7 +978,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 	  error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
2fa04c0
 		 elf_errmsg (-1));
e39c7bc
 
2fa04c0
-	assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
2fa04c0
+	elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
e39c7bc
 
2fa04c0
 	/* Add this name to the section header string table.  */
2fa04c0
 	shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
a6ac629
@@ -991,7 +1015,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 	error (EXIT_FAILURE, 0,
2fa04c0
 	       gettext ("while create section header section: %s"),
2fa04c0
 	       elf_errmsg (-1));
2fa04c0
-      assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
2fa04c0
+      elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
e39c7bc
 
2fa04c0
       shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
2fa04c0
       if (shdr_info[cnt].data == NULL)
a6ac629
@@ -1047,7 +1071,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
     error (EXIT_FAILURE, 0,
2fa04c0
 	   gettext ("while create section header section: %s"),
2fa04c0
 	   elf_errmsg (-1));
2fa04c0
-  assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
2fa04c0
+  elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
e39c7bc
 
2fa04c0
   /* Finalize the string table and fill in the correct indices in the
2fa04c0
      section headers.  */
a6ac629
@@ -1137,20 +1161,20 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 		    shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
2fa04c0
 					     NULL);
e39c7bc
 
2fa04c0
-		    assert ((versiondata->d_size / sizeof (Elf32_Word))
2fa04c0
+		    elf_assert ((versiondata->d_size / sizeof (Elf32_Word))
2fa04c0
 			    >= shdr_info[cnt].data->d_size / elsize);
2fa04c0
 		  }
e39c7bc
 
2fa04c0
 		if (shdr_info[cnt].version_idx != 0)
2fa04c0
 		  {
2fa04c0
-		    assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
2fa04c0
+		    elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
2fa04c0
 		    /* This section has associated version
2fa04c0
 		       information.  We have to modify that
2fa04c0
 		       information, too.  */
2fa04c0
 		    versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
2fa04c0
 					       NULL);
e39c7bc
 
2fa04c0
-		    assert ((versiondata->d_size / sizeof (GElf_Versym))
2fa04c0
+		    elf_assert ((versiondata->d_size / sizeof (GElf_Versym))
2fa04c0
 			    >= shdr_info[cnt].data->d_size / elsize);
2fa04c0
 		  }
e39c7bc
 
a6ac629
@@ -1205,7 +1229,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 		      sec = shdr_info[sym->st_shndx].idx;
2fa04c0
 		    else
2fa04c0
 		      {
2fa04c0
-			assert (shndxdata != NULL);
2fa04c0
+			elf_assert (shndxdata != NULL);
e39c7bc
 
2fa04c0
 			sec = shdr_info[xshndx].idx;
2fa04c0
 		      }
a6ac629
@@ -1226,7 +1250,7 @@ handle_elf (int fd, Elf *elf, const char
2fa04c0
 			    nxshndx = sec;
2fa04c0
 			  }
e39c7bc
 
2fa04c0
-			assert (sec < SHN_LORESERVE || shndxdata != NULL);
2fa04c0
+			elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
e39c7bc
 
2fa04c0
 			if ((inner != destidx || nshndx != sym->st_shndx
2fa04c0
 			     || (shndxdata != NULL && nxshndx != xshndx))
a6ac629
@@ -1250,7 +1274,7 @@ handle_elf (int fd, Elf *elf, const char
a6ac629
 			     || shdr_info[cnt].debug_data == NULL)
2fa04c0
 		      /* This is a section symbol for a section which has
2fa04c0
 			 been removed.  */
2fa04c0
-		      assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
2fa04c0
+		      elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
2fa04c0
 		  }
e39c7bc
 
2fa04c0
 		if (destidx != inner)
a6ac629
@@ -1437,11 +1461,11 @@ handle_elf (int fd, Elf *elf, const char
a6ac629
 		  {
a6ac629
 		    GElf_Sym sym_mem;
a6ac629
 		    GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
a6ac629
-		    assert (sym != NULL);
a6ac629
+		    elf_assert (sym != NULL);
a6ac629
 
a6ac629
 		    const char *name = elf_strptr (elf, strshndx,
a6ac629
 						   sym->st_name);
a6ac629
-		    assert (name != NULL);
a6ac629
+		    elf_assert (name != NULL);
a6ac629
 		    size_t hidx = elf_hash (name) % nbucket;
a6ac629
 
a6ac629
 		    if (bucket[hidx] == 0)
a6ac629
@@ -1460,7 +1484,7 @@ handle_elf (int fd, Elf *elf, const char
a6ac629
 	    else
a6ac629
 	      {
a6ac629
 		/* Alpha and S390 64-bit use 64-bit SHT_HASH entries.  */
a6ac629
-		assert (shdr_info[cnt].shdr.sh_entsize
a6ac629
+		elf_assert (shdr_info[cnt].shdr.sh_entsize
a6ac629
 			== sizeof (Elf64_Xword));
a6ac629
 
a6ac629
 		Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
a6ac629
@@ -1491,11 +1515,11 @@ handle_elf (int fd, Elf *elf, const char
a6ac629
 		  {
a6ac629
 		    GElf_Sym sym_mem;
a6ac629
 		    GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
a6ac629
-		    assert (sym != NULL);
a6ac629
+		    elf_assert (sym != NULL);
a6ac629
 
a6ac629
 		    const char *name = elf_strptr (elf, strshndx,
a6ac629
 						   sym->st_name);
a6ac629
-		    assert (name != NULL);
a6ac629
+		    elf_assert (name != NULL);
a6ac629
 		    size_t hidx = elf_hash (name) % nbucket;
a6ac629
 
a6ac629
 		    if (bucket[hidx] == 0)
a6ac629
diff -up elfutils-0.135/libelf/gelf_getdyn.c.robustify elfutils-0.135/libelf/gelf_getdyn.c
a6ac629
--- elfutils-0.135/libelf/gelf_getdyn.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_getdyn.c
f6dcaea
@@ -93,7 +93,8 @@ gelf_getdyn (data, ndx, dst)
f6dcaea
 	 table entries has to be adopted.  The user better has provided
f6dcaea
 	 a buffer where we can store the information.  While copying the
f6dcaea
 	 data we are converting the format.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf32_Dyn)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
e39c7bc
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  goto out;
f6dcaea
@@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
e39c7bc
 
f6dcaea
       /* The data is already in the correct form.  Just make sure the
f6dcaea
 	 index is OK.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, GElf_Dyn)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
e39c7bc
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  goto out;
a6ac629
diff -up elfutils-0.135/libelf/gelf_getrel.c.robustify elfutils-0.135/libelf/gelf_getrel.c
a6ac629
--- elfutils-0.135/libelf/gelf_getrel.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_getrel.c
f6dcaea
@@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
f6dcaea
   if (data_scn == NULL)
f6dcaea
     return NULL;
e39c7bc
 
f6dcaea
-  if (unlikely (ndx < 0))
f6dcaea
-    {
f6dcaea
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
-      return NULL;
f6dcaea
-    }
f6dcaea
-
f6dcaea
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
e39c7bc
     {
f6dcaea
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
f6dcaea
@@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
f6dcaea
   if (scn->elf->class == ELFCLASS32)
e39c7bc
     {
f6dcaea
       /* We have to convert the data.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf32_Rel)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
f6dcaea
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  result = NULL;
f6dcaea
@@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
e39c7bc
     {
f6dcaea
       /* Simply copy the data after we made sure we are actually getting
f6dcaea
 	 correct data.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf64_Rel)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
f6dcaea
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  result = NULL;
a6ac629
diff -up elfutils-0.135/libelf/gelf_getversym.c.robustify elfutils-0.135/libelf/gelf_getversym.c
a6ac629
--- elfutils-0.135/libelf/gelf_getversym.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_getversym.c
f6dcaea
@@ -92,7 +92,8 @@ gelf_getversym (data, ndx, dst)
e39c7bc
 
f6dcaea
   /* The data is already in the correct form.  Just make sure the
f6dcaea
      index is OK.  */
f6dcaea
-  if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
f6dcaea
+  if (INVALID_NDX (ndx, GElf_Versym)
f6dcaea
+      || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
e39c7bc
     {
f6dcaea
       __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
       result = NULL;
a6ac629
diff -up elfutils-0.135/libelf/elf_newscn.c.robustify elfutils-0.135/libelf/elf_newscn.c
a6ac629
--- elfutils-0.135/libelf/elf_newscn.c.robustify
a6ac629
+++ elfutils-0.135/libelf/elf_newscn.c
f6dcaea
@@ -104,10 +104,18 @@ elf_newscn (elf)
f6dcaea
   else
acf77d5
     {
f6dcaea
       /* We must allocate a new element.  */
f6dcaea
-      Elf_ScnList *newp;
f6dcaea
+      Elf_ScnList *newp = NULL;
e39c7bc
 
f6dcaea
       assert (elf->state.elf.scnincr > 0);
e39c7bc
 
f6dcaea
+      if (
f6dcaea
+#if SIZE_MAX <= 4294967295U
f6dcaea
+	  likely (elf->state.elf.scnincr
f6dcaea
+		  < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
f6dcaea
+#else
f6dcaea
+	  1
f6dcaea
+#endif
f6dcaea
+	  )
f6dcaea
       newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
f6dcaea
 				     + ((elf->state.elf.scnincr *= 2)
f6dcaea
 					* sizeof (Elf_Scn)), 1);
a6ac629
diff -up elfutils-0.135/libelf/elf32_getshdr.c.robustify elfutils-0.135/libelf/elf32_getshdr.c
a6ac629
--- elfutils-0.135/libelf/elf32_getshdr.c.robustify
a6ac629
+++ elfutils-0.135/libelf/elf32_getshdr.c
f6dcaea
@@ -101,7 +101,8 @@ elfw2(LIBELFBITS,getshdr) (scn)
f6dcaea
 	goto out;
e39c7bc
 
f6dcaea
       size_t shnum;
f6dcaea
-      if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
f6dcaea
+      if (INTUSE (elf_getshnum) (elf, &shnum) != 0
f6dcaea
+	  || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
f6dcaea
 	goto out;
f6dcaea
       size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
e39c7bc
 
f6dcaea
@@ -118,6 +119,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
f6dcaea
 
f6dcaea
       if (elf->map_address != NULL)
f6dcaea
 	{
f6dcaea
+	  /* First see whether the information in the ELF header is
f6dcaea
+	     valid and it does not ask for too much.  */
f6dcaea
+	  if (unlikely (ehdr->e_shoff >= elf->maximum_size)
f6dcaea
+	      || unlikely (ehdr->e_shoff + size > elf->maximum_size))
f6dcaea
+	    {
f6dcaea
+	      /* Something is wrong.  */
f6dcaea
+	      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
f6dcaea
+	      goto free_and_out;
f6dcaea
+	    }
e39c7bc
+
f6dcaea
 	  ElfW2(LIBELFBITS,Shdr) *notcvt;
e39c7bc
 
f6dcaea
 	  /* All the data is already mapped.  If we could use it
a6ac629
diff -up elfutils-0.135/libelf/gelf_getsymshndx.c.robustify elfutils-0.135/libelf/gelf_getsymshndx.c
a6ac629
--- elfutils-0.135/libelf/gelf_getsymshndx.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_getsymshndx.c
f6dcaea
@@ -90,7 +90,9 @@ gelf_getsymshndx (symdata, shndxdata, nd
f6dcaea
      section index table.  */
f6dcaea
   if (likely (shndxdata_scn != NULL))
e39c7bc
     {
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf32_Word)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Word)
f6dcaea
+		       > shndxdata_scn->d.d_size))
f6dcaea
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  goto out;
f6dcaea
@@ -110,7 +112,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
2fa04c0
 	 table entries has to be adopted.  The user better has provided
2fa04c0
 	 a buffer where we can store the information.  While copying the
2fa04c0
 	 data we are converting the format.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
2fa04c0
+      if (INVALID_NDX (ndx, Elf32_Sym)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
 	  goto out;
f6dcaea
@@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
e39c7bc
 
2fa04c0
       /* The data is already in the correct form.  Just make sure the
2fa04c0
 	 index is OK.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
2fa04c0
+      if (INVALID_NDX (ndx, GElf_Sym)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
 	  goto out;
a6ac629
diff -up elfutils-0.135/libelf/gelf_update_versym.c.robustify elfutils-0.135/libelf/gelf_update_versym.c
a6ac629
--- elfutils-0.135/libelf/gelf_update_versym.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_update_versym.c
f6dcaea
@@ -75,7 +75,7 @@ gelf_update_versym (data, ndx, src)
f6dcaea
   assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
e39c7bc
 
f6dcaea
   /* Check whether we have to resize the data buffer.  */
f6dcaea
-  if (unlikely (ndx < 0)
f6dcaea
+  if (INVALID_NDX (ndx, GElf_Versym)
f6dcaea
       || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
2fa04c0
     {
f6dcaea
       __libelf_seterrno (ELF_E_INVALID_INDEX);
a6ac629
diff -up elfutils-0.135/libelf/elf_getshstrndx.c.robustify elfutils-0.135/libelf/elf_getshstrndx.c
a6ac629
--- elfutils-0.135/libelf/elf_getshstrndx.c.robustify
a6ac629
+++ elfutils-0.135/libelf/elf_getshstrndx.c
f6dcaea
@@ -125,10 +125,25 @@ elf_getshstrndx (elf, dst)
f6dcaea
 	      if (elf->map_address != NULL
f6dcaea
 		  && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
f6dcaea
 		  && (ALLOW_UNALIGNED
f6dcaea
-		      || (((size_t) ((char *) elf->map_address + offset))
f6dcaea
+		      || (((size_t) ((char *) elf->map_address
f6dcaea
+			   + elf->start_offset + offset))
f6dcaea
 			  & (__alignof__ (Elf32_Shdr) - 1)) == 0))
f6dcaea
+		{
f6dcaea
+		  /* First see whether the information in the ELF header is
f6dcaea
+		     valid and it does not ask for too much.  */
f6dcaea
+		  if (unlikely (offset + sizeof (Elf32_Shdr)
f6dcaea
+				> elf->maximum_size))
f6dcaea
+		    {
f6dcaea
+		      /* Something is wrong.  */
f6dcaea
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
f6dcaea
+		      result = -1;
f6dcaea
+		      goto out;
f6dcaea
+		    }
f6dcaea
+
f6dcaea
 		/* We can directly access the memory.  */
f6dcaea
-		num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
f6dcaea
+		  num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
f6dcaea
+					 + offset))->sh_link;
f6dcaea
+		}
f6dcaea
 	      else
f6dcaea
 		{
f6dcaea
 		  /* We avoid reading in all the section headers.  Just read
f6dcaea
@@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
f6dcaea
 	      if (elf->map_address != NULL
f6dcaea
 		  && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
f6dcaea
 		  && (ALLOW_UNALIGNED
f6dcaea
-		      || (((size_t) ((char *) elf->map_address + offset))
f6dcaea
+		      || (((size_t) ((char *) elf->map_address
f6dcaea
+			   + elf->start_offset + offset))
f6dcaea
 			  & (__alignof__ (Elf64_Shdr) - 1)) == 0))
f6dcaea
+		{
f6dcaea
+		  /* First see whether the information in the ELF header is
f6dcaea
+		     valid and it does not ask for too much.  */
f6dcaea
+		  if (unlikely (offset + sizeof (Elf64_Shdr)
f6dcaea
+				> elf->maximum_size))
f6dcaea
+		    {
f6dcaea
+		      /* Something is wrong.  */
f6dcaea
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
f6dcaea
+		      result = -1;
f6dcaea
+		      goto out;
f6dcaea
+		    }
f6dcaea
+
f6dcaea
 		/* We can directly access the memory.  */
f6dcaea
-		num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
f6dcaea
+		  num = ((Elf64_Shdr *) (elf->map_address
f6dcaea
+			 + elf->start_offset + offset))->sh_link;
f6dcaea
+		}
f6dcaea
 	      else
f6dcaea
 		{
f6dcaea
 		  /* We avoid reading in all the section headers.  Just read
a6ac629
diff -up elfutils-0.135/libelf/libelfP.h.robustify elfutils-0.135/libelf/libelfP.h
a6ac629
--- elfutils-0.135/libelf/libelfP.h.robustify
a6ac629
+++ elfutils-0.135/libelf/libelfP.h
f6dcaea
@@ -596,4 +596,13 @@ extern uint32_t __libelf_crc32 (uint32_t
f6dcaea
 /* Align offset to 4 bytes as needed for note name and descriptor data.  */
f6dcaea
 #define NOTE_ALIGN(n)	(((n) + 3) & -4U)
f6dcaea
 
f6dcaea
+/* Convenience macro.  Assumes int NDX and TYPE with size at least
f6dcaea
+   2 bytes.  */
f6dcaea
+#if SIZE_MAX > 4294967295U
f6dcaea
+# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
f6dcaea
+#else
f6dcaea
+# define INVALID_NDX(ndx, type) \
f6dcaea
+  unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
f6dcaea
+#endif
f6dcaea
+
f6dcaea
 #endif  /* libelfP.h */
a6ac629
diff -up elfutils-0.135/libelf/gelf_getmove.c.robustify elfutils-0.135/libelf/gelf_getmove.c
a6ac629
--- elfutils-0.135/libelf/gelf_getmove.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_getmove.c
2fa04c0
@@ -83,7 +83,8 @@ gelf_getmove (data, ndx, dst)
e39c7bc
 
2fa04c0
   /* The data is already in the correct form.  Just make sure the
2fa04c0
      index is OK.  */
2fa04c0
-  if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
2fa04c0
+  if (INVALID_NDX (ndx, GElf_Move)
2fa04c0
+      || unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
2fa04c0
     {
2fa04c0
       __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
       goto out;
a6ac629
diff -up elfutils-0.135/libelf/elf32_updatefile.c.robustify elfutils-0.135/libelf/elf32_updatefile.c
a6ac629
--- elfutils-0.135/libelf/elf32_updatefile.c.robustify
a6ac629
+++ elfutils-0.135/libelf/elf32_updatefile.c
f6dcaea
@@ -212,6 +212,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf
f6dcaea
   /* Write all the sections.  Well, only those which are modified.  */
f6dcaea
   if (shnum > 0)
2fa04c0
     {
f6dcaea
+      if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
f6dcaea
+ 	return 1;
f6dcaea
+
f6dcaea
       Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
f6dcaea
       Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
f6dcaea
       char *const shdr_start = ((char *) elf->map_address + elf->start_offset
f6dcaea
@@ -582,6 +585,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
f6dcaea
   /* Write all the sections.  Well, only those which are modified.  */
f6dcaea
   if (shnum > 0)
2fa04c0
     {
f6dcaea
+      if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
f6dcaea
+					+ sizeof (ElfW2(LIBELFBITS,Shdr)))))
f6dcaea
+	return 1;
f6dcaea
+
f6dcaea
       off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
f6dcaea
 #if EV_NUM != 2
f6dcaea
       xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
a6ac629
diff -up elfutils-0.135/libelf/gelf_update_lib.c.robustify elfutils-0.135/libelf/gelf_update_lib.c
a6ac629
--- elfutils-0.135/libelf/gelf_update_lib.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_update_lib.c
f6dcaea
@@ -68,12 +68,6 @@ gelf_update_lib (data, ndx, src)
2fa04c0
   if (data == NULL)
2fa04c0
     return 0;
2fa04c0
 
2fa04c0
-  if (unlikely (ndx < 0))
2fa04c0
-    {
2fa04c0
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
-      return 0;
2fa04c0
-    }
2fa04c0
-
f6dcaea
   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
f6dcaea
   if (unlikely (data_scn->d.d_type != ELF_T_LIB))
2fa04c0
     {
f6dcaea
@@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
e39c7bc
 
f6dcaea
   /* Check whether we have to resize the data buffer.  */
f6dcaea
   int result = 0;
f6dcaea
-  if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
f6dcaea
+  if (INVALID_NDX (ndx, Elf64_Lib)
f6dcaea
+      || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
f6dcaea
     __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
   else
f6dcaea
     {
a6ac629
diff -up elfutils-0.135/libelf/elf32_getphdr.c.robustify elfutils-0.135/libelf/elf32_getphdr.c
a6ac629
--- elfutils-0.135/libelf/elf32_getphdr.c.robustify
a6ac629
+++ elfutils-0.135/libelf/elf32_getphdr.c
f6dcaea
@@ -116,6 +116,16 @@ elfw2(LIBELFBITS,getphdr) (elf)
f6dcaea
 
f6dcaea
       if (elf->map_address != NULL)
e39c7bc
 	{
f6dcaea
+	  /* First see whether the information in the ELF header is
f6dcaea
+	     valid and it does not ask for too much.  */
f6dcaea
+	  if (unlikely (ehdr->e_phoff >= elf->maximum_size)
f6dcaea
+	      || unlikely (ehdr->e_phoff + size > elf->maximum_size))
f6dcaea
+	    {
f6dcaea
+	      /* Something is wrong.  */
f6dcaea
+	      __libelf_seterrno (ELF_E_INVALID_PHDR);
f6dcaea
+	      goto out;
f6dcaea
+	    }
f6dcaea
+
f6dcaea
 	  /* All the data is already mapped.  Use it.  */
f6dcaea
 	  void *file_phdr = ((char *) elf->map_address
f6dcaea
 			     + elf->start_offset + ehdr->e_phoff);
a6ac629
diff -up elfutils-0.135/libelf/gelf_getlib.c.robustify elfutils-0.135/libelf/gelf_getlib.c
a6ac629
--- elfutils-0.135/libelf/gelf_getlib.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_getlib.c
f6dcaea
@@ -86,7 +86,8 @@ gelf_getlib (data, ndx, dst)
f6dcaea
   /* The data is already in the correct form.  Just make sure the
f6dcaea
      index is OK.  */
f6dcaea
   GElf_Lib *result = NULL;
f6dcaea
-  if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
f6dcaea
+  if (INVALID_NDX (ndx, GElf_Lib)
f6dcaea
+      || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
f6dcaea
     __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
   else
2fa04c0
     {
a6ac629
diff -up elfutils-0.135/libelf/elf32_newphdr.c.robustify elfutils-0.135/libelf/elf32_newphdr.c
a6ac629
--- elfutils-0.135/libelf/elf32_newphdr.c.robustify
a6ac629
+++ elfutils-0.135/libelf/elf32_newphdr.c
f6dcaea
@@ -124,6 +124,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
f6dcaea
   else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
f6dcaea
 	   || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
f6dcaea
     {
f6dcaea
+      if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
f6dcaea
+	{
f6dcaea
+	  result = NULL;
f6dcaea
+	  goto out;
f6dcaea
+	}
f6dcaea
+
f6dcaea
       /* Allocate a new program header with the appropriate number of
f6dcaea
 	 elements.  */
f6dcaea
       result = (ElfW2(LIBELFBITS,Phdr) *)
a6ac629
diff -up elfutils-0.135/libelf/gelf_update_rela.c.robustify elfutils-0.135/libelf/gelf_update_rela.c
a6ac629
--- elfutils-0.135/libelf/gelf_update_rela.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_update_rela.c
2fa04c0
@@ -68,12 +68,6 @@ gelf_update_rela (Elf_Data *dst, int ndx
2fa04c0
   if (dst == NULL)
2fa04c0
     return 0;
e39c7bc
 
2fa04c0
-  if (unlikely (ndx < 0))
2fa04c0
-    {
2fa04c0
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
-      return 0;
2fa04c0
-    }
2fa04c0
-
2fa04c0
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
2fa04c0
     {
2fa04c0
       /* The type of the data better should match.  */
2fa04c0
@@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
2fa04c0
 	}
e39c7bc
 
2fa04c0
       /* Check whether we have to resize the data buffer.  */
2fa04c0
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
2fa04c0
+      if (INVALID_NDX (ndx, Elf32_Rela)
2fa04c0
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
 	  goto out;
2fa04c0
@@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
2fa04c0
   else
2fa04c0
     {
2fa04c0
       /* Check whether we have to resize the data buffer.  */
2fa04c0
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
2fa04c0
+      if (INVALID_NDX (ndx, Elf64_Rela)
2fa04c0
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
 	  goto out;
a6ac629
diff -up elfutils-0.135/libelf/gelf_update_syminfo.c.robustify elfutils-0.135/libelf/gelf_update_syminfo.c
a6ac629
--- elfutils-0.135/libelf/gelf_update_syminfo.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_update_syminfo.c
f6dcaea
@@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
f6dcaea
   if (data == NULL)
f6dcaea
     return 0;
f6dcaea
 
f6dcaea
-  if (unlikely (ndx < 0))
f6dcaea
-    {
f6dcaea
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
-      return 0;
f6dcaea
-    }
f6dcaea
-
f6dcaea
   if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
2fa04c0
     {
f6dcaea
       /* The type of the data better should match.  */
f6dcaea
@@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
f6dcaea
   rwlock_wrlock (scn->elf->lock);
e39c7bc
 
f6dcaea
   /* Check whether we have to resize the data buffer.  */
f6dcaea
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
f6dcaea
+  if (INVALID_NDX (ndx, GElf_Syminfo)
f6dcaea
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
2fa04c0
     {
f6dcaea
       __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
       goto out;
a6ac629
diff -up elfutils-0.135/libelf/gelf_update_sym.c.robustify elfutils-0.135/libelf/gelf_update_sym.c
a6ac629
--- elfutils-0.135/libelf/gelf_update_sym.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_update_sym.c
2fa04c0
@@ -72,12 +72,6 @@ gelf_update_sym (data, ndx, src)
2fa04c0
   if (data == NULL)
2fa04c0
     return 0;
e39c7bc
 
2fa04c0
-  if (unlikely (ndx < 0))
2fa04c0
-    {
2fa04c0
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
-      return 0;
2fa04c0
-    }
2fa04c0
-
2fa04c0
   if (unlikely (data_scn->d.d_type != ELF_T_SYM))
2fa04c0
     {
2fa04c0
       /* The type of the data better should match.  */
2fa04c0
@@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
2fa04c0
 	}
e39c7bc
 
2fa04c0
       /* Check whether we have to resize the data buffer.  */
2fa04c0
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
2fa04c0
+      if (INVALID_NDX (ndx, Elf32_Sym)
2fa04c0
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
 	  goto out;
2fa04c0
@@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
2fa04c0
   else
2fa04c0
     {
2fa04c0
       /* Check whether we have to resize the data buffer.  */
2fa04c0
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
2fa04c0
+      if (INVALID_NDX (ndx, Elf64_Sym)
2fa04c0
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
 	  goto out;
a6ac629
diff -up elfutils-0.135/libelf/gelf_getsyminfo.c.robustify elfutils-0.135/libelf/gelf_getsyminfo.c
a6ac629
--- elfutils-0.135/libelf/gelf_getsyminfo.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_getsyminfo.c
2fa04c0
@@ -84,7 +84,8 @@ gelf_getsyminfo (data, ndx, dst)
e39c7bc
 
2fa04c0
   /* The data is already in the correct form.  Just make sure the
2fa04c0
      index is OK.  */
2fa04c0
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
2fa04c0
+  if (INVALID_NDX (ndx, GElf_Syminfo)
2fa04c0
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
2fa04c0
     {
2fa04c0
       __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
       goto out;
a6ac629
diff -up elfutils-0.135/libelf/gelf_update_symshndx.c.robustify elfutils-0.135/libelf/gelf_update_symshndx.c
a6ac629
--- elfutils-0.135/libelf/gelf_update_symshndx.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_update_symshndx.c
f6dcaea
@@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata
f6dcaea
   if (symdata == NULL)
f6dcaea
     return 0;
f6dcaea
 
f6dcaea
-  if (unlikely (ndx < 0))
f6dcaea
-    {
f6dcaea
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
-      return 0;
f6dcaea
-    }
f6dcaea
-
f6dcaea
   if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
2fa04c0
     {
f6dcaea
       /* The type of the data better should match.  */
f6dcaea
@@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
f6dcaea
 	}
e39c7bc
 
f6dcaea
       /* Check whether we have to resize the data buffer.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf32_Sym)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
f6dcaea
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  goto out;
f6dcaea
@@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
f6dcaea
   else
2fa04c0
     {
f6dcaea
       /* Check whether we have to resize the data buffer.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf64_Sym)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
f6dcaea
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  goto out;
a6ac629
diff -up elfutils-0.135/libelf/elf_getarsym.c.robustify elfutils-0.135/libelf/elf_getarsym.c
a6ac629
--- elfutils-0.135/libelf/elf_getarsym.c.robustify
a6ac629
+++ elfutils-0.135/libelf/elf_getarsym.c
2fa04c0
@@ -179,6 +179,9 @@ elf_getarsym (elf, ptr)
2fa04c0
       size_t index_size = atol (tmpbuf);
e39c7bc
 
2fa04c0
       if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
2fa04c0
+#if SIZE_MAX <= 4294967295U
2fa04c0
+	  || n >= SIZE_MAX / sizeof (Elf_Arsym)
2fa04c0
+#endif
2fa04c0
 	  || n * sizeof (uint32_t) > index_size)
2fa04c0
 	{
2fa04c0
 	  /* This index table cannot be right since it does not fit into
a6ac629
diff -up elfutils-0.135/libelf/gelf_getsym.c.robustify elfutils-0.135/libelf/gelf_getsym.c
a6ac629
--- elfutils-0.135/libelf/gelf_getsym.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_getsym.c
f6dcaea
@@ -90,7 +90,8 @@ gelf_getsym (data, ndx, dst)
f6dcaea
 	 table entries has to be adopted.  The user better has provided
f6dcaea
 	 a buffer where we can store the information.  While copying the
f6dcaea
 	 data we are converting the format.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf32_Sym)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
f6dcaea
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  goto out;
f6dcaea
@@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
f6dcaea
 
f6dcaea
       /* The data is already in the correct form.  Just make sure the
f6dcaea
 	 index is OK.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
f6dcaea
+      if (INVALID_NDX (ndx, GElf_Sym)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
f6dcaea
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  goto out;
a6ac629
diff -up elfutils-0.135/libelf/elf_begin.c.robustify elfutils-0.135/libelf/elf_begin.c
a6ac629
--- elfutils-0.135/libelf/elf_begin.c.robustify
a6ac629
+++ elfutils-0.135/libelf/elf_begin.c
f6dcaea
@@ -155,7 +155,8 @@ get_shnum (void *map_address, unsigned c
f6dcaea
 
f6dcaea
       if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
f6dcaea
 	{
f6dcaea
-	  if (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
f6dcaea
+	  if (unlikely (ehdr.e32->e_shoff >= maxsize)
f6dcaea
+	      || unlikely (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize))
f6dcaea
 	    /* Cannot read the first section header.  */
f6dcaea
 	    return 0;
f6dcaea
 
f6dcaea
@@ -203,7 +204,8 @@ get_shnum (void *map_address, unsigned c
f6dcaea
 
f6dcaea
       if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
f6dcaea
 	{
f6dcaea
-	  if (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
f6dcaea
+	  if (unlikely (ehdr.e64->e_shoff >= maxsize)
f6dcaea
+	      || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
f6dcaea
 	    /* Cannot read the first section header.  */
f6dcaea
 	    return 0;
f6dcaea
 
f6dcaea
@@ -275,6 +277,15 @@ file_read_elf (int fildes, void *map_add
f6dcaea
     /* Could not determine the number of sections.  */
2fa04c0
     return NULL;
e39c7bc
 
f6dcaea
+  /* Check for too many sections.  */
f6dcaea
+  if (e_ident[EI_CLASS] == ELFCLASS32)
f6dcaea
+    {
f6dcaea
+      if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
f6dcaea
+	return NULL;
f6dcaea
+    }
f6dcaea
+  else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
f6dcaea
+    return NULL;
f6dcaea
+
f6dcaea
   /* We can now allocate the memory.  */
f6dcaea
   Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
f6dcaea
 			   ELF_K_ELF, scncnt * sizeof (Elf_Scn));
f6dcaea
@@ -308,13 +319,31 @@ file_read_elf (int fildes, void *map_add
f6dcaea
 	{
f6dcaea
 	  /* We can use the mmapped memory.  */
f6dcaea
 	  elf->state.elf32.ehdr = ehdr;
f6dcaea
+
f6dcaea
+	  if (unlikely (ehdr->e_shoff >= maxsize)
f6dcaea
+	      || unlikely (ehdr->e_shoff
f6dcaea
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
f6dcaea
+	    {
f6dcaea
+	    free_and_out:
f6dcaea
+	      free (elf);
f6dcaea
+	      __libelf_seterrno (ELF_E_INVALID_FILE);
f6dcaea
+	      return NULL;
f6dcaea
+	    }
f6dcaea
 	  elf->state.elf32.shdr
f6dcaea
 	    = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
f6dcaea
+
f6dcaea
 	  if (ehdr->e_phnum > 0)
f6dcaea
+	    {
f6dcaea
 	    /* Assign a value only if there really is a program
f6dcaea
 	       header.  Otherwise the value remains NULL.  */
f6dcaea
+	      if (unlikely (ehdr->e_phoff >= maxsize)
f6dcaea
+		  || unlikely (ehdr->e_phoff
f6dcaea
+			       + ehdr->e_phnum
f6dcaea
+			       * sizeof (Elf32_Phdr) > maxsize))
f6dcaea
+		goto free_and_out;
f6dcaea
 	    elf->state.elf32.phdr
f6dcaea
 	      = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
f6dcaea
+	    }
f6dcaea
 
f6dcaea
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
f6dcaea
 	    {
f6dcaea
@@ -396,13 +425,26 @@ file_read_elf (int fildes, void *map_add
f6dcaea
 	{
f6dcaea
 	  /* We can use the mmapped memory.  */
f6dcaea
 	  elf->state.elf64.ehdr = ehdr;
f6dcaea
+
f6dcaea
+	  if (unlikely (ehdr->e_shoff >= maxsize)
f6dcaea
+	      || unlikely (ehdr->e_shoff
f6dcaea
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
f6dcaea
+	    goto free_and_out;
f6dcaea
 	  elf->state.elf64.shdr
f6dcaea
 	    = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
f6dcaea
+
f6dcaea
 	  if (ehdr->e_phnum > 0)
f6dcaea
+	    {
f6dcaea
 	    /* Assign a value only if there really is a program
f6dcaea
 	       header.  Otherwise the value remains NULL.  */
f6dcaea
+	      if (unlikely (ehdr->e_phoff >= maxsize)
f6dcaea
+		  || unlikely (ehdr->e_phoff
f6dcaea
+			       + ehdr->e_phnum
f6dcaea
+			       * sizeof (Elf32_Phdr) > maxsize))
f6dcaea
+		goto free_and_out;
f6dcaea
 	    elf->state.elf64.phdr
f6dcaea
 	      = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
f6dcaea
+	    }
f6dcaea
 
f6dcaea
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
f6dcaea
 	    {
a6ac629
diff -up elfutils-0.135/libelf/gelf_update_rel.c.robustify elfutils-0.135/libelf/gelf_update_rel.c
a6ac629
--- elfutils-0.135/libelf/gelf_update_rel.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_update_rel.c
f6dcaea
@@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx,
f6dcaea
   if (dst == NULL)
f6dcaea
     return 0;
f6dcaea
 
2fa04c0
-  if (unlikely (ndx < 0))
2fa04c0
-    {
2fa04c0
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
-      return 0;
2fa04c0
-    }
2fa04c0
-
2fa04c0
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
2fa04c0
     {
f6dcaea
       /* The type of the data better should match.  */
f6dcaea
@@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
f6dcaea
 	}
f6dcaea
 
f6dcaea
       /* Check whether we have to resize the data buffer.  */
2fa04c0
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
2fa04c0
+      if (INVALID_NDX (ndx, Elf32_Rel)
2fa04c0
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  goto out;
f6dcaea
@@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
f6dcaea
   else
2fa04c0
     {
f6dcaea
       /* Check whether we have to resize the data buffer.  */
2fa04c0
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
2fa04c0
+      if (INVALID_NDX (ndx, Elf64_Rel)
2fa04c0
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  goto out;
a6ac629
diff -up elfutils-0.135/libelf/gelf_update_move.c.robustify elfutils-0.135/libelf/gelf_update_move.c
a6ac629
--- elfutils-0.135/libelf/gelf_update_move.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_update_move.c
f6dcaea
@@ -75,7 +75,7 @@ gelf_update_move (data, ndx, src)
f6dcaea
   assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
e39c7bc
 
2fa04c0
   /* Check whether we have to resize the data buffer.  */
2fa04c0
-  if (unlikely (ndx < 0)
f6dcaea
+  if (INVALID_NDX (ndx, GElf_Move)
f6dcaea
       || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
2fa04c0
     {
2fa04c0
       __libelf_seterrno (ELF_E_INVALID_INDEX);
a6ac629
diff -up elfutils-0.135/libelf/gelf_update_dyn.c.robustify elfutils-0.135/libelf/gelf_update_dyn.c
a6ac629
--- elfutils-0.135/libelf/gelf_update_dyn.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_update_dyn.c
f6dcaea
@@ -71,12 +71,6 @@ gelf_update_dyn (data, ndx, src)
2fa04c0
   if (data == NULL)
2fa04c0
     return 0;
e39c7bc
 
2fa04c0
-  if (unlikely (ndx < 0))
2fa04c0
-    {
2fa04c0
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
-      return 0;
2fa04c0
-    }
2fa04c0
-
f6dcaea
   if (unlikely (data_scn->d.d_type != ELF_T_DYN))
2fa04c0
     {
2fa04c0
       /* The type of the data better should match.  */
f6dcaea
@@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
2fa04c0
 	}
e39c7bc
 
2fa04c0
       /* Check whether we have to resize the data buffer.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf32_Dyn)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
 	  goto out;
f6dcaea
@@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
2fa04c0
   else
2fa04c0
     {
2fa04c0
       /* Check whether we have to resize the data buffer.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf64_Dyn)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
2fa04c0
 	{
2fa04c0
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
2fa04c0
 	  goto out;
a6ac629
diff -up elfutils-0.135/libelf/gelf_getrela.c.robustify elfutils-0.135/libelf/gelf_getrela.c
a6ac629
--- elfutils-0.135/libelf/gelf_getrela.c.robustify
a6ac629
+++ elfutils-0.135/libelf/gelf_getrela.c
f6dcaea
@@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
f6dcaea
   if (data_scn == NULL)
f6dcaea
     return NULL;
e39c7bc
 
2fa04c0
-  if (unlikely (ndx < 0))
2fa04c0
-    {
2fa04c0
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
-      return NULL;
2fa04c0
-    }
2fa04c0
-
f6dcaea
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
2fa04c0
     {
f6dcaea
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
f6dcaea
@@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
f6dcaea
   if (scn->elf->class == ELFCLASS32)
2fa04c0
     {
f6dcaea
       /* We have to convert the data.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf32_Rela)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
2fa04c0
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  result = NULL;
f6dcaea
@@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
f6dcaea
     {
f6dcaea
       /* Simply copy the data after we made sure we are actually getting
f6dcaea
 	 correct data.  */
f6dcaea
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
f6dcaea
+      if (INVALID_NDX (ndx, Elf64_Rela)
f6dcaea
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
2fa04c0
 	{
f6dcaea
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
f6dcaea
 	  result = NULL;