T09 · Insecure Skill Coding Practices
- Location
scripts/dream-check.sh:49- Finding
Ineffective Locking Permits Concurrent Persistent-Memory Modification
- Content
View full analysis
Vulnerability Details
File Location:
scripts/dream-check.sh:49-60
Vulnerability Type: Race condition caused by incomplete lock implementation
Risk Level: MediumVulnerable Code
bash # Check 4: Lock file (prevent concurrent dreams) LOCK_FILE="$WORKSPACE/memory/.dream_lock" if [ -f "$LOCK_FILE" ]; then LOCK_AGE=$(( ($(date +%s) - $(stat -c %Y "$LOCK_FILE")) / 60 )) if [ "$LOCK_AGE" -lt 30 ]; then echo "SKIP: dream already running (lock age: ${LOCK_AGE}m)" exit 1 else echo "WARN: stale lock file (${LOCK_AGE}m old) — removing" rm -f "$LOCK_FILE" fi fi # All checks passed echo "READY: dream should run"The subsequent workflow is instructed to modify persistent memory in
SKILL.md:91-109:markdown 1. **Update MEMORY.md**: - Apply all changes from Phase 3 - Keep total line count under 250 lines (warn if approaching) - Update the `*最后更新*` date at the bottom - Preserve existing section structure (don't reorganize unless necessary) 2. **Rebuild vector index**: ```bash openclaw memory index --force- Generate dream log at
memory/dream-log-YYYY-MM-DD.md:
text ### Technical Analysis The pre-flight script checks whether `.dream_lock` already exists and may remove a stale lock, but it never creates or atomically acquires the lock. It also does not hold a lock for the duration of the read-modify-write operation. The check and subsequent consolidation are separate operations. Multiple scheduled, heartbeat, or manually triggered agents can therefore execute the script concurrently, all observe that no lock exists, and all receive the `READY` result. Each agent may then read the same initial memory state and independently modify `MEMORY.md`, generate a log, rebuild the index, and update timestamps. This is a time-of-check-to-time-of-use race condition. A plain existence check would remain insufficient even if a later non-atomic file creation were added. The lock must be acquired atom ...[truncated 1503 chars]- Generate dream log at
- Remediation
View remediation
Remediation Suggestions
- Acquire an atomic lock before any scan or modification and hold it through all memory writes, index rebuilding, log generation, and timestamp updates.
- Prefer
flockon a dedicated file descriptor, for example:
bash LOCK_FILE="$WORKSPACE/memory/.dream_lock" exec 9>"$LOCK_FILE" if ! flock -n 9; then echo "SKIP: dream already running" exit 1 fi- Ensure the process that performs consolidation inherits and retains the locked file descriptor. A pre-flight script that exits before consolidation cannot protect the later operation.
- If
flockis unavailable, atomically create a lock directory withmkdir, record the owner PID and start time, and install a cleanup trap. Stale-lock removal must verify that the recorded owner is no longer active. - Write the new
MEMORY.mdto a temporary file on the same filesystem, validate it, and use an atomic rename to replace the original. - Avoid multiple processes writing the same date-based dream-log file. Use an atomic append operation or unique run identifiers.
- Rebuild the vector index only after the final memory file has been committed while the lock remains held.
- Add an integration test that launches multiple simultaneous consolidation attempts and verifies that exactly one obtains the lock and modifies persistent state.
