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