diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 0000000..8df1ef0 --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2025-02-20 - TOCTOU Vulnerability in SSH Key Restoration +**Vulnerability:** Time-of-Check to Time-of-Use (TOCTOU) when restoring SSH private keys from 1Password. +**Learning:** Writing sensitive data to a file using default permissions and then using `chmod 600` leaves the file briefly readable by other users on the system between the write and the chmod. +**Prevention:** Use a subshell with `umask 077` (e.g., `(umask 077 && command > file)`) to ensure the file is created with secure permissions (600) from the start. diff --git a/tools/setup-ssh-keys.sh b/tools/setup-ssh-keys.sh index bde52fd..c1e8ec6 100755 --- a/tools/setup-ssh-keys.sh +++ b/tools/setup-ssh-keys.sh @@ -149,15 +149,15 @@ cmd_restore() { say "Restoring SSH key from 1Password..." # Create SSH directory - mkdir -p "$SSH_DIR" + (umask 077 && mkdir -p "$SSH_DIR") chmod 700 "$SSH_DIR" # Read private key from 1Password and save locally - op read "op://$VAULT/$KEY_NAME/private_key" > "$PRIVATE_KEY_FILE" + (umask 077 && op read "op://$VAULT/$KEY_NAME/private_key" > "$PRIVATE_KEY_FILE") chmod 600 "$PRIVATE_KEY_FILE" # Read public key from 1Password and save locally - op read "op://$VAULT/$KEY_NAME/public_key" > "$PUBLIC_KEY_FILE" + (umask 022 && op read "op://$VAULT/$KEY_NAME/public_key" > "$PUBLIC_KEY_FILE") chmod 644 "$PUBLIC_KEY_FILE" say "SSH key restored to $SSH_DIR"