From 05803cab48e26c35de0edf056484ad6be94be64d Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Mon, 30 Nov 2026 08:24:22 +0000 Subject: [PATCH] OSS-Fuzz: Add new fuzzer/corpus/dict targets UCL API Signed-off-by: Arthur Chan --- tests/fuzzers/ucl_api_fuzzer.c | 270 +++++++++++++++++++++++++++ tests/fuzzers/ucl_api_fuzzer.dict | 42 +++++ tests/fuzzers/ucl_api_seed_corpus.py | 75 ++++++++ 3 files changed, 387 insertions(+) create mode 100644 tests/fuzzers/ucl_api_fuzzer.c create mode 100644 tests/fuzzers/ucl_api_fuzzer.dict create mode 100644 tests/fuzzers/ucl_api_seed_corpus.py diff --git a/tests/fuzzers/ucl_api_fuzzer.c b/tests/fuzzers/ucl_api_fuzzer.c new file mode 100644 index 00000000..45029a8d --- /dev/null +++ b/tests/fuzzers/ucl_api_fuzzer.c @@ -0,0 +1,270 @@ +#include +#include +#include +#include "ucl.h" + +/* + * Component-API / option-matrix harness for the parts of libucl that the two + * pre-existing parse-and-free targets never reach: + * + * - parser option state: flags, duplicate strategies, chunk priorities, + * parse types (UCL, msgpack, canonical S-expressions, auto-detection), + * - stateful multi-chunk accumulation and the incremental insertion path + * ucl_parser_insert_chunk() (0% covered, accumulated complexity 2210), + * - custom macro dispatch via the public registration API (reaches the + * macro/argument parsing machinery without any filesystem access), + * - the comments store saved under UCL_PARSER_SAVE_COMMENTS, + * - the object lifecycle API: iteration, key lookup, typed accessors, + * deep copy and merge. + * + * Threat model: libucl consumers feed untrusted configuration text through + * exactly these knobs (rspamd drives parsers with custom flags, macros and + * incremental chunk insertion). Memory-safety bugs in hash handling, key + * merging and object ownership are reachable only through this surface, + * which is why the harness walks, copies and merges the parsed tree instead + * of freeing it immediately. + * + * Input layout: + * data[0] flag bits (KEY_LOWERCASE / NO_TIME / NO_IMPLICIT_ARRAYS / + * SAVE_COMMENTS / DISABLE_MACRO; NO_FILEVARS always on: the + * harness parses strings, never files) + * data[1] duplicate strategy selector + * data[2] parse type selector + * data[3] low nibble: chunk priority, high nibble: chunk count - 2 + * remainder: document area, split into 2..4 chunks + * + * ZEROCOPY is deliberately not exercised: it makes parsed objects reference + * the input buffer, which is a lifetime trap for callers that keep objects + * longer than the input, and fuzzing it would primarily measure harness + * bugs rather than library bugs. + * + * Parser limits are set explicitly: the library documents that processors of + * untrusted data should opt into them via ucl_parser_set_limits(), and the + * depth bound also bounds the recursive walk below. + */ + +#define FUZZ_MAX_DEPTH 64 +#define FUZZ_MAX_NODES (1u << 20) +#define FUZZ_MAX_ALLOC (64u << 20) +#define FUZZ_INSERT_CHUNK_MAX 32 + +static void +set_fuzz_limits(struct ucl_parser *parser) +{ + struct ucl_parser_limits limits = { + .max_depth = FUZZ_MAX_DEPTH, + .max_nodes = FUZZ_MAX_NODES, + .max_alloc = FUZZ_MAX_ALLOC, + }; + + ucl_parser_set_limits(parser, &limits); +} + +/* Absorbs `.fz ` without touching the filesystem: the parser-side + * macro and argument parsing machinery runs, the handler accepts the + * document and contributes no value. */ +static bool +fz_macro_handler(const unsigned char *data, size_t len, + const ucl_object_t *arguments, void *ud) +{ + (void) data; + (void) len; + (void) arguments; + (void) ud; + + return true; +} + +static bool +fz_context_macro_handler(const unsigned char *data, size_t len, + const ucl_object_t *arguments, const ucl_object_t *context, + void *ud) +{ + (void) data; + (void) len; + (void) arguments; + (void) context; + (void) ud; + + return true; +} + +static void +walk_object(const ucl_object_t *obj) +{ + ucl_object_iter_t it = NULL; + const ucl_object_t *cur; + + while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) { + const char *key = ucl_object_key(cur); + + switch (ucl_object_type(cur)) { + case UCL_STRING: + { + size_t len = 0; + const char *s = ucl_object_tolstring(cur, &len); + (void) ucl_object_tostring_forced(cur); + (void) s; + break; + } + case UCL_INT: + (void) ucl_object_toint(cur); + break; + case UCL_FLOAT: + (void) ucl_object_todouble(cur); + break; + case UCL_BOOLEAN: + (void) ucl_object_toboolean(cur); + break; + default: + break; + } + + if (ucl_object_type(cur) == UCL_OBJECT || ucl_object_type(cur) == UCL_ARRAY) { + walk_object(cur); + } + + (void) key; + } +} + +static void +exercise_object_api(const ucl_object_t *obj) +{ + ucl_object_t *copy; + + walk_object(obj); + + if (ucl_object_type(obj) == UCL_OBJECT) { + (void) ucl_object_lookup(obj, "key"); + (void) ucl_object_lookup_any(obj, "key", "key1", "a", "b", NULL); + (void) ucl_object_lookup_path(obj, "a/b"); + } + + /* Deep copy, then absorb a freshly built object into the copy. + * ucl_object_merge with copy=false refcounts elt's members, so the + * extra object can be unreffed by us afterwards. */ + copy = ucl_object_copy(obj); + + if (copy != NULL) { + ucl_object_t *extra = ucl_object_typed_new(UCL_OBJECT); + + if (extra != NULL) { + ucl_object_t *val = ucl_object_fromstring("fzval"); + + if (val != NULL && ucl_object_type(copy) == UCL_OBJECT) { + (void) ucl_object_insert_key(extra, val, "fzkey", 5, true); + (void) ucl_object_merge(copy, extra, false); + } + else { + ucl_object_unref(val); + } + + ucl_object_unref(extra); + } + + ucl_object_unref(copy); + } +} + +int +LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + struct ucl_parser *parser; + struct ucl_parser_limits limits = { + .max_depth = FUZZ_MAX_DEPTH, + .max_nodes = FUZZ_MAX_NODES, + .max_alloc = FUZZ_MAX_ALLOC, + }; + const uint8_t *body; + size_t blen, nchunks, chunk_size, i; + int flags = UCL_PARSER_NO_FILEVARS; + unsigned priority, strat, ptype; + bool ok = true; + ucl_object_t *obj; + + if (size < 8) { + return 0; + } + + if (data[0] & 1) { + flags |= UCL_PARSER_KEY_LOWERCASE; + } + if (data[0] & 2) { + flags |= UCL_PARSER_NO_TIME; + } + if (data[0] & 4) { + flags |= UCL_PARSER_NO_IMPLICIT_ARRAYS; + } + if (data[0] & 8) { + flags |= UCL_PARSER_SAVE_COMMENTS; + } + if (data[0] & 16) { + flags |= UCL_PARSER_DISABLE_MACRO; + } + + strat = (unsigned) (data[1] % 4); + ptype = (unsigned) (data[2] % 5); + priority = (unsigned) (data[3] & 0x0f); + nchunks = 2 + (size_t) ((data[3] >> 4) % 3); + + parser = ucl_parser_new(flags); + + if (parser == NULL) { + return 0; + } + + ucl_parser_set_limits(parser, &limits); + ucl_parser_set_default_priority(parser, priority); + + if (!(flags & UCL_PARSER_DISABLE_MACRO)) { + (void) ucl_parser_register_macro(parser, "fz", fz_macro_handler, NULL); + (void) ucl_parser_register_context_macro(parser, "fzc", + fz_context_macro_handler, NULL); + } + + body = data + 4; + blen = size - 4; + chunk_size = blen / nchunks; + + for (i = 0; i < nchunks && ok; i++) { + size_t off = i * chunk_size; + size_t len = (i == nchunks - 1) ? blen - off : chunk_size; + + ok = ucl_parser_add_chunk_full(parser, body + off, len, priority, + (enum ucl_duplicate_strategy) strat, + (enum ucl_parse_type) ptype); + } + + if (ok && ptype != UCL_PARSE_MSGPACK && ptype != UCL_PARSE_CBOR) { + /* Incremental insertion on top of the accumulated state; binary + * parse types are skipped to keep a single format per parser. */ + (void) ucl_parser_insert_chunk(parser, body, + blen > FUZZ_INSERT_CHUNK_MAX ? FUZZ_INSERT_CHUNK_MAX : blen); + } + + obj = ucl_parser_get_object(parser); + + if (obj != NULL) { + exercise_object_api(obj); + + if (flags & UCL_PARSER_SAVE_COMMENTS) { + const ucl_object_t *comments = ucl_parser_get_comments(parser); + + if (comments != NULL) { + ucl_object_iter_t cit = NULL; + const ucl_object_t *cc; + + while ((cc = ucl_object_iterate(comments, &cit, true)) != NULL) { + (void) ucl_object_key(cc); + } + } + } + + ucl_object_unref(obj); + } + + ucl_parser_free(parser); + + return 0; +} diff --git a/tests/fuzzers/ucl_api_fuzzer.dict b/tests/fuzzers/ucl_api_fuzzer.dict new file mode 100644 index 00000000..bca8857a --- /dev/null +++ b/tests/fuzzers/ucl_api_fuzzer.dict @@ -0,0 +1,42 @@ +# UCL / JSON syntax and parser-surface tokens for ucl_api_fuzzer. +# Multi-byte structural tokens a byte mutator is unlikely to invent alone; +# macro keywords exercise the registered-macro dispatch in the harness. +"key = value" +"true" +"false" +"null" +"yes" +"no" +"// comment" +"# comment" +"/*" +"*/" +"section {" +"}" +"[" +"]" +"\"quoted string\"" +"'single quoted'" +"key = < + +import glob +import os +import sys + +# Well-formed canonical S-expressions (upstream test format: (len:atom)). +CSEXP_SEEDS = ( + b'((3:abc)(4:test))', + b'(3:abc)', + b'(1:1)', + b'((1:a)(1:b)(1:c))', + b'((2:ab)(3:cd(e)f))', +) + +# (flags, dup, parse_type, prio_nibbles) headers used for the UCL fixtures. +# flags: 0 default, 4 NO_IMPLICIT_ARRAYS, 8 SAVE_COMMENTS, +# 16 DISABLE_MACRO, 3 KEY_LOWERCASE|NO_TIME, 6 NO_TIME|NO_IMPLICIT_ARRAYS +UCL_HEADERS = ( + bytes([0, 0, 0, 0]), # defaults, 2 chunks + bytes([4, 0, 0, 0]), # explicit arrays, 2 chunks + bytes([8, 0, 0, 0]), # save comments, 2 chunks + bytes([16, 1, 0, 0]), # macros disabled, merge, 2 chunks + bytes([3, 2, 0, 0x10]), # lowercase+no-time, rewrite, 3 chunks + bytes([6, 3, 3, 0x20]), # no-time+explicit-arrays, error-dup, AUTO, 4 chunks +) + + +def main(argv): + if len(argv) != 3: + sys.stderr.write('usage: ucl_api_seed_corpus.py \n') + return 2 + + src_dir, out_dir = argv[1], argv[2] + os.makedirs(out_dir, exist_ok=True) + + count = 0 + for path in sorted(glob.glob(os.path.join(src_dir, '*.in'))): + with open(path, 'rb') as f: + doc = f.read() + if not doc or len(doc) > 8192: + continue + for header in UCL_HEADERS: + with open(os.path.join(out_dir, 'seed_%04d.ucl' % count), 'wb') as f: + f.write(header + doc) + count += 1 + + for doc in CSEXP_SEEDS: + # parse type byte 2 -> UCL_PARSE_CSEXP + with open(os.path.join(out_dir, 'seed_%04d.ucl' % count), 'wb') as f: + f.write(bytes([0, 0, 2, 0]) + doc) + count += 1 + + if count == 0: + sys.stderr.write('error: no usable fixtures in %s\n' % src_dir) + return 1 + + print('ucl_api_seed_corpus: wrote %d seeds' % count) + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv))