olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1642094-3.patch

077c9d
commit 7c9a7c68363051cfc5fa1ebb96b3b2c1f82dcb76
077c9d
Author: DJ Delorie <dj@redhat.com>
077c9d
Date:   Fri Nov 30 22:13:09 2018 -0500
077c9d
077c9d
    malloc: Add another test for tcache double free check.
077c9d
    
077c9d
    This one tests for BZ#23907 where the double free
077c9d
    test didn't check the tcache bin bounds before dereferencing
077c9d
    the bin.
077c9d
    
077c9d
    [BZ #23907]
077c9d
    * malloc/tst-tcfree3.c: New.
077c9d
    * malloc/Makefile: Add it.
077c9d
077c9d
diff --git a/malloc/Makefile b/malloc/Makefile
077c9d
index e6dfbfc14cb3d140..388cf7e9ee3a2569 100644
077c9d
--- a/malloc/Makefile
077c9d
+++ b/malloc/Makefile
077c9d
@@ -38,7 +38,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
077c9d
 	 tst-malloc_info \
077c9d
 	 tst-malloc-too-large \
077c9d
 	 tst-malloc-stats-cancellation \
077c9d
-	 tst-tcfree1 tst-tcfree2 \
077c9d
+	 tst-tcfree1 tst-tcfree2 tst-tcfree3 \
077c9d
 
077c9d
 tests-static := \
077c9d
 	 tst-interpose-static-nothread \
077c9d
diff --git a/malloc/tst-tcfree3.c b/malloc/tst-tcfree3.c
077c9d
new file mode 100644
077c9d
index 0000000000000000..016d30ddd8114082
077c9d
--- /dev/null
077c9d
+++ b/malloc/tst-tcfree3.c
077c9d
@@ -0,0 +1,56 @@
077c9d
+/* Test that malloc tcache catches double free.
077c9d
+   Copyright (C) 2018 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#include <malloc.h>
077c9d
+#include <string.h>
077c9d
+
077c9d
+/* Prevent GCC from optimizing away any malloc/free pairs.  */
077c9d
+#pragma GCC optimize ("O0")
077c9d
+
077c9d
+static int
077c9d
+do_test (void)
077c9d
+{
077c9d
+  /* Do two allocation of any size that fit in tcache, and one that
077c9d
+     doesn't.  */
077c9d
+  int ** volatile a = malloc (32);
077c9d
+  int ** volatile b = malloc (32);
077c9d
+  /* This is just under the mmap threshold.  */
077c9d
+  int ** volatile c = malloc (127 * 1024);
077c9d
+
077c9d
+  /* The invalid "tcache bucket" we might dereference will likely end
077c9d
+     up somewhere within this memory block, so make all the accidental
077c9d
+     "next" pointers cause segfaults.  BZ #23907.  */
077c9d
+  memset (c, 0xff, 127 * 1024);
077c9d
+
077c9d
+  free (a); // puts in tcache
077c9d
+
077c9d
+  /* A is now free and contains the key we use to detect in-tcache.
077c9d
+     Copy the key to the other chunks.  */
077c9d
+  memcpy (b, a, 32);
077c9d
+  memcpy (c, a, 32);
077c9d
+
077c9d
+  /* This free tests the "are we in the tcache already" loop with a
077c9d
+     VALID bin but "coincidental" matching key.  */
077c9d
+  free (b); // should NOT abort
077c9d
+  /* This free tests the "is it a valid tcache bin" test.  */
077c9d
+  free (c); // should NOT abort
077c9d
+
077c9d
+  return 0;
077c9d
+}
077c9d
+
077c9d
+#include <support/test-driver.c>