From d703033ae6c839c9ad0ffab66ce1733f77671e5b Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Wed, 6 Jul 2022 18:45:40 +0200 Subject: [PATCH] zeek-setup: Validate plugin debug streams during startup Providing an unknown plugin debug stream with -B was previously silently accepted. This caused user confusing as the behavior is "no output, but seems to work". Check the enabled debug streams once all plugins have been loaded and exit early on for invalid streams. $ ZEEK_PLUGIN_PATH=./build zeek -B plugin-zeek-myplugin -e 'print zeek_version();' error in , line 3: No plugin debug stream 'plugin-zeek-myplugin' found Closes #913. --- src/DebugLogger.cc | 28 +++++++++++++++++-- src/DebugLogger.h | 10 +++++++ src/zeek-setup.cc | 14 ++++++++++ .../plugins.debug-streams/zeek.stderr | 2 ++ testing/btest/plugins/debug-streams.zeek | 7 +++++ 5 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 testing/btest/Baseline/plugins.debug-streams/zeek.stderr create mode 100644 testing/btest/plugins/debug-streams.zeek diff --git a/src/DebugLogger.cc b/src/DebugLogger.cc index f95db79139..49b0205efe 100644 --- a/src/DebugLogger.cc +++ b/src/DebugLogger.cc @@ -115,7 +115,7 @@ void DebugLogger::EnableStreams(const char* s) exit(0); } - if ( strncmp(tok, "plugin-", strlen("plugin-")) == 0 ) + if ( util::starts_with(tok, "plugin-") ) { // Cannot verify this at this time, plugins may not // have been loaded. @@ -144,6 +144,29 @@ void DebugLogger::EnableStreams(const char* s) delete[] tmp; } +bool DebugLogger::CheckStreams(const std::set& plugin_names) + { + bool ok = true; + + std::set available_plugin_streams; + for ( const auto& p : plugin_names ) + available_plugin_streams.insert(PluginStreamName(p)); + + for ( auto const& stream : enabled_streams ) + { + if ( ! util::starts_with(stream, "plugin-") ) + continue; + + if ( available_plugin_streams.count(stream) == 0 ) + { + reporter->Error("No plugin debug stream '%s' found", stream.c_str()); + ok = false; + } + } + + return ok; + } + void DebugLogger::Log(DebugStream stream, const char* fmt, ...) { Stream* g = &streams[int(stream)]; @@ -168,8 +191,7 @@ void DebugLogger::Log(DebugStream stream, const char* fmt, ...) void DebugLogger::Log(const plugin::Plugin& plugin, const char* fmt, ...) { - std::string tok = std::string("plugin-") + plugin.Name(); - tok = util::strreplace(tok, "::", "-"); + std::string tok = PluginStreamName(plugin.Name()); if ( enabled_streams.find(tok) == enabled_streams.end() ) return; diff --git a/src/DebugLogger.h b/src/DebugLogger.h index ce2aae57dc..591bfb85c4 100644 --- a/src/DebugLogger.h +++ b/src/DebugLogger.h @@ -11,6 +11,8 @@ #include #include +#include "zeek/util.h" + #define DBG_LOG(stream, args...) \ if ( ::zeek::detail::debug_logger.IsEnabled(stream) ) \ ::zeek::detail::debug_logger.Log(stream, args) @@ -84,6 +86,9 @@ public: // Takes comma-seperated list of stream prefixes. void EnableStreams(const char* streams); + // Check the enabled streams for invalid ones. + bool CheckStreams(const std::set& plugin_names); + bool IsEnabled(DebugStream stream) const { return streams[int(stream)].enabled; } void SetVerbose(bool arg_verbose) { verbose = arg_verbose; } @@ -105,6 +110,11 @@ private: std::set enabled_streams; static Stream streams[NUM_DBGS]; + + const std::string PluginStreamName(const std::string& plugin_name) + { + return "plugin-" + util::strreplace(plugin_name, "::", "-"); + } }; extern DebugLogger debug_logger; diff --git a/src/zeek-setup.cc b/src/zeek-setup.cc index ca7e5333e3..53f9b781a3 100644 --- a/src/zeek-setup.cc +++ b/src/zeek-setup.cc @@ -847,6 +847,20 @@ SetupResult setup(int argc, char** argv, Options* zopts) exit(success ? 0 : 1); } +#ifdef DEBUG + // Check debug streams. Specifically that all plugin- + // streams are valid now that the active plugins are known. + std::set active_plugins; + for ( const auto p : plugin_mgr->ActivePlugins() ) + active_plugins.insert(p->Name()); + + if ( ! debug_logger.CheckStreams(active_plugins) ) + { + early_shutdown(); + exit(1); + } +#endif + packet_mgr->InitPostScript(options.unprocessed_output_file.value_or("")); analyzer_mgr->InitPostScript(); file_mgr->InitPostScript(); diff --git a/testing/btest/Baseline/plugins.debug-streams/zeek.stderr b/testing/btest/Baseline/plugins.debug-streams/zeek.stderr new file mode 100644 index 0000000000..7f6cbf9e04 --- /dev/null +++ b/testing/btest/Baseline/plugins.debug-streams/zeek.stderr @@ -0,0 +1,2 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +error in , line 3: No plugin debug stream 'plugin-zeek-http' found diff --git a/testing/btest/plugins/debug-streams.zeek b/testing/btest/plugins/debug-streams.zeek new file mode 100644 index 0000000000..87d60773a1 --- /dev/null +++ b/testing/btest/plugins/debug-streams.zeek @@ -0,0 +1,7 @@ +# This requires Zeek with debug streams support. +# @TEST-REQUIRES: test "$(zeek-config --build_type)" = "debug" + +# @TEST-EXEC: zeek -B plugin-Zeek-HTTP -e 'event zeek_init() { print "zeek_init"; }' 2>zeek.stderr +# @TEST-EXEC-FAIL: zeek -B plugin-zeek-http -e 'event zeek_init() { print "zeek_init"; }' 2>zeek.stderr + +# @TEST-EXEC: btest-diff zeek.stderr