T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/restore.sh:61
- Finding
- Unvalidated archive extraction permits writes outside the restore directory<![CDATA[ ## Vulnerability Details **File Location**: `scripts/restore.sh:61-69` **Vulnerability Type**: Untrusted archive extraction **Risk Level**: High ### Vulnerable Code ```bash WORK_DIR="/tmp/hermes-restore-$$" mkdir -p "$WORK_DIR" trap "rm -rf $WORK_DIR" EXIT info "Extracting archive..." tar -xzf "$ARCHIVE" -C "$WORK_DIR" BACKUP_DIR=$(find "$WORK_DIR" -maxdepth 1 -name "hermes-backup_*" -type d | head -1) [[ -z "$BACKUP_DIR" ]] && error "Invalid archive: no hermes-backup_* directory found" ``` ### Technical Analysis The restore script extracts an attacker-controlled archive before validating its entries. It does not reject: - Absolute paths - Paths containing `..` - Symbolic or hard links pointing outside the extraction directory - Device files, FIFOs, or other unexpected entry types - Multiple or unexpected top-level directories Checking for a directory named `hermes-backup_*` after extraction does not make the other archive entries safe. The extraction also occurs during `--dry-run`, even though that mode states that no changes will be made. Depending on the host `tar` implementation and archive structure, malicious entries or link-based extraction sequences may write outside the intended temporary directory. ### Attack Path 1. An attacker creates a backup archive containing the expected `hermes-backup_*` directory and malicious traversal or link entries. 2. The attacker convinces a user to inspect or restore the archive. 3. The user invokes `restore.sh`, potentially with `--dry-run`. 4. `tar -xzf` processes all archive members before the manifest or directory structure is validated. 5. Malicious members write or redirect files outside the intended restore directory. 6. The overwritten file may subsequently expose data, alter configuration, or execute code when loaded by the user or agent platform. ### Impact Assessment Successful exploitation operates with the privileges of the user running the restore command. It may permit modification o ...[truncated 204 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. List all archive members before extraction and reject entries that: - Are absolute paths - Contain `..` path components - Resolve outside the extraction root - Are device files, FIFOs, or other unsupported types - Contain unsafe symbolic or hard links 2. Require exactly one expected top-level directory. 3. Extract into a directory created with `mktemp -d` under a restrictive `umask`. 4. Use extraction options that prevent ownership and permission restoration, such as `--no-same-owner` and `--no-same-permissions`, where supported. 5. Validate the manifest and archive structure before performing the actual extraction. 6. Ensure dry-run mode only lists and validates archive members and never extracts them. 7. Cryptographically authenticate backups before processing them. ]]>
