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