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 1791 2007-07-06 13:40:55Z 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_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> |
---|
35 | #endif |
---|
36 | #if defined HAVE_REGEX_H |
---|
37 | # include <regex.h> |
---|
38 | #endif |
---|
39 | #if defined HAVE_WINSOCK2_H |
---|
40 | # include <winsock2.h> |
---|
41 | #endif |
---|
42 | #if defined HAVE_WINDOWS_H |
---|
43 | # include <windows.h> |
---|
44 | #endif |
---|
45 | #if defined HAVE_IO_H |
---|
46 | # include <io.h> |
---|
47 | #endif |
---|
48 | #include <string.h> |
---|
49 | #include <fcntl.h> |
---|
50 | #include <errno.h> |
---|
51 | #include <signal.h> |
---|
52 | #if defined HAVE_SYS_WAIT_H |
---|
53 | # include <sys/wait.h> |
---|
54 | #endif |
---|
55 | #if defined HAVE_SYS_RESOURCE_H |
---|
56 | # include <sys/resource.h> |
---|
57 | #endif |
---|
58 | |
---|
59 | #include "libzzuf.h" |
---|
60 | #include "opts.h" |
---|
61 | #include "random.h" |
---|
62 | #include "fd.h" |
---|
63 | #include "fuzz.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 | #if defined RLIMIT_AS |
---|
79 | # define ZZUF_RLIMIT_CONST RLIMIT_AS |
---|
80 | #elif defined RLIMIT_VMEM |
---|
81 | # define ZZUF_RLIMIT_CONST RLIMIT_VMEM |
---|
82 | #elif defined RLIMIT_DATA |
---|
83 | # define ZZUF_RLIMIT_CONST RLIMIT_DATA |
---|
84 | #else |
---|
85 | # undef HAVE_SETRLIMIT |
---|
86 | #endif |
---|
87 | |
---|
88 | /* We use file descriptor 17 as the debug channel */ |
---|
89 | #define DEBUG_FILENO 17 |
---|
90 | #define DEBUG_FILENO_STR "17" |
---|
91 | |
---|
92 | static void loop_stdin(struct opts *); |
---|
93 | static int run_process(struct opts *, int[][2]); |
---|
94 | |
---|
95 | static void spawn_children(struct opts *); |
---|
96 | static void clean_children(struct opts *); |
---|
97 | static void read_children(struct opts *); |
---|
98 | |
---|
99 | #if !defined HAVE_SETENV |
---|
100 | static void setenv(char const *, char const *, int); |
---|
101 | #endif |
---|
102 | #if defined HAVE_WAITPID |
---|
103 | static char const *sig2str(int); |
---|
104 | #endif |
---|
105 | #if defined HAVE_WINDOWS_H |
---|
106 | static int dll_inject(void *, void *); |
---|
107 | static void *get_entry(char const *); |
---|
108 | #endif |
---|
109 | static void finfo(FILE *, struct opts *, uint32_t); |
---|
110 | #if defined HAVE_REGEX_H |
---|
111 | static char *merge_regex(char *, char *); |
---|
112 | static char *merge_file(char *, char *); |
---|
113 | #endif |
---|
114 | static void version(void); |
---|
115 | static void usage(void); |
---|
116 | |
---|
117 | #if defined HAVE_WINDOWS_H |
---|
118 | static inline void addcpy(void *buf, void *x) |
---|
119 | { |
---|
120 | memcpy(buf, &x, 4); |
---|
121 | } |
---|
122 | #endif |
---|
123 | |
---|
124 | #define ZZUF_FD_SET(fd, p_fdset, maxfd) \ |
---|
125 | if(fd >= 0) \ |
---|
126 | { \ |
---|
127 | FD_SET((unsigned int)fd, p_fdset); \ |
---|
128 | if(fd > maxfd) \ |
---|
129 | maxfd = fd; \ |
---|
130 | } |
---|
131 | |
---|
132 | #define ZZUF_FD_ISSET(fd, p_fdset) \ |
---|
133 | ((fd >= 0) && (FD_ISSET(fd, p_fdset))) |
---|
134 | |
---|
135 | int main(int argc, char *argv[]) |
---|
136 | { |
---|
137 | struct opts _opts, *opts = &_opts; |
---|
138 | char *tmp; |
---|
139 | #if defined HAVE_REGEX_H |
---|
140 | char *include = NULL, *exclude = NULL; |
---|
141 | int cmdline = 0; |
---|
142 | #endif |
---|
143 | int i; |
---|
144 | |
---|
145 | _zz_opts_init(opts); |
---|
146 | |
---|
147 | for(;;) |
---|
148 | { |
---|
149 | #if defined HAVE_REGEX_H |
---|
150 | # define OPTSTR_REGEX "cE:I:" |
---|
151 | #else |
---|
152 | # define OPTSTR_REGEX "" |
---|
153 | #endif |
---|
154 | #if defined HAVE_SETRLIMIT |
---|
155 | # define OPTSTR_RLIMIT "M:" |
---|
156 | #else |
---|
157 | # define OPTSTR_RLIMIT "" |
---|
158 | #endif |
---|
159 | #define OPTSTR OPTSTR_REGEX OPTSTR_RLIMIT \ |
---|
160 | "Ab:B:C:dD:f:F:imnp:P:qr:R:s:ST:vxhV" |
---|
161 | #define MOREINFO "Try `%s --help' for more information.\n" |
---|
162 | int option_index = 0; |
---|
163 | static struct myoption long_options[] = |
---|
164 | { |
---|
165 | /* Long option, needs arg, flag, short option */ |
---|
166 | { "autoinc", 0, NULL, 'A' }, |
---|
167 | { "bytes", 1, NULL, 'b' }, |
---|
168 | { "max-bytes", 1, NULL, 'B' }, |
---|
169 | #if defined HAVE_REGEX_H |
---|
170 | { "cmdline", 0, NULL, 'c' }, |
---|
171 | #endif |
---|
172 | { "max-crashes", 1, NULL, 'C' }, |
---|
173 | { "debug", 0, NULL, 'd' }, |
---|
174 | { "delay", 1, NULL, 'D' }, |
---|
175 | #if defined HAVE_REGEX_H |
---|
176 | { "exclude", 1, NULL, 'E' }, |
---|
177 | #endif |
---|
178 | { "fuzzing", 1, NULL, 'f' }, |
---|
179 | { "max-forks", 1, NULL, 'F' }, |
---|
180 | { "stdin", 0, NULL, 'i' }, |
---|
181 | #if defined HAVE_REGEX_H |
---|
182 | { "include", 1, NULL, 'I' }, |
---|
183 | #endif |
---|
184 | { "md5", 0, NULL, 'm' }, |
---|
185 | { "max-memory", 1, NULL, 'M' }, |
---|
186 | { "network", 0, NULL, 'n' }, |
---|
187 | { "pick", 1, NULL, 'p' }, |
---|
188 | { "protect", 1, NULL, 'P' }, |
---|
189 | { "quiet", 0, NULL, 'q' }, |
---|
190 | { "ratio", 1, NULL, 'r' }, |
---|
191 | { "refuse", 1, NULL, 'R' }, |
---|
192 | { "seed", 1, NULL, 's' }, |
---|
193 | { "signal", 0, NULL, 'S' }, |
---|
194 | { "max-time", 1, NULL, 'T' }, |
---|
195 | { "verbose", 0, NULL, 'v' }, |
---|
196 | { "check-exit", 0, NULL, 'x' }, |
---|
197 | { "help", 0, NULL, 'h' }, |
---|
198 | { "version", 0, NULL, 'V' }, |
---|
199 | { NULL, 0, NULL, 0 } |
---|
200 | }; |
---|
201 | int c = mygetopt(argc, argv, OPTSTR, long_options, &option_index); |
---|
202 | |
---|
203 | if(c == -1) |
---|
204 | break; |
---|
205 | |
---|
206 | switch(c) |
---|
207 | { |
---|
208 | case 'A': /* --autoinc */ |
---|
209 | setenv("ZZUF_AUTOINC", "1", 1); |
---|
210 | break; |
---|
211 | case 'b': /* --bytes */ |
---|
212 | opts->bytes = myoptarg; |
---|
213 | break; |
---|
214 | case 'B': /* --max-bytes */ |
---|
215 | opts->maxbytes = atoi(myoptarg); |
---|
216 | break; |
---|
217 | #if defined HAVE_REGEX_H |
---|
218 | case 'c': /* --cmdline */ |
---|
219 | cmdline = 1; |
---|
220 | break; |
---|
221 | #endif |
---|
222 | case 'C': /* --max-crashes */ |
---|
223 | opts->maxcrashes = atoi(myoptarg); |
---|
224 | if(opts->maxcrashes <= 0) |
---|
225 | opts->maxcrashes = 0; |
---|
226 | break; |
---|
227 | case 'd': /* --debug */ |
---|
228 | setenv("ZZUF_DEBUG", DEBUG_FILENO_STR, 1); |
---|
229 | break; |
---|
230 | case 'D': /* --delay */ |
---|
231 | opts->delay = (int64_t)(atof(myoptarg) * 1000000.0); |
---|
232 | break; |
---|
233 | #if defined HAVE_REGEX_H |
---|
234 | case 'E': /* --exclude */ |
---|
235 | exclude = merge_regex(exclude, myoptarg); |
---|
236 | if(!exclude) |
---|
237 | { |
---|
238 | fprintf(stderr, "%s: invalid regex -- `%s'\n", |
---|
239 | argv[0], myoptarg); |
---|
240 | _zz_opts_fini(opts); |
---|
241 | return EXIT_FAILURE; |
---|
242 | } |
---|
243 | break; |
---|
244 | #endif |
---|
245 | case 'f': /* --fuzzing */ |
---|
246 | opts->fuzzing = myoptarg; |
---|
247 | break; |
---|
248 | case 'F': /* --max-forks */ |
---|
249 | opts->maxchild = atoi(myoptarg) > 1 ? atoi(myoptarg) : 1; |
---|
250 | break; |
---|
251 | case 'i': /* --stdin */ |
---|
252 | setenv("ZZUF_STDIN", "1", 1); |
---|
253 | break; |
---|
254 | #if defined HAVE_REGEX_H |
---|
255 | case 'I': /* --include */ |
---|
256 | include = merge_regex(include, myoptarg); |
---|
257 | if(!include) |
---|
258 | { |
---|
259 | fprintf(stderr, "%s: invalid regex -- `%s'\n", |
---|
260 | argv[0], myoptarg); |
---|
261 | _zz_opts_fini(opts); |
---|
262 | return EXIT_FAILURE; |
---|
263 | } |
---|
264 | break; |
---|
265 | #endif |
---|
266 | case 'm': /* --md5 */ |
---|
267 | opts->md5 = 1; |
---|
268 | break; |
---|
269 | #if defined HAVE_SETRLIMIT |
---|
270 | case 'M': /* --max-memory */ |
---|
271 | setenv("ZZUF_MEMORY", "1", 1); |
---|
272 | opts->maxmem = atoi(myoptarg); |
---|
273 | break; |
---|
274 | #endif |
---|
275 | case 'n': /* --network */ |
---|
276 | setenv("ZZUF_NETWORK", "1", 1); |
---|
277 | break; |
---|
278 | case 'p': /* --pick */ |
---|
279 | opts->pick = myoptarg; |
---|
280 | break; |
---|
281 | case 'P': /* --protect */ |
---|
282 | opts->protect = myoptarg; |
---|
283 | break; |
---|
284 | case 'q': /* --quiet */ |
---|
285 | opts->quiet = 1; |
---|
286 | break; |
---|
287 | case 'r': /* --ratio */ |
---|
288 | tmp = strchr(myoptarg, ':'); |
---|
289 | opts->minratio = atof(myoptarg); |
---|
290 | opts->maxratio = tmp ? atof(tmp + 1) : opts->minratio; |
---|
291 | break; |
---|
292 | case 'R': /* --refuse */ |
---|
293 | opts->refuse = myoptarg; |
---|
294 | break; |
---|
295 | case 's': /* --seed */ |
---|
296 | tmp = strchr(myoptarg, ':'); |
---|
297 | opts->seed = atol(myoptarg); |
---|
298 | opts->endseed = tmp ? (uint32_t)atoi(tmp + 1) : opts->seed + 1; |
---|
299 | break; |
---|
300 | case 'S': /* --signal */ |
---|
301 | setenv("ZZUF_SIGNAL", "1", 1); |
---|
302 | break; |
---|
303 | case 'T': /* --max-time */ |
---|
304 | opts->maxtime = (int64_t)(atof(myoptarg) * 1000000.0); |
---|
305 | break; |
---|
306 | case 'x': /* --check-exit */ |
---|
307 | opts->checkexit = 1; |
---|
308 | break; |
---|
309 | case 'v': /* --verbose */ |
---|
310 | opts->verbose = 1; |
---|
311 | break; |
---|
312 | case 'h': /* --help */ |
---|
313 | usage(); |
---|
314 | _zz_opts_fini(opts); |
---|
315 | return 0; |
---|
316 | case 'V': /* --version */ |
---|
317 | version(); |
---|
318 | _zz_opts_fini(opts); |
---|
319 | return 0; |
---|
320 | default: |
---|
321 | fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c); |
---|
322 | printf(MOREINFO, argv[0]); |
---|
323 | _zz_opts_fini(opts); |
---|
324 | return EXIT_FAILURE; |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | _zz_setratio(opts->minratio, opts->maxratio); |
---|
329 | _zz_setseed(opts->seed); |
---|
330 | |
---|
331 | /* If asked to read from the standard input */ |
---|
332 | if(myoptind >= argc) |
---|
333 | { |
---|
334 | if(opts->verbose) |
---|
335 | { |
---|
336 | finfo(stderr, opts, opts->seed); |
---|
337 | fprintf(stderr, "reading from stdin\n"); |
---|
338 | } |
---|
339 | |
---|
340 | if(opts->endseed != opts->seed + 1) |
---|
341 | { |
---|
342 | fprintf(stderr, "%s: seed ranges are incompatible with " |
---|
343 | "stdin fuzzing\n", argv[0]); |
---|
344 | printf(MOREINFO, argv[0]); |
---|
345 | _zz_opts_fini(opts); |
---|
346 | return EXIT_FAILURE; |
---|
347 | } |
---|
348 | |
---|
349 | loop_stdin(opts); |
---|
350 | |
---|
351 | _zz_opts_fini(opts); |
---|
352 | return EXIT_SUCCESS; |
---|
353 | } |
---|
354 | |
---|
355 | /* If asked to launch programs */ |
---|
356 | #if defined HAVE_REGEX_H |
---|
357 | if(cmdline) |
---|
358 | { |
---|
359 | int dashdash = 0; |
---|
360 | |
---|
361 | for(i = myoptind + 1; i < argc; i++) |
---|
362 | { |
---|
363 | if(dashdash) |
---|
364 | include = merge_file(include, argv[i]); |
---|
365 | else if(!strcmp("--", argv[i])) |
---|
366 | dashdash = 1; |
---|
367 | else if(argv[i][0] != '-') |
---|
368 | include = merge_file(include, argv[i]); |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | if(include) |
---|
373 | setenv("ZZUF_INCLUDE", include, 1); |
---|
374 | if(exclude) |
---|
375 | setenv("ZZUF_EXCLUDE", exclude, 1); |
---|
376 | #endif |
---|
377 | |
---|
378 | if(opts->fuzzing) |
---|
379 | setenv("ZZUF_FUZZING", opts->fuzzing, 1); |
---|
380 | if(opts->bytes) |
---|
381 | setenv("ZZUF_BYTES", opts->bytes, 1); |
---|
382 | if(opts->pick) |
---|
383 | setenv("ZZUF_PICK", opts->pick, 1); |
---|
384 | if(opts->protect) |
---|
385 | setenv("ZZUF_PROTECT", opts->protect, 1); |
---|
386 | if(opts->refuse) |
---|
387 | setenv("ZZUF_REFUSE", opts->refuse, 1); |
---|
388 | |
---|
389 | /* Allocate memory for children handling */ |
---|
390 | opts->child = malloc(opts->maxchild * sizeof(struct child)); |
---|
391 | for(i = 0; i < opts->maxchild; i++) |
---|
392 | opts->child[i].status = STATUS_FREE; |
---|
393 | opts->nchild = 0; |
---|
394 | |
---|
395 | /* Create new argv */ |
---|
396 | opts->oldargv = argv; |
---|
397 | opts->newargv = malloc((argc - myoptind + 1) * sizeof(char *)); |
---|
398 | memcpy(opts->newargv, argv + myoptind, (argc - myoptind) * sizeof(char *)); |
---|
399 | opts->newargv[argc - myoptind] = (char *)NULL; |
---|
400 | |
---|
401 | /* Main loop */ |
---|
402 | while(opts->nchild || opts->seed < opts->endseed) |
---|
403 | { |
---|
404 | /* Spawn new children, if necessary */ |
---|
405 | spawn_children(opts); |
---|
406 | |
---|
407 | /* Cleanup dead or dying children */ |
---|
408 | clean_children(opts); |
---|
409 | |
---|
410 | /* Read data from children */ |
---|
411 | read_children(opts); |
---|
412 | |
---|
413 | if(opts->maxcrashes && opts->crashes >= opts->maxcrashes |
---|
414 | && opts->nchild == 0) |
---|
415 | break; |
---|
416 | } |
---|
417 | |
---|
418 | /* Clean up */ |
---|
419 | _zz_opts_fini(opts); |
---|
420 | |
---|
421 | return opts->crashes ? EXIT_FAILURE : EXIT_SUCCESS; |
---|
422 | } |
---|
423 | |
---|
424 | static void loop_stdin(struct opts *opts) |
---|
425 | { |
---|
426 | uint8_t md5sum[16]; |
---|
427 | struct md5 *ctx = NULL; |
---|
428 | |
---|
429 | if(opts->md5) |
---|
430 | ctx = _zz_md5_init(); |
---|
431 | |
---|
432 | if(opts->fuzzing) |
---|
433 | _zz_fuzzing(opts->fuzzing); |
---|
434 | if(opts->bytes) |
---|
435 | _zz_bytes(opts->bytes); |
---|
436 | if(opts->pick) |
---|
437 | _zz_pick(opts->pick); |
---|
438 | if(opts->protect) |
---|
439 | _zz_protect(opts->protect); |
---|
440 | if(opts->refuse) |
---|
441 | _zz_refuse(opts->refuse); |
---|
442 | |
---|
443 | _zz_fd_init(); |
---|
444 | _zz_register(0); |
---|
445 | |
---|
446 | for(;;) |
---|
447 | { |
---|
448 | uint8_t buf[BUFSIZ]; |
---|
449 | int ret, off = 0, nw = 0; |
---|
450 | |
---|
451 | ret = read(0, buf, BUFSIZ); |
---|
452 | if(ret <= 0) |
---|
453 | break; |
---|
454 | |
---|
455 | _zz_fuzz(0, buf, ret); |
---|
456 | _zz_addpos(0, ret); |
---|
457 | |
---|
458 | if(opts->md5) |
---|
459 | _zz_md5_add(ctx, buf, ret); |
---|
460 | else while(ret) |
---|
461 | { |
---|
462 | if((nw = write(1, buf + off, (unsigned int)ret)) < 0) |
---|
463 | break; |
---|
464 | ret -= nw; |
---|
465 | off += nw; |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | if(opts->md5) |
---|
470 | { |
---|
471 | _zz_md5_fini(md5sum, ctx); |
---|
472 | finfo(stdout, opts, opts->seed); |
---|
473 | fprintf(stdout, "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x" |
---|
474 | "%.02x%.02x%.02x%.02x%.02x%.02x\n", md5sum[0], md5sum[1], |
---|
475 | md5sum[2], md5sum[3], md5sum[4], md5sum[5], md5sum[6], |
---|
476 | md5sum[7], md5sum[8], md5sum[9], md5sum[10], md5sum[11], |
---|
477 | md5sum[12], md5sum[13], md5sum[14], md5sum[15]); |
---|
478 | fflush(stdout); |
---|
479 | } |
---|
480 | |
---|
481 | _zz_unregister(0); |
---|
482 | _zz_fd_fini(); |
---|
483 | } |
---|
484 | |
---|
485 | static void finfo(FILE *fp, struct opts *opts, uint32_t seed) |
---|
486 | { |
---|
487 | if(opts->minratio == opts->maxratio) |
---|
488 | fprintf(fp, "zzuf[s=%i,r=%g]: ", seed, opts->minratio); |
---|
489 | else |
---|
490 | fprintf(fp, "zzuf[s=%i,r=%g:%g]: ", seed, |
---|
491 | opts->minratio, opts->maxratio); |
---|
492 | } |
---|
493 | |
---|
494 | #if defined HAVE_REGEX_H |
---|
495 | static char *merge_file(char *regex, char *file) |
---|
496 | { |
---|
497 | char *newfile = malloc(5 + 2 * strlen(file) + 1 + 1), *tmp = newfile; |
---|
498 | |
---|
499 | *tmp++ = '('; |
---|
500 | *tmp++ = '^'; |
---|
501 | *tmp++ = '|'; |
---|
502 | *tmp++ = '/'; |
---|
503 | *tmp++ = ')'; |
---|
504 | while(*file) |
---|
505 | { |
---|
506 | if(strchr("^.[$()|*+?{\\", *file)) |
---|
507 | *tmp++ = '\\'; |
---|
508 | *tmp++ = *file++; |
---|
509 | } |
---|
510 | *tmp++ = '$'; |
---|
511 | *tmp++ = '\0'; |
---|
512 | |
---|
513 | tmp = merge_regex(regex, newfile); |
---|
514 | free(newfile); |
---|
515 | return tmp; |
---|
516 | } |
---|
517 | |
---|
518 | static char *merge_regex(char *regex, char *string) |
---|
519 | { |
---|
520 | regex_t optre; |
---|
521 | |
---|
522 | if(regex) |
---|
523 | { |
---|
524 | regex = realloc(regex, strlen(regex) + strlen(string) + 1 + 1); |
---|
525 | sprintf(regex + strlen(regex) - 1, "|%s)", string); |
---|
526 | } |
---|
527 | else |
---|
528 | { |
---|
529 | regex = malloc(1 + strlen(string) + 1 + 1); |
---|
530 | sprintf(regex, "(%s)", string); |
---|
531 | } |
---|
532 | |
---|
533 | if(regcomp(&optre, regex, REG_EXTENDED) != 0) |
---|
534 | { |
---|
535 | free(regex); |
---|
536 | return NULL; |
---|
537 | } |
---|
538 | regfree(&optre); |
---|
539 | |
---|
540 | return regex; |
---|
541 | } |
---|
542 | #endif |
---|
543 | |
---|
544 | static void spawn_children(struct opts *opts) |
---|
545 | { |
---|
546 | int pipes[3][2]; |
---|
547 | int64_t now = _zz_time(); |
---|
548 | pid_t pid; |
---|
549 | int i, j; |
---|
550 | |
---|
551 | if(opts->nchild == opts->maxchild) |
---|
552 | return; /* no slot */ |
---|
553 | |
---|
554 | if(opts->seed == opts->endseed) |
---|
555 | return; /* job finished */ |
---|
556 | |
---|
557 | if(opts->maxcrashes && opts->crashes >= opts->maxcrashes) |
---|
558 | return; /* all jobs crashed */ |
---|
559 | |
---|
560 | if(opts->delay > 0 && opts->lastlaunch + opts->delay > now) |
---|
561 | return; /* too early */ |
---|
562 | |
---|
563 | /* Find the empty slot */ |
---|
564 | for(i = 0; i < opts->maxchild; i++) |
---|
565 | if(opts->child[i].status == STATUS_FREE) |
---|
566 | break; |
---|
567 | |
---|
568 | /* Prepare communication pipe */ |
---|
569 | for(j = 0; j < 3; j++) |
---|
570 | { |
---|
571 | int ret; |
---|
572 | #if defined HAVE_PIPE |
---|
573 | ret = pipe(pipes[j]); |
---|
574 | #elif defined HAVE__PIPE |
---|
575 | ret = _pipe(pipes[j], 512, _O_BINARY | O_NOINHERIT); |
---|
576 | #endif |
---|
577 | if(ret < 0) |
---|
578 | { |
---|
579 | perror("pipe"); |
---|
580 | return; |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | pid = run_process(opts, pipes); |
---|
585 | if(pid < 0) |
---|
586 | return; |
---|
587 | |
---|
588 | /* We’re the parent, acknowledge spawn */ |
---|
589 | opts->child[i].date = now; |
---|
590 | opts->child[i].pid = pid; |
---|
591 | for(j = 0; j < 3; j++) |
---|
592 | { |
---|
593 | close(pipes[j][1]); |
---|
594 | opts->child[i].fd[j] = pipes[j][0]; |
---|
595 | } |
---|
596 | opts->child[i].bytes = 0; |
---|
597 | opts->child[i].seed = opts->seed; |
---|
598 | opts->child[i].ratio = _zz_getratio(); |
---|
599 | opts->child[i].status = STATUS_RUNNING; |
---|
600 | if(opts->md5) |
---|
601 | opts->child[i].ctx = _zz_md5_init(); |
---|
602 | |
---|
603 | if(opts->verbose) |
---|
604 | { |
---|
605 | finfo(stderr, opts, opts->child[i].seed); |
---|
606 | fprintf(stderr, "launched %s\n", opts->newargv[0]); |
---|
607 | } |
---|
608 | |
---|
609 | opts->lastlaunch = now; |
---|
610 | opts->nchild++; |
---|
611 | opts->seed++; |
---|
612 | |
---|
613 | _zz_setseed(opts->seed); |
---|
614 | } |
---|
615 | |
---|
616 | static void clean_children(struct opts *opts) |
---|
617 | { |
---|
618 | #if defined HAVE_KILL |
---|
619 | int64_t now = _zz_time(); |
---|
620 | #endif |
---|
621 | int i, j; |
---|
622 | |
---|
623 | #if defined HAVE_KILL |
---|
624 | /* Terminate children if necessary */ |
---|
625 | for(i = 0; i < opts->maxchild; i++) |
---|
626 | { |
---|
627 | if(opts->child[i].status == STATUS_RUNNING |
---|
628 | && opts->maxbytes >= 0 |
---|
629 | && opts->child[i].bytes > opts->maxbytes) |
---|
630 | { |
---|
631 | if(opts->verbose) |
---|
632 | { |
---|
633 | finfo(stderr, opts, opts->child[i].seed); |
---|
634 | fprintf(stderr, "data output exceeded, sending SIGTERM\n"); |
---|
635 | } |
---|
636 | kill(opts->child[i].pid, SIGTERM); |
---|
637 | opts->child[i].date = now; |
---|
638 | opts->child[i].status = STATUS_SIGTERM; |
---|
639 | } |
---|
640 | |
---|
641 | if(opts->child[i].status == STATUS_RUNNING |
---|
642 | && opts->maxtime >= 0 |
---|
643 | && now > opts->child[i].date + opts->maxtime) |
---|
644 | { |
---|
645 | if(opts->verbose) |
---|
646 | { |
---|
647 | finfo(stderr, opts, opts->child[i].seed); |
---|
648 | fprintf(stderr, "running time exceeded, sending SIGTERM\n"); |
---|
649 | } |
---|
650 | kill(opts->child[i].pid, SIGTERM); |
---|
651 | opts->child[i].date = now; |
---|
652 | opts->child[i].status = STATUS_SIGTERM; |
---|
653 | } |
---|
654 | } |
---|
655 | |
---|
656 | /* Kill children if necessary (still there after 2 seconds) */ |
---|
657 | for(i = 0; i < opts->maxchild; i++) |
---|
658 | { |
---|
659 | if(opts->child[i].status == STATUS_SIGTERM |
---|
660 | && now > opts->child[i].date + 2000000) |
---|
661 | { |
---|
662 | if(opts->verbose) |
---|
663 | { |
---|
664 | finfo(stderr, opts, opts->child[i].seed); |
---|
665 | fprintf(stderr, "not responding, sending SIGKILL\n"); |
---|
666 | } |
---|
667 | kill(opts->child[i].pid, SIGKILL); |
---|
668 | opts->child[i].status = STATUS_SIGKILL; |
---|
669 | } |
---|
670 | } |
---|
671 | #endif |
---|
672 | |
---|
673 | /* Collect dead children */ |
---|
674 | for(i = 0; i < opts->maxchild; i++) |
---|
675 | { |
---|
676 | uint8_t md5sum[16]; |
---|
677 | #if defined HAVE_WAITPID |
---|
678 | int status; |
---|
679 | pid_t pid; |
---|
680 | #endif |
---|
681 | |
---|
682 | if(opts->child[i].status != STATUS_SIGKILL |
---|
683 | && opts->child[i].status != STATUS_SIGTERM |
---|
684 | && opts->child[i].status != STATUS_EOF) |
---|
685 | continue; |
---|
686 | |
---|
687 | #if defined HAVE_WAITPID |
---|
688 | pid = waitpid(opts->child[i].pid, &status, WNOHANG); |
---|
689 | if(pid <= 0) |
---|
690 | continue; |
---|
691 | |
---|
692 | if(opts->checkexit && WIFEXITED(status) && WEXITSTATUS(status)) |
---|
693 | { |
---|
694 | finfo(stderr, opts, opts->child[i].seed); |
---|
695 | fprintf(stderr, "exit %i\n", WEXITSTATUS(status)); |
---|
696 | opts->crashes++; |
---|
697 | } |
---|
698 | else if(WIFSIGNALED(status) |
---|
699 | && !(WTERMSIG(status) == SIGTERM |
---|
700 | && opts->child[i].status == STATUS_SIGTERM)) |
---|
701 | { |
---|
702 | finfo(stderr, opts, opts->child[i].seed); |
---|
703 | fprintf(stderr, "signal %i%s%s\n", |
---|
704 | WTERMSIG(status), sig2str(WTERMSIG(status)), |
---|
705 | (WTERMSIG(status) == SIGKILL && opts->maxmem >= 0) ? |
---|
706 | " (memory exceeded?)" : ""); |
---|
707 | opts->crashes++; |
---|
708 | } |
---|
709 | #endif |
---|
710 | |
---|
711 | for(j = 0; j < 3; j++) |
---|
712 | if(opts->child[i].fd[j] >= 0) |
---|
713 | close(opts->child[i].fd[j]); |
---|
714 | |
---|
715 | if(opts->md5) |
---|
716 | { |
---|
717 | _zz_md5_fini(md5sum, opts->child[i].ctx); |
---|
718 | finfo(stdout, opts, opts->child[i].seed); |
---|
719 | fprintf(stdout, "%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x%.02x" |
---|
720 | "%.02x%.02x%.02x%.02x%.02x%.02x%.02x\n", md5sum[0], |
---|
721 | md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], |
---|
722 | md5sum[6], md5sum[7], md5sum[8], md5sum[9], md5sum[10], |
---|
723 | md5sum[11], md5sum[12], md5sum[13], md5sum[14], md5sum[15]); |
---|
724 | fflush(stdout); |
---|
725 | } |
---|
726 | opts->child[i].status = STATUS_FREE; |
---|
727 | opts->nchild--; |
---|
728 | } |
---|
729 | } |
---|
730 | |
---|
731 | static void read_children(struct opts *opts) |
---|
732 | { |
---|
733 | struct timeval tv; |
---|
734 | fd_set fdset; |
---|
735 | int i, j, ret, maxfd = 0; |
---|
736 | |
---|
737 | /* Read data from all sockets */ |
---|
738 | FD_ZERO(&fdset); |
---|
739 | for(i = 0; i < opts->maxchild; i++) |
---|
740 | { |
---|
741 | if(opts->child[i].status != STATUS_RUNNING) |
---|
742 | continue; |
---|
743 | |
---|
744 | for(j = 0; j < 3; j++) |
---|
745 | ZZUF_FD_SET(opts->child[i].fd[j], &fdset, maxfd); |
---|
746 | } |
---|
747 | tv.tv_sec = 0; |
---|
748 | tv.tv_usec = 1000; |
---|
749 | |
---|
750 | ret = select(maxfd + 1, &fdset, NULL, NULL, &tv); |
---|
751 | if(ret < 0 && errno) |
---|
752 | perror("select"); |
---|
753 | if(ret <= 0) |
---|
754 | return; |
---|
755 | |
---|
756 | /* XXX: cute (i, j) iterating hack */ |
---|
757 | for(i = 0, j = 0; i < opts->maxchild; i += (j == 2), j = (j + 1) % 3) |
---|
758 | { |
---|
759 | uint8_t buf[BUFSIZ]; |
---|
760 | |
---|
761 | if(opts->child[i].status != STATUS_RUNNING) |
---|
762 | continue; |
---|
763 | |
---|
764 | if(!ZZUF_FD_ISSET(opts->child[i].fd[j], &fdset)) |
---|
765 | continue; |
---|
766 | |
---|
767 | ret = read(opts->child[i].fd[j], buf, BUFSIZ - 1); |
---|
768 | if(ret > 0) |
---|
769 | { |
---|
770 | /* We got data */ |
---|
771 | if(j != 0) |
---|
772 | opts->child[i].bytes += ret; |
---|
773 | |
---|
774 | if(opts->md5 && j == 2) |
---|
775 | _zz_md5_add(opts->child[i].ctx, buf, ret); |
---|
776 | else if(!opts->quiet || j == 0) |
---|
777 | write((j < 2) ? STDERR_FILENO : STDOUT_FILENO, buf, ret); |
---|
778 | } |
---|
779 | else if(ret == 0) |
---|
780 | { |
---|
781 | /* End of file reached */ |
---|
782 | close(opts->child[i].fd[j]); |
---|
783 | opts->child[i].fd[j] = -1; |
---|
784 | |
---|
785 | if(opts->child[i].fd[0] == -1 |
---|
786 | && opts->child[i].fd[1] == -1 |
---|
787 | && opts->child[i].fd[2] == -1) |
---|
788 | opts->child[i].status = STATUS_EOF; |
---|
789 | } |
---|
790 | } |
---|
791 | } |
---|
792 | |
---|
793 | #if !defined HAVE_SETENV |
---|
794 | static void setenv(char const *name, char const *value, int overwrite) |
---|
795 | { |
---|
796 | char *str; |
---|
797 | |
---|
798 | if(!overwrite && getenv(name)) |
---|
799 | return; |
---|
800 | |
---|
801 | str = malloc(strlen(name) + 1 + strlen(value) + 1); |
---|
802 | sprintf(str, "%s=%s", name, value); |
---|
803 | putenv(str); |
---|
804 | } |
---|
805 | #endif |
---|
806 | |
---|
807 | #if defined HAVE_WAITPID |
---|
808 | static char const *sig2str(int signum) |
---|
809 | { |
---|
810 | switch(signum) |
---|
811 | { |
---|
812 | case SIGABRT: return " (SIGABRT)"; |
---|
813 | case SIGFPE: return " (SIGFPE)"; |
---|
814 | case SIGILL: return " (SIGILL)"; |
---|
815 | #ifdef SIGQUIT |
---|
816 | case SIGQUIT: return " (SIGQUIT)"; |
---|
817 | #endif |
---|
818 | case SIGSEGV: return " (SIGSEGV)"; |
---|
819 | #ifdef SIGTRAP |
---|
820 | case SIGTRAP: return " (SIGTRAP)"; |
---|
821 | #endif |
---|
822 | #ifdef SIGSYS |
---|
823 | case SIGSYS: return " (SIGSYS)"; |
---|
824 | #endif |
---|
825 | #ifdef SIGEMT |
---|
826 | case SIGEMT: return " (SIGEMT)"; |
---|
827 | #endif |
---|
828 | #ifdef SIGBUS |
---|
829 | case SIGBUS: return " (SIGBUS)"; |
---|
830 | #endif |
---|
831 | #ifdef SIGXCPU |
---|
832 | case SIGXCPU: return " (SIGXCPU)"; |
---|
833 | #endif |
---|
834 | #ifdef SIGXFSZ |
---|
835 | case SIGXFSZ: return " (SIGXFSZ)"; |
---|
836 | #endif |
---|
837 | } |
---|
838 | |
---|
839 | return ""; |
---|
840 | } |
---|
841 | #endif |
---|
842 | |
---|
843 | static int run_process(struct opts *opts, int pipes[][2]) |
---|
844 | { |
---|
845 | char buf[64]; |
---|
846 | #if defined HAVE_FORK |
---|
847 | static int const files[] = { DEBUG_FILENO, STDERR_FILENO, STDOUT_FILENO }; |
---|
848 | char *libpath, *tmp; |
---|
849 | int pid, j, len = strlen(opts->oldargv[0]); |
---|
850 | # if defined __APPLE__ |
---|
851 | # define FILENAME "libzzuf.dylib" |
---|
852 | # define EXTRAINFO "" |
---|
853 | # define PRELOAD "DYLD_INSERT_LIBRARIES" |
---|
854 | setenv("DYLD_FORCE_FLAT_NAMESPACE", "1", 1); |
---|
855 | # elif defined __osf__ |
---|
856 | # define FILENAME "libzzuf.so" |
---|
857 | # define EXTRAINFO ":DEFAULT" |
---|
858 | # define PRELOAD "_RLD_LIST" |
---|
859 | # else |
---|
860 | # define FILENAME "libzzuf.so" |
---|
861 | # define EXTRAINFO "" |
---|
862 | # define PRELOAD "LD_PRELOAD" |
---|
863 | # endif |
---|
864 | #elif HAVE_WINDOWS_H |
---|
865 | PROCESS_INFORMATION pinfo; |
---|
866 | STARTUPINFO sinfo; |
---|
867 | HANDLE pid; |
---|
868 | void *epaddr; |
---|
869 | #endif |
---|
870 | int ret; |
---|
871 | |
---|
872 | #if defined HAVE_FORK |
---|
873 | /* Fork and launch child */ |
---|
874 | pid = fork(); |
---|
875 | if(pid < -1) |
---|
876 | perror("fork"); |
---|
877 | if(pid != 0) |
---|
878 | return pid; |
---|
879 | |
---|
880 | /* We loop in reverse order so that files[0] is done last, |
---|
881 | * just in case one of the other dup2()ed fds had the value */ |
---|
882 | for(j = 3; j--; ) |
---|
883 | { |
---|
884 | close(pipes[j][0]); |
---|
885 | if(pipes[j][1] != files[j]) |
---|
886 | { |
---|
887 | dup2(pipes[j][1], files[j]); |
---|
888 | close(pipes[j][1]); |
---|
889 | } |
---|
890 | } |
---|
891 | #endif |
---|
892 | |
---|
893 | #if defined HAVE_SETRLIMIT |
---|
894 | if(opts->maxmem >= 0) |
---|
895 | { |
---|
896 | struct rlimit rlim; |
---|
897 | rlim.rlim_cur = opts->maxmem * 1000000; |
---|
898 | rlim.rlim_max = opts->maxmem * 1000000; |
---|
899 | setrlimit(ZZUF_RLIMIT_CONST, &rlim); |
---|
900 | } |
---|
901 | #endif |
---|
902 | |
---|
903 | /* Set environment variables */ |
---|
904 | sprintf(buf, "%i", opts->seed); |
---|
905 | setenv("ZZUF_SEED", buf, 1); |
---|
906 | sprintf(buf, "%g", opts->minratio); |
---|
907 | setenv("ZZUF_MINRATIO", buf, 1); |
---|
908 | sprintf(buf, "%g", opts->maxratio); |
---|
909 | setenv("ZZUF_MAXRATIO", buf, 1); |
---|
910 | |
---|
911 | #if defined HAVE_FORK |
---|
912 | libpath = malloc(len + strlen("/.libs/" FILENAME EXTRAINFO) + 1); |
---|
913 | strcpy(libpath, opts->oldargv[0]); |
---|
914 | |
---|
915 | tmp = strrchr(libpath, '/'); |
---|
916 | strcpy(tmp ? tmp + 1 : libpath, ".libs/" FILENAME); |
---|
917 | ret = access(libpath, R_OK); |
---|
918 | |
---|
919 | strcpy(tmp ? tmp + 1 : libpath, ".libs/" FILENAME EXTRAINFO); |
---|
920 | if(ret == 0) |
---|
921 | setenv(PRELOAD, libpath, 1); |
---|
922 | else |
---|
923 | setenv(PRELOAD, LIBDIR "/" FILENAME EXTRAINFO, 1); |
---|
924 | free(libpath); |
---|
925 | |
---|
926 | if(execvp(opts->newargv[0], opts->newargv)) |
---|
927 | { |
---|
928 | perror(opts->newargv[0]); |
---|
929 | exit(EXIT_FAILURE); |
---|
930 | } |
---|
931 | |
---|
932 | exit(EXIT_SUCCESS); |
---|
933 | /* no return */ |
---|
934 | return 0; |
---|
935 | #elif HAVE_WINDOWS_H |
---|
936 | pid = GetCurrentProcess(); |
---|
937 | |
---|
938 | /* Get entry point */ |
---|
939 | epaddr = get_entry(opts->newargv[0]); |
---|
940 | if(!epaddr) |
---|
941 | return -1; |
---|
942 | |
---|
943 | memset(&sinfo, 0, sizeof(sinfo)); |
---|
944 | sinfo.cb = sizeof(sinfo); |
---|
945 | DuplicateHandle(pid, (HANDLE)_get_osfhandle(pipes[0][1]), pid, |
---|
946 | /* FIXME */ &sinfo.hStdInput, 0, TRUE, DUPLICATE_SAME_ACCESS); |
---|
947 | DuplicateHandle(pid, (HANDLE)_get_osfhandle(pipes[1][1]), pid, |
---|
948 | &sinfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS); |
---|
949 | DuplicateHandle(pid, (HANDLE)_get_osfhandle(pipes[2][1]), pid, |
---|
950 | &sinfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS); |
---|
951 | sinfo.dwFlags = STARTF_USESTDHANDLES; |
---|
952 | ret = CreateProcess(NULL, opts->newargv[0], NULL, NULL, FALSE, |
---|
953 | CREATE_SUSPENDED, NULL, NULL, &sinfo, &pinfo); |
---|
954 | if(!ret) |
---|
955 | return -1; |
---|
956 | |
---|
957 | /* Insert the replacement code */ |
---|
958 | ret = dll_inject(pinfo.hProcess, epaddr); |
---|
959 | if(ret < 0) |
---|
960 | { |
---|
961 | TerminateProcess(pinfo.hProcess, -1); |
---|
962 | return -1; |
---|
963 | } |
---|
964 | |
---|
965 | ret = ResumeThread(pinfo.hThread); |
---|
966 | if(ret < 0) |
---|
967 | { |
---|
968 | TerminateProcess(pinfo.hProcess, -1); |
---|
969 | return -1; |
---|
970 | } |
---|
971 | |
---|
972 | return (long int)pinfo.hProcess; |
---|
973 | #endif |
---|
974 | } |
---|
975 | |
---|
976 | #if defined HAVE_WINDOWS_H |
---|
977 | static int dll_inject(void *process, void *epaddr) |
---|
978 | { |
---|
979 | uint8_t old_ep[7]; |
---|
980 | uint8_t new_ep[] = "\xb8<01>\xff\xe0"; |
---|
981 | uint8_t loader[] = "libzzuf.dll\0<0000c>\xb8<14>\x50\xb8<1a>\xff\xd0" |
---|
982 | "\xb8\0\0\0\0\x50\xb8\x07\x00\x00\x00\x50\xb8<2d>" |
---|
983 | "\x50\xb8<33>\x50\xb8<39>\xff\xd0\x50\xb8<41>\xff" |
---|
984 | "\xd0\xb8<48>\xff\xe0"; |
---|
985 | void *lib; |
---|
986 | uint8_t *loaderaddr; |
---|
987 | DWORD tmp; |
---|
988 | |
---|
989 | /* Save the old entry-point code */ |
---|
990 | ReadProcessMemory(process, epaddr, old_ep, 7, &tmp); |
---|
991 | if(tmp != 7) |
---|
992 | return -1; |
---|
993 | |
---|
994 | loaderaddr = VirtualAllocEx(process, NULL, 78, MEM_COMMIT, |
---|
995 | PAGE_EXECUTE_READWRITE); |
---|
996 | if(!loaderaddr) |
---|
997 | return -1; |
---|
998 | |
---|
999 | addcpy(new_ep + 0x01, loaderaddr + 0x0c + 7); |
---|
1000 | WriteProcessMemory(process, epaddr, new_ep, 7, &tmp); |
---|
1001 | if(tmp != 7) |
---|
1002 | return -1; |
---|
1003 | |
---|
1004 | lib = LoadLibrary("kernel32.dll"); |
---|
1005 | if(!lib) |
---|
1006 | return -1; |
---|
1007 | |
---|
1008 | memcpy(loader + 0x0c, old_ep, 7); |
---|
1009 | addcpy(loader + 0x14, loaderaddr + 0x00); /* offset for dll string */ |
---|
1010 | addcpy(loader + 0x1a, GetProcAddress(lib, "LoadLibraryA")); |
---|
1011 | addcpy(loader + 0x2d, loaderaddr + 0x0c); |
---|
1012 | addcpy(loader + 0x33, epaddr); |
---|
1013 | addcpy(loader + 0x39, GetProcAddress(lib, "GetCurrentProcess")); |
---|
1014 | addcpy(loader + 0x41, GetProcAddress(lib, "WriteProcessMemory")); |
---|
1015 | addcpy(loader + 0x48, epaddr); |
---|
1016 | FreeLibrary(lib); |
---|
1017 | |
---|
1018 | WriteProcessMemory(process, loaderaddr, loader, 78, &tmp); |
---|
1019 | if(tmp != 78) |
---|
1020 | return -1; |
---|
1021 | |
---|
1022 | return 0; |
---|
1023 | } |
---|
1024 | |
---|
1025 | static void *get_entry(char const *name) |
---|
1026 | { |
---|
1027 | PIMAGE_DOS_HEADER dos; |
---|
1028 | PIMAGE_NT_HEADERS nt; |
---|
1029 | void *file, *map, *base; |
---|
1030 | |
---|
1031 | file = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, |
---|
1032 | NULL, OPEN_EXISTING, 0, NULL); |
---|
1033 | if(file == INVALID_HANDLE_VALUE) |
---|
1034 | return NULL; |
---|
1035 | |
---|
1036 | map = CreateFileMapping(file, NULL, PAGE_READONLY, 0, 0, NULL); |
---|
1037 | if(!map) |
---|
1038 | { |
---|
1039 | CloseHandle(file); |
---|
1040 | return NULL; |
---|
1041 | } |
---|
1042 | |
---|
1043 | base = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0); |
---|
1044 | if(!base) |
---|
1045 | { |
---|
1046 | CloseHandle(map); |
---|
1047 | CloseHandle(file); |
---|
1048 | return NULL; |
---|
1049 | } |
---|
1050 | |
---|
1051 | /* Sanity checks */ |
---|
1052 | dos = (PIMAGE_DOS_HEADER)base; |
---|
1053 | nt = (PIMAGE_NT_HEADERS)((char *)base + dos->e_lfanew); |
---|
1054 | if(dos->e_magic != IMAGE_DOS_SIGNATURE |
---|
1055 | || nt->Signature != IMAGE_NT_SIGNATURE |
---|
1056 | || nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386 |
---|
1057 | || nt->OptionalHeader.Magic != 0x10b /* IMAGE_NT_OPTIONAL_HDR32_MAGIC */) |
---|
1058 | { |
---|
1059 | UnmapViewOfFile(base); |
---|
1060 | CloseHandle(map); |
---|
1061 | CloseHandle(file); |
---|
1062 | return NULL; |
---|
1063 | } |
---|
1064 | |
---|
1065 | return (void *)(uintptr_t)(nt->OptionalHeader.ImageBase + |
---|
1066 | nt->OptionalHeader.AddressOfEntryPoint); |
---|
1067 | } |
---|
1068 | #endif |
---|
1069 | |
---|
1070 | static void version(void) |
---|
1071 | { |
---|
1072 | printf("zzuf %s\n", VERSION); |
---|
1073 | printf("Copyright (C) 2002, 2007 Sam Hocevar <sam@zoy.org>\n"); |
---|
1074 | printf("This is free software. You may redistribute copies of it under the\n"); |
---|
1075 | printf("terms of the Do What The Fuck You Want To Public License, Version 2\n"); |
---|
1076 | printf("<http://sam.zoy.org/wtfpl/>.\n"); |
---|
1077 | printf("There is NO WARRANTY, to the extent permitted by law.\n"); |
---|
1078 | printf("\n"); |
---|
1079 | printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n"); |
---|
1080 | } |
---|
1081 | |
---|
1082 | static void usage(void) |
---|
1083 | { |
---|
1084 | #if defined HAVE_REGEX_H |
---|
1085 | printf("Usage: zzuf [-AcdimnqSvx] [-s seed|-s start:stop] [-r ratio|-r min:max]\n"); |
---|
1086 | #else |
---|
1087 | printf("Usage: zzuf [-AdimnqSvx] [-s seed|-s start:stop] [-r ratio|-r min:max]\n"); |
---|
1088 | #endif |
---|
1089 | printf(" [-f fuzzing] [-D delay] [-F forks] [-C crashes] [-B bytes]\n"); |
---|
1090 | #if defined HAVE_SETRLIMIT |
---|
1091 | printf(" [-T seconds] [-M bytes] [-b ranges] [-P protect] [-R refuse]\n"); |
---|
1092 | #else |
---|
1093 | printf(" [-T seconds] [-b ranges] [-P protect] [-R refuse]\n"); |
---|
1094 | #endif |
---|
1095 | #if defined HAVE_REGEX_H |
---|
1096 | printf(" [-p descriptors] [-I include] [-E exclude]\n"); |
---|
1097 | printf(" [PROGRAM [--] [ARGS]...]\n"); |
---|
1098 | #else |
---|
1099 | printf(" [-I include] [-E exclude] [PROGRAM [--] [ARGS]...]\n"); |
---|
1100 | #endif |
---|
1101 | printf(" zzuf -h | --help\n"); |
---|
1102 | printf(" zzuf -V | --version\n"); |
---|
1103 | printf("Run PROGRAM with optional arguments ARGS and fuzz its input.\n"); |
---|
1104 | printf("\n"); |
---|
1105 | printf("Mandatory arguments to long options are mandatory for short options too.\n"); |
---|
1106 | printf(" -A, --autoinc increment seed each time a new file is opened\n"); |
---|
1107 | printf(" -b, --bytes <ranges> only fuzz bytes at offsets within <ranges>\n"); |
---|
1108 | printf(" -B, --max-bytes <n> kill children that output more than <n> bytes\n"); |
---|
1109 | #if defined HAVE_REGEX_H |
---|
1110 | printf(" -c, --cmdline only fuzz files specified in the command line\n"); |
---|
1111 | #endif |
---|
1112 | printf(" -C, --max-crashes <n> stop after <n> children have crashed (default 1)\n"); |
---|
1113 | printf(" -d, --debug print debug messages\n"); |
---|
1114 | printf(" -D, --delay delay between forks\n"); |
---|
1115 | #if defined HAVE_REGEX_H |
---|
1116 | printf(" -E, --exclude <regex> do not fuzz files matching <regex>\n"); |
---|
1117 | #endif |
---|
1118 | printf(" -f, --fuzzing <mode> use fuzzing mode <mode> ([xor] set unset)\n"); |
---|
1119 | printf(" -F, --max-forks <n> number of concurrent children (default 1)\n"); |
---|
1120 | printf(" -i, --stdin fuzz standard input\n"); |
---|
1121 | #if defined HAVE_REGEX_H |
---|
1122 | printf(" -I, --include <regex> only fuzz files matching <regex>\n"); |
---|
1123 | #endif |
---|
1124 | printf(" -m, --md5 compute the output's MD5 hash\n"); |
---|
1125 | #if defined HAVE_SETRLIMIT |
---|
1126 | printf(" -M, --max-memory <n> maximum child virtual memory size in MB\n"); |
---|
1127 | #endif |
---|
1128 | printf(" -n, --network fuzz network input\n"); |
---|
1129 | printf(" -p, --pick <list> only fuzz Nth descriptor with N in <list>\n"); |
---|
1130 | printf(" -P, --protect <list> protect bytes and characters in <list>\n"); |
---|
1131 | printf(" -q, --quiet do not print children's messages\n"); |
---|
1132 | printf(" -r, --ratio <ratio> bit fuzzing ratio (default %g)\n", DEFAULT_RATIO); |
---|
1133 | printf(" --ratio <start:stop> specify a ratio range\n"); |
---|
1134 | printf(" -R, --refuse <list> refuse bytes and characters in <list>\n"); |
---|
1135 | printf(" -s, --seed <seed> random seed (default %i)\n", DEFAULT_SEED); |
---|
1136 | printf(" --seed <start:stop> specify a seed range\n"); |
---|
1137 | printf(" -S, --signal prevent children from diverting crashing signals\n"); |
---|
1138 | printf(" -T, --max-time <n> kill children that run for more than <n> seconds\n"); |
---|
1139 | printf(" -v, --verbose print information during the run\n"); |
---|
1140 | printf(" -x, --check-exit report processes that exit with a non-zero status\n"); |
---|
1141 | printf(" -h, --help display this help and exit\n"); |
---|
1142 | printf(" -V, --version output version information and exit\n"); |
---|
1143 | printf("\n"); |
---|
1144 | printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n"); |
---|
1145 | } |
---|
1146 | |
---|