1*381ab8c9Schristos /* $NetBSD: t_exhaust.c,v 1.14 2021/06/09 21:09:20 christos Exp $ */
280ee4a02Schristos
380ee4a02Schristos /*-
480ee4a02Schristos * Copyright (c) 2011 The NetBSD Foundation, Inc.
580ee4a02Schristos * All rights reserved.
680ee4a02Schristos *
780ee4a02Schristos * This code is derived from software contributed to The NetBSD Foundation
880ee4a02Schristos * by Christos Zoulas.
980ee4a02Schristos *
1080ee4a02Schristos * Redistribution and use in source and binary forms, with or without
1180ee4a02Schristos * modification, are permitted provided that the following conditions
1280ee4a02Schristos * are met:
1380ee4a02Schristos * 1. Redistributions of source code must retain the above copyright
1480ee4a02Schristos * notice, this list of conditions and the following disclaimer.
1580ee4a02Schristos * 2. Redistributions in binary form must reproduce the above copyright
1680ee4a02Schristos * notice, this list of conditions and the following disclaimer in the
1780ee4a02Schristos * documentation and/or other materials provided with the distribution.
1880ee4a02Schristos * 3. All advertising materials mentioning features or use of this software
1980ee4a02Schristos * must display the following acknowledgement:
2080ee4a02Schristos * This product includes software developed by the NetBSD
2180ee4a02Schristos * Foundation, Inc. and its contributors.
2280ee4a02Schristos * 4. Neither the name of The NetBSD Foundation nor the names of its
2380ee4a02Schristos * contributors may be used to endorse or promote products derived
2480ee4a02Schristos * from this software without specific prior written permission.
2580ee4a02Schristos *
2680ee4a02Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2780ee4a02Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2880ee4a02Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2980ee4a02Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3080ee4a02Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3180ee4a02Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3280ee4a02Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3380ee4a02Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3480ee4a02Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3580ee4a02Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3680ee4a02Schristos * POSSIBILITY OF SUCH DAMAGE.
3780ee4a02Schristos */
3880ee4a02Schristos
3980ee4a02Schristos #include <sys/cdefs.h>
40*381ab8c9Schristos __RCSID("$NetBSD: t_exhaust.c,v 1.14 2021/06/09 21:09:20 christos Exp $");
4180ee4a02Schristos
426a94a076Schristos #include <sys/resource.h>
436a94a076Schristos #include <err.h>
44a7b6ad49Schristos
45a7b6ad49Schristos #ifdef TEST
46a7b6ad49Schristos # include <assert.h>
47a7b6ad49Schristos # define ATF_REQUIRE(a) assert(a)
48a7b6ad49Schristos # define ATF_REQUIRE_MSG(a, fmt, ...) \
49a7b6ad49Schristos if (!(a)) err(EXIT_FAILURE, fmt, __VA_ARGS__)
50a7b6ad49Schristos #else
51a7b6ad49Schristos # include <atf-c.h>
52a7b6ad49Schristos #endif
53a7b6ad49Schristos
546a94a076Schristos #include <regex.h>
556a94a076Schristos #include <stdio.h>
566a94a076Schristos #include <stdlib.h>
576a94a076Schristos #include <string.h>
5880ee4a02Schristos
5953528ec2Schristos #ifndef REGEX_MAXSIZE
6053528ec2Schristos #define REGEX_MAXSIZE 9999
6153528ec2Schristos #endif
6280ee4a02Schristos
63a7b6ad49Schristos #ifdef TRACE
64f4ff94b4Schristos
65f4ff94b4Schristos #include <dlfcn.h>
66a7b6ad49Schristos void *
malloc(size_t l)67a7b6ad49Schristos malloc(size_t l)
68a7b6ad49Schristos {
69a7b6ad49Schristos static void *(*m)(size_t);
70a7b6ad49Schristos static int q;
71a7b6ad49Schristos if (m == NULL) m = dlsym(RTLD_NEXT, "malloc");
72a7b6ad49Schristos void *p = (*m)(l);
73a7b6ad49Schristos if (q)
74a7b6ad49Schristos return p;
75a7b6ad49Schristos q = 1;
76a7b6ad49Schristos printf("%p m %zu\n", p, l);
77a7b6ad49Schristos q = 0;
78a7b6ad49Schristos return p;
79a7b6ad49Schristos }
80a7b6ad49Schristos
81a7b6ad49Schristos void
free(void * p)82a7b6ad49Schristos free(void *p)
83a7b6ad49Schristos {
84a7b6ad49Schristos static void (*f)(void *);
85a7b6ad49Schristos if (f == NULL) f = dlsym(RTLD_NEXT, "malloc");
86a7b6ad49Schristos printf("%p f\n", p);
87a7b6ad49Schristos (*f)(p);
88a7b6ad49Schristos }
89a7b6ad49Schristos #endif
90a7b6ad49Schristos
9180ee4a02Schristos static char *
mkstr(const char * str,size_t len)9280ee4a02Schristos mkstr(const char *str, size_t len)
9380ee4a02Schristos {
9480ee4a02Schristos size_t slen = strlen(str);
9580ee4a02Schristos char *p = malloc(slen * len + 1);
965686c770Schristos ATF_REQUIRE_MSG(p != NULL, "slen=%zu, len=%zu", slen, len);
9780ee4a02Schristos for (size_t i = 0; i < len; i++)
9880ee4a02Schristos strcpy(&p[i * slen], str);
9980ee4a02Schristos return p;
10080ee4a02Schristos }
10180ee4a02Schristos
10280ee4a02Schristos static char *
concat(const char * d,const char * s)10380ee4a02Schristos concat(const char *d, const char *s)
10480ee4a02Schristos {
10580ee4a02Schristos size_t dlen = strlen(d);
10680ee4a02Schristos size_t slen = strlen(s);
10780ee4a02Schristos char *p = malloc(dlen + slen + 1);
108fcc088cdSchristos
109fa566a79Schristos ATF_REQUIRE_MSG(p != NULL, "slen=%zu, dlen=%zu", slen, dlen);
11080ee4a02Schristos strcpy(p, d);
11180ee4a02Schristos strcpy(p + dlen, s);
11280ee4a02Schristos return p;
11380ee4a02Schristos }
11480ee4a02Schristos
11580ee4a02Schristos static char *
p0(size_t len)11680ee4a02Schristos p0(size_t len)
11780ee4a02Schristos {
11880ee4a02Schristos char *d, *s1, *s2;
11980ee4a02Schristos s1 = mkstr("\\(", len);
12080ee4a02Schristos s2 = concat(s1, ")");
12180ee4a02Schristos free(s1);
12280ee4a02Schristos d = concat("(", s2);
12380ee4a02Schristos free(s2);
12480ee4a02Schristos return d;
12580ee4a02Schristos }
12680ee4a02Schristos
12780ee4a02Schristos static char *
p1(size_t len)12880ee4a02Schristos p1(size_t len)
12980ee4a02Schristos {
13080ee4a02Schristos char *d, *s1, *s2, *s3;
13180ee4a02Schristos s1 = mkstr("\\(", 60);
13280ee4a02Schristos s2 = mkstr("(.*)", len);
13380ee4a02Schristos s3 = concat(s1, s2);
13480ee4a02Schristos free(s2);
13580ee4a02Schristos free(s1);
13680ee4a02Schristos s1 = concat(s3, ")");
13780ee4a02Schristos free(s3);
13880ee4a02Schristos d = concat("(", s1);
13980ee4a02Schristos free(s1);
14080ee4a02Schristos return d;
14180ee4a02Schristos }
14280ee4a02Schristos
14380ee4a02Schristos static char *
ps(const char * m,const char * s,size_t len)14480ee4a02Schristos ps(const char *m, const char *s, size_t len)
14580ee4a02Schristos {
14680ee4a02Schristos char *d, *s1, *s2, *s3;
14780ee4a02Schristos s1 = mkstr(m, len);
14880ee4a02Schristos s2 = mkstr(s, len);
14980ee4a02Schristos s3 = concat(s1, s2);
15080ee4a02Schristos free(s2);
15180ee4a02Schristos free(s1);
15280ee4a02Schristos d = concat("(.?)", s3);
15380ee4a02Schristos free(s3);
15480ee4a02Schristos return d;
15580ee4a02Schristos }
15680ee4a02Schristos
15780ee4a02Schristos static char *
p2(size_t len)15880ee4a02Schristos p2(size_t len)
15980ee4a02Schristos {
16080ee4a02Schristos return ps("((.*){0,255}", ")", len);
16180ee4a02Schristos }
16280ee4a02Schristos
16380ee4a02Schristos static char *
p3(size_t len)16480ee4a02Schristos p3(size_t len)
16580ee4a02Schristos {
16680ee4a02Schristos return ps("(.\\{0,}", ")", len);
16780ee4a02Schristos }
16880ee4a02Schristos
16980ee4a02Schristos static char *
p4(size_t len)17080ee4a02Schristos p4(size_t len)
17180ee4a02Schristos {
17280ee4a02Schristos return ps("((.*){1,255}", ")", len);
17380ee4a02Schristos }
17480ee4a02Schristos
17580ee4a02Schristos static char *
p5(size_t len)17680ee4a02Schristos p5(size_t len)
17780ee4a02Schristos {
17880ee4a02Schristos return ps("(", "){1,100}", len);
17980ee4a02Schristos }
18080ee4a02Schristos
18180ee4a02Schristos static char *
p6(size_t len)18280ee4a02Schristos p6(size_t len)
18380ee4a02Schristos {
18480ee4a02Schristos char *d, *s1, *s2;
18580ee4a02Schristos s1 = mkstr("(?:(.*)|", len);
18680ee4a02Schristos s2 = concat(s1, "(.*)");
18780ee4a02Schristos free(s1);
18880ee4a02Schristos s1 = mkstr(")", len);
18980ee4a02Schristos d = concat(s2, s1);
19080ee4a02Schristos free(s1);
19180ee4a02Schristos free(s2);
19280ee4a02Schristos return d;
19380ee4a02Schristos }
19480ee4a02Schristos
195fcc088cdSchristos static const struct {
196fcc088cdSchristos char *(*pattern)(size_t);
197fcc088cdSchristos int type;
198fcc088cdSchristos } tests[] = {
199fcc088cdSchristos { p0, REG_EXTENDED },
200fcc088cdSchristos { p1, REG_EXTENDED },
201fcc088cdSchristos { p2, REG_EXTENDED },
202fcc088cdSchristos { p3, REG_EXTENDED },
203fcc088cdSchristos { p4, REG_EXTENDED },
204fcc088cdSchristos { p5, REG_EXTENDED },
205fcc088cdSchristos { p6, REG_BASIC },
20680ee4a02Schristos };
20780ee4a02Schristos
208a7b6ad49Schristos static void
run(void)209a7b6ad49Schristos run(void)
210a7b6ad49Schristos {
211a7b6ad49Schristos regex_t re;
212a7b6ad49Schristos int e;
213a7b6ad49Schristos struct rlimit limit;
214a7b6ad49Schristos char *patterns[__arraycount(tests)];
215a7b6ad49Schristos
216a7b6ad49Schristos for (size_t i = 0; i < __arraycount(patterns); i++) {
217a7b6ad49Schristos patterns[i] = (*tests[i].pattern)(REGEX_MAXSIZE);
218a7b6ad49Schristos }
219a7b6ad49Schristos
220a7b6ad49Schristos limit.rlim_cur = limit.rlim_max = 256 * 1024 * 1024;
221a7b6ad49Schristos ATF_REQUIRE(setrlimit(RLIMIT_VMEM, &limit) != -1);
222a7b6ad49Schristos
223a7b6ad49Schristos for (size_t i = 0; i < __arraycount(tests); i++) {
224a7b6ad49Schristos e = regcomp(&re, patterns[i], tests[i].type);
225a7b6ad49Schristos if (e) {
226a7b6ad49Schristos char ebuf[1024];
227a7b6ad49Schristos (void)regerror(e, &re, ebuf, sizeof(ebuf));
228a7b6ad49Schristos ATF_REQUIRE_MSG(e == REG_ESPACE,
229a7b6ad49Schristos "regcomp returned %d (%s) for pattern %zu [%s]", e,
230a7b6ad49Schristos ebuf, i, patterns[i]);
231a7b6ad49Schristos continue;
232a7b6ad49Schristos }
233a7b6ad49Schristos (void)regexec(&re, "aaaaaaaaaaa", 0, NULL, 0);
234a7b6ad49Schristos regfree(&re);
235a7b6ad49Schristos }
236a7b6ad49Schristos for (size_t i = 0; i < __arraycount(patterns); i++) {
237a7b6ad49Schristos free(patterns[i]);
238a7b6ad49Schristos }
239a7b6ad49Schristos }
240a7b6ad49Schristos
241a7b6ad49Schristos #ifndef TEST
242a7b6ad49Schristos
24380ee4a02Schristos ATF_TC(regcomp_too_big);
24480ee4a02Schristos
ATF_TC_HEAD(regcomp_too_big,tc)24580ee4a02Schristos ATF_TC_HEAD(regcomp_too_big, tc)
24680ee4a02Schristos {
24780ee4a02Schristos
24880ee4a02Schristos atf_tc_set_md_var(tc, "descr", "Check that large patterns don't"
24980ee4a02Schristos " crash, but return a proper error code");
2505607c3d4Schristos // libtre needs it.
2515607c3d4Schristos atf_tc_set_md_var(tc, "timeout", "600");
2527753bf0bSgson atf_tc_set_md_var(tc, "require.memory", "256M");
25380ee4a02Schristos }
25480ee4a02Schristos
ATF_TC_BODY(regcomp_too_big,tc)25580ee4a02Schristos ATF_TC_BODY(regcomp_too_big, tc)
25680ee4a02Schristos {
257a7b6ad49Schristos run();
25880ee4a02Schristos }
25980ee4a02Schristos
ATF_TP_ADD_TCS(tp)26080ee4a02Schristos ATF_TP_ADD_TCS(tp)
26180ee4a02Schristos {
26280ee4a02Schristos
26380ee4a02Schristos ATF_TP_ADD_TC(tp, regcomp_too_big);
26480ee4a02Schristos return atf_no_error();
26580ee4a02Schristos }
266a7b6ad49Schristos #else
267a7b6ad49Schristos int
main(void)268a7b6ad49Schristos main(void)
269a7b6ad49Schristos {
270a7b6ad49Schristos run();
271a7b6ad49Schristos return 0;
272a7b6ad49Schristos }
273a7b6ad49Schristos #endif
274