From c164cfcf6b74ebc81f6577920a40ccbe30dbe669 Mon Sep 17 00:00:00 2001 From: Philippe Antoine Date: Thu, 6 Mar 2025 14:15:27 +0100 Subject: [PATCH] plugins: check version for all plugins --- examples/plugins/c-custom-loggers/custom-logger.c | 3 +++ examples/plugins/c-json-filetype/filetype.c | 3 +++ examples/plugins/ci-capture/plugin.c | 3 +++ plugins/napatech/plugin.c | 3 +++ plugins/pfring/plugin.c | 3 +++ rust/sys/src/sys.rs | 7 +++++-- src/suricata-plugin.h | 15 ++++++++++----- src/util-plugin.c | 14 +++++++++----- 8 files changed, 39 insertions(+), 12 deletions(-) diff --git a/examples/plugins/c-custom-loggers/custom-logger.c b/examples/plugins/c-custom-loggers/custom-logger.c index d533b8830d..c74066faf8 100644 --- a/examples/plugins/c-custom-loggers/custom-logger.c +++ b/examples/plugins/c-custom-loggers/custom-logger.c @@ -118,7 +118,10 @@ static void Init(void) } const SCPlugin PluginRegistration = { + .version = SC_API_VERSION, + .suricata_version = SC_PACKAGE_VERSION, .name = "CustomLogger", + .plugin_version = "1.0.0", .author = "Firstname Lastname", .license = "GPLv2", .Init = Init, diff --git a/examples/plugins/c-json-filetype/filetype.c b/examples/plugins/c-json-filetype/filetype.c index a66b9779df..ad60b2e59b 100644 --- a/examples/plugins/c-json-filetype/filetype.c +++ b/examples/plugins/c-json-filetype/filetype.c @@ -206,7 +206,10 @@ void PluginInit(void) } const SCPlugin PluginRegistration = { + .version = SC_API_VERSION, + .suricata_version = SC_PACKAGE_VERSION, .name = FILETYPE_NAME, + .plugin_version = "0.1.0", .author = "FirstName LastName ", .license = "GPL-2.0-only", .Init = PluginInit, diff --git a/examples/plugins/ci-capture/plugin.c b/examples/plugins/ci-capture/plugin.c index 26a2a749ef..5bcf3c1911 100644 --- a/examples/plugins/ci-capture/plugin.c +++ b/examples/plugins/ci-capture/plugin.c @@ -44,7 +44,10 @@ static void SCPluginInit(void) } const SCPlugin PluginRegistration = { + .version = SC_API_VERSION, + .suricata_version = SC_PACKAGE_VERSION, .name = "ci-capture", + .plugin_version = "0.1.0", .author = "OISF Developer", .license = "GPL-2.0-only", .Init = SCPluginInit, diff --git a/plugins/napatech/plugin.c b/plugins/napatech/plugin.c index 71631bf7f9..d3104d5cc7 100644 --- a/plugins/napatech/plugin.c +++ b/plugins/napatech/plugin.c @@ -44,7 +44,10 @@ void SCPluginInit(void) } const SCPlugin PluginRegistration = { + .version = SC_API_VERSION, + .suricata_version = SC_PACKAGE_VERSION, .name = "napatech", + .plugin_version = "1.0.0", .author = "Open Information Security Foundation", .license = "GPLv2", .Init = SCPluginInit, diff --git a/plugins/pfring/plugin.c b/plugins/pfring/plugin.c index 0f41ee45fd..145f8f440c 100644 --- a/plugins/pfring/plugin.c +++ b/plugins/pfring/plugin.c @@ -44,7 +44,10 @@ void SCPluginInit(void) } const SCPlugin PluginRegistration = { + .version = SC_API_VERSION, + .suricata_version = SC_PACKAGE_VERSION, .name = "pfring", + .plugin_version = "1.0.0", .author = "Open Information Security Foundation", .license = "GPLv2", .Init = SCPluginInit, diff --git a/rust/sys/src/sys.rs b/rust/sys/src/sys.rs index 057d566f18..cadf3de725 100644 --- a/rust/sys/src/sys.rs +++ b/rust/sys/src/sys.rs @@ -1,5 +1,6 @@ // This file is automatically generated. Do not edit. +pub const SC_PACKAGE_VERSION: &[u8; 10] = b"8.0.0-dev\0"; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum AppProtoEnum { @@ -54,11 +55,15 @@ extern "C" { alproto: AppProto, proto_name: *const ::std::os::raw::c_char, ); } +pub const SC_API_VERSION: u64 = 2048; #[doc = " Structure to define a Suricata plugin."] #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct SCPlugin_ { + pub version: u64, + pub suricata_version: *const ::std::os::raw::c_char, pub name: *const ::std::os::raw::c_char, + pub plugin_version: *const ::std::os::raw::c_char, pub license: *const ::std::os::raw::c_char, pub author: *const ::std::os::raw::c_char, pub Init: ::std::option::Option, @@ -105,11 +110,9 @@ pub type SCCapturePlugin = SCCapturePlugin_; extern "C" { pub fn SCPluginRegisterCapture(arg1: *mut SCCapturePlugin) -> ::std::os::raw::c_int; } -pub const SC_PLUGIN_API_VERSION: u64 = 8; #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct SCAppLayerPlugin_ { - pub version: u64, pub name: *const ::std::os::raw::c_char, pub Register: ::std::option::Option, pub KeywordsRegister: ::std::option::Option, diff --git a/src/suricata-plugin.h b/src/suricata-plugin.h index fbc4979611..7c51fef633 100644 --- a/src/suricata-plugin.h +++ b/src/suricata-plugin.h @@ -22,6 +22,7 @@ #include #include "queue.h" +#include "autoconf.h" /** * The size of the data chunk inside each packet structure a plugin @@ -29,11 +30,20 @@ */ #define PLUGIN_VAR_SIZE 64 +// Do not reuse autoconf PACKAGE_VERSION which is a string +// Defined as major version.minor version (no patch version) +static const uint64_t SC_API_VERSION = 0x0800; +#define SC_PACKAGE_VERSION PACKAGE_VERSION + /** * Structure to define a Suricata plugin. */ typedef struct SCPlugin_ { + // versioning to check suricata/plugin API compatibility + uint64_t version; + const char *suricata_version; const char *name; + const char *plugin_version; const char *license; const char *author; void (*Init)(void); @@ -52,12 +62,7 @@ typedef struct SCCapturePlugin_ { int SCPluginRegisterCapture(SCCapturePlugin *); -// Every change in the API used by plugins should change this number -static const uint64_t SC_PLUGIN_API_VERSION = 8; - typedef struct SCAppLayerPlugin_ { - // versioning to check suricata/plugin API compatibility - uint64_t version; const char *name; void (*Register)(void); void (*KeywordsRegister)(void); diff --git a/src/util-plugin.c b/src/util-plugin.c index eb2ce7647e..ed534f2470 100644 --- a/src/util-plugin.c +++ b/src/util-plugin.c @@ -50,6 +50,12 @@ static TAILQ_HEAD(, SCCapturePlugin_) capture_plugins = TAILQ_HEAD_INITIALIZER(c bool RegisterPlugin(SCPlugin *plugin, void *lib) { + if (plugin->version != SC_API_VERSION) { + SCLogError("Suricata and plugin versions differ: plugin has %" PRIx64 + " (%s) vs Suricata %" PRIx64 " (plugin was built with %s)", + plugin->version, plugin->plugin_version, SC_API_VERSION, plugin->suricata_version); + return false; + } BUG_ON(plugin->name == NULL); BUG_ON(plugin->author == NULL); BUG_ON(plugin->license == NULL); @@ -63,8 +69,9 @@ bool RegisterPlugin(SCPlugin *plugin, void *lib) node->plugin = plugin; node->lib = lib; TAILQ_INSERT_TAIL(&plugins, node, entries); - SCLogNotice("Initializing plugin %s; author=%s; license=%s", plugin->name, plugin->author, - plugin->license); + SCLogNotice("Initializing plugin %s; version= %s; author=%s; license=%s; built from %s", + plugin->name, plugin->plugin_version, plugin->author, plugin->license, + plugin->suricata_version); (*plugin->Init)(); return true; } @@ -156,9 +163,6 @@ SCCapturePlugin *SCPluginFindCaptureByName(const char *name) int SCPluginRegisterAppLayer(SCAppLayerPlugin *plugin) { - if (plugin->version != SC_PLUGIN_API_VERSION) { - return 1; - } AppProto alproto = g_alproto_max; AppProtoRegisterProtoString(alproto, plugin->name); if (plugin->Register) { -- 2.47.2