313519
From 6310d570bf20348135d09e1f9de84a9ae7d06f83 Mon Sep 17 00:00:00 2001
313519
From: Eyal Itkin <eyalit@checkpoint.com>
313519
Date: Thu, 2 Apr 2020 07:26:35 -0400
313519
Subject: Add tests for Safe-Linking
313519
313519
Adding the test "tst-safe-linking" for testing that Safe-Linking works
313519
as expected. The test checks these 3 main flows:
313519
 * tcache protection
313519
 * fastbin protection
313519
 * malloc_consolidate() correctness
313519
313519
As there is a random chance of 1/16 that of the alignment will remain
313519
correct, the test checks each flow up to 10 times, using different random
313519
values for the pointer corruption. As a result, the chance for a false
313519
failure of a given tested flow is 2**(-40), thus highly unlikely.
313519
313519
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
313519
313519
diff --git a/malloc/Makefile b/malloc/Makefile
313519
index 984045b5b9..e22cbde22d 100644
313519
--- a/malloc/Makefile
313519
+++ b/malloc/Makefile
313519
@@ -39,6 +39,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
313519
 	 tst-malloc-too-large \
313519
 	 tst-malloc-stats-cancellation \
313519
 	 tst-tcfree1 tst-tcfree2 tst-tcfree3 \
313519
+	 tst-safe-linking \
313519
 
313519
 tests-static := \
313519
 	 tst-interpose-static-nothread \
313519
diff --git a/malloc/tst-safe-linking.c b/malloc/tst-safe-linking.c
313519
new file mode 100644
313519
index 0000000000..067b6c09cf
313519
--- /dev/null
313519
+++ b/malloc/tst-safe-linking.c
313519
@@ -0,0 +1,179 @@
313519
+/* Test reporting of Safe-Linking caught errors.
313519
+   Copyright (C) 2020 Free Software Foundation, Inc.
313519
+   This file is part of the GNU C Library.
313519
+
313519
+   The GNU C Library is free software; you can redistribute it and/or
313519
+   modify it under the terms of the GNU Lesser General Public
313519
+   License as published by the Free Software Foundation; either
313519
+   version 2.1 of the License, or (at your option) any later version.
313519
+
313519
+   The GNU C Library is distributed in the hope that it will be useful,
313519
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
313519
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
313519
+   Lesser General Public License for more details.
313519
+
313519
+   You should have received a copy of the GNU Lesser General Public
313519
+   License along with the GNU C Library; if not, see
313519
+   <https://www.gnu.org/licenses/>.  */
313519
+
313519
+#include <signal.h>
313519
+#include <stdint.h>
313519
+#include <stdlib.h>
313519
+#include <memory.h>
313519
+#include <string.h>
313519
+#include <time.h>
313519
+#include <stdbool.h>
313519
+#include <support/capture_subprocess.h>
313519
+#include <support/check.h>
313519
+
313519
+/* Run CALLBACK and check that the data on standard error equals
313519
+   EXPECTED.  */
313519
+static void
313519
+check (const char *test, void (*callback) (void *),
313519
+       const char *expected)
313519
+{
313519
+  int i, rand_mask;
313519
+  bool success = false;
313519
+  /* There is a chance of 1/16 that a corrupted pointer will be aligned.
313519
+     Try multiple times so that statistical failure will be improbable.  */
313519
+  for (i = 0; i < 10 && !success; ++i)
313519
+    {
313519
+      rand_mask = rand () & 0xFF;
313519
+      struct support_capture_subprocess result
313519
+	= support_capture_subprocess (callback, &rand_mask);
313519
+      /* Did not crash, could happen.  Try again.  */
313519
+      if (strlen (result.err.buffer) == 0)
313519
+	continue;
313519
+      /* Crashed, must be the expected result.  */
313519
+      if (strcmp (result.err.buffer, expected) != 0)
313519
+	{
313519
+	  support_record_failure ();
313519
+	  printf ("error: test %s unexpected standard error data\n"
313519
+	          "  expected: %s\n"
313519
+	          "  actual:   %s\n",
313519
+	          test, expected, result.err.buffer);
313519
+	}
313519
+      TEST_VERIFY (WIFSIGNALED (result.status));
313519
+      if (WIFSIGNALED (result.status))
313519
+	TEST_VERIFY (WTERMSIG (result.status) == SIGABRT);
313519
+      support_capture_subprocess_free (&result);
313519
+      success = true;
313519
+    }
313519
+  TEST_VERIFY (success);
313519
+}
313519
+
313519
+/* Implementation details must be kept in sync with malloc.  */
313519
+#define TCACHE_FILL_COUNT               7
313519
+#define TCACHE_ALLOC_SIZE               0x20
313519
+#define MALLOC_CONSOLIDATE_SIZE         256*1024
313519
+
313519
+/* Try corrupting the tcache list.  */
313519
+static void
313519
+test_tcache (void *closure)
313519
+{
313519
+  int mask = ((int *)closure)[0];
313519
+  size_t size = TCACHE_ALLOC_SIZE;
313519
+
313519
+  /* Populate the tcache list.  */
313519
+  void * volatile a = malloc (size);
313519
+  void * volatile b = malloc (size);
313519
+  void * volatile c = malloc (size);
313519
+  free (a);
313519
+  free (b);
313519
+  free (c);
313519
+
313519
+  /* Corrupt the pointer with a random value, and avoid optimizations.  */
313519
+  printf ("Before: c=%p, c[0]=%p\n", c, ((void **)c)[0]);
313519
+  memset (c, mask & 0xFF, size);
313519
+  printf ("After: c=%p, c[0]=%p\n", c, ((void **)c)[0]);
313519
+
313519
+  c = malloc (size);
313519
+  /* This line will trigger the Safe-Linking check.  */
313519
+  b = malloc (size);
313519
+  printf ("b=%p\n", b);
313519
+}
313519
+
313519
+/* Try corrupting the fastbin list.  */
313519
+static void
313519
+test_fastbin (void *closure)
313519
+{
313519
+  int i;
313519
+  int mask = ((int *)closure)[0];
313519
+  size_t size = TCACHE_ALLOC_SIZE;
313519
+
313519
+  /* Take the tcache out of the game.  */
313519
+  for (i = 0; i < TCACHE_FILL_COUNT; ++i)
313519
+    {
313519
+      void * volatile p = calloc (1, size);
313519
+      free (p);
313519
+    }
313519
+
313519
+  /* Populate the fastbin list.  */
313519
+  void * volatile a = calloc (1, size);
313519
+  void * volatile b = calloc (1, size);
313519
+  void * volatile c = calloc (1, size);
313519
+  free (a);
313519
+  free (b);
313519
+  free (c);
313519
+
313519
+  /* Corrupt the pointer with a random value, and avoid optimizations.  */
313519
+  printf ("Before: c=%p, c[0]=%p\n", c, ((void **)c)[0]);
313519
+  memset (c, mask & 0xFF, size);
313519
+  printf ("After: c=%p, c[0]=%p\n", c, ((void **)c)[0]);
313519
+
313519
+  c = calloc (1, size);
313519
+  /* This line will trigger the Safe-Linking check.  */
313519
+  b = calloc (1, size);
313519
+  printf ("b=%p\n", b);
313519
+}
313519
+
313519
+/* Try corrupting the fastbin list and trigger a consolidate.  */
313519
+static void
313519
+test_fastbin_consolidate (void *closure)
313519
+{
313519
+  int i;
313519
+  int mask = ((int*)closure)[0];
313519
+  size_t size = TCACHE_ALLOC_SIZE;
313519
+
313519
+  /* Take the tcache out of the game.  */
313519
+  for (i = 0; i < TCACHE_FILL_COUNT; ++i)
313519
+    {
313519
+      void * volatile p = calloc (1, size);
313519
+      free (p);
313519
+    }
313519
+
313519
+  /* Populate the fastbin list.  */
313519
+  void * volatile a = calloc (1, size);
313519
+  void * volatile b = calloc (1, size);
313519
+  void * volatile c = calloc (1, size);
313519
+  free (a);
313519
+  free (b);
313519
+  free (c);
313519
+
313519
+  /* Corrupt the pointer with a random value, and avoid optimizations.  */
313519
+  printf ("Before: c=%p, c[0]=%p\n", c, ((void **)c)[0]);
313519
+  memset (c, mask & 0xFF, size);
313519
+  printf ("After: c=%p, c[0]=%p\n", c, ((void **)c)[0]);
313519
+
313519
+  /* This line will trigger the Safe-Linking check.  */
313519
+  b = malloc (MALLOC_CONSOLIDATE_SIZE);
313519
+  printf ("b=%p\n", b);
313519
+}
313519
+
313519
+static int
313519
+do_test (void)
313519
+{
313519
+  /* Seed the random for the test.  */
313519
+  srand (time (NULL));
313519
+
313519
+  check ("test_tcache", test_tcache,
313519
+         "malloc(): unaligned tcache chunk detected\n");
313519
+  check ("test_fastbin", test_fastbin,
313519
+         "malloc(): unaligned fastbin chunk detected 2\n");
313519
+  check ("test_fastbin_consolidate", test_fastbin_consolidate,
313519
+         "malloc_consolidate(): unaligned fastbin chunk detected\n");
313519
+
313519
+  return 0;
313519
+}
313519
+
313519
+#include <support/test-driver.c>