From: Jeff Lucovsky Date: Wed, 26 Jan 2022 21:35:12 +0000 (-0500) Subject: config/ref: Raise errors for ref.config parsing X-Git-Tag: suricata-5.0.9~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=852c77448411bb83f2e339b88c684e467242beb9;p=thirdparty%2Fsuricata.git config/ref: Raise errors for ref.config parsing This commit raises an error in configuration test mode if there was an error parsing reference.config. Issue: 4659 (cherry picked from commit be2155b4edb712913166cb5e8c10b36fb4362f2a) --- diff --git a/src/detect-engine.c b/src/detect-engine.c index bcd6c373f4..d14f0d1cc0 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2020 Open Information Security Foundation +/* Copyright (C) 2007-2022 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -2101,7 +2101,10 @@ static DetectEngineCtx *DetectEngineCtxInitReal(enum DetectEngineType type, cons (void)SRepInit(de_ctx); SCClassConfLoadClassficationConfigFile(de_ctx, NULL); - SCRConfLoadReferenceConfigFile(de_ctx, NULL); + if (SCRConfLoadReferenceConfigFile(de_ctx, NULL) < 0) { + if (RunmodeGetCurrent() == RUNMODE_CONF_TEST) + goto error; + } if (ActionInitConfig() < 0) { goto error; diff --git a/src/util-reference-config.c b/src/util-reference-config.c index 4519b7b019..2ad81bc648 100644 --- a/src/util-reference-config.c +++ b/src/util-reference-config.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2019 Open Information Security Foundation +/* Copyright (C) 2007-2022 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -125,8 +125,9 @@ static FILE *SCRConfInitContextAndLocalResources(DetectEngineCtx *de_ctx, FILE * const char *filename = SCRConfGetConfFilename(de_ctx); if ((fd = fopen(filename, "r")) == NULL) { #ifdef UNITTESTS - if (RunmodeIsUnittests()) + if (RunmodeIsUnittests()) { return NULL; // silently fail + } #endif SCLogError(SC_ERR_FOPEN, "Error opening file: \"%s\": %s", filename, strerror(errno)); @@ -322,16 +323,22 @@ static int SCRConfIsLineBlankOrComment(char *line) * * \param de_ctx Pointer to the Detection Engine Context. */ -static void SCRConfParseFile(DetectEngineCtx *de_ctx, FILE *fd) +static bool SCRConfParseFile(DetectEngineCtx *de_ctx, FILE *fd) { char line[1024]; uint8_t i = 1; + int runmode = RunmodeGetCurrent(); + bool is_conf_test_mode = runmode == RUNMODE_CONF_TEST; while (fgets(line, sizeof(line), fd) != NULL) { if (SCRConfIsLineBlankOrComment(line)) continue; - SCRConfAddReference(de_ctx, line); + if (SCRConfAddReference(de_ctx, line) != 0) { + if (is_conf_test_mode) { + return false; + } + } i++; } @@ -339,7 +346,7 @@ static void SCRConfParseFile(DetectEngineCtx *de_ctx, FILE *fd) SCLogInfo("Added \"%d\" reference types from the reference.config file", de_ctx->reference_conf_ht->count); #endif /* UNITTESTS */ - return; + return true; } /** @@ -495,7 +502,7 @@ int SCRConfLoadReferenceConfigFile(DetectEngineCtx *de_ctx, FILE *fd) fd = SCRConfInitContextAndLocalResources(de_ctx, fd); if (fd == NULL) { #ifdef UNITTESTS - if (RunmodeIsUnittests() && fd == NULL) { + if (RunmodeIsUnittests()) { return -1; } #endif @@ -504,10 +511,10 @@ int SCRConfLoadReferenceConfigFile(DetectEngineCtx *de_ctx, FILE *fd) return -1; } - SCRConfParseFile(de_ctx, fd); + bool rc = SCRConfParseFile(de_ctx, fd); SCRConfDeInitLocalResources(de_ctx, fd); - return 0; + return rc ? 0 : -1; } /**