The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
fuzzer.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 03353ac43236c1716611c56e926c39e02c5713ce $
19 *
20 * @file src/bin/fuzzer.c
21 * @brief Functions to fuzz protocol decoding
22 *
23 * @copyright 2019 Network RADIUS SAS (legal@networkradius.com)
24 */
25RCSID("$Id: 03353ac43236c1716611c56e926c39e02c5713ce $")
26
27#include <freeradius-devel/util/dl.h>
28#include <freeradius-devel/util/conf.h>
29#include <freeradius-devel/util/dict.h>
30#include <freeradius-devel/util/atexit.h>
31#include <freeradius-devel/util/syserror.h>
32#include <freeradius-devel/util/strerror.h>
33#include <freeradius-devel/io/test_point.h>
34
35/*
36 * Run from the source directory via:
37 *
38 * ./build/make/jlibtool --mode=execute ./build/bin/local/fuzzer_radius -D share/dictionary /path/to/corpus/directory/
39 */
40
41static bool init = false;
42static dl_t *dl = NULL;
45static TALLOC_CTX *autofree = NULL;
46
47static fr_dict_t *dict = NULL;
48
50
51int LLVMFuzzerInitialize(int *argc, char ***argv);
52int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
53
54static void exitHandler(void)
55{
57
58 fr_dict_free(&dict, __FILE__);
59
60 if (dl && dl->handle) {
61 dlclose(dl->handle);
62 dl->handle = NULL;
63 }
65
67
68 /*
69 * Ensure our atexit handlers run before any other
70 * atexit handlers registered by third party libraries.
71 */
73}
74
75static inline
76fr_dict_protocol_t *fuzzer_dict_init(void *dl_handle, char const *proto)
77{
78 char buffer[256];
79 fr_dict_protocol_t *our_dl_proto;
80
81 snprintf(buffer, sizeof(buffer), "libfreeradius_%s_dict_protocol", proto);
82
83 our_dl_proto = dlsym(dl_handle, buffer);
84 if (our_dl_proto && our_dl_proto->init() && (our_dl_proto->init() < 0)) {
85 fr_perror("fuzzer: Failed initializing library %s", buffer);
86 fr_exit_now(EXIT_FAILURE);
87 }
88
89 return our_dl_proto;
90}
91
92int LLVMFuzzerInitialize(int *argc, char ***argv)
93{
94 char const *lib_dir = getenv("FR_LIBRARY_PATH");
95 char const *proto = getenv("FR_LIBRARY_FUZZ_PROTOCOL");
96 char const *dict_dir = getenv("FR_DICTIONARY_DIR");
97 char const *debug_lvl_str = getenv("FR_DEBUG_LVL");
98 char const *panic_action = getenv("PANIC_ACTION");
99 char const *p;
100#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
101 char *dict_dir_to_free = NULL;
102 char *lib_dir_to_free = NULL;
103#endif
104
105 if (!argc || !argv || !*argv) return -1; /* shut up clang scan */
106
107 if (debug_lvl_str) fr_debug_lvl = atoi(debug_lvl_str);
108
109 /*
110 * Setup atexit handlers to free any thread local
111 * memory on exit
112 */
114
115 /*
116 * Initialise the talloc fault handlers.
117 */
119
120 /*
121 * Initialise the error stack _before_ we run any
122 * tests so there's no chance of the memory
123 * appearing as a leak the first time an error
124 * is generated.
125 */
126 fr_strerror_const("fuzz"); /* allocate the pools */
127 fr_strerror_clear(); /* clears the message, leaves the pools */
128
129 /*
130 * Setup our own internal atexit handler
131 */
132 if (atexit(exitHandler)) {
133 fr_perror("fuzzer: Failed to register exit handler: %s", fr_syserror(errno));
134 fr_exit_now(EXIT_FAILURE);
135 }
136
137 /*
138 * Get the name from the binary name of fuzzer_foo
139 */
140 if (!proto) {
141 proto = strrchr((*argv)[0], '_');
142 if (proto) proto++;
143 }
144
145 /*
146 * Look for -D dir
147 *
148 * If found, nuke it from the argument list.
149 */
150 if (!dict_dir) {
151 int i, j;
152
153 for (i = 0; i < *argc - 1; i++) {
154 p = (*argv)[i];
155
156 if ((p[0] == '-') && (p[1] == 'D')) {
157 dict_dir = (*argv)[i + 1];
158
159 for (j = i + 2; j < *argc; i++, j++) {
160 (*argv)[i] = (*argv)[j];
161 }
162
163 *argc -= 2;
164 break;
165 }
166 }
167 }
168
169#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
170 /*
171 * oss-fuzz puts the dictionaries, etc. into subdirectories named after the location of the
172 * binary. So we find the directory of the binary, and append "/dict" or "/lib" to find
173 * dictionaries and libraries.
174 */
175 p = strrchr((*argv)[0], '/');
176 if (p) {
177 if (!dict_dir) {
178 dict_dir = dict_dir_to_free = talloc_asprintf(NULL, "%.*s/dict", (int) (p - (*argv)[0]), (*argv)[0]);
179 if (!dict_dir_to_free) fr_exit_now(EXIT_FAILURE);
180 }
181
182 if (!lib_dir) {
183 lib_dir = lib_dir_to_free = talloc_asprintf(NULL, "%.*s/lib", (int) (p - (*argv)[0]), (*argv)[0]);
184 if (!lib_dir_to_free) fr_exit_now(EXIT_FAILURE);
185 }
186 }
187#endif
188
189 if (!dict_dir) dict_dir = DICTDIR;
190 if (!lib_dir) lib_dir = LIBDIR;
191
192 /*
193 * Set the global search path for all dynamic libraries we load.
194 */
195 if (dl_search_global_path_set(lib_dir) < 0) {
196 fr_perror("fuzzer: Failed setting library path");
197 fr_exit_now(EXIT_FAILURE);
198 }
199
200 /*
201 * When jobs=N is specified the fuzzer spawns worker processes via
202 * a shell. We have removed any -D dictdir argument that were
203 * supplied, so we pass it to our children via the environment.
204 */
205 if (setenv("FR_DICTIONARY_DIR", dict_dir, 1)) {
206 fprintf(stderr, "Failed to set FR_DICTIONARY_DIR env variable\n");
207 fr_exit_now(EXIT_FAILURE);
208 }
209
210 if (!fr_dict_global_ctx_init(NULL, true, dict_dir)) {
211 fr_perror("dict_global");
212 fr_exit_now(EXIT_FAILURE);
213 }
214
216 fr_perror("fuzzer: Failed initializing internal dictionary");
217 fr_exit_now(EXIT_FAILURE);
218 }
219
220 if (!proto) {
221 fr_perror("Failed to find protocol for fuzzer");
222 fr_exit_now(EXIT_FAILURE);
223 }
224
225 /*
226 * Disable hostname lookups, so we don't produce spurious DNS
227 * queries, and there's no chance of spurious failures if
228 * it takes a long time to get a response.
229 */
231
232 /*
233 * Search in our symbol space first. We may have been dynamically
234 * or statically linked to the library we're fuzzing...
235 */
236 dl_proto = fuzzer_dict_init(RTLD_DEFAULT, proto);
237
238 if (panic_action) {
240
241 if (fr_fault_setup(autofree, panic_action, (*argv)[0]) < 0) {
242 fr_perror("Failed initializing panic action");
243 fr_exit_now(EXIT_FAILURE);
244 }
245 }
246
247 init = true;
248
249#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
250 talloc_free(dict_dir_to_free);
251 talloc_free(lib_dir_to_free);
252#endif
253
254 return 1;
255}
256
257int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
258{
259 TALLOC_CTX * ctx = talloc_init_const("fuzzer");
260 fr_pair_list_t vps;
261 void *decode_ctx = NULL;
263
264 fr_pair_list_init(&vps);
265 if (!init) LLVMFuzzerInitialize(NULL, NULL);
266
267 if (tp->test_ctx && (tp->test_ctx(&decode_ctx, NULL, dict) < 0)) {
268 fr_perror("fuzzer: Failed initializing test point decode_ctx");
269 fr_exit_now(EXIT_FAILURE);
270 }
271
272 tp->func(ctx, &vps, buf, len, decode_ctx);
273 if (fr_debug_lvl > 3) fr_pair_list_debug(&vps);
274
275 talloc_free(decode_ctx);
276 talloc_free(ctx);
277
278 /*
279 * Clear error messages from the run. Clearing these
280 * keeps malloc/free balanced, which helps to avoid the
281 * fuzzers leak heuristics from firing.
282 */
284
285 return 0;
286}
static int const char char buffer[256]
Definition acutest.h:576
int fr_atexit_global_setup(void)
Setup the atexit handler, should be called at the start of a program's execution.
Definition atexit.c:160
int fr_atexit_global_trigger_all(void)
Cause all global free triggers to fire.
Definition atexit.c:286
static dl_loader_t * dl_loader
Definition fuzzer.c:43
static TALLOC_CTX * autofree
Definition fuzzer.c:45
fr_test_point_proto_decode_t XX_PROTOCOL_XX_tp_decode_proto
static dl_t * dl
Definition fuzzer.c:42
static fr_dict_t * dict
Definition fuzzer.c:47
static fr_dict_protocol_t * dl_proto
Definition fuzzer.c:44
static bool init
Definition fuzzer.c:41
static fr_dict_protocol_t * fuzzer_dict_init(void *dl_handle, char const *proto)
Definition fuzzer.c:76
int LLVMFuzzerInitialize(int *argc, char ***argv)
Definition fuzzer.c:92
static void exitHandler(void)
Definition fuzzer.c:54
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
Definition fuzzer.c:257
#define RCSID(id)
Definition build.h:485
static char panic_action[512]
The command to execute when panicking.
Definition debug.c:96
int fr_fault_setup(TALLOC_CTX *ctx, char const *cmd, char const *program)
Registers signal handlers to execute panic_action on fatal signal.
Definition debug.c:1242
void fr_talloc_fault_setup(void)
Register talloc fault handlers.
Definition debug.c:1223
#define fr_exit_now(_x)
Exit without calling atexit() handlers, producing a log message in debug builds.
Definition debug.h:234
fr_dict_protocol_free_t free
free the library
Definition dict.h:463
fr_dict_gctx_t * fr_dict_global_ctx_init(TALLOC_CTX *ctx, bool free_at_exit, char const *dict_dir)
Initialise the global protocol hashes.
Definition dict_util.c:4436
int fr_dict_internal_afrom_file(fr_dict_t **out, char const *internal_name, char const *dependent)
(Re-)Initialize the special internal dictionary
fr_dict_protocol_init_t init
initialize the library
Definition dict.h:462
int fr_dict_free(fr_dict_t **dict, char const *dependent)
Decrement the reference count on a previously loaded dictionary.
Definition dict_util.c:4068
Protocol-specific callbacks in libfreeradius-PROTOCOL.
Definition dict.h:433
int dl_search_global_path_set(char const *lib_dir)
Set the global library path.
Definition dl.c:768
A dynamic loader.
Definition dl.c:81
void * handle
Handle returned by dlopen.
Definition dl.h:62
Module handle.
Definition dl.h:58
bool fr_hostname_lookups
hostname -> IP lookups?
Definition inet.c:53
bool fr_reverse_lookups
IP -> hostname lookups?
Definition inet.c:52
talloc_free(reap)
int fr_debug_lvl
Definition log.c:40
unsigned char uint8_t
void fr_pair_list_init(fr_pair_list_t *list)
Initialise a pair list header.
Definition pair.c:46
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:112
#define talloc_autofree_context
The original function is deprecated, so replace it with our version.
Definition talloc.h:51
fr_tp_proto_decode_t func
Decoder for proto layer.
Definition test_point.h:68
fr_test_point_ctx_alloc_t test_ctx
Allocate a test ctx for the encoder.
Definition test_point.h:67
Entry point for protocol decoders.
Definition test_point.h:66
#define FR_DICTIONARY_INTERNAL_DIR
Definition conf.h:8
void fr_pair_list_debug(fr_pair_list_t const *list)
Dumps a list to the default logging destination - Useful for calling from debuggers.
Definition pair_print.c:328
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:732
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
#define fr_strerror_const(_msg)
Definition strerror.h:223