| 1 | /* |
|---|
| 2 | * zzuf - general purpose fuzzer |
|---|
| 3 | * Copyright (c) 2002, 2007 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This program is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * main.c: main program |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| 20 | |
|---|
| 21 | #if defined HAVE_STDINT_H |
|---|
| 22 | # include <stdint.h> |
|---|
| 23 | #elif defined HAVE_INTTYPES_H |
|---|
| 24 | # include <inttypes.h> |
|---|
| 25 | #endif |
|---|
| 26 | #if defined HAVE_GETOPT_H |
|---|
| 27 | # include <getopt.h> |
|---|
| 28 | #endif |
|---|
| 29 | #include <stdio.h> |
|---|
| 30 | #include <stdlib.h> |
|---|
| 31 | #if defined HAVE_UNISTD_H |
|---|
| 32 | # include <unistd.h> |
|---|
| 33 | #endif |
|---|
| 34 | #if defined HAVE_REGEX_H |
|---|
| 35 | # include <regex.h> |
|---|
| 36 | #endif |
|---|
| 37 | #if defined HAVE_WINDOWS_H |
|---|
| 38 | # include <windows.h> |
|---|
| 39 | #endif |
|---|
| 40 | #if defined HAVE_WINSOCK2_H |
|---|
| 41 | # include <winsock2.h> |
|---|
| 42 | #endif |
|---|
| 43 | #if defined HAVE_IO_H |
|---|
| 44 | # include <io.h> |
|---|
| 45 | #endif |
|---|
| 46 | #include <string.h> |
|---|
| 47 | #include <fcntl.h> |
|---|
| 48 | #include <errno.h> |
|---|
| 49 | #include <signal.h> |
|---|
| 50 | #if defined HAVE_SYS_WAIT_H |
|---|
| 51 | # include <sys/wait.h> |
|---|
| 52 | #endif |
|---|
| 53 | #if defined HAVE_SYS_RESOURCE_H |
|---|
| 54 | # include <sys/resource.h> |
|---|
| 55 | #endif |
|---|
| 56 | |
|---|
| 57 | #include "libzzuf.h" |
|---|
| 58 | #include "opts.h" |
|---|
| 59 | #include "random.h" |
|---|
| 60 | #include "fd.h" |
|---|
| 61 | #include "fuzz.h" |
|---|
| 62 | #include "md5.h" |
|---|
| 63 | #include "timer.h" |
|---|
| 64 | |
|---|
| 65 | #if !defined SIGKILL |
|---|
| 66 | # define SIGKILL 9 |
|---|
| 67 | #endif |
|---|
| 68 | |
|---|
| 69 | /* We use file descriptor 17 as the debug channel */ |
|---|
| 70 | #define DEBUG_FILENO 17 |
|---|
| 71 | #define DEBUG_FILENO_STR "17" |
|---|
| 72 | |
|---|
| 73 | static void loop_stdin(struct opts *); |
|---|
| 74 | static int run_process(char const *, char *[]); |
|---|
| 75 | |
|---|
| 76 | static void spawn_children(struct opts *); |
|---|
| 77 | static void clean_children(struct opts *); |
|---|
| 78 | static void read_children(struct opts *); |
|---|
| 79 | |
|---|
| 80 | #if !defined HAVE_SETENV |
|---|
| 81 | static void setenv(char const *, char const *, int); |
|---|
| 82 | #endif |
|---|
| 83 | #if defined HAVE_WAITPID |
|---|
| 84 | static char const *sig2str(int); |
|---|
| 85 | #endif |
|---|
| 86 | #if defined HAVE_WINDOWS_H |
|---|
| 87 | static int dll_inject(void *, void *); |
|---|
| 88 | static void *get_entry(char const *); |
|---|
| 89 | #endif |
|---|
| 90 | static void finfo(FILE *, struct opts *, uint32_t); |
|---|
| 91 | #if defined HAVE_REGEX_H |
|---|
| 92 | static char *merge_regex(char *, char *); |
|---|
| 93 | static char *merge_file(char *, char *); |
|---|
| 94 | #endif |
|---|
| 95 | static void version(void); |
|---|
| 96 | #if defined HAVE_GETOPT_H |
|---|
| 97 | static void usage(void); |
|---|
| 98 | #endif |
|---|
| 99 | |
|---|
| 100 | #if defined HAVE_WINDOWS_H |
|---|
| 101 | static inline void addcpy(void *buf, void *x) |
|---|
| 102 | { |
|---|
| 103 | memcpy(buf, &x, 4); |
|---|
| 104 | } |
|---|
| 105 | #endif |
|---|
| 106 | |
|---|
| 107 | #define ZZUF_FD_SET(fd, p_fdset, maxfd) \ |
|---|
| 108 | if(fd >= 0) \ |
|---|
| 109 | { \ |
|---|
| 110 | FD_SET((unsigned int)fd, p_fdset); \ |
|---|
| 111 | if(fd > maxfd) \ |
|---|
| 112 | maxfd = fd; \ |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | #define ZZUF_FD_ISSET(fd, p_fdset) \ |
|---|
| 116 | ((fd >= 0) && (FD_ISSET(fd, p_fdset))) |
|---|
| 117 | |
|---|
| 118 | int main(int argc, char *argv[]) |
|---|
| 119 | { |
|---|
| 120 | struct opts _opts, *opts = &_opts; |
|---|
| 121 | char *tmp; |
|---|
| 122 | #if defined HAVE_REGEX_H |
|---|
| 123 | char *include = NULL, *exclude = NULL; |
|---|
| 124 | int cmdline = 0; |
|---|
| 125 | #endif |
|---|
| 126 | int i; |
|---|
| 127 | |
|---|
| 128 | _zz_opts_init(opts); |
|---|
| 129 | |
|---|
| 130 | #if defined HAVE_GETOPT_H |
|---|
| 131 | for(;;) |
|---|
| 132 | { |
|---|
| 133 | # if defined HAVE_REGEX_H |
|---|
| 134 | # define OPTSTR "Ab:B:cC:dD:E:f:F:iI:mM:nP:qr:R:s:ST:vxhV" |
|---|
| 135 | # else |
|---|
| 136 | # define OPTSTR "Ab:B:C:dD:f:F:imM:nP:qr:R:s:ST:vxhV" |
|---|
| 137 | # endif |
|---|
| 138 | # if defined HAVE_GETOPT_LONG |
|---|
| 139 | # define MOREINFO "Try `%s --help' for more information.\n" |
|---|
| 140 | int option_index = 0; |
|---|
| 141 | static struct option long_options[] = |
|---|
| 142 | { |
|---|
| 143 | /* Long option, needs arg, flag, short option */ |
|---|
| 144 | { "autoinc", 0, NULL, 'A' }, |
|---|
| 145 | { "bytes", 1, NULL, 'b' }, |
|---|
| 146 | { "max-bytes", 1, NULL, 'B' }, |
|---|
| 147 | #if defined HAVE_REGEX_H |
|---|
| 148 | { "cmdline", 0, NULL, 'c' }, |
|---|
| 149 | #endif |
|---|
| 150 | { "max-crashes", 1, NULL, 'C' }, |
|---|
| 151 | { "debug", 0, NULL, 'd' }, |
|---|
| 152 | { "delay", 1, NULL, 'D' }, |
|---|
| 153 | #if defined HAVE_REGEX_H |
|---|
| 154 | { "exclude", 1, NULL, 'E' }, |
|---|
| 155 | #endif |
|---|
| 156 | { "fuzzing", 1, NULL, 'f' }, |
|---|
| 157 | { "max-forks", 1, NULL, 'F' }, |
|---|
| 158 | { "stdin", 0, NULL, 'i' }, |
|---|
| 159 | #if defined HAVE_REGEX_H |
|---|
| 160 | { "include", 1, NULL, 'I' }, |
|---|
| 161 | #endif |
|---|
| 162 | { "md5", 0, NULL, 'm' }, |
|---|
| 163 | { "max-memory", 1, NULL, 'M' }, |
|---|
| 164 | { "network", 0, NULL, 'n' }, |
|---|
| 165 | { "protect", 1, NULL, 'P' }, |
|---|
| 166 | { "quiet", 0, NULL, 'q' }, |
|---|
| 167 | { "ratio", 1, NULL, 'r' }, |
|---|
| 168 | { "refuse", 1, NULL, 'R' }, |
|---|
| 169 | { "seed", 1, NULL, 's' }, |
|---|
| 170 | { "signal", 0, NULL, 'S' }, |
|---|
| 171 | { "max-time", 1, NULL, 'T' }, |
|---|
| 172 | { "verbose", 0, NULL, 'v' }, |
|---|
| 173 | { "check-exit", 0, NULL, 'x' }, |
|---|
| 174 | { "help", 0, NULL, 'h' }, |
|---|
| 175 | { "version", 0, NULL, 'V' }, |
|---|
| 176 | { NULL, 0, NULL, 0 } |
|---|
| 177 | }; |
|---|
| 178 | int c = getopt_long(argc, argv, OPTSTR, long_options, &option_index); |
|---|
| 179 | # else |
|---|
| 180 | # define MOREINFO "Try `%s -h' for more information.\n" |
|---|
| 181 | int c = getopt(argc, argv, OPTSTR); |
|---|
| 182 | # endif |
|---|
| 183 | if(c == -1) |
|---|
| 184 | break; |
|---|
| 185 | |
|---|
| 186 | switch(c) |
|---|
| 187 | { |
|---|
| 188 | case 'A': /* --autoinc */ |
|---|
| 189 | setenv("ZZUF_AUTOINC", "1", 1); |
|---|
| 190 | break; |
|---|
| 191 | case 'b': /* --bytes */ |
|---|
| 192 | opts->bytes = optarg; |
|---|
| 193 | break; |
|---|
| 194 | case 'B': /* --max-bytes */ |
|---|
| 195 | opts->maxbytes = atoi(optarg); |
|---|
| 196 | break; |
|---|
| 197 | #if defined HAVE_REGEX_H |
|---|
| 198 | case 'c': /* --cmdline */ |
|---|
| 199 | cmdline = 1; |
|---|
| 200 | break; |
|---|
| 201 | #endif |
|---|
| 202 | case 'C': /* --max-crashes */ |
|---|
| 203 | opts->maxcrashes = atoi(optarg); |
|---|
| 204 | if(opts->maxcrashes <= 0) |
|---|
| 205 | opts->maxcrashes = 0; |
|---|
| 206 | break; |
|---|
| 207 | case 'd': /* --debug */ |
|---|
| 208 | setenv("ZZUF_DEBUG", DEBUG_FILENO_STR, 1); |
|---|
| 209 | break; |
|---|
| 210 | case 'D': /* --delay */ |
|---|
| 211 | opts->delay = (int64_t)(atof(optarg) * 1000000.0); |
|---|
| 212 | break; |
|---|
| 213 | #if defined HAVE_REGEX_H |
|---|
| 214 | case 'E': /* --exclude */ |
|---|
| 215 | exclude = merge_regex(exclude, optarg); |
|---|
| 216 | if(!exclude) |
|---|
| 217 | { |
|---|
| 218 | printf("%s: invalid regex -- `%s'\n", argv[0], optarg); |
|---|
| 219 | _zz_opts_fini(opts); |
|---|
| 220 | return EXIT_FAILURE; |
|---|
| 221 | } |
|---|
| 222 | break; |
|---|
| 223 | #endif |
|---|
| 224 | case 'f': /* --fuzzing */ |
|---|
| 225 | opts->fuzzing = optarg; |
|---|
| 226 | break; |
|---|
| 227 | case 'F': /* --max-forks */ |
|---|
| 228 | opts->maxchild = atoi(optarg) > 1 ? atoi(optarg) : 1; |
|---|
| 229 | break; |
|---|
| 230 | case 'i': /* --stdin */ |
|---|
| 231 | setenv("ZZUF_STDIN", "1", 1); |
|---|
| 232 | break; |
|---|
| 233 | #if defined HAVE_REGEX_H |
|---|
| 234 | case 'I': /* --include */ |
|---|
| 235 | include = merge_regex(include, optarg); |
|---|
| 236 | if(!include) |
|---|
| 237 | { |
|---|
| 238 | printf("%s: invalid regex -- `%s'\n", argv[0], optarg); |
|---|
| 239 | _zz_opts_fini(opts); |
|---|
| 240 | return EXIT_FAILURE; |
|---|
| 241 | } |
|---|
| 242 | break; |
|---|
| 243 | #endif |
|---|
| 244 | case 'm': /* --md5 */ |
|---|
| 245 | opts->md5 = 1; |
|---|
| 246 | break; |
|---|
| 247 | #if defined HAVE_SETRLIMIT |
|---|
| 248 | case 'M': /* --max-memory */ |
|---|
| 249 | setenv("ZZUF_MEMORY", "1", 1); |
|---|
| 250 | opts->maxmem = atoi(optarg); |
|---|
| 251 | break; |
|---|
| 252 | #endif |
|---|
| 253 | case 'n': /* --network */ |
|---|
| 254 | setenv("ZZUF_NETWORK", "1", 1); |
|---|
| 255 | break; |
|---|
| 256 | case 'P': /* --protect */ |
|---|
| 257 | opts->protect = optarg; |
|---|
| 258 | break; |
|---|
| 259 | case 'q': /* --quiet */ |
|---|
| 260 | opts->quiet = 1; |
|---|
| 261 | break; |
|---|
| 262 | case 'r': /* --ratio */ |
|---|
| 263 | tmp = strchr(optarg, ':'); |
|---|
| 264 | opts->minratio = atof(optarg); |
|---|
| 265 | opts->maxratio = tmp ? atof(tmp + 1) : opts->minratio; |
|---|
| 266 | break; |
|---|
| 267 | case 'R': /* --refuse */ |
|---|
| 268 | opts->refuse = optarg; |
|---|
| 269 | break; |
|---|
| 270 | case 's': /* --seed */ |
|---|
| 271 | tmp = strchr(optarg, ':'); |
|---|
| 272 | opts->seed = atol(optarg); |
|---|
| 273 | opts->endseed = tmp ? (uint32_t)atoi(tmp + 1) : opts->seed + 1; |
|---|
| 274 | break; |
|---|
| 275 | case 'S': /* --signal */ |
|---|
| 276 | setenv("ZZUF_SIGNAL", "1", 1); |
|---|
| 277 | break; |
|---|
| 278 | case 'T': /* --max-time */ |
|---|
| 279 | opts->maxtime = (int64_t)(atof(optarg) * 1000000.0); |
|---|
| 280 | break; |
|---|
| 281 | case 'x': /* --check-exit */ |
|---|
| 282 | opts->checkexit = 1; |
|---|
| 283 | break; |
|---|
| 284 | case 'v': /* --verbose */ |
|---|
| 285 | opts->verbose = 1; |
|---|
| 286 | break; |
|---|
| 287 | case 'h': /* --help */ |
|---|
| 288 | usage(); |
|---|
| 289 | _zz_opts_fini(opts); |
|---|
| 290 | return 0; |
|---|
| 291 | case 'V': /* --version */ |
|---|
| 292 | version(); |
|---|
| 293 | _zz_opts_fini(opts); |
|---|
| 294 | return 0; |
|---|
| 295 | default: |
|---|
| 296 | printf("%s: invalid option -- %c\n", argv[0], c); |
|---|
| 297 | printf(MOREINFO, argv[0]); |
|---|
| 298 | _zz_opts_fini(opts); |
|---|
| 299 | return EXIT_FAILURE; |
|---|
| 300 | } |
|---|
| 301 | } |
|---|
| 302 | #else |
|---|
| 303 | # define MOREINFO "Usage: %s message...\n" |
|---|
| 304 | int optind = 1; |
|---|
| 305 | #endif |
|---|
| 306 | |
|---|
| 307 | _zz_setratio(opts->minratio, opts->maxratio); |
|---|
| 308 | _zz_setseed(opts->seed); |
|---|
| 309 | |
|---|
| 310 | /* If asked to read from the standard input */ |
|---|
| 311 | if(optind >= argc) |
|---|
| 312 | { |
|---|
| 313 | if(opts->endseed != opts->seed + 1) |
|---|
| 314 | { |
|---|
| 315 | printf("%s: seed ranges are incompatible with stdin fuzzing\n", |
|---|
| 316 | argv[0]); |
|---|
| 317 | printf(MOREINFO, argv[0]); |
|---|
| 318 | _zz_opts_fini(opts); |
|---|
| 319 | return EXIT_FAILURE; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | loop_stdin(opts); |
|---|
| 323 | |
|---|
| 324 | _zz_opts_fini(opts); |
|---|
| 325 | return EXIT_SUCCESS; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | /* If asked to launch programs */ |
|---|
| 329 | #if defined HAVE_REGEX_H |
|---|
| 330 | if(cmdline) |
|---|
| 331 | { |
|---|
| 332 | int dashdash = 0; |
|---|
| 333 | |
|---|
| 334 | for(i = optind + 1; i < argc; i++) |
|---|
| 335 | { |
|---|
| 336 | if(dashdash) |
|---|
| 337 | include = merge_file(include, argv[i]); |
|---|
| 338 | else if(!strcmp("--", argv[i])) |
|---|
| 339 | dashdash = 1; |
|---|
| 340 | else if(argv[i][0] != '-') |
|---|
| 341 | include = merge_file(include, argv[i]); |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | if(include) |
|---|
| 346 | setenv("ZZUF_INCLUDE", include, 1); |
|---|
| 347 | if(exclude) |
|---|
| 348 | setenv("ZZUF_EXCLUDE", exclude, 1); |
|---|
| 349 | #endif |
|---|
| 350 | |
|---|
| 351 | if(opts->fuzzing) |
|---|
| 352 | setenv("ZZUF_FUZZING", opts->fuzzing, 1); |
|---|
| 353 | if(opts->bytes) |
|---|
| 354 | setenv("ZZUF_BYTES", opts->bytes, 1); |
|---|
| 355 | if(opts->protect) |
|---|
| 356 | setenv("ZZUF_PROTECT", opts->protect, 1); |
|---|
| 357 | if(opts->refuse) |
|---|
| 358 | setenv("ZZUF_REFUSE", opts->refuse, 1); |
|---|
| 359 | |
|---|
| 360 | /* Allocate memory for children handling */ |
|---|
| 361 | opts->child = malloc(opts->maxchild * sizeof(struct child)); |
|---|
| 362 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 363 | opts->child[i].status = STATUS_FREE; |
|---|
| 364 | opts->nchild = 0; |
|---|
| 365 | |
|---|
| 366 | /* Create new argv */ |
|---|
| 367 | opts->oldargv = argv; |
|---|
| 368 | opts->newargv = malloc((argc - optind + 1) * sizeof(char *)); |
|---|
| 369 | memcpy(opts->newargv, argv + optind, (argc - optind) * sizeof(char *)); |
|---|
| 370 | opts->newargv[argc - optind] = (char *)NULL; |
|---|
| 371 | |
|---|
| 372 | /* Main loop */ |
|---|
| 373 | while(opts->nchild || opts->seed < opts->endseed) |
|---|
| 374 | { |
|---|
| 375 | /* Spawn new children, if necessary */ |
|---|
| 376 | spawn_children(opts); |
|---|
| 377 | |
|---|
| 378 | /* Cleanup dead or dying children */ |
|---|
| 379 | clean_children(opts); |
|---|
| 380 | |
|---|
| 381 | /* Read data from children */ |
|---|
| 382 | read_children(opts); |
|---|
| 383 | |
|---|
| 384 | if(opts->maxcrashes && opts->crashes >= opts->maxcrashes |
|---|
| 385 | && opts->nchild == 0) |
|---|
| 386 | break; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | /* Clean up */ |
|---|
| 390 | _zz_opts_fini(opts); |
|---|
| 391 | |
|---|
| 392 | return opts->crashes ? EXIT_FAILURE : EXIT_SUCCESS; |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 395 | static void loop_stdin(struct opts *opts) |
|---|
| 396 | { |
|---|
| 397 | uint8_t md5sum[16]; |
|---|
| 398 | struct md5 *ctx = NULL; |
|---|
| 399 | |
|---|
| 400 | if(opts->md5) |
|---|
| 401 | ctx = _zz_md5_init(); |
|---|
| 402 | |
|---|
| 403 | if(opts->fuzzing) |
|---|
| 404 | _zz_fuzzing(opts->fuzzing); |
|---|
| 405 | if(opts->bytes) |
|---|
| 406 | _zz_bytes(opts->bytes); |
|---|
| 407 | if(opts->protect) |
|---|
| 408 | _zz_protect(opts->protect); |
|---|
| 409 | if(opts->refuse) |
|---|
| 410 | _zz_refuse(opts->refuse); |
|---|
| 411 | |
|---|
| 412 | _zz_fd_init(); |
|---|
| 413 | _zz_register(0); |
|---|
| 414 | |
|---|
| 415 | for(;;) |
|---|
| 416 | { |
|---|
| 417 | uint8_t buf[BUFSIZ]; |
|---|
| 418 | int ret, off = 0, nw = 0; |
|---|
| 419 | |
|---|
| 420 | ret = read(0, buf, BUFSIZ); |
|---|
| 421 | if(ret <= 0) |
|---|
| 422 | break; |
|---|
| 423 | |
|---|
| 424 | _zz_fuzz(0, buf, ret); |
|---|
| 425 | _zz_addpos(0, ret); |
|---|
| 426 | |
|---|
| 427 | if(opts->md5) |
|---|
| 428 | _zz_md5_add(ctx, buf, ret); |
|---|
| 429 | else while(ret) |
|---|
| 430 | { |
|---|
| 431 | if((nw = write(1, buf + off, (size_t)ret)) < 0) |
|---|
| 432 | break; |
|---|
| 433 | ret -= nw; |
|---|
| 434 | off += nw; |
|---|
| 435 | } |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | if(opts->md5) |
|---|
| 439 | { |
|---|
| 440 | _zz_md5_fini(md5sum, ctx); |
|---|
| 441 | finfo(stdout, opts, opts->seed); |
|---|
| 442 | fprintf(stdout, "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x" |
|---|
| 443 | "%.02x%.02x%.02x%.02x%.02x%.02x\n", md5sum[0], md5sum[1], |
|---|
| 444 | md5sum[2], md5sum[3], md5sum[4], md5sum[5], md5sum[6], |
|---|
| 445 | md5sum[7], md5sum[8], md5sum[9], md5sum[10], md5sum[11], |
|---|
| 446 | md5sum[12], md5sum[13], md5sum[14], md5sum[15]); |
|---|
| 447 | fflush(stdout); |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | _zz_unregister(0); |
|---|
| 451 | _zz_fd_fini(); |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | static void finfo(FILE *fp, struct opts *opts, uint32_t seed) |
|---|
| 455 | { |
|---|
| 456 | if(opts->minratio == opts->maxratio) |
|---|
| 457 | fprintf(fp, "zzuf[s=%i,r=%g]: ", seed, opts->minratio); |
|---|
| 458 | else |
|---|
| 459 | fprintf(fp, "zzuf[s=%i,r=%g:%g]: ", seed, |
|---|
| 460 | opts->minratio, opts->maxratio); |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | #if defined HAVE_REGEX_H |
|---|
| 464 | static char *merge_file(char *regex, char *file) |
|---|
| 465 | { |
|---|
| 466 | char *newfile = malloc(5 + 2 * strlen(file) + 1 + 1), *tmp = newfile; |
|---|
| 467 | |
|---|
| 468 | *tmp++ = '('; |
|---|
| 469 | *tmp++ = '^'; |
|---|
| 470 | *tmp++ = '|'; |
|---|
| 471 | *tmp++ = '/'; |
|---|
| 472 | *tmp++ = ')'; |
|---|
| 473 | while(*file) |
|---|
| 474 | { |
|---|
| 475 | if(strchr("^.[$()|*+?{\\", *file)) |
|---|
| 476 | *tmp++ = '\\'; |
|---|
| 477 | *tmp++ = *file++; |
|---|
| 478 | } |
|---|
| 479 | *tmp++ = '$'; |
|---|
| 480 | *tmp++ = '\0'; |
|---|
| 481 | |
|---|
| 482 | tmp = merge_regex(regex, newfile); |
|---|
| 483 | free(newfile); |
|---|
| 484 | return tmp; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | static char *merge_regex(char *regex, char *string) |
|---|
| 488 | { |
|---|
| 489 | regex_t optre; |
|---|
| 490 | |
|---|
| 491 | if(regex) |
|---|
| 492 | { |
|---|
| 493 | regex = realloc(regex, strlen(regex) + strlen(string) + 1 + 1); |
|---|
| 494 | sprintf(regex + strlen(regex) - 1, "|%s)", string); |
|---|
| 495 | } |
|---|
| 496 | else |
|---|
| 497 | { |
|---|
| 498 | regex = malloc(1 + strlen(string) + 1 + 1); |
|---|
| 499 | sprintf(regex, "(%s)", string); |
|---|
| 500 | } |
|---|
| 501 | |
|---|
| 502 | if(regcomp(&optre, regex, REG_EXTENDED) != 0) |
|---|
| 503 | { |
|---|
| 504 | free(regex); |
|---|
| 505 | return NULL; |
|---|
| 506 | } |
|---|
| 507 | regfree(&optre); |
|---|
| 508 | |
|---|
| 509 | return regex; |
|---|
| 510 | } |
|---|
| 511 | #endif |
|---|
| 512 | |
|---|
| 513 | static void spawn_children(struct opts *opts) |
|---|
| 514 | { |
|---|
| 515 | static int const files[] = { DEBUG_FILENO, STDERR_FILENO, STDOUT_FILENO }; |
|---|
| 516 | char buf[64]; |
|---|
| 517 | int fd[3][2]; |
|---|
| 518 | int64_t now = _zz_time(); |
|---|
| 519 | pid_t pid; |
|---|
| 520 | int i, j; |
|---|
| 521 | |
|---|
| 522 | if(opts->nchild == opts->maxchild) |
|---|
| 523 | return; /* no slot */ |
|---|
| 524 | |
|---|
| 525 | if(opts->seed == opts->endseed) |
|---|
| 526 | return; /* job finished */ |
|---|
| 527 | |
|---|
| 528 | if(opts->maxcrashes && opts->crashes >= opts->maxcrashes) |
|---|
| 529 | return; /* all jobs crashed */ |
|---|
| 530 | |
|---|
| 531 | if(opts->delay > 0 && opts->lastlaunch + opts->delay > now) |
|---|
| 532 | return; /* too early */ |
|---|
| 533 | |
|---|
| 534 | /* Find the empty slot */ |
|---|
| 535 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 536 | if(opts->child[i].status == STATUS_FREE) |
|---|
| 537 | break; |
|---|
| 538 | |
|---|
| 539 | /* Prepare communication pipe */ |
|---|
| 540 | for(j = 0; j < 3; j++) |
|---|
| 541 | { |
|---|
| 542 | int ret; |
|---|
| 543 | #if defined HAVE_PIPE |
|---|
| 544 | ret = pipe(fd[j]); |
|---|
| 545 | #elif defined HAVE__PIPE |
|---|
| 546 | ret = _pipe(fd[j], 256, _O_BINARY); |
|---|
| 547 | #endif |
|---|
| 548 | if(ret < 0) |
|---|
| 549 | { |
|---|
| 550 | perror("pipe"); |
|---|
| 551 | return; |
|---|
| 552 | } |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | #if defined HAVE_FORK |
|---|
| 556 | /* Fork and launch child */ |
|---|
| 557 | pid = fork(); |
|---|
| 558 | if(pid < -1) |
|---|
| 559 | { |
|---|
| 560 | perror("fork"); |
|---|
| 561 | return; |
|---|
| 562 | } |
|---|
| 563 | #else |
|---|
| 564 | pid = 0; |
|---|
| 565 | #endif |
|---|
| 566 | |
|---|
| 567 | if(pid == 0) |
|---|
| 568 | { |
|---|
| 569 | #if defined HAVE_SETRLIMIT |
|---|
| 570 | if(opts->maxmem >= 0) |
|---|
| 571 | { |
|---|
| 572 | struct rlimit rlim; |
|---|
| 573 | rlim.rlim_cur = opts->maxmem * 1000000; |
|---|
| 574 | rlim.rlim_max = opts->maxmem * 1000000; |
|---|
| 575 | setrlimit(RLIMIT_AS, &rlim); |
|---|
| 576 | } |
|---|
| 577 | #endif |
|---|
| 578 | |
|---|
| 579 | #if defined HAVE_FORK |
|---|
| 580 | /* We loop in reverse order so that files[0] is done last, |
|---|
| 581 | * just in case one of the other dup2()ed fds had the value */ |
|---|
| 582 | for(j = 3; j--; ) |
|---|
| 583 | { |
|---|
| 584 | close(fd[j][0]); |
|---|
| 585 | if(fd[j][1] != files[j]) |
|---|
| 586 | { |
|---|
| 587 | dup2(fd[j][1], files[j]); |
|---|
| 588 | close(fd[j][1]); |
|---|
| 589 | } |
|---|
| 590 | } |
|---|
| 591 | #endif |
|---|
| 592 | |
|---|
| 593 | /* Set environment variables */ |
|---|
| 594 | sprintf(buf, "%i", opts->seed); |
|---|
| 595 | setenv("ZZUF_SEED", buf, 1); |
|---|
| 596 | sprintf(buf, "%g", opts->minratio); |
|---|
| 597 | setenv("ZZUF_MINRATIO", buf, 1); |
|---|
| 598 | sprintf(buf, "%g", opts->maxratio); |
|---|
| 599 | setenv("ZZUF_MAXRATIO", buf, 1); |
|---|
| 600 | |
|---|
| 601 | #if defined HAVE_FORK |
|---|
| 602 | if(run_process(opts->oldargv[0], opts->newargv) < 0) |
|---|
| 603 | exit(EXIT_FAILURE); |
|---|
| 604 | exit(EXIT_SUCCESS); |
|---|
| 605 | #else |
|---|
| 606 | if(run_process(opts->oldargv[0], opts->newargv) < 0) |
|---|
| 607 | return; |
|---|
| 608 | #endif |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | /* We’re the parent, acknowledge spawn */ |
|---|
| 612 | opts->child[i].date = now; |
|---|
| 613 | opts->child[i].pid = pid; |
|---|
| 614 | for(j = 0; j < 3; j++) |
|---|
| 615 | { |
|---|
| 616 | close(fd[j][1]); |
|---|
| 617 | opts->child[i].fd[j] = fd[j][0]; |
|---|
| 618 | } |
|---|
| 619 | opts->child[i].bytes = 0; |
|---|
| 620 | opts->child[i].seed = opts->seed; |
|---|
| 621 | opts->child[i].ratio = _zz_getratio(); |
|---|
| 622 | opts->child[i].status = STATUS_RUNNING; |
|---|
| 623 | if(opts->md5) |
|---|
| 624 | opts->child[i].ctx = _zz_md5_init(); |
|---|
| 625 | |
|---|
| 626 | if(opts->verbose) |
|---|
| 627 | { |
|---|
| 628 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 629 | fprintf(stderr, "launched %s\n", opts->newargv[0]); |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | opts->lastlaunch = now; |
|---|
| 633 | opts->nchild++; |
|---|
| 634 | opts->seed++; |
|---|
| 635 | |
|---|
| 636 | _zz_setseed(opts->seed); |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | static void clean_children(struct opts *opts) |
|---|
| 640 | { |
|---|
| 641 | #if defined HAVE_KILL |
|---|
| 642 | int64_t now = _zz_time(); |
|---|
| 643 | #endif |
|---|
| 644 | int i, j; |
|---|
| 645 | |
|---|
| 646 | #if defined HAVE_KILL |
|---|
| 647 | /* Terminate children if necessary */ |
|---|
| 648 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 649 | { |
|---|
| 650 | if(opts->child[i].status == STATUS_RUNNING |
|---|
| 651 | && opts->maxbytes >= 0 |
|---|
| 652 | && opts->child[i].bytes > opts->maxbytes) |
|---|
| 653 | { |
|---|
| 654 | if(opts->verbose) |
|---|
| 655 | { |
|---|
| 656 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 657 | fprintf(stderr, "data output exceeded, sending SIGTERM\n"); |
|---|
| 658 | } |
|---|
| 659 | kill(opts->child[i].pid, SIGTERM); |
|---|
| 660 | opts->child[i].date = now; |
|---|
| 661 | opts->child[i].status = STATUS_SIGTERM; |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | if(opts->child[i].status == STATUS_RUNNING |
|---|
| 665 | && opts->maxtime >= 0 |
|---|
| 666 | && now > opts->child[i].date + opts->maxtime) |
|---|
| 667 | { |
|---|
| 668 | if(opts->verbose) |
|---|
| 669 | { |
|---|
| 670 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 671 | fprintf(stderr, "running time exceeded, sending SIGTERM\n"); |
|---|
| 672 | } |
|---|
| 673 | kill(opts->child[i].pid, SIGTERM); |
|---|
| 674 | opts->child[i].date = now; |
|---|
| 675 | opts->child[i].status = STATUS_SIGTERM; |
|---|
| 676 | } |
|---|
| 677 | } |
|---|
| 678 | |
|---|
| 679 | /* Kill children if necessary (still there after 2 seconds) */ |
|---|
| 680 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 681 | { |
|---|
| 682 | if(opts->child[i].status == STATUS_SIGTERM |
|---|
| 683 | && now > opts->child[i].date + 2000000) |
|---|
| 684 | { |
|---|
| 685 | if(opts->verbose) |
|---|
| 686 | { |
|---|
| 687 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 688 | fprintf(stderr, "not responding, sending SIGKILL\n"); |
|---|
| 689 | } |
|---|
| 690 | kill(opts->child[i].pid, SIGKILL); |
|---|
| 691 | opts->child[i].status = STATUS_SIGKILL; |
|---|
| 692 | } |
|---|
| 693 | } |
|---|
| 694 | #endif |
|---|
| 695 | |
|---|
| 696 | /* Collect dead children */ |
|---|
| 697 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 698 | { |
|---|
| 699 | uint8_t md5sum[16]; |
|---|
| 700 | #if defined HAVE_WAITPID |
|---|
| 701 | int status; |
|---|
| 702 | pid_t pid; |
|---|
| 703 | #endif |
|---|
| 704 | |
|---|
| 705 | if(opts->child[i].status != STATUS_SIGKILL |
|---|
| 706 | && opts->child[i].status != STATUS_SIGTERM |
|---|
| 707 | && opts->child[i].status != STATUS_EOF) |
|---|
| 708 | continue; |
|---|
| 709 | |
|---|
| 710 | #if defined HAVE_WAITPID |
|---|
| 711 | pid = waitpid(opts->child[i].pid, &status, WNOHANG); |
|---|
| 712 | if(pid <= 0) |
|---|
| 713 | continue; |
|---|
| 714 | |
|---|
| 715 | if(opts->checkexit && WIFEXITED(status) && WEXITSTATUS(status)) |
|---|
| 716 | { |
|---|
| 717 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 718 | fprintf(stderr, "exit %i\n", WEXITSTATUS(status)); |
|---|
| 719 | opts->crashes++; |
|---|
| 720 | } |
|---|
| 721 | else if(WIFSIGNALED(status) |
|---|
| 722 | && !(WTERMSIG(status) == SIGTERM |
|---|
| 723 | && opts->child[i].status == STATUS_SIGTERM)) |
|---|
| 724 | { |
|---|
| 725 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 726 | fprintf(stderr, "signal %i%s%s\n", |
|---|
| 727 | WTERMSIG(status), sig2str(WTERMSIG(status)), |
|---|
| 728 | (WTERMSIG(status) == SIGKILL && opts->maxmem >= 0) ? |
|---|
| 729 | " (memory exceeded?)" : ""); |
|---|
| 730 | opts->crashes++; |
|---|
| 731 | } |
|---|
| 732 | #endif |
|---|
| 733 | |
|---|
| 734 | for(j = 0; j < 3; j++) |
|---|
| 735 | if(opts->child[i].fd[j] >= 0) |
|---|
| 736 | close(opts->child[i].fd[j]); |
|---|
| 737 | |
|---|
| 738 | if(opts->md5) |
|---|
| 739 | { |
|---|
| 740 | _zz_md5_fini(md5sum, opts->child[i].ctx); |
|---|
| 741 | finfo(stdout, opts, opts->child[i].seed); |
|---|
| 742 | fprintf(stdout, "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x" |
|---|
| 743 | "%.02x%.02x%.02x%.02x%.02x%.02x%.02x\n", md5sum[0], |
|---|
| 744 | md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], |
|---|
| 745 | md5sum[6], md5sum[7], md5sum[8], md5sum[9], md5sum[10], |
|---|
| 746 | md5sum[11], md5sum[12], md5sum[13], md5sum[14], md5sum[15]); |
|---|
| 747 | fflush(stdout); |
|---|
| 748 | } |
|---|
| 749 | opts->child[i].status = STATUS_FREE; |
|---|
| 750 | opts->nchild--; |
|---|
| 751 | } |
|---|
| 752 | } |
|---|
| 753 | |
|---|
| 754 | static void read_children(struct opts *opts) |
|---|
| 755 | { |
|---|
| 756 | struct timeval tv; |
|---|
| 757 | fd_set fdset; |
|---|
| 758 | int i, j, ret, maxfd = 0; |
|---|
| 759 | |
|---|
| 760 | /* Read data from all sockets */ |
|---|
| 761 | FD_ZERO(&fdset); |
|---|
| 762 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 763 | { |
|---|
| 764 | if(opts->child[i].status != STATUS_RUNNING) |
|---|
| 765 | continue; |
|---|
| 766 | |
|---|
| 767 | for(j = 0; j < 3; j++) |
|---|
| 768 | ZZUF_FD_SET(opts->child[i].fd[j], &fdset, maxfd); |
|---|
| 769 | } |
|---|
| 770 | tv.tv_sec = 0; |
|---|
| 771 | tv.tv_usec = 1000; |
|---|
| 772 | |
|---|
| 773 | ret = select(maxfd + 1, &fdset, NULL, NULL, &tv); |
|---|
| 774 | if(ret < 0 && errno) |
|---|
| 775 | perror("select"); |
|---|
| 776 | if(ret <= 0) |
|---|
| 777 | return; |
|---|
| 778 | |
|---|
| 779 | /* XXX: cute (i, j) iterating hack */ |
|---|
| 780 | for(i = 0, j = 0; i < opts->maxchild; i += (j == 2), j = (j + 1) % 3) |
|---|
| 781 | { |
|---|
| 782 | uint8_t buf[BUFSIZ]; |
|---|
| 783 | |
|---|
| 784 | if(opts->child[i].status != STATUS_RUNNING) |
|---|
| 785 | continue; |
|---|
| 786 | |
|---|
| 787 | if(!ZZUF_FD_ISSET(opts->child[i].fd[j], &fdset)) |
|---|
| 788 | continue; |
|---|
| 789 | |
|---|
| 790 | ret = read(opts->child[i].fd[j], buf, BUFSIZ - 1); |
|---|
| 791 | if(ret > 0) |
|---|
| 792 | { |
|---|
| 793 | /* We got data */ |
|---|
| 794 | if(j != 0) |
|---|
| 795 | opts->child[i].bytes += ret; |
|---|
| 796 | |
|---|
| 797 | if(opts->md5 && j == 2) |
|---|
| 798 | _zz_md5_add(opts->child[i].ctx, buf, ret); |
|---|
| 799 | else if(!opts->quiet || j == 0) |
|---|
| 800 | write((j < 2) ? STDERR_FILENO : STDOUT_FILENO, buf, ret); |
|---|
| 801 | } |
|---|
| 802 | else if(ret == 0) |
|---|
| 803 | { |
|---|
| 804 | /* End of file reached */ |
|---|
| 805 | close(opts->child[i].fd[j]); |
|---|
| 806 | opts->child[i].fd[j] = -1; |
|---|
| 807 | |
|---|
| 808 | if(opts->child[i].fd[0] == -1 |
|---|
| 809 | && opts->child[i].fd[1] == -1 |
|---|
| 810 | && opts->child[i].fd[2] == -1) |
|---|
| 811 | opts->child[i].status = STATUS_EOF; |
|---|
| 812 | } |
|---|
| 813 | } |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | #if !defined HAVE_SETENV |
|---|
| 817 | static void setenv(char const *name, char const *value, int overwrite) |
|---|
| 818 | { |
|---|
| 819 | char *str; |
|---|
| 820 | |
|---|
| 821 | if(!overwrite && getenv(name)) |
|---|
| 822 | return; |
|---|
| 823 | |
|---|
| 824 | str = malloc(strlen(name) + 1 + strlen(value) + 1); |
|---|
| 825 | sprintf(str, "%s=%s", name, value); |
|---|
| 826 | putenv(str); |
|---|
| 827 | } |
|---|
| 828 | #endif |
|---|
| 829 | |
|---|
| 830 | #if defined HAVE_WAITPID |
|---|
| 831 | static char const *sig2str(int signum) |
|---|
| 832 | { |
|---|
| 833 | switch(signum) |
|---|
| 834 | { |
|---|
| 835 | case SIGABRT: return " (SIGABRT)"; |
|---|
| 836 | case SIGFPE: return " (SIGFPE)"; |
|---|
| 837 | case SIGILL: return " (SIGILL)"; |
|---|
| 838 | #ifdef SIGQUIT |
|---|
| 839 | case SIGQUIT: return " (SIGQUIT)"; |
|---|
| 840 | #endif |
|---|
| 841 | case SIGSEGV: return " (SIGSEGV)"; |
|---|
| 842 | #ifdef SIGTRAP |
|---|
| 843 | case SIGTRAP: return " (SIGTRAP)"; |
|---|
| 844 | #endif |
|---|
| 845 | #ifdef SIGSYS |
|---|
| 846 | case SIGSYS: return " (SIGSYS)"; |
|---|
| 847 | #endif |
|---|
| 848 | #ifdef SIGEMT |
|---|
| 849 | case SIGEMT: return " (SIGEMT)"; |
|---|
| 850 | #endif |
|---|
| 851 | #ifdef SIGBUS |
|---|
| 852 | case SIGBUS: return " (SIGBUS)"; |
|---|
| 853 | #endif |
|---|
| 854 | #ifdef SIGXCPU |
|---|
| 855 | case SIGXCPU: return " (SIGXCPU)"; |
|---|
| 856 | #endif |
|---|
| 857 | #ifdef SIGXFSZ |
|---|
| 858 | case SIGXFSZ: return " (SIGXFSZ)"; |
|---|
| 859 | #endif |
|---|
| 860 | } |
|---|
| 861 | |
|---|
| 862 | return ""; |
|---|
| 863 | } |
|---|
| 864 | #endif |
|---|
| 865 | |
|---|
| 866 | static int run_process(char const *zzuf_exe, char *argv[]) |
|---|
| 867 | { |
|---|
| 868 | #if defined HAVE_FORK |
|---|
| 869 | char *libpath, *tmp; |
|---|
| 870 | int ret, len = strlen(zzuf_exe); |
|---|
| 871 | # if defined __APPLE__ |
|---|
| 872 | # define FILENAME "libzzuf.dylib" |
|---|
| 873 | # define EXTRAINFO "" |
|---|
| 874 | # define PRELOAD "DYLD_INSERT_LIBRARIES" |
|---|
| 875 | setenv("DYLD_FORCE_FLAT_NAMESPACE", "1", 1); |
|---|
| 876 | # elif defined __osf__ |
|---|
| 877 | # define FILENAME "libzzuf.so" |
|---|
| 878 | # define EXTRAINFO ":DEFAULT" |
|---|
| 879 | # define PRELOAD "_RLD_LIST" |
|---|
| 880 | # else |
|---|
| 881 | # define FILENAME "libzzuf.so" |
|---|
| 882 | # define EXTRAINFO "" |
|---|
| 883 | # define PRELOAD "LD_PRELOAD" |
|---|
| 884 | # endif |
|---|
| 885 | |
|---|
| 886 | libpath = malloc(len + strlen("/.libs/" FILENAME EXTRAINFO) + 1); |
|---|
| 887 | strcpy(libpath, zzuf_exe); |
|---|
| 888 | |
|---|
| 889 | tmp = strrchr(libpath, '/'); |
|---|
| 890 | strcpy(tmp ? tmp + 1 : libpath, ".libs/" FILENAME); |
|---|
| 891 | ret = access(libpath, R_OK); |
|---|
| 892 | |
|---|
| 893 | strcpy(tmp ? tmp + 1 : libpath, ".libs/" FILENAME EXTRAINFO); |
|---|
| 894 | if(ret == 0) |
|---|
| 895 | setenv(PRELOAD, libpath, 1); |
|---|
| 896 | else |
|---|
| 897 | setenv(PRELOAD, LIBDIR "/" FILENAME EXTRAINFO, 1); |
|---|
| 898 | free(libpath); |
|---|
| 899 | |
|---|
| 900 | if(execvp(argv[0], argv)) |
|---|
| 901 | { |
|---|
| 902 | perror(argv[0]); |
|---|
| 903 | return -1; |
|---|
| 904 | } |
|---|
| 905 | |
|---|
| 906 | return 0; |
|---|
| 907 | #elif HAVE_WINDOWS_H |
|---|
| 908 | PROCESS_INFORMATION pinfo; |
|---|
| 909 | STARTUPINFO sinfo; |
|---|
| 910 | void *epaddr; |
|---|
| 911 | int ret; |
|---|
| 912 | |
|---|
| 913 | /* Get entry point */ |
|---|
| 914 | epaddr = get_entry(argv[0]); |
|---|
| 915 | if(!epaddr) |
|---|
| 916 | return -1; |
|---|
| 917 | |
|---|
| 918 | memset(&sinfo, 0, sizeof(sinfo)); |
|---|
| 919 | sinfo.cb = sizeof(sinfo); |
|---|
| 920 | ret = CreateProcess(NULL, argv[0], NULL, NULL, FALSE, |
|---|
| 921 | CREATE_SUSPENDED, NULL, NULL, &sinfo, &pinfo); |
|---|
| 922 | if(!ret) |
|---|
| 923 | return -1; |
|---|
| 924 | |
|---|
| 925 | /* Insert the replacement code */ |
|---|
| 926 | ret = dll_inject(pinfo.hProcess, epaddr); |
|---|
| 927 | if(ret < 0) |
|---|
| 928 | { |
|---|
| 929 | TerminateProcess(pinfo.hProcess, -1); |
|---|
| 930 | return -1; |
|---|
| 931 | } |
|---|
| 932 | |
|---|
| 933 | ret = ResumeThread(pinfo.hThread); |
|---|
| 934 | if(ret < 0) |
|---|
| 935 | { |
|---|
| 936 | TerminateProcess(pinfo.hProcess, -1); |
|---|
| 937 | return -1; |
|---|
| 938 | } |
|---|
| 939 | |
|---|
| 940 | return 0; |
|---|
| 941 | #else |
|---|
| 942 | return -1; |
|---|
| 943 | #endif |
|---|
| 944 | } |
|---|
| 945 | |
|---|
| 946 | #if defined HAVE_WINDOWS_H |
|---|
| 947 | static int dll_inject(void *process, void *epaddr) |
|---|
| 948 | { |
|---|
| 949 | uint8_t old_ep[7]; |
|---|
| 950 | uint8_t new_ep[] = "\xb8<01>\xff\xe0"; |
|---|
| 951 | uint8_t loader[] = "libzzuf.dll\0<0000c>\xb8<14>\x50\xb8<1a>\xff\xd0" |
|---|
| 952 | "\xb8\0\0\0\0\x50\xb8\x07\x00\x00\x00\x50\xb8<2d>" |
|---|
| 953 | "\x50\xb8<33>\x50\xb8<39>\xff\xd0\x50\xb8<41>\xff" |
|---|
| 954 | "\xd0\xb8<48>\xff\xe0"; |
|---|
| 955 | void *lib; |
|---|
| 956 | uint8_t *loaderaddr; |
|---|
| 957 | DWORD tmp; |
|---|
| 958 | |
|---|
| 959 | /* Save the old entry-point code */ |
|---|
| 960 | ReadProcessMemory(process, epaddr, old_ep, 7, &tmp); |
|---|
| 961 | if(tmp != 7) |
|---|
| 962 | return -1; |
|---|
| 963 | |
|---|
| 964 | loaderaddr = VirtualAllocEx(process, NULL, 78, MEM_COMMIT, |
|---|
| 965 | PAGE_EXECUTE_READWRITE); |
|---|
| 966 | if(!loaderaddr) |
|---|
| 967 | return -1; |
|---|
| 968 | |
|---|
| 969 | addcpy(new_ep + 0x01, loaderaddr + 0x0c + 7); |
|---|
| 970 | WriteProcessMemory(process, epaddr, new_ep, 7, &tmp); |
|---|
| 971 | if(tmp != 7) |
|---|
| 972 | return -1; |
|---|
| 973 | |
|---|
| 974 | lib = LoadLibrary("kernel32.dll"); |
|---|
| 975 | if(!lib) |
|---|
| 976 | return -1; |
|---|
| 977 | |
|---|
| 978 | memcpy(loader + 0x0c, old_ep, 7); |
|---|
| 979 | addcpy(loader + 0x14, loaderaddr + 0x00); /* offset for dll string */ |
|---|
| 980 | addcpy(loader + 0x1a, GetProcAddress(lib, "LoadLibraryA")); |
|---|
| 981 | addcpy(loader + 0x2d, loaderaddr + 0x0c); |
|---|
| 982 | addcpy(loader + 0x33, epaddr); |
|---|
| 983 | addcpy(loader + 0x39, GetProcAddress(lib, "GetCurrentProcess")); |
|---|
| 984 | addcpy(loader + 0x41, GetProcAddress(lib, "WriteProcessMemory")); |
|---|
| 985 | addcpy(loader + 0x48, epaddr); |
|---|
| 986 | FreeLibrary(lib); |
|---|
| 987 | |
|---|
| 988 | WriteProcessMemory(process, loaderaddr, loader, 78, &tmp); |
|---|
| 989 | if(tmp != 78) |
|---|
| 990 | return -1; |
|---|
| 991 | |
|---|
| 992 | return 0; |
|---|
| 993 | } |
|---|
| 994 | |
|---|
| 995 | static void *get_entry(char const *name) |
|---|
| 996 | { |
|---|
| 997 | PIMAGE_DOS_HEADER dos; |
|---|
| 998 | PIMAGE_NT_HEADERS nt; |
|---|
| 999 | void *file, *map, *base; |
|---|
| 1000 | |
|---|
| 1001 | file = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, |
|---|
| 1002 | NULL, OPEN_EXISTING, 0, NULL); |
|---|
| 1003 | if(file == INVALID_HANDLE_VALUE) |
|---|
| 1004 | return NULL; |
|---|
| 1005 | |
|---|
| 1006 | map = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL); |
|---|
| 1007 | if(!map) |
|---|
| 1008 | { |
|---|
| 1009 | CloseHandle(file); |
|---|
| 1010 | return NULL; |
|---|
| 1011 | } |
|---|
| 1012 | |
|---|
| 1013 | base = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0); |
|---|
| 1014 | if(!base) |
|---|
| 1015 | { |
|---|
| 1016 | CloseHandle(map); |
|---|
| 1017 | CloseHandle(file); |
|---|
| 1018 | return NULL; |
|---|
| 1019 | } |
|---|
| 1020 | |
|---|
| 1021 | /* Sanity checks */ |
|---|
| 1022 | dos = (PIMAGE_DOS_HEADER)base; |
|---|
| 1023 | nt = (PIMAGE_NT_HEADERS)((char *)base + dos->e_lfanew); |
|---|
| 1024 | if(dos->e_magic != IMAGE_DOS_SIGNATURE |
|---|
| 1025 | || nt->Signature != IMAGE_NT_SIGNATURE |
|---|
| 1026 | || nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386 |
|---|
| 1027 | || nt->OptionalHeader.Magic != 0x10b /* IMAGE_NT_OPTIONAL_HDR32_MAGIC */) |
|---|
| 1028 | { |
|---|
| 1029 | UnmapViewOfFile(base); |
|---|
| 1030 | CloseHandle(map); |
|---|
| 1031 | CloseHandle(file); |
|---|
| 1032 | return NULL; |
|---|
| 1033 | } |
|---|
| 1034 | |
|---|
| 1035 | return (char *)nt->OptionalHeader.ImageBase + |
|---|
| 1036 | nt->OptionalHeader.AddressOfEntryPoint; |
|---|
| 1037 | } |
|---|
| 1038 | #endif |
|---|
| 1039 | |
|---|
| 1040 | static void version(void) |
|---|
| 1041 | { |
|---|
| 1042 | printf("zzuf %s\n", VERSION); |
|---|
| 1043 | printf("Copyright (C) 2002, 2007 Sam Hocevar <sam@zoy.org>\n"); |
|---|
| 1044 | printf("This is free software. You may redistribute copies of it under the\n"); |
|---|
| 1045 | printf("terms of the Do What The Fuck You Want To Public License, Version 2\n"); |
|---|
| 1046 | printf("<http://sam.zoy.org/wtfpl/>.\n"); |
|---|
| 1047 | printf("There is NO WARRANTY, to the extent permitted by law.\n"); |
|---|
| 1048 | printf("\n"); |
|---|
| 1049 | printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n"); |
|---|
| 1050 | } |
|---|
| 1051 | |
|---|
| 1052 | #if defined HAVE_GETOPT_H |
|---|
| 1053 | static void usage(void) |
|---|
| 1054 | { |
|---|
| 1055 | #if defined HAVE_REGEX_H |
|---|
| 1056 | printf("Usage: zzuf [-AcdimnqSvx] [-s seed|-s start:stop] [-r ratio|-r min:max]\n"); |
|---|
| 1057 | #else |
|---|
| 1058 | printf("Usage: zzuf [-AdimnqSvx] [-s seed|-s start:stop] [-r ratio|-r min:max]\n"); |
|---|
| 1059 | #endif |
|---|
| 1060 | printf(" [-f fuzzing] [-D delay] [-F forks] [-C crashes] [-B bytes]\n"); |
|---|
| 1061 | printf(" [-T seconds] [-M bytes] [-b ranges] [-P protect] [-R refuse]\n"); |
|---|
| 1062 | #if defined HAVE_REGEX_H |
|---|
| 1063 | printf(" [-I include] [-E exclude] [PROGRAM [--] [ARGS]...]\n"); |
|---|
| 1064 | #else |
|---|
| 1065 | printf(" [PROGRAM [--] [ARGS]...]\n"); |
|---|
| 1066 | #endif |
|---|
| 1067 | # if defined HAVE_GETOPT_LONG |
|---|
| 1068 | printf(" zzuf -h | --help\n"); |
|---|
| 1069 | printf(" zzuf -V | --version\n"); |
|---|
| 1070 | # else |
|---|
| 1071 | printf(" zzuf -h\n"); |
|---|
| 1072 | printf(" zzuf -V\n"); |
|---|
| 1073 | # endif |
|---|
| 1074 | printf("Run PROGRAM with optional arguments ARGS and fuzz its input.\n"); |
|---|
| 1075 | printf("\n"); |
|---|
| 1076 | printf("Mandatory arguments to long options are mandatory for short options too.\n"); |
|---|
| 1077 | # if defined HAVE_GETOPT_LONG |
|---|
| 1078 | printf(" -A, --autoinc increment seed each time a new file is opened\n"); |
|---|
| 1079 | printf(" -b, --bytes <ranges> only fuzz bytes at offsets within <ranges>\n"); |
|---|
| 1080 | printf(" -B, --max-bytes <n> kill children that output more than <n> bytes\n"); |
|---|
| 1081 | #if defined HAVE_REGEX_H |
|---|
| 1082 | printf(" -c, --cmdline only fuzz files specified in the command line\n"); |
|---|
| 1083 | #endif |
|---|
| 1084 | printf(" -C, --max-crashes <n> stop after <n> children have crashed (default 1)\n"); |
|---|
| 1085 | printf(" -d, --debug print debug messages\n"); |
|---|
| 1086 | printf(" -D, --delay delay between forks\n"); |
|---|
| 1087 | #if defined HAVE_REGEX_H |
|---|
| 1088 | printf(" -E, --exclude <regex> do not fuzz files matching <regex>\n"); |
|---|
| 1089 | #endif |
|---|
| 1090 | printf(" -f, --fuzzing <mode> use fuzzing mode <mode> ([xor] set unset)\n"); |
|---|
| 1091 | printf(" -F, --max-forks <n> number of concurrent children (default 1)\n"); |
|---|
| 1092 | printf(" -i, --stdin fuzz standard input\n"); |
|---|
| 1093 | #if defined HAVE_REGEX_H |
|---|
| 1094 | printf(" -I, --include <regex> only fuzz files matching <regex>\n"); |
|---|
| 1095 | #endif |
|---|
| 1096 | printf(" -m, --md5 compute the output's MD5 hash\n"); |
|---|
| 1097 | #if defined HAVE_SETRLIMIT |
|---|
| 1098 | printf(" -M, --max-memory <n> maximum child virtual memory size in MB\n"); |
|---|
| 1099 | #endif |
|---|
| 1100 | printf(" -n, --network fuzz network input\n"); |
|---|
| 1101 | printf(" -P, --protect <list> protect bytes and characters in <list>\n"); |
|---|
| 1102 | printf(" -q, --quiet do not print children's messages\n"); |
|---|
| 1103 | printf(" -r, --ratio <ratio> bit fuzzing ratio (default %g)\n", DEFAULT_RATIO); |
|---|
| 1104 | printf(" --ratio <start:stop> specify a ratio range\n"); |
|---|
| 1105 | printf(" -R, --refuse <list> refuse bytes and characters in <list>\n"); |
|---|
| 1106 | printf(" -s, --seed <seed> random seed (default %i)\n", DEFAULT_SEED); |
|---|
| 1107 | printf(" --seed <start:stop> specify a seed range\n"); |
|---|
| 1108 | printf(" -S, --signal prevent children from diverting crashing signals\n"); |
|---|
| 1109 | printf(" -T, --max-time <n> kill children that run for more than <n> seconds\n"); |
|---|
| 1110 | printf(" -v, --verbose print information during the run\n"); |
|---|
| 1111 | printf(" -x, --check-exit report processes that exit with a non-zero status\n"); |
|---|
| 1112 | printf(" -h, --help display this help and exit\n"); |
|---|
| 1113 | printf(" -V, --version output version information and exit\n"); |
|---|
| 1114 | # else |
|---|
| 1115 | printf(" -A increment seed each time a new file is opened\n"); |
|---|
| 1116 | printf(" -b <ranges> only fuzz bytes at offsets within <ranges>\n"); |
|---|
| 1117 | printf(" -B <n> kill children that output more than <n> bytes\n"); |
|---|
| 1118 | #if defined HAVE_REGEX_H |
|---|
| 1119 | printf(" -c only fuzz files specified in the command line\n"); |
|---|
| 1120 | #endif |
|---|
| 1121 | printf(" -C <n> stop after <n> children have crashed (default 1)\n"); |
|---|
| 1122 | printf(" -d print debug messages\n"); |
|---|
| 1123 | printf(" -D delay between forks\n"); |
|---|
| 1124 | #if defined HAVE_REGEX_H |
|---|
| 1125 | printf(" -E <regex> do not fuzz files matching <regex>\n"); |
|---|
| 1126 | #endif |
|---|
| 1127 | printf(" -f <mode> use fuzzing mode <mode>\n"); |
|---|
| 1128 | printf(" -F <n> number of concurrent forks (default 1)\n"); |
|---|
| 1129 | printf(" -i fuzz standard input\n"); |
|---|
| 1130 | #if defined HAVE_REGEX_H |
|---|
| 1131 | printf(" -I <regex> only fuzz files matching <regex>\n"); |
|---|
| 1132 | #endif |
|---|
| 1133 | printf(" -m compute the output's MD5 hash\n"); |
|---|
| 1134 | #if defined HAVE_SETRLIMIT |
|---|
| 1135 | printf(" -M maximum child virtual memory size in MB\n"); |
|---|
| 1136 | #endif |
|---|
| 1137 | printf(" -n fuzz network input\n"); |
|---|
| 1138 | printf(" -P <list> protect bytes and characters in <list>\n"); |
|---|
| 1139 | printf(" -q do not print the fuzzed application's messages\n"); |
|---|
| 1140 | printf(" -r <ratio> bit fuzzing ratio (default %g)\n", DEFAULT_RATIO); |
|---|
| 1141 | printf(" <start:stop> specify a ratio range\n"); |
|---|
| 1142 | printf(" -R <list> refuse bytes and characters in <list>\n"); |
|---|
| 1143 | printf(" -s <seed> random seed (default %i)\n", DEFAULT_SEED); |
|---|
| 1144 | printf(" <start:stop> specify a seed range\n"); |
|---|
| 1145 | printf(" -S prevent children from diverting crashing signals\n"); |
|---|
| 1146 | printf(" -T <n> kill children that run for more than <n> seconds\n"); |
|---|
| 1147 | printf(" -v print information during the run\n"); |
|---|
| 1148 | printf(" -x report processes that exit with a non-zero status\n"); |
|---|
| 1149 | printf(" -h display this help and exit\n"); |
|---|
| 1150 | printf(" -V output version information and exit\n"); |
|---|
| 1151 | # endif |
|---|
| 1152 | printf("\n"); |
|---|
| 1153 | printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n"); |
|---|
| 1154 | } |
|---|
| 1155 | #endif |
|---|
| 1156 | |
|---|