50f89d
commit bcdaad21d4635931d1bd3b54a7894276925d081d
50f89d
Author: DJ Delorie <dj@delorie.com>
50f89d
Date:   Tue Nov 20 13:24:09 2018 -0500
50f89d
50f89d
    malloc: tcache double free check
50f89d
    
50f89d
    * malloc/malloc.c (tcache_entry): Add key field.
50f89d
    (tcache_put): Set it.
50f89d
    (tcache_get): Likewise.
50f89d
    (_int_free): Check for double free in tcache.
50f89d
    * malloc/tst-tcfree1.c: New.
50f89d
    * malloc/tst-tcfree2.c: New.
50f89d
    * malloc/Makefile: Run the new tests.
50f89d
    * manual/probes.texi: Document memory_tcache_double_free probe.
50f89d
    
50f89d
    * dlfcn/dlerror.c (check_free): Prevent double frees.
50f89d
50f89d
diff --git a/dlfcn/dlerror.c b/dlfcn/dlerror.c
50f89d
index 33574faab65628ff..96bf92533335036b 100644
50f89d
--- a/dlfcn/dlerror.c
50f89d
+++ b/dlfcn/dlerror.c
50f89d
@@ -198,7 +198,10 @@ check_free (struct dl_action_result *rec)
50f89d
       Dl_info info;
50f89d
       if (_dl_addr (check_free, &info, &map, NULL) != 0 && map->l_ns == 0)
50f89d
 #endif
50f89d
-	free ((char *) rec->errstring);
50f89d
+	{
50f89d
+	  free ((char *) rec->errstring);
50f89d
+	  rec->errstring = NULL;
50f89d
+	}
50f89d
     }
50f89d
 }
50f89d
 
50f89d
diff --git a/malloc/Makefile b/malloc/Makefile
50f89d
index 7d54bad866f63cb8..e6dfbfc14cb3d140 100644
50f89d
--- a/malloc/Makefile
50f89d
+++ b/malloc/Makefile
50f89d
@@ -38,6 +38,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
50f89d
 	 tst-malloc_info \
50f89d
 	 tst-malloc-too-large \
50f89d
 	 tst-malloc-stats-cancellation \
50f89d
+	 tst-tcfree1 tst-tcfree2 \
50f89d
 
50f89d
 tests-static := \
50f89d
 	 tst-interpose-static-nothread \
50f89d
diff --git a/malloc/malloc.c b/malloc/malloc.c
50f89d
index e247c77b7d4de26e..c6b0282e783eaeea 100644
50f89d
--- a/malloc/malloc.c
50f89d
+++ b/malloc/malloc.c
50f89d
@@ -2888,6 +2888,8 @@ mremap_chunk (mchunkptr p, size_t new_size)
50f89d
 typedef struct tcache_entry
50f89d
 {
50f89d
   struct tcache_entry *next;
50f89d
+  /* This field exists to detect double frees.  */
50f89d
+  struct tcache_perthread_struct *key;
50f89d
 } tcache_entry;
50f89d
 
50f89d
 /* There is one of these for each thread, which contains the
50f89d
@@ -2911,6 +2913,11 @@ tcache_put (mchunkptr chunk, size_t tc_idx)
50f89d
 {
50f89d
   tcache_entry *e = (tcache_entry *) chunk2mem (chunk);
50f89d
   assert (tc_idx < TCACHE_MAX_BINS);
50f89d
+
50f89d
+  /* Mark this chunk as "in the tcache" so the test in _int_free will
50f89d
+     detect a double free.  */
50f89d
+  e->key = tcache;
50f89d
+
50f89d
   e->next = tcache->entries[tc_idx];
50f89d
   tcache->entries[tc_idx] = e;
50f89d
   ++(tcache->counts[tc_idx]);
50f89d
@@ -2926,6 +2933,7 @@ tcache_get (size_t tc_idx)
50f89d
   assert (tcache->entries[tc_idx] > 0);
50f89d
   tcache->entries[tc_idx] = e->next;
50f89d
   --(tcache->counts[tc_idx]);
50f89d
+  e->key = NULL;
50f89d
   return (void *) e;
50f89d
 }
50f89d
 
50f89d
@@ -4152,6 +4160,26 @@ _int_free (mstate av, mchunkptr p, int have_lock)
50f89d
   {
50f89d
     size_t tc_idx = csize2tidx (size);
50f89d
 
50f89d
+    /* Check to see if it's already in the tcache.  */
50f89d
+    tcache_entry *e = (tcache_entry *) chunk2mem (p);
50f89d
+
50f89d
+    /* This test succeeds on double free.  However, we don't 100%
50f89d
+       trust it (it also matches random payload data at a 1 in
50f89d
+       2^<size_t> chance), so verify it's not an unlikely coincidence
50f89d
+       before aborting.  */
50f89d
+    if (__glibc_unlikely (e->key == tcache && tcache))
50f89d
+      {
50f89d
+	tcache_entry *tmp;
50f89d
+	LIBC_PROBE (memory_tcache_double_free, 2, e, tc_idx);
50f89d
+	for (tmp = tcache->entries[tc_idx];
50f89d
+	     tmp;
50f89d
+	     tmp = tmp->next)
50f89d
+	  if (tmp == e)
50f89d
+	    malloc_printerr ("free(): double free detected in tcache 2");
50f89d
+	/* If we get here, it was a coincidence.  We've wasted a few
50f89d
+	   cycles, but don't abort.  */
50f89d
+      }
50f89d
+
50f89d
     if (tcache
50f89d
 	&& tc_idx < mp_.tcache_bins
50f89d
 	&& tcache->counts[tc_idx] < mp_.tcache_count)
50f89d
diff --git a/malloc/tst-tcfree1.c b/malloc/tst-tcfree1.c
50f89d
new file mode 100644
50f89d
index 0000000000000000..bc29375ce77304ac
50f89d
--- /dev/null
50f89d
+++ b/malloc/tst-tcfree1.c
50f89d
@@ -0,0 +1,42 @@
50f89d
+/* Test that malloc tcache catches double free.
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+#include <errno.h>
50f89d
+#include <error.h>
50f89d
+#include <limits.h>
50f89d
+#include <malloc.h>
50f89d
+#include <stdlib.h>
50f89d
+#include <stdio.h>
50f89d
+#include <sys/signal.h>
50f89d
+
50f89d
+static int
50f89d
+do_test (void)
50f89d
+{
50f89d
+  /* Do one allocation of any size that fits in tcache.  */
50f89d
+  char * volatile x = malloc (32);
50f89d
+
50f89d
+  free (x); // puts in tcache
50f89d
+  free (x); // should abort
50f89d
+
50f89d
+  printf("FAIL: tcache double free not detected\n");
50f89d
+  return 1;
50f89d
+}
50f89d
+
50f89d
+#define TEST_FUNCTION do_test
50f89d
+#define EXPECTED_SIGNAL SIGABRT
50f89d
+#include <support/test-driver.c>
50f89d
diff --git a/malloc/tst-tcfree2.c b/malloc/tst-tcfree2.c
50f89d
new file mode 100644
50f89d
index 0000000000000000..17f06bacd411c315
50f89d
--- /dev/null
50f89d
+++ b/malloc/tst-tcfree2.c
50f89d
@@ -0,0 +1,48 @@
50f89d
+/* Test that malloc tcache catches double free.
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+#include <errno.h>
50f89d
+#include <error.h>
50f89d
+#include <limits.h>
50f89d
+#include <malloc.h>
50f89d
+#include <stdlib.h>
50f89d
+#include <stdio.h>
50f89d
+#include <sys/signal.h>
50f89d
+
50f89d
+static int
50f89d
+do_test (void)
50f89d
+{
50f89d
+  char * volatile ptrs[20];
50f89d
+  int i;
50f89d
+
50f89d
+  /* Allocate enough small chunks so that when we free them all, the tcache
50f89d
+     is full, and the first one we freed is at the end of its linked list.  */
50f89d
+#define COUNT 20
50f89d
+  for (i=0; i
50f89d
+    ptrs[i] = malloc (20);
50f89d
+  for (i=0; i
50f89d
+    free (ptrs[i]);
50f89d
+  free (ptrs[0]);
50f89d
+
50f89d
+  printf("FAIL: tcache double free\n");
50f89d
+  return 1;
50f89d
+}
50f89d
+
50f89d
+#define TEST_FUNCTION do_test
50f89d
+#define EXPECTED_SIGNAL SIGABRT
50f89d
+#include <support/test-driver.c>
50f89d
diff --git a/manual/probes.texi b/manual/probes.texi
50f89d
index ab2a3102bb350ef4..0ea560ed78bcfd7e 100644
50f89d
--- a/manual/probes.texi
50f89d
+++ b/manual/probes.texi
50f89d
@@ -243,6 +243,18 @@ This probe is triggered when the
50f89d
 value of this tunable.
50f89d
 @end deftp
50f89d
 
50f89d
+@deftp Probe memory_tcache_double_free (void *@var{$arg1}, int @var{$arg2})
50f89d
+This probe is triggered when @code{free} determines that the memory
50f89d
+being freed has probably already been freed, and resides in the
50f89d
+per-thread cache.  Note that there is an extremely unlikely chance
50f89d
+that this probe will trigger due to random payload data remaining in
50f89d
+the allocated memory matching the key used to detect double frees.
50f89d
+This probe actually indicates that an expensive linear search of the
50f89d
+tcache, looking for a double free, has happened.  Argument @var{$arg1}
50f89d
+is the memory location as passed to @code{free}, Argument @var{$arg2}
50f89d
+is the tcache bin it resides in.
50f89d
+@end deftp
50f89d
+
50f89d
 @node Mathematical Function Probes
50f89d
 @section Mathematical Function Probes
50f89d