T09 · Insecure Skill Coding Practices
- Location
scripts/gen_images.sh:40- Finding
Symbolic Link Allows Writes Outside the Job Directory
- Content
View full analysis
"$DEST" } ``` The destination is constructed elsewhere as follows: ```bash DEST="$JOB/images/$(basename "$FILE")" gen_one "$ID" "$PROMPT" "$DEST" & ``` ### Technical Analysis Using `basename` prevents direct `../` path traversal through the timeline filename, but it does not protect against symbolic links already present in the `images` directory. The script writes directly to `$DEST` using shell redirection without checking whether the destination is a symbolic link. The initial `-s` test only skips an existing target whose resolved file has a nonzero size. It does not reject: - A dangling symbolic link. - A symbolic link to an empty writable file. - A link whose target changes between the check and the write. When redirection opens such a path, the operating system follows the symbolic link. Generated image bytes can consequently be written to a location outside `$JOB/images`. The separate existence check and subsequent write also create a time-of-check-to-time-of-use race. An attacker with concurrent write access to the job directory could replace the destination with a symbolic link after the check. ### Attack Path 1. The attacker obtains the ability to prepare or modify the supplied job directory. 2. The attacker creates `JOB/images/scene.png` as a symbolic link to an empty file writable by the account running the Skill. 3. The attacker places `scene.png` in the corresponding timeline entry in `input.json`. 4. The Skill derives the destination as `JOB/images/scene.png`. 5. Because the symbolic-link targe ...[truncated 847 chars]- Remediation
View remediation
&2 return 1 fi ``` 2. Canonicalize the images directory and destination parent, then verify that the destination remains beneath the canonical images directory. 3. Create a temporary output file inside the images directory with restrictive permissions. 4. Decode and validate the response into that temporary file. 5. Atomically rename the validated temporary file to the final destination. 6. Use an operating-system mechanism that refuses to follow links, such as `openat2` with path-resolution restrictions or `O_NOFOLLOW`, if implemented through a helper language. 7. Do not rely on a check followed by ordinary shell redirection, because that remains vulnerable to a race. 8. Restrict permissions on the job and images directories so untrusted users cannot modify them during generation. ]]>
