2cd5135
#
2cd5135
# CVE-2013-4788 The pointer guard used for pointer mangling was not
2cd5135
# initialized for static applications resulting in the security feature
2cd5135
# being disabled. The pointer guard is now correctly initialized to a
2cd5135
# random value for static applications. Existing static applications need
2cd5135
# to be recompiled to take advantage of the fix (bug 15754).
2cd5135
#
2cd5135
# Not yet upstream.
2cd5135
#
2cd5135
diff --git a/csu/libc-start.c b/csu/libc-start.c
2cd5135
index e5da3ef..c898d06 100644
2cd5135
--- a/csu/libc-start.c
2cd5135
+++ b/csu/libc-start.c
2cd5135
@@ -37,6 +37,12 @@ extern void __pthread_initialize_minimal (void);
2cd5135
    in thread local area.  */
2cd5135
 uintptr_t __stack_chk_guard attribute_relro;
2cd5135
 # endif
2cd5135
+# ifndef  THREAD_SET_POINTER_GUARD
2cd5135
+/* Only exported for architectures that don't store the pointer guard
2cd5135
+   value in thread local area.  */
2cd5135
+uintptr_t __pointer_chk_guard_local
2cd5135
+	attribute_relro attribute_hidden __attribute__ ((nocommon));
2cd5135
+# endif
2cd5135
 #endif
2cd5135
 
2cd5135
 #ifdef HAVE_PTR_NTHREADS
2cd5135
@@ -195,6 +201,16 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
2cd5135
 # else
2cd5135
   __stack_chk_guard = stack_chk_guard;
2cd5135
 # endif
2cd5135
+
2cd5135
+  /* Set up the pointer guard value.  */
2cd5135
+  uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
2cd5135
+							 stack_chk_guard);
2cd5135
+# ifdef THREAD_SET_POINTER_GUARD
2cd5135
+  THREAD_SET_POINTER_GUARD (pointer_chk_guard);
2cd5135
+# else
2cd5135
+  __pointer_chk_guard_local = pointer_chk_guard;
2cd5135
+# endif
2cd5135
+
2cd5135
 #endif
2cd5135
 
2cd5135
   /* Register the destructor of the dynamic linker if there is any.  */
2cd5135
diff --git a/elf/Makefile b/elf/Makefile
2cd5135
index aaa9534..cb8da93 100644
2cd5135
--- a/elf/Makefile
2cd5135
+++ b/elf/Makefile
2cd5135
@@ -121,7 +121,8 @@ endif
2cd5135
 tests = tst-tls1 tst-tls2 tst-tls9 tst-leaks1 \
2cd5135
 	tst-array1 tst-array2 tst-array3 tst-array4 tst-array5
2cd5135
 tests-static = tst-tls1-static tst-tls2-static tst-stackguard1-static \
2cd5135
-	       tst-leaks1-static tst-array1-static tst-array5-static
2cd5135
+	       tst-leaks1-static tst-array1-static tst-array5-static \
2cd5135
+	       tst-ptrguard1-static
2cd5135
 ifeq (yes,$(build-shared))
2cd5135
 tests-static += tst-tls9-static
2cd5135
 tst-tls9-static-ENV = \
2cd5135
@@ -145,7 +146,8 @@ tests += loadtest restest1 preloadtest loadfail multiload origtest resolvfail \
2cd5135
 	 tst-audit1 tst-audit2 tst-audit8 \
2cd5135
 	 tst-stackguard1 tst-addr1 tst-thrlock \
2cd5135
 	 tst-unique1 tst-unique2 tst-unique3 tst-unique4 \
2cd5135
-	 tst-initorder tst-initorder2 tst-relsort1 tst-null-argv
2cd5135
+	 tst-initorder tst-initorder2 tst-relsort1 tst-null-argv \
2cd5135
+	 tst-ptrguard1
2cd5135
 #	 reldep9
2cd5135
 test-srcs = tst-pathopt
2cd5135
 selinux-enabled := $(shell cat /selinux/enforce 2> /dev/null)
2cd5135
@@ -1016,6 +1018,9 @@ LDFLAGS-order2mod2.so = $(no-as-needed)
2cd5135
 tst-stackguard1-ARGS = --command "$(host-test-program-cmd) --child"
2cd5135
 tst-stackguard1-static-ARGS = --command "$(objpfx)tst-stackguard1-static --child"
2cd5135
 
2cd5135
+tst-ptrguard1-ARGS = --command "$(host-test-program-cmd) --child"
2cd5135
+tst-ptrguard1-static-ARGS = --command "$(objpfx)tst-ptrguard1-static --child"
2cd5135
+
2cd5135
 $(objpfx)tst-leaks1: $(libdl)
2cd5135
 $(objpfx)tst-leaks1-mem: $(objpfx)tst-leaks1.out
2cd5135
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-leaks1.mtrace > $@
2cd5135
diff --git a/elf/tst-ptrguard1-static.c b/elf/tst-ptrguard1-static.c
2cd5135
new file mode 100644
2cd5135
index 0000000..7aff3b7
2cd5135
--- /dev/null
2cd5135
+++ b/elf/tst-ptrguard1-static.c
2cd5135
@@ -0,0 +1 @@
2cd5135
+#include "tst-ptrguard1.c"
2cd5135
diff --git a/elf/tst-ptrguard1.c b/elf/tst-ptrguard1.c
2cd5135
new file mode 100644
2cd5135
index 0000000..c344a04
2cd5135
--- /dev/null
2cd5135
+++ b/elf/tst-ptrguard1.c
2cd5135
@@ -0,0 +1,202 @@
2cd5135
+/* Copyright (C) 2013 Free Software Foundation, Inc.
2cd5135
+   This file is part of the GNU C Library.
2cd5135
+
2cd5135
+   The GNU C Library is free software; you can redistribute it and/or
2cd5135
+   modify it under the terms of the GNU Lesser General Public
2cd5135
+   License as published by the Free Software Foundation; either
2cd5135
+   version 2.1 of the License, or (at your option) any later version.
2cd5135
+
2cd5135
+   The GNU C Library is distributed in the hope that it will be useful,
2cd5135
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
2cd5135
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2cd5135
+   Lesser General Public License for more details.
2cd5135
+
2cd5135
+   You should have received a copy of the GNU Lesser General Public
2cd5135
+   License along with the GNU C Library; if not, see
2cd5135
+   <http://www.gnu.org/licenses/>.  */
2cd5135
+
2cd5135
+#include <errno.h>
2cd5135
+#include <stdbool.h>
2cd5135
+#include <stdio.h>
2cd5135
+#include <stdlib.h>
2cd5135
+#include <string.h>
2cd5135
+#include <sys/wait.h>
2cd5135
+#include <stackguard-macros.h>
2cd5135
+#include <tls.h>
2cd5135
+#include <unistd.h>
2cd5135
+
2cd5135
+#ifndef POINTER_CHK_GUARD
2cd5135
+extern uintptr_t __pointer_chk_guard;
2cd5135
+# define POINTER_CHK_GUARD __pointer_chk_guard
2cd5135
+#endif
2cd5135
+
2cd5135
+static const char *command;
2cd5135
+static bool child;
2cd5135
+static uintptr_t ptr_chk_guard_copy;
2cd5135
+static bool ptr_chk_guard_copy_set;
2cd5135
+static int fds[2];
2cd5135
+
2cd5135
+static void __attribute__ ((constructor))
2cd5135
+con (void)
2cd5135
+{
2cd5135
+  ptr_chk_guard_copy = POINTER_CHK_GUARD;
2cd5135
+  ptr_chk_guard_copy_set = true;
2cd5135
+}
2cd5135
+
2cd5135
+static int
2cd5135
+uintptr_t_cmp (const void *a, const void *b)
2cd5135
+{
2cd5135
+  if (*(uintptr_t *) a < *(uintptr_t *) b)
2cd5135
+    return 1;
2cd5135
+  if (*(uintptr_t *) a > *(uintptr_t *) b)
2cd5135
+    return -1;
2cd5135
+  return 0;
2cd5135
+}
2cd5135
+
2cd5135
+static int
2cd5135
+do_test (void)
2cd5135
+{
2cd5135
+  if (!ptr_chk_guard_copy_set)
2cd5135
+    {
2cd5135
+      puts ("constructor has not been run");
2cd5135
+      return 1;
2cd5135
+    }
2cd5135
+
2cd5135
+  if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
2cd5135
+    {
2cd5135
+      puts ("POINTER_CHK_GUARD changed between constructor and do_test");
2cd5135
+      return 1;
2cd5135
+    }
2cd5135
+
2cd5135
+  if (child)
2cd5135
+    {
2cd5135
+      write (2, &ptr_chk_guard_copy, sizeof (ptr_chk_guard_copy));
2cd5135
+      return 0;
2cd5135
+    }
2cd5135
+
2cd5135
+  if (command == NULL)
2cd5135
+    {
2cd5135
+      puts ("missing --command or --child argument");
2cd5135
+      return 1;
2cd5135
+    }
2cd5135
+
2cd5135
+#define N 16
2cd5135
+  uintptr_t child_ptr_chk_guards[N + 1];
2cd5135
+  child_ptr_chk_guards[N] = ptr_chk_guard_copy;
2cd5135
+  int i;
2cd5135
+  for (i = 0; i < N; ++i)
2cd5135
+    {
2cd5135
+      if (pipe (fds) < 0)
2cd5135
+	{
2cd5135
+	  printf ("couldn't create pipe: %m\n");
2cd5135
+	  return 1;
2cd5135
+	}
2cd5135
+
2cd5135
+      pid_t pid = fork ();
2cd5135
+      if (pid < 0)
2cd5135
+	{
2cd5135
+	  printf ("fork failed: %m\n");
2cd5135
+	  return 1;
2cd5135
+	}
2cd5135
+
2cd5135
+      if (!pid)
2cd5135
+	{
2cd5135
+	  if (ptr_chk_guard_copy != POINTER_CHK_GUARD)
2cd5135
+	    {
2cd5135
+	      puts ("POINTER_CHK_GUARD changed after fork");
2cd5135
+	      exit (1);
2cd5135
+	    }
2cd5135
+
2cd5135
+	  close (fds[0]);
2cd5135
+	  close (2);
2cd5135
+	  dup2 (fds[1], 2);
2cd5135
+	  close (fds[1]);
2cd5135
+
2cd5135
+	  system (command);
2cd5135
+	  exit (0);
2cd5135
+	}
2cd5135
+
2cd5135
+      close (fds[1]);
2cd5135
+
2cd5135
+      if (TEMP_FAILURE_RETRY (read (fds[0], &child_ptr_chk_guards[i],
2cd5135
+				    sizeof (uintptr_t))) != sizeof (uintptr_t))
2cd5135
+	{
2cd5135
+	  puts ("could not read ptr_chk_guard value from child");
2cd5135
+	  return 1;
2cd5135
+	}
2cd5135
+
2cd5135
+      close (fds[0]);
2cd5135
+
2cd5135
+      pid_t termpid;
2cd5135
+      int status;
2cd5135
+      termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
2cd5135
+      if (termpid == -1)
2cd5135
+	{
2cd5135
+	  printf ("waitpid failed: %m\n");
2cd5135
+	  return 1;
2cd5135
+	}
2cd5135
+      else if (termpid != pid)
2cd5135
+	{
2cd5135
+	  printf ("waitpid returned %ld != %ld\n",
2cd5135
+		  (long int) termpid, (long int) pid);
2cd5135
+	  return 1;
2cd5135
+	}
2cd5135
+      else if (!WIFEXITED (status) || WEXITSTATUS (status))
2cd5135
+	{
2cd5135
+	  puts ("child hasn't exited with exit status 0");
2cd5135
+	  return 1;
2cd5135
+	}
2cd5135
+    }
2cd5135
+
2cd5135
+  qsort (child_ptr_chk_guards, N + 1, sizeof (uintptr_t), uintptr_t_cmp);
2cd5135
+
2cd5135
+  /* The default pointer guard is the same as the default stack guard.
2cd5135
+     They are only set to default if dl_random is NULL.  */
2cd5135
+  uintptr_t default_guard = 0;
2cd5135
+  unsigned char *p = (unsigned char *) &default_guard;
2cd5135
+  p[sizeof (uintptr_t) - 1] = 255;
2cd5135
+  p[sizeof (uintptr_t) - 2] = '\n';
2cd5135
+  p[0] = 0;
2cd5135
+
2cd5135
+  /* Test if the pointer guard canaries are either randomized,
2cd5135
+     or equal to the default pointer guard value.
2cd5135
+     Even with randomized pointer guards it might happen
2cd5135
+     that the random number generator generates the same
2cd5135
+     values, but if that happens in more than half from
2cd5135
+     the 16 runs, something is very wrong.  */
2cd5135
+  int ndifferences = 0;
2cd5135
+  int ndefaults = 0;
2cd5135
+  for (i = 0; i < N; ++i)
2cd5135
+    {
2cd5135
+      if (child_ptr_chk_guards[i] != child_ptr_chk_guards[i+1])
2cd5135
+	ndifferences++;
2cd5135
+      else if (child_ptr_chk_guards[i] == default_guard)
2cd5135
+	ndefaults++;
2cd5135
+    }
2cd5135
+
2cd5135
+  printf ("differences %d defaults %d\n", ndifferences, ndefaults);
2cd5135
+
2cd5135
+  if (ndifferences < N / 2 && ndefaults < N / 2)
2cd5135
+    {
2cd5135
+      puts ("pointer guard values are not randomized enough");
2cd5135
+      puts ("nor equal to the default value");
2cd5135
+      return 1;
2cd5135
+    }
2cd5135
+
2cd5135
+  return 0;
2cd5135
+}
2cd5135
+
2cd5135
+#define OPT_COMMAND	10000
2cd5135
+#define OPT_CHILD	10001
2cd5135
+#define CMDLINE_OPTIONS	\
2cd5135
+  { "command", required_argument, NULL, OPT_COMMAND },  \
2cd5135
+  { "child", no_argument, NULL, OPT_CHILD },
2cd5135
+#define CMDLINE_PROCESS	\
2cd5135
+  case OPT_COMMAND:	\
2cd5135
+    command = optarg;	\
2cd5135
+    break;		\
2cd5135
+  case OPT_CHILD:	\
2cd5135
+    child = true;	\
2cd5135
+    break;
2cd5135
+#define TEST_FUNCTION do_test ()
2cd5135
+#include "../test-skeleton.c"
2cd5135
diff --git a/ports/sysdeps/ia64/stackguard-macros.h b/ports/sysdeps/ia64/stackguard-macros.h
2cd5135
index dc683c2..3907293 100644
2cd5135
--- a/ports/sysdeps/ia64/stackguard-macros.h
2cd5135
+++ b/ports/sysdeps/ia64/stackguard-macros.h
2cd5135
@@ -2,3 +2,6 @@
2cd5135
 
2cd5135
 #define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("adds %0 = -8, r13;; ld8 %0 = [%0]" : "=r" (x)); x; })
2cd5135
+
2cd5135
+#define POINTER_CHK_GUARD \
2cd5135
+  ({ uintptr_t x; asm ("adds %0 = -16, r13;; ld8 %0 = [%0]" : "=r" (x)); x; })
2cd5135
diff --git a/ports/sysdeps/tile/stackguard-macros.h b/ports/sysdeps/tile/stackguard-macros.h
2cd5135
index 589ea2b..f2e041b 100644
2cd5135
--- a/ports/sysdeps/tile/stackguard-macros.h
2cd5135
+++ b/ports/sysdeps/tile/stackguard-macros.h
2cd5135
@@ -4,11 +4,17 @@
2cd5135
 # if __WORDSIZE == 64
2cd5135
 #  define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("addi %0, tp, -16; ld %0, %0" : "=r" (x)); x; })
2cd5135
+#  define POINTER_CHK_GUARD \
2cd5135
+  ({ uintptr_t x; asm ("addi %0, tp, -24; ld %0, %0" : "=r" (x)); x; })
2cd5135
 # else
2cd5135
 #  define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("addi %0, tp, -8; ld4s %0, %0" : "=r" (x)); x; })
2cd5135
+#  define POINTER_CHK_GUARD \
2cd5135
+  ({ uintptr_t x; asm ("addi %0, tp, -12; ld4s %0, %0" : "=r" (x)); x; })
2cd5135
 # endif
2cd5135
 #else
2cd5135
 # define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("addi %0, tp, -8; lw %0, %0" : "=r" (x)); x; })
2cd5135
+# define POINTER_CHK_GUARD \
2cd5135
+  ({ uintptr_t x; asm ("addi %0, tp, -12; lw %0, %0" : "=r" (x)); x; })
2cd5135
 #endif
2cd5135
diff --git a/sysdeps/generic/stackguard-macros.h b/sysdeps/generic/stackguard-macros.h
2cd5135
index ababf65..77408c6 100644
2cd5135
--- a/sysdeps/generic/stackguard-macros.h
2cd5135
+++ b/sysdeps/generic/stackguard-macros.h
2cd5135
@@ -2,3 +2,6 @@
2cd5135
 
2cd5135
 extern uintptr_t __stack_chk_guard;
2cd5135
 #define STACK_CHK_GUARD __stack_chk_guard
2cd5135
+
593edbc
+extern uintptr_t __pointer_chk_guard_local;
593edbc
+#define POINTER_CHK_GUARD __pointer_chk_guard_local
2cd5135
diff --git a/sysdeps/i386/stackguard-macros.h b/sysdeps/i386/stackguard-macros.h
2cd5135
index 8c31e19..0397629 100644
2cd5135
--- a/sysdeps/i386/stackguard-macros.h
2cd5135
+++ b/sysdeps/i386/stackguard-macros.h
2cd5135
@@ -2,3 +2,11 @@
2cd5135
 
2cd5135
 #define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("movl %%gs:0x14, %0" : "=r" (x)); x; })
2cd5135
+
2cd5135
+#define POINTER_CHK_GUARD \
2cd5135
+  ({							\
2cd5135
+     uintptr_t x;					\
2cd5135
+     asm ("movl %%gs:%c1, %0" : "=r" (x)		\
2cd5135
+	  : "i" (offsetof (tcbhead_t, pointer_guard)));	\
2cd5135
+     x;							\
2cd5135
+   })
2cd5135
diff --git a/sysdeps/powerpc/powerpc32/stackguard-macros.h b/sysdeps/powerpc/powerpc32/stackguard-macros.h
2cd5135
index 839f6a4..b3d0af8 100644
2cd5135
--- a/sysdeps/powerpc/powerpc32/stackguard-macros.h
2cd5135
+++ b/sysdeps/powerpc/powerpc32/stackguard-macros.h
2cd5135
@@ -2,3 +2,13 @@
2cd5135
 
2cd5135
 #define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("lwz %0,-28680(2)" : "=r" (x)); x; })
2cd5135
+
2cd5135
+#define POINTER_CHK_GUARD \
2cd5135
+  ({												\
2cd5135
+     uintptr_t x;										\
2cd5135
+     asm ("lwz %0,%1(2)"									\
2cd5135
+	  : "=r" (x)										\
2cd5135
+	  : "i" (offsetof (tcbhead_t, pointer_guard) - TLS_TCB_OFFSET - sizeof (tcbhead_t))	\
2cd5135
+         );											\
2cd5135
+     x;												\
2cd5135
+   })
2cd5135
diff --git a/sysdeps/powerpc/powerpc64/stackguard-macros.h b/sysdeps/powerpc/powerpc64/stackguard-macros.h
2cd5135
index 9da879c..4620f96 100644
2cd5135
--- a/sysdeps/powerpc/powerpc64/stackguard-macros.h
2cd5135
+++ b/sysdeps/powerpc/powerpc64/stackguard-macros.h
2cd5135
@@ -2,3 +2,13 @@
2cd5135
 
2cd5135
 #define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("ld %0,-28688(13)" : "=r" (x)); x; })
2cd5135
+
2cd5135
+#define POINTER_CHK_GUARD \
2cd5135
+  ({												\
2cd5135
+     uintptr_t x;										\
2cd5135
+     asm ("ld %0,%1(2)"										\
2cd5135
+	  : "=r" (x)										\
2cd5135
+	  : "i" (offsetof (tcbhead_t, pointer_guard) - TLS_TCB_OFFSET - sizeof (tcbhead_t))	\
2cd5135
+         );											\
2cd5135
+     x;												\
2cd5135
+   })
2cd5135
diff --git a/sysdeps/s390/s390-32/stackguard-macros.h b/sysdeps/s390/s390-32/stackguard-macros.h
2cd5135
index b74c579..449e8d4 100644
2cd5135
--- a/sysdeps/s390/s390-32/stackguard-macros.h
2cd5135
+++ b/sysdeps/s390/s390-32/stackguard-macros.h
2cd5135
@@ -2,3 +2,14 @@
2cd5135
 
2cd5135
 #define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("ear %0,%%a0; l %0,0x14(%0)" : "=a" (x)); x; })
2cd5135
+
2cd5135
+/* On s390/s390x there is no unique pointer guard, instead we use the
2cd5135
+   same value as the stack guard.  */
2cd5135
+#define POINTER_CHK_GUARD \
2cd5135
+  ({							\
2cd5135
+     uintptr_t x;					\
2cd5135
+     asm ("ear %0,%%a0; l %0,%1(%0)"			\
2cd5135
+	  : "=a" (x)					\
2cd5135
+	  : "i" (offsetof (tcbhead_t, stack_guard)));	\
2cd5135
+     x;							\
2cd5135
+   })
2cd5135
diff --git a/sysdeps/s390/s390-64/stackguard-macros.h b/sysdeps/s390/s390-64/stackguard-macros.h
2cd5135
index 0cebb5f..c8270fb 100644
2cd5135
--- a/sysdeps/s390/s390-64/stackguard-macros.h
2cd5135
+++ b/sysdeps/s390/s390-64/stackguard-macros.h
2cd5135
@@ -2,3 +2,17 @@
2cd5135
 
2cd5135
 #define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("ear %0,%%a0; sllg %0,%0,32; ear %0,%%a1; lg %0,0x28(%0)" : "=a" (x)); x; })
2cd5135
+
2cd5135
+/* On s390/s390x there is no unique pointer guard, instead we use the
2cd5135
+   same value as the stack guard.  */
2cd5135
+#define POINTER_CHK_GUARD \
2cd5135
+  ({							\
2cd5135
+     uintptr_t x;					\
2cd5135
+     asm ("ear %0,%%a0;"				\
2cd5135
+	  "sllg %0,%0,32;"				\
2cd5135
+	  "ear %0,%%a1;"				\
2cd5135
+	  "lg %0,%1(%0)"				\
2cd5135
+	 : "=a" (x)					\
2cd5135
+	 : "i" (offsetof (tcbhead_t, stack_guard)));	\
2cd5135
+     x;							\
2cd5135
+   })
2cd5135
diff --git a/sysdeps/sparc/sparc32/stackguard-macros.h b/sysdeps/sparc/sparc32/stackguard-macros.h
2cd5135
index c0b02b0..1eef0f1 100644
2cd5135
--- a/sysdeps/sparc/sparc32/stackguard-macros.h
2cd5135
+++ b/sysdeps/sparc/sparc32/stackguard-macros.h
2cd5135
@@ -2,3 +2,6 @@
2cd5135
 
2cd5135
 #define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("ld [%%g7+0x14], %0" : "=r" (x)); x; })
2cd5135
+
2cd5135
+#define POINTER_CHK_GUARD \
2cd5135
+  ({ uintptr_t x; asm ("ld [%%g7+0x18], %0" : "=r" (x)); x; })
2cd5135
diff --git a/sysdeps/sparc/sparc64/stackguard-macros.h b/sysdeps/sparc/sparc64/stackguard-macros.h
2cd5135
index 80f0635..cc0c12c 100644
2cd5135
--- a/sysdeps/sparc/sparc64/stackguard-macros.h
2cd5135
+++ b/sysdeps/sparc/sparc64/stackguard-macros.h
2cd5135
@@ -2,3 +2,6 @@
2cd5135
 
2cd5135
 #define STACK_CHK_GUARD \
2cd5135
   ({ uintptr_t x; asm ("ldx [%%g7+0x28], %0" : "=r" (x)); x; })
2cd5135
+
2cd5135
+#define POINTER_CHK_GUARD \
2cd5135
+  ({ uintptr_t x; asm ("ldx [%%g7+0x30], %0" : "=r" (x)); x; })
2cd5135
diff --git a/sysdeps/x86_64/stackguard-macros.h b/sysdeps/x86_64/stackguard-macros.h
2cd5135
index d7fedb3..1948800 100644
2cd5135
--- a/sysdeps/x86_64/stackguard-macros.h
2cd5135
+++ b/sysdeps/x86_64/stackguard-macros.h
2cd5135
@@ -4,3 +4,8 @@
2cd5135
   ({ uintptr_t x;						\
2cd5135
      asm ("mov %%fs:%c1, %0" : "=r" (x)				\
2cd5135
 	  : "i" (offsetof (tcbhead_t, stack_guard))); x; })
2cd5135
+
2cd5135
+#define POINTER_CHK_GUARD \
2cd5135
+  ({ uintptr_t x;						\
2cd5135
+     asm ("mov %%fs:%c1, %0" : "=r" (x)				\
2cd5135
+	  : "i" (offsetof (tcbhead_t, pointer_guard))); x; })
1a343f8
diff --git a/elf/Makefile b/elf/Makefile
1a343f8
index cb8da93..27d249b 100644
1a343f8
--- a/elf/Makefile
1a343f8
+++ b/elf/Makefile
1a343f8
@@ -1019,6 +1019,9 @@ tst-stackguard1-ARGS = --command "$(host-test-program-cmd) --child"
1a343f8
 tst-stackguard1-static-ARGS = --command "$(objpfx)tst-stackguard1-static --child"
1a343f8
 
1a343f8
 tst-ptrguard1-ARGS = --command "$(host-test-program-cmd) --child"
1a343f8
+# When built statically, the pointer guard interface uses
1a343f8
+# __pointer_chk_guard_local.
1a343f8
+CFLAGS-tst-ptrguard1-static.c = -DPTRGUARD_LOCAL
1a343f8
 tst-ptrguard1-static-ARGS = --command "$(objpfx)tst-ptrguard1-static --child"
1a343f8
 
1a343f8
 $(objpfx)tst-leaks1: $(libdl)
1a343f8
diff --git a/sysdeps/generic/stackguard-macros.h b/sysdeps/generic/stackguard-macros.h
1a343f8
index 4fa3d96..b4a6b23 100644
1a343f8
--- a/sysdeps/generic/stackguard-macros.h
1a343f8
+++ b/sysdeps/generic/stackguard-macros.h
1a343f8
@@ -3,5 +3,10 @@
1a343f8
 extern uintptr_t __stack_chk_guard;
1a343f8
 #define STACK_CHK_GUARD __stack_chk_guard
1a343f8
 
1a343f8
+#ifdef PTRGUARD_LOCAL
1a343f8
 extern uintptr_t __pointer_chk_guard_local;
1a343f8
-#define POINTER_CHK_GUARD __pointer_chk_guard_local
1a343f8
+# define POINTER_CHK_GUARD __pointer_chk_guard_local
1a343f8
+#else
1a343f8
+extern uintptr_t __pointer_chk_guard;
1a343f8
+# define POINTER_CHK_GUARD __pointer_chk_guard
1a343f8
+#endif