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