t_exhaust.c revision 1.9 1 1.9 christos /* $NetBSD: t_exhaust.c,v 1.9 2019/03/16 21:57:15 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.9 christos __RCSID("$NetBSD: t_exhaust.c,v 1.9 2019/03/16 21:57:15 christos Exp $");
41 1.1 christos
42 1.8 christos #include <sys/resource.h>
43 1.8 christos #include <atf-c.h>
44 1.8 christos #include <err.h>
45 1.8 christos #include <regex.h>
46 1.1 christos #include <stdio.h>
47 1.8 christos #include <stdlib.h>
48 1.1 christos #include <string.h>
49 1.1 christos
50 1.4 christos #ifndef REGEX_MAXSIZE
51 1.4 christos #define REGEX_MAXSIZE 9999
52 1.4 christos #endif
53 1.1 christos
54 1.1 christos static char *
55 1.1 christos mkstr(const char *str, size_t len)
56 1.1 christos {
57 1.1 christos size_t slen = strlen(str);
58 1.1 christos char *p = malloc(slen * len + 1);
59 1.9 christos ATF_REQUIRE_MSG(p != NULL, "slen=%zu, len=%zu", slen, len);
60 1.1 christos for (size_t i = 0; i < len; i++)
61 1.1 christos strcpy(&p[i * slen], str);
62 1.1 christos return p;
63 1.1 christos }
64 1.1 christos
65 1.1 christos static char *
66 1.1 christos concat(const char *d, const char *s)
67 1.1 christos {
68 1.1 christos size_t dlen = strlen(d);
69 1.1 christos size_t slen = strlen(s);
70 1.1 christos char *p = malloc(dlen + slen + 1);
71 1.3 christos
72 1.3 christos ATF_REQUIRE(p != NULL);
73 1.1 christos strcpy(p, d);
74 1.1 christos strcpy(p + dlen, s);
75 1.1 christos return p;
76 1.1 christos }
77 1.1 christos
78 1.1 christos static char *
79 1.1 christos p0(size_t len)
80 1.1 christos {
81 1.1 christos char *d, *s1, *s2;
82 1.1 christos s1 = mkstr("\\(", len);
83 1.1 christos s2 = concat(s1, ")");
84 1.1 christos free(s1);
85 1.1 christos d = concat("(", s2);
86 1.1 christos free(s2);
87 1.1 christos return d;
88 1.1 christos }
89 1.1 christos
90 1.1 christos static char *
91 1.1 christos p1(size_t len)
92 1.1 christos {
93 1.1 christos char *d, *s1, *s2, *s3;
94 1.1 christos s1 = mkstr("\\(", 60);
95 1.1 christos s2 = mkstr("(.*)", len);
96 1.1 christos s3 = concat(s1, s2);
97 1.1 christos free(s2);
98 1.1 christos free(s1);
99 1.1 christos s1 = concat(s3, ")");
100 1.1 christos free(s3);
101 1.1 christos d = concat("(", s1);
102 1.1 christos free(s1);
103 1.1 christos return d;
104 1.1 christos }
105 1.1 christos
106 1.1 christos static char *
107 1.1 christos ps(const char *m, const char *s, size_t len)
108 1.1 christos {
109 1.1 christos char *d, *s1, *s2, *s3;
110 1.1 christos s1 = mkstr(m, len);
111 1.1 christos s2 = mkstr(s, len);
112 1.1 christos s3 = concat(s1, s2);
113 1.1 christos free(s2);
114 1.1 christos free(s1);
115 1.1 christos d = concat("(.?)", s3);
116 1.1 christos free(s3);
117 1.1 christos return d;
118 1.1 christos }
119 1.1 christos
120 1.1 christos static char *
121 1.1 christos p2(size_t len)
122 1.1 christos {
123 1.1 christos return ps("((.*){0,255}", ")", len);
124 1.1 christos }
125 1.1 christos
126 1.1 christos static char *
127 1.1 christos p3(size_t len)
128 1.1 christos {
129 1.1 christos return ps("(.\\{0,}", ")", len);
130 1.1 christos }
131 1.1 christos
132 1.1 christos static char *
133 1.1 christos p4(size_t len)
134 1.1 christos {
135 1.1 christos return ps("((.*){1,255}", ")", len);
136 1.1 christos }
137 1.1 christos
138 1.1 christos static char *
139 1.1 christos p5(size_t len)
140 1.1 christos {
141 1.1 christos return ps("(", "){1,100}", len);
142 1.1 christos }
143 1.1 christos
144 1.1 christos static char *
145 1.1 christos p6(size_t len)
146 1.1 christos {
147 1.1 christos char *d, *s1, *s2;
148 1.1 christos s1 = mkstr("(?:(.*)|", len);
149 1.1 christos s2 = concat(s1, "(.*)");
150 1.1 christos free(s1);
151 1.1 christos s1 = mkstr(")", len);
152 1.1 christos d = concat(s2, s1);
153 1.1 christos free(s1);
154 1.1 christos free(s2);
155 1.1 christos return d;
156 1.1 christos }
157 1.1 christos
158 1.3 christos static const struct {
159 1.3 christos char *(*pattern)(size_t);
160 1.3 christos int type;
161 1.3 christos } tests[] = {
162 1.3 christos { p0, REG_EXTENDED },
163 1.3 christos { p1, REG_EXTENDED },
164 1.3 christos { p2, REG_EXTENDED },
165 1.3 christos { p3, REG_EXTENDED },
166 1.3 christos { p4, REG_EXTENDED },
167 1.3 christos { p5, REG_EXTENDED },
168 1.3 christos { p6, REG_BASIC },
169 1.1 christos };
170 1.1 christos
171 1.1 christos ATF_TC(regcomp_too_big);
172 1.1 christos
173 1.1 christos ATF_TC_HEAD(regcomp_too_big, tc)
174 1.1 christos {
175 1.1 christos
176 1.1 christos atf_tc_set_md_var(tc, "descr", "Check that large patterns don't"
177 1.1 christos " crash, but return a proper error code");
178 1.5 christos // libtre needs it.
179 1.5 christos atf_tc_set_md_var(tc, "timeout", "600");
180 1.8 christos atf_tc_set_md_var(tc, "require.memory", "64M");
181 1.1 christos }
182 1.1 christos
183 1.1 christos ATF_TC_BODY(regcomp_too_big, tc)
184 1.1 christos {
185 1.1 christos regex_t re;
186 1.9 christos int e;
187 1.8 christos struct rlimit limit;
188 1.1 christos
189 1.9 christos limit.rlim_cur = limit.rlim_max = 256 * 1024 * 1024;
190 1.8 christos ATF_REQUIRE(setrlimit(RLIMIT_VMEM, &limit) != -1);
191 1.9 christos
192 1.3 christos for (size_t i = 0; i < __arraycount(tests); i++) {
193 1.4 christos char *d = (*tests[i].pattern)(REGEX_MAXSIZE);
194 1.3 christos e = regcomp(&re, d, tests[i].type);
195 1.2 christos if (e) {
196 1.4 christos char ebuf[1024];
197 1.4 christos (void)regerror(e, &re, ebuf, sizeof(ebuf));
198 1.1 christos ATF_REQUIRE_MSG(e == REG_ESPACE,
199 1.4 christos "regcomp returned %d (%s) for pattern %zu [%s]", e, ebuf,
200 1.4 christos i, d);
201 1.4 christos free(d);
202 1.1 christos continue;
203 1.2 christos }
204 1.4 christos free(d);
205 1.3 christos (void)regexec(&re, "aaaaaaaaaaa", 0, NULL, 0);
206 1.1 christos regfree(&re);
207 1.1 christos }
208 1.1 christos }
209 1.1 christos
210 1.1 christos ATF_TP_ADD_TCS(tp)
211 1.1 christos {
212 1.1 christos
213 1.1 christos ATF_TP_ADD_TC(tp, regcomp_too_big);
214 1.1 christos return atf_no_error();
215 1.1 christos }
216