]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output/rotate: Serialize rotation flag handling
authorJeff Lucovsky <jlucovsky@oisf.net>
Mon, 14 Apr 2025 20:06:08 +0000 (16:06 -0400)
committerVictor Julien <victor@inliniac.net>
Thu, 17 Apr 2025 06:22:14 +0000 (08:22 +0200)
Issue: 3436

Serialize rotation flag handling to avoid corruption.

src/output.c

index 11ae93c3f01d21b1b70708557584bafb20fc03bd..da2d661294c1071751f51de9839c880314b03dd6 100644 (file)
@@ -126,6 +126,8 @@ typedef struct OutputFileRolloverFlag_ {
     TAILQ_ENTRY(OutputFileRolloverFlag_) entries;
 } OutputFileRolloverFlag;
 
+static SCMutex output_file_rotation_mutex = SCMUTEX_INITIALIZER;
+
 TAILQ_HEAD(, OutputFileRolloverFlag_) output_file_rotation_flags =
     TAILQ_HEAD_INITIALIZER(output_file_rotation_flags);
 
@@ -672,7 +674,9 @@ void OutputRegisterFileRotationFlag(int *flag)
         return;
     }
     flag_entry->flag = flag;
+    SCMutexLock(&output_file_rotation_mutex);
     TAILQ_INSERT_TAIL(&output_file_rotation_flags, flag_entry, entries);
+    SCMutexUnlock(&output_file_rotation_mutex);
 }
 
 /**
@@ -688,25 +692,31 @@ void OutputRegisterFileRotationFlag(int *flag)
 void OutputUnregisterFileRotationFlag(int *flag)
 {
     OutputFileRolloverFlag *entry, *next;
+    SCMutexLock(&output_file_rotation_mutex);
     for (entry = TAILQ_FIRST(&output_file_rotation_flags); entry != NULL;
          entry = next) {
         next = TAILQ_NEXT(entry, entries);
         if (entry->flag == flag) {
             TAILQ_REMOVE(&output_file_rotation_flags, entry, entries);
+            SCMutexUnlock(&output_file_rotation_mutex);
             SCFree(entry);
-            break;
+            return;
         }
     }
+    SCMutexUnlock(&output_file_rotation_mutex);
 }
 
 /**
  * \brief Notifies all registered file rotation notification flags.
  */
 void OutputNotifyFileRotation(void) {
-    OutputFileRolloverFlag *flag;
-    TAILQ_FOREACH(flag, &output_file_rotation_flags, entries) {
+    OutputFileRolloverFlag *flag = NULL;
+    OutputFileRolloverFlag *tflag;
+    SCMutexLock(&output_file_rotation_mutex);
+    TAILQ_FOREACH_SAFE (flag, &output_file_rotation_flags, entries, tflag) {
         *(flag->flag) = 1;
     }
+    SCMutexUnlock(&output_file_rotation_mutex);
 }
 
 TmEcode OutputLoggerFlush(ThreadVars *tv, Packet *p, void *thread_data)