From: Jason Ish Date: Tue, 11 Aug 2020 16:26:43 +0000 (-0600) Subject: plugins: track all loaded plugins in a list X-Git-Tag: suricata-6.0.0-rc1~121 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8994cdaca119360ff8f18cc2432460d10142c5d;p=thirdparty%2Fsuricata.git plugins: track all loaded plugins in a list Track the pointer returned from dlopen in a list to prevent a resource leak by the pointer going out of scope. Found by Coverity, CID 1465661. Redmine issue: https://redmine.openinfosecfoundation.org/issues/3864 --- diff --git a/src/util-plugin.c b/src/util-plugin.c index 95125ce716..94f7c13d51 100644 --- a/src/util-plugin.c +++ b/src/util-plugin.c @@ -23,6 +23,20 @@ #include +typedef struct PluginListNode_ { + SCPlugin *plugin; + void *lib; + TAILQ_ENTRY(PluginListNode_) entries; +} PluginListNode; + +/** + * The list of loaded plugins. + * + * Currently only used as a place to stash the pointer returned from + * dlopen, but could have other uses, such as a plugin unload destructor. + */ +static TAILQ_HEAD(, PluginListNode_) plugins = TAILQ_HEAD_INITIALIZER(plugins); + static TAILQ_HEAD(, SCPluginFileType_) output_types = TAILQ_HEAD_INITIALIZER(output_types); @@ -38,11 +52,22 @@ static void InitPlugin(char *path) SCPlugin *plugin = dlsym(lib, "PluginSpec"); if (plugin == NULL) { SCLogError(SC_ERR_PLUGIN, "Plugin does not export a plugin specification: %s", path); + dlclose(lib); } else { BUG_ON(plugin->name == NULL); BUG_ON(plugin->author == NULL); BUG_ON(plugin->license == NULL); BUG_ON(plugin->Init == NULL); + + PluginListNode *node = SCCalloc(1, sizeof(*node)); + if (node == NULL) { + SCLogError(SC_ERR_MEM_ALLOC, "Failed to allocated memory for plugin"); + dlclose(lib); + return; + } + 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); (*plugin->Init)();