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