]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libelf: Check compression ratio before trying to allocate output buffer.
authorMark Wielaard <mark@klomp.org>
Fri, 24 Mar 2017 14:06:04 +0000 (15:06 +0100)
committerMark Wielaard <mark@klomp.org>
Mon, 3 Apr 2017 21:53:10 +0000 (23:53 +0200)
The maximum compression factor (http://www.zlib.net/zlib_tech.html) is
1032:1. Add a sanity check for that before trying to allocate lots of
memory and trying to decompress lots of bogus data.

https://sourceware.org/bugzilla/show_bug.cgi?id=21301

Signed-off-by: Mark Wielaard <mark@klomp.org>
libelf/ChangeLog
libelf/elf_compress.c

index 8539cb56f975a7a438f0a48c2829870e1d6056b7..35e5271d202aafd06f83aba2b4875cf090449d8e 100644 (file)
@@ -1,3 +1,8 @@
+2017-03-24  Mark Wielaard  <mark@klomp.org>
+
+       * elf_compress.c (__libelf_decompress): Check insane compression
+       ratios before trying to allocate output buffer.
+
 2016-10-11  Akihiko Odaki  <akihiko.odaki.4i@stu.hosei.ac.jp>
            Mark Wielaard  <mjw@redhat.com>
 
index dac0ac6d07db53cc36234fe3bab94f4165fe2615..711be591b5e466ec207351ca1d31cc0bcbef59a6 100644 (file)
@@ -211,6 +211,15 @@ void *
 internal_function
 __libelf_decompress (void *buf_in, size_t size_in, size_t size_out)
 {
+  /* Catch highly unlikely compression ratios so we don't allocate
+     some giant amount of memory for nothing. The max compression
+     factor 1032:1 comes from http://www.zlib.net/zlib_tech.html  */
+  if (unlikely (size_out / 1032 > size_in))
+    {
+      __libelf_seterrno (ELF_E_INVALID_DATA);
+      return NULL;
+    }
+
   void *buf_out = malloc (size_out);
   if (unlikely (buf_out == NULL))
     {