File libelf-sanity-check-offset-and-size.patch of Package elfutils.13244

From: Mark Wielaard <mark at klomp dot org>
Subject: [PATCH] libelf: Sanity check offset and size before trying to malloc
  and read data.
Date: Fri, 21 Oct 2016 08:24:00
Reference: bnc#1030476

Bad sh_off or sh_size could trigger a bad malloc or read. Sanity check
the header values first before trying to malloc a huge buffer or reading
any data that will certainly fail.

https://bugzilla.redhat.com/show_bug.cgi?id=1387584

elfutils packaging edits:
 - changelog hunk removed from original patch to avoid conflict
 - extracted from mailing list web interface, thus missing some information

Signed-off-by: Mark Wielaard <mjw(a)redhat.com&gt;
diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c
index d1fafbf..97c503b 100644
--- a/libelf/elf_getdata.c
+++ b/libelf/elf_getdata.c
@@ -312,6 +312,17 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
 	}
       else if (likely (elf->fildes != -1))
 	{
+	  /* First see whether the information in the section header is
+	     valid and it does not ask for too much.  Check for unsigned
+	     overflow.  */
+	  if (unlikely (offset > elf->maximum_size
+			|| elf->maximum_size - offset < size))
+	    {
+	      /* Something is wrong.  */
+	      __libelf_seterrno (ELF_E_INVALID_SECTION_HEADER);
+	      return 1;
+	    }
+
 	  /* We have to read the data from the file.  Allocate the needed
 	     memory.  */
 	  scn->rawdata_base = scn->rawdata.d.d_buf
-- 
1.8.3.1
openSUSE Build Service is sponsored by