Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/lib/custom/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ int main(int argc, char **argv)
* ThreadVars will be ready. */
SuricataInit();

SCDetectEngineRegisterRateFilterCallback(RateFilterCallback, NULL);
if (DetectEngineEnabled()) {
SCDetectEngineRegisterRateFilterCallback(RateFilterCallback, NULL);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would look a warning here, and in the other plugin as well on failure, just for completeness of the examples.

}

/* Spawn our worker threads. */
pthread_t worker;
Expand Down
4 changes: 3 additions & 1 deletion examples/lib/live/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ int main(int argc, char **argv)
* ThreadVars will be ready. */
SuricataInit();

SCDetectEngineRegisterRateFilterCallback(RateFilterCallback, NULL);
if (DetectEngineEnabled()) {
SCDetectEngineRegisterRateFilterCallback(RateFilterCallback, NULL);
}

/* Spawn our worker threads, one for each interface. */
pthread_t workers[MAX_INTERFACES];
Expand Down
8 changes: 7 additions & 1 deletion src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -5122,12 +5122,18 @@ void DetectLowerSetupCallback(
}
}

void SCDetectEngineRegisterRateFilterCallback(SCDetectRateFilterFunc fn, void *arg)
bool SCDetectEngineRegisterRateFilterCallback(SCDetectRateFilterFunc fn, void *arg)
{
DetectEngineCtx *de_ctx = DetectEngineGetCurrent();
if (de_ctx == NULL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really curious how we would get here with a NULL de_ctx. The only call site:

            if (de_ctx->RateFilterCallback && original_action != pa->action) {
                pa->action = de_ctx->RateFilterCallback(p, sid, gid, rev, original_action,
                        pa->action, de_ctx->rate_filter_callback_arg);

So there a detect engine when this is called. It's also a full engine as this is only called on a signature match AFAICS.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also called in plugins see

examples/lib/custom/main.c:    SCDetectEngineRegisterRateFilterCallback(RateFilterCallback, NULL);
examples/lib/live/main.c:    SCDetectEngineRegisterRateFilterCallback(RateFilterCallback, NULL);

cc @jasonish

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually a scenario a plugin could end up in?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if it can, where should a plugin attempt to reregister these callbacks again?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. SCDetectEngineRegisterRateFilterCallback() is a documented public
extension API. A plugin author following the bundled examples
(examples/lib/custom/main.c, examples/lib/live/main.c) will naturally
call it during plugin/library initialization — before DetectEngineCtxInit()
has run and registered an engine as current. In that window,
DetectEngineGetCurrent() returns NULL and the original code dereferences it
unconditionally. In multi-tenant or delayed-detect configurations the window
is even wider.

The runtime call-site that @victorjulien points to (inside the signature-match
path) is indeed safe, because there a live engine is guaranteed. But this
function is also a registration entry-point called by external code that has
no visibility into the engine lifecycle — that is the path Svace flags.

Where should a plugin reregister?

That is a fair follow-up. The current fix (guard + log + early return) prevents
the crash, but since the function is void the caller has no way to detect
that registration was silently dropped.

Two options, in order of invasiveness:

  1. Change the return type to bool/int so the caller knows the
    registration failed and can retry after the engine is ready. This is a
    small API change but requires updating the devguide and the two bundled
    examples.

  2. Keep void, document the constraint — add a note to the devguide
    stating that this API must be called after the detect engine is initialized.
    The guard then acts purely as a defensive belt-and-suspenders measure.

SCLogError("no detection engine available for rate filter callback registration");
return false;
}

de_ctx->RateFilterCallback = fn;
de_ctx->rate_filter_callback_arg = arg;
DetectEngineDeReference(&de_ctx);
return true;
}

int DetectEngineThreadCtxGetJsonContext(DetectEngineThreadCtx *det_ctx)
Expand Down
2 changes: 1 addition & 1 deletion src/detect.h
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ typedef struct DetectEngineCtx_ {
* This callback is added to the current detection engine and will be
* copied to all future detection engines over rule reloads.
*/
void SCDetectEngineRegisterRateFilterCallback(SCDetectRateFilterFunc cb, void *arg);
bool SCDetectEngineRegisterRateFilterCallback(SCDetectRateFilterFunc cb, void *arg);

/* Engine groups profiles (low, medium, high, custom) */
enum {
Expand Down