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