T05 · Unauthorized Access and Privilege Escalation
Error
- Location
- dream-tools.sh:92
- Finding
- Unrestricted Atomic-Write Command Allows Arbitrary File Replacement<![CDATA[ ## Vulnerability Details **File Location**: `dream-tools.sh`, lines 92–113 **Vulnerability Type**: Arbitrary file write through missing path authorization **Risk Level**: High ### Vulnerable Code ```bash cmd_atomic_write() { local target="${1:-}" local tmpfile="${2:-}" [[ -z "$target" || -z "$tmpfile" ]] && die "--atomic-write 需要 <target> <tmpfile>" [[ ! -f "$tmpfile" ]] && die "tmp 文件不存在:$tmpfile" # 若目标是 MEMORY.md,检查字符数上限 if [[ "$(realpath "$target" 2>/dev/null)" == "$(realpath "$MEMORY_MD" 2>/dev/null)" ]] || \ [[ "$target" == *"MEMORY.md" ]]; then local size size=$(wc -c < "$tmpfile" | tr -d ' ') if [[ $size -gt $MEMORY_HARD_LIMIT ]]; then die "写入中止:tmp 文件大小 ${size} 字符,超过硬上限 ${MEMORY_HARD_LIMIT}。请先压缩内容。" fi log "MEMORY.md 写入校验通过:${size}/${MEMORY_HARD_LIMIT} 字符" fi ensure_dir "$target" # mv 在同一文件系统上是原子操作 mv "$tmpfile" "$target" log "原子写入完成:$target" } ``` ### Technical Analysis The `--atomic-write` operation accepts both its source and destination from command-line arguments. It does not verify that the canonical destination is located under either `DREAM_VAULT_PATH` or `OPENCLAW_WORKSPACE`. The `realpath` comparison only determines whether to apply the `MEMORY.md` size limit. It is not an access-control check. For any destination that does not end in `MEMORY.md`, the script creates the destination's parent directory and moves the supplied file over the destination. Consequently, any component or user able to invoke this helper can replace any file writable by the OpenClaw operating-system account. This directly contradicts the security claim in `readme.md` that all file operations are strictly scoped to the vault and workspace. Path and symlink behavior also require hardening. A lexical prefix check alone would not be sufficient because traversal components and symlinks could redirect writes outside an approved root. ### Attack Path 1. Th ...[truncated 1429 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Canonicalize the destination and enforce an explicit allowlist of permitted files or directories. 2. Require every destination to resolve beneath `DREAM_VAULT_PATH` or `OPENCLAW_WORKSPACE`. 3. Reject symbolic-link destinations and symbolic-link parent components. 4. Restrict `--atomic-write` to known destinations such as the configured `MEMORY_MD` rather than accepting a general path. 5. Create temporary files in the destination directory to preserve same-filesystem atomic replacement. 6. Verify ownership and permissions before replacement. 7. Use `mv -- "$tmpfile" "$target"` and similar end-of-options markers as defense in depth. 8. Add negative tests covering absolute paths, `..` traversal, symlink escapes, shell configuration files, and destinations outside approved roots. A hardened implementation should derive the target internally where possible instead of trusting a caller-supplied path. ]]>
