| 1 | /* |
|---|
| 2 | * zzuf - general purpose fuzzer |
|---|
| 3 | * Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * This program is free software. It comes without any warranty, to |
|---|
| 7 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 8 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 9 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | /* |
|---|
| 14 | * main.c: main program |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #include "config.h" |
|---|
| 18 | |
|---|
| 19 | #define _INCLUDE_POSIX_SOURCE /* for STDERR_FILENO on HP-UX */ |
|---|
| 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_LONG |
|---|
| 27 | # include "mygetopt.h" |
|---|
| 28 | #elif defined HAVE_GETOPT_H |
|---|
| 29 | # include <getopt.h> |
|---|
| 30 | #endif |
|---|
| 31 | #include <stdio.h> |
|---|
| 32 | #include <stdlib.h> |
|---|
| 33 | #if defined HAVE_UNISTD_H |
|---|
| 34 | # include <unistd.h> /* for read(), write(), close() */ |
|---|
| 35 | #endif |
|---|
| 36 | #if defined HAVE_REGEX_H |
|---|
| 37 | # include <regex.h> |
|---|
| 38 | #endif |
|---|
| 39 | #if defined HAVE_WINSOCK2_H |
|---|
| 40 | # include <winsock2.h> /* for fd_set */ |
|---|
| 41 | #endif |
|---|
| 42 | #if defined HAVE_IO_H |
|---|
| 43 | # include <io.h> |
|---|
| 44 | #endif |
|---|
| 45 | #include <string.h> |
|---|
| 46 | #include <errno.h> |
|---|
| 47 | #include <signal.h> |
|---|
| 48 | #if defined HAVE_SYS_TIME_H |
|---|
| 49 | # include <sys/time.h> |
|---|
| 50 | #endif |
|---|
| 51 | #if defined HAVE_SYS_WAIT_H |
|---|
| 52 | # include <sys/wait.h> |
|---|
| 53 | #endif |
|---|
| 54 | #if defined HAVE_SYS_RESOURCE_H |
|---|
| 55 | # include <sys/resource.h> /* for RLIMIT_AS */ |
|---|
| 56 | #endif |
|---|
| 57 | |
|---|
| 58 | #include "common.h" |
|---|
| 59 | #include "opts.h" |
|---|
| 60 | #include "random.h" |
|---|
| 61 | #include "fd.h" |
|---|
| 62 | #include "fuzz.h" |
|---|
| 63 | #include "myfork.h" |
|---|
| 64 | #include "md5.h" |
|---|
| 65 | #include "timer.h" |
|---|
| 66 | |
|---|
| 67 | #if defined HAVE_GETOPT_LONG |
|---|
| 68 | # define mygetopt getopt_long |
|---|
| 69 | # define myoptind optind |
|---|
| 70 | # define myoptarg optarg |
|---|
| 71 | # define myoption option |
|---|
| 72 | #endif |
|---|
| 73 | |
|---|
| 74 | #if !defined SIGKILL |
|---|
| 75 | # define SIGKILL 9 |
|---|
| 76 | #endif |
|---|
| 77 | |
|---|
| 78 | /* Handle old libtool versions */ |
|---|
| 79 | #if !defined LT_OBJDIR |
|---|
| 80 | # define LT_OBJDIR ".libs/" |
|---|
| 81 | #endif |
|---|
| 82 | |
|---|
| 83 | #if defined RLIMIT_AS |
|---|
| 84 | # define ZZUF_RLIMIT_MEM RLIMIT_AS |
|---|
| 85 | #elif defined RLIMIT_VMEM |
|---|
| 86 | # define ZZUF_RLIMIT_MEM RLIMIT_VMEM |
|---|
| 87 | #elif defined RLIMIT_DATA |
|---|
| 88 | # define ZZUF_RLIMIT_MEM RLIMIT_DATA |
|---|
| 89 | #else |
|---|
| 90 | # undef ZZUF_RLIMIT_MEM |
|---|
| 91 | #endif |
|---|
| 92 | |
|---|
| 93 | #if defined RLIMIT_CPU |
|---|
| 94 | # define ZZUF_RLIMIT_CPU RLIMIT_CPU |
|---|
| 95 | #else |
|---|
| 96 | # undef ZZUF_RLIMIT_CPU |
|---|
| 97 | #endif |
|---|
| 98 | |
|---|
| 99 | static void loop_stdin(struct opts *); |
|---|
| 100 | |
|---|
| 101 | static void spawn_children(struct opts *); |
|---|
| 102 | static void clean_children(struct opts *); |
|---|
| 103 | static void read_children(struct opts *); |
|---|
| 104 | |
|---|
| 105 | #if !defined HAVE_SETENV |
|---|
| 106 | static void setenv(char const *, char const *, int); |
|---|
| 107 | #endif |
|---|
| 108 | #if defined HAVE_WAITPID |
|---|
| 109 | static char const *sig2name(int); |
|---|
| 110 | #endif |
|---|
| 111 | static void finfo(FILE *, struct opts *, uint32_t); |
|---|
| 112 | #if defined HAVE_REGEX_H |
|---|
| 113 | static char *merge_regex(char *, char *); |
|---|
| 114 | static char *merge_file(char *, char *); |
|---|
| 115 | #endif |
|---|
| 116 | static void version(void); |
|---|
| 117 | static void usage(void); |
|---|
| 118 | |
|---|
| 119 | #define ZZUF_FD_SET(fd, p_fdset, maxfd) \ |
|---|
| 120 | if(fd >= 0) \ |
|---|
| 121 | { \ |
|---|
| 122 | FD_SET((unsigned int)fd, p_fdset); \ |
|---|
| 123 | if(fd > maxfd) \ |
|---|
| 124 | maxfd = fd; \ |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | #define ZZUF_FD_ISSET(fd, p_fdset) \ |
|---|
| 128 | ((fd >= 0) && (FD_ISSET(fd, p_fdset))) |
|---|
| 129 | |
|---|
| 130 | int main(int argc, char *argv[]) |
|---|
| 131 | { |
|---|
| 132 | struct opts _opts, *opts = &_opts; |
|---|
| 133 | char *tmp; |
|---|
| 134 | #if defined HAVE_REGEX_H |
|---|
| 135 | char *include = NULL, *exclude = NULL; |
|---|
| 136 | int cmdline = 0; |
|---|
| 137 | #endif |
|---|
| 138 | int debug = 0, network = 0; |
|---|
| 139 | int i; |
|---|
| 140 | |
|---|
| 141 | _zz_opts_init(opts); |
|---|
| 142 | |
|---|
| 143 | for(;;) |
|---|
| 144 | { |
|---|
| 145 | #if defined HAVE_REGEX_H |
|---|
| 146 | # define OPTSTR_REGEX "cE:I:" |
|---|
| 147 | #else |
|---|
| 148 | # define OPTSTR_REGEX "" |
|---|
| 149 | #endif |
|---|
| 150 | #if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_MEM |
|---|
| 151 | # define OPTSTR_RLIMIT_MEM "M:" |
|---|
| 152 | #else |
|---|
| 153 | # define OPTSTR_RLIMIT_MEM "" |
|---|
| 154 | #endif |
|---|
| 155 | #if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_CPU |
|---|
| 156 | # define OPTSTR_RLIMIT_CPU "T:" |
|---|
| 157 | #else |
|---|
| 158 | # define OPTSTR_RLIMIT_CPU "" |
|---|
| 159 | #endif |
|---|
| 160 | #define OPTSTR "+" OPTSTR_REGEX OPTSTR_RLIMIT_MEM OPTSTR_RLIMIT_CPU \ |
|---|
| 161 | "a:Ab:B:C:dD:e:f:F:ij:l:mnp:P:qr:R:s:St:U:vxhV" |
|---|
| 162 | #define MOREINFO "Try `%s --help' for more information.\n" |
|---|
| 163 | int option_index = 0; |
|---|
| 164 | static struct myoption long_options[] = |
|---|
| 165 | { |
|---|
| 166 | /* Long option, needs arg, flag, short option */ |
|---|
| 167 | { "allow", 1, NULL, 'a' }, |
|---|
| 168 | { "autoinc", 0, NULL, 'A' }, |
|---|
| 169 | { "bytes", 1, NULL, 'b' }, |
|---|
| 170 | { "max-bytes", 1, NULL, 'B' }, |
|---|
| 171 | #if defined HAVE_REGEX_H |
|---|
| 172 | { "cmdline", 0, NULL, 'c' }, |
|---|
| 173 | #endif |
|---|
| 174 | { "max-crashes", 1, NULL, 'C' }, |
|---|
| 175 | { "debug", 0, NULL, 'd' }, |
|---|
| 176 | { "delay", 1, NULL, 'D' }, |
|---|
| 177 | #if defined HAVE_REGEX_H |
|---|
| 178 | { "exclude", 1, NULL, 'E' }, |
|---|
| 179 | #endif |
|---|
| 180 | { "fuzzing", 1, NULL, 'f' }, |
|---|
| 181 | { "stdin", 0, NULL, 'i' }, |
|---|
| 182 | #if defined HAVE_REGEX_H |
|---|
| 183 | { "include", 1, NULL, 'I' }, |
|---|
| 184 | #endif |
|---|
| 185 | { "jobs", 1, NULL, 'j' }, |
|---|
| 186 | { "list", 1, NULL, 'l' }, |
|---|
| 187 | { "md5", 0, NULL, 'm' }, |
|---|
| 188 | { "max-memory", 1, NULL, 'M' }, |
|---|
| 189 | { "network", 0, NULL, 'n' }, |
|---|
| 190 | { "ports", 1, NULL, 'p' }, |
|---|
| 191 | { "protect", 1, NULL, 'P' }, |
|---|
| 192 | { "quiet", 0, NULL, 'q' }, |
|---|
| 193 | { "ratio", 1, NULL, 'r' }, |
|---|
| 194 | { "refuse", 1, NULL, 'R' }, |
|---|
| 195 | { "seed", 1, NULL, 's' }, |
|---|
| 196 | { "signal", 0, NULL, 'S' }, |
|---|
| 197 | { "max-time", 1, NULL, 't' }, |
|---|
| 198 | { "max-cputime", 1, NULL, 'T' }, |
|---|
| 199 | { "max-usertime", 1, NULL, 'U' }, |
|---|
| 200 | { "verbose", 0, NULL, 'v' }, |
|---|
| 201 | { "check-exit", 0, NULL, 'x' }, |
|---|
| 202 | { "help", 0, NULL, 'h' }, |
|---|
| 203 | { "version", 0, NULL, 'V' }, |
|---|
| 204 | { NULL, 0, NULL, 0 } |
|---|
| 205 | }; |
|---|
| 206 | int c = mygetopt(argc, argv, OPTSTR, long_options, &option_index); |
|---|
| 207 | |
|---|
| 208 | if(c == -1) |
|---|
| 209 | break; |
|---|
| 210 | |
|---|
| 211 | switch(c) |
|---|
| 212 | { |
|---|
| 213 | case 'a': /* --allow */ |
|---|
| 214 | opts->allow = myoptarg; |
|---|
| 215 | break; |
|---|
| 216 | case 'A': /* --autoinc */ |
|---|
| 217 | setenv("ZZUF_AUTOINC", "1", 1); |
|---|
| 218 | break; |
|---|
| 219 | case 'b': /* --bytes */ |
|---|
| 220 | opts->bytes = myoptarg; |
|---|
| 221 | break; |
|---|
| 222 | case 'B': /* --max-bytes */ |
|---|
| 223 | if(myoptarg[0] == '=') |
|---|
| 224 | myoptarg++; |
|---|
| 225 | opts->maxbytes = atoi(myoptarg); |
|---|
| 226 | break; |
|---|
| 227 | #if defined HAVE_REGEX_H |
|---|
| 228 | case 'c': /* --cmdline */ |
|---|
| 229 | cmdline = 1; |
|---|
| 230 | break; |
|---|
| 231 | #endif |
|---|
| 232 | case 'C': /* --max-crashes */ |
|---|
| 233 | if(myoptarg[0] == '=') |
|---|
| 234 | myoptarg++; |
|---|
| 235 | opts->maxcrashes = atoi(myoptarg); |
|---|
| 236 | if(opts->maxcrashes <= 0) |
|---|
| 237 | opts->maxcrashes = 0; |
|---|
| 238 | break; |
|---|
| 239 | case 'd': /* --debug */ |
|---|
| 240 | debug++; |
|---|
| 241 | break; |
|---|
| 242 | case 'D': /* --delay */ |
|---|
| 243 | if(myoptarg[0] == '=') |
|---|
| 244 | myoptarg++; |
|---|
| 245 | opts->delay = (int64_t)(atof(myoptarg) * 1000000.0); |
|---|
| 246 | break; |
|---|
| 247 | #if defined HAVE_REGEX_H |
|---|
| 248 | case 'E': /* --exclude */ |
|---|
| 249 | exclude = merge_regex(exclude, myoptarg); |
|---|
| 250 | if(!exclude) |
|---|
| 251 | { |
|---|
| 252 | fprintf(stderr, "%s: invalid regex -- `%s'\n", |
|---|
| 253 | argv[0], myoptarg); |
|---|
| 254 | _zz_opts_fini(opts); |
|---|
| 255 | return EXIT_FAILURE; |
|---|
| 256 | } |
|---|
| 257 | break; |
|---|
| 258 | #endif |
|---|
| 259 | case 'f': /* --fuzzing */ |
|---|
| 260 | opts->fuzzing = myoptarg; |
|---|
| 261 | break; |
|---|
| 262 | case 'F': |
|---|
| 263 | fprintf(stderr, "%s: `-F' is deprecated, use `-j'\n", argv[0]); |
|---|
| 264 | return EXIT_FAILURE; |
|---|
| 265 | case 'i': /* --stdin */ |
|---|
| 266 | setenv("ZZUF_STDIN", "1", 1); |
|---|
| 267 | break; |
|---|
| 268 | #if defined HAVE_REGEX_H |
|---|
| 269 | case 'I': /* --include */ |
|---|
| 270 | include = merge_regex(include, myoptarg); |
|---|
| 271 | if(!include) |
|---|
| 272 | { |
|---|
| 273 | fprintf(stderr, "%s: invalid regex -- `%s'\n", |
|---|
| 274 | argv[0], myoptarg); |
|---|
| 275 | _zz_opts_fini(opts); |
|---|
| 276 | return EXIT_FAILURE; |
|---|
| 277 | } |
|---|
| 278 | break; |
|---|
| 279 | #endif |
|---|
| 280 | case 'j': /* --jobs */ |
|---|
| 281 | if(myoptarg[0] == '=') |
|---|
| 282 | myoptarg++; |
|---|
| 283 | opts->maxchild = atoi(myoptarg) > 1 ? atoi(myoptarg) : 1; |
|---|
| 284 | break; |
|---|
| 285 | case 'l': /* --list */ |
|---|
| 286 | opts->list = myoptarg; |
|---|
| 287 | break; |
|---|
| 288 | case 'm': /* --md5 */ |
|---|
| 289 | opts->md5 = 1; |
|---|
| 290 | break; |
|---|
| 291 | #if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_MEM |
|---|
| 292 | case 'M': /* --max-memory */ |
|---|
| 293 | setenv("ZZUF_MEMORY", "1", 1); |
|---|
| 294 | if(myoptarg[0] == '=') |
|---|
| 295 | myoptarg++; |
|---|
| 296 | opts->maxmem = atoi(myoptarg); |
|---|
| 297 | break; |
|---|
| 298 | #endif |
|---|
| 299 | case 'n': /* --network */ |
|---|
| 300 | setenv("ZZUF_NETWORK", "1", 1); |
|---|
| 301 | network = 1; |
|---|
| 302 | break; |
|---|
| 303 | case 'p': /* --ports */ |
|---|
| 304 | opts->ports = myoptarg; |
|---|
| 305 | break; |
|---|
| 306 | case 'P': /* --protect */ |
|---|
| 307 | opts->protect = myoptarg; |
|---|
| 308 | break; |
|---|
| 309 | case 'q': /* --quiet */ |
|---|
| 310 | opts->quiet = 1; |
|---|
| 311 | break; |
|---|
| 312 | case 'r': /* --ratio */ |
|---|
| 313 | if(myoptarg[0] == '=') |
|---|
| 314 | myoptarg++; |
|---|
| 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 | if(myoptarg[0] == '=') |
|---|
| 324 | myoptarg++; |
|---|
| 325 | tmp = strchr(myoptarg, ':'); |
|---|
| 326 | opts->seed = atol(myoptarg); |
|---|
| 327 | opts->endseed = tmp ? tmp[1] ? (uint32_t)atol(tmp + 1) |
|---|
| 328 | : (uint32_t)-1L |
|---|
| 329 | : opts->seed + 1; |
|---|
| 330 | break; |
|---|
| 331 | case 'S': /* --signal */ |
|---|
| 332 | setenv("ZZUF_SIGNAL", "1", 1); |
|---|
| 333 | break; |
|---|
| 334 | case 't': /* --max-time */ |
|---|
| 335 | if(myoptarg[0] == '=') |
|---|
| 336 | myoptarg++; |
|---|
| 337 | opts->maxtime = (int64_t)atoll(myoptarg) * 1000000; |
|---|
| 338 | break; |
|---|
| 339 | #if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_CPU |
|---|
| 340 | case 'T': /* --max-cputime */ |
|---|
| 341 | if(myoptarg[0] == '=') |
|---|
| 342 | myoptarg++; |
|---|
| 343 | opts->maxcpu = (int)(atof(myoptarg) + 0.5); |
|---|
| 344 | break; |
|---|
| 345 | #endif |
|---|
| 346 | case 'U': /* --max-usertime */ |
|---|
| 347 | if(myoptarg[0] == '=') |
|---|
| 348 | myoptarg++; |
|---|
| 349 | opts->maxusertime = (int64_t)(atof(myoptarg) * 1000000.0); |
|---|
| 350 | break; |
|---|
| 351 | case 'x': /* --check-exit */ |
|---|
| 352 | opts->checkexit = 1; |
|---|
| 353 | break; |
|---|
| 354 | case 'v': /* --verbose */ |
|---|
| 355 | opts->verbose = 1; |
|---|
| 356 | break; |
|---|
| 357 | case 'h': /* --help */ |
|---|
| 358 | usage(); |
|---|
| 359 | _zz_opts_fini(opts); |
|---|
| 360 | return 0; |
|---|
| 361 | case 'V': /* --version */ |
|---|
| 362 | version(); |
|---|
| 363 | _zz_opts_fini(opts); |
|---|
| 364 | return 0; |
|---|
| 365 | default: |
|---|
| 366 | fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c); |
|---|
| 367 | printf(MOREINFO, argv[0]); |
|---|
| 368 | _zz_opts_fini(opts); |
|---|
| 369 | return EXIT_FAILURE; |
|---|
| 370 | } |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | if(opts->ports && !network) |
|---|
| 374 | { |
|---|
| 375 | fprintf(stderr, "%s: port option (-p) requires network fuzzing (-n)\n", |
|---|
| 376 | argv[0]); |
|---|
| 377 | printf(MOREINFO, argv[0]); |
|---|
| 378 | _zz_opts_fini(opts); |
|---|
| 379 | return EXIT_FAILURE; |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | if (opts->allow && !network) |
|---|
| 383 | { |
|---|
| 384 | fprintf(stderr, "%s: allow option (-a) requires network fuzzing (-n)\n", |
|---|
| 385 | argv[0]); |
|---|
| 386 | printf(MOREINFO, argv[0]); |
|---|
| 387 | _zz_opts_fini(opts); |
|---|
| 388 | return EXIT_FAILURE; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | _zz_setratio(opts->minratio, opts->maxratio); |
|---|
| 392 | _zz_setseed(opts->seed); |
|---|
| 393 | |
|---|
| 394 | /* If asked to read from the standard input */ |
|---|
| 395 | if(myoptind >= argc) |
|---|
| 396 | { |
|---|
| 397 | if(opts->verbose) |
|---|
| 398 | { |
|---|
| 399 | finfo(stderr, opts, opts->seed); |
|---|
| 400 | fprintf(stderr, "reading from stdin\n"); |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | if(opts->endseed != opts->seed + 1) |
|---|
| 404 | { |
|---|
| 405 | fprintf(stderr, "%s: seed ranges are incompatible with " |
|---|
| 406 | "stdin fuzzing\n", argv[0]); |
|---|
| 407 | printf(MOREINFO, argv[0]); |
|---|
| 408 | _zz_opts_fini(opts); |
|---|
| 409 | return EXIT_FAILURE; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | loop_stdin(opts); |
|---|
| 413 | |
|---|
| 414 | _zz_opts_fini(opts); |
|---|
| 415 | return EXIT_SUCCESS; |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | /* If asked to launch programs */ |
|---|
| 419 | #if defined HAVE_REGEX_H |
|---|
| 420 | if(cmdline) |
|---|
| 421 | { |
|---|
| 422 | int dashdash = 0; |
|---|
| 423 | |
|---|
| 424 | for(i = myoptind + 1; i < argc; i++) |
|---|
| 425 | { |
|---|
| 426 | if(dashdash) |
|---|
| 427 | include = merge_file(include, argv[i]); |
|---|
| 428 | else if(!strcmp("--", argv[i])) |
|---|
| 429 | dashdash = 1; |
|---|
| 430 | else if(argv[i][0] != '-') |
|---|
| 431 | include = merge_file(include, argv[i]); |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | if(include) |
|---|
| 436 | setenv("ZZUF_INCLUDE", include, 1); |
|---|
| 437 | if(exclude) |
|---|
| 438 | setenv("ZZUF_EXCLUDE", exclude, 1); |
|---|
| 439 | #endif |
|---|
| 440 | |
|---|
| 441 | setenv("ZZUF_DEBUG", debug ? debug > 1 ? "2" : "1" : "0", 1); |
|---|
| 442 | setenv("ZZUF_DEBUGFD", DEBUG_FILENO_STR, 1); |
|---|
| 443 | |
|---|
| 444 | if(opts->fuzzing) |
|---|
| 445 | setenv("ZZUF_FUZZING", opts->fuzzing, 1); |
|---|
| 446 | if(opts->bytes) |
|---|
| 447 | setenv("ZZUF_BYTES", opts->bytes, 1); |
|---|
| 448 | if(opts->list) |
|---|
| 449 | setenv("ZZUF_LIST", opts->list, 1); |
|---|
| 450 | if(opts->ports) |
|---|
| 451 | setenv("ZZUF_PORTS", opts->ports, 1); |
|---|
| 452 | if(opts->allow && opts->allow[0] == '!') |
|---|
| 453 | setenv("ZZUF_DENY", opts->allow, 1); |
|---|
| 454 | else if(opts->allow) |
|---|
| 455 | setenv("ZZUF_ALLOW", opts->allow, 1); |
|---|
| 456 | if(opts->protect) |
|---|
| 457 | setenv("ZZUF_PROTECT", opts->protect, 1); |
|---|
| 458 | if(opts->refuse) |
|---|
| 459 | setenv("ZZUF_REFUSE", opts->refuse, 1); |
|---|
| 460 | |
|---|
| 461 | /* Allocate memory for children handling */ |
|---|
| 462 | opts->child = malloc(opts->maxchild * sizeof(struct child)); |
|---|
| 463 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 464 | opts->child[i].status = STATUS_FREE; |
|---|
| 465 | opts->nchild = 0; |
|---|
| 466 | |
|---|
| 467 | /* Create new argv */ |
|---|
| 468 | opts->oldargv = argv; |
|---|
| 469 | opts->newargv = malloc((argc - myoptind + 1) * sizeof(char *)); |
|---|
| 470 | memcpy(opts->newargv, argv + myoptind, (argc - myoptind) * sizeof(char *)); |
|---|
| 471 | opts->newargv[argc - myoptind] = (char *)NULL; |
|---|
| 472 | |
|---|
| 473 | /* Main loop */ |
|---|
| 474 | while(opts->nchild || opts->seed < opts->endseed) |
|---|
| 475 | { |
|---|
| 476 | /* Spawn new children, if necessary */ |
|---|
| 477 | spawn_children(opts); |
|---|
| 478 | |
|---|
| 479 | /* Cleanup dead or dying children */ |
|---|
| 480 | clean_children(opts); |
|---|
| 481 | |
|---|
| 482 | /* Read data from children */ |
|---|
| 483 | read_children(opts); |
|---|
| 484 | |
|---|
| 485 | if(opts->maxcrashes && opts->crashes >= opts->maxcrashes |
|---|
| 486 | && opts->nchild == 0) |
|---|
| 487 | { |
|---|
| 488 | if(opts->verbose) |
|---|
| 489 | fprintf(stderr, |
|---|
| 490 | "zzuf: maximum crash count reached, exiting\n"); |
|---|
| 491 | break; |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | if(opts->maxtime && _zz_time() - opts->starttime >= opts->maxtime |
|---|
| 495 | && opts->nchild == 0) |
|---|
| 496 | { |
|---|
| 497 | if(opts->verbose) |
|---|
| 498 | fprintf(stderr, |
|---|
| 499 | "zzuf: maximum running time reached, exiting\n"); |
|---|
| 500 | break; |
|---|
| 501 | } |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | /* Clean up */ |
|---|
| 505 | _zz_opts_fini(opts); |
|---|
| 506 | |
|---|
| 507 | return opts->crashes ? EXIT_FAILURE : EXIT_SUCCESS; |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | static void loop_stdin(struct opts *opts) |
|---|
| 511 | { |
|---|
| 512 | uint8_t md5sum[16]; |
|---|
| 513 | struct md5 *ctx = NULL; |
|---|
| 514 | int total = 0; |
|---|
| 515 | |
|---|
| 516 | if(opts->md5) |
|---|
| 517 | ctx = _zz_md5_init(); |
|---|
| 518 | |
|---|
| 519 | if(opts->fuzzing) |
|---|
| 520 | _zz_fuzzing(opts->fuzzing); |
|---|
| 521 | if(opts->bytes) |
|---|
| 522 | _zz_bytes(opts->bytes); |
|---|
| 523 | if(opts->list) |
|---|
| 524 | _zz_list(opts->list); |
|---|
| 525 | if(opts->protect) |
|---|
| 526 | _zz_protect(opts->protect); |
|---|
| 527 | if(opts->refuse) |
|---|
| 528 | _zz_refuse(opts->refuse); |
|---|
| 529 | |
|---|
| 530 | _zz_fd_init(); |
|---|
| 531 | _zz_register(0); |
|---|
| 532 | |
|---|
| 533 | for(;;) |
|---|
| 534 | { |
|---|
| 535 | uint8_t buf[BUFSIZ]; |
|---|
| 536 | int ret, toread = BUFSIZ, off = 0, nw = 0; |
|---|
| 537 | |
|---|
| 538 | if(opts->maxbytes >= 0) |
|---|
| 539 | { |
|---|
| 540 | if(total >= opts->maxbytes) |
|---|
| 541 | break; |
|---|
| 542 | if(total + BUFSIZ >= opts->maxbytes) |
|---|
| 543 | toread = opts->maxbytes - total; |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | ret = read(0, buf, toread); |
|---|
| 547 | if(ret <= 0) |
|---|
| 548 | break; |
|---|
| 549 | |
|---|
| 550 | total += ret; |
|---|
| 551 | |
|---|
| 552 | _zz_fuzz(0, buf, ret); |
|---|
| 553 | _zz_addpos(0, ret); |
|---|
| 554 | |
|---|
| 555 | if(opts->md5) |
|---|
| 556 | _zz_md5_add(ctx, buf, ret); |
|---|
| 557 | else while(ret) |
|---|
| 558 | { |
|---|
| 559 | if((nw = write(1, buf + off, (unsigned int)ret)) < 0) |
|---|
| 560 | break; |
|---|
| 561 | ret -= nw; |
|---|
| 562 | off += nw; |
|---|
| 563 | } |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | if(opts->md5) |
|---|
| 567 | { |
|---|
| 568 | _zz_md5_fini(md5sum, ctx); |
|---|
| 569 | finfo(stdout, opts, opts->seed); |
|---|
| 570 | fprintf(stdout, "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x" |
|---|
| 571 | "%.02x%.02x%.02x%.02x%.02x%.02x\n", md5sum[0], md5sum[1], |
|---|
| 572 | md5sum[2], md5sum[3], md5sum[4], md5sum[5], md5sum[6], |
|---|
| 573 | md5sum[7], md5sum[8], md5sum[9], md5sum[10], md5sum[11], |
|---|
| 574 | md5sum[12], md5sum[13], md5sum[14], md5sum[15]); |
|---|
| 575 | fflush(stdout); |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | _zz_unregister(0); |
|---|
| 579 | _zz_fd_fini(); |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | static void finfo(FILE *fp, struct opts *opts, uint32_t seed) |
|---|
| 583 | { |
|---|
| 584 | if(opts->minratio == opts->maxratio) |
|---|
| 585 | fprintf(fp, "zzuf[s=%i,r=%g]: ", seed, opts->minratio); |
|---|
| 586 | else |
|---|
| 587 | fprintf(fp, "zzuf[s=%i,r=%g:%g]: ", seed, |
|---|
| 588 | opts->minratio, opts->maxratio); |
|---|
| 589 | } |
|---|
| 590 | |
|---|
| 591 | #if defined HAVE_REGEX_H |
|---|
| 592 | static char *merge_file(char *regex, char *file) |
|---|
| 593 | { |
|---|
| 594 | char *newfile = malloc(5 + 2 * strlen(file) + 1 + 1), *tmp = newfile; |
|---|
| 595 | |
|---|
| 596 | *tmp++ = '('; |
|---|
| 597 | *tmp++ = '^'; |
|---|
| 598 | *tmp++ = '|'; |
|---|
| 599 | *tmp++ = '/'; |
|---|
| 600 | *tmp++ = ')'; |
|---|
| 601 | while(*file) |
|---|
| 602 | { |
|---|
| 603 | if(strchr("^.[$()|*+?{\\", *file)) |
|---|
| 604 | *tmp++ = '\\'; |
|---|
| 605 | *tmp++ = *file++; |
|---|
| 606 | } |
|---|
| 607 | *tmp++ = '$'; |
|---|
| 608 | *tmp++ = '\0'; |
|---|
| 609 | |
|---|
| 610 | tmp = merge_regex(regex, newfile); |
|---|
| 611 | free(newfile); |
|---|
| 612 | return tmp; |
|---|
| 613 | } |
|---|
| 614 | |
|---|
| 615 | static char *merge_regex(char *regex, char *string) |
|---|
| 616 | { |
|---|
| 617 | regex_t optre; |
|---|
| 618 | |
|---|
| 619 | if(regex) |
|---|
| 620 | { |
|---|
| 621 | regex = realloc(regex, strlen(regex) + strlen(string) + 1 + 1); |
|---|
| 622 | sprintf(regex + strlen(regex) - 1, "|%s)", string); |
|---|
| 623 | } |
|---|
| 624 | else |
|---|
| 625 | { |
|---|
| 626 | regex = malloc(1 + strlen(string) + 1 + 1); |
|---|
| 627 | sprintf(regex, "(%s)", string); |
|---|
| 628 | } |
|---|
| 629 | |
|---|
| 630 | if(regcomp(&optre, regex, REG_EXTENDED) != 0) |
|---|
| 631 | { |
|---|
| 632 | free(regex); |
|---|
| 633 | return NULL; |
|---|
| 634 | } |
|---|
| 635 | regfree(&optre); |
|---|
| 636 | |
|---|
| 637 | return regex; |
|---|
| 638 | } |
|---|
| 639 | #endif |
|---|
| 640 | |
|---|
| 641 | static void spawn_children(struct opts *opts) |
|---|
| 642 | { |
|---|
| 643 | int64_t now = _zz_time(); |
|---|
| 644 | int i; |
|---|
| 645 | |
|---|
| 646 | if(opts->nchild == opts->maxchild) |
|---|
| 647 | return; /* no slot */ |
|---|
| 648 | |
|---|
| 649 | if(opts->seed == opts->endseed) |
|---|
| 650 | return; /* job finished */ |
|---|
| 651 | |
|---|
| 652 | if(opts->maxcrashes && opts->crashes >= opts->maxcrashes) |
|---|
| 653 | return; /* all jobs crashed */ |
|---|
| 654 | |
|---|
| 655 | if(opts->maxtime && now - opts->starttime >= opts->maxtime) |
|---|
| 656 | return; /* run time exceeded */ |
|---|
| 657 | |
|---|
| 658 | if(opts->delay > 0 && opts->lastlaunch + opts->delay > now) |
|---|
| 659 | return; /* too early */ |
|---|
| 660 | |
|---|
| 661 | /* Find the empty slot */ |
|---|
| 662 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 663 | if(opts->child[i].status == STATUS_FREE) |
|---|
| 664 | break; |
|---|
| 665 | |
|---|
| 666 | if (myfork(&opts->child[i], opts) < 0) |
|---|
| 667 | { |
|---|
| 668 | fprintf(stderr, "error launching `%s'\n", opts->newargv[0]); |
|---|
| 669 | opts->seed++; |
|---|
| 670 | return; |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | /* We’re the parent, acknowledge spawn */ |
|---|
| 674 | opts->child[i].date = now; |
|---|
| 675 | opts->child[i].bytes = 0; |
|---|
| 676 | opts->child[i].seed = opts->seed; |
|---|
| 677 | opts->child[i].ratio = _zz_getratio(); |
|---|
| 678 | opts->child[i].status = STATUS_RUNNING; |
|---|
| 679 | if(opts->md5) |
|---|
| 680 | opts->child[i].ctx = _zz_md5_init(); |
|---|
| 681 | |
|---|
| 682 | if(opts->verbose) |
|---|
| 683 | { |
|---|
| 684 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 685 | fprintf(stderr, "launched `%s'\n", opts->newargv[0]); |
|---|
| 686 | } |
|---|
| 687 | |
|---|
| 688 | opts->lastlaunch = now; |
|---|
| 689 | opts->nchild++; |
|---|
| 690 | opts->seed++; |
|---|
| 691 | |
|---|
| 692 | _zz_setseed(opts->seed); |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | static void clean_children(struct opts *opts) |
|---|
| 696 | { |
|---|
| 697 | #if defined HAVE_KILL |
|---|
| 698 | int64_t now = _zz_time(); |
|---|
| 699 | #endif |
|---|
| 700 | int i, j; |
|---|
| 701 | |
|---|
| 702 | #if defined HAVE_KILL |
|---|
| 703 | /* Terminate children if necessary */ |
|---|
| 704 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 705 | { |
|---|
| 706 | if(opts->child[i].status == STATUS_RUNNING |
|---|
| 707 | && opts->maxbytes >= 0 |
|---|
| 708 | && opts->child[i].bytes > opts->maxbytes) |
|---|
| 709 | { |
|---|
| 710 | if(opts->verbose) |
|---|
| 711 | { |
|---|
| 712 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 713 | fprintf(stderr, "data output exceeded, sending SIGTERM\n"); |
|---|
| 714 | } |
|---|
| 715 | kill(opts->child[i].pid, SIGTERM); |
|---|
| 716 | opts->child[i].date = now; |
|---|
| 717 | opts->child[i].status = STATUS_SIGTERM; |
|---|
| 718 | } |
|---|
| 719 | |
|---|
| 720 | if(opts->child[i].status == STATUS_RUNNING |
|---|
| 721 | && opts->maxusertime >= 0 |
|---|
| 722 | && now > opts->child[i].date + opts->maxusertime) |
|---|
| 723 | { |
|---|
| 724 | if(opts->verbose) |
|---|
| 725 | { |
|---|
| 726 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 727 | fprintf(stderr, "running time exceeded, sending SIGTERM\n"); |
|---|
| 728 | } |
|---|
| 729 | kill(opts->child[i].pid, SIGTERM); |
|---|
| 730 | opts->child[i].date = now; |
|---|
| 731 | opts->child[i].status = STATUS_SIGTERM; |
|---|
| 732 | } |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | /* Kill children if necessary (still there after 2 seconds) */ |
|---|
| 736 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 737 | { |
|---|
| 738 | if(opts->child[i].status == STATUS_SIGTERM |
|---|
| 739 | && now > opts->child[i].date + 2000000) |
|---|
| 740 | { |
|---|
| 741 | if(opts->verbose) |
|---|
| 742 | { |
|---|
| 743 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 744 | fprintf(stderr, "not responding, sending SIGKILL\n"); |
|---|
| 745 | } |
|---|
| 746 | kill(opts->child[i].pid, SIGKILL); |
|---|
| 747 | opts->child[i].status = STATUS_SIGKILL; |
|---|
| 748 | } |
|---|
| 749 | } |
|---|
| 750 | #endif |
|---|
| 751 | |
|---|
| 752 | /* Collect dead children */ |
|---|
| 753 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 754 | { |
|---|
| 755 | uint8_t md5sum[16]; |
|---|
| 756 | #if defined HAVE_WAITPID |
|---|
| 757 | int status; |
|---|
| 758 | pid_t pid; |
|---|
| 759 | #endif |
|---|
| 760 | |
|---|
| 761 | if(opts->child[i].status != STATUS_SIGKILL |
|---|
| 762 | && opts->child[i].status != STATUS_SIGTERM |
|---|
| 763 | && opts->child[i].status != STATUS_EOF) |
|---|
| 764 | continue; |
|---|
| 765 | |
|---|
| 766 | #if defined HAVE_WAITPID |
|---|
| 767 | pid = waitpid(opts->child[i].pid, &status, WNOHANG); |
|---|
| 768 | if(pid <= 0) |
|---|
| 769 | continue; |
|---|
| 770 | |
|---|
| 771 | if(opts->checkexit && WIFEXITED(status) && WEXITSTATUS(status)) |
|---|
| 772 | { |
|---|
| 773 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 774 | fprintf(stderr, "exit %i\n", WEXITSTATUS(status)); |
|---|
| 775 | opts->crashes++; |
|---|
| 776 | } |
|---|
| 777 | else if(WIFSIGNALED(status) |
|---|
| 778 | && !(WTERMSIG(status) == SIGTERM |
|---|
| 779 | && opts->child[i].status == STATUS_SIGTERM)) |
|---|
| 780 | { |
|---|
| 781 | char const *message = ""; |
|---|
| 782 | |
|---|
| 783 | if(WTERMSIG(status) == SIGKILL && opts->maxmem >= 0) |
|---|
| 784 | message = " (memory exceeded?)"; |
|---|
| 785 | # if defined SIGXCPU |
|---|
| 786 | else if(WTERMSIG(status) == SIGXCPU && opts->maxcpu >= 0) |
|---|
| 787 | message = " (CPU time exceeded?)"; |
|---|
| 788 | # endif |
|---|
| 789 | else if(WTERMSIG(status) == SIGKILL && opts->maxcpu >= 0) |
|---|
| 790 | message = " (CPU time exceeded?)"; |
|---|
| 791 | |
|---|
| 792 | finfo(stderr, opts, opts->child[i].seed); |
|---|
| 793 | fprintf(stderr, "signal %i%s%s\n", |
|---|
| 794 | WTERMSIG(status), sig2name(WTERMSIG(status)), message); |
|---|
| 795 | opts->crashes++; |
|---|
| 796 | } |
|---|
| 797 | #endif |
|---|
| 798 | |
|---|
| 799 | for(j = 0; j < 3; j++) |
|---|
| 800 | if(opts->child[i].fd[j] >= 0) |
|---|
| 801 | close(opts->child[i].fd[j]); |
|---|
| 802 | |
|---|
| 803 | if(opts->md5) |
|---|
| 804 | { |
|---|
| 805 | _zz_md5_fini(md5sum, opts->child[i].ctx); |
|---|
| 806 | finfo(stdout, opts, opts->child[i].seed); |
|---|
| 807 | fprintf(stdout, "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x" |
|---|
| 808 | "%.02x%.02x%.02x%.02x%.02x%.02x%.02x\n", md5sum[0], |
|---|
| 809 | md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], |
|---|
| 810 | md5sum[6], md5sum[7], md5sum[8], md5sum[9], md5sum[10], |
|---|
| 811 | md5sum[11], md5sum[12], md5sum[13], md5sum[14], md5sum[15]); |
|---|
| 812 | fflush(stdout); |
|---|
| 813 | } |
|---|
| 814 | opts->child[i].status = STATUS_FREE; |
|---|
| 815 | opts->nchild--; |
|---|
| 816 | } |
|---|
| 817 | } |
|---|
| 818 | |
|---|
| 819 | static void read_children(struct opts *opts) |
|---|
| 820 | { |
|---|
| 821 | struct timeval tv; |
|---|
| 822 | fd_set fdset; |
|---|
| 823 | int i, j, ret, maxfd = 0; |
|---|
| 824 | |
|---|
| 825 | /* Read data from all sockets */ |
|---|
| 826 | FD_ZERO(&fdset); |
|---|
| 827 | for(i = 0; i < opts->maxchild; i++) |
|---|
| 828 | { |
|---|
| 829 | if(opts->child[i].status != STATUS_RUNNING) |
|---|
| 830 | continue; |
|---|
| 831 | |
|---|
| 832 | for(j = 0; j < 3; j++) |
|---|
| 833 | ZZUF_FD_SET(opts->child[i].fd[j], &fdset, maxfd); |
|---|
| 834 | } |
|---|
| 835 | tv.tv_sec = 0; |
|---|
| 836 | tv.tv_usec = 1000; |
|---|
| 837 | |
|---|
| 838 | ret = select(maxfd + 1, &fdset, NULL, NULL, &tv); |
|---|
| 839 | if(ret < 0 && errno) |
|---|
| 840 | perror("select"); |
|---|
| 841 | if(ret <= 0) |
|---|
| 842 | return; |
|---|
| 843 | |
|---|
| 844 | /* XXX: cute (i, j) iterating hack */ |
|---|
| 845 | for(i = 0, j = 0; i < opts->maxchild; i += (j == 2), j = (j + 1) % 3) |
|---|
| 846 | { |
|---|
| 847 | uint8_t buf[BUFSIZ]; |
|---|
| 848 | |
|---|
| 849 | if(opts->child[i].status != STATUS_RUNNING) |
|---|
| 850 | continue; |
|---|
| 851 | |
|---|
| 852 | if(!ZZUF_FD_ISSET(opts->child[i].fd[j], &fdset)) |
|---|
| 853 | continue; |
|---|
| 854 | |
|---|
| 855 | ret = read(opts->child[i].fd[j], buf, BUFSIZ - 1); |
|---|
| 856 | if(ret > 0) |
|---|
| 857 | { |
|---|
| 858 | /* We got data */ |
|---|
| 859 | if(j != 0) |
|---|
| 860 | opts->child[i].bytes += ret; |
|---|
| 861 | |
|---|
| 862 | if(opts->md5 && j == 2) |
|---|
| 863 | _zz_md5_add(opts->child[i].ctx, buf, ret); |
|---|
| 864 | else if(!opts->quiet || j == 0) |
|---|
| 865 | write((j < 2) ? STDERR_FILENO : STDOUT_FILENO, buf, ret); |
|---|
| 866 | } |
|---|
| 867 | else if(ret == 0) |
|---|
| 868 | { |
|---|
| 869 | /* End of file reached */ |
|---|
| 870 | close(opts->child[i].fd[j]); |
|---|
| 871 | opts->child[i].fd[j] = -1; |
|---|
| 872 | |
|---|
| 873 | if(opts->child[i].fd[0] == -1 |
|---|
| 874 | && opts->child[i].fd[1] == -1 |
|---|
| 875 | && opts->child[i].fd[2] == -1) |
|---|
| 876 | opts->child[i].status = STATUS_EOF; |
|---|
| 877 | } |
|---|
| 878 | } |
|---|
| 879 | } |
|---|
| 880 | |
|---|
| 881 | #if !defined HAVE_SETENV |
|---|
| 882 | static void setenv(char const *name, char const *value, int overwrite) |
|---|
| 883 | { |
|---|
| 884 | char *str; |
|---|
| 885 | |
|---|
| 886 | if(!overwrite && getenv(name)) |
|---|
| 887 | return; |
|---|
| 888 | |
|---|
| 889 | str = malloc(strlen(name) + 1 + strlen(value) + 1); |
|---|
| 890 | sprintf(str, "%s=%s", name, value); |
|---|
| 891 | putenv(str); |
|---|
| 892 | } |
|---|
| 893 | #endif |
|---|
| 894 | |
|---|
| 895 | #if defined HAVE_WAITPID |
|---|
| 896 | static char const *sig2name(int signum) |
|---|
| 897 | { |
|---|
| 898 | switch(signum) |
|---|
| 899 | { |
|---|
| 900 | #ifdef SIGQUIT |
|---|
| 901 | case SIGQUIT: return " (SIGQUIT)"; /* 3 */ |
|---|
| 902 | #endif |
|---|
| 903 | case SIGILL: return " (SIGILL)"; /* 4 */ |
|---|
| 904 | #ifdef SIGTRAP |
|---|
| 905 | case SIGTRAP: return " (SIGTRAP)"; /* 5 */ |
|---|
| 906 | #endif |
|---|
| 907 | case SIGABRT: return " (SIGABRT)"; /* 6 */ |
|---|
| 908 | #ifdef SIGBUS |
|---|
| 909 | case SIGBUS: return " (SIGBUS)"; /* 7 */ |
|---|
| 910 | #endif |
|---|
| 911 | case SIGFPE: return " (SIGFPE)"; /* 8 */ |
|---|
| 912 | case SIGSEGV: return " (SIGSEGV)"; /* 11 */ |
|---|
| 913 | case SIGPIPE: return " (SIGPIPE)"; /* 13 */ |
|---|
| 914 | #ifdef SIGEMT |
|---|
| 915 | case SIGEMT: return " (SIGEMT)"; /* ? */ |
|---|
| 916 | #endif |
|---|
| 917 | #ifdef SIGXCPU |
|---|
| 918 | case SIGXCPU: return " (SIGXCPU)"; /* 24 */ |
|---|
| 919 | #endif |
|---|
| 920 | #ifdef SIGXFSZ |
|---|
| 921 | case SIGXFSZ: return " (SIGXFSZ)"; /* 25 */ |
|---|
| 922 | #endif |
|---|
| 923 | #ifdef SIGSYS |
|---|
| 924 | case SIGSYS: return " (SIGSYS)"; /* 31 */ |
|---|
| 925 | #endif |
|---|
| 926 | } |
|---|
| 927 | |
|---|
| 928 | return ""; |
|---|
| 929 | } |
|---|
| 930 | #endif |
|---|
| 931 | |
|---|
| 932 | static void version(void) |
|---|
| 933 | { |
|---|
| 934 | printf("zzuf %s\n", PACKAGE_VERSION); |
|---|
| 935 | printf("Copyright (C) 2002-2010 Sam Hocevar <sam@hocevar.net>\n"); |
|---|
| 936 | printf("This program is free software. It comes without any warranty, to the extent\n"); |
|---|
| 937 | printf("permitted by applicable law. You can redistribute it and/or modify it under\n"); |
|---|
| 938 | printf("the terms of the Do What The Fuck You Want To Public License, Version 2, as\n"); |
|---|
| 939 | printf("published by Sam Hocevar. See <http://sam.zoy.org/wtfpl/> for more details.\n"); |
|---|
| 940 | printf("\n"); |
|---|
| 941 | printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n"); |
|---|
| 942 | } |
|---|
| 943 | |
|---|
| 944 | static void usage(void) |
|---|
| 945 | { |
|---|
| 946 | #if defined HAVE_REGEX_H |
|---|
| 947 | printf("Usage: zzuf [-AcdimnqSvx] [-s seed|-s start:stop] [-r ratio|-r min:max]\n"); |
|---|
| 948 | #else |
|---|
| 949 | printf("Usage: zzuf [-AdimnqSvx] [-s seed|-s start:stop] [-r ratio|-r min:max]\n"); |
|---|
| 950 | #endif |
|---|
| 951 | printf(" [-f mode] [-D delay] [-j jobs] [-C crashes] [-B bytes] [-a list]\n"); |
|---|
| 952 | printf(" [-t seconds]"); |
|---|
| 953 | #if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_CPU |
|---|
| 954 | printf( " [-T seconds]"); |
|---|
| 955 | #endif |
|---|
| 956 | #if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_MEM |
|---|
| 957 | printf( " [-M mebibytes]"); |
|---|
| 958 | #endif |
|---|
| 959 | printf( " [-b ranges] [-p ports]\n"); |
|---|
| 960 | printf(" [-P protect] [-R refuse] [-l list]"); |
|---|
| 961 | #if defined HAVE_REGEX_H |
|---|
| 962 | printf( " [-I include] [-E exclude]"); |
|---|
| 963 | #endif |
|---|
| 964 | printf("\n"); |
|---|
| 965 | printf(" [PROGRAM [--] [ARGS]...]\n"); |
|---|
| 966 | printf(" zzuf -h | --help\n"); |
|---|
| 967 | printf(" zzuf -V | --version\n"); |
|---|
| 968 | printf("Run PROGRAM with optional arguments ARGS and fuzz its input.\n"); |
|---|
| 969 | printf("\n"); |
|---|
| 970 | printf("Mandatory arguments to long options are mandatory for short options too.\n"); |
|---|
| 971 | printf(" -a, --allow <list> only fuzz network input for IPs in <list>\n"); |
|---|
| 972 | printf(" ... !<list> do not fuzz network input for IPs in <list>\n"); |
|---|
| 973 | printf(" -A, --autoinc increment seed each time a new file is opened\n"); |
|---|
| 974 | printf(" -b, --bytes <ranges> only fuzz bytes at offsets within <ranges>\n"); |
|---|
| 975 | printf(" -B, --max-bytes <n> kill children that output more than <n> bytes\n"); |
|---|
| 976 | #if defined HAVE_REGEX_H |
|---|
| 977 | printf(" -c, --cmdline only fuzz files specified in the command line\n"); |
|---|
| 978 | #endif |
|---|
| 979 | printf(" -C, --max-crashes <n> stop after <n> children have crashed (default 1)\n"); |
|---|
| 980 | printf(" -d, --debug print debug messages (twice for more verbosity)\n"); |
|---|
| 981 | printf(" -D, --delay delay between forks\n"); |
|---|
| 982 | #if defined HAVE_REGEX_H |
|---|
| 983 | printf(" -E, --exclude <regex> do not fuzz files matching <regex>\n"); |
|---|
| 984 | #endif |
|---|
| 985 | printf(" -f, --fuzzing <mode> use fuzzing mode <mode> ([xor] set unset)\n"); |
|---|
| 986 | printf(" -i, --stdin fuzz standard input\n"); |
|---|
| 987 | #if defined HAVE_REGEX_H |
|---|
| 988 | printf(" -I, --include <regex> only fuzz files matching <regex>\n"); |
|---|
| 989 | #endif |
|---|
| 990 | printf(" -j, --jobs <n> number of simultaneous jobs (default 1)\n"); |
|---|
| 991 | printf(" -l, --list <list> only fuzz Nth descriptor with N in <list>\n"); |
|---|
| 992 | printf(" -m, --md5 compute the output's MD5 hash\n"); |
|---|
| 993 | #if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_MEM |
|---|
| 994 | printf(" -M, --max-memory <n> maximum child virtual memory in MiB (default %u)\n", DEFAULT_MEM); |
|---|
| 995 | #endif |
|---|
| 996 | printf(" -n, --network fuzz network input\n"); |
|---|
| 997 | printf(" -p, --ports <list> only fuzz network destination ports in <list>\n"); |
|---|
| 998 | printf(" -P, --protect <list> protect bytes and characters in <list>\n"); |
|---|
| 999 | printf(" -q, --quiet do not print children's messages\n"); |
|---|
| 1000 | printf(" -r, --ratio <ratio> bit fuzzing ratio (default %g)\n", DEFAULT_RATIO); |
|---|
| 1001 | printf(" ... <start:stop> specify a ratio range\n"); |
|---|
| 1002 | printf(" -R, --refuse <list> refuse bytes and characters in <list>\n"); |
|---|
| 1003 | printf(" -s, --seed <seed> random seed (default %i)\n", DEFAULT_SEED); |
|---|
| 1004 | printf(" ... <start:stop> specify a seed range\n"); |
|---|
| 1005 | printf(" -S, --signal prevent children from diverting crashing signals\n"); |
|---|
| 1006 | printf(" -t, --max-time <n> stop spawning children after <n> seconds\n"); |
|---|
| 1007 | printf(" -T, --max-cputime <n> kill children that use more than <n> CPU seconds\n"); |
|---|
| 1008 | printf(" -U, --max-usertime <n> kill children that run for more than <n> seconds\n"); |
|---|
| 1009 | printf(" -v, --verbose print information during the run\n"); |
|---|
| 1010 | printf(" -x, --check-exit report processes that exit with a non-zero status\n"); |
|---|
| 1011 | printf(" -h, --help display this help and exit\n"); |
|---|
| 1012 | printf(" -V, --version output version information and exit\n"); |
|---|
| 1013 | printf("\n"); |
|---|
| 1014 | printf("Written by Sam Hocevar. Report bugs to <sam@hocevar.net>.\n"); |
|---|
| 1015 | } |
|---|
| 1016 | |
|---|