source: zzuf/trunk/src/libzzuf.c @ 3635

Last change on this file since 3635 was 3635, checked in by Sam Hocevar, 11 years ago

Allow remote network host filtering, courtesy of Corentin Delorme.

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