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