diff --git a/src/fuzzers/fuzzer-setup.h b/src/fuzzers/fuzzer-setup.h index a66d2a8448..40e577c5a5 100644 --- a/src/fuzzers/fuzzer-setup.h +++ b/src/fuzzers/fuzzer-setup.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "zeek-setup.h" @@ -11,6 +12,22 @@ extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) { + auto zeekpath = getenv("ZEEKPATH"); + + if ( ! zeekpath ) + { + // Set up an expected script search path for use with OSS-Fuzz + auto constexpr oss_fuzz_scripts = "oss-fuzz-zeek-scripts"; + auto fuzzer_path = get_exe_path(*argv[0]); + auto fuzzer_dir = SafeDirname(fuzzer_path).result; + std::string fs = fmt("%s/%s", fuzzer_dir.data(), oss_fuzz_scripts); + auto p = fs.data(); + auto oss_fuzz_zeekpath = fmt(".:%s:%s/policy:%s/site", p, p, p); + + if ( setenv("ZEEKPATH", oss_fuzz_zeekpath, true) == -1 ) + abort(); + } + zeek::Options options; options.scripts_to_load.emplace_back("local.zeek"); options.script_options_to_set.emplace_back("Site::local_nets={10.0.0.0/8}"); diff --git a/src/util.cc b/src/util.cc index 1293c34804..5c0988177b 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1745,6 +1745,38 @@ static string find_file_in_path(const string& filename, const string& path, return string(); } +std::string get_exe_path(const std::string& invocation) + { + if ( invocation.empty() ) + return ""; + + if ( invocation[0] == '/' || invocation[0] == '~' ) + // Absolute path + return invocation; + + if ( invocation.find('/') != std::string::npos ) + { + // Relative path + char cwd[PATH_MAX]; + + if ( ! getcwd(cwd, sizeof(cwd)) ) + { + fprintf(stderr, "failed to get current directory: %s\n", + strerror(errno)); + exit(1); + } + + return std::string(cwd) + "/" + invocation; + } + + auto path = getenv("PATH"); + + if ( ! path ) + return ""; + + return find_file(invocation, path); + } + string find_file(const string& filename, const string& path_set, const string& opt_ext) { diff --git a/src/util.h b/src/util.h index 1353a36cdd..7878a73a46 100644 --- a/src/util.h +++ b/src/util.h @@ -346,6 +346,14 @@ std::string normalize_path(std::string_view path); */ std::string without_bropath_component(std::string_view path); +/** + * Gets the full path used to invoke some executable. + * @param invocation any possible string that may be seen in argv[0], such as + * absolute path, relative path, or name to lookup in PATH. + * @return the absolute path to the executable file + */ +std::string get_exe_path(const std::string& invocation); + /** * Locate a file within a given search path. * @param filename Name of a file to find. diff --git a/src/zeek-setup.cc b/src/zeek-setup.cc index 8cc392e4fb..6d9d825266 100644 --- a/src/zeek-setup.cc +++ b/src/zeek-setup.cc @@ -374,38 +374,6 @@ static std::vector get_script_signature_files() return rval; } -static std::string get_exe_path(const std::string& invocation) - { - if ( invocation.empty() ) - return ""; - - if ( invocation[0] == '/' || invocation[0] == '~' ) - // Absolute path - return invocation; - - if ( invocation.find('/') != std::string::npos ) - { - // Relative path - char cwd[PATH_MAX]; - - if ( ! getcwd(cwd, sizeof(cwd)) ) - { - fprintf(stderr, "failed to get current directory: %s\n", - strerror(errno)); - exit(1); - } - - return std::string(cwd) + "/" + invocation; - } - - auto path = getenv("PATH"); - - if ( ! path ) - return ""; - - return find_file(invocation, path); - } - zeek::SetupResult zeek::setup(int argc, char** argv, zeek::Options* zopts) { ZEEK_LSAN_DISABLE();