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