1 1.14 christos /* $NetBSD: t_exhaust.c,v 1.14 2021/06/09 21:09:20 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.14 christos __RCSID("$NetBSD: t_exhaust.c,v 1.14 2021/06/09 21:09:20 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.1 christos #include <stdio.h> 56 1.8 christos #include <stdlib.h> 57 1.1 christos #include <string.h> 58 1.1 christos 59 1.4 christos #ifndef REGEX_MAXSIZE 60 1.4 christos #define REGEX_MAXSIZE 9999 61 1.4 christos #endif 62 1.1 christos 63 1.12 christos #ifdef TRACE 64 1.13 christos 65 1.13 christos #include <dlfcn.h> 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 q = 0; 78 1.12 christos return p; 79 1.12 christos } 80 1.12 christos 81 1.12 christos void 82 1.12 christos free(void *p) 83 1.12 christos { 84 1.12 christos static void (*f)(void *); 85 1.12 christos if (f == NULL) f = dlsym(RTLD_NEXT, "malloc"); 86 1.12 christos printf("%p f\n", p); 87 1.12 christos (*f)(p); 88 1.12 christos } 89 1.12 christos #endif 90 1.12 christos 91 1.1 christos static char * 92 1.1 christos mkstr(const char *str, size_t len) 93 1.1 christos { 94 1.1 christos size_t slen = strlen(str); 95 1.1 christos char *p = malloc(slen * len + 1); 96 1.9 christos ATF_REQUIRE_MSG(p != NULL, "slen=%zu, len=%zu", slen, len); 97 1.1 christos for (size_t i = 0; i < len; i++) 98 1.1 christos strcpy(&p[i * slen], str); 99 1.1 christos return p; 100 1.1 christos } 101 1.1 christos 102 1.1 christos static char * 103 1.1 christos concat(const char *d, const char *s) 104 1.1 christos { 105 1.1 christos size_t dlen = strlen(d); 106 1.1 christos size_t slen = strlen(s); 107 1.1 christos char *p = malloc(dlen + slen + 1); 108 1.3 christos 109 1.11 christos ATF_REQUIRE_MSG(p != NULL, "slen=%zu, dlen=%zu", slen, dlen); 110 1.1 christos strcpy(p, d); 111 1.1 christos strcpy(p + dlen, s); 112 1.1 christos return p; 113 1.1 christos } 114 1.1 christos 115 1.1 christos static char * 116 1.1 christos p0(size_t len) 117 1.1 christos { 118 1.1 christos char *d, *s1, *s2; 119 1.1 christos s1 = mkstr("\\(", len); 120 1.1 christos s2 = concat(s1, ")"); 121 1.1 christos free(s1); 122 1.1 christos d = concat("(", s2); 123 1.1 christos free(s2); 124 1.1 christos return d; 125 1.1 christos } 126 1.1 christos 127 1.1 christos static char * 128 1.1 christos p1(size_t len) 129 1.1 christos { 130 1.1 christos char *d, *s1, *s2, *s3; 131 1.1 christos s1 = mkstr("\\(", 60); 132 1.1 christos s2 = mkstr("(.*)", len); 133 1.1 christos s3 = concat(s1, s2); 134 1.1 christos free(s2); 135 1.1 christos free(s1); 136 1.1 christos s1 = concat(s3, ")"); 137 1.1 christos free(s3); 138 1.1 christos d = concat("(", s1); 139 1.1 christos free(s1); 140 1.1 christos return d; 141 1.1 christos } 142 1.1 christos 143 1.1 christos static char * 144 1.1 christos ps(const char *m, const char *s, size_t len) 145 1.1 christos { 146 1.1 christos char *d, *s1, *s2, *s3; 147 1.1 christos s1 = mkstr(m, len); 148 1.1 christos s2 = mkstr(s, len); 149 1.1 christos s3 = concat(s1, s2); 150 1.1 christos free(s2); 151 1.1 christos free(s1); 152 1.1 christos d = concat("(.?)", s3); 153 1.1 christos free(s3); 154 1.1 christos return d; 155 1.1 christos } 156 1.1 christos 157 1.1 christos static char * 158 1.1 christos p2(size_t len) 159 1.1 christos { 160 1.1 christos return ps("((.*){0,255}", ")", len); 161 1.1 christos } 162 1.1 christos 163 1.1 christos static char * 164 1.1 christos p3(size_t len) 165 1.1 christos { 166 1.1 christos return ps("(.\\{0,}", ")", len); 167 1.1 christos } 168 1.1 christos 169 1.1 christos static char * 170 1.1 christos p4(size_t len) 171 1.1 christos { 172 1.1 christos return ps("((.*){1,255}", ")", len); 173 1.1 christos } 174 1.1 christos 175 1.1 christos static char * 176 1.1 christos p5(size_t len) 177 1.1 christos { 178 1.1 christos return ps("(", "){1,100}", len); 179 1.1 christos } 180 1.1 christos 181 1.1 christos static char * 182 1.1 christos p6(size_t len) 183 1.1 christos { 184 1.1 christos char *d, *s1, *s2; 185 1.1 christos s1 = mkstr("(?:(.*)|", len); 186 1.1 christos s2 = concat(s1, "(.*)"); 187 1.1 christos free(s1); 188 1.1 christos s1 = mkstr(")", len); 189 1.1 christos d = concat(s2, s1); 190 1.1 christos free(s1); 191 1.1 christos free(s2); 192 1.1 christos return d; 193 1.1 christos } 194 1.1 christos 195 1.3 christos static const struct { 196 1.3 christos char *(*pattern)(size_t); 197 1.3 christos int type; 198 1.3 christos } tests[] = { 199 1.3 christos { p0, REG_EXTENDED }, 200 1.3 christos { p1, REG_EXTENDED }, 201 1.3 christos { p2, REG_EXTENDED }, 202 1.3 christos { p3, REG_EXTENDED }, 203 1.3 christos { p4, REG_EXTENDED }, 204 1.3 christos { p5, REG_EXTENDED }, 205 1.3 christos { p6, REG_BASIC }, 206 1.1 christos }; 207 1.1 christos 208 1.12 christos static void 209 1.12 christos run(void) 210 1.1 christos { 211 1.1 christos regex_t re; 212 1.9 christos int e; 213 1.8 christos struct rlimit limit; 214 1.12 christos char *patterns[__arraycount(tests)]; 215 1.12 christos 216 1.12 christos for (size_t i = 0; i < __arraycount(patterns); i++) { 217 1.12 christos patterns[i] = (*tests[i].pattern)(REGEX_MAXSIZE); 218 1.12 christos } 219 1.1 christos 220 1.9 christos limit.rlim_cur = limit.rlim_max = 256 * 1024 * 1024; 221 1.8 christos ATF_REQUIRE(setrlimit(RLIMIT_VMEM, &limit) != -1); 222 1.9 christos 223 1.3 christos for (size_t i = 0; i < __arraycount(tests); i++) { 224 1.12 christos e = regcomp(&re, patterns[i], tests[i].type); 225 1.2 christos if (e) { 226 1.4 christos char ebuf[1024]; 227 1.4 christos (void)regerror(e, &re, ebuf, sizeof(ebuf)); 228 1.1 christos ATF_REQUIRE_MSG(e == REG_ESPACE, 229 1.12 christos "regcomp returned %d (%s) for pattern %zu [%s]", e, 230 1.12 christos ebuf, i, patterns[i]); 231 1.1 christos continue; 232 1.2 christos } 233 1.3 christos (void)regexec(&re, "aaaaaaaaaaa", 0, NULL, 0); 234 1.1 christos regfree(&re); 235 1.1 christos } 236 1.12 christos for (size_t i = 0; i < __arraycount(patterns); i++) { 237 1.12 christos free(patterns[i]); 238 1.12 christos } 239 1.12 christos } 240 1.12 christos 241 1.12 christos #ifndef TEST 242 1.12 christos 243 1.12 christos ATF_TC(regcomp_too_big); 244 1.12 christos 245 1.12 christos ATF_TC_HEAD(regcomp_too_big, tc) 246 1.12 christos { 247 1.12 christos 248 1.12 christos atf_tc_set_md_var(tc, "descr", "Check that large patterns don't" 249 1.12 christos " crash, but return a proper error code"); 250 1.12 christos // libtre needs it. 251 1.12 christos atf_tc_set_md_var(tc, "timeout", "600"); 252 1.12 christos atf_tc_set_md_var(tc, "require.memory", "256M"); 253 1.12 christos } 254 1.12 christos 255 1.12 christos ATF_TC_BODY(regcomp_too_big, tc) 256 1.12 christos { 257 1.12 christos run(); 258 1.1 christos } 259 1.1 christos 260 1.1 christos ATF_TP_ADD_TCS(tp) 261 1.1 christos { 262 1.1 christos 263 1.1 christos ATF_TP_ADD_TC(tp, regcomp_too_big); 264 1.1 christos return atf_no_error(); 265 1.1 christos } 266 1.12 christos #else 267 1.12 christos int 268 1.12 christos main(void) 269 1.12 christos { 270 1.12 christos run(); 271 1.12 christos return 0; 272 1.12 christos } 273 1.12 christos #endif 274