arrfab / rpms / glibc

Forked from rpms/glibc 4 years ago
Clone

Blame SOURCES/glibc-rh990481-CVE-2013-4788.patch

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