[1466] | 1 | /* |
---|
| 2 | * zzuf - general purpose fuzzer |
---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
| 5 | * |
---|
| 6 | * $Id: libzzuf.c 2527 2008-07-15 20:16:06Z 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 |
---|
[1701] | 27 | #if defined HAVE_WINDOWS_H |
---|
| 28 | # include <windows.h> |
---|
| 29 | #endif |
---|
[1730] | 30 | #if defined HAVE_PROCESS_H |
---|
| 31 | # include <process.h> |
---|
| 32 | #endif |
---|
[1466] | 33 | #include <stdio.h> |
---|
[1662] | 34 | #include <sys/types.h> |
---|
[1728] | 35 | #if defined HAVE_UNISTD_H |
---|
| 36 | # include <unistd.h> |
---|
| 37 | #endif |
---|
[1466] | 38 | #include <stdlib.h> |
---|
[1554] | 39 | #include <string.h> |
---|
[1466] | 40 | #include <fcntl.h> |
---|
| 41 | |
---|
| 42 | #include <stdarg.h> |
---|
| 43 | |
---|
[1470] | 44 | #include "libzzuf.h" |
---|
| 45 | #include "debug.h" |
---|
[1613] | 46 | #include "fd.h" |
---|
[1701] | 47 | #include "sys.h" |
---|
[1613] | 48 | #include "fuzz.h" |
---|
[1467] | 49 | |
---|
[1701] | 50 | /* Library initialisation shit */ |
---|
[2527] | 51 | #if defined __GNUC__ |
---|
[1701] | 52 | void _zz_init(void) __attribute__((constructor)); |
---|
| 53 | void _zz_fini(void) __attribute__((destructor)); |
---|
[2527] | 54 | #elif defined HAVE_PRAGMA_INIT |
---|
| 55 | # pragma INIT "_zz_init" |
---|
| 56 | # pragma FINI "_zz_fini" |
---|
| 57 | #endif |
---|
| 58 | |
---|
[1701] | 59 | #if defined HAVE_WINDOWS_H |
---|
| 60 | BOOL WINAPI DllMain(HINSTANCE, DWORD, PVOID); |
---|
| 61 | #endif |
---|
| 62 | |
---|
[2336] | 63 | /** |
---|
| 64 | * Is libzzuf fully initialised? |
---|
| 65 | */ |
---|
[1523] | 66 | int _zz_ready = 0; |
---|
[2336] | 67 | |
---|
| 68 | /** |
---|
| 69 | * The file descriptor used by libzzuf for communication with the main |
---|
| 70 | * zzuf program in debug mode. Its value is set by the ZZUF_DEBUG |
---|
| 71 | * environment variable. |
---|
| 72 | */ |
---|
[1718] | 73 | int _zz_debugfd = -1; |
---|
[2336] | 74 | |
---|
| 75 | /** |
---|
| 76 | * If set to 1, this boolean variable will prevent the called application |
---|
| 77 | * from installing signal handlers that would prevent it from really crashing. |
---|
| 78 | * SDL applications often do that when not using SDL_INIT_NOPARACHUTE, for |
---|
| 79 | * instance. Its value is set by the ZZUF_SIGNAL environment variable. |
---|
| 80 | */ |
---|
[1532] | 81 | int _zz_signal = 0; |
---|
[2336] | 82 | |
---|
| 83 | /** |
---|
| 84 | * If set to a positive value, this value will indicate the maximum number |
---|
[2345] | 85 | * of mebibytes (1 MiB = 1,048,576 bytes) that the called application will be |
---|
| 86 | * allowed to allocate. Its value is set by the ZZUF_MEMORY environment |
---|
| 87 | * variable. |
---|
[2336] | 88 | */ |
---|
[1641] | 89 | int _zz_memory = 0; |
---|
[2336] | 90 | |
---|
| 91 | /** |
---|
| 92 | * If set to 1, this boolean will tell libzzuf to fuzz network file |
---|
| 93 | * descriptors, too. Its value is set by the ZZUF_NETWORK environment |
---|
| 94 | * variable. |
---|
| 95 | */ |
---|
[1560] | 96 | int _zz_network = 0; |
---|
[1466] | 97 | |
---|
[2336] | 98 | /** |
---|
| 99 | * Library initialisation routine. |
---|
| 100 | * |
---|
| 101 | * This function reads all configuration variables put by zzuf in the |
---|
| 102 | * called process's environment and initialises diversions for the three |
---|
| 103 | * main function families: memory functions (initialised very early because |
---|
| 104 | * other functions we need such as dlsym() require them), file descriptor |
---|
| 105 | * functions and stream functions. |
---|
| 106 | */ |
---|
[1523] | 107 | void _zz_init(void) |
---|
[1466] | 108 | { |
---|
[1672] | 109 | char *tmp, *tmp2; |
---|
[1466] | 110 | |
---|
| 111 | tmp = getenv("ZZUF_DEBUG"); |
---|
[1718] | 112 | if(tmp) |
---|
| 113 | _zz_debugfd = atoi(tmp); |
---|
[1466] | 114 | |
---|
[2354] | 115 | /* We need this as soon as possible */ |
---|
| 116 | _zz_mem_init(); |
---|
| 117 | |
---|
[1470] | 118 | tmp = getenv("ZZUF_SEED"); |
---|
| 119 | if(tmp && *tmp) |
---|
[1613] | 120 | _zz_setseed(atol(tmp)); |
---|
[1466] | 121 | |
---|
[1672] | 122 | tmp = getenv("ZZUF_MINRATIO"); |
---|
| 123 | tmp2 = getenv("ZZUF_MAXRATIO"); |
---|
| 124 | if(tmp && *tmp && tmp2 && *tmp2) |
---|
| 125 | _zz_setratio(atof(tmp), atof(tmp2)); |
---|
[1466] | 126 | |
---|
[1663] | 127 | tmp = getenv("ZZUF_AUTOINC"); |
---|
| 128 | if(tmp && *tmp == '1') |
---|
| 129 | _zz_setautoinc(); |
---|
| 130 | |
---|
[1705] | 131 | tmp = getenv("ZZUF_BYTES"); |
---|
| 132 | if(tmp && *tmp) |
---|
| 133 | _zz_bytes(tmp); |
---|
| 134 | |
---|
[1858] | 135 | tmp = getenv("ZZUF_LIST"); |
---|
[1791] | 136 | if(tmp && *tmp) |
---|
[1858] | 137 | _zz_list(tmp); |
---|
[1791] | 138 | |
---|
[1858] | 139 | tmp = getenv("ZZUF_PORTS"); |
---|
| 140 | if(tmp && *tmp) |
---|
| 141 | _zz_ports(tmp); |
---|
| 142 | |
---|
[1554] | 143 | tmp = getenv("ZZUF_PROTECT"); |
---|
| 144 | if(tmp && *tmp) |
---|
[1614] | 145 | _zz_protect(tmp); |
---|
[1554] | 146 | |
---|
[1555] | 147 | tmp = getenv("ZZUF_REFUSE"); |
---|
| 148 | if(tmp && *tmp) |
---|
[1614] | 149 | _zz_refuse(tmp); |
---|
[1555] | 150 | |
---|
[1472] | 151 | tmp = getenv("ZZUF_INCLUDE"); |
---|
| 152 | if(tmp && *tmp) |
---|
[1621] | 153 | _zz_include(tmp); |
---|
[1472] | 154 | |
---|
| 155 | tmp = getenv("ZZUF_EXCLUDE"); |
---|
| 156 | if(tmp && *tmp) |
---|
[1621] | 157 | _zz_exclude(tmp); |
---|
[1472] | 158 | |
---|
[1532] | 159 | tmp = getenv("ZZUF_SIGNAL"); |
---|
| 160 | if(tmp && *tmp == '1') |
---|
| 161 | _zz_signal = 1; |
---|
| 162 | |
---|
[1641] | 163 | tmp = getenv("ZZUF_MEMORY"); |
---|
| 164 | if(tmp && *tmp == '1') |
---|
| 165 | _zz_memory = 1; |
---|
| 166 | |
---|
[1560] | 167 | tmp = getenv("ZZUF_NETWORK"); |
---|
| 168 | if(tmp && *tmp == '1') |
---|
| 169 | _zz_network = 1; |
---|
| 170 | |
---|
[1524] | 171 | _zz_fd_init(); |
---|
[1701] | 172 | _zz_sys_init(); |
---|
[1473] | 173 | |
---|
[1526] | 174 | tmp = getenv("ZZUF_STDIN"); |
---|
| 175 | if(tmp && *tmp == '1') |
---|
| 176 | _zz_register(0); |
---|
| 177 | |
---|
[1523] | 178 | _zz_ready = 1; |
---|
[1490] | 179 | |
---|
[1662] | 180 | debug("libzzuf initialised for PID %li", (long int)getpid()); |
---|
[1466] | 181 | } |
---|
| 182 | |
---|
[2336] | 183 | /** |
---|
| 184 | * Library deinitialisation routine. |
---|
| 185 | * |
---|
| 186 | * Free all the memory allocated by libzzuf during its lifetime. |
---|
| 187 | */ |
---|
[1523] | 188 | void _zz_fini(void) |
---|
[1466] | 189 | { |
---|
[1524] | 190 | _zz_fd_fini(); |
---|
[1523] | 191 | } |
---|
| 192 | |
---|
[1701] | 193 | #if defined HAVE_WINDOWS_H |
---|
| 194 | BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, PVOID impLoad) |
---|
| 195 | { |
---|
| 196 | (void)hinst; /* unused */ |
---|
| 197 | (void)impLoad; /* unused */ |
---|
| 198 | |
---|
| 199 | switch(reason) |
---|
| 200 | { |
---|
| 201 | case DLL_PROCESS_ATTACH: |
---|
| 202 | _zz_init(); |
---|
| 203 | break; |
---|
| 204 | case DLL_PROCESS_DETACH: |
---|
| 205 | _zz_fini(); |
---|
| 206 | break; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | return TRUE; |
---|
| 210 | } |
---|
| 211 | #endif |
---|
[2336] | 212 | |
---|