T09 · Insecure Skill Coding Practices
- Location
scripts/export_srt.sh:43- Finding
Predictable Temporary File Permits Symlink-Based File Overwrite
- Content
View full analysis
"$JOB/.cues.tmp" # Step 2: convert cues to SRT (awk handles HH:MM:SS,mmm formatting + numbering) awk -F'|' ' function ts(t, h,m,s,ms) { h=int(t/3600); t-=h*3600; m=int(t/60); t-=m*60; s=int(t); ms=int((t-s)*1000 + 0.5) if (ms==1000){s++; ms=0} return sprintf("%02d:%02d:%02d,%03d", h, m, s, ms) } { printf "%d\n%s --> %s\n%s\n\n", NR, ts($1), ts($2), $3 } ' "$JOB/.cues.tmp" > "$SRT" rm -f "$JOB/.cues.tmp" ``` ### Technical Analysis The script stores intermediate data at the fixed, predictable path `$JOB/.cues.tmp`. Shell output redirection opens this path with truncation and follows symbolic links. The script neither creates the file atomically nor verifies that the path is a regular file owned by the current process. If an attacker can write to the supplied job directory before the script runs, the attacker can create `.cues.tmp` as a symbolic link to another file writable by the victim. The redirection on line 43 then follows that link, truncates the target, and writes attacker-influenced subtitle cue data into it. The later `rm -f` does not prevent exploitation because the target has already been overwritten. It ordinarily removes only the symbolic link, potentially concealing the temporary artifact after the overwrite. ### Attack Path 1. An attacker obtains write access to a job directory that a more privileged victim will process. 2. The attacker creates a symbolic link at the predictable temporary path: ```bash ln -s /path/to/victim-writable-file /shared/job/.cues.tmp ``` 3. The victim invokes: ```bash bash scripts/export_srt.sh /shared/job ``` 4. The shell evaluates the redirection to `$JOB/.cues.tmp`, follows the symbolic link, and truncates the linked target. 5. `jq` output is w ...[truncated 808 chars]- Remediation
View remediation
"$TMP_CUES" awk -F'|' '...' "$TMP_CUES" > "$SRT" ``` Additional hardening measures: - Verify that `JOB` exists and is a directory before creating output files. - Reject job directories writable by untrusted users when the script runs with elevated or shared-account privileges. - Use `--` before path arguments where supported to prevent option interpretation. - Avoid running this script as `root` or another privileged account. - If hostile concurrent modification is within the threat model, open files through a trusted file descriptor or use a small helper that applies `O_CREAT | O_EXCL | O_NOFOLLOW` rather than relying solely on path-based shell redirection. - Apply equivalent symlink-safety controls to all output paths when job directories may be attacker-controlled. ]]>
