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