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