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