The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
fuzzer_cf.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: e74e4bb7eb1f5d1997db8032c515663fbc37e1e4 $
19 *
20 * @file src/bin/fuzzer_cf.c
21 * @brief Functions to fuzz the FreeRADIUS config-file parser
22 *
23 * Targets cf_file_read() and the section/pair tokenisers it drives
24 * (cf_file.c, cf_util.c, cf_parse.c). The full configuration grammar -
25 * sections, pairs, quoting, line continuation, $INCLUDE / $-INCLUDE
26 * resolution, operators, and xlat expansions - is exercised through
27 * this single entry point.
28 *
29 * The harness writes each fuzzer input to a per-process file under the
30 * system temporary directory because cf_file_read() is path-based and
31 * resolves $INCLUDE relative to the directory of the file being parsed.
32 * A pid-suffixed name keeps the harness safe under libFuzzer's -jobs=N.
33 */
34RCSID("$Id: e74e4bb7eb1f5d1997db8032c515663fbc37e1e4 $")
35
36#include <freeradius-devel/build.h>
37#include <freeradius-devel/server/cf_file.h>
38#include <freeradius-devel/server/cf_util.h>
39#include <freeradius-devel/server/main_config.h>
40#include <freeradius-devel/util/strerror.h>
41
42extern char const *__lsan_default_suppressions(void);
43
44int LLVMFuzzerInitialize(int *argc, char ***argv);
45int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
46
47int LLVMFuzzerInitialize(UNUSED int *argc, UNUSED char ***argv)
48{
49 /*
50 * Don't put output anywhere. Otherwise we will have reams of log messages.
51 */
53 default_log.fd = -1;
56
57 return 0;
58}
59
60int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
61{
63 size_t depth = 0, max_depth = 0;
64
65 /*
66 * cap input size: the parser is line-oriented and a
67 * pathological input can cost a great deal of time without
68 * exposing new states.
69 */
70 if (size > 16 * 1024) return 0;
71
72 /*
73 * Pre-filter on brace nesting depth. The config parser
74 * recurses via C function calls on '{'-introduced
75 * sub-sections (parse_subrequest, parse_foreach,
76 * parse_switch, etc., and cf_section_pass2 walking the
77 * section tree). libFuzzer trivially discovers inputs of
78 * the form "{{{{ ... }}}}" that exhaust the C stack without
79 * revealing any new parser states. Real configs nest fewer
80 * than ten levels deep; the cap is set generously here so
81 * that any legitimate nesting still reaches the parser.
82 *
83 * Quoting is intentionally ignored: a conservative count
84 * can only over-reject, never under-reject, and the cost of
85 * dropping a few well-formed inputs with '{' embedded in
86 * strings is negligible compared with the cost of burning
87 * every fuzz cycle on the same recursion failure.
88 */
89 for (size_t i = 0; i < size; i++) {
90 if (!data[i]) {
91 size = i;
92 break;
93 }
94
95 if (data[i] == '{') {
96 depth++;
97 if (depth > max_depth) max_depth = depth;
98 } else if ((data[i] == '}') && (depth > 0)) {
99 depth--;
100 }
101 }
102 if (max_depth > 64) return 0;
103
105 if (!config) return 0;
106
107 config->root_cs = cf_section_alloc(config, NULL, "main", NULL);
108 if (!config->root_cs) {
110 return 0;
111 }
113
114 (void) cf_file_read_buffer(config->root_cs, (char const *) data, size, "/");
115
117
118 /*
119 * Clear error messages from the run, keeping malloc/free
120 * balanced so the fuzzer's leak heuristics do not fire.
121 */
123
124 return 0;
125}
#define RCSID(id)
Definition build.h:512
#define UNUSED
Definition build.h:336
void cf_section_set_unlang(CONF_SECTION *cs)
Definition cf_file.c:4449
int cf_file_read_buffer(CONF_SECTION *cs, char const *buffer, size_t buflen, char const *filename)
Bootstrap a configuration section from an in-memory buffer.
Definition cf_file.c:3967
#define cf_section_alloc(_ctx, _parent, _name1, _name2)
Definition cf_util.h:201
int LLVMFuzzerInitialize(int *argc, char ***argv)
Definition fuzzer.c:46
char const * __lsan_default_suppressions(void)
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
Definition fuzzer_cf.c:60
talloc_free(hp)
fr_log_t default_log
Definition log.c:306
@ L_DST_NULL
Discard log messages.
Definition log.h:80
main_config_t * main_config_alloc(TALLOC_CTX *ctx)
Allocate a main_config_t struct, setting defaults.
Main server configuration.
Definition main_config.h:51
unsigned char uint8_t
static uint8_t depth(fr_minmax_heap_index_t i)
Definition minmax_heap.c:83
static const conf_parser_t config[]
Definition base.c:163
fr_log_dst_t dst
Log destination.
Definition log.h:94
int fd
File descriptor to write messages to.
Definition log.h:109
bool suppress_secrets
suppress secrets when printing to this destination
Definition log.h:105
bool print_level
sometimes we don't want log levels printed
Definition log.h:103
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:581
static fr_slen_t data
Definition value.h:1340