]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
make xlat fuzzer use the common framework
authorAlan T. DeKok <aland@freeradius.org>
Mon, 25 May 2026 11:57:42 +0000 (07:57 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 25 May 2026 11:57:42 +0000 (07:57 -0400)
and update it so that it actually runs

src/fuzzer/all.mk
src/fuzzer/fuzzer_xlat.c
src/fuzzer/fuzzer_xlat.mk

index 008cf1699004198f290f5b19fef52f253dc9fae8..3a1981a9297cfcd649abc0e8b22efbc55029a627 100644 (file)
@@ -25,6 +25,7 @@ FUZZER_NON_PROTOCOL_TARGETS = util json value xlat
 #  json / cf / value / xlat are standalone parsers that don't.
 #
 FUZZER_util_ARGS  := -D share/dictionary
+FUZZER_xlat_ARGS  := -D share/dictionary
 
 #
 #  Add the fuzzer only if everything was built with the fuzzing flags.
index e457dd1e0612da9625e7ce07f8b2570275a7e093..a4d9acc4b7c23dbda45aa15679fbbe5150c8d233 100644 (file)
  */
 RCSID("$Id$")
 
-#include <freeradius-devel/build.h>
-#include <freeradius-devel/util/atexit.h>
-#include <freeradius-devel/util/dict.h>
-#include <freeradius-devel/util/dl.h>
-#include <freeradius-devel/util/lsan.h>
-#include <freeradius-devel/util/sbuff.h>
-#include <freeradius-devel/util/strerror.h>
-#include <freeradius-devel/util/syserror.h>
-#include <freeradius-devel/util/talloc.h>
-#include <freeradius-devel/util/types.h>
-#include <freeradius-devel/util/value.h>
+#include <freeradius-devel/fuzzer/common.h>
 #include <freeradius-devel/server/base.h>
 #include <freeradius-devel/server/request.h>
 #include <freeradius-devel/server/tmpl.h>
@@ -55,59 +45,18 @@ RCSID("$Id$")
 int LLVMFuzzerInitialize(int *argc, char ***argv);
 int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
 
-static bool            init_done = false;
-static fr_dict_t       *dict_internal = NULL;
-
 int LLVMFuzzerInitialize(int *argc, char ***argv)
 {
-       char const      *dict_dir = NULL;
-       char const      *lib_dir  = NULL;
-       char            *dict_buf = NULL, *lib_buf = NULL;
-       char const      *p;
-
-       if (init_done) return 0;
-       if (!argc || !argv || !*argv) return -1;
+       if (dict) return 0;
 
-       fr_atexit_global_setup();
-       fr_talloc_fault_setup();
-       fr_strerror_const("fuzz");
-       fr_strerror_clear();
+       if (fuzzer_common_init(argc, argv, false) < 0) fr_exit_now(EXIT_FAILURE);
 
        if (fr_check_lib_magic(RADIUSD_MAGIC_NUMBER) < 0) {
        error:
-               talloc_free(dict_buf);
-               talloc_free(lib_buf);
                fr_perror("fuzzer_xlat");
-               return -1;
-       }
-
-       dict_dir = getenv("FR_DICTIONARY_DIR");
-       lib_dir  = getenv("FR_LIBRARY_PATH");
-
-       p = strrchr((*argv)[0], '/');
-       if (p) {
-               if (!dict_dir) {
-                       dict_buf = talloc_asprintf(NULL, "%.*s/dict",
-                                                  (int)(p - (*argv)[0]), (*argv)[0]);
-                       if (!dict_buf) goto error;
-                       dict_dir = dict_buf;
-               }
-               if (!lib_dir) {
-                       lib_buf = talloc_asprintf(NULL, "%.*s/lib",
-                                                 (int)(p - (*argv)[0]), (*argv)[0]);
-                       if (!lib_buf) goto error;
-                       lib_dir = lib_buf;
-               }
+               fr_exit_now(EXIT_FAILURE);
        }
 
-       if (lib_dir && dl_search_global_path_set(lib_dir) < 0) goto error;
-
-       if (dict_dir) (void) setenv("FR_DICTIONARY_DIR", dict_dir, 1);
-
-       if (!fr_dict_global_ctx_init(NULL, true, dict_dir ? dict_dir : "share/dictionary")) goto error;
-
-       if (fr_dict_internal_afrom_file(&dict_internal, FR_DICTIONARY_INTERNAL_DIR, __FILE__) < 0) goto error;
-
        if (request_global_init() < 0) goto error;
 
        /*
@@ -116,10 +65,6 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
         */
        if (unlang_global_init() < 0) goto error;
 
-       talloc_free(dict_buf);
-       talloc_free(lib_buf);
-
-       init_done = true;
        return 0;
 }
 
@@ -146,7 +91,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
        size_t                  fmt_len;
        fr_slen_t               slen;
 
-       if (!init_done) return 0;
+       if (!dict) return 0;
        if (size < 2) return 0;
        if (size > 4096) return 0; /* keep iterations fast */
 
@@ -178,7 +123,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
         */
        t_rules = (tmpl_rules_t) {
                .attr = (tmpl_attr_rules_t) {
-                       .dict_def             = dict_internal,
+                       .dict_def             = dict,
                        .list_def             = request_attr_request,
                        .allow_unresolved = false,
                        .allow_unknown    = false,
@@ -219,7 +164,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
         */
        {
                xlat_res_rules_t const  xr_rules = {
-                       .tr_rules = &(tmpl_res_rules_t){ .dict_def = dict_internal },
+                       .tr_rules = &(tmpl_res_rules_t){ .dict_def = dict },
                        .allow_unresolved = false,
                };
                (void) xlat_resolve(head, &xr_rules);
index c44bc0b874c58ef328a9508858121cb7920830e6..9aeb8cf1338ce7ad650c3082697f00479b0392bd 100644 (file)
@@ -1,5 +1,5 @@
 TARGET                 := fuzzer_xlat$(E)
-SOURCES                        := fuzzer_xlat.c
+SOURCES                        := fuzzer_xlat.c common.c
 
 TGT_PREREQS            := $(LIBFREERADIUS_SERVER)