Agent sync protocol: concurrent flushes in `PersistentQueue` silently drop buffered events
| Wazuh version | Component | Install type | Install method | Platform |
|---|---|---|---|---|
| 5.0.0 | Agent sync protocol (shared_modules/sync_protocol), seen through FIM |
Agent | Packages | all |
Description
PersistentQueue buffers module events in two in-memory slots (m_buffers[0]/m_buffers[1]) and writes them to SQLite in batches. Two code paths flush those slots:
- the background thread,
flushLoop()(persistent_queue.cpp:87-105), every 500 ms or once 100 events are buffered; flushPendingBuffer()(persistent_queue.cpp:125-143), called at the start of everyfetchAndMarkForSync()andfetchPendingItems(), which is at the start of every sync.
Both follow the same pattern: under m_mutex they take the active slot and flip m_currentIdx. Then, without m_mutex, they call submitBatch() on that slot and clear() it. Neither path checks whether the other one is still writing the slot it is about to hand to producers. When the two run at the same time:
- The background thread takes slot 0, flips the index to 1 and starts writing slot 0.
- A sync starts.
flushPendingBuffer()sees items in slot 1, takes it and flips the index back to 0, so producers now point at the slot the background thread is still writing. It then waits onm_storageMutex. - A module calls
submit(), which doespush_backinto slot 0 whilesubmitBatch()may still be iterating it. - The background thread finishes and runs
m_buffers[0].clear(), which discards the event from step 3. That event never reaches SQLite and nothing is logged.
The mirror case also happens: the sync is writing its slot when the background thread's interval fires and flips the index onto it.
Besides the lost event, step 3 is a data race on a std::vector: push_back against an ongoing iteration, and a reallocation when the slot is at capacity.
Why modules reach it
AgentSyncProtocol::persistDifference()callssubmit()with no synchronization against a running sync.m_syncInProgressonly stops two syncs from running at once (agent_sync_protocol.cpp:271-285,:331).fetchPendingItems()is not covered bym_syncInProgresseither, so a third flusher can join.- FIM: the regular sync calls
asp_sync_module()without the scan or realtime mutex (run_check.c:1299). Persistence (persist_syscheck_msg(),run_check.c:564) and the sync both take the same rwlock for reading, so they do not exclude each other. A scheduled scan or a realtime event therefore keeps submitting while the sync thread flushes.
Only FIM was exercised. The race is in the shared queue, so any module whose events can be submitted during its own sync is exposed. SCA and Syscollector were not checked.
Impact
- Stateful FIM documents are silently missing from the indexer. The local FIM DB considers them synced (
sync=1), the local sync queue is empty, and the sync reports success. - The vector data race corrupts what is read. In one run SQLite rejected the garbage:
PersistentQueueStorage: Transaction failed in submitBatch: string or blob too big. - Not verified: whether the next integrity check (
integrity_interval, 24h by default) restores the missing documents.
Steps to reproduce
The race needs submits while a sync is running, so the setup keeps a large scheduled scan running while the sync fires every 2 s.
- Install the 5.0.0 agent package (Ubuntu 24.04 container) and enroll it against a 5.0.0 manager.
- Use a FIM-only configuration (rootcheck, syscollector and SCA disabled):
<syscheck> <disabled>no</disabled> <frequency>30</frequency> <directories>/race</directories> <max_eps>0</max_eps> <synchronization> <enabled>yes</enabled> <interval>2s</interval> <integrity_interval>1d</integrity_interval> </synchronization> </syscheck> - Create 2,000 seed files in
/raceand start the agent. - After a scheduled scan ends, create 13,500 new, uniquely named files. Repeat once after the next scan, for 29,000 files in total. This stays under the manager's per-agent FIM document limit of 30,000. New files are used, never modified, so a later change cannot hide a lost one.
- Wait until the local queue is empty (
select count(*) from persistent_queueonqueue/fim/db/fim_sync.dbreturns 0) and the sync logsnothing to send. - Compare the agent's synced files with what the indexer holds:
# agent sqlite3 /var/ossec/queue/fim/db/fim.db "select path from file_entry where sync=1;" | sort > synced.txt # indexer: every file.path in wazuh-states-fim-files with wazuh.agent.id = <id>, sorted, into indexer.txt comm -23 synced.txt indexer.txt # files the agent persisted that never arrived
Expected: the two sets are identical. Actual: some files are missing from the indexer.
Results
| Run | Synced on the agent | In the indexer | Missing |
|---|---|---|---|
| 1 | 30,000 | 29,987 | 13 |
| 2 | 29,000 | 28,993 | 7 |
| 3 | 29,000 | 29,000 | 0 |
| 4 | 29,000 | 28,999 | 1, plus the string or blob too big error |
- Run 1 used four rounds of 20,000 files, and only 30,000 were synced because of the per-agent limit.
- Run 4 used the same source built locally.
- In every run, the missing files have
sync=1infim.db, the agent logs no other error, the local queue ends empty and nothing is pending.
Examples of missing paths: /race/r1/f_10500, /race/r1/f_3013, /race/r2/f_13155, /race/r1/f_3448.
Control: the same load with <interval>120s</interval>, with the files created right after a sync, so that no sync overlaps the scan that submits them. All 22,000 files arrived. The manager and indexer path is the same in both setups, so the losses point to the agent side.
The loss is timing-dependent: it appeared in 3 of 5 overlapping runs. It is less likely when the host is busy (slower scans keep the background flush less busy).
Root cause in short
Two flushers can each own one slot at the same time. A flip made by one can then point producers at the slot the other is about to clear. The swap, the write and the clear() have to be one critical section shared by every flusher.
Source: wazuh/wazuh