T09 · Insecure Skill Coding Practices
- Location
scripts/compose_from_perception.py:48- Finding
Predictable Shared Temporary Files in Perception Composition
- Content
View full analysis
Vulnerability Details
File Location:
scripts/compose_from_perception.py, lines 48-76
Vulnerability Type: Predictable temporary-file creation and potential symbolic-link overwrite
Risk Level: MediumVulnerable Code
python date_str = datetime.now().strftime("%Y%m%d_%H%M") wav_path = f"/tmp/perception_music_{date_str}.wav" mp3_path = f"/tmp/perception_music_{date_str}.mp3" fm = os.path.expanduser("~/.local/bin/fm_compose") if not os.path.exists(fm): print("Error: fm_compose not found at ~/.local/bin/fm_compose", file=sys.stderr) print("Build from: https://github.com/citriac/perception-music", file=sys.stderr) sys.exit(1) print(f"Composing: brightness={p['brightness']} rms={p['rms']:.1f} phase={p['phase']}", file=sys.stderr) result = subprocess.run([ fm, "--brightness", str(p["brightness"]), "--rms", str(p["rms"]), "--phase", p["phase"], "--duration", "90", "--output", wav_path, ], capture_output=True, text=True, timeout=30) if result.returncode != 0: print(f"Error: {result.stderr}", file=sys.stderr) sys.exit(1) print(result.stderr.strip(), file=sys.stderr) # Convert to MP3 if ffmpeg available ffmpeg = os.path.expanduser("~/.local/bin/ffmpeg") if not os.path.exists(ffmpeg): ffmpeg = "ffmpeg" subprocess.run([ffmpeg, "-y", "-i", wav_path, "-codec:a", "libmp3lame", "-b:a", "128k", mp3_path], capture_output=True, timeout=30)Technical Analysis
The script constructs WAV and MP3 paths directly under the shared
/tmpdirectory using a timestamp with only minute-level precision. These names are predictable, and no exclusive file creation or private temporary directory is used.A local attacker can create one of the anticipated paths before the script runs. If
fm_composeor FFmpeg follows a symbolic link when opening its output, the external process may write through that link. FFmpeg is invoked wi ...[truncated 1542 chars]- Remediation
View remediation
Remediation Suggestions
- Use
tempfile.TemporaryDirectory()to create a private, unpredictable workspace with restrictive permissions. - Place both intermediate files inside that directory rather than directly under
/tmp. - Ensure output files are created exclusively and reject pre-existing destinations.
- Avoid relying on external tools to provide safe symbolic-link handling.
- After successful generation, atomically move the finished file into the destination directory with an explicitly defined collision policy.
- Validate the FFmpeg return code before treating the MP3 as successfully generated.
- Run the script with the least-privileged account required for audio generation.
Example hardening pattern:
python import shutil import tempfile from pathlib import Path with tempfile.TemporaryDirectory(prefix="perception_music_") as temp_dir: wav_path = Path(temp_dir) / "output.wav" mp3_path = Path(temp_dir) / "output.mp3" # Invoke the synthesizer and FFmpeg using these private paths. # Check both subprocess return codes before publishing the result. destination = Path(sc_dir) / f"compose_{date_str}.mp3" os.replace(mp3_path, destination)- Use
