Skip to content

Commit

Permalink
reference: fix multi-tenant loading issues
Browse files Browse the repository at this point in the history
Bug: OISF#4797.
  • Loading branch information
victorjulien committed Jul 16, 2023
1 parent 2859eea commit e2f4c75
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/detect-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -2523,6 +2523,7 @@ static DetectEngineCtx *DetectEngineCtxInitReal(enum DetectEngineType type, cons
if (ActionInitConfig() < 0) {
goto error;
}
SCReferenceConfInit(de_ctx);
if (SCRConfLoadReferenceConfigFile(de_ctx, NULL) < 0) {
if (RunmodeGetCurrent() == RUNMODE_CONF_TEST)
goto error;
Expand Down Expand Up @@ -2660,6 +2661,7 @@ void DetectEngineCtxFree(DetectEngineCtx *de_ctx)
/* freed our var name hash */
VarNameStoreFree(de_ctx->version);
SCClassConfDeinit(de_ctx);
SCReferenceConfDeinit(de_ctx);

SCFree(de_ctx);
//DetectAddressGroupPrintMemory();
Expand Down
11 changes: 8 additions & 3 deletions src/detect.h
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,6 @@ typedef struct DetectEngineCtx_ {
/* used by the signature ordering module */
struct SCSigOrderFunc_ *sc_sig_order_funcs;

/* hash table used for holding the reference config info */
HashTable *reference_conf_ht;

/* main sigs */
DetectEngineLookupFlow flow_gh[FLOW_STATES];

Expand Down Expand Up @@ -1016,6 +1013,14 @@ typedef struct DetectEngineCtx_ {
HashTable *class_conf_ht;
pcre2_code *class_conf_regex;
pcre2_match_data *class_conf_regex_match;

/* reference config parsing */

/* hash table used for holding the reference config info */
HashTable *reference_conf_ht;
pcre2_code *reference_conf_regex;
pcre2_match_data *reference_conf_regex_match;

} DetectEngineCtx;

/* Engine groups profiles (low, medium, high, custom) */
Expand Down
1 change: 0 additions & 1 deletion src/runmode-unittests.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ void RunUnittests(int list_unittests, const char *regex_arg)
TmqhSetup();

TagInitCtx();
SCReferenceConfInit();

UtInitialize();

Expand Down
4 changes: 0 additions & 4 deletions src/suricata.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,6 @@ static void GlobalsDestroy(SCInstance *suri)
FeatureTrackingRelease();
SCProtoNameRelease();
TimeDeinit();
if (!suri->disabled_detect) {
SCReferenceConfDeinit();
}
TmqhCleanup();
TmModuleRunDeInit();
ParseSizeDeinit();
Expand Down Expand Up @@ -2549,7 +2546,6 @@ void PostConfLoadedDetectSetup(SCInstance *suri)
{
DetectEngineCtx *de_ctx = NULL;
if (!suri->disabled_detect) {
SCReferenceConfInit();
SetupDelayedDetect(suri);
int mt_enabled = 0;
(void)ConfGetBool("multi-detect.enabled", &mt_enabled);
Expand Down
1 change: 0 additions & 1 deletion src/tests/fuzz/fuzz_siginit.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
SpmTableSetup();
EngineModeSetIDS();
SigTableSetup();
SCReferenceConfInit();
}
if (cnt++ == 1024) {
DetectEngineCtxFree(de_ctx);
Expand Down
37 changes: 19 additions & 18 deletions src/util-reference-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
/* Default path for the reference.conf file */
#define SC_RCONF_DEFAULT_FILE_PATH CONFIG_DIR "/reference.config"

static pcre2_code *regex = NULL;
static pcre2_match_data *regex_match = NULL;

/* the hash functions */
uint32_t SCRConfReferenceHashFunc(HashTable *ht, void *data, uint16_t datalen);
char SCRConfReferenceHashCompareFunc(void *data1, uint16_t datalen1,
Expand All @@ -53,35 +50,36 @@ void SCRConfReferenceHashFree(void *ch);
/* used to get the reference.config file path */
static const char *SCRConfGetConfFilename(const DetectEngineCtx *de_ctx);

void SCReferenceConfInit(void)
void SCReferenceConfInit(DetectEngineCtx *de_ctx)
{
int en;
PCRE2_SIZE eo;
int opts = 0;

regex = pcre2_compile((PCRE2_SPTR8)SC_RCONF_REGEX, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
if (regex == NULL) {
de_ctx->reference_conf_regex =
pcre2_compile((PCRE2_SPTR8)SC_RCONF_REGEX, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
if (de_ctx->reference_conf_regex == NULL) {
PCRE2_UCHAR errbuffer[256];
pcre2_get_error_message(en, errbuffer, sizeof(errbuffer));
SCLogWarning("pcre2 compile of \"%s\" failed at "
"offset %d: %s",
SC_RCONF_REGEX, (int)eo, errbuffer);
return;
}
regex_match = pcre2_match_data_create_from_pattern(regex, NULL);

de_ctx->reference_conf_regex_match =
pcre2_match_data_create_from_pattern(de_ctx->reference_conf_regex, NULL);
return;
}

void SCReferenceConfDeinit(void)
void SCReferenceConfDeinit(DetectEngineCtx *de_ctx)
{
if (regex != NULL) {
pcre2_code_free(regex);
regex = NULL;
if (de_ctx->reference_conf_regex != NULL) {
pcre2_code_free(de_ctx->reference_conf_regex);
de_ctx->reference_conf_regex = NULL;
}
if (regex_match != NULL) {
pcre2_match_data_free(regex_match);
regex_match = NULL;
if (de_ctx->reference_conf_regex_match != NULL) {
pcre2_match_data_free(de_ctx->reference_conf_regex_match);
de_ctx->reference_conf_regex_match = NULL;
}
}

Expand Down Expand Up @@ -235,7 +233,8 @@ int SCRConfAddReference(DetectEngineCtx *de_ctx, const char *line)

int ret = 0;

ret = pcre2_match(regex, (PCRE2_SPTR8)line, strlen(line), 0, 0, regex_match, NULL);
ret = pcre2_match(de_ctx->reference_conf_regex, (PCRE2_SPTR8)line, strlen(line), 0, 0,
de_ctx->reference_conf_regex_match, NULL);
if (ret < 0) {
SCLogError("Invalid Reference Config in "
"reference.config file");
Expand All @@ -244,15 +243,17 @@ int SCRConfAddReference(DetectEngineCtx *de_ctx, const char *line)

/* retrieve the reference system */
size_t copylen = sizeof(system);
ret = pcre2_substring_copy_bynumber(regex_match, 1, (PCRE2_UCHAR8 *)system, &copylen);
ret = pcre2_substring_copy_bynumber(
de_ctx->reference_conf_regex_match, 1, (PCRE2_UCHAR8 *)system, &copylen);
if (ret < 0) {
SCLogError("pcre2_substring_copy_bynumber() failed");
goto error;
}

/* retrieve the reference url */
copylen = sizeof(url);
ret = pcre2_substring_copy_bynumber(regex_match, 2, (PCRE2_UCHAR8 *)url, &copylen);
ret = pcre2_substring_copy_bynumber(
de_ctx->reference_conf_regex_match, 2, (PCRE2_UCHAR8 *)url, &copylen);
if (ret < 0) {
SCLogError("pcre2_substring_copy_bynumber() failed");
goto error;
Expand Down
4 changes: 2 additions & 2 deletions src/util-reference-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ FILE *SCRConfGenerateValidDummyReferenceConfigFD01(void);
FILE *SCRConfGenerateInvalidDummyReferenceConfigFD02(void);
FILE *SCRConfGenerateInvalidDummyReferenceConfigFD03(void);

void SCReferenceConfInit(void);
void SCReferenceConfDeinit(void);
void SCReferenceConfInit(DetectEngineCtx *de_ctx);
void SCReferenceConfDeinit(DetectEngineCtx *de_ctx);

#endif /* __UTIL_REFERENCE_CONFIG_H__ */

0 comments on commit e2f4c75

Please sign in to comment.