• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.03
/src/util-plugin.c
1
/* Copyright (C) 2020-2021 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
#include "suricata-common.h"
19
#include "suricata-plugin.h"
20
#include "suricata.h"
21
#include "runmodes.h"
22
#include "util-plugin.h"
23
#include "util-debug.h"
24
#include "conf.h"
25

26
#ifdef HAVE_PLUGINS
27

28
#include "app-layer-protos.h"
29
#include "app-layer-parser.h"
30
#include "detect-engine-register.h"
31
#include "output-eve.h"
32

33
#include <dlfcn.h>
34

35
typedef struct PluginListNode_ {
36
    SCPlugin *plugin;
37
    void *lib;
38
    TAILQ_ENTRY(PluginListNode_) entries;
39
} PluginListNode;
40

41
/**
42
 * The list of loaded plugins.
43
 *
44
 * Currently only used as a place to stash the pointer returned from
45
 * dlopen, but could have other uses, such as a plugin unload destructor.
46
 */
47
static TAILQ_HEAD(, PluginListNode_) plugins = TAILQ_HEAD_INITIALIZER(plugins);
48

49
static TAILQ_HEAD(, SCCapturePlugin_) capture_plugins = TAILQ_HEAD_INITIALIZER(capture_plugins);
50

51
bool RegisterPlugin(SCPlugin *plugin, void *lib)
52
{
×
53
    if (plugin->version != SC_API_VERSION) {
×
54
        SCLogError("Suricata and plugin versions differ: plugin has %" PRIx64
×
55
                   " (%s) vs Suricata %" PRIx64 " (plugin was built with %s)",
×
56
                plugin->version, plugin->plugin_version, SC_API_VERSION, plugin->suricata_version);
×
57
        return false;
×
58
    }
×
59
    BUG_ON(plugin->name == NULL);
×
60
    BUG_ON(plugin->author == NULL);
×
61
    BUG_ON(plugin->license == NULL);
×
62
    BUG_ON(plugin->Init == NULL);
×
63

64
    PluginListNode *node = SCCalloc(1, sizeof(*node));
×
65
    if (node == NULL) {
×
66
        SCLogError("Failed to allocate memory for plugin");
×
67
        return false;
×
68
    }
×
69
    node->plugin = plugin;
×
70
    node->lib = lib;
×
71
    TAILQ_INSERT_TAIL(&plugins, node, entries);
×
72
    SCLogNotice("Initializing plugin %s; version= %s; author=%s; license=%s; built from %s",
×
73
            plugin->name, plugin->plugin_version, plugin->author, plugin->license,
×
74
            plugin->suricata_version);
×
75
    (*plugin->Init)();
×
76
    return true;
×
77
}
×
78

79
static void InitPlugin(char *path)
80
{
×
81
    void *lib = dlopen(path, RTLD_NOW);
×
82
    if (lib == NULL) {
×
83
        SCLogNotice("Failed to open %s as a plugin: %s", path, dlerror());
×
84
    } else {
×
85
        SCLogNotice("Loading plugin %s", path);
×
86

87
        SCPluginRegisterFunc plugin_register = dlsym(lib, "SCPluginRegister");
×
88
        if (plugin_register == NULL) {
×
89
            SCLogError("Plugin does not export SCPluginRegister function: %s", path);
×
90
            dlclose(lib);
×
91
            return;
×
92
        }
×
93

94
        if (!RegisterPlugin(plugin_register(), lib)) {
×
95
            SCLogError("Plugin registration failed: %s", path);
×
96
            dlclose(lib);
×
97
            return;
×
98
        }
×
99
    }
×
100
}
×
101

102
void SCPluginsLoad(const char *capture_plugin_name, const char *capture_plugin_args)
103
{
2✔
104
    SCConfNode *conf = SCConfGetNode("plugins");
2✔
105
    if (conf == NULL) {
2✔
106
        return;
2✔
107
    }
2✔
UNCOV
108
    SCConfNode *plugin = NULL;
×
UNCOV
109
    TAILQ_FOREACH(plugin, &conf->head, next) {
×
110
        struct stat statbuf;
×
111
        if (stat(plugin->val, &statbuf) == -1) {
×
112
            SCLogError("Bad plugin path: %s: %s", plugin->val, strerror(errno));
×
113
            continue;
×
114
        }
×
115
        if (S_ISDIR(statbuf.st_mode)) {
×
116
            // coverity[toctou : FALSE]
117
            DIR *dir = opendir(plugin->val);
×
118
            if (dir == NULL) {
×
119
                SCLogError("Failed to open plugin directory %s: %s", plugin->val, strerror(errno));
×
120
                continue;
×
121
            }
×
122
            struct dirent *entry = NULL;
×
123
            char path[PATH_MAX];
×
124
            while ((entry = readdir(dir)) != NULL) {
×
125
                if (strstr(entry->d_name, ".so") != NULL) {
×
126
                    snprintf(path, sizeof(path), "%s/%s", plugin->val, entry->d_name);
×
127
                    InitPlugin(path);
×
128
                }
×
129
            }
×
130
            closedir(dir);
×
131
        } else {
×
132
            InitPlugin(plugin->val);
×
133
        }
×
134
    }
×
135

UNCOV
136
    if (SCRunmodeGet() == RUNMODE_PLUGIN) {
×
137
        SCCapturePlugin *capture = SCPluginFindCaptureByName(capture_plugin_name);
×
138
        if (capture == NULL) {
×
139
            FatalError("No capture plugin found with name %s", capture_plugin_name);
×
140
        }
×
141
        capture->Init(capture_plugin_args, RUNMODE_PLUGIN, TMM_RECEIVEPLUGIN,
×
142
                TMM_DECODEPLUGIN);
×
143
    }
×
UNCOV
144
}
×
145

146
int SCPluginRegisterCapture(SCCapturePlugin *plugin)
147
{
×
148
    TAILQ_INSERT_TAIL(&capture_plugins, plugin, entries);
×
149
    SCLogNotice("Capture plugin registered: %s", plugin->name);
×
150
    return 0;
×
151
}
×
152

153
SCCapturePlugin *SCPluginFindCaptureByName(const char *name)
154
{
×
155
    SCCapturePlugin *plugin = NULL;
×
156
    TAILQ_FOREACH(plugin, &capture_plugins, entries) {
×
157
        if (strcmp(name, plugin->name) == 0) {
×
158
            return plugin;
×
159
        }
×
160
    }
×
161
    return plugin;
×
162
}
×
163

164
int SCPluginRegisterAppLayer(SCAppLayerPlugin *plugin)
165
{
×
166
    AppProto alproto = AppProtoNewProtoFromString(plugin->name);
×
167
    if (plugin->Register) {
×
168
        if (AppLayerParserPreRegister(plugin->Register) != 0) {
×
169
            return 1;
×
170
        }
×
171
    }
×
172
    if (plugin->KeywordsRegister) {
×
173
        if (SCSigTablePreRegister(plugin->KeywordsRegister) != 0) {
×
174
            return 1;
×
175
        }
×
176
    }
×
177
    if (plugin->Logger) {
×
178
        EveJsonTxLoggerRegistrationData reg_data = {
×
179
            .confname = plugin->confname,
×
180
            .logname = plugin->logname,
×
181
            .alproto = alproto,
×
182
            .dir = plugin->dir,
×
183
            .LogTx = plugin->Logger,
×
184
        };
×
185
        if (SCOutputEvePreRegisterLogger(reg_data) != 0) {
×
186
            return 1;
×
187
        }
×
188
    }
×
189
    return 0;
×
190
}
×
191
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc