From 97779595089b1623e513bfc84baa8c3a2935afc0 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Tue, 1 Dec 2026 02:05:36 -0800 Subject: [PATCH] fix(prelaunch): v0.0.22 - stop installing user_config SSH keys as root The script appended user_config.ssh_authorized_keys to root's authorized_keys. The host supplies user_config: it is not measured, not authenticated and not filtered by allowed_envs. On any image with sshd, which means every dstack 0.5.x dev image, whoever ran the host could therefore log in as root. v0.0.22 ignores that key and logs that it did. The key count and fingerprint log now cover whatever the encrypted env installed, so the boot log still lists every authorized key. The README documents the remaining channel, DSTACK_AUTHORIZED_KEYS, DSTACK_ROOT_PUBLIC_KEY and DSTACK_ROOT_PASSWORD from the encrypted env. Anyone can produce a valid encrypted env, because it is sealed to a public key, so these are for dev images on trusted hosts only. The README also covers migration for apps that used user_config keys. Signed-off-by: Kevin Wang --- phala-cloud-prelaunch-script/README.md | 18 ++++++++++++++++++ phala-cloud-prelaunch-script/prelaunch.sh | 22 +++++++++++----------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/phala-cloud-prelaunch-script/README.md b/phala-cloud-prelaunch-script/README.md index f23b8ad7..42f39609 100644 --- a/phala-cloud-prelaunch-script/README.md +++ b/phala-cloud-prelaunch-script/README.md @@ -8,6 +8,7 @@ This pre-launch script for Phala Cloud handles: - Pull private images from AWS ECR - Remove unused images and containers from local disk - Expose App ID via `DSTACK_APP_ID` environment variable +- Install root SSH keys and the root password from encrypted environment variables ## Private registry support @@ -37,3 +38,20 @@ The following three environment variables will be automatically added by the def - `DSTACK_APP_DOMAIN`: The default domain of the Dstack App. NOTE: `DSTACK_APP_DOMAIN` equals to `${DSTACK_APP_ID}.${DSTACK_GATEWAY_DOMAIN}`, which acccessing port 80. It equals to `${DSTACK_APP_ID}-80.${DSTACK_GATEWAY_DOMAIN}`. + +## SSH access + +On images that ship `sshd`, which are the dstack 0.5.x dev images, the script sets up root access from these encrypted environment variables: + +- `DSTACK_AUTHORIZED_KEYS` / `DSTACK_ROOT_PUBLIC_KEY`: written to root's `authorized_keys` +- `DSTACK_ROOT_PASSWORD`: root password + +The boot log prints the SHA256 fingerprint of every installed key. Compare these fingerprints with your own keys. + +**Encrypted env is not authenticated.** It is sealed to the app's public encryption key, so anyone can produce a valid ciphertext, including whoever runs the host. A host that relaunches your app with its own encrypted env can therefore set these variables and log in as root. Use them only with a development image, and only on a host you trust. Production images ship no `sshd`. To detect a replaced env, use the checks in dstack's [security best practices](https://github.com/Dstack-TEE/dstack/blob/next/docs/security/security-best-practices.md#authenticated-envs-and-user_config), such as `APP_LAUNCH_TOKEN` or KMS launch tickets. + +### `user_config.ssh_authorized_keys` is ignored (v0.0.22) + +Up to v0.0.21 the script also appended `ssh_authorized_keys` from `/dstack/user_config` to root's `authorized_keys`. The host supplies `user_config`. That file is not measured, not authenticated, and not filtered by `allowed_envs`. Any host could therefore give itself root SSH on every image with `sshd`. From v0.0.22 the script ignores that key and logs that it did so. + +Migration: to keep SSH access through the pre-launch script, put your public keys in the `DSTACK_AUTHORIZED_KEYS` encrypted environment variable. The risk described above applies. Changing the pre-launch script changes the compose hash, so for apps governed on chain the new hash must be whitelisted before the upgrade. `/home` is not persistent, so keys installed by an older version are gone after the first boot with v0.0.22. diff --git a/phala-cloud-prelaunch-script/prelaunch.sh b/phala-cloud-prelaunch-script/prelaunch.sh index 12442a1e..ce5ec72b 100644 --- a/phala-cloud-prelaunch-script/prelaunch.sh +++ b/phala-cloud-prelaunch-script/prelaunch.sh @@ -1,6 +1,6 @@ #!/bin/bash echo "----------------------------------------------" -echo "Running Phala Cloud Pre-Launch Script v0.0.21" +echo "Running Phala Cloud Pre-Launch Script v0.0.22" echo "----------------------------------------------" set -e @@ -357,20 +357,20 @@ if mkdir -p /home/root/.ssh 2>/dev/null; then echo "Root authorized_keys set" fi + # user_config is supplied by the host: it is not measured, not + # authenticated, and not filtered by allowed_envs. Installing its + # ssh_authorized_keys would let whoever runs the host log in as root on + # any image that ships sshd, so the keys are ignored. if [[ -f /dstack/user_config ]] && jq empty /dstack/user_config 2>/dev/null; then if [[ $(jq 'has("ssh_authorized_keys")' /dstack/user_config 2>/dev/null) == "true" ]]; then - # jq -r terminates the value with a newline; jq -j does not, which - # both concatenated the next appended key onto the same line and - # made line-based counting report one key fewer than the file holds. - jq -r '.ssh_authorized_keys' /dstack/user_config >> /home/root/.ssh/authorized_keys - # Drop duplicates and the blank line an empty key list produces. - sort -u /home/root/.ssh/authorized_keys > /home/root/.ssh/authorized_keys.tmp - mv /home/root/.ssh/authorized_keys.tmp /home/root/.ssh/authorized_keys - KEY_COUNT=$(grep -c '^[^[:space:]]' /home/root/.ssh/authorized_keys || true) - echo "Set root authorized_keys from user preferences, total $KEY_COUNT keys" - print_authorized_key_fingerprints /home/root/.ssh/authorized_keys + echo "Ignoring ssh_authorized_keys from user_config (host-supplied, unauthenticated)" fi fi + if [[ -f /home/root/.ssh/authorized_keys ]]; then + KEY_COUNT=$(grep -c '^[^[:space:]]' /home/root/.ssh/authorized_keys || true) + echo "Root authorized_keys holds $KEY_COUNT keys" + print_authorized_key_fingerprints /home/root/.ssh/authorized_keys + fi else echo "Warning: Cannot create /home/root/.ssh directory (read-only file system?)" echo "Skipping SSH key setup"