Skip to content

Commit 6ec9e5c

Browse files
Lukas Sismisvictorjulien
authored andcommitted
hs: address coverity warning in a reference string
Move the locking mechanism outside of the getter function and hold the lock until the reference string is no longer reused. ** CID 1682023: Concurrent data access violations (MISSING_LOCK) /src/util-mpm-hs-cache.c: 139 in HSGetReferenceDbInfo()
1 parent f711e57 commit 6ec9e5c

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/util-mpm-hs-cache.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,7 @@ static void SCHSCachePatternHash(const SCHSPattern *p, SCSha256 *sha256)
133133
*/
134134
static const char *HSGetReferenceDbInfo(void)
135135
{
136-
SCMutexLock(&g_hs_ref_info_mutex);
137136
if (g_hs_ref_info != NULL) {
138-
SCMutexUnlock(&g_hs_ref_info_mutex);
139137
return g_hs_ref_info;
140138
}
141139

@@ -156,7 +154,6 @@ static const char *HSGetReferenceDbInfo(void)
156154
hs_free_compile_error(compile_err);
157155
}
158156

159-
SCMutexUnlock(&g_hs_ref_info_mutex);
160157
return g_hs_ref_info;
161158
}
162159

@@ -192,23 +189,26 @@ int HSLoadCache(hs_database_t **hs_db, const char *hs_db_hash, const char *dirpa
192189

193190
// Verify the loaded database is compatible with the current Hyperscan
194191
// If both the loaded DB and the reference DB fail to load, consider the cache.
192+
SCMutexLock(&g_hs_ref_info_mutex);
195193
const char *ref_info = HSGetReferenceDbInfo();
196194
if (ref_info != NULL) {
197195
if (hs_database_info(*hs_db, &db_info) != HS_SUCCESS || db_info == NULL) {
198196
SCLogDebug("Failed to query info for loaded Hyperscan database %s: %s",
199197
hash_file_static, HSErrorToStr(error));
200198
ret = -1;
199+
SCMutexUnlock(&g_hs_ref_info_mutex);
201200
goto freeup;
202201
}
203202
if (strcmp(db_info, ref_info) != 0) {
204203
SCLogDebug("Loaded Hyperscan database %s is incompatible with the current "
205204
"Hyperscan installation and will be ignored",
206205
hash_file_static);
207206
ret = -1;
207+
SCMutexUnlock(&g_hs_ref_info_mutex);
208208
goto freeup;
209209
}
210210
}
211-
211+
SCMutexUnlock(&g_hs_ref_info_mutex);
212212
ret = 0;
213213
/* Touch file to update modification time so active caches are retained. */
214214
if (SCTouchFile(hash_file_static) != 0) {
@@ -295,10 +295,12 @@ int HSHashDb(const PatternDatabase *pd, char *hash, size_t hash_len)
295295
return -1;
296296
}
297297

298+
SCMutexLock(&g_hs_ref_info_mutex);
298299
const char *ref_info = HSGetReferenceDbInfo();
299300
if (ref_info != NULL) {
300301
SCSha256Update(hasher, (const uint8_t *)ref_info, strlen(ref_info));
301302
}
303+
SCMutexUnlock(&g_hs_ref_info_mutex);
302304

303305
SCSha256Update(hasher, (const uint8_t *)&pd->pattern_cnt, sizeof(pd->pattern_cnt));
304306
for (uint32_t i = 0; i < pd->pattern_cnt; i++) {

0 commit comments

Comments
 (0)