1
From 0f58539030e436449f79189b6edab17d7479796e Mon Sep 17 00:00:00 2001
2
From: Paul Pluzhnikov <ppluzhnikov@google.com>
3
Date: Sat, 8 Aug 2015 15:53:03 -0700
4
Subject: [PATCH] Fix BZ #17905
7
* catgets/Makefile (tst-catgets-mem): New test.
8
* catgets/catgets.c (catopen): Don't use unbounded alloca.
9
* catgets/open_catalog.c (__open_catalog): Likewise.
10
* catgets/tst-catgets.c (do_bz17905): Test unbounded alloca.
12
[Note: patch differs from upstream commit in that the entries in
13
the Changelog and NEWS were dropped to avoid patch conflicts. -- sbeattie]
15
catgets/Makefile | 10 ++++++++--
16
catgets/catgets.c | 19 ++++++++++++-------
17
catgets/open_catalog.c | 23 ++++++++++++++---------
18
catgets/tst-catgets.c | 31 +++++++++++++++++++++++++++++++
19
4 files changed, 65 insertions(+), 18 deletions(-)
21
Index: b/catgets/Makefile
22
===================================================================
23
--- a/catgets/Makefile
24
+++ b/catgets/Makefile
25
@@ -48,14 +48,16 @@ CPPFLAGS-gencat = -DNOT_IN_libc
27
generated = de.msg test1.cat test1.h test2.cat test2.h sample.SJIS.cat \
29
+generated += tst-catgets.mtrace tst-catgets-mem.out
33
-tst-catgets-ENV = NLSPATH="$(objpfx)%l/%N.cat" LANG=de
34
+tst-catgets-ENV = NLSPATH="$(objpfx)%l/%N.cat" LANG=de MALLOC_TRACE=$(objpfx)tst-catgets.mtrace
36
ifeq ($(run-built-tests),yes)
37
ifeq (y,$(OPTION_EGLIBC_CATGETS))
38
tests: $(objpfx)de/libc.cat $(objpfx)test1.cat $(objpfx)test2.cat \
39
- $(objpfx)test-gencat.out
40
+ $(objpfx)test-gencat.out $(objpfx)tst-catgets-mem.out
42
# This test just checks whether the program produces any error or not.
43
# The result is not tested.
44
@@ -84,4 +86,8 @@ $(objpfx)test-gencat.out: test-gencat.sh
45
$(objpfx)sample.SJIS.cat: sample.SJIS $(objpfx)gencat
46
GCONV_PATH=$(common-objpfx)iconvdata LC_ALL=C \
47
$(built-program-cmd) -H $(objpfx)test-gencat.h < $(word 1,$^) > $@
49
+$(objpfx)tst-catgets-mem.out: $(objpfx)tst-catgets.out
50
+ $(common-objpfx)malloc/mtrace $(objpfx)tst-catgets.mtrace > $@; \
53
Index: b/catgets/catgets.c
54
===================================================================
55
--- a/catgets/catgets.c
56
+++ b/catgets/catgets.c
58
License along with the GNU C Library; if not, see
59
<http://www.gnu.org/licenses/>. */
65
@@ -35,6 +34,7 @@ catopen (const char *cat_name, int flag)
67
const char *env_var = NULL;
68
const char *nlspath = NULL;
71
if (strchr (cat_name, '/') == NULL)
73
@@ -54,7 +54,10 @@ catopen (const char *cat_name, int flag)
75
/* Append the system dependent directory. */
76
size_t len = strlen (nlspath) + 1 + sizeof NLSPATH;
77
- char *tmp = alloca (len);
80
+ if (__glibc_unlikely (tmp == NULL))
81
+ return (nl_catd) -1;
83
__stpcpy (__stpcpy (__stpcpy (tmp, nlspath), ":"), NLSPATH);
85
@@ -65,16 +68,18 @@ catopen (const char *cat_name, int flag)
87
result = (__nl_catd) malloc (sizeof (*result));
89
- /* We cannot get enough memory. */
90
- return (nl_catd) -1;
92
- if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
94
+ /* We cannot get enough memory. */
95
+ result = (nl_catd) -1;
97
+ else if (__open_catalog (cat_name, nlspath, env_var, result) != 0)
99
/* Couldn't open the file. */
100
free ((void *) result);
101
- return (nl_catd) -1;
102
+ result = (nl_catd) -1;
106
return (nl_catd) result;
109
Index: b/catgets/open_catalog.c
110
===================================================================
111
--- a/catgets/open_catalog.c
112
+++ b/catgets/open_catalog.c
113
@@ -47,6 +47,7 @@ __open_catalog (const char *cat_name, co
119
if (strchr (cat_name, '/') != NULL || nlspath == NULL)
120
fd = open_not_cancel_2 (cat_name, O_RDONLY);
121
@@ -57,23 +58,23 @@ __open_catalog (const char *cat_name, co
122
if (__builtin_expect (bufact + (n) >= bufmax, 0)) \
124
char *old_buf = buf; \
125
- bufmax += 256 + (n); \
126
- buf = (char *) alloca (bufmax); \
127
- memcpy (buf, old_buf, bufact); \
128
+ bufmax += (bufmax < 256 + (n)) ? 256 + (n) : bufmax; \
129
+ buf = realloc (buf, bufmax); \
130
+ if (__glibc_unlikely (buf == NULL)) \
137
/* The RUN_NLSPATH variable contains a colon separated list of
138
descriptions where we expect to find catalogs. We have to
139
recognize certain % substitutions and stop when we found the
140
first existing file. */
151
while (*run_nlspath != '\0')
153
@@ -188,7 +189,10 @@ __open_catalog (const char *cat_name, co
155
/* Avoid dealing with directories and block devices */
156
if (__builtin_expect (fd, 0) < 0)
163
if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0) < 0)
164
goto close_unlock_return;
165
@@ -325,6 +329,7 @@ __open_catalog (const char *cat_name, co
166
/* Release the lock again. */
168
close_not_cancel_no_status (fd);
173
Index: b/catgets/tst-catgets.c
174
===================================================================
175
--- a/catgets/tst-catgets.c
176
+++ b/catgets/tst-catgets.c
180
#include <nl_types.h>
184
+#include <sys/resource.h>
187
static const char *msgs[] =
188
@@ -12,6 +15,33 @@ static const char *msgs[] =
190
#define nmsgs (sizeof (msgs) / sizeof (msgs[0]))
193
+/* Test for unbounded alloca. */
201
+ const int sz = 1024 * 1024;
203
+ getrlimit (RLIMIT_STACK, &rl);
205
+ setrlimit (RLIMIT_STACK, &rl);
207
+ buf = malloc (sz + 1);
208
+ memset (buf, 'A', sz);
210
+ setenv ("NLSPATH", buf, 1);
212
+ result = catopen (buf, NL_CAT_LOCALE);
213
+ assert (result == (nl_catd) -1);
222
@@ -62,5 +92,6 @@ main (void)
226
+ result += do_bz17905 ();