T09 · Insecure Skill Coding Practices
- Location
scripts/set_timeout.py:116- Finding
Atomic Configuration Replacement Does Not Preserve Restrictive File Permissions
- Content
View full analysis
Vulnerability Details
File Location:
scripts/set_timeout.py, lines 116–124
Vulnerability Type: Unsafe temporary-file permissions and loss of destination permission mode
Risk Level: MediumVulnerable Code
python def write_config(path: Path, config: Dict) -> bool: try: # Atomic write: temp file + rename tmp = path.with_suffix(path.suffix + ".tmp") with open(tmp, "w", encoding="utf-8") as f: json.dump(config, f, indent=2, ensure_ascii=False) f.write("\n") os.replace(tmp, path) return True except Exception as e: print(f"❌ Failed to write config: {e}", file=sys.stderr) return FalseTechnical Analysis
The implementation serializes the complete OpenClaw configuration into a predictably named temporary file using
open(tmp, "w"). The temporary file's mode is determined by the process umask rather than inherited from the original configuration. The subsequentos.replace(tmp, path)installs the temporary file—including its independently assigned permission mode—as the live configuration.Consequently, an original configuration restricted to its owner, such as mode
0600, can become mode0644under a common0022umask. The temporary copy may also be readable by other local users while it exists. Although the skill only modifies timeout fields, it reads and rewrites the entire JSON document, which may contain unrelated sensitive OpenClaw settings.The predictable
<config>.tmppath also lacks the collision resistance of a securely created temporary file. This increases robustness concerns when concurrent invocations occur, although exploitation through link manipulation depends on directory permissions and the victim's local environment.Attack Path
- A user has an OpenClaw configuration file with restrictive permissions, such as
0600, and the file contains sensitive settings. - The user invokes the skill to apply a timeout profil ...[truncated 1155 chars]
- A user has an OpenClaw configuration file with restrictive permissions, such as
- Remediation
View remediation
Remediation Suggestions
- Obtain the original file's mode before writing and explicitly apply it to the replacement file.
- Create the temporary file securely and exclusively in the destination directory, initially using mode
0600. A random temporary name created throughtempfile.mkstemp(dir=path.parent)avoids predictable-name collisions. - After serialization, flush Python buffers and call
os.fsync()on the temporary file before replacement. - Apply the original permission mode with
os.fchmod()before installing the file. If ownership preservation is required and permitted, verify and preserve ownership as well. - Atomically replace the destination only after all writes and metadata operations succeed.
- Remove the temporary file in a
finallyblock when any operation fails. - Consider
fsync()on the parent directory afteros.replace()where crash durability is required. - Add an automated test that begins with a
0600configuration, performs an update, and verifies that the resulting file remains0600.
A hardened implementation should follow this sequence:
python import os import stat import tempfile original_mode = stat.S_IMODE(path.stat().st_mode) fd, tmp_name = tempfile.mkstemp(prefix=f".{path.name}.", dir=path.parent) try: with os.fdopen(fd, "w", encoding="utf-8") as f: json.dump(config, f, indent=2, ensure_ascii=False) f.write("\n") f.flush() os.fsync(f.fileno()) os.fchmod(f.fileno(), original_mode) os.replace(tmp_name, path) finally: try: os.unlink(tmp_name) except FileNotFoundError: pass
