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