1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2002, 2007 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: zzuf.c 1692 2007-01-17 19:48:23Z sam $ |
---|
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 <errno.h> |
---|
35 | #include <signal.h> |
---|
36 | #include <sys/wait.h> |
---|
37 | #include <sys/time.h> |
---|
38 | #include <sys/resource.h> |
---|
39 | |
---|
40 | #include "libzzuf.h" |
---|
41 | #include "opts.h" |
---|
42 | #include "random.h" |
---|
43 | #include "fd.h" |
---|
44 | #include "fuzz.h" |
---|
45 | #include "md5.h" |
---|
46 | #include "timer.h" |
---|
47 | |
---|
48 | static void loop_stdin(struct opts *opts); |
---|
49 | |
---|
50 | static void spawn_children(struct opts *opts); |
---|
51 | static void clean_children(struct opts *opts); |
---|
52 | static void read_children(struct opts *opts); |
---|
53 | |
---|
54 | static char const *sig2str(int); |
---|
55 | static char *merge_regex(char *, char *); |
---|
56 | static char *merge_file(char *, char *); |
---|
57 | static void set_environment(char const *); |
---|
58 | static void version(void); |
---|
59 | #if defined(HAVE_GETOPT_H) |
---|
60 | static void usage(void); |
---|
61 | #endif |
---|
62 | |
---|
63 | #define ZZUF_FD_SET(fd, p_fdset, maxfd) \ |
---|
64 | if(fd >= 0) \ |
---|
65 | { \ |
---|
66 | FD_SET(fd, p_fdset); \ |
---|
67 | if(fd > maxfd) \ |
---|
68 | maxfd = fd; \ |
---|
69 | } |
---|
70 | |
---|
71 | #define ZZUF_FD_ISSET(fd, p_fdset) \ |
---|
72 | ((fd >= 0) && (FD_ISSET(fd, p_fdset))) |
---|
73 | |
---|
74 | int main(int argc, char *argv[]) |
---|
75 | { |
---|
76 | struct opts _opts, *opts = &_opts; |
---|
77 | char *tmp, *include, *exclude; |
---|
78 | int i, cmdline = 0; |
---|
79 | |
---|
80 | include = exclude = NULL; |
---|
81 | |
---|
82 | _zz_opts_init(opts); |
---|
83 | |
---|
84 | #if defined(HAVE_GETOPT_H) |
---|
85 | for(;;) |
---|
86 | { |
---|
87 | # define OPTSTR "AB:cC:dD:E:F:iI:mM:nP:qr:R:s:ST:vxhV" |
---|
88 | # ifdef HAVE_GETOPT_LONG |
---|
89 | # define MOREINFO "Try `%s --help' for more information.\n" |
---|
90 | int option_index = 0; |
---|
91 | static struct option long_options[] = |
---|
92 | { |
---|
93 | /* Long option, needs arg, flag, short option */ |
---|
94 | { "autoinc", 0, NULL, 'A' }, |
---|
95 | { "max-bytes", 1, NULL, 'B' }, |
---|
96 | { "cmdline", 0, NULL, 'c' }, |
---|
97 | { "max-crashes", 1, NULL, 'C' }, |
---|
98 | { "debug", 0, NULL, 'd' }, |
---|
99 | { "delay", 1, NULL, 'D' }, |
---|
100 | { "exclude", 1, NULL, 'E' }, |
---|
101 | { "max-forks", 1, NULL, 'F' }, |
---|
102 | { "stdin", 0, NULL, 'i' }, |
---|
103 | { "include", 1, NULL, 'I' }, |
---|
104 | { "md5", 0, NULL, 'm' }, |
---|
105 | { "max-memory", 1, NULL, 'M' }, |
---|
106 | { "network", 0, NULL, 'n' }, |
---|
107 | { "protect", 1, NULL, 'P' }, |
---|
108 | { "quiet", 0, NULL, 'q' }, |
---|
109 | { "ratio", 1, NULL, 'r' }, |
---|
110 | { "refuse", 1, NULL, 'R' }, |
---|
111 | { "seed", 1, NULL, 's' }, |
---|
112 | { "signal", 0, NULL, 'S' }, |
---|
113 | { "max-time", 1, NULL, 'T' }, |
---|
114 | { "verbose", 0, NULL, 'v' }, |
---|
115 | { "check-exit", 0, NULL, 'x' }, |
---|
116 | { "help", 0, NULL, 'h' }, |
---|
117 | { "version", 0, NULL, 'V' }, |
---|
118 | { NULL, 0, NULL, 0 } |
---|
119 | }; |
---|
120 | int c = getopt_long(argc, argv, OPTSTR, long_options, &option_index); |
---|
121 | # else |
---|
122 | # define MOREINFO "Try `%s -h' for more information.\n" |
---|
123 | int c = getopt(argc, argv, OPTSTR); |
---|
124 | # endif |
---|
125 | if(c == -1) |
---|
126 | break; |
---|
127 | |
---|
128 | switch(c) |
---|
129 | { |
---|
130 | case 'A': /* --autoinc */ |
---|
131 | setenv("ZZUF_AUTOINC", "1", 1); |
---|
132 | break; |
---|
133 | case 'B': /* --max-bytes */ |
---|
134 | opts->maxbytes = atoi(optarg); |
---|
135 | break; |
---|
136 | case 'c': /* --cmdline */ |
---|
137 | cmdline = 1; |
---|
138 | break; |
---|
139 | case 'C': /* --max-crashes */ |
---|
140 | opts->maxcrashes = atoi(optarg); |
---|
141 | if(opts->maxcrashes <= 0) |
---|
142 | opts->maxcrashes = 0; |
---|
143 | break; |
---|
144 | case 'd': /* --debug */ |
---|
145 | setenv("ZZUF_DEBUG", "1", 1); |
---|
146 | break; |
---|
147 | case 'D': /* --delay */ |
---|
148 | opts->delay = (int64_t)(atof(optarg) * 1000000.0); |
---|
149 | break; |
---|
150 | case 'E': /* --exclude */ |
---|
151 | exclude = merge_regex(exclude, optarg); |
---|
152 | if(!exclude) |
---|
153 | { |
---|
154 | printf("%s: invalid regex -- `%s'\n", argv[0], optarg); |
---|
155 | _zz_opts_fini(opts); |
---|
156 | return EXIT_FAILURE; |
---|
157 | } |
---|
158 | break; |
---|
159 | case 'F': /* --max-forks */ |
---|
160 | opts->maxchild = atoi(optarg) > 1 ? atoi(optarg) : 1; |
---|
161 | break; |
---|
162 | case 'i': /* --stdin */ |
---|
163 | setenv("ZZUF_STDIN", "1", 1); |
---|
164 | break; |
---|
165 | case 'I': /* --include */ |
---|
166 | include = merge_regex(include, optarg); |
---|
167 | if(!include) |
---|
168 | { |
---|
169 | printf("%s: invalid regex -- `%s'\n", argv[0], optarg); |
---|
170 | _zz_opts_fini(opts); |
---|
171 | return EXIT_FAILURE; |
---|
172 | } |
---|
173 | break; |
---|
174 | case 'm': /* --md5 */ |
---|
175 | opts->md5 = 1; |
---|
176 | break; |
---|
177 | case 'M': /* --max-memory */ |
---|
178 | setenv("ZZUF_MEMORY", "1", 1); |
---|
179 | opts->maxmem = atoi(optarg); |
---|
180 | break; |
---|
181 | case 'n': /* --network */ |
---|
182 | setenv("ZZUF_NETWORK", "1", 1); |
---|
183 | break; |
---|
184 | case 'P': /* --protect */ |
---|
185 | opts->protect = optarg; |
---|
186 | break; |
---|
187 | case 'q': /* --quiet */ |
---|
188 | opts->quiet = 1; |
---|
189 | break; |
---|
190 | case 'r': /* --ratio */ |
---|
191 | tmp = strchr(optarg, ':'); |
---|
192 | opts->minratio = atof(optarg); |
---|
193 | opts->maxratio = tmp ? atof(tmp + 1) : opts->minratio; |
---|
194 | break; |
---|
195 | case 'R': /* --refuse */ |
---|
196 | opts->refuse = optarg; |
---|
197 | break; |
---|
198 | case 's': /* --seed */ |
---|
199 | tmp = strchr(optarg, ':'); |
---|
200 | opts->seed = atol(optarg); |
---|
201 | opts->endseed = tmp ? (uint32_t)atoi(tmp + 1) : opts->seed + 1; |
---|
202 | break; |
---|
203 | case 'S': /* --signal */ |
---|
204 | setenv("ZZUF_SIGNAL", "1", 1); |
---|
205 | break; |
---|
206 | case 'T': /* --max-time */ |
---|
207 | opts->maxtime = (int64_t)(atof(optarg) * 1000000.0); |
---|
208 | break; |
---|
209 | case 'x': /* --check-exit */ |
---|
210 | opts->checkexit = 1; |
---|
211 | break; |
---|
212 | case 'v': /* --verbose */ |
---|
213 | opts->verbose = 1; |
---|
214 | break; |
---|
215 | case 'h': /* --help */ |
---|
216 | usage(); |
---|
217 | _zz_opts_fini(opts); |
---|
218 | return 0; |
---|
219 | case 'V': /* --version */ |
---|
220 | version(); |
---|
221 | _zz_opts_fini(opts); |
---|
222 | return 0; |
---|
223 | default: |
---|
224 | printf("%s: invalid option -- %c\n", argv[0], c); |
---|
225 | printf(MOREINFO, argv[0]); |
---|
226 | _zz_opts_fini(opts); |
---|
227 | return EXIT_FAILURE; |
---|
228 | } |
---|
229 | } |
---|
230 | #else |
---|
231 | # define MOREINFO "Usage: %s message...\n" |
---|
232 | int optind = 1; |
---|
233 | #endif |
---|
234 | |
---|
235 | _zz_setratio(opts->minratio, opts->maxratio); |
---|
236 | _zz_setseed(opts->seed); |
---|
237 | |
---|
238 | /* If asked to read from the standard input */ |
---|
239 | if(optind >= argc) |
---|
240 | { |
---|
241 | if(opts->endseed != opts->seed + 1) |
---|
242 | { |
---|
243 | printf("%s: seed ranges are incompatible with stdin fuzzing\n", |
---|
244 | argv[0]); |
---|
245 | printf(MOREINFO, argv[0]); |
---|
246 | _zz_opts_fini(opts); |
---|
247 | return EXIT_FAILURE; |
---|
248 | } |
---|
249 | |
---|
250 | loop_stdin(opts); |
---|
251 | |
---|
252 | _zz_opts_fini(opts); |
---|
253 | return EXIT_SUCCESS; |
---|
254 | } |
---|
255 | |
---|
256 | /* If asked to launch programs */ |
---|
257 | if(cmdline) |
---|
258 | { |
---|
259 | int dashdash = 0; |
---|
260 | |
---|
261 | for(i = optind + 1; i < argc; i++) |
---|
262 | { |
---|
263 | if(dashdash) |
---|
264 | include = merge_file(include, argv[i]); |
---|
265 | else if(!strcmp("--", argv[i])) |
---|
266 | dashdash = 1; |
---|
267 | else if(argv[i][0] != '-') |
---|
268 | include = merge_file(include, argv[i]); |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | if(include) |
---|
273 | setenv("ZZUF_INCLUDE", include, 1); |
---|
274 | if(exclude) |
---|
275 | setenv("ZZUF_EXCLUDE", exclude, 1); |
---|
276 | if(opts->protect) |
---|
277 | setenv("ZZUF_PROTECT", opts->protect, 1); |
---|
278 | if(opts->refuse) |
---|
279 | setenv("ZZUF_REFUSE", opts->refuse, 1); |
---|
280 | |
---|
281 | /* Allocate memory for children handling */ |
---|
282 | opts->child = malloc(opts->maxchild * sizeof(struct child)); |
---|
283 | for(i = 0; i < opts->maxchild; i++) |
---|
284 | opts->child[i].status = STATUS_FREE; |
---|
285 | opts->nchild = 0; |
---|
286 | |
---|
287 | /* Preload libzzuf.so */ |
---|
288 | set_environment(argv[0]); |
---|
289 | |
---|
290 | /* Create new argv */ |
---|
291 | opts->newargv = malloc((argc - optind + 1) * sizeof(char *)); |
---|
292 | memcpy(opts->newargv, argv + optind, (argc - optind) * sizeof(char *)); |
---|
293 | opts->newargv[argc - optind] = (char *)NULL; |
---|
294 | |
---|
295 | /* Main loop */ |
---|
296 | while(opts->nchild || opts->seed < opts->endseed) |
---|
297 | { |
---|
298 | /* Spawn new children, if necessary */ |
---|
299 | spawn_children(opts); |
---|
300 | |
---|
301 | /* Cleanup dead or dying children */ |
---|
302 | clean_children(opts); |
---|
303 | |
---|
304 | /* Read data from children */ |
---|
305 | read_children(opts); |
---|
306 | |
---|
307 | if(opts->maxcrashes && opts->crashes >= opts->maxcrashes |
---|
308 | && opts->nchild == 0) |
---|
309 | break; |
---|
310 | } |
---|
311 | |
---|
312 | /* Clean up */ |
---|
313 | _zz_opts_fini(opts); |
---|
314 | |
---|
315 | return opts->crashes ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
316 | } |
---|
317 | |
---|
318 | static void loop_stdin(struct opts *opts) |
---|
319 | { |
---|
320 | uint8_t md5sum[16]; |
---|
321 | struct md5 *ctx = NULL; |
---|
322 | |
---|
323 | if(opts->md5) |
---|
324 | ctx = _zz_md5_init(); |
---|
325 | |
---|
326 | if(opts->protect) |
---|
327 | _zz_protect(opts->protect); |
---|
328 | if(opts->refuse) |
---|
329 | _zz_refuse(opts->refuse); |
---|
330 | |
---|
331 | _zz_fd_init(); |
---|
332 | _zz_register(0); |
---|
333 | |
---|
334 | for(;;) |
---|
335 | { |
---|
336 | uint8_t buf[BUFSIZ]; |
---|
337 | int ret, off = 0, nw = 0; |
---|
338 | |
---|
339 | ret = read(0, buf, BUFSIZ); |
---|
340 | if(ret <= 0) |
---|
341 | break; |
---|
342 | |
---|
343 | _zz_fuzz(0, buf, ret); |
---|
344 | _zz_addpos(0, ret); |
---|
345 | |
---|
346 | if(opts->md5) |
---|
347 | _zz_md5_add(ctx, buf, ret); |
---|
348 | else while(ret) |
---|
349 | { |
---|
350 | if((nw = write(1, buf + off, (size_t)ret)) < 0) |
---|
351 | break; |
---|
352 | ret -= nw; |
---|
353 | off += nw; |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | if(opts->md5) |
---|
358 | { |
---|
359 | _zz_md5_fini(md5sum, ctx); |
---|
360 | fprintf(stdout, "zzuf[s=%i,r=%g]: %.02x%.02x%.02x%.02x%.02x" |
---|
361 | "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x\n", |
---|
362 | opts->seed, opts->minratio, md5sum[0], md5sum[1], md5sum[2], |
---|
363 | md5sum[3], md5sum[4], md5sum[5], md5sum[6], md5sum[7], |
---|
364 | md5sum[8], md5sum[9], md5sum[10], md5sum[11], md5sum[12], |
---|
365 | md5sum[13], md5sum[14], md5sum[15]); |
---|
366 | fflush(stdout); |
---|
367 | } |
---|
368 | |
---|
369 | _zz_unregister(0); |
---|
370 | _zz_fd_fini(); |
---|
371 | } |
---|
372 | |
---|
373 | static char *merge_file(char *regex, char *file) |
---|
374 | { |
---|
375 | char *newfile = malloc(5 + 2 * strlen(file) + 1 + 1), *tmp = newfile; |
---|
376 | |
---|
377 | *tmp++ = '('; |
---|
378 | *tmp++ = '^'; |
---|
379 | *tmp++ = '|'; |
---|
380 | *tmp++ = '/'; |
---|
381 | *tmp++ = ')'; |
---|
382 | while(*file) |
---|
383 | { |
---|
384 | if(strchr("^.[$()|*+?{\\", *file)) |
---|
385 | *tmp++ = '\\'; |
---|
386 | *tmp++ = *file++; |
---|
387 | } |
---|
388 | *tmp++ = '$'; |
---|
389 | *tmp++ = '\0'; |
---|
390 | |
---|
391 | tmp = merge_regex(regex, newfile); |
---|
392 | free(newfile); |
---|
393 | return tmp; |
---|
394 | } |
---|
395 | |
---|
396 | static char *merge_regex(char *regex, char *string) |
---|
397 | { |
---|
398 | regex_t optre; |
---|
399 | |
---|
400 | if(regex) |
---|
401 | { |
---|
402 | regex = realloc(regex, strlen(regex) + strlen(string) + 1 + 1); |
---|
403 | sprintf(regex + strlen(regex) - 1, "|%s)", string); |
---|
404 | } |
---|
405 | else |
---|
406 | { |
---|
407 | regex = malloc(1 + strlen(string) + 1 + 1); |
---|
408 | sprintf(regex, "(%s)", string); |
---|
409 | } |
---|
410 | |
---|
411 | if(regcomp(&optre, regex, REG_EXTENDED) != 0) |
---|
412 | { |
---|
413 | free(regex); |
---|
414 | return NULL; |
---|
415 | } |
---|
416 | regfree(&optre); |
---|
417 | |
---|
418 | return regex; |
---|
419 | } |
---|
420 | |
---|
421 | static void spawn_children(struct opts *opts) |
---|
422 | { |
---|
423 | static int const files[] = { DEBUG_FILENO, STDERR_FILENO, STDOUT_FILENO }; |
---|
424 | char buf[64]; |
---|
425 | int fd[3][2]; |
---|
426 | int64_t now = _zz_time(); |
---|
427 | pid_t pid; |
---|
428 | int i, j; |
---|
429 | |
---|
430 | if(opts->nchild == opts->maxchild) |
---|
431 | return; /* no slot */ |
---|
432 | |
---|
433 | if(opts->seed == opts->endseed) |
---|
434 | return; /* job finished */ |
---|
435 | |
---|
436 | if(opts->maxcrashes && opts->crashes >= opts->maxcrashes) |
---|
437 | return; /* all jobs crashed */ |
---|
438 | |
---|
439 | if(opts->delay > 0 && opts->lastlaunch + opts->delay > now) |
---|
440 | return; /* too early */ |
---|
441 | |
---|
442 | /* Find the empty slot */ |
---|
443 | for(i = 0; i < opts->maxchild; i++) |
---|
444 | if(opts->child[i].status == STATUS_FREE) |
---|
445 | break; |
---|
446 | |
---|
447 | /* Prepare communication pipe */ |
---|
448 | for(j = 0; j < 3; j++) |
---|
449 | if(pipe(fd[j]) == -1) |
---|
450 | { |
---|
451 | perror("pipe"); |
---|
452 | return; |
---|
453 | } |
---|
454 | |
---|
455 | /* Fork and launch child */ |
---|
456 | pid = fork(); |
---|
457 | switch(pid) |
---|
458 | { |
---|
459 | case -1: |
---|
460 | perror("fork"); |
---|
461 | return; |
---|
462 | case 0: |
---|
463 | /* We’re the child */ |
---|
464 | if(opts->maxmem >= 0) |
---|
465 | { |
---|
466 | struct rlimit rlim; |
---|
467 | rlim.rlim_cur = opts->maxmem * 1000000; |
---|
468 | rlim.rlim_max = opts->maxmem * 1000000; |
---|
469 | setrlimit(RLIMIT_AS, &rlim); |
---|
470 | } |
---|
471 | |
---|
472 | /* We loop in reverse order so that files[0] is done last, |
---|
473 | * just in case one of the other dup2()ed fds had the value */ |
---|
474 | for(j = 3; j--; ) |
---|
475 | { |
---|
476 | close(fd[j][0]); |
---|
477 | if(fd[j][1] != files[j]) |
---|
478 | { |
---|
479 | dup2(fd[j][1], files[j]); |
---|
480 | close(fd[j][1]); |
---|
481 | } |
---|
482 | } |
---|
483 | |
---|
484 | /* Set environment variables */ |
---|
485 | sprintf(buf, "%i", opts->seed); |
---|
486 | setenv("ZZUF_SEED", buf, 1); |
---|
487 | sprintf(buf, "%g", opts->minratio); |
---|
488 | setenv("ZZUF_MINRATIO", buf, 1); |
---|
489 | sprintf(buf, "%g", opts->maxratio); |
---|
490 | setenv("ZZUF_MAXRATIO", buf, 1); |
---|
491 | |
---|
492 | /* Run our process */ |
---|
493 | if(execvp(opts->newargv[0], opts->newargv)) |
---|
494 | { |
---|
495 | perror(opts->newargv[0]); |
---|
496 | exit(EXIT_FAILURE); |
---|
497 | } |
---|
498 | return; |
---|
499 | } |
---|
500 | |
---|
501 | /* We’re the parent, acknowledge spawn */ |
---|
502 | opts->child[i].date = now; |
---|
503 | opts->child[i].pid = pid; |
---|
504 | for(j = 0; j < 3; j++) |
---|
505 | { |
---|
506 | close(fd[j][1]); |
---|
507 | opts->child[i].fd[j] = fd[j][0]; |
---|
508 | } |
---|
509 | opts->child[i].bytes = 0; |
---|
510 | opts->child[i].seed = opts->seed; |
---|
511 | opts->child[i].ratio = _zz_getratio(); |
---|
512 | opts->child[i].status = STATUS_RUNNING; |
---|
513 | if(opts->md5) |
---|
514 | opts->child[i].ctx = _zz_md5_init(); |
---|
515 | |
---|
516 | if(opts->verbose) |
---|
517 | fprintf(stderr, "zzuf[s=%i,r=%g]: launched %s\n", |
---|
518 | opts->child[i].seed, opts->child[i].ratio, |
---|
519 | opts->newargv[0]); |
---|
520 | |
---|
521 | opts->lastlaunch = now; |
---|
522 | opts->nchild++; |
---|
523 | opts->seed++; |
---|
524 | |
---|
525 | _zz_setseed(opts->seed); |
---|
526 | } |
---|
527 | |
---|
528 | static void clean_children(struct opts *opts) |
---|
529 | { |
---|
530 | int64_t now = _zz_time(); |
---|
531 | int i, j; |
---|
532 | |
---|
533 | /* Terminate children if necessary */ |
---|
534 | for(i = 0; i < opts->maxchild; i++) |
---|
535 | { |
---|
536 | if(opts->child[i].status == STATUS_RUNNING |
---|
537 | && opts->maxbytes >= 0 |
---|
538 | && opts->child[i].bytes > opts->maxbytes) |
---|
539 | { |
---|
540 | if(opts->verbose) |
---|
541 | fprintf(stderr, "zzuf[s=%i,r=%g]: " |
---|
542 | "data output exceeded, sending SIGTERM\n", |
---|
543 | opts->child[i].seed, opts->child[i].ratio); |
---|
544 | kill(opts->child[i].pid, SIGTERM); |
---|
545 | opts->child[i].date = now; |
---|
546 | opts->child[i].status = STATUS_SIGTERM; |
---|
547 | } |
---|
548 | |
---|
549 | if(opts->child[i].status == STATUS_RUNNING |
---|
550 | && opts->maxtime >= 0 |
---|
551 | && now > opts->child[i].date + opts->maxtime) |
---|
552 | { |
---|
553 | if(opts->verbose) |
---|
554 | fprintf(stderr, "zzuf[s=%i,r=%g]: " |
---|
555 | "running time exceeded, sending SIGTERM\n", |
---|
556 | opts->child[i].seed, opts->child[i].ratio); |
---|
557 | kill(opts->child[i].pid, SIGTERM); |
---|
558 | opts->child[i].date = now; |
---|
559 | opts->child[i].status = STATUS_SIGTERM; |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | /* Kill children if necessary (still there after 2 seconds) */ |
---|
564 | for(i = 0; i < opts->maxchild; i++) |
---|
565 | { |
---|
566 | if(opts->child[i].status == STATUS_SIGTERM |
---|
567 | && now > opts->child[i].date + 2000000) |
---|
568 | { |
---|
569 | if(opts->verbose) |
---|
570 | fprintf(stderr, "zzuf[s=%i,r=%g]: " |
---|
571 | "not responding, sending SIGKILL\n", |
---|
572 | opts->child[i].seed, opts->child[i].ratio); |
---|
573 | kill(opts->child[i].pid, SIGKILL); |
---|
574 | opts->child[i].status = STATUS_SIGKILL; |
---|
575 | } |
---|
576 | } |
---|
577 | |
---|
578 | /* Collect dead children */ |
---|
579 | for(i = 0; i < opts->maxchild; i++) |
---|
580 | { |
---|
581 | uint8_t md5sum[16]; |
---|
582 | int status; |
---|
583 | pid_t pid; |
---|
584 | |
---|
585 | if(opts->child[i].status != STATUS_SIGKILL |
---|
586 | && opts->child[i].status != STATUS_SIGTERM |
---|
587 | && opts->child[i].status != STATUS_EOF) |
---|
588 | continue; |
---|
589 | |
---|
590 | pid = waitpid(opts->child[i].pid, &status, WNOHANG); |
---|
591 | if(pid <= 0) |
---|
592 | continue; |
---|
593 | |
---|
594 | if(opts->checkexit && WIFEXITED(status) && WEXITSTATUS(status)) |
---|
595 | { |
---|
596 | fprintf(stderr, "zzuf[s=%i,r=%g]: exit %i\n", |
---|
597 | opts->child[i].seed, opts->child[i].ratio, |
---|
598 | WEXITSTATUS(status)); |
---|
599 | opts->crashes++; |
---|
600 | } |
---|
601 | else if(WIFSIGNALED(status) |
---|
602 | && !(WTERMSIG(status) == SIGTERM |
---|
603 | && opts->child[i].status == STATUS_SIGTERM)) |
---|
604 | { |
---|
605 | fprintf(stderr, "zzuf[s=%i,r=%g]: signal %i%s%s\n", |
---|
606 | opts->child[i].seed, opts->child[i].ratio, |
---|
607 | WTERMSIG(status), sig2str(WTERMSIG(status)), |
---|
608 | (WTERMSIG(status) == SIGKILL && opts->maxmem >= 0) ? |
---|
609 | " (memory exceeded?)" : ""); |
---|
610 | opts->crashes++; |
---|
611 | } |
---|
612 | |
---|
613 | for(j = 0; j < 3; j++) |
---|
614 | if(opts->child[i].fd[j] >= 0) |
---|
615 | close(opts->child[i].fd[j]); |
---|
616 | |
---|
617 | if(opts->md5) |
---|
618 | { |
---|
619 | _zz_md5_fini(md5sum, opts->child[i].ctx); |
---|
620 | fprintf(stdout, "zzuf[s=%i,r=%g]: %.02x%.02x%.02x%.02x%.02x%.02x" |
---|
621 | "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x\n", |
---|
622 | opts->child[i].seed, opts->child[i].ratio, |
---|
623 | md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], |
---|
624 | md5sum[5], md5sum[6], md5sum[7], md5sum[8], md5sum[9], |
---|
625 | md5sum[10], md5sum[11], md5sum[12], md5sum[13], |
---|
626 | md5sum[14], md5sum[15]); |
---|
627 | fflush(stdout); |
---|
628 | } |
---|
629 | opts->child[i].status = STATUS_FREE; |
---|
630 | opts->nchild--; |
---|
631 | } |
---|
632 | } |
---|
633 | |
---|
634 | static void read_children(struct opts *opts) |
---|
635 | { |
---|
636 | struct timeval tv; |
---|
637 | fd_set fdset; |
---|
638 | int i, j, ret, maxfd = 0; |
---|
639 | |
---|
640 | /* Read data from all sockets */ |
---|
641 | FD_ZERO(&fdset); |
---|
642 | for(i = 0; i < opts->maxchild; i++) |
---|
643 | { |
---|
644 | if(opts->child[i].status != STATUS_RUNNING) |
---|
645 | continue; |
---|
646 | |
---|
647 | for(j = 0; j < 3; j++) |
---|
648 | ZZUF_FD_SET(opts->child[i].fd[j], &fdset, maxfd); |
---|
649 | } |
---|
650 | tv.tv_sec = 0; |
---|
651 | tv.tv_usec = 1000; |
---|
652 | |
---|
653 | ret = select(maxfd + 1, &fdset, NULL, NULL, &tv); |
---|
654 | if(ret < 0) |
---|
655 | perror("select"); |
---|
656 | if(ret <= 0) |
---|
657 | return; |
---|
658 | |
---|
659 | /* XXX: cute (i, j) iterating hack */ |
---|
660 | for(i = 0, j = 0; i < opts->maxchild; i += (j == 2), j = (j + 1) % 3) |
---|
661 | { |
---|
662 | uint8_t buf[BUFSIZ]; |
---|
663 | |
---|
664 | if(opts->child[i].status != STATUS_RUNNING) |
---|
665 | continue; |
---|
666 | |
---|
667 | if(!ZZUF_FD_ISSET(opts->child[i].fd[j], &fdset)) |
---|
668 | continue; |
---|
669 | |
---|
670 | ret = read(opts->child[i].fd[j], buf, BUFSIZ - 1); |
---|
671 | if(ret > 0) |
---|
672 | { |
---|
673 | /* We got data */ |
---|
674 | if(j != 0) |
---|
675 | opts->child[i].bytes += ret; |
---|
676 | |
---|
677 | if(opts->md5 && j == 2) |
---|
678 | _zz_md5_add(opts->child[i].ctx, buf, ret); |
---|
679 | else if(!opts->quiet || j == 0) |
---|
680 | write((j < 2) ? STDERR_FILENO : STDOUT_FILENO, buf, ret); |
---|
681 | } |
---|
682 | else if(ret == 0) |
---|
683 | { |
---|
684 | /* End of file reached */ |
---|
685 | close(opts->child[i].fd[j]); |
---|
686 | opts->child[i].fd[j] = -1; |
---|
687 | |
---|
688 | if(opts->child[i].fd[0] == -1 |
---|
689 | && opts->child[i].fd[1] == -1 |
---|
690 | && opts->child[i].fd[2] == -1) |
---|
691 | opts->child[i].status = STATUS_EOF; |
---|
692 | } |
---|
693 | } |
---|
694 | } |
---|
695 | |
---|
696 | static char const *sig2str(int signum) |
---|
697 | { |
---|
698 | switch(signum) |
---|
699 | { |
---|
700 | case SIGABRT: return " (SIGABRT)"; |
---|
701 | case SIGFPE: return " (SIGFPE)"; |
---|
702 | case SIGILL: return " (SIGILL)"; |
---|
703 | case SIGQUIT: return " (SIGQUIT)"; |
---|
704 | case SIGSEGV: return " (SIGSEGV)"; |
---|
705 | case SIGTRAP: return " (SIGTRAP)"; |
---|
706 | #ifdef SIGSYS |
---|
707 | case SIGSYS: return " (SIGSYS)"; |
---|
708 | #endif |
---|
709 | #ifdef SIGEMT |
---|
710 | case SIGEMT: return " (SIGEMT)"; |
---|
711 | #endif |
---|
712 | #ifdef SIGBUS |
---|
713 | case SIGBUS: return " (SIGBUS)"; |
---|
714 | #endif |
---|
715 | #ifdef SIGXCPU |
---|
716 | case SIGXCPU: return " (SIGXCPU)"; |
---|
717 | #endif |
---|
718 | #ifdef SIGXFSZ |
---|
719 | case SIGXFSZ: return " (SIGXFSZ)"; |
---|
720 | #endif |
---|
721 | } |
---|
722 | |
---|
723 | return ""; |
---|
724 | } |
---|
725 | |
---|
726 | static void set_environment(char const *progpath) |
---|
727 | { |
---|
728 | char *libpath, *tmp; |
---|
729 | int ret, len = strlen(progpath); |
---|
730 | #if defined __APPLE__ |
---|
731 | # define FILENAME "libzzuf.dylib" |
---|
732 | # define EXTRAINFO "" |
---|
733 | # define PRELOAD "DYLD_INSERT_LIBRARIES" |
---|
734 | setenv("DYLD_FORCE_FLAT_NAMESPACE", "1", 1); |
---|
735 | #elif defined __osf__ |
---|
736 | # define FILENAME "libzzuf.so" |
---|
737 | # define EXTRAINFO ":DEFAULT" |
---|
738 | # define PRELOAD "_RLD_LIST" |
---|
739 | #else |
---|
740 | # define FILENAME "libzzuf.so" |
---|
741 | # define EXTRAINFO "" |
---|
742 | # define PRELOAD "LD_PRELOAD" |
---|
743 | #endif |
---|
744 | |
---|
745 | libpath = malloc(len + strlen("/.libs/" FILENAME EXTRAINFO) + 1); |
---|
746 | strcpy(libpath, progpath); |
---|
747 | |
---|
748 | tmp = strrchr(libpath, '/'); |
---|
749 | strcpy(tmp ? tmp + 1 : libpath, ".libs/" FILENAME); |
---|
750 | ret = access(libpath, R_OK); |
---|
751 | |
---|
752 | strcpy(tmp ? tmp + 1 : libpath, ".libs/" FILENAME EXTRAINFO); |
---|
753 | if(ret == 0) |
---|
754 | setenv(PRELOAD, libpath, 1); |
---|
755 | else |
---|
756 | setenv(PRELOAD, LIBDIR "/" FILENAME EXTRAINFO, 1); |
---|
757 | free(libpath); |
---|
758 | } |
---|
759 | |
---|
760 | static void version(void) |
---|
761 | { |
---|
762 | printf("zzuf %s\n", VERSION); |
---|
763 | printf("Copyright (C) 2002, 2007 Sam Hocevar <sam@zoy.org>\n"); |
---|
764 | printf("This is free software. You may redistribute copies of it under the\n"); |
---|
765 | printf("terms of the Do What The Fuck You Want To Public License, Version 2\n"); |
---|
766 | printf("<http://sam.zoy.org/wtfpl/>.\n"); |
---|
767 | printf("There is NO WARRANTY, to the extent permitted by law.\n"); |
---|
768 | printf("\n"); |
---|
769 | printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n"); |
---|
770 | } |
---|
771 | |
---|
772 | #if defined(HAVE_GETOPT_H) |
---|
773 | static void usage(void) |
---|
774 | { |
---|
775 | printf("Usage: zzuf [-AcdimnqSvx] [-r ratio] [-s seed | -s start:stop]\n"); |
---|
776 | printf(" [-D delay] [-F forks] [-C crashes] [-B bytes]\n"); |
---|
777 | printf(" [-T seconds] [-M bytes] [-P protect] [-R refuse]\n"); |
---|
778 | printf(" [-I include] [-E exclude] [PROGRAM [--] [ARGS]...]\n"); |
---|
779 | # ifdef HAVE_GETOPT_LONG |
---|
780 | printf(" zzuf -h | --help\n"); |
---|
781 | printf(" zzuf -V | --version\n"); |
---|
782 | # else |
---|
783 | printf(" zzuf -h\n"); |
---|
784 | printf(" zzuf -V\n"); |
---|
785 | # endif |
---|
786 | printf("Run PROGRAM with optional arguments ARGS and fuzz its input.\n"); |
---|
787 | printf("\n"); |
---|
788 | printf("Mandatory arguments to long options are mandatory for short options too.\n"); |
---|
789 | # ifdef HAVE_GETOPT_LONG |
---|
790 | printf(" -A, --autoinc increment seed each time a new file is opened\n"); |
---|
791 | printf(" -B, --max-bytes <n> kill children that output more than <n> bytes\n"); |
---|
792 | printf(" -c, --cmdline only fuzz files specified in the command line\n"); |
---|
793 | printf(" -C, --max-crashes <n> stop after <n> children have crashed (default 1)\n"); |
---|
794 | printf(" -d, --debug print debug messages\n"); |
---|
795 | printf(" -D, --delay delay between forks\n"); |
---|
796 | printf(" -E, --exclude <regex> do not fuzz files matching <regex>\n"); |
---|
797 | printf(" -F, --max-forks <n> number of concurrent children (default 1)\n"); |
---|
798 | printf(" -i, --stdin fuzz standard input\n"); |
---|
799 | printf(" -I, --include <regex> only fuzz files matching <regex>\n"); |
---|
800 | printf(" -m, --md5 compute the output's MD5 hash\n"); |
---|
801 | printf(" -M, --max-memory <n> maximum child virtual memory size in MB\n"); |
---|
802 | printf(" -n, --network fuzz network input\n"); |
---|
803 | printf(" -P, --protect <list> protect bytes and characters in <list>\n"); |
---|
804 | printf(" -q, --quiet do not print children's messages\n"); |
---|
805 | printf(" -r, --ratio <ratio> bit fuzzing ratio (default %g)\n", DEFAULT_RATIO); |
---|
806 | printf(" --ratio <start:stop> specify a ratio range\n"); |
---|
807 | printf(" -R, --refuse <list> refuse bytes and characters in <list>\n"); |
---|
808 | printf(" -s, --seed <seed> random seed (default %i)\n", DEFAULT_SEED); |
---|
809 | printf(" --seed <start:stop> specify a seed range\n"); |
---|
810 | printf(" -S, --signal prevent children from diverting crashing signals\n"); |
---|
811 | printf(" -T, --max-time <n> kill children that run for more than <n> seconds\n"); |
---|
812 | printf(" -v, --verbose print information during the run\n"); |
---|
813 | printf(" -x, --check-exit report processes that exit with a non-zero status\n"); |
---|
814 | printf(" -h, --help display this help and exit\n"); |
---|
815 | printf(" -V, --version output version information and exit\n"); |
---|
816 | # else |
---|
817 | printf(" -A increment seed each time a new file is opened\n"); |
---|
818 | printf(" -B <n> kill children that output more than <n> bytes\n"); |
---|
819 | printf(" -c only fuzz files specified in the command line\n"); |
---|
820 | printf(" -C <n> stop after <n> children have crashed (default 1)\n"); |
---|
821 | printf(" -d print debug messages\n"); |
---|
822 | printf(" -D delay between forks\n"); |
---|
823 | printf(" -E <regex> do not fuzz files matching <regex>\n"); |
---|
824 | printf(" -F <n> number of concurrent forks (default 1)\n"); |
---|
825 | printf(" -i fuzz standard input\n"); |
---|
826 | printf(" -I <regex> only fuzz files matching <regex>\n"); |
---|
827 | printf(" -m compute the output's MD5 hash\n"); |
---|
828 | printf(" -M maximum child virtual memory size in MB\n"); |
---|
829 | printf(" -n fuzz network input\n"); |
---|
830 | printf(" -P <list> protect bytes and characters in <list>\n"); |
---|
831 | printf(" -q do not print the fuzzed application's messages\n"); |
---|
832 | printf(" -r <ratio> bit fuzzing ratio (default %g)\n", DEFAULT_RATIO); |
---|
833 | printf(" <start:stop> specify a ratio range\n"); |
---|
834 | printf(" -R <list> refuse bytes and characters in <list>\n"); |
---|
835 | printf(" -s <seed> random seed (default %i)\n", DEFAULT_SEED); |
---|
836 | printf(" <start:stop> specify a seed range\n"); |
---|
837 | printf(" -S prevent children from diverting crashing signals\n"); |
---|
838 | printf(" -T <n> kill children that run for more than <n> seconds\n"); |
---|
839 | printf(" -v print information during the run\n"); |
---|
840 | printf(" -x report processes that exit with a non-zero status\n"); |
---|
841 | printf(" -h display this help and exit\n"); |
---|
842 | printf(" -V output version information and exit\n"); |
---|
843 | # endif |
---|
844 | printf("\n"); |
---|
845 | printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n"); |
---|
846 | } |
---|
847 | #endif |
---|
848 | |
---|