| 1 | /* |
|---|
| 2 | * zzuf - general purpose fuzzer |
|---|
| 3 | * Copyright (c) 2006 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 | #include <unistd.h> |
|---|
| 32 | #include <regex.h> |
|---|
| 33 | #include <string.h> |
|---|
| 34 | #include <signal.h> |
|---|
| 35 | #include <errno.h> |
|---|
| 36 | #include <sys/time.h> |
|---|
| 37 | #include <time.h> |
|---|
| 38 | #include <sys/wait.h> |
|---|
| 39 | |
|---|
| 40 | #include "random.h" |
|---|
| 41 | #include "libzzuf.h" |
|---|
| 42 | |
|---|
| 43 | static void spawn_child(char **); |
|---|
| 44 | static void clean_children(void); |
|---|
| 45 | static void read_children(void); |
|---|
| 46 | |
|---|
| 47 | static char *merge_regex(char *, char *); |
|---|
| 48 | static char *merge_file(char *, char *); |
|---|
| 49 | static void set_environment(char const *); |
|---|
| 50 | static void version(void); |
|---|
| 51 | #if defined(HAVE_GETOPT_H) |
|---|
| 52 | static void usage(void); |
|---|
| 53 | #endif |
|---|
| 54 | |
|---|
| 55 | static struct child_list |
|---|
| 56 | { |
|---|
| 57 | enum status |
|---|
| 58 | { |
|---|
| 59 | STATUS_FREE, |
|---|
| 60 | STATUS_RUNNING, |
|---|
| 61 | STATUS_SIGTERM, |
|---|
| 62 | STATUS_SIGKILL, |
|---|
| 63 | STATUS_EOF, |
|---|
| 64 | } status; |
|---|
| 65 | |
|---|
| 66 | pid_t pid; |
|---|
| 67 | int fd[3]; /* 0 is debug, 1 is stderr, 2 is stdout */ |
|---|
| 68 | int bytes, seed; |
|---|
| 69 | time_t date; |
|---|
| 70 | } *child_list; |
|---|
| 71 | static int parallel = 1, child_count = 0; |
|---|
| 72 | |
|---|
| 73 | static int seed = 0; |
|---|
| 74 | static int endseed = 1; |
|---|
| 75 | static int quiet = 0; |
|---|
| 76 | static int maxbytes = -1; |
|---|
| 77 | static double maxtime = -1.0; |
|---|
| 78 | |
|---|
| 79 | #define ZZUF_FD_SET(fd, p_fdset, maxfd) \ |
|---|
| 80 | if(fd >= 0) \ |
|---|
| 81 | { \ |
|---|
| 82 | FD_SET(fd, p_fdset); \ |
|---|
| 83 | if(fd > maxfd) \ |
|---|
| 84 | maxfd = fd; \ |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | #define ZZUF_FD_ISSET(fd, p_fdset) \ |
|---|
| 88 | ((fd >= 0) && (FD_ISSET(fd, p_fdset))) |
|---|
| 89 | |
|---|
| 90 | int main(int argc, char *argv[]) |
|---|
| 91 | { |
|---|
| 92 | char **newargv; |
|---|
| 93 | char *parser, *include = NULL, *exclude = NULL; |
|---|
| 94 | int i, cmdline = 0; |
|---|
| 95 | |
|---|
| 96 | #if defined(HAVE_GETOPT_H) |
|---|
| 97 | for(;;) |
|---|
| 98 | { |
|---|
| 99 | # ifdef HAVE_GETOPT_LONG |
|---|
| 100 | # define MOREINFO "Try `%s --help' for more information.\n" |
|---|
| 101 | int option_index = 0; |
|---|
| 102 | static struct option long_options[] = |
|---|
| 103 | { |
|---|
| 104 | /* Long option, needs arg, flag, short option */ |
|---|
| 105 | { "max-bytes", 1, NULL, 'B' }, |
|---|
| 106 | { "cmdline", 0, NULL, 'c' }, |
|---|
| 107 | { "debug", 0, NULL, 'd' }, |
|---|
| 108 | { "exclude", 1, NULL, 'E' }, |
|---|
| 109 | { "fork", 1, NULL, 'F' }, |
|---|
| 110 | { "help", 0, NULL, 'h' }, |
|---|
| 111 | { "stdin", 0, NULL, 'i' }, |
|---|
| 112 | { "include", 1, NULL, 'I' }, |
|---|
| 113 | { "protect", 1, NULL, 'P' }, |
|---|
| 114 | { "quiet", 0, NULL, 'q' }, |
|---|
| 115 | { "ratio", 1, NULL, 'r' }, |
|---|
| 116 | { "seed", 1, NULL, 's' }, |
|---|
| 117 | { "signal", 0, NULL, 'S' }, |
|---|
| 118 | { "max-time", 1, NULL, 'T' }, |
|---|
| 119 | { "version", 0, NULL, 'v' }, |
|---|
| 120 | }; |
|---|
| 121 | int c = getopt_long(argc, argv, "B:cdE:F:hiI:P:qr:s:ST:v", |
|---|
| 122 | long_options, &option_index); |
|---|
| 123 | # else |
|---|
| 124 | # define MOREINFO "Try `%s -h' for more information.\n" |
|---|
| 125 | int c = getopt(argc, argv, "B:cdE:F:hiI:P:qr:s:ST:v"); |
|---|
| 126 | # endif |
|---|
| 127 | if(c == -1) |
|---|
| 128 | break; |
|---|
| 129 | |
|---|
| 130 | switch(c) |
|---|
| 131 | { |
|---|
| 132 | case 'I': /* --include */ |
|---|
| 133 | include = merge_regex(include, optarg); |
|---|
| 134 | if(!include) |
|---|
| 135 | { |
|---|
| 136 | printf("%s: invalid regex -- `%s'\n", argv[0], optarg); |
|---|
| 137 | return EXIT_FAILURE; |
|---|
| 138 | } |
|---|
| 139 | break; |
|---|
| 140 | case 'E': /* --exclude */ |
|---|
| 141 | exclude = merge_regex(exclude, optarg); |
|---|
| 142 | if(!exclude) |
|---|
| 143 | { |
|---|
| 144 | printf("%s: invalid regex -- `%s'\n", argv[0], optarg); |
|---|
| 145 | return EXIT_FAILURE; |
|---|
| 146 | } |
|---|
| 147 | break; |
|---|
| 148 | case 'c': /* --cmdline */ |
|---|
| 149 | cmdline = 1; |
|---|
| 150 | break; |
|---|
| 151 | case 'i': /* --stdin */ |
|---|
| 152 | setenv("ZZUF_STDIN", "1", 1); |
|---|
| 153 | break; |
|---|
| 154 | case 's': /* --seed */ |
|---|
| 155 | parser = strchr(optarg, ':'); |
|---|
| 156 | seed = atoi(optarg); |
|---|
| 157 | endseed = parser ? atoi(parser + 1) : seed + 1; |
|---|
| 158 | break; |
|---|
| 159 | case 'r': /* --ratio */ |
|---|
| 160 | setenv("ZZUF_RATIO", optarg, 1); |
|---|
| 161 | break; |
|---|
| 162 | case 'F': /* --fork */ |
|---|
| 163 | parallel = atoi(optarg) > 1 ? atoi(optarg) : 1; |
|---|
| 164 | break; |
|---|
| 165 | case 'B': /* --max-bytes */ |
|---|
| 166 | maxbytes = atoi(optarg); |
|---|
| 167 | break; |
|---|
| 168 | case 'T': /* --max-time */ |
|---|
| 169 | maxtime = atof(optarg); |
|---|
| 170 | break; |
|---|
| 171 | case 'P': /* --protect */ |
|---|
| 172 | setenv("ZZUF_PROTECT", optarg, 1); |
|---|
| 173 | break; |
|---|
| 174 | case 'q': /* --quiet */ |
|---|
| 175 | quiet = 1; |
|---|
| 176 | break; |
|---|
| 177 | case 'S': /* --signal */ |
|---|
| 178 | setenv("ZZUF_SIGNAL", "1", 1); |
|---|
| 179 | break; |
|---|
| 180 | case 'd': /* --debug */ |
|---|
| 181 | setenv("ZZUF_DEBUG", "1", 1); |
|---|
| 182 | break; |
|---|
| 183 | case 'h': /* --help */ |
|---|
| 184 | usage(); |
|---|
| 185 | return 0; |
|---|
| 186 | case 'v': /* --version */ |
|---|
| 187 | version(); |
|---|
| 188 | return 0; |
|---|
| 189 | default: |
|---|
| 190 | printf("%s: invalid option -- %c\n", argv[0], c); |
|---|
| 191 | printf(MOREINFO, argv[0]); |
|---|
| 192 | return EXIT_FAILURE; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | #else |
|---|
| 196 | # define MOREINFO "Usage: %s message...\n" |
|---|
| 197 | int optind = 1; |
|---|
| 198 | #endif |
|---|
| 199 | |
|---|
| 200 | if(optind >= argc) |
|---|
| 201 | { |
|---|
| 202 | printf("%s: missing argument\n", argv[0]); |
|---|
| 203 | printf(MOREINFO, argv[0]); |
|---|
| 204 | return EXIT_FAILURE; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | if(cmdline) |
|---|
| 208 | { |
|---|
| 209 | int dashdash = 0; |
|---|
| 210 | |
|---|
| 211 | for(i = optind + 1; i < argc; i++) |
|---|
| 212 | { |
|---|
| 213 | if(dashdash) |
|---|
| 214 | include = merge_file(include, argv[i]); |
|---|
| 215 | else if(!strcmp("--", argv[i])) |
|---|
| 216 | dashdash = 1; |
|---|
| 217 | else if(argv[i][0] != '-') |
|---|
| 218 | include = merge_file(include, argv[i]); |
|---|
| 219 | } |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | if(include) |
|---|
| 223 | setenv("ZZUF_INCLUDE", include, 1); |
|---|
| 224 | if(exclude) |
|---|
| 225 | setenv("ZZUF_EXCLUDE", exclude, 1); |
|---|
| 226 | |
|---|
| 227 | /* Allocate memory for children handling */ |
|---|
| 228 | child_list = malloc(parallel * sizeof(struct child_list)); |
|---|
| 229 | for(i = 0; i < parallel; i++) |
|---|
| 230 | child_list[i].status = STATUS_FREE; |
|---|
| 231 | child_count = 0; |
|---|
| 232 | |
|---|
| 233 | /* Preload libzzuf.so */ |
|---|
| 234 | set_environment(argv[0]); |
|---|
| 235 | |
|---|
| 236 | /* Create new argv */ |
|---|
| 237 | newargv = malloc((argc - optind + 1) * sizeof(char *)); |
|---|
| 238 | memcpy(newargv, argv + optind, (argc - optind) * sizeof(char *)); |
|---|
| 239 | newargv[argc - optind] = (char *)NULL; |
|---|
| 240 | |
|---|
| 241 | /* Handle children in our way */ |
|---|
| 242 | signal(SIGCHLD, SIG_DFL); |
|---|
| 243 | |
|---|
| 244 | /* Main loop */ |
|---|
| 245 | while(child_count || seed < endseed) |
|---|
| 246 | { |
|---|
| 247 | /* Spawn one new child, if necessary */ |
|---|
| 248 | if(child_count < parallel && seed < endseed) |
|---|
| 249 | spawn_child(newargv); |
|---|
| 250 | |
|---|
| 251 | /* Cleanup dead or dying children */ |
|---|
| 252 | clean_children(); |
|---|
| 253 | |
|---|
| 254 | /* Read data from children */ |
|---|
| 255 | read_children(); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /* Clean up */ |
|---|
| 259 | free(newargv); |
|---|
| 260 | free(child_list); |
|---|
| 261 | |
|---|
| 262 | return EXIT_SUCCESS; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | static char *merge_file(char *regex, char *file) |
|---|
| 266 | { |
|---|
| 267 | char *newfile = malloc(1 + 2 * strlen(file) + 1 + 1), *tmp = newfile; |
|---|
| 268 | |
|---|
| 269 | *tmp++ = '^'; |
|---|
| 270 | while(*file) |
|---|
| 271 | { |
|---|
| 272 | if(strchr("^.[$()|*+?{\\", *file)) |
|---|
| 273 | *tmp++ = '\\'; |
|---|
| 274 | *tmp++ = *file++; |
|---|
| 275 | } |
|---|
| 276 | *tmp++ = '$'; |
|---|
| 277 | *tmp++ = '\0'; |
|---|
| 278 | |
|---|
| 279 | tmp = merge_regex(regex, newfile); |
|---|
| 280 | free(newfile); |
|---|
| 281 | return tmp; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | static char *merge_regex(char *regex, char *string) |
|---|
| 285 | { |
|---|
| 286 | regex_t optre; |
|---|
| 287 | |
|---|
| 288 | if(regex) |
|---|
| 289 | { |
|---|
| 290 | regex = realloc(regex, strlen(regex) + strlen(string) + 1 + 1); |
|---|
| 291 | sprintf(regex + strlen(regex) - 1, "|%s)", string); |
|---|
| 292 | } |
|---|
| 293 | else |
|---|
| 294 | { |
|---|
| 295 | regex = malloc(1 + strlen(string) + 1 + 1); |
|---|
| 296 | sprintf(regex, "(%s)", string); |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | if(regcomp(&optre, regex, REG_EXTENDED) != 0) |
|---|
| 300 | { |
|---|
| 301 | free(regex); |
|---|
| 302 | return NULL; |
|---|
| 303 | } |
|---|
| 304 | regfree(&optre); |
|---|
| 305 | |
|---|
| 306 | return regex; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | static void spawn_child(char **argv) |
|---|
| 310 | { |
|---|
| 311 | static int const files[] = { DEBUG_FILENO, STDERR_FILENO, STDOUT_FILENO }; |
|---|
| 312 | char buf[BUFSIZ]; |
|---|
| 313 | int fd[3][2]; |
|---|
| 314 | pid_t pid; |
|---|
| 315 | int i, j; |
|---|
| 316 | |
|---|
| 317 | /* Find an empty slot */ |
|---|
| 318 | for(i = 0; i < parallel; i++) |
|---|
| 319 | if(child_list[i].status == STATUS_FREE) |
|---|
| 320 | break; |
|---|
| 321 | |
|---|
| 322 | /* Prepare communication pipe */ |
|---|
| 323 | for(j = 0; j < 3; j++) |
|---|
| 324 | if(pipe(fd[j]) == -1) |
|---|
| 325 | { |
|---|
| 326 | perror("pipe"); |
|---|
| 327 | return; |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | /* Fork and launch child */ |
|---|
| 331 | pid = fork(); |
|---|
| 332 | switch(pid) |
|---|
| 333 | { |
|---|
| 334 | case -1: |
|---|
| 335 | perror("fork"); |
|---|
| 336 | return; |
|---|
| 337 | case 0: |
|---|
| 338 | /* We’re the child */ |
|---|
| 339 | for(j = 0; j < 3; j++) |
|---|
| 340 | { |
|---|
| 341 | close(fd[j][0]); |
|---|
| 342 | dup2(fd[j][1], files[j]); |
|---|
| 343 | close(fd[j][1]); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | /* Set environment variables */ |
|---|
| 347 | sprintf(buf, "%i", seed); |
|---|
| 348 | setenv("ZZUF_SEED", buf, 1); |
|---|
| 349 | |
|---|
| 350 | /* Run our process */ |
|---|
| 351 | if(execvp(argv[0], argv)) |
|---|
| 352 | { |
|---|
| 353 | perror(argv[0]); |
|---|
| 354 | exit(EXIT_FAILURE); |
|---|
| 355 | } |
|---|
| 356 | break; |
|---|
| 357 | default: |
|---|
| 358 | /* We’re the parent, acknowledge spawn */ |
|---|
| 359 | child_list[i].date = time(NULL); |
|---|
| 360 | child_list[i].pid = pid; |
|---|
| 361 | for(j = 0; j < 3; j++) |
|---|
| 362 | { |
|---|
| 363 | close(fd[j][1]); |
|---|
| 364 | child_list[i].fd[j] = fd[j][0]; |
|---|
| 365 | } |
|---|
| 366 | child_list[i].bytes = 0; |
|---|
| 367 | child_list[i].seed = seed; |
|---|
| 368 | child_list[i].status = STATUS_RUNNING; |
|---|
| 369 | child_count++; |
|---|
| 370 | seed++; |
|---|
| 371 | break; |
|---|
| 372 | } |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | static void clean_children(void) |
|---|
| 376 | { |
|---|
| 377 | time_t now = time(NULL); |
|---|
| 378 | int i, j; |
|---|
| 379 | |
|---|
| 380 | /* Terminate children if necessary */ |
|---|
| 381 | for(i = 0; i < parallel; i++) |
|---|
| 382 | { |
|---|
| 383 | if(child_list[i].status == STATUS_RUNNING |
|---|
| 384 | && maxbytes >= 0 && child_list[i].bytes > maxbytes) |
|---|
| 385 | { |
|---|
| 386 | fprintf(stdout, "seed %i: data exceeded, sending SIGTERM\n", |
|---|
| 387 | child_list[i].seed); |
|---|
| 388 | kill(child_list[i].pid, SIGTERM); |
|---|
| 389 | child_list[i].date = now; |
|---|
| 390 | child_list[i].status = STATUS_SIGTERM; |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | if(child_list[i].status == STATUS_RUNNING |
|---|
| 394 | && maxtime >= 0.0 |
|---|
| 395 | && difftime(now, child_list[i].date) > maxtime) |
|---|
| 396 | { |
|---|
| 397 | fprintf(stdout, "seed %i: time exceeded, sending SIGTERM\n", |
|---|
| 398 | child_list[i].seed); |
|---|
| 399 | kill(child_list[i].pid, SIGTERM); |
|---|
| 400 | child_list[i].date = now; |
|---|
| 401 | child_list[i].status = STATUS_SIGTERM; |
|---|
| 402 | } |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | /* Kill children if necessary */ |
|---|
| 406 | for(i = 0; i < parallel; i++) |
|---|
| 407 | { |
|---|
| 408 | if(child_list[i].status == STATUS_SIGTERM |
|---|
| 409 | && difftime(now, child_list[i].date) > 2.0) |
|---|
| 410 | { |
|---|
| 411 | fprintf(stdout, "seed %i: not responding, sending SIGKILL\n", |
|---|
| 412 | child_list[i].seed); |
|---|
| 413 | kill(child_list[i].pid, SIGKILL); |
|---|
| 414 | child_list[i].status = STATUS_SIGKILL; |
|---|
| 415 | } |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | /* Collect dead children */ |
|---|
| 419 | for(i = 0; i < parallel; i++) |
|---|
| 420 | { |
|---|
| 421 | int status; |
|---|
| 422 | pid_t pid; |
|---|
| 423 | |
|---|
| 424 | if(child_list[i].status != STATUS_SIGKILL |
|---|
| 425 | && child_list[i].status != STATUS_SIGTERM |
|---|
| 426 | && child_list[i].status != STATUS_EOF) |
|---|
| 427 | continue; |
|---|
| 428 | |
|---|
| 429 | pid = waitpid(child_list[i].pid, &status, WNOHANG); |
|---|
| 430 | if(pid <= 0) |
|---|
| 431 | continue; |
|---|
| 432 | |
|---|
| 433 | if(WIFEXITED(status) && WEXITSTATUS(status)) |
|---|
| 434 | fprintf(stdout, "seed %i: exit %i\n", |
|---|
| 435 | child_list[i].seed, WEXITSTATUS(status)); |
|---|
| 436 | else if(WIFSIGNALED(status)) |
|---|
| 437 | fprintf(stdout, "seed %i: signal %i\n", |
|---|
| 438 | child_list[i].seed, WTERMSIG(status)); |
|---|
| 439 | |
|---|
| 440 | for(j = 0; j < 3; j++) |
|---|
| 441 | if(child_list[i].fd[j] >= 0) |
|---|
| 442 | close(child_list[i].fd[j]); |
|---|
| 443 | |
|---|
| 444 | child_list[i].status = STATUS_FREE; |
|---|
| 445 | child_count--; |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| 448 | fflush(stdout); |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | static void read_children(void) |
|---|
| 452 | { |
|---|
| 453 | struct timeval tv; |
|---|
| 454 | fd_set fdset; |
|---|
| 455 | int i, j, ret, maxfd = 0; |
|---|
| 456 | |
|---|
| 457 | /* Read data from all sockets */ |
|---|
| 458 | FD_ZERO(&fdset); |
|---|
| 459 | for(i = 0; i < parallel; i++) |
|---|
| 460 | { |
|---|
| 461 | if(child_list[i].status != STATUS_RUNNING) |
|---|
| 462 | continue; |
|---|
| 463 | |
|---|
| 464 | for(j = 0; j < 3; j++) |
|---|
| 465 | ZZUF_FD_SET(child_list[i].fd[j], &fdset, maxfd); |
|---|
| 466 | } |
|---|
| 467 | tv.tv_sec = 0; |
|---|
| 468 | tv.tv_usec = 1000; |
|---|
| 469 | |
|---|
| 470 | ret = select(maxfd + 1, &fdset, NULL, NULL, &tv); |
|---|
| 471 | if(ret < 0) |
|---|
| 472 | perror("select"); |
|---|
| 473 | if(ret <= 0) |
|---|
| 474 | return; |
|---|
| 475 | |
|---|
| 476 | /* XXX: cute (i, j) iterating hack */ |
|---|
| 477 | for(i = 0, j = 0; i < parallel; i += (j == 2), j = (j + 1) % 3) |
|---|
| 478 | { |
|---|
| 479 | char buf[BUFSIZ]; |
|---|
| 480 | |
|---|
| 481 | if(child_list[i].status != STATUS_RUNNING) |
|---|
| 482 | continue; |
|---|
| 483 | |
|---|
| 484 | if(!ZZUF_FD_ISSET(child_list[i].fd[j], &fdset)) |
|---|
| 485 | continue; |
|---|
| 486 | |
|---|
| 487 | ret = read(child_list[i].fd[j], buf, BUFSIZ - 1); |
|---|
| 488 | if(ret > 0) |
|---|
| 489 | { |
|---|
| 490 | /* We got data */ |
|---|
| 491 | if(j != 0) |
|---|
| 492 | child_list[i].bytes += ret; |
|---|
| 493 | if(!quiet || j == 0) |
|---|
| 494 | write((j < 2) ? STDERR_FILENO : STDOUT_FILENO, buf, ret); |
|---|
| 495 | } |
|---|
| 496 | else if(ret == 0) |
|---|
| 497 | { |
|---|
| 498 | /* End of file reached */ |
|---|
| 499 | close(child_list[i].fd[j]); |
|---|
| 500 | child_list[i].fd[j] = -1; |
|---|
| 501 | |
|---|
| 502 | if(child_list[i].fd[0] == -1 && child_list[i].fd[1] == -1 |
|---|
| 503 | && child_list[i].fd[2] == -1) |
|---|
| 504 | child_list[i].status = STATUS_EOF; |
|---|
| 505 | } |
|---|
| 506 | } |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | static void set_environment(char const *progpath) |
|---|
| 510 | { |
|---|
| 511 | char *libpath, *tmp; |
|---|
| 512 | int len = strlen(progpath); |
|---|
| 513 | #ifdef __APPLE__ |
|---|
| 514 | # define FILENAME "libzzuf.dylib" |
|---|
| 515 | # define PRELOAD "DYLD_INSERT_LIBRARIES" |
|---|
| 516 | setenv("DYLD_FORCE_FLAT_NAMESPACE", "1", 1); |
|---|
| 517 | #else |
|---|
| 518 | # define FILENAME "libzzuf.so" |
|---|
| 519 | # define PRELOAD "LD_PRELOAD" |
|---|
| 520 | #endif |
|---|
| 521 | |
|---|
| 522 | libpath = malloc(len + strlen("/.libs/" FILENAME) + 1); |
|---|
| 523 | strcpy(libpath, progpath); |
|---|
| 524 | tmp = strrchr(libpath, '/'); |
|---|
| 525 | strcpy(tmp ? tmp + 1 : libpath, ".libs/" FILENAME); |
|---|
| 526 | if(access(libpath, R_OK) == 0) |
|---|
| 527 | setenv(PRELOAD, libpath, 1); |
|---|
| 528 | else |
|---|
| 529 | setenv(PRELOAD, LIBDIR "/" FILENAME, 1); |
|---|
| 530 | free(libpath); |
|---|
| 531 | } |
|---|
| 532 | |
|---|
| 533 | static void version(void) |
|---|
| 534 | { |
|---|
| 535 | printf("zzuf %s\n", VERSION); |
|---|
| 536 | printf("Copyright (C) 2006 Sam Hocevar <sam@zoy.org>\n"); |
|---|
| 537 | printf("This is free software. You may redistribute copies of it under the\n"); |
|---|
| 538 | printf("terms of the Do What The Fuck You Want To Public License, Version 2\n"); |
|---|
| 539 | printf("<http://sam.zoy.org/wtfpl/>.\n"); |
|---|
| 540 | printf("There is NO WARRANTY, to the extent permitted by law.\n"); |
|---|
| 541 | printf("\n"); |
|---|
| 542 | printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n"); |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | #if defined(HAVE_GETOPT_H) |
|---|
| 546 | static void usage(void) |
|---|
| 547 | { |
|---|
| 548 | printf("Usage: zzuf [ -qdic ] [ -r ratio ] [ -s seed | -s start:stop ]\n"); |
|---|
| 549 | printf(" [ -F children ] [ -B bytes ] [ -T seconds ] [ -P protect ]\n"); |
|---|
| 550 | printf(" [ -I include ] [ -E exclude ] COMMAND [ARGS]...\n"); |
|---|
| 551 | printf(" zzuf -h\n"); |
|---|
| 552 | printf(" zzuf -v\n"); |
|---|
| 553 | printf("Run COMMAND and randomly fuzz its input.\n"); |
|---|
| 554 | printf("\n"); |
|---|
| 555 | printf("Mandatory arguments to long options are mandatory for short options too.\n"); |
|---|
| 556 | # ifdef HAVE_GETOPT_LONG |
|---|
| 557 | printf(" -B, --max-bytes <n> kill children that output more than <n> bytes\n"); |
|---|
| 558 | printf(" -c, --cmdline only fuzz files specified in the command line\n"); |
|---|
| 559 | printf(" -d, --debug print debug messages\n"); |
|---|
| 560 | printf(" -E, --exclude <regex> do not fuzz files matching <regex>\n"); |
|---|
| 561 | printf(" -F, --fork <count> number of concurrent children (default 1)\n"); |
|---|
| 562 | printf(" -i, --stdin fuzz standard input\n"); |
|---|
| 563 | printf(" -I, --include <regex> only fuzz files matching <regex>\n"); |
|---|
| 564 | printf(" -P, --protect <list> protect bytes and characters in <list>\n"); |
|---|
| 565 | printf(" -q, --quiet do not print children's messages\n"); |
|---|
| 566 | printf(" -r, --ratio <ratio> bit fuzzing ratio (default 0.004)\n"); |
|---|
| 567 | printf(" -s, --seed <seed> random seed (default 0)\n"); |
|---|
| 568 | printf(" --seed <start:stop> specify a seed range\n"); |
|---|
| 569 | printf(" -S, --signal prevent children from diverting crashing signals\n"); |
|---|
| 570 | printf(" -T, --max-time <n> kill children that run for more than <n> seconds\n"); |
|---|
| 571 | printf(" -h, --help display this help and exit\n"); |
|---|
| 572 | printf(" -v, --version output version information and exit\n"); |
|---|
| 573 | # else |
|---|
| 574 | printf(" -B <n> kill children that output more than <n> bytes\n"); |
|---|
| 575 | printf(" -c only fuzz files specified in the command line\n"); |
|---|
| 576 | printf(" -d print debug messages\n"); |
|---|
| 577 | printf(" -E <regex> do not fuzz files matching <regex>\n"); |
|---|
| 578 | printf(" -F <count> number of concurrent forks (default 1)\n"); |
|---|
| 579 | printf(" -i fuzz standard input\n"); |
|---|
| 580 | printf(" -I <regex> only fuzz files matching <regex>\n"); |
|---|
| 581 | printf(" -P <list> protect bytes and characters in <list>\n"); |
|---|
| 582 | printf(" -q do not print the fuzzed application's messages\n"); |
|---|
| 583 | printf(" -r <ratio> bit fuzzing ratio (default 0.004)\n"); |
|---|
| 584 | printf(" -s <seed> random seed (default 0)\n"); |
|---|
| 585 | printf(" <start:stop> specify a seed range\n"); |
|---|
| 586 | printf(" -S prevent children from diverting crashing signals\n"); |
|---|
| 587 | printf(" -T <n> kill children that run for more than <n> seconds\n"); |
|---|
| 588 | printf(" -h display this help and exit\n"); |
|---|
| 589 | printf(" -v output version information and exit\n"); |
|---|
| 590 | # endif |
|---|
| 591 | printf("\n"); |
|---|
| 592 | printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n"); |
|---|
| 593 | } |
|---|
| 594 | #endif |
|---|
| 595 | |
|---|