1 | /* |
---|
2 | * zzuf - general purpose fuzzer |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: libzzuf.c 1728 2007-02-01 16:08:33Z 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 | * libzzuf.c: preloaded wrapper library |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | #define _GNU_SOURCE |
---|
21 | |
---|
22 | #if defined HAVE_STDINT_H |
---|
23 | # include <stdint.h> |
---|
24 | #elif defined HAVE_INTTYPES_H |
---|
25 | # include <inttypes.h> |
---|
26 | #endif |
---|
27 | #if defined HAVE_WINDOWS_H |
---|
28 | # include <windows.h> |
---|
29 | #endif |
---|
30 | #include <stdio.h> |
---|
31 | #include <sys/types.h> |
---|
32 | #if defined HAVE_UNISTD_H |
---|
33 | # include <unistd.h> |
---|
34 | #endif |
---|
35 | #include <stdlib.h> |
---|
36 | #include <string.h> |
---|
37 | #include <fcntl.h> |
---|
38 | |
---|
39 | #include <stdarg.h> |
---|
40 | |
---|
41 | #include "libzzuf.h" |
---|
42 | #include "debug.h" |
---|
43 | #include "fd.h" |
---|
44 | #include "sys.h" |
---|
45 | #include "fuzz.h" |
---|
46 | |
---|
47 | /* Library initialisation shit */ |
---|
48 | void _zz_init(void) __attribute__((constructor)); |
---|
49 | void _zz_fini(void) __attribute__((destructor)); |
---|
50 | #if defined HAVE_WINDOWS_H |
---|
51 | BOOL WINAPI DllMain(HINSTANCE, DWORD, PVOID); |
---|
52 | #endif |
---|
53 | |
---|
54 | /* Global variables */ |
---|
55 | int _zz_ready = 0; |
---|
56 | int _zz_debugfd = -1; |
---|
57 | int _zz_signal = 0; |
---|
58 | int _zz_memory = 0; |
---|
59 | int _zz_network = 0; |
---|
60 | |
---|
61 | /* Library initialisation shit */ |
---|
62 | void _zz_init(void) |
---|
63 | { |
---|
64 | char *tmp, *tmp2; |
---|
65 | |
---|
66 | /* We need this as soon as possible */ |
---|
67 | _zz_mem_init(); |
---|
68 | |
---|
69 | tmp = getenv("ZZUF_DEBUG"); |
---|
70 | if(tmp) |
---|
71 | _zz_debugfd = atoi(tmp); |
---|
72 | |
---|
73 | tmp = getenv("ZZUF_SEED"); |
---|
74 | if(tmp && *tmp) |
---|
75 | _zz_setseed(atol(tmp)); |
---|
76 | |
---|
77 | tmp = getenv("ZZUF_MINRATIO"); |
---|
78 | tmp2 = getenv("ZZUF_MAXRATIO"); |
---|
79 | if(tmp && *tmp && tmp2 && *tmp2) |
---|
80 | _zz_setratio(atof(tmp), atof(tmp2)); |
---|
81 | |
---|
82 | tmp = getenv("ZZUF_AUTOINC"); |
---|
83 | if(tmp && *tmp == '1') |
---|
84 | _zz_setautoinc(); |
---|
85 | |
---|
86 | tmp = getenv("ZZUF_BYTES"); |
---|
87 | if(tmp && *tmp) |
---|
88 | _zz_bytes(tmp); |
---|
89 | |
---|
90 | tmp = getenv("ZZUF_PROTECT"); |
---|
91 | if(tmp && *tmp) |
---|
92 | _zz_protect(tmp); |
---|
93 | |
---|
94 | tmp = getenv("ZZUF_REFUSE"); |
---|
95 | if(tmp && *tmp) |
---|
96 | _zz_refuse(tmp); |
---|
97 | |
---|
98 | tmp = getenv("ZZUF_INCLUDE"); |
---|
99 | if(tmp && *tmp) |
---|
100 | _zz_include(tmp); |
---|
101 | |
---|
102 | tmp = getenv("ZZUF_EXCLUDE"); |
---|
103 | if(tmp && *tmp) |
---|
104 | _zz_exclude(tmp); |
---|
105 | |
---|
106 | tmp = getenv("ZZUF_SIGNAL"); |
---|
107 | if(tmp && *tmp == '1') |
---|
108 | _zz_signal = 1; |
---|
109 | |
---|
110 | tmp = getenv("ZZUF_MEMORY"); |
---|
111 | if(tmp && *tmp == '1') |
---|
112 | _zz_memory = 1; |
---|
113 | |
---|
114 | tmp = getenv("ZZUF_NETWORK"); |
---|
115 | if(tmp && *tmp == '1') |
---|
116 | _zz_network = 1; |
---|
117 | |
---|
118 | _zz_fd_init(); |
---|
119 | _zz_sys_init(); |
---|
120 | |
---|
121 | tmp = getenv("ZZUF_STDIN"); |
---|
122 | if(tmp && *tmp == '1') |
---|
123 | _zz_register(0); |
---|
124 | |
---|
125 | _zz_ready = 1; |
---|
126 | |
---|
127 | debug("libzzuf initialised for PID %li", (long int)getpid()); |
---|
128 | } |
---|
129 | |
---|
130 | /* Deinitialisation */ |
---|
131 | void _zz_fini(void) |
---|
132 | { |
---|
133 | _zz_fd_fini(); |
---|
134 | } |
---|
135 | |
---|
136 | #if defined HAVE_WINDOWS_H |
---|
137 | BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, PVOID impLoad) |
---|
138 | { |
---|
139 | (void)hinst; /* unused */ |
---|
140 | (void)impLoad; /* unused */ |
---|
141 | |
---|
142 | switch(reason) |
---|
143 | { |
---|
144 | case DLL_PROCESS_ATTACH: |
---|
145 | _zz_init(); |
---|
146 | break; |
---|
147 | case DLL_PROCESS_DETACH: |
---|
148 | _zz_fini(); |
---|
149 | break; |
---|
150 | } |
---|
151 | |
---|
152 | return TRUE; |
---|
153 | } |
---|
154 | #endif |
---|