T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/little7_backup.sh:18
- Finding
- Predictable and Insecure Temporary Directory Used for Sensitive Backup Staging<![CDATA[ ## Vulnerability Details **File Location**: `scripts/little7_backup.sh`, lines 18–34 **Vulnerability Type**: Predictable temporary directory with unenforced access permissions **Risk Level**: High ### Vulnerable Code ```bash DATE="$(date +%F)" TMP_BASE="${TMPDIR:-/tmp}/little7-backup-${MODE}-${DATE}-$$" STAGE_STATE="$TMP_BASE/state" STAGE_SECRETS="$TMP_BASE/secrets" OUT_DIR="$DRIVE_BASE/$MODE" LATEST_DIR="$DRIVE_BASE/latest" RESTORE_DIR="$DRIVE_BASE/restore-notes" STATE_NAME="little7-${MODE}-${DATE}.tar.gz" SECRETS_NAME="little7-${MODE}-secrets-${DATE}.tar.gz" STATE_OUT="$OUT_DIR/$STATE_NAME" SECRETS_OUT="$OUT_DIR/$SECRETS_NAME" LATEST_STATE="$LATEST_DIR/little7-${MODE}-latest.tar.gz" LATEST_SECRETS="$LATEST_DIR/little7-${MODE}-secrets-latest.tar.gz" MANIFEST="$TMP_BASE/manifest.txt" mkdir -p "$STAGE_STATE" "$STAGE_SECRETS" "$OUT_DIR" "$LATEST_DIR" "$RESTORE_DIR" cleanup() { rm -rf "$TMP_BASE"; } trap cleanup EXIT ``` ### Technical Analysis The staging path is constructed from a fixed prefix, backup mode, current date, and process ID. These values are predictable or observable on a multi-user system. The directory is created using `mkdir -p` instead of an atomic temporary-directory facility such as `mktemp -d`. The script also does not establish a restrictive `umask` or explicitly set the staging directory to mode `0700`. Consequently, the effective permissions depend on the invoking process's environment and existing filesystem objects. Because `mkdir -p` accepts existing directory components, a local attacker may pre-create a predicted staging path or manipulate its components before the backup process uses it. The staging area subsequently receives identity files, memory, scripts, and explicitly allowlisted secrets. ### Attack Path 1. A local attacker determines the backup schedule and predicts or observes candidate process IDs. 2. The attacker pre-creates a matching path under `/tmp`, or manipulates a directory component used by the backu ...[truncated 1183 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Create the temporary directory atomically and enforce private permissions before copying any data: ```bash umask 077 TMP_ROOT="${TMPDIR:-/tmp}" TMP_BASE="$(mktemp -d "$TMP_ROOT/little7-backup-${MODE}-${DATE}-XXXXXXXX")" chmod 700 "$TMP_BASE" STAGE_STATE="$TMP_BASE/state" STAGE_SECRETS="$TMP_BASE/secrets" mkdir -m 700 "$STAGE_STATE" "$STAGE_SECRETS" ``` Additional hardening should include: 1. Validate that `TMPDIR` is an absolute path to a trusted directory. 2. Reject a `TMPDIR` that is unexpectedly writable or controlled by an untrusted party. 3. Store the exact path returned by `mktemp` and only remove that path during cleanup. 4. Verify ownership and permissions before staging sensitive content. 5. Consider staging secret material on an encrypted filesystem or avoiding plaintext secret staging entirely. ]]>
