Blame SOURCES/glibc-rh1374654.patch

147e83
From 0f58539030e436449f79189b6edab17d7479796e Mon Sep 17 00:00:00 2001
147e83
From: Paul Pluzhnikov <ppluzhnikov@google.com>
147e83
Date: Sat, 8 Aug 2015 15:53:03 -0700
147e83
Subject: [PATCH] Fix BZ #17905
147e83
147e83
diff -rupN a/catgets/Makefile b/catgets/Makefile
147e83
--- a/catgets/Makefile	2017-03-03 17:54:39.000000000 -0500
147e83
+++ b/catgets/Makefile	2017-03-03 18:05:02.506889588 -0500
147e83
@@ -44,13 +44,15 @@ catgets-CPPFLAGS := -DNLSPATH='"$(msgcat
147e83
 
147e83
 generated = de.msg test1.cat test1.h test2.cat test2.h sample.SJIS.cat \
147e83
 	    test-gencat.h
147e83
+generated += tst-catgets.mtrace tst-catgets-mem.out
147e83
+
147e83
 generated-dirs = de
147e83
 
147e83
-tst-catgets-ENV = NLSPATH="$(objpfx)%l/%N.cat" LANG=de
147e83
+tst-catgets-ENV = NLSPATH="$(objpfx)%l/%N.cat" LANG=de MALLOC_TRACE=$(objpfx)tst-catgets.mtrace
147e83
 
147e83
 ifeq ($(run-built-tests),yes)
147e83
 tests: $(objpfx)de/libc.cat $(objpfx)test1.cat $(objpfx)test2.cat \
147e83
-       $(objpfx)test-gencat.out
147e83
+       $(objpfx)test-gencat.out $(objpfx)tst-catgets-mem.out
147e83
 # This test just checks whether the program produces any error or not.
147e83
 # The result is not tested.
147e83
 $(objpfx)test1.cat: test1.msg $(objpfx)gencat
147e83
@@ -78,4 +80,8 @@ $(objpfx)test-gencat.out: test-gencat.sh
147e83
 $(objpfx)sample.SJIS.cat: sample.SJIS $(objpfx)gencat
147e83
 	GCONV_PATH=$(common-objpfx)iconvdata LC_ALL=C \
147e83
 	$(built-program-cmd) -H $(objpfx)test-gencat.h < $(word 1,$^) > $@
147e83
+
147e83
+$(objpfx)tst-catgets-mem.out: $(objpfx)tst-catgets.out
147e83
+	$(common-objpfx)malloc/mtrace $(objpfx)tst-catgets.mtrace > $@; \
147e83
+	$(evaluate-test)
147e83
 endif
147e83
diff -rupN a/catgets/catgets.c b/catgets/catgets.c
147e83
--- a/catgets/catgets.c	2012-12-24 22:02:13.000000000 -0500
147e83
+++ b/catgets/catgets.c	2017-03-03 17:55:43.750147349 -0500
147e83
@@ -16,7 +16,6 @@
147e83
    License along with the GNU C Library; if not, see
147e83
    <http://www.gnu.org/licenses/>.  */
147e83
 
147e83
-#include <alloca.h>
147e83
 #include <errno.h>
147e83
 #include <locale.h>
147e83
 #include <nl_types.h>
147e83
@@ -35,6 +34,7 @@ catopen (const char *cat_name, int flag)
147e83
   __nl_catd result;
147e83
   const char *env_var = NULL;
147e83
   const char *nlspath = NULL;
147e83
+  char *tmp = NULL;
147e83
 
147e83
   if (strchr (cat_name, '/') == NULL)
147e83
     {
147e83
@@ -54,7 +54,10 @@ catopen (const char *cat_name, int flag)
147e83
 	{
147e83
 	  /* Append the system dependent directory.  */
147e83
 	  size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
147e83
-	  char *tmp = alloca (len);
147e83
+	  tmp = malloc (len);
147e83
+
147e83
+	  if (__glibc_unlikely (tmp == NULL))
147e83
+	    return (nl_catd) -1;
147e83
 
147e83
 	  __stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
147e83
 	  nlspath = tmp;
147e83
@@ -65,16 +68,18 @@ catopen (const char *cat_name, int flag)
147e83
 
147e83
   result = (__nl_catd) malloc (sizeof (*result));
147e83
   if (result == NULL)
147e83
-    /* We cannot get enough memory.  */
147e83
-    return (nl_catd) -1;
147e83
-
147e83
-  if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
147e83
+    {
147e83
+      /* We cannot get enough memory.  */
147e83
+      result = (nl_catd) -1;
147e83
+    }
147e83
+  else if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
147e83
     {
147e83
       /* Couldn't open the file.  */
147e83
       free ((void *) result);
147e83
-      return (nl_catd) -1;
147e83
+      result = (nl_catd) -1;
147e83
     }
147e83
 
147e83
+  free (tmp);
147e83
   return (nl_catd) result;
147e83
 }
147e83
 
147e83
diff -rupN a/catgets/open_catalog.c b/catgets/open_catalog.c
147e83
--- a/catgets/open_catalog.c	2012-12-24 22:02:13.000000000 -0500
147e83
+++ b/catgets/open_catalog.c	2017-03-03 17:55:43.753147332 -0500
147e83
@@ -47,6 +47,7 @@ __open_catalog (const char *cat_name, co
147e83
   size_t tab_size;
147e83
   const char *lastp;
147e83
   int result = -1;
147e83
+  char *buf = NULL;
147e83
 
147e83
   if (strchr (cat_name, '/') != NULL || nlspath == NULL)
147e83
     fd = open_not_cancel_2 (cat_name, O_RDONLY);
147e83
@@ -57,23 +58,23 @@ __open_catalog (const char *cat_name, co
147e83
   if (__builtin_expect (bufact + (n) >= bufmax, 0))			      \
147e83
     {									      \
147e83
       char *old_buf = buf;						      \
147e83
-      bufmax += 256 + (n);						      \
147e83
-      buf = (char *) alloca (bufmax);					      \
147e83
-      memcpy (buf, old_buf, bufact);					      \
147e83
+      bufmax += (bufmax < 256 + (n)) ? 256 + (n) : bufmax;		      \
147e83
+      buf = realloc (buf, bufmax);					      \
147e83
+      if (__glibc_unlikely (buf == NULL))				      \
147e83
+	{								      \
147e83
+	  free (old_buf);						      \
147e83
+	  return -1;							      \
147e83
+	}								      \
147e83
     }
147e83
 
147e83
       /* The RUN_NLSPATH variable contains a colon separated list of
147e83
 	 descriptions where we expect to find catalogs.  We have to
147e83
 	 recognize certain % substitutions and stop when we found the
147e83
 	 first existing file.  */
147e83
-      char *buf;
147e83
       size_t bufact;
147e83
-      size_t bufmax;
147e83
+      size_t bufmax = 0;
147e83
       size_t len;
147e83
 
147e83
-      buf = NULL;
147e83
-      bufmax = 0;
147e83
-
147e83
       fd = -1;
147e83
       while (*run_nlspath != '\0')
147e83
 	{
147e83
@@ -188,7 +189,10 @@ __open_catalog (const char *cat_name, co
147e83
 
147e83
   /* Avoid dealing with directories and block devices */
147e83
   if (__builtin_expect (fd, 0) < 0)
147e83
-    return -1;
147e83
+    {
147e83
+      free (buf);
147e83
+      return -1;
147e83
+    }
147e83
 
147e83
   if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
147e83
     goto close_unlock_return;
147e83
@@ -325,6 +329,7 @@ __open_catalog (const char *cat_name, co
147e83
   /* Release the lock again.  */
147e83
  close_unlock_return:
147e83
   close_not_cancel_no_status (fd);
147e83
+  free (buf);
147e83
 
147e83
   return result;
147e83
 }
147e83
diff -rupN a/catgets/tst-catgets.c b/catgets/tst-catgets.c
147e83
--- a/catgets/tst-catgets.c	2017-03-03 17:54:38.000000000 -0500
147e83
+++ b/catgets/tst-catgets.c	2017-03-03 17:55:43.755147321 -0500
147e83
@@ -1,7 +1,10 @@
147e83
+#include <assert.h>
147e83
 #include <mcheck.h>
147e83
 #include <nl_types.h>
147e83
 #include <stdio.h>
147e83
+#include <stdlib.h>
147e83
 #include <string.h>
147e83
+#include <sys/resource.h>
147e83
 
147e83
 
147e83
 static const char *msgs[] =
147e83
@@ -12,6 +15,33 @@ static const char *msgs[] =
147e83
 };
147e83
 #define nmsgs (sizeof (msgs) / sizeof (msgs[0]))
147e83
 
147e83
+
147e83
+/* Test for unbounded alloca.  */
147e83
+static int
147e83
+do_bz17905 (void)
147e83
+{
147e83
+  char *buf;
147e83
+  struct rlimit rl;
147e83
+  nl_catd result;
147e83
+
147e83
+  const int sz = 1024 * 1024;
147e83
+
147e83
+  getrlimit (RLIMIT_STACK, &rl);
147e83
+  rl.rlim_cur = sz;
147e83
+  setrlimit (RLIMIT_STACK, &rl);
147e83
+
147e83
+  buf = malloc (sz + 1); 
147e83
+  memset (buf, 'A', sz);
147e83
+  buf[sz] = '\0';
147e83
+  setenv ("NLSPATH", buf, 1);
147e83
+
147e83
+  result = catopen (buf, NL_CAT_LOCALE);
147e83
+  assert (result == (nl_catd) -1);
147e83
+
147e83
+  free (buf);
147e83
+  return 0;
147e83
+}
147e83
+
147e83
 #define ROUNDS 5
147e83
 
147e83
 static int
147e83
@@ -62,6 +92,7 @@ do_test (void)
147e83
 	}
147e83
     }
147e83
 
147e83
+  result += do_bz17905 ();
147e83
   return result;
147e83
 }
147e83