t_exhaust.c revision 1.12 1 1.12 christos /* $NetBSD: t_exhaust.c,v 1.12 2021/06/09 20:48:37 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos #include <sys/cdefs.h>
40 1.12 christos __RCSID("$NetBSD: t_exhaust.c,v 1.12 2021/06/09 20:48:37 christos Exp $");
41 1.1 christos
42 1.8 christos #include <sys/resource.h>
43 1.8 christos #include <err.h>
44 1.12 christos
45 1.12 christos #ifdef TEST
46 1.12 christos # include <assert.h>
47 1.12 christos # define ATF_REQUIRE(a) assert(a)
48 1.12 christos # define ATF_REQUIRE_MSG(a, fmt, ...) \
49 1.12 christos if (!(a)) err(EXIT_FAILURE, fmt, __VA_ARGS__)
50 1.12 christos #else
51 1.12 christos # include <atf-c.h>
52 1.12 christos #endif
53 1.12 christos
54 1.8 christos #include <regex.h>
55 1.12 christos #include <dlfcn.h>
56 1.1 christos #include <stdio.h>
57 1.8 christos #include <stdlib.h>
58 1.1 christos #include <string.h>
59 1.12 christos #include <signal.h>
60 1.1 christos
61 1.4 christos #ifndef REGEX_MAXSIZE
62 1.4 christos #define REGEX_MAXSIZE 9999
63 1.4 christos #endif
64 1.1 christos
65 1.12 christos #ifdef TRACE
66 1.12 christos void *
67 1.12 christos malloc(size_t l)
68 1.12 christos {
69 1.12 christos static void *(*m)(size_t);
70 1.12 christos static int q;
71 1.12 christos if (m == NULL) m = dlsym(RTLD_NEXT, "malloc");
72 1.12 christos void *p = (*m)(l);
73 1.12 christos if (q)
74 1.12 christos return p;
75 1.12 christos q = 1;
76 1.12 christos printf("%p m %zu\n", p, l);
77 1.12 christos if (p == (void *)0x7f7ff7e21ac0)
78 1.12 christos kill(0, SIGSTOP);
79 1.12 christos q = 0;
80 1.12 christos return p;
81 1.12 christos }
82 1.12 christos
83 1.12 christos void
84 1.12 christos free(void *p)
85 1.12 christos {
86 1.12 christos static void (*f)(void *);
87 1.12 christos if (f == NULL) f = dlsym(RTLD_NEXT, "malloc");
88 1.12 christos printf("%p f\n", p);
89 1.12 christos (*f)(p);
90 1.12 christos }
91 1.12 christos #endif
92 1.12 christos
93 1.1 christos static char *
94 1.1 christos mkstr(const char *str, size_t len)
95 1.1 christos {
96 1.1 christos size_t slen = strlen(str);
97 1.1 christos char *p = malloc(slen * len + 1);
98 1.9 christos ATF_REQUIRE_MSG(p != NULL, "slen=%zu, len=%zu", slen, len);
99 1.1 christos for (size_t i = 0; i < len; i++)
100 1.1 christos strcpy(&p[i * slen], str);
101 1.1 christos return p;
102 1.1 christos }
103 1.1 christos
104 1.1 christos static char *
105 1.1 christos concat(const char *d, const char *s)
106 1.1 christos {
107 1.1 christos size_t dlen = strlen(d);
108 1.1 christos size_t slen = strlen(s);
109 1.1 christos char *p = malloc(dlen + slen + 1);
110 1.3 christos
111 1.11 christos ATF_REQUIRE_MSG(p != NULL, "slen=%zu, dlen=%zu", slen, dlen);
112 1.1 christos strcpy(p, d);
113 1.1 christos strcpy(p + dlen, s);
114 1.1 christos return p;
115 1.1 christos }
116 1.1 christos
117 1.1 christos static char *
118 1.1 christos p0(size_t len)
119 1.1 christos {
120 1.1 christos char *d, *s1, *s2;
121 1.1 christos s1 = mkstr("\\(", len);
122 1.1 christos s2 = concat(s1, ")");
123 1.1 christos free(s1);
124 1.1 christos d = concat("(", s2);
125 1.1 christos free(s2);
126 1.1 christos return d;
127 1.1 christos }
128 1.1 christos
129 1.1 christos static char *
130 1.1 christos p1(size_t len)
131 1.1 christos {
132 1.1 christos char *d, *s1, *s2, *s3;
133 1.1 christos s1 = mkstr("\\(", 60);
134 1.1 christos s2 = mkstr("(.*)", len);
135 1.1 christos s3 = concat(s1, s2);
136 1.1 christos free(s2);
137 1.1 christos free(s1);
138 1.1 christos s1 = concat(s3, ")");
139 1.1 christos free(s3);
140 1.1 christos d = concat("(", s1);
141 1.1 christos free(s1);
142 1.1 christos return d;
143 1.1 christos }
144 1.1 christos
145 1.1 christos static char *
146 1.1 christos ps(const char *m, const char *s, size_t len)
147 1.1 christos {
148 1.1 christos char *d, *s1, *s2, *s3;
149 1.1 christos s1 = mkstr(m, len);
150 1.1 christos s2 = mkstr(s, len);
151 1.1 christos s3 = concat(s1, s2);
152 1.1 christos free(s2);
153 1.1 christos free(s1);
154 1.1 christos d = concat("(.?)", s3);
155 1.1 christos free(s3);
156 1.1 christos return d;
157 1.1 christos }
158 1.1 christos
159 1.1 christos static char *
160 1.1 christos p2(size_t len)
161 1.1 christos {
162 1.1 christos return ps("((.*){0,255}", ")", len);
163 1.1 christos }
164 1.1 christos
165 1.1 christos static char *
166 1.1 christos p3(size_t len)
167 1.1 christos {
168 1.1 christos return ps("(.\\{0,}", ")", len);
169 1.1 christos }
170 1.1 christos
171 1.1 christos static char *
172 1.1 christos p4(size_t len)
173 1.1 christos {
174 1.1 christos return ps("((.*){1,255}", ")", len);
175 1.1 christos }
176 1.1 christos
177 1.1 christos static char *
178 1.1 christos p5(size_t len)
179 1.1 christos {
180 1.1 christos return ps("(", "){1,100}", len);
181 1.1 christos }
182 1.1 christos
183 1.1 christos static char *
184 1.1 christos p6(size_t len)
185 1.1 christos {
186 1.1 christos char *d, *s1, *s2;
187 1.1 christos s1 = mkstr("(?:(.*)|", len);
188 1.1 christos s2 = concat(s1, "(.*)");
189 1.1 christos free(s1);
190 1.1 christos s1 = mkstr(")", len);
191 1.1 christos d = concat(s2, s1);
192 1.1 christos free(s1);
193 1.1 christos free(s2);
194 1.1 christos return d;
195 1.1 christos }
196 1.1 christos
197 1.3 christos static const struct {
198 1.3 christos char *(*pattern)(size_t);
199 1.3 christos int type;
200 1.3 christos } tests[] = {
201 1.3 christos { p0, REG_EXTENDED },
202 1.3 christos { p1, REG_EXTENDED },
203 1.3 christos { p2, REG_EXTENDED },
204 1.3 christos { p3, REG_EXTENDED },
205 1.3 christos { p4, REG_EXTENDED },
206 1.3 christos { p5, REG_EXTENDED },
207 1.3 christos { p6, REG_BASIC },
208 1.1 christos };
209 1.1 christos
210 1.12 christos static void
211 1.12 christos run(void)
212 1.1 christos {
213 1.1 christos regex_t re;
214 1.9 christos int e;
215 1.8 christos struct rlimit limit;
216 1.12 christos char *patterns[__arraycount(tests)];
217 1.12 christos
218 1.12 christos for (size_t i = 0; i < __arraycount(patterns); i++) {
219 1.12 christos patterns[i] = (*tests[i].pattern)(REGEX_MAXSIZE);
220 1.12 christos }
221 1.1 christos
222 1.9 christos limit.rlim_cur = limit.rlim_max = 256 * 1024 * 1024;
223 1.8 christos ATF_REQUIRE(setrlimit(RLIMIT_VMEM, &limit) != -1);
224 1.9 christos
225 1.3 christos for (size_t i = 0; i < __arraycount(tests); i++) {
226 1.12 christos e = regcomp(&re, patterns[i], tests[i].type);
227 1.2 christos if (e) {
228 1.4 christos char ebuf[1024];
229 1.4 christos (void)regerror(e, &re, ebuf, sizeof(ebuf));
230 1.1 christos ATF_REQUIRE_MSG(e == REG_ESPACE,
231 1.12 christos "regcomp returned %d (%s) for pattern %zu [%s]", e,
232 1.12 christos ebuf, i, patterns[i]);
233 1.1 christos continue;
234 1.2 christos }
235 1.3 christos (void)regexec(&re, "aaaaaaaaaaa", 0, NULL, 0);
236 1.1 christos regfree(&re);
237 1.1 christos }
238 1.12 christos for (size_t i = 0; i < __arraycount(patterns); i++) {
239 1.12 christos free(patterns[i]);
240 1.12 christos }
241 1.12 christos }
242 1.12 christos
243 1.12 christos #ifndef TEST
244 1.12 christos
245 1.12 christos ATF_TC(regcomp_too_big);
246 1.12 christos
247 1.12 christos ATF_TC_HEAD(regcomp_too_big, tc)
248 1.12 christos {
249 1.12 christos
250 1.12 christos atf_tc_set_md_var(tc, "descr", "Check that large patterns don't"
251 1.12 christos " crash, but return a proper error code");
252 1.12 christos // libtre needs it.
253 1.12 christos atf_tc_set_md_var(tc, "timeout", "600");
254 1.12 christos atf_tc_set_md_var(tc, "require.memory", "256M");
255 1.12 christos }
256 1.12 christos
257 1.12 christos ATF_TC_BODY(regcomp_too_big, tc)
258 1.12 christos {
259 1.12 christos run();
260 1.1 christos }
261 1.1 christos
262 1.1 christos ATF_TP_ADD_TCS(tp)
263 1.1 christos {
264 1.1 christos
265 1.1 christos ATF_TP_ADD_TC(tp, regcomp_too_big);
266 1.1 christos return atf_no_error();
267 1.1 christos }
268 1.12 christos #else
269 1.12 christos int
270 1.12 christos main(void)
271 1.12 christos {
272 1.12 christos run();
273 1.12 christos return 0;
274 1.12 christos }
275 1.12 christos #endif
276