]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Fix stack overflow with huge PT_NOTE segment [BZ #20419]
authorPaul Pluzhnikov <ppluzhnikov@google.com>
Sun, 6 May 2018 01:08:27 +0000 (18:08 -0700)
committerFlorian Weimer <fweimer@redhat.com>
Thu, 17 May 2018 12:08:37 +0000 (14:08 +0200)
A PT_NOTE in a binary could be arbitratily large, so using alloca
for it may cause stack overflow.  If the note is larger than
__MAX_ALLOCA_CUTOFF, use dynamically allocated memory to read it in.

2018-05-05  Paul Pluzhnikov  <ppluzhnikov@google.com>

[BZ #20419]
* elf/dl-load.c (open_verify): Fix stack overflow.
* elf/Makefile (tst-big-note): New test.
* elf/tst-big-note-lib.S: New.
* elf/tst-big-note.c: New.

(cherry picked from commit 0065aaaaae51cd60210ec3a7e13dddd8e01ffe2c)

ChangeLog
NEWS
elf/Makefile
elf/dl-load.c
elf/tst-big-note-lib.S [new file with mode: 0644]
elf/tst-big-note.c [new file with mode: 0644]

index 488bd789b8f86f0da2e08ae14d9bea3f9fb171f7..0bda4a3d1bb0d1134b062fb275df5590c0475c46 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2018-05-05  Paul Pluzhnikov  <ppluzhnikov@google.com>
+
+       [BZ #20419]
+       * elf/dl-load.c (open_verify): Fix stack overflow.
+       * elf/Makefile (tst-big-note): New test.
+       * elf/tst-big-note-lib.S: New.
+       * elf/tst-big-note.c: New.
+
 2018-05-04  Stefan Liebler  <stli@linux.vnet.ibm.com>
 
        [BZ #23137]
diff --git a/NEWS b/NEWS
index 8a2da5091f315ab9b50a242785524fc2cb8f081f..7f5b68a0069d3f794146afdb3a981ab49c9f163c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -76,6 +76,7 @@ The following bugs are resolved with this release:
   [16750] ldd: Never run file directly.
   [17343] Fix signed integer overflow in random_r
   [17956] crypt: Use NSPR header files in addition to NSS header files
+  [20419] elf: Fix stack overflow with huge PT_NOTE segment
   [20532] getaddrinfo: More robust handling of dlopen failures
   [21242] assert: Suppress pedantic warning caused by statement expression
   [21265] x86-64: Use fxsave/xsave/xsavec in _dl_runtime_resolve
index e758a4c96002ee44ec28a0358b567bcc241c1060..c2d5269d80242ec9512167eec082e5b47bcf93b8 100644 (file)
@@ -179,7 +179,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
         tst-initorder tst-initorder2 tst-relsort1 tst-null-argv \
         tst-tlsalign tst-tlsalign-extern tst-nodelete-opened \
         tst-nodelete2 tst-audit11 tst-audit12 tst-dlsym-error tst-noload \
-        tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose
+        tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose \
+        tst-big-note
 #       reldep9
 tests-internal += loadtest unload unload2 circleload1 \
         neededtest neededtest2 neededtest3 neededtest4 \
@@ -263,7 +264,9 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
                tst-audit11mod1 tst-audit11mod2 tst-auditmod11 \
                tst-audit12mod1 tst-audit12mod2 tst-audit12mod3 tst-auditmod12 \
                tst-latepthreadmod $(tst-tls-many-dynamic-modules) \
-               tst-nodelete-dlclose-dso tst-nodelete-dlclose-plugin
+               tst-nodelete-dlclose-dso tst-nodelete-dlclose-plugin \
+               tst-big-note-lib
+
 ifeq (yes,$(have-mtls-dialect-gnu2))
 tests += tst-gnu2-tls1
 modules-names += tst-gnu2-tls1mod
@@ -1410,3 +1413,5 @@ tst-env-setuid-ENV = MALLOC_CHECK_=2 MALLOC_MMAP_THRESHOLD_=4096 \
                     LD_HWCAP_MASK=0x1
 tst-env-setuid-tunables-ENV = \
        GLIBC_TUNABLES=glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096
+
+$(objpfx)tst-big-note: $(objpfx)tst-big-note-lib.so
index 7397c1882cb0522789fd37990c455ac7a3e3d8e4..127be5beca66e73e08f454fc9e40c0d78811d3ce 100644 (file)
@@ -1520,6 +1520,7 @@ open_verify (const char *name, int fd,
       ElfW(Ehdr) *ehdr;
       ElfW(Phdr) *phdr, *ph;
       ElfW(Word) *abi_note;
+      ElfW(Word) *abi_note_malloced = NULL;
       unsigned int osversion;
       size_t maplength;
 
@@ -1679,10 +1680,25 @@ open_verify (const char *name, int fd,
              abi_note = (void *) (fbp->buf + ph->p_offset);
            else
              {
-               abi_note = alloca (size);
+               /* Note: __libc_use_alloca is not usable here, because
+                  thread info may not have been set up yet.  */
+               if (size < __MAX_ALLOCA_CUTOFF)
+                 abi_note = alloca (size);
+               else
+                 {
+                   /* There could be multiple PT_NOTEs.  */
+                   abi_note_malloced = realloc (abi_note_malloced, size);
+                   if (abi_note_malloced == NULL)
+                     goto read_error;
+
+                   abi_note = abi_note_malloced;
+                 }
                __lseek (fd, ph->p_offset, SEEK_SET);
                if (__libc_read (fd, (void *) abi_note, size) != size)
-                 goto read_error;
+                 {
+                   free (abi_note_malloced);
+                   goto read_error;
+                 }
              }
 
            while (memcmp (abi_note, &expected_note, sizeof (expected_note)))
@@ -1718,6 +1734,7 @@ open_verify (const char *name, int fd,
 
            break;
          }
+      free (abi_note_malloced);
     }
 
   return fd;
diff --git a/elf/tst-big-note-lib.S b/elf/tst-big-note-lib.S
new file mode 100644 (file)
index 0000000..6b514a0
--- /dev/null
@@ -0,0 +1,26 @@
+/* Bug 20419: test for stack overflow in elf/dl-load.c open_verify()
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* This creates a .so with 8MiB PT_NOTE segment.
+   On a typical Linux system with 8MiB "ulimit -s", that was enough
+   to trigger stack overflow in open_verify.  */
+
+.pushsection .note.big,"a"
+.balign 4
+.fill 8*1024*1024, 1, 0
+.popsection
diff --git a/elf/tst-big-note.c b/elf/tst-big-note.c
new file mode 100644 (file)
index 0000000..fcd2b0e
--- /dev/null
@@ -0,0 +1,26 @@
+/* Bug 20419: test for stack overflow in elf/dl-load.c open_verify()
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+/* This file must be run from within a directory called "elf".  */
+
+int main (int argc, char *argv[])
+{
+  /* Nothing to do here: merely linking against tst-big-note-lib.so triggers
+     the bug.  */
+  return 0;
+}