29e444
#
29e444
# As of 2013-08-09 this patch is posted upstream here, but not checked in yet:
29e444
# http://sourceware.org/ml/libc-alpha/2013-07/msg00367.html
29e444
# http://sourceware.org/ml/libc-alpha/2013-08/msg00057.html
29e444
#
29e444
# Red Hat bug:
29e444
# https://bugzilla.redhat.com/show_bug.cgi?id=990481
29e444
#
29e444
# Upstream bug:
29e444
# http://sourceware.org/bugzilla/show_bug.cgi?id=15754
29e444
#
29e444
# 2013-07-19  Carlos O'Donell  <carlos@redhat.com>
29e444
#
29e444
# 	[BZ #15754]
29e444
# 	* elf/Makefile (tests): Add tst-ptrguard1.
29e444
# 	(tests-static): Add tst-ptrguard1-static.
29e444
# 	(tst-ptrguard1-ARGS): Define.
29e444
# 	(tst-ptrguard1-static-ARGS): Define.
29e444
# 	* elf/tst-ptrguard1.c: New file.
29e444
# 	* elf/tst-ptrguard1-static.c: New file.
29e444
# 	* sysdeps/x86_64/stackguard-macros.h: Define POINTER_CHK_GUARD.
29e444
# 	* sysdeps/i386/stackguard-macros.h: Likewise.
29e444
# 	* sysdeps/powerpc/powerpc32/stackguard-macros.h: Likewise.
29e444
#	* sysdeps/powerpc/powerpc64/stackguard-macros.h: Likewise.
29e444
#	* sysdeps/s390/s390-32/stackguard-macros.h: Likewise.
29e444
#	* sysdeps/s390/s390-64/stackguard-macros.h: Likewise.
29e444
#
29e444
# 2013-07-19  Hector Marco  <hecmargi@upv.es>
29e444
# 	    Ismael Ripoll  <iripoll@disca.upv.es>
29e444
# 	    Carlos O'Donell  <carlos@redhat.com>
29e444
#
29e444
# 	[BZ #15754]
29e444
# 	* sysdeps/generic/stackguard-macros.h: Define __pointer_chk_guard_local
29e444
# 	and POINTER_CHK_GUARD.
29e444
# 	* csu/libc-start.c [!SHARED && !THREAD_SET_POINTER_GUARD]:
29e444
# 	Define __pointer_chk_guard_local.
29e444
# 	(LIBC_START_MAIN) [!SHARED]: Call _dl_setup_pointer_guard.
29e444
# 	Use THREAD_SET_POINTER_GUARD or set __pointer_chk_guard_local.
29e444
#
12745e
diff -urN glibc-2.17-c758a686/csu/libc-start.c glibc-2.17-c758a686/csu/libc-start.c
12745e
--- glibc-2.17-c758a686/csu/libc-start.c	2013-08-09 17:40:41.662856773 -0400
29e444
+++ glibc-2.17-c758a686/csu/libc-start.c	2013-08-09 17:53:40.383236966 -0400
29e444
@@ -38,6 +38,12 @@
29e444
    in thread local area.  */
29e444
 uintptr_t __stack_chk_guard attribute_relro;
29e444
 # endif
29e444
+# ifndef  THREAD_SET_POINTER_GUARD
29e444
+/* Only exported for architectures that don't store the pointer guard
29e444
+   value in thread local area.  */
29e444
+uintptr_t __pointer_chk_guard_local
29e444
+	attribute_relro attribute_hidden __attribute__ ((nocommon));
29e444
+# endif
29e444
 #endif
29e444
 
29e444
 #ifdef HAVE_PTR_NTHREADS
29e444
@@ -184,6 +190,16 @@
29e444
 # else
29e444
   __stack_chk_guard = stack_chk_guard;
29e444
 # endif
29e444
+
29e444
+  /* Set up the pointer guard value.  */
29e444
+  uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
29e444
+							 stack_chk_guard);
29e444
+# ifdef THREAD_SET_POINTER_GUARD
29e444
+  THREAD_SET_POINTER_GUARD (pointer_chk_guard);
29e444
+# else
29e444
+  __pointer_chk_guard_local = pointer_chk_guard;
29e444
+# endif
29e444
+
29e444
 #endif
29e444
 
29e444
   /* Register the destructor of the dynamic linker if there is any.  */
12745e
diff -urN glibc-2.17-c758a686/elf/Makefile glibc-2.17-c758a686/elf/Makefile
12745e
--- glibc-2.17-c758a686/elf/Makefile	2013-08-09 17:40:41.757856472 -0400
29e444
+++ glibc-2.17-c758a686/elf/Makefile	2013-08-09 17:53:40.383236966 -0400
29e444
@@ -121,7 +121,8 @@
29e444
 tests = tst-tls1 tst-tls2 tst-tls9 tst-leaks1 \
29e444
 	tst-array1 tst-array2 tst-array3 tst-array4 tst-array5
29e444
 tests-static = tst-tls1-static tst-tls2-static tst-stackguard1-static \
29e444
-	       tst-leaks1-static tst-array1-static tst-array5-static
29e444
+	       tst-leaks1-static tst-array1-static tst-array5-static \
29e444
+	       tst-ptrguard1-static
29e444
 ifeq (yes,$(build-shared))
29e444
 tests-static += tst-tls9-static
29e444
 tst-tls9-static-ENV = \
29e444
@@ -145,7 +146,7 @@
29e444
 	 tst-audit1 tst-audit2 tst-audit8 tst-audit9 \
29e444
 	 tst-stackguard1 tst-addr1 tst-thrlock \
29e444
 	 tst-unique1 tst-unique2 tst-unique3 tst-unique4 \
29e444
-	 tst-initorder tst-initorder2 tst-relsort1
29e444
+	 tst-initorder tst-initorder2 tst-relsort1 tst-ptrguard1
29e444
 #	 reldep9
29e444
 test-srcs = tst-pathopt
29e444
 selinux-enabled := $(shell cat /selinux/enforce 2> /dev/null)
29e444
@@ -1062,6 +1063,9 @@
29e444
 tst-stackguard1-ARGS = --command "$(host-built-program-cmd) --child"
29e444
 tst-stackguard1-static-ARGS = --command "$(objpfx)tst-stackguard1-static --child"
29e444
 
29e444
+tst-ptrguard1-ARGS = --command "$(host-built-program-cmd) --child"
29e444
+tst-ptrguard1-static-ARGS = --command "$(objpfx)tst-ptrguard1-static --child"
29e444
+
29e444
 $(objpfx)tst-leaks1: $(libdl)
29e444
 $(objpfx)tst-leaks1-mem: $(objpfx)tst-leaks1.out
29e444
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-leaks1.mtrace > $@
12745e
diff -urN glibc-2.17-c758a686/elf/tst-ptrguard1.c glibc-2.17-c758a686/elf/tst-ptrguard1.c
12745e
--- glibc-2.17-c758a686/elf/tst-ptrguard1.c	1969-12-31 19:00:00.000000000 -0500
29e444
+++ glibc-2.17-c758a686/elf/tst-ptrguard1.c	2013-08-09 17:53:40.383236966 -0400
29e444
@@ -0,0 +1,202 @@
29e444
+/* Copyright (C) 2013 Free Software Foundation, Inc.
29e444
+   This file is part of the GNU C Library.
29e444
+
29e444
+   The GNU C Library is free software; you can redistribute it and/or
29e444
+   modify it under the terms of the GNU Lesser General Public
29e444
+   License as published by the Free Software Foundation; either
29e444
+   version 2.1 of the License, or (at your option) any later version.
29e444
+
29e444
+   The GNU C Library is distributed in the hope that it will be useful,
29e444
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
29e444
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29e444
+   Lesser General Public License for more details.
29e444
+
29e444
+   You should have received a copy of the GNU Lesser General Public
29e444
+   License along with the GNU C Library; if not, see
29e444
+   <http://www.gnu.org/licenses/>.  */
29e444
+
29e444
+#include <errno.h>
29e444
+#include <stdbool.h>
29e444
+#include <stdio.h>
29e444
+#include <stdlib.h>
29e444
+#include <string.h>
29e444
+#include <sys/wait.h>
29e444
+#include <stackguard-macros.h>
29e444
+#include <tls.h>
29e444
+#include <unistd.h>
29e444
+
29e444
+#ifndef POINTER_CHK_GUARD
29e444
+extern uintptr_t __pointer_chk_guard_local;
29e444
+# define POINTER_CHK_GUARD __pointer_chk_guard_local
29e444
+#endif
29e444
+
29e444
+static const char *command;
29e444
+static bool child;
29e444
+static uintptr_t ptr_chk_guard_copy;
29e444
+static bool ptr_chk_guard_copy_set;
29e444
+static int fds[2];
29e444
+
29e444
+static void __attribute__ ((constructor))
29e444
+con (void)
29e444
+{
29e444
+  ptr_chk_guard_copy = POINTER_CHK_GUARD;
29e444
+  ptr_chk_guard_copy_set = true;
29e444
+}
29e444
+
29e444
+static int
29e444
+uintptr_t_cmp (const void *a, const void *b)
29e444
+{
29e444
+  if (*(uintptr_t *) a < *(uintptr_t *) b)
29e444
+    return 1;
29e444
+  if (*(uintptr_t *) a > *(uintptr_t *) b)
29e444
+    return -1;
29e444
+  return 0;
29e444
+}
29e444
+
29e444
+static int
29e444
+do_test (void)
29e444
+{
29e444
+  if (!ptr_chk_guard_copy_set)
29e444
+    {
29e444
+      puts ("constructor has not been run");
29e444
+      return 1;
29e444
+    }
29e444
+
29e444
+  if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
29e444
+    {
29e444
+      puts ("POINTER_CHK_GUARD changed between constructor and do_test");
29e444
+      return 1;
29e444
+    }
29e444
+
29e444
+  if (child)
29e444
+    {
29e444
+      write (2, &ptr_chk_guard_copy, sizeof (ptr_chk_guard_copy));
29e444
+      return 0;
29e444
+    }
29e444
+
29e444
+  if (command == NULL)
29e444
+    {
29e444
+      puts ("missing --command or --child argument");
29e444
+      return 1;
29e444
+    }
29e444
+
29e444
+#define N 16
29e444
+  uintptr_t child_ptr_chk_guards[N + 1];
29e444
+  child_ptr_chk_guards[N] = ptr_chk_guard_copy;
29e444
+  int i;
29e444
+  for (i = 0; i < N; ++i)
29e444
+    {
29e444
+      if (pipe (fds) < 0)
29e444
+	{
29e444
+	  printf ("couldn't create pipe: %m\n");
29e444
+	  return 1;
29e444
+	}
29e444
+
29e444
+      pid_t pid = fork ();
29e444
+      if (pid < 0)
29e444
+	{
29e444
+	  printf ("fork failed: %m\n");
29e444
+	  return 1;
29e444
+	}
29e444
+
29e444
+      if (!pid)
29e444
+	{
29e444
+	  if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
29e444
+	    {
29e444
+	      puts ("POINTER_CHK_GUARD changed after fork");
29e444
+	      exit (1);
29e444
+	    }
29e444
+
29e444
+	  close (fds[0]);
29e444
+	  close (2);
29e444
+	  dup2 (fds[1], 2);
29e444
+	  close (fds[1]);
29e444
+
29e444
+	  system (command);
29e444
+	  exit (0);
29e444
+	}
29e444
+
29e444
+      close (fds[1]);
29e444
+
29e444
+      if (TEMP_FAILURE_RETRY (read (fds[0], &child_ptr_chk_guards[i],
29e444
+				    sizeof (uintptr_t))) != sizeof (uintptr_t))
29e444
+	{
29e444
+	  puts ("could not read ptr_chk_guard value from child");
29e444
+	  return 1;
29e444
+	}
29e444
+
29e444
+      close (fds[0]);
29e444
+
29e444
+      pid_t termpid;
29e444
+      int status;
29e444
+      termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
29e444
+      if (termpid == -1)
29e444
+	{
29e444
+	  printf ("waitpid failed: %m\n");
29e444
+	  return 1;
29e444
+	}
29e444
+      else if (termpid != pid)
29e444
+	{
29e444
+	  printf ("waitpid returned %ld != %ld\n",
29e444
+		  (long int) termpid, (long int) pid);
29e444
+	  return 1;
29e444
+	}
29e444
+      else if (!WIFEXITED (status) || WEXITSTATUS (status))
29e444
+	{
29e444
+	  puts ("child hasn't exited with exit status 0");
29e444
+	  return 1;
29e444
+	}
29e444
+    }
29e444
+
29e444
+  qsort (child_ptr_chk_guards, N + 1, sizeof (uintptr_t), uintptr_t_cmp);
29e444
+
29e444
+  /* The default pointer guard is the same as the default stack guard.
29e444
+     They are only set to default if dl_random is NULL.  */
29e444
+  uintptr_t default_guard = 0;
29e444
+  unsigned char *p = (unsigned char *) &default_guard;
29e444
+  p[sizeof (uintptr_t) - 1] = 255;
29e444
+  p[sizeof (uintptr_t) - 2] = '\n';
29e444
+  p[0] = 0;
29e444
+
29e444
+  /* Test if the pointer guard canaries are either randomized,
29e444
+     or equal to the default pointer guard canary value.
29e444
+     Even with randomized pointer guards it might happen
29e444
+     that the random number generator generates the same
29e444
+     values, but if that happens in more than half from
29e444
+     the 16 runs, something is very wrong.  */
29e444
+  int ndifferences = 0;
29e444
+  int ndefaults = 0;
29e444
+  for (i = 0; i < N; ++i)
29e444
+    {
29e444
+      if (child_ptr_chk_guards[i] != child_ptr_chk_guards[i+1])
29e444
+	ndifferences++;
29e444
+      else if (child_ptr_chk_guards[i] == default_guard)
29e444
+	ndefaults++;
29e444
+    }
29e444
+
29e444
+  printf ("differences %d defaults %d\n", ndifferences, ndefaults);
29e444
+
29e444
+  if (ndifferences < N / 2 && ndefaults < N / 2)
29e444
+    {
29e444
+      puts ("pointer guard canaries are not randomized enough");
29e444
+      puts ("nor equal to the default canary value");
29e444
+      return 1;
29e444
+    }
29e444
+
29e444
+  return 0;
29e444
+}
29e444
+
29e444
+#define OPT_COMMAND	10000
29e444
+#define OPT_CHILD	10001
29e444
+#define CMDLINE_OPTIONS	\
29e444
+  { "command", required_argument, NULL, OPT_COMMAND },  \
29e444
+  { "child", no_argument, NULL, OPT_CHILD },
29e444
+#define CMDLINE_PROCESS	\
29e444
+  case OPT_COMMAND:	\
29e444
+    command = optarg;	\
29e444
+    break;		\
29e444
+  case OPT_CHILD:	\
29e444
+    child = true;	\
29e444
+    break;
29e444
+#define TEST_FUNCTION do_test ()
29e444
+#include "../test-skeleton.c"
12745e
diff -urN glibc-2.17-c758a686/elf/tst-ptrguard1-static.c glibc-2.17-c758a686/elf/tst-ptrguard1-static.c
12745e
--- glibc-2.17-c758a686/elf/tst-ptrguard1-static.c	1969-12-31 19:00:00.000000000 -0500
29e444
+++ glibc-2.17-c758a686/elf/tst-ptrguard1-static.c	2013-08-09 17:53:40.384236962 -0400
29e444
@@ -0,0 +1 @@
29e444
+#include "tst-ptrguard1.c"
12745e
diff -urN glibc-2.17-c758a686/sysdeps/generic/stackguard-macros.h glibc-2.17-c758a686/sysdeps/generic/stackguard-macros.h
12745e
--- glibc-2.17-c758a686/sysdeps/generic/stackguard-macros.h	2013-08-09 17:40:41.917855965 -0400
29e444
+++ glibc-2.17-c758a686/sysdeps/generic/stackguard-macros.h	2013-08-09 17:53:40.384236962 -0400
29e444
@@ -2,3 +2,6 @@
29e444
 
29e444
 extern uintptr_t __stack_chk_guard;
29e444
 #define STACK_CHK_GUARD __stack_chk_guard
29e444
+
29e444
+extern uintptr_t __pointer_chk_guard_local;
29e444
+#define POINTER_CHK_GUARD __pointer_chk_guard_local
12745e
diff -urN glibc-2.17-c758a686/sysdeps/i386/stackguard-macros.h glibc-2.17-c758a686/sysdeps/i386/stackguard-macros.h
12745e
--- glibc-2.17-c758a686/sysdeps/i386/stackguard-macros.h	2013-08-09 17:40:41.893856041 -0400
29e444
+++ glibc-2.17-c758a686/sysdeps/i386/stackguard-macros.h	2013-08-09 22:44:04.774298862 -0400
29e444
@@ -2,3 +2,11 @@
29e444
 
29e444
 #define STACK_CHK_GUARD \
29e444
   ({ uintptr_t x; asm ("movl %%gs:0x14, %0" : "=r" (x)); x; })
29e444
+
29e444
+#define POINTER_CHK_GUARD \
29e444
+  ({								\
29e444
+     uintptr_t x; 						\
29e444
+     asm ("movl %%gs:%c1, %0" : "=r" (x)			\
29e444
+	  : "i" (offsetof (tcbhead_t, pointer_guard)));		\
29e444
+     x;								\
29e444
+   })
12745e
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/stackguard-macros.h glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/stackguard-macros.h
12745e
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/stackguard-macros.h	2013-08-09 17:40:42.006855683 -0400
29e444
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc32/stackguard-macros.h	2013-08-09 22:24:48.778793075 -0400
29e444
@@ -2,3 +2,13 @@
29e444
 
29e444
 #define STACK_CHK_GUARD \
29e444
   ({ uintptr_t x; asm ("lwz %0,-28680(2)" : "=r" (x)); x; })
29e444
+
29e444
+#define POINTER_CHK_GUARD \
29e444
+  ({												\
29e444
+     uintptr_t x;										\
29e444
+     asm ("lwz %0,%1(2)"									\
29e444
+	  : "=r" (x)										\
29e444
+	  : "i" (offsetof (tcbhead_t, pointer_guard) - TLS_TCB_OFFSET - sizeof (tcbhead_t))	\
29e444
+         );											\
29e444
+     x;												\
29e444
+   })
12745e
diff -urN glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/stackguard-macros.h glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/stackguard-macros.h
12745e
--- glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/stackguard-macros.h	2013-08-09 17:40:41.994855721 -0400
29e444
+++ glibc-2.17-c758a686/sysdeps/powerpc/powerpc64/stackguard-macros.h	2013-08-09 22:24:47.831795865 -0400
29e444
@@ -2,3 +2,13 @@
29e444
 
29e444
 #define STACK_CHK_GUARD \
29e444
   ({ uintptr_t x; asm ("ld %0,-28688(13)" : "=r" (x)); x; })
29e444
+
29e444
+#define POINTER_CHK_GUARD \
29e444
+  ({												\
29e444
+     uintptr_t x;										\
29e444
+     asm ("ld %0,%1(13)"									\
29e444
+	  : "=r" (x)										\
29e444
+	  : "i" (offsetof (tcbhead_t, pointer_guard) - TLS_TCB_OFFSET - sizeof (tcbhead_t))	\
29e444
+         );											\
29e444
+     x;												\
29e444
+   })
12745e
diff -urN glibc-2.17-c758a686/sysdeps/s390/s390-32/stackguard-macros.h glibc-2.17-c758a686/sysdeps/s390/s390-32/stackguard-macros.h
12745e
--- glibc-2.17-c758a686/sysdeps/s390/s390-32/stackguard-macros.h	2013-08-09 17:40:42.059855515 -0400
29e444
+++ glibc-2.17-c758a686/sysdeps/s390/s390-32/stackguard-macros.h	2013-08-09 22:39:31.913120373 -0400
29e444
@@ -2,3 +2,15 @@
29e444
 
29e444
 #define STACK_CHK_GUARD \
29e444
   ({ uintptr_t x; asm ("ear %0,%%a0; l %0,0x14(%0)" : "=a" (x)); x; })
29e444
+
29e444
+/* On s390/s390x there is no unique pointer guard, instead we use the
29e444
+   same value as the stack guard.  */
29e444
+#define POINTER_CHK_GUARD \
29e444
+  ({							\
29e444
+     uintptr_t x;					\
29e444
+     asm ("ear %0,%%a0; l %0,%1(%0)"			\
29e444
+	  : "=a" (x)					\
29e444
+	  : "i" (offsetof (tcbhead_t, stack_guard)));	\
29e444
+     x;							\
29e444
+   })
29e444
+
12745e
diff -urN glibc-2.17-c758a686/sysdeps/x86_64/stackguard-macros.h glibc-2.17-c758a686/sysdeps/x86_64/stackguard-macros.h
12745e
--- glibc-2.17-c758a686/sysdeps/x86_64/stackguard-macros.h	2013-08-09 17:40:42.013855661 -0400
29e444
+++ glibc-2.17-c758a686/sysdeps/x86_64/stackguard-macros.h	2013-08-09 22:44:53.550153736 -0400
29e444
@@ -4,3 +4,11 @@
29e444
   ({ uintptr_t x;						\
29e444
      asm ("mov %%fs:%c1, %0" : "=r" (x)				\
29e444
 	  : "i" (offsetof (tcbhead_t, stack_guard))); x; })
29e444
+
29e444
+#define POINTER_CHK_GUARD \
29e444
+  ({								\
29e444
+     uintptr_t x;						\
29e444
+     asm ("mov %%fs:%c1, %0" : "=r" (x)				\
29e444
+	  : "i" (offsetof (tcbhead_t, pointer_guard)));		\
29e444
+     x;								\
29e444
+   })
12745e
--- glibc-2.17-c758a686/sysdeps/s390/s390-64/stackguard-macros.h	2013-08-09 17:40:42.057855522 -0400
29e444
+++ glibc-2.17-c758a686/sysdeps/s390/s390-64/stackguard-macros.h	2013-08-26 15:21:27.239043425 -0400
29e444
@@ -2,3 +2,17 @@
29e444
 
29e444
 #define STACK_CHK_GUARD \
29e444
   ({ uintptr_t x; asm ("ear %0,%%a0; sllg %0,%0,32; ear %0,%%a1; lg %0,0x28(%0)" : "=a" (x)); x; })
29e444
+
29e444
+/* On s390/s390x there is no unique pointer guard, instead we use the
29e444
+   same value as the stack guard.  */
29e444
+#define POINTER_CHK_GUARD \
29e444
+  ({							\
29e444
+     uintptr_t x;					\
29e444
+     asm ("ear %0,%%a0;"				\
29e444
+	  "sllg %0,%0,32;"				\
29e444
+	  "ear %0,%%a1;"				\
29e444
+	  "lg %0,%1(%0)"				\
29e444
+	  : "=a" (x)					\
29e444
+	  : "i" (offsetof (tcbhead_t, stack_guard)));	\
29e444
+     x;							\
29e444
+   })