T09 · Insecure Skill Coding Practices
- Location
scripts/restore_memory.sh:35- Finding
Command Injection Through Unvalidated Restore Selection
- Content
View full analysis
/dev/null | sed -n "${choice}p") ``` ### Technical Analysis The script requests a numeric backup selection but does not verify that `choice` contains only an integer from 1 through 10. Instead, it directly interpolates the value into a GNU `sed` program: ```bash sed -n "${choice}p" ``` This is not ordinary argument injection; the user-controlled value becomes executable `sed` syntax. GNU `sed` supports commands such as `e`, which can invoke a shell command. An input containing a valid address followed by an injected `e` command can therefore cause command execution while `sed` evaluates the generated program. Shell quoting around `"${choice}p"` does not prevent this vulnerability. It prevents shell word splitting, but it does not prevent `sed` from interpreting the content as program instructions. ### Attack Path 1. An attacker convinces a user to run `scripts/restore_memory.sh`, or otherwise controls the value entered at the backup-selection prompt. 2. Instead of entering a number from 1 through 10, the attacker supplies a crafted GNU `sed` expression containing the `e` command. 3. The script appends `p` and passes the resulting attacker-controlled program to `sed -n`. 4. GNU `sed` evaluates the injected command and invokes a shell command. 5. The command runs with the same operating-system privileges and environment as the user running the restore script. 6. Subsequent backup-path validation may fail, but it occurs only after the injected command has already executed. ### Impact Assessment Successful exploitation permits arbitrary local command execution ...[truncated 418 chars]- Remediation
View remediation
${#backups[@]} )); then echo "Invalid selection" exit 1 fi SELECTED_BACKUP=${backups[choice-1]} ``` At minimum, enforce `^([1-9]|10)$` before the existing `sed` call. Using a Bash array is preferable because it eliminates dynamic `sed` syntax entirely. The selected path should also be canonicalized and verified to remain beneath `$BACKUP_BASE` before any restore or deletion operation. ]]>
