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