bf5b25
src/
bf5b25
2005-06-09  Roland McGrath  <roland@redhat.com>
bf5b25
bf5b25
	* readelf.c (handle_dynamic, handle_symtab): Check for bogus sh_link.
bf5b25
	(handle_verneed, handle_verdef, handle_versym, handle_hash): Likewise.
bf5b25
	(handle_scngrp): Check for bogus sh_info.
bf5b25
bf5b25
	* strip.c (handle_elf): Check for bogus values in sh_link, sh_info,
bf5b25
	st_shndx, e_shstrndx, and SHT_GROUP or SHT_SYMTAB_SHNDX data.
bf5b25
	Don't use assert on input values, instead bail with "illformed" error.
bf5b25
bf5b25
2005-05-17  Jakub Jelinek  <jakub@redhat.com>
bf5b25
bf5b25
libelf/
bf5b25
	* elf32_getphdr.c (elfw2(LIBELFBITS,getphdr)): Check if program header
bf5b25
	table fits into object's bounds.
bf5b25
	* elf_getshstrndx.c (elf_getshstrndx): Add elf->start_offset to
bf5b25
	elf->map_address.  Check if first section header fits into object's
bf5b25
	bounds.
bf5b25
	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Fix comment pasto.
bf5b25
	Check if section header table fits into object's bounds.
65b080
	* elf_begin.c (get_shnum): Ensure section headers fits into
65b080
	object's bounds.
bf5b25
	(file_read_elf): Make sure scncnt is small enough to allocate both
bf5b25
	ElfXX_Shdr and Elf_Scn array.  Make sure section and program header
bf5b25
	tables fit into object's bounds.  Avoid memory leak on failure.
bf5b25
bf5b25
src/
bf5b25
	* elflint.c (check_hash): Don't check entries beyond end of section.
bf5b25
	(check_note): Don't crash if gelf_rawchunk fails.
bf5b25
	(section_name): Return <invalid> if gelf_getshdr returns NULL.
bf5b25
bf5b25
2005-05-14  Jakub Jelinek  <jakub@redhat.com>
bf5b25
bf5b25
libelf/
bf5b25
	* libelfP.h (INVALID_NDX): Define.
bf5b25
	* gelf_getdyn.c (gelf_getdyn): Use it.  Remove ndx < 0 test if any.
bf5b25
	* gelf_getlib.c (gelf_getlib): Likewise.
bf5b25
	* gelf_getmove.c (gelf_getmove): Likewise.
bf5b25
	* gelf_getrel.c (gelf_getrel): Likewise.
bf5b25
	* gelf_getrela.c (gelf_getrela): Likewise.
bf5b25
	* gelf_getsym.c (gelf_getsym): Likewise.
bf5b25
	* gelf_getsyminfo.c (gelf_getsyminfo): Likewise.
bf5b25
	* gelf_getsymshndx.c (gelf_getsymshndx): Likewise.
bf5b25
	* gelf_getversym.c (gelf_getversym): Likewise.
bf5b25
	* gelf_update_dyn.c (gelf_update_dyn): Likewise.
bf5b25
	* gelf_update_lib.c (gelf_update_lib): Likewise.
bf5b25
	* gelf_update_move.c (gelf_update_move): Likewise.
bf5b25
	* gelf_update_rel.c (gelf_update_rel): Likewise.
bf5b25
	* gelf_update_rela.c (gelf_update_rela): Likewise.
bf5b25
	* gelf_update_sym.c (gelf_update_sym): Likewise.
bf5b25
	* gelf_update_syminfo.c (gelf_update_syminfo): Likewise.
bf5b25
	* gelf_update_symshndx.c (gelf_update_symshndx): Likewise.
bf5b25
	* gelf_update_versym.c (gelf_update_versym): Likewise.
bf5b25
	* elf_newscn.c (elf_newscn): Check for overflow.
bf5b25
	* elf32_updatefile.c (__elfw2(LIBELFBITS,updatemmap)): Likewise.
bf5b25
	(__elfw2(LIBELFBITS,updatefile)): Likewise.
bf5b25
	* elf_begin.c (file_read_elf): Likewise.
bf5b25
	* elf32_newphdr.c (elfw2(LIBELFBITS,newphdr)): Likewise.
bf5b25
	* elf_getarsym.c (elf_getarsym): Likewise.
bf5b25
	* elf32_getshdr.c (elfw2(LIBELFBITS,getshdr)): Likewise.
bf5b25
src/
bf5b25
	* elflint.c (section_name): Return "<invalid>" instead of
bf5b25
	crashing on invalid section name.
bf5b25
	(check_symtab, is_rel_dyn, check_rela, check_rel, check_dynamic,
bf5b25
	check_symtab_shndx, check_hash, check_versym): Robustify.
bf5b25
a78f3e
--- elfutils-0.120/libelf/elf32_getphdr.c
a78f3e
+++ elfutils-0.120/libelf/elf32_getphdr.c
a78f3e
@@ -115,6 +115,16 @@ elfw2(LIBELFBITS,getphdr) (elf)
bf5b25
 
bf5b25
       if (elf->map_address != NULL)
bf5b25
 	{
bf5b25
+	  /* First see whether the information in the ELF header is
bf5b25
+	     valid and it does not ask for too much.  */
bf5b25
+	  if (unlikely (ehdr->e_phoff >= elf->maximum_size)
bf5b25
+	      || unlikely (ehdr->e_phoff + size > elf->maximum_size))
bf5b25
+	    {
bf5b25
+	      /* Something is wrong.  */
bf5b25
+	      __libelf_seterrno (ELF_E_INVALID_PHDR);
bf5b25
+	      goto out;
bf5b25
+	    }
bf5b25
+
bf5b25
 	  /* All the data is already mapped.  Use it.  */
bf5b25
 	  if (ehdr->e_ident[EI_DATA] == MY_ELFDATA
bf5b25
 	      && (ALLOW_UNALIGNED
a78f3e
--- elfutils-0.120/libelf/elf32_getshdr.c
a78f3e
+++ elfutils-0.120/libelf/elf32_getshdr.c
a78f3e
@@ -101,11 +101,12 @@ elfw2(LIBELFBITS,getshdr) (scn)
bf5b25
 	goto out;
bf5b25
 
bf5b25
       size_t shnum;
bf5b25
-      if (INTUSE (elf_getshnum) (elf, &shnum) != 0)
bf5b25
+      if (INTUSE (elf_getshnum) (elf, &shnum) != 0
bf5b25
+	  || shnum > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Shdr)))
bf5b25
 	goto out;
bf5b25
       size_t size = shnum * sizeof (ElfW2(LIBELFBITS,Shdr));
bf5b25
 
bf5b25
-      /* Allocate memory for the program headers.  We know the number
bf5b25
+      /* Allocate memory for the section headers.  We know the number
bf5b25
 	 of entries from the ELF header.  */
bf5b25
       ElfW2(LIBELFBITS,Shdr) *shdr = elf->state.ELFW(elf,LIBELFBITS).shdr =
bf5b25
 	(ElfW2(LIBELFBITS,Shdr) *) malloc (size);
a78f3e
@@ -128,6 +129,16 @@ elfw2(LIBELFBITS,getshdr) (scn)
a78f3e
 			   + ehdr->e_shoff)
bf5b25
 			  & (__alignof__ (ElfW2(LIBELFBITS,Shdr)) - 1)) != 0));
bf5b25
 
bf5b25
+	  /* First see whether the information in the ELF header is
bf5b25
+	     valid and it does not ask for too much.  */
bf5b25
+	  if (unlikely (ehdr->e_shoff >= elf->maximum_size)
bf5b25
+	      || unlikely (ehdr->e_shoff + size > elf->maximum_size))
bf5b25
+	    {
bf5b25
+	      /* Something is wrong.  */
bf5b25
+	      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
bf5b25
+	      goto free_and_out;
bf5b25
+	    }
bf5b25
+
bf5b25
 	  /* Now copy the data and at the same time convert the byte
bf5b25
 	     order.  */
bf5b25
 	  if (ALLOW_UNALIGNED
a78f3e
--- elfutils-0.120/libelf/elf32_newphdr.c
a78f3e
+++ elfutils-0.120/libelf/elf32_newphdr.c
a78f3e
@@ -124,6 +124,12 @@ elfw2(LIBELFBITS,newphdr) (elf, count)
bf5b25
   else if (elf->state.ELFW(elf,LIBELFBITS).ehdr->e_phnum != count
bf5b25
 	   || elf->state.ELFW(elf,LIBELFBITS).phdr == NULL)
bf5b25
     {
bf5b25
+      if (unlikely (count > SIZE_MAX / sizeof (ElfW2(LIBELFBITS,Phdr))))
bf5b25
+	{
bf5b25
+	  result = NULL;
bf5b25
+	  goto out;
bf5b25
+	}
bf5b25
+
bf5b25
       /* Allocate a new program header with the appropriate number of
bf5b25
 	 elements.  */
bf5b25
       result = (ElfW2(LIBELFBITS,Phdr) *)
a78f3e
--- elfutils-0.120/libelf/elf32_updatefile.c
a78f3e
+++ elfutils-0.120/libelf/elf32_updatefile.c
a78f3e
@@ -201,6 +201,9 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf
bf5b25
   /* Write all the sections.  Well, only those which are modified.  */
bf5b25
   if (shnum > 0)
bf5b25
     {
bf5b25
+      if (unlikely (shnum > SIZE_MAX / sizeof (Elf_Scn *)))
a78f3e
+ 	return 1;
bf5b25
+
bf5b25
       Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
bf5b25
       Elf_Scn **scns = (Elf_Scn **) alloca (shnum * sizeof (Elf_Scn *));
a78f3e
       char *const shdr_start = ((char *) elf->map_address + elf->start_offset
a78f3e
@@ -545,6 +548,10 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf
bf5b25
   /* Write all the sections.  Well, only those which are modified.  */
bf5b25
   if (shnum > 0)
bf5b25
     {
bf5b25
+      if (unlikely (shnum > SIZE_MAX / (sizeof (Elf_Scn *)
bf5b25
+					+ sizeof (ElfW2(LIBELFBITS,Shdr)))))
bf5b25
+	return 1;
bf5b25
+
bf5b25
       off_t shdr_offset = elf->start_offset + ehdr->e_shoff;
bf5b25
 #if EV_NUM != 2
bf5b25
       xfct_t shdr_fctp = __elf_xfctstom[__libelf_version - 1][EV_CURRENT - 1][ELFW(ELFCLASS, LIBELFBITS) - 1][ELF_T_SHDR];
a78f3e
--- elfutils-0.120/libelf/elf_begin.c
a78f3e
+++ elfutils-0.120/libelf/elf_begin.c
a78f3e
@@ -155,7 +155,8 @@ get_shnum (void *map_address, unsigned c
bf5b25
 
bf5b25
       if (unlikely (result == 0) && ehdr.e32->e_shoff != 0)
bf5b25
 	{
bf5b25
-	  if (offset + ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize)
bf5b25
+	  if (unlikely (ehdr.e32->e_shoff >= maxsize)
bf5b25
+	      || unlikely (ehdr.e32->e_shoff + sizeof (Elf32_Shdr) > maxsize))
bf5b25
 	    /* Cannot read the first section header.  */
bf5b25
 	    return (size_t) -1l;
bf5b25
 
a78f3e
@@ -198,7 +199,8 @@ get_shnum (void *map_address, unsigned c
bf5b25
 
bf5b25
       if (unlikely (result == 0) && ehdr.e64->e_shoff != 0)
bf5b25
 	{
bf5b25
-	  if (offset + ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize)
bf5b25
+	  if (unlikely (ehdr.e64->e_shoff >= maxsize)
bf5b25
+	      || unlikely (ehdr.e64->e_shoff + sizeof (Elf64_Shdr) > maxsize))
bf5b25
 	    /* Cannot read the first section header.  */
bf5b25
 	    return (size_t) -1l;
bf5b25
 
a78f3e
@@ -265,6 +267,15 @@ file_read_elf (int fildes, void *map_add
bf5b25
     /* Could not determine the number of sections.  */
bf5b25
     return NULL;
bf5b25
 
bf5b25
+  /* Check for too many sections.  */
bf5b25
+  if (e_ident[EI_CLASS] == ELFCLASS32)
bf5b25
+    {
bf5b25
+      if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf32_Shdr)))
bf5b25
+	return NULL;
bf5b25
+    }
bf5b25
+  else if (scncnt > SIZE_MAX / (sizeof (Elf_Scn) + sizeof (Elf64_Shdr)))
bf5b25
+    return NULL;
bf5b25
+
bf5b25
   /* We can now allocate the memory.  */
65b080
   Elf *elf = allocate_elf (fildes, map_address, offset, maxsize, cmd, parent,
65b080
 			   ELF_K_ELF, scncnt * sizeof (Elf_Scn));
a78f3e
@@ -298,13 +309,31 @@ file_read_elf (int fildes, void *map_add
65b080
 	{
bf5b25
 	  /* We can use the mmapped memory.  */
65b080
 	  elf->state.elf32.ehdr = ehdr;
65b080
+
65b080
+	  if (unlikely (ehdr->e_shoff >= maxsize)
65b080
+	      || unlikely (ehdr->e_shoff
bf5b25
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
bf5b25
+	    {
bf5b25
+	    free_and_out:
bf5b25
+	      free (elf);
65b080
+	      __libelf_seterrno (ELF_E_INVALID_FILE);
bf5b25
+	      return NULL;
bf5b25
+	    }
65b080
 	  elf->state.elf32.shdr
65b080
 	    = (Elf32_Shdr *) ((char *) ehdr + ehdr->e_shoff);
65b080
+
65b080
 	  if (ehdr->e_phnum > 0)
bf5b25
-	    /* Assign a value only if there really is a program
bf5b25
-	       header.  Otherwise the value remains NULL.  */
bf5b25
-	    elf->state.elf32.phdr
65b080
-	      = (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
bf5b25
+	    {
bf5b25
+	      /* Assign a value only if there really is a program
bf5b25
+		 header.  Otherwise the value remains NULL.  */
65b080
+	      if (unlikely (ehdr->e_phoff >= maxsize)
65b080
+		  || unlikely (ehdr->e_phoff
65b080
+			       + ehdr->e_phnum
bf5b25
+			       * sizeof (Elf32_Phdr) > maxsize))
bf5b25
+		goto free_and_out;
bf5b25
+	      elf->state.elf32.phdr
65b080
+		= (Elf32_Phdr *) ((char *) ehdr + ehdr->e_phoff);
bf5b25
+	    }
bf5b25
 
bf5b25
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
bf5b25
 	    {
a78f3e
@@ -373,13 +402,26 @@ file_read_elf (int fildes, void *map_add
65b080
 	{
bf5b25
 	  /* We can use the mmapped memory.  */
65b080
 	  elf->state.elf64.ehdr = ehdr;
65b080
+
65b080
+	  if (unlikely (ehdr->e_shoff >= maxsize)
65b080
+	      || unlikely (ehdr->e_shoff
65b080
+			   + scncnt * sizeof (Elf32_Shdr) > maxsize))
bf5b25
+	    goto free_and_out;
65b080
 	  elf->state.elf64.shdr
65b080
 	    = (Elf64_Shdr *) ((char *) ehdr + ehdr->e_shoff);
65b080
+
65b080
 	  if (ehdr->e_phnum > 0)
bf5b25
-	    /* Assign a value only if there really is a program
bf5b25
-	       header.  Otherwise the value remains NULL.  */
bf5b25
-	    elf->state.elf64.phdr
65b080
-	      = (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
bf5b25
+	    {
bf5b25
+	      /* Assign a value only if there really is a program
bf5b25
+		 header.  Otherwise the value remains NULL.  */
65b080
+	      if (unlikely (ehdr->e_phoff >= maxsize)
65b080
+		  || unlikely (ehdr->e_phoff
65b080
+			       + ehdr->e_phnum
65b080
+			       * sizeof (Elf32_Phdr) > maxsize))
bf5b25
+		goto free_and_out;
bf5b25
+	      elf->state.elf64.phdr
65b080
+		= (Elf64_Phdr *) ((char *) ehdr + ehdr->e_phoff);
bf5b25
+	    }
bf5b25
 
bf5b25
 	  for (size_t cnt = 0; cnt < scncnt; ++cnt)
bf5b25
 	    {
a78f3e
--- elfutils-0.120/libelf/elf_getarsym.c
a78f3e
+++ elfutils-0.120/libelf/elf_getarsym.c
a78f3e
@@ -179,6 +179,9 @@ elf_getarsym (elf, ptr)
bf5b25
       size_t index_size = atol (tmpbuf);
bf5b25
 
bf5b25
       if (SARMAG + sizeof (struct ar_hdr) + index_size > elf->maximum_size
bf5b25
+#if SIZE_MAX <= 4294967295U
bf5b25
+	  || n >= SIZE_MAX / sizeof (Elf_Arsym)
bf5b25
+#endif
bf5b25
 	  || n * sizeof (uint32_t) > index_size)
bf5b25
 	{
bf5b25
 	  /* This index table cannot be right since it does not fit into
a78f3e
--- elfutils-0.120/libelf/elf_getshstrndx.c
a78f3e
+++ elfutils-0.120/libelf/elf_getshstrndx.c
a78f3e
@@ -125,10 +125,25 @@ elf_getshstrndx (elf, dst)
bf5b25
 	      if (elf->map_address != NULL
bf5b25
 		  && elf->state.elf32.ehdr->e_ident[EI_DATA] == MY_ELFDATA
bf5b25
 		  && (ALLOW_UNALIGNED
bf5b25
-		      || (((size_t) ((char *) elf->map_address + offset))
bf5b25
+		      || (((size_t) ((char *) elf->map_address
bf5b25
+			   + elf->start_offset + offset))
bf5b25
 			  & (__alignof__ (Elf32_Shdr) - 1)) == 0))
bf5b25
-		/* We can directly access the memory.  */
bf5b25
-		num = ((Elf32_Shdr *) (elf->map_address + offset))->sh_link;
bf5b25
+		{
bf5b25
+		  /* First see whether the information in the ELF header is
bf5b25
+		     valid and it does not ask for too much.  */
bf5b25
+		  if (unlikely (offset + sizeof (Elf32_Shdr)
bf5b25
+				> elf->maximum_size))
bf5b25
+		    {
bf5b25
+		      /* Something is wrong.  */
bf5b25
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
bf5b25
+		      result = -1;
bf5b25
+		      goto out;
bf5b25
+		    }
bf5b25
+
bf5b25
+		  /* We can directly access the memory.  */
bf5b25
+		  num = ((Elf32_Shdr *) (elf->map_address + elf->start_offset
65b080
+					 + offset))->sh_link;
bf5b25
+		}
bf5b25
 	      else
bf5b25
 		{
bf5b25
 		  /* We avoid reading in all the section headers.  Just read
a78f3e
@@ -163,10 +178,25 @@ elf_getshstrndx (elf, dst)
bf5b25
 	      if (elf->map_address != NULL
bf5b25
 		  && elf->state.elf64.ehdr->e_ident[EI_DATA] == MY_ELFDATA
bf5b25
 		  && (ALLOW_UNALIGNED
bf5b25
-		      || (((size_t) ((char *) elf->map_address + offset))
bf5b25
+		      || (((size_t) ((char *) elf->map_address
bf5b25
+			   + elf->start_offset + offset))
bf5b25
 			  & (__alignof__ (Elf64_Shdr) - 1)) == 0))
bf5b25
-		/* We can directly access the memory.  */
bf5b25
-		num = ((Elf64_Shdr *) (elf->map_address + offset))->sh_link;
bf5b25
+		{
bf5b25
+		  /* First see whether the information in the ELF header is
bf5b25
+		     valid and it does not ask for too much.  */
bf5b25
+		  if (unlikely (offset + sizeof (Elf64_Shdr)
bf5b25
+				> elf->maximum_size))
bf5b25
+		    {
bf5b25
+		      /* Something is wrong.  */
bf5b25
+		      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
bf5b25
+		      result = -1;
bf5b25
+		      goto out;
bf5b25
+		    }
bf5b25
+
bf5b25
+		  /* We can directly access the memory.  */
bf5b25
+		  num = ((Elf64_Shdr *) (elf->map_address
bf5b25
+			 + elf->start_offset + offset))->sh_link;
bf5b25
+		}
bf5b25
 	      else
bf5b25
 		{
bf5b25
 		  /* We avoid reading in all the section headers.  Just read
a78f3e
--- elfutils-0.120/libelf/elf_newscn.c
a78f3e
+++ elfutils-0.120/libelf/elf_newscn.c
a78f3e
@@ -104,13 +104,21 @@ elf_newscn (elf)
bf5b25
   else
bf5b25
     {
bf5b25
       /* We must allocate a new element.  */
bf5b25
-      Elf_ScnList *newp;
bf5b25
+      Elf_ScnList *newp = NULL;
bf5b25
 
bf5b25
       assert (elf->state.elf.scnincr > 0);
bf5b25
 
bf5b25
-      newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
bf5b25
-				     + ((elf->state.elf.scnincr *= 2)
bf5b25
-					* sizeof (Elf_Scn)), 1);
bf5b25
+      if (
bf5b25
+#if SIZE_MAX <= 4294967295U
bf5b25
+	  likely (elf->state.elf.scnincr
bf5b25
+		  < SIZE_MAX / 2 / sizeof (Elf_Scn) - sizeof (Elf_ScnList))
bf5b25
+#else
bf5b25
+	  1
bf5b25
+#endif
bf5b25
+	  )
bf5b25
+	newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
bf5b25
+				       + ((elf->state.elf.scnincr *= 2)
bf5b25
+					  * sizeof (Elf_Scn)), 1);
bf5b25
       if (newp == NULL)
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_NOMEM);
a78f3e
--- elfutils-0.120/libelf/gelf_getdyn.c
a78f3e
+++ elfutils-0.120/libelf/gelf_getdyn.c
a78f3e
@@ -93,7 +93,8 @@ gelf_getdyn (data, ndx, dst)
bf5b25
 	 table entries has to be adopted.  The user better has provided
bf5b25
 	 a buffer where we can store the information.  While copying the
bf5b25
 	 data we are converting the format.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Dyn)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
@@ -114,7 +115,8 @@ gelf_getdyn (data, ndx, dst)
bf5b25
 
bf5b25
       /* The data is already in the correct form.  Just make sure the
bf5b25
 	 index is OK.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, GElf_Dyn)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (GElf_Dyn) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_getlib.c
a78f3e
+++ elfutils-0.120/libelf/gelf_getlib.c
a78f3e
@@ -86,7 +86,8 @@ gelf_getlib (data, ndx, dst)
bf5b25
   /* The data is already in the correct form.  Just make sure the
bf5b25
      index is OK.  */
bf5b25
   GElf_Lib *result = NULL;
bf5b25
-  if (unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
bf5b25
+  if (INVALID_NDX (ndx, GElf_Lib)
bf5b25
+      || unlikely ((ndx + 1) * sizeof (GElf_Lib) > data->d_size))
bf5b25
     __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
   else
bf5b25
     {
a78f3e
--- elfutils-0.120/libelf/gelf_getmove.c
a78f3e
+++ elfutils-0.120/libelf/gelf_getmove.c
a78f3e
@@ -83,7 +83,8 @@ gelf_getmove (data, ndx, dst)
bf5b25
 
bf5b25
   /* The data is already in the correct form.  Just make sure the
bf5b25
      index is OK.  */
bf5b25
-  if (unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
bf5b25
+  if (INVALID_NDX (ndx, GElf_Move)
bf5b25
+      || unlikely ((ndx + 1) * sizeof (GElf_Move) > data->d_size))
bf5b25
     {
bf5b25
       __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
       goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_getrela.c
a78f3e
+++ elfutils-0.120/libelf/gelf_getrela.c
a78f3e
@@ -71,12 +71,6 @@ gelf_getrela (data, ndx, dst)
bf5b25
   if (data_scn == NULL)
bf5b25
     return NULL;
bf5b25
 
bf5b25
-  if (unlikely (ndx < 0))
bf5b25
-    {
bf5b25
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
-      return NULL;
bf5b25
-    }
bf5b25
-
bf5b25
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
bf5b25
     {
bf5b25
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
a78f3e
@@ -93,7 +87,8 @@ gelf_getrela (data, ndx, dst)
bf5b25
   if (scn->elf->class == ELFCLASS32)
bf5b25
     {
bf5b25
       /* We have to convert the data.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Rela)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  result = NULL;
a78f3e
@@ -114,7 +109,8 @@ gelf_getrela (data, ndx, dst)
bf5b25
     {
bf5b25
       /* Simply copy the data after we made sure we are actually getting
bf5b25
 	 correct data.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf64_Rela)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  result = NULL;
a78f3e
--- elfutils-0.120/libelf/gelf_getrel.c
a78f3e
+++ elfutils-0.120/libelf/gelf_getrel.c
a78f3e
@@ -71,12 +71,6 @@ gelf_getrel (data, ndx, dst)
bf5b25
   if (data_scn == NULL)
bf5b25
     return NULL;
bf5b25
 
bf5b25
-  if (unlikely (ndx < 0))
bf5b25
-    {
bf5b25
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
-      return NULL;
bf5b25
-    }
bf5b25
-
bf5b25
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
bf5b25
     {
bf5b25
       __libelf_seterrno (ELF_E_INVALID_HANDLE);
a78f3e
@@ -93,7 +87,8 @@ gelf_getrel (data, ndx, dst)
bf5b25
   if (scn->elf->class == ELFCLASS32)
bf5b25
     {
bf5b25
       /* We have to convert the data.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Rel)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  result = NULL;
a78f3e
@@ -113,7 +108,8 @@ gelf_getrel (data, ndx, dst)
bf5b25
     {
bf5b25
       /* Simply copy the data after we made sure we are actually getting
bf5b25
 	 correct data.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf64_Rel)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  result = NULL;
a78f3e
--- elfutils-0.120/libelf/gelf_getsym.c
a78f3e
+++ elfutils-0.120/libelf/gelf_getsym.c
a78f3e
@@ -90,7 +90,8 @@ gelf_getsym (data, ndx, dst)
bf5b25
 	 table entries has to be adopted.  The user better has provided
bf5b25
 	 a buffer where we can store the information.  While copying the
bf5b25
 	 data we are converting the format.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Sym)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data->d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
@@ -119,7 +120,8 @@ gelf_getsym (data, ndx, dst)
bf5b25
 
bf5b25
       /* The data is already in the correct form.  Just make sure the
bf5b25
 	 index is OK.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
bf5b25
+      if (INVALID_NDX (ndx, GElf_Sym)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > data->d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_getsyminfo.c
a78f3e
+++ elfutils-0.120/libelf/gelf_getsyminfo.c
a78f3e
@@ -84,7 +84,8 @@ gelf_getsyminfo (data, ndx, dst)
bf5b25
 
bf5b25
   /* The data is already in the correct form.  Just make sure the
bf5b25
      index is OK.  */
bf5b25
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
bf5b25
+  if (INVALID_NDX (ndx, GElf_Syminfo)
bf5b25
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data->d_size))
bf5b25
     {
bf5b25
       __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
       goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_getsymshndx.c
a78f3e
+++ elfutils-0.120/libelf/gelf_getsymshndx.c
a78f3e
@@ -90,7 +90,9 @@ gelf_getsymshndx (symdata, shndxdata, nd
bf5b25
      section index table.  */
bf5b25
   if (likely (shndxdata_scn != NULL))
bf5b25
     {
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Word) > shndxdata_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Word)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Word)
bf5b25
+		       > shndxdata_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
@@ -110,7 +112,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
bf5b25
 	 table entries has to be adopted.  The user better has provided
bf5b25
 	 a buffer where we can store the information.  While copying the
bf5b25
 	 data we are converting the format.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Sym)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata->d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
@@ -139,7 +142,8 @@ gelf_getsymshndx (symdata, shndxdata, nd
bf5b25
 
bf5b25
       /* The data is already in the correct form.  Just make sure the
bf5b25
 	 index is OK.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
bf5b25
+      if (INVALID_NDX (ndx, GElf_Sym)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (GElf_Sym) > symdata->d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_getversym.c
a78f3e
+++ elfutils-0.120/libelf/gelf_getversym.c
a78f3e
@@ -92,7 +92,8 @@ gelf_getversym (data, ndx, dst)
bf5b25
 
bf5b25
   /* The data is already in the correct form.  Just make sure the
bf5b25
      index is OK.  */
bf5b25
-  if (unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
bf5b25
+  if (INVALID_NDX (ndx, GElf_Versym)
bf5b25
+      || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data->d_size))
bf5b25
     {
bf5b25
       __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
       result = NULL;
a78f3e
--- elfutils-0.120/libelf/gelf_update_dyn.c
a78f3e
+++ elfutils-0.120/libelf/gelf_update_dyn.c
a78f3e
@@ -71,12 +71,6 @@ gelf_update_dyn (data, ndx, src)
bf5b25
   if (data == NULL)
bf5b25
     return 0;
bf5b25
 
bf5b25
-  if (unlikely (ndx < 0))
bf5b25
-    {
bf5b25
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
-      return 0;
bf5b25
-    }
bf5b25
-
bf5b25
   if (unlikely (data_scn->d.d_type != ELF_T_DYN))
bf5b25
     {
bf5b25
       /* The type of the data better should match.  */
a78f3e
@@ -102,7 +96,8 @@ gelf_update_dyn (data, ndx, src)
bf5b25
 	}
bf5b25
 
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Dyn)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Dyn) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
@@ -116,7 +111,8 @@ gelf_update_dyn (data, ndx, src)
bf5b25
   else
bf5b25
     {
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf64_Dyn)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Dyn) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_update_lib.c
a78f3e
+++ elfutils-0.120/libelf/gelf_update_lib.c
a78f3e
@@ -68,12 +68,6 @@ gelf_update_lib (data, ndx, src)
bf5b25
   if (data == NULL)
bf5b25
     return 0;
bf5b25
 
bf5b25
-  if (unlikely (ndx < 0))
bf5b25
-    {
bf5b25
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
-      return 0;
bf5b25
-    }
bf5b25
-
bf5b25
   Elf_Data_Scn *data_scn = (Elf_Data_Scn *) data;
bf5b25
   if (unlikely (data_scn->d.d_type != ELF_T_LIB))
bf5b25
     {
a78f3e
@@ -87,7 +81,8 @@ gelf_update_lib (data, ndx, src)
bf5b25
 
bf5b25
   /* Check whether we have to resize the data buffer.  */
bf5b25
   int result = 0;
bf5b25
-  if (unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
bf5b25
+  if (INVALID_NDX (ndx, Elf64_Lib)
bf5b25
+      || unlikely ((ndx + 1) * sizeof (Elf64_Lib) > data_scn->d.d_size))
bf5b25
     __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
   else
bf5b25
     {
a78f3e
--- elfutils-0.120/libelf/gelf_update_move.c
a78f3e
+++ elfutils-0.120/libelf/gelf_update_move.c
a78f3e
@@ -75,7 +75,7 @@ gelf_update_move (data, ndx, src)
bf5b25
   assert (sizeof (GElf_Move) == sizeof (Elf64_Move));
bf5b25
 
bf5b25
   /* Check whether we have to resize the data buffer.  */
bf5b25
-  if (unlikely (ndx < 0)
bf5b25
+  if (INVALID_NDX (ndx, GElf_Move)
bf5b25
       || unlikely ((ndx + 1) * sizeof (GElf_Move) > data_scn->d.d_size))
bf5b25
     {
bf5b25
       __libelf_seterrno (ELF_E_INVALID_INDEX);
a78f3e
--- elfutils-0.120/libelf/gelf_update_rela.c
a78f3e
+++ elfutils-0.120/libelf/gelf_update_rela.c
a78f3e
@@ -68,12 +68,6 @@ gelf_update_rela (Elf_Data *dst, int ndx
bf5b25
   if (dst == NULL)
bf5b25
     return 0;
bf5b25
 
bf5b25
-  if (unlikely (ndx < 0))
bf5b25
-    {
bf5b25
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
-      return 0;
bf5b25
-    }
bf5b25
-
bf5b25
   if (unlikely (data_scn->d.d_type != ELF_T_RELA))
bf5b25
     {
bf5b25
       /* The type of the data better should match.  */
a78f3e
@@ -101,7 +95,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
bf5b25
 	}
bf5b25
 
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Rela)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rela) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
@@ -117,7 +112,8 @@ gelf_update_rela (Elf_Data *dst, int ndx
bf5b25
   else
bf5b25
     {
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf64_Rela)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rela) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_update_rel.c
a78f3e
+++ elfutils-0.120/libelf/gelf_update_rel.c
a78f3e
@@ -68,12 +68,6 @@ gelf_update_rel (Elf_Data *dst, int ndx,
bf5b25
   if (dst == NULL)
bf5b25
     return 0;
bf5b25
 
bf5b25
-  if (unlikely (ndx < 0))
bf5b25
-    {
bf5b25
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
-      return 0;
bf5b25
-    }
bf5b25
-
bf5b25
   if (unlikely (data_scn->d.d_type != ELF_T_REL))
bf5b25
     {
bf5b25
       /* The type of the data better should match.  */
a78f3e
@@ -99,7 +93,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
bf5b25
 	}
bf5b25
 
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Rel)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Rel) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
@@ -114,7 +109,8 @@ gelf_update_rel (Elf_Data *dst, int ndx,
bf5b25
   else
bf5b25
     {
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf64_Rel)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Rel) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_update_sym.c
a78f3e
+++ elfutils-0.120/libelf/gelf_update_sym.c
a78f3e
@@ -72,12 +72,6 @@ gelf_update_sym (data, ndx, src)
bf5b25
   if (data == NULL)
bf5b25
     return 0;
bf5b25
 
bf5b25
-  if (unlikely (ndx < 0))
bf5b25
-    {
bf5b25
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
-      return 0;
bf5b25
-    }
bf5b25
-
bf5b25
   if (unlikely (data_scn->d.d_type != ELF_T_SYM))
bf5b25
     {
bf5b25
       /* The type of the data better should match.  */
a78f3e
@@ -102,7 +96,8 @@ gelf_update_sym (data, ndx, src)
bf5b25
 	}
bf5b25
 
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Sym)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
@@ -125,7 +120,8 @@ gelf_update_sym (data, ndx, src)
bf5b25
   else
bf5b25
     {
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf64_Sym)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > data_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_update_syminfo.c
a78f3e
+++ elfutils-0.120/libelf/gelf_update_syminfo.c
a78f3e
@@ -72,12 +72,6 @@ gelf_update_syminfo (data, ndx, src)
bf5b25
   if (data == NULL)
bf5b25
     return 0;
bf5b25
 
bf5b25
-  if (unlikely (ndx < 0))
bf5b25
-    {
bf5b25
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
-      return 0;
bf5b25
-    }
bf5b25
-
bf5b25
   if (unlikely (data_scn->d.d_type != ELF_T_SYMINFO))
bf5b25
     {
bf5b25
       /* The type of the data better should match.  */
a78f3e
@@ -93,7 +87,8 @@ gelf_update_syminfo (data, ndx, src)
bf5b25
   rwlock_wrlock (scn->elf->lock);
bf5b25
 
bf5b25
   /* Check whether we have to resize the data buffer.  */
bf5b25
-  if (unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
bf5b25
+  if (INVALID_NDX (ndx, GElf_Syminfo)
bf5b25
+      || unlikely ((ndx + 1) * sizeof (GElf_Syminfo) > data_scn->d.d_size))
bf5b25
     {
bf5b25
       __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
       goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_update_symshndx.c
a78f3e
+++ elfutils-0.120/libelf/gelf_update_symshndx.c
a78f3e
@@ -77,12 +77,6 @@ gelf_update_symshndx (symdata, shndxdata
bf5b25
   if (symdata == NULL)
bf5b25
     return 0;
bf5b25
 
bf5b25
-  if (unlikely (ndx < 0))
bf5b25
-    {
bf5b25
-      __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
-      return 0;
bf5b25
-    }
bf5b25
-
bf5b25
   if (unlikely (symdata_scn->d.d_type != ELF_T_SYM))
bf5b25
     {
bf5b25
       /* The type of the data better should match.  */
a78f3e
@@ -128,7 +122,8 @@ gelf_update_symshndx (symdata, shndxdata
bf5b25
 	}
bf5b25
 
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf32_Sym)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf32_Sym) > symdata_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
@@ -151,7 +146,8 @@ gelf_update_symshndx (symdata, shndxdata
bf5b25
   else
bf5b25
     {
bf5b25
       /* Check whether we have to resize the data buffer.  */
bf5b25
-      if (unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
bf5b25
+      if (INVALID_NDX (ndx, Elf64_Sym)
bf5b25
+	  || unlikely ((ndx + 1) * sizeof (Elf64_Sym) > symdata_scn->d.d_size))
bf5b25
 	{
bf5b25
 	  __libelf_seterrno (ELF_E_INVALID_INDEX);
bf5b25
 	  goto out;
a78f3e
--- elfutils-0.120/libelf/gelf_update_versym.c
a78f3e
+++ elfutils-0.120/libelf/gelf_update_versym.c
a78f3e
@@ -75,7 +75,7 @@ gelf_update_versym (data, ndx, src)
bf5b25
   assert (sizeof (GElf_Versym) == sizeof (Elf64_Versym));
bf5b25
 
bf5b25
   /* Check whether we have to resize the data buffer.  */
bf5b25
-  if (unlikely (ndx < 0)
bf5b25
+  if (INVALID_NDX (ndx, GElf_Versym)
bf5b25
       || unlikely ((ndx + 1) * sizeof (GElf_Versym) > data_scn->d.d_size))
bf5b25
     {
bf5b25
       __libelf_seterrno (ELF_E_INVALID_INDEX);
a78f3e
--- elfutils-0.120/libelf/libelfP.h
a78f3e
+++ elfutils-0.120/libelf/libelfP.h
a78f3e
@@ -569,4 +569,13 @@ extern uint32_t __libelf_crc32 (uint32_t
bf5b25
   } while (0)
bf5b25
 #endif
bf5b25
 
bf5b25
+/* Convenience macro.  Assumes int NDX and TYPE with size at least
bf5b25
+   2 bytes.  */
bf5b25
+#if SIZE_MAX > 4294967295U
bf5b25
+# define INVALID_NDX(ndx, type) unlikely (ndx < 0)
bf5b25
+#else
bf5b25
+# define INVALID_NDX(ndx, type) \
bf5b25
+  unlikely ((unsigned int) (ndx) >= SIZE_MAX / sizeof (type))
bf5b25
+#endif
bf5b25
+
bf5b25
 #endif  /* libelfP.h */
a78f3e
--- elfutils-0.120/src/elflint.c
a78f3e
+++ elfutils-0.120/src/elflint.c
a78f3e
@@ -123,6 +123,9 @@ static uint32_t shstrndx;
bf5b25
 /* Array to count references in section groups.  */
bf5b25
 static int *scnref;
bf5b25
 
bf5b25
+/* Number of sections.  */
bf5b25
+static unsigned int shnum;
bf5b25
+
bf5b25
 
bf5b25
 int
bf5b25
 main (int argc, char *argv[])
a78f3e
@@ -312,10 +315,19 @@ section_name (Ebl *ebl, int idx)
bf5b25
 {
bf5b25
   GElf_Shdr shdr_mem;
bf5b25
   GElf_Shdr *shdr;
bf5b25
+  const char *ret;
bf5b25
+
bf5b25
+  if ((unsigned int) idx > shnum)
bf5b25
+    return "<invalid>";
bf5b25
 
bf5b25
   shdr = gelf_getshdr (elf_getscn (ebl->elf, idx), &shdr_mem);
bf5b25
+  if (shdr == NULL)
bf5b25
+    return "<invalid>";
bf5b25
 
bf5b25
-  return elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
bf5b25
+  ret = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
bf5b25
+  if (ret == NULL)
bf5b25
+    return "<invalid>";
bf5b25
+  return ret;
bf5b25
 }
bf5b25
 
bf5b25
 
a78f3e
@@ -337,10 +349,6 @@ static const int valid_e_machine[] =
bf5b25
   (sizeof (valid_e_machine) / sizeof (valid_e_machine[0]))
bf5b25
 
bf5b25
 
bf5b25
-/* Number of sections.  */
bf5b25
-static unsigned int shnum;
bf5b25
-
bf5b25
-
bf5b25
 static void
bf5b25
 check_elf_header (Ebl *ebl, GElf_Ehdr *ehdr, size_t size)
bf5b25
 {
a78f3e
@@ -603,7 +611,8 @@ section [%2d] '%s': symbol table cannot 
bf5b25
 	  }
bf5b25
       }
bf5b25
 
bf5b25
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT))
bf5b25
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_SYM, 1, EV_CURRENT);
bf5b25
+  if (shdr->sh_entsize != sh_entsize)
bf5b25
     ERROR (gettext ("\
bf5b25
 section [%2zu] '%s': entry size is does not match ElfXX_Sym\n"),
bf5b25
 	   cnt, section_name (ebl, cnt));
a78f3e
@@ -641,7 +650,7 @@ section [%2d] '%s': XINDEX for zeroth en
bf5b25
 	       xndxscnidx, section_name (ebl, xndxscnidx));
bf5b25
     }
bf5b25
 
bf5b25
-  for (cnt = 1; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
bf5b25
+  for (cnt = 1; cnt < shdr->sh_size / sh_entsize; ++cnt)
bf5b25
     {
bf5b25
       sym = gelf_getsymshndx (data, xndxdata, cnt, &sym_mem, &xndx);
bf5b25
       if (sym == NULL)
a78f3e
@@ -659,7 +668,8 @@ section [%2d] '%s': symbol %zu: invalid 
bf5b25
       else
bf5b25
 	{
bf5b25
 	  name = elf_strptr (ebl->elf, shdr->sh_link, sym->st_name);
bf5b25
-	  assert (name != NULL);
bf5b25
+	  assert (name != NULL
bf5b25
+		  || strshdr->sh_type != SHT_STRTAB);
bf5b25
 	}
bf5b25
 
bf5b25
       if (sym->st_shndx == SHN_XINDEX)
a78f3e
@@ -970,7 +980,7 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
bf5b25
       const GElf_Shdr *rcshdr = gelf_getshdr (scn, &rcshdr_mem);
bf5b25
       assert (rcshdr != NULL);
bf5b25
 
bf5b25
-      if (rcshdr->sh_type == SHT_DYNAMIC)
bf5b25
+      if (rcshdr->sh_type == SHT_DYNAMIC && rcshdr->sh_entsize)
bf5b25
 	{
bf5b25
 	  /* Found the dynamic section.  Look through it.  */
bf5b25
 	  Elf_Data *d = elf_getdata (scn, NULL);
a78f3e
@@ -980,14 +990,17 @@ is_rel_dyn (Ebl *ebl, const GElf_Ehdr *e
bf5b25
 	    {
bf5b25
 	      GElf_Dyn dyn_mem;
bf5b25
 	      GElf_Dyn *dyn = gelf_getdyn (d, cnt, &dyn_mem);
bf5b25
-	      assert (dyn != NULL);
bf5b25
+
bf5b25
+	      if (dyn == NULL)
bf5b25
+		break;
bf5b25
 
bf5b25
 	      if (dyn->d_tag == DT_RELCOUNT)
bf5b25
 		{
bf5b25
 		  /* Found it.  One last check: does the number
bf5b25
 		     specified number of relative relocations exceed
bf5b25
 		     the total number of relocations?  */
bf5b25
-		  if (dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
bf5b25
+		  if (shdr->sh_entsize
bf5b25
+		      && dyn->d_un.d_val > shdr->sh_size / shdr->sh_entsize)
bf5b25
 		    ERROR (gettext ("\
bf5b25
 section [%2d] '%s': DT_RELCOUNT value %d too high for this section\n"),
bf5b25
 			   idx, section_name (ebl, idx),
a78f3e
@@ -1062,7 +1075,8 @@ section [%2d] '%s': no relocations for m
bf5b25
 	}
bf5b25
     }
bf5b25
 
bf5b25
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT))
bf5b25
+  size_t sh_entsize = gelf_fsize (ebl->elf, reltype, 1, EV_CURRENT);
bf5b25
+  if (shdr->sh_entsize != sh_entsize)
bf5b25
     ERROR (gettext (reltype == ELF_T_RELA ? "\
bf5b25
 section [%2d] '%s': section entry size does not match ElfXX_Rela\n" : "\
bf5b25
 section [%2d] '%s': section entry size does not match ElfXX_Rel\n"),
a78f3e
@@ -1280,7 +1294,8 @@ check_rela (Ebl *ebl, GElf_Ehdr *ehdr, G
bf5b25
   Elf_Data *symdata = elf_getdata (symscn, NULL);
bf5b25
   enum load_state state = state_undecided;
bf5b25
 
bf5b25
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
bf5b25
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_RELA, 1, EV_CURRENT);
bf5b25
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
bf5b25
     {
bf5b25
       GElf_Rela rela_mem;
bf5b25
       GElf_Rela *rela = gelf_getrela (data, cnt, &rela_mem);
a78f3e
@@ -1330,7 +1345,8 @@ check_rel (Ebl *ebl, GElf_Ehdr *ehdr, GE
bf5b25
   Elf_Data *symdata = elf_getdata (symscn, NULL);
bf5b25
   enum load_state state = state_undecided;
bf5b25
 
bf5b25
-  for (size_t cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
bf5b25
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_REL, 1, EV_CURRENT);
bf5b25
+  for (size_t cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
bf5b25
     {
bf5b25
       GElf_Rel rel_mem;
bf5b25
       GElf_Rel *rel = gelf_getrel (data, cnt, &rel_mem);
a78f3e
@@ -1432,7 +1448,8 @@ section [%2d] '%s': referenced as string
bf5b25
 	   shdr->sh_link, section_name (ebl, shdr->sh_link),
bf5b25
 	   idx, section_name (ebl, idx));
bf5b25
 
bf5b25
-  if (shdr->sh_entsize != gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT))
bf5b25
+  size_t sh_entsize = gelf_fsize (ebl->elf, ELF_T_DYN, 1, EV_CURRENT);
bf5b25
+  if (shdr->sh_entsize != sh_entsize)
bf5b25
     ERROR (gettext ("\
bf5b25
 section [%2d] '%s': section entry size does not match ElfXX_Dyn\n"),
bf5b25
 	   idx, section_name (ebl, idx));
a78f3e
@@ -1442,7 +1459,7 @@ section [%2d] '%s': section entry size d
bf5b25
 	   idx, section_name (ebl, idx));
bf5b25
 
bf5b25
   bool non_null_warned = false;
bf5b25
-  for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
bf5b25
+  for (cnt = 0; cnt < shdr->sh_size / sh_entsize; ++cnt)
bf5b25
     {
bf5b25
       GElf_Dyn dyn_mem;
bf5b25
       GElf_Dyn *dyn = gelf_getdyn (data, cnt, &dyn_mem);
a78f3e
@@ -1633,6 +1650,8 @@ section [%2d] '%s': entry size does not 
bf5b25
 	   idx, section_name (ebl, idx));
bf5b25
 
bf5b25
   if (symshdr != NULL
bf5b25
+      && shdr->sh_entsize
bf5b25
+      && symshdr->sh_entsize
bf5b25
       && (shdr->sh_size / shdr->sh_entsize
bf5b25
 	  < symshdr->sh_size / symshdr->sh_entsize))
bf5b25
     ERROR (gettext ("\
a78f3e
@@ -1659,6 +1678,12 @@ section [%2d] '%s': extended section ind
bf5b25
     }
bf5b25
 
bf5b25
   Elf_Data *data = elf_getdata (elf_getscn (ebl->elf, idx), NULL);
bf5b25
+  if (data == NULL)
bf5b25
+    {
bf5b25
+      ERROR (gettext ("section [%2d] '%s': cannot get section data\n"),
bf5b25
+ 	     idx, section_name (ebl, idx));
bf5b25
+      return;
bf5b25
+    }
bf5b25
 
bf5b25
   if (*((Elf32_Word *) data->d_buf) != 0)
bf5b25
     ERROR (gettext ("symbol 0 should have zero extended section index\n"));
a78f3e
@@ -1739,23 +1764,30 @@ section [%2d] '%s': hash table section i
bf5b25
 	   idx, section_name (ebl, idx), (long int) shdr->sh_size,
bf5b25
 	   (long int) ((2 + nbucket + nchain) * shdr->sh_entsize));
bf5b25
 
bf5b25
-  if (symshdr != NULL)
bf5b25
+  if (symshdr != NULL && symshdr->sh_entsize)
bf5b25
     {
bf5b25
       size_t symsize = symshdr->sh_size / symshdr->sh_entsize;
bf5b25
       size_t cnt;
bf5b25
+      Elf32_Word *buf, *end;
bf5b25
 
bf5b25
       if (nchain < symshdr->sh_size / symshdr->sh_entsize)
bf5b25
 	ERROR (gettext ("section [%2d] '%s': chain array not large enough\n"),
bf5b25
 	       idx, section_name (ebl, idx));
bf5b25
 
bf5b25
+      buf = ((Elf32_Word *) data->d_buf) + 2;
bf5b25
+      end = (Elf32_Word *) ((char *) data->d_buf + shdr->sh_size);
bf5b25
       for (cnt = 2; cnt < 2 + nbucket; ++cnt)
bf5b25
-	if (((Elf32_Word *) data->d_buf)[cnt] >= symsize)
bf5b25
+	if (buf >= end)
bf5b25
+	  return;
bf5b25
+	else if (*buf++ >= symsize)
bf5b25
 	  ERROR (gettext ("\
bf5b25
 section [%2d] '%s': hash bucket reference %zu out of bounds\n"),
bf5b25
 		 idx, section_name (ebl, idx), cnt - 2);
bf5b25
 
bf5b25
       for (; cnt < 2 + nbucket + nchain; ++cnt)
bf5b25
-	if (((Elf32_Word *) data->d_buf)[cnt] >= symsize)
bf5b25
+	if (buf >= end)
bf5b25
+	  return;
bf5b25
+	else if (*buf++ >= symsize)
bf5b25
 	  ERROR (gettext ("\
bf5b25
 section [%2d] '%s': hash chain reference %zu out of bounds\n"),
bf5b25
 		 idx, section_name (ebl, idx), cnt - 2 - nbucket);
a78f3e
@@ -2097,8 +2129,9 @@ section [%2d] '%s' refers in sh_link to 
bf5b25
 
bf5b25
   /* The number of elements in the version symbol table must be the
bf5b25
      same as the number of symbols.  */
bf5b25
-  if (shdr->sh_size / shdr->sh_entsize
bf5b25
-      != symshdr->sh_size / symshdr->sh_entsize)
bf5b25
+  if (shdr->sh_entsize && symshdr->sh_entsize
bf5b25
+      && (shdr->sh_size / shdr->sh_entsize
bf5b25
+	  != symshdr->sh_size / symshdr->sh_entsize))
bf5b25
     ERROR (gettext ("\
bf5b25
 section [%2d] '%s' has different number of entries than symbol table [%2d] '%s'\n"),
bf5b25
 	   idx, section_name (ebl, idx),
a78f3e
@@ -3002,6 +3035,8 @@ phdr[%d]: no note entries defined for th
bf5b25
     return;
bf5b25
 
bf5b25
   char *notemem = gelf_rawchunk (ebl->elf, phdr->p_offset, phdr->p_filesz);
bf5b25
+  if (notemem == NULL)
bf5b25
+    return;
bf5b25
 
bf5b25
   /* ELF64 files often use note section entries in the 32-bit format.
bf5b25
      The p_align field is set to 8 in case the 64-bit format is used.
a78f3e
--- elfutils-0.120/src/readelf.c
a78f3e
+++ elfutils-0.120/src/readelf.c
a78f3e
@@ -958,6 +958,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
d88264
   Elf32_Word *grpref = (Elf32_Word *) data->d_buf;
bf5b25
 
d88264
   GElf_Sym sym_mem;
d88264
+  GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem);
bf5b25
+
bf5b25
   printf ((grpref[0] & GRP_COMDAT)
d88264
 	  ? ngettext ("\
d88264
 \nCOMDAT section group [%2zu] '%s' with signature '%s' contains %zu entry:\n",
a78f3e
@@ -970,8 +972,8 @@ handle_scngrp (Ebl *ebl, Elf_Scn *scn, G
bf5b25
 		      data->d_size / sizeof (Elf32_Word) - 1),
bf5b25
 	  elf_ndxscn (scn),
bf5b25
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name),
bf5b25
-	  elf_strptr (ebl->elf, symshdr->sh_link,
bf5b25
-		      gelf_getsym (symdata, shdr->sh_info, &sym_mem)->st_name)
bf5b25
+	  (sym == NULL ? NULL
bf5b25
+	   : elf_strptr (ebl->elf, symshdr->sh_link, sym->st_name))
bf5b25
 	  ?: gettext ("<INVALID SYMBOL>"),
bf5b25
 	  data->d_size / sizeof (Elf32_Word) - 1);
bf5b25
 
a78f3e
@@ -1122,7 +1124,8 @@ static void
bf5b25
 handle_dynamic (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr)
bf5b25
 {
bf5b25
   int class = gelf_getclass (ebl->elf);
bf5b25
-  GElf_Shdr glink;
bf5b25
+  GElf_Shdr glink_mem;
bf5b25
+  GElf_Shdr *glink;
bf5b25
   Elf_Data *data;
bf5b25
   size_t cnt;
bf5b25
   size_t shstrndx;
a78f3e
@@ -1137,6 +1140,11 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
bf5b25
     error (EXIT_FAILURE, 0,
bf5b25
 	   gettext ("cannot get section header string table index"));
bf5b25
 
bf5b25
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
bf5b25
+  if (glink == NULL)
bf5b25
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
bf5b25
+	   elf_ndxscn (scn));
bf5b25
+
bf5b25
   printf (ngettext ("\
bf5b25
 \nDynamic segment contains %lu entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
bf5b25
 		    "\
a78f3e
@@ -1146,9 +1154,7 @@ handle_dynamic (Ebl *ebl, Elf_Scn *scn, 
bf5b25
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
bf5b25
 	  shdr->sh_offset,
bf5b25
 	  (int) shdr->sh_link,
bf5b25
-	  elf_strptr (ebl->elf, shstrndx,
bf5b25
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
bf5b25
-				    &glink)->sh_name));
bf5b25
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
bf5b25
   fputs_unlocked (gettext ("  Type              Value\n"), stdout);
bf5b25
 
bf5b25
   for (cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
a78f3e
@@ -1666,6 +1672,13 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
bf5b25
     error (EXIT_FAILURE, 0,
bf5b25
 	   gettext ("cannot get section header string table index"));
bf5b25
 
d88264
+  GElf_Shdr glink_mem;
d88264
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
d88264
+				   &glink_mem);
bf5b25
+  if (glink == NULL)
bf5b25
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
bf5b25
+	   elf_ndxscn (scn));
bf5b25
+
bf5b25
   /* Now we can compute the number of entries in the section.  */
d88264
   unsigned int nsyms = data->d_size / (class == ELFCLASS32
d88264
 				       ? sizeof (Elf32_Sym)
a78f3e
@@ -1676,15 +1689,12 @@ handle_symtab (Ebl *ebl, Elf_Scn *scn, G
d88264
 		    nsyms),
d88264
 	  (unsigned int) elf_ndxscn (scn),
d88264
 	  elf_strptr (ebl->elf, shstrndx, shdr->sh_name), nsyms);
d88264
-  GElf_Shdr glink;
d88264
   printf (ngettext (" %lu local symbol  String table: [%2u] '%s'\n",
d88264
 		    " %lu local symbols  String table: [%2u] '%s'\n",
bf5b25
 		    shdr->sh_info),
bf5b25
 	  (unsigned long int) shdr->sh_info,
bf5b25
 	  (unsigned int) shdr->sh_link,
bf5b25
-	  elf_strptr (ebl->elf, shstrndx,
bf5b25
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
bf5b25
-				    &glink)->sh_name));
bf5b25
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
bf5b25
 
bf5b25
   fputs_unlocked (class == ELFCLASS32
bf5b25
 		  ? gettext ("\
a78f3e
@@ -1920,7 +1930,13 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
bf5b25
     error (EXIT_FAILURE, 0,
bf5b25
 	   gettext ("cannot get section header string table index"));
bf5b25
 
bf5b25
-  GElf_Shdr glink;
bf5b25
+  GElf_Shdr glink_mem;
bf5b25
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
bf5b25
+				   &glink_mem);
bf5b25
+  if (glink == NULL)
bf5b25
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
bf5b25
+	   elf_ndxscn (scn));
bf5b25
+
bf5b25
   printf (ngettext ("\
bf5b25
 \nVersion needs section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
bf5b25
 		    "\
a78f3e
@@ -1931,9 +1947,7 @@ handle_verneed (Ebl *ebl, Elf_Scn *scn, 
bf5b25
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
bf5b25
 	  shdr->sh_offset,
bf5b25
 	  (unsigned int) shdr->sh_link,
bf5b25
-	  elf_strptr (ebl->elf, shstrndx,
bf5b25
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
bf5b25
-				    &glink)->sh_name));
bf5b25
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
bf5b25
 
bf5b25
   unsigned int offset = 0;
bf5b25
   for (int cnt = shdr->sh_info; --cnt >= 0; )
a78f3e
@@ -1986,8 +2000,14 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
bf5b25
     error (EXIT_FAILURE, 0,
bf5b25
 	   gettext ("cannot get section header string table index"));
bf5b25
 
bf5b25
+  GElf_Shdr glink_mem;
bf5b25
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
bf5b25
+				   &glink_mem);
bf5b25
+  if (glink == NULL)
bf5b25
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
bf5b25
+	   elf_ndxscn (scn));
bf5b25
+
bf5b25
   int class = gelf_getclass (ebl->elf);
bf5b25
-  GElf_Shdr glink;
bf5b25
   printf (ngettext ("\
bf5b25
 \nVersion definition section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'\n",
bf5b25
 		    "\
a78f3e
@@ -1999,9 +2019,7 @@ handle_verdef (Ebl *ebl, Elf_Scn *scn, G
bf5b25
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
bf5b25
 	  shdr->sh_offset,
bf5b25
 	  (unsigned int) shdr->sh_link,
bf5b25
-	  elf_strptr (ebl->elf, shstrndx,
bf5b25
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
bf5b25
-				    &glink)->sh_name));
bf5b25
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
bf5b25
 
bf5b25
   unsigned int offset = 0;
bf5b25
   for (int cnt = shdr->sh_info; --cnt >= 0; )
a78f3e
@@ -2263,8 +2281,14 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
bf5b25
       filename = NULL;
bf5b25
     }
bf5b25
 
bf5b25
+  GElf_Shdr glink_mem;
bf5b25
+  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
bf5b25
+				   &glink_mem);
bf5b25
+  if (glink == NULL)
bf5b25
+    error (EXIT_FAILURE, 0, gettext ("invalid sh_link value in section %Zu"),
bf5b25
+	   elf_ndxscn (scn));
bf5b25
+
bf5b25
   /* Print the header.  */
d88264
-  GElf_Shdr glink;
bf5b25
   printf (ngettext ("\
bf5b25
 \nVersion symbols section [%2u] '%s' contains %d entry:\n Addr: %#0*" PRIx64 "  Offset: %#08" PRIx64 "  Link to section: [%2u] '%s'",
d88264
 		    "\
a78f3e
@@ -2276,9 +2300,7 @@ handle_versym (Ebl *ebl, Elf_Scn *scn, G
bf5b25
 	  class == ELFCLASS32 ? 10 : 18, shdr->sh_addr,
bf5b25
 	  shdr->sh_offset,
bf5b25
 	  (unsigned int) shdr->sh_link,
bf5b25
-	  elf_strptr (ebl->elf, shstrndx,
bf5b25
-		      gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link),
bf5b25
-				    &glink)->sh_name));
bf5b25
+	  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
bf5b25
 
bf5b25
   /* Now we can finally look at the actual contents of this section.  */
bf5b25
   for (unsigned int cnt = 0; cnt < shdr->sh_size / shdr->sh_entsize; ++cnt)
a78f3e
@@ -2353,7 +2375,17 @@ handle_hash (Ebl *ebl)
d88264
 	  Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
d88264
 	  Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
bf5b25
 
d88264
-	  GElf_Shdr glink;
d88264
+ 	  GElf_Shdr glink_mem;
d88264
+ 	  GElf_Shdr *glink = gelf_getshdr (elf_getscn (ebl->elf,
d88264
+						       shdr->sh_link),
d88264
+					   &glink_mem);
d88264
+ 	  if (glink == NULL)
d88264
+ 	    {
d88264
+ 	      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
d88264
+ 		     elf_ndxscn (scn));
d88264
+ 	      continue;
d88264
+ 	    }
bf5b25
+
d88264
 	  printf (ngettext ("\
d88264
 \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",
d88264
 			    "\
a78f3e
@@ -2366,10 +2398,7 @@ handle_hash (Ebl *ebl)
bf5b25
 		  shdr->sh_addr,
bf5b25
 		  shdr->sh_offset,
bf5b25
 		  (unsigned int) shdr->sh_link,
bf5b25
-		  elf_strptr (ebl->elf, shstrndx,
bf5b25
-			      gelf_getshdr (elf_getscn (ebl->elf,
bf5b25
-							shdr->sh_link),
bf5b25
-					    &glink)->sh_name));
bf5b25
+		  elf_strptr (ebl->elf, shstrndx, glink->sh_name));
bf5b25
 
d88264
 	  uint32_t *lengths = (uint32_t *) xcalloc (nbucket,
d88264
 						    sizeof (uint32_t));
a78f3e
@@ -3510,6 +3539,16 @@ print_debug_aranges_section (Ebl *ebl __
bf5b25
       return;
bf5b25
     }
bf5b25
 
bf5b25
+  GElf_Shdr glink_mem;
bf5b25
+  GElf_Shdr *glink;
bf5b25
+  glink = gelf_getshdr (elf_getscn (ebl->elf, shdr->sh_link), &glink_mem);
bf5b25
+  if (glink == NULL)
bf5b25
+    {
bf5b25
+      error (0, 0, gettext ("invalid sh_link value in section %Zu"),
bf5b25
+	     elf_ndxscn (scn));
bf5b25
+      return;
bf5b25
+    }
bf5b25
+
bf5b25
   printf (ngettext ("\
bf5b25
 \nDWARF section '%s' at offset %#" PRIx64 " contains %zu entry:\n",
bf5b25
 		    "\
a78f3e
--- elfutils-0.120/src/strip.c
a78f3e
+++ elfutils-0.120/src/strip.c
a78f3e
@@ -412,6 +412,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
   Elf_Data debuglink_crc_data;
bf5b25
   bool any_symtab_changes = false;
bf5b25
   Elf_Data *shstrtab_data = NULL;
bf5b25
+  size_t shdridx = 0;
bf5b25
 
bf5b25
   /* Create the full name of the file.  */
bf5b25
   if (prefix != NULL)
a78f3e
@@ -542,6 +543,11 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
       goto fail_close;
bf5b25
     }
bf5b25
 
bf5b25
+  if (shstrndx >= shnum)
bf5b25
+    goto illformed;
bf5b25
+
bf5b25
+#define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
bf5b25
+
bf5b25
   /* Storage for section information.  We leave room for two more
bf5b25
      entries since we unconditionally create a section header string
bf5b25
      table.  Maybe some weird tool created an ELF file without one.
a78f3e
@@ -563,7 +569,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
     {
bf5b25
       /* This should always be true (i.e., there should not be any
bf5b25
 	 holes in the numbering).  */
bf5b25
-      assert (elf_ndxscn (scn) == cnt);
bf5b25
+      elf_assert (elf_ndxscn (scn) == cnt);
bf5b25
 
bf5b25
       shdr_info[cnt].scn = scn;
bf5b25
 
a78f3e
@@ -576,6 +582,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 					shdr_info[cnt].shdr.sh_name);
bf5b25
       if (shdr_info[cnt].name == NULL)
bf5b25
 	{
bf5b25
+	illformed:
bf5b25
 	  error (0, 0, gettext ("illformed file '%s'"), fname);
bf5b25
 	  goto fail_close;
bf5b25
 	}
a78f3e
@@ -585,6 +592,8 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 
bf5b25
       /* Remember the shdr.sh_link value.  */
bf5b25
       shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
bf5b25
+      if (shdr_info[cnt].old_sh_link >= shnum)
bf5b25
+	goto illformed;
bf5b25
 
bf5b25
       /* Sections in files other than relocatable object files which
bf5b25
 	 are not loaded can be freely moved by us.  In relocatable
a78f3e
@@ -597,7 +606,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 	 appropriate reference.  */
bf5b25
       if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
bf5b25
 	{
bf5b25
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
bf5b25
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
bf5b25
 	  shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
bf5b25
 	}
bf5b25
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
a78f3e
@@ -614,7 +623,12 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 	  for (inner = 1;
bf5b25
 	       inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
bf5b25
 	       ++inner)
bf5b25
-	    shdr_info[grpref[inner]].group_idx = cnt;
bf5b25
+	    {
bf5b25
+	      if (grpref[inner] < shnum)
bf5b25
+		shdr_info[grpref[inner]].group_idx = cnt;
bf5b25
+	      else
bf5b25
+		goto illformed;
bf5b25
+	    }
bf5b25
 
bf5b25
 	  if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
bf5b25
 	    /* If the section group contains only one element and this
a78f3e
@@ -625,7 +639,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 	}
bf5b25
       else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
bf5b25
 	{
bf5b25
-	  assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
bf5b25
+	  elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
bf5b25
 	  shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
bf5b25
 	}
bf5b25
 
a78f3e
@@ -633,7 +647,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 	 discarded right away.  */
bf5b25
       if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
bf5b25
 	{
bf5b25
-	  assert (shdr_info[cnt].group_idx != 0);
bf5b25
+	  elf_assert (shdr_info[cnt].group_idx != 0);
bf5b25
 
bf5b25
 	  if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
bf5b25
 	    {
a78f3e
@@ -708,10 +722,14 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 	    {
bf5b25
 	      /* If a relocation section is marked as being removed make
bf5b25
 		 sure the section it is relocating is removed, too.  */
bf5b25
-	      if ((shdr_info[cnt].shdr.sh_type == SHT_REL
bf5b25
-		   || shdr_info[cnt].shdr.sh_type == SHT_RELA)
bf5b25
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
bf5b25
-		shdr_info[cnt].idx = 1;
bf5b25
+	      if (shdr_info[cnt].shdr.sh_type == SHT_REL
bf5b25
+		  || shdr_info[cnt].shdr.sh_type == SHT_RELA)
bf5b25
+		{
bf5b25
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
bf5b25
+		    goto illformed;
bf5b25
+		  else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
bf5b25
+		    shdr_info[cnt].idx = 1;
bf5b25
+		}
bf5b25
 	    }
bf5b25
 
bf5b25
 	  if (shdr_info[cnt].idx == 1)
a78f3e
@@ -737,7 +755,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 		  if (shdr_info[cnt].symtab_idx != 0
bf5b25
 		      && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
bf5b25
 		    {
bf5b25
-		      assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
bf5b25
+		      elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
bf5b25
 
bf5b25
 		      shdr_info[shdr_info[cnt].symtab_idx].data
bf5b25
 			= elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
a78f3e
@@ -777,6 +795,9 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 		      else if (scnidx == SHN_XINDEX)
bf5b25
 			scnidx = xndx;
bf5b25
 
bf5b25
+		      if (scnidx >= shnum)
bf5b25
+			goto illformed;
bf5b25
+
bf5b25
 		      if (shdr_info[scnidx].idx == 0)
bf5b25
 			{
bf5b25
 			  /* Mark this section as used.  */
a78f3e
@@ -808,11 +829,15 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 		}
bf5b25
 
bf5b25
 	      /* Handle references through sh_info.  */
bf5b25
-	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr)
bf5b25
-		  && shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
bf5b25
+	      if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
bf5b25
 		{
bf5b25
-		  shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
bf5b25
-		  changes |= shdr_info[cnt].shdr.sh_info < cnt;
bf5b25
+		  if (shdr_info[cnt].shdr.sh_info >= shnum)
bf5b25
+		    goto illformed;
bf5b25
+		  else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
bf5b25
+		    {
bf5b25
+		      shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
bf5b25
+		      changes |= shdr_info[cnt].shdr.sh_info < cnt;
bf5b25
+		    }
bf5b25
 		}
bf5b25
 
bf5b25
 	      /* Mark the section as investigated.  */
a78f3e
@@ -912,7 +937,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 	  error (EXIT_FAILURE, 0, gettext ("while generating output file: %s"),
bf5b25
 		 elf_errmsg (-1));
bf5b25
 
bf5b25
-	assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
bf5b25
+	elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
bf5b25
 
bf5b25
 	/* Add this name to the section header string table.  */
bf5b25
 	shdr_info[cnt].se = ebl_strtabadd (shst, shdr_info[cnt].name, 0);
a78f3e
@@ -949,7 +974,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 	error (EXIT_FAILURE, 0,
bf5b25
 	       gettext ("while create section header section: %s"),
bf5b25
 	       elf_errmsg (-1));
bf5b25
-      assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
bf5b25
+      elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
bf5b25
 
bf5b25
       shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
bf5b25
       if (shdr_info[cnt].data == NULL)
a78f3e
@@ -980,7 +1005,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
     }
bf5b25
 
bf5b25
   /* Index of the section header table in the shdr_info array.  */
bf5b25
-  size_t shdridx = cnt;
bf5b25
+  shdridx = cnt;
bf5b25
 
bf5b25
   /* Add the section header string table section name.  */
bf5b25
   shdr_info[cnt].se = ebl_strtabadd (shst, ".shstrtab", 10);
a78f3e
@@ -1005,7 +1030,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
     error (EXIT_FAILURE, 0,
bf5b25
 	   gettext ("while create section header section: %s"),
bf5b25
 	   elf_errmsg (-1));
bf5b25
-  assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
bf5b25
+  elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
bf5b25
 
bf5b25
   /* Finalize the string table and fill in the correct indices in the
bf5b25
      section headers.  */
a78f3e
@@ -1095,21 +1120,21 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 		    shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
bf5b25
 					     NULL);
bf5b25
 
bf5b25
-		    assert ((versiondata->d_size / sizeof (Elf32_Word))
bf5b25
-			    >= shdr_info[cnt].data->d_size / elsize);
bf5b25
+		    elf_assert ((versiondata->d_size / sizeof (Elf32_Word))
bf5b25
+				>= shdr_info[cnt].data->d_size / elsize);
bf5b25
 		  }
bf5b25
 
bf5b25
 		if (shdr_info[cnt].version_idx != 0)
bf5b25
 		  {
bf5b25
-		    assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
bf5b25
+		    elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
bf5b25
 		    /* This section has associated version
bf5b25
 		       information.  We have to modify that
bf5b25
 		       information, too.  */
bf5b25
 		    versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
bf5b25
 					       NULL);
bf5b25
 
bf5b25
-		    assert ((versiondata->d_size / sizeof (GElf_Versym))
bf5b25
-			    >= shdr_info[cnt].data->d_size / elsize);
bf5b25
+		    elf_assert ((versiondata->d_size / sizeof (GElf_Versym))
bf5b25
+				>= shdr_info[cnt].data->d_size / elsize);
bf5b25
 		  }
bf5b25
 
bf5b25
 		shdr_info[cnt].newsymidx
a78f3e
@@ -1163,7 +1188,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 		      sec = shdr_info[sym->st_shndx].idx;
bf5b25
 		    else
bf5b25
 		      {
bf5b25
-			assert (shndxdata != NULL);
bf5b25
+			elf_assert (shndxdata != NULL);
bf5b25
 
bf5b25
 			sec = shdr_info[xshndx].idx;
bf5b25
 		      }
a78f3e
@@ -1184,7 +1209,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 			    nxshndx = sec;
bf5b25
 			  }
bf5b25
 
bf5b25
-			assert (sec < SHN_LORESERVE || shndxdata != NULL);
bf5b25
+			elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
bf5b25
 
bf5b25
 			if ((inner != destidx || nshndx != sym->st_shndx
bf5b25
 			     || (shndxdata != NULL && nxshndx != xshndx))
a78f3e
@@ -1207,7 +1232,7 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 		    else
bf5b25
 		      /* This is a section symbol for a section which has
bf5b25
 			 been removed.  */
bf5b25
-		      assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
bf5b25
+		      elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION);
bf5b25
 		  }
bf5b25
 
bf5b25
 		if (destidx != inner)
a78f3e
@@ -1371,11 +1396,11 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 		    {
bf5b25
 		      GElf_Sym sym_mem;
bf5b25
 		      GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
bf5b25
-		      assert (sym != NULL);
bf5b25
+		      elf_assert (sym != NULL);
bf5b25
 
bf5b25
 		      const char *name = elf_strptr (elf, strshndx,
bf5b25
 						     sym->st_name);
bf5b25
-		      assert (name != NULL);
bf5b25
+		      elf_assert (name != NULL);
bf5b25
 		      size_t hidx = elf_hash (name) % nbucket;
bf5b25
 
bf5b25
 		      if (bucket[hidx] == 0)
a78f3e
@@ -1394,8 +1419,8 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 	      else
bf5b25
 		{
bf5b25
 		  /* Alpha and S390 64-bit use 64-bit SHT_HASH entries.  */
bf5b25
-		  assert (shdr_info[cnt].shdr.sh_entsize
bf5b25
-			  == sizeof (Elf64_Xword));
bf5b25
+		  elf_assert (shdr_info[cnt].shdr.sh_entsize
bf5b25
+			      == sizeof (Elf64_Xword));
bf5b25
 
bf5b25
 		  Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
bf5b25
 
a78f3e
@@ -1428,11 +1453,11 @@ handle_elf (int fd, Elf *elf, const char
bf5b25
 		    {
bf5b25
 		      GElf_Sym sym_mem;
bf5b25
 		      GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
bf5b25
-		      assert (sym != NULL);
bf5b25
+		      elf_assert (sym != NULL);
bf5b25
 
bf5b25
 		      const char *name = elf_strptr (elf, strshndx,
bf5b25
 						     sym->st_name);
bf5b25
-		      assert (name != NULL);
bf5b25
+		      elf_assert (name != NULL);
bf5b25
 		      size_t hidx = elf_hash (name) % nbucket;
bf5b25
 
bf5b25
 		      if (bucket[hidx] == 0)