Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
.env
.env.new
.env.new.*
.env.restore-old.*
config.json.restore-old.*
Caddyfile.restore-old.*
.env.dryrun
.env.apply-incomplete
.env.caddy-restart-pending
Expand Down
16 changes: 16 additions & 0 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,22 @@ preserves the destination's file in `data/monero`, `data/tari`, and `data/p2pool
install may already have synced chain data, and replacing it would force a resync. It still adds
files that only the archive has. The wizard replaces its Tor and dashboard directories normally.

The fixed files have specific permissions: `config.json` and `.env` carry secrets and stay owner-only
(600); the `Caddyfile` is restored as world-readable (644) because the `caddy` container runs as root
with `cap_drop: ALL` and no `CAP_DAC_OVERRIDE`, so a 600 file would leave it permission-denied on
its bind mount for the rest of the appliance's life (#2329).

`./pithead restore` copies the archive once into a private staging directory, so every check and
the extraction read the same bytes; that directory is removed when restore exits.

If `./pithead restore` fails, or is interrupted, while it installs a fixed file, clears the dashboard
database sidecars or copies a data directory, it puts `config.json`, `.env` and the `Caddyfile` back
as they were before the restore and removes any of them that the restore added. It does not undo the
data: directories copied before the failure, and the one being copied when it failed, keep the
archive's files, and dashboard database sidecars it had removed stay removed. A failure after the
last data directory is copied leaves the restored files in place. A previous file restore cannot put
back stays beside its original name as a `.restore-old` copy, and restore names that copy.

When the administrative archive contains `mining_data.db`, restore removes the destination's
WAL, shared-memory and rollback-journal sidecars before copying the archived database and its
sidecars. A crashed destination writer's committed rows cannot override the archived rows.
Expand Down
40 changes: 35 additions & 5 deletions lib/pithead/16a-restore-safety.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,32 @@ restore_member_allowed() {
restore_discard_stage() {
local stage="${RESTORE_STAGE_DIR:-}"
RESTORE_STAGE_DIR=""
restore_fixed_backups_finish rollback ||
warn "Restore could not put some previous files back — they are kept beside their original names as .restore-old copies."
[ -z "$stage" ] || rm -rf -- "$stage"
}

restore_fixed_backups_finish() {
local entry path backup rc=0 action="$1"
[ "${RESTORE_FIXED_KEEP:-}" != 1 ] || action=keep
[ "$action" != keep ] || RESTORE_FIXED_KEEP=1
for entry in ${RESTORE_FIXED_BACKUPS[@]+"${RESTORE_FIXED_BACKUPS[@]}"}; do
path="${entry%%$'\t'*}" backup="${entry#*$'\t'}"
if [ "$action" = keep ]; then
[ -z "$backup" ] || sudo rm -f -- "$backup" || warn "Could not remove $backup, the copy of $path from before the restore — delete it by hand."
elif [ -n "$backup" ]; then
sudo mv -f -- "$backup" "$path" || {
rc=1
warn "Restore could not put $path back — it is kept as $backup."
}
else
sudo rm -f -- "$path" || warn "Restore could not remove $path, which it had added — it still holds the archive's version."
fi
done
RESTORE_FIXED_BACKUPS=() RESTORE_FIXED_KEEP=""
return "$rc"
}

restore_destination_safe() { # <path> <file|dir>
local path="$1" kind="$2" probe="$1"
[ ! -L "$path" ] || return 1
Expand Down Expand Up @@ -255,7 +278,8 @@ restore_recheck_destinations() {
}

restore_commit_stage() {
local path rel owner_uid owner_gid
local path rel owner_uid owner_gid backup
RESTORE_FIXED_BACKUPS=() RESTORE_FIXED_KEEP=""
if ! owner_uid=$(id -u "$REAL_USER") || ! owner_gid=$(id -g "$REAL_USER"); then
restore_discard_stage
error "Restore could not resolve the invoking operator's ownership — nothing was restored."
Expand All @@ -264,8 +288,15 @@ restore_commit_stage() {
rel="${path#/}"
local mode=600
[ "$path" != "$PWD/Caddyfile" ] || mode=644
[ ! -e "$RESTORE_STAGE_DIR/$rel" ] ||
sudo_install_file "$RESTORE_STAGE_DIR/$rel" "$path" "$mode" "$owner_uid:$owner_gid" || {
[ -e "$RESTORE_STAGE_DIR/$rel" ] || continue
backup=""
if [ -e "$path" ] && ! { backup=$(sudo mktemp "$path.restore-old.XXXXXXXXXX") && sudo cp -a -- "$path" "$backup"; }; then
[ -z "$backup" ] || sudo rm -f -- "$backup" || warn "Could not remove $backup, a partial copy of $path — delete it by hand."
restore_discard_stage
error "Restore could not keep a copy of $path — nothing was restored."
fi
RESTORE_FIXED_BACKUPS+=("$path"$'\t'"$backup")
sudo_install_file "$RESTORE_STAGE_DIR/$rel" "$path" "$mode" "$owner_uid:$owner_gid" || {
restore_discard_stage
error "Restore failed while committing $path; inspect the destination before retrying."
}
Expand All @@ -286,8 +317,7 @@ restore_commit_stage() {
error "Restore failed while committing $path; inspect the destination before retrying."
}
done
# The archive wins ordinary collisions, but Tor's circuit history must never survive a
# restore. Its onion keys are separate files and remain untouched.
restore_fixed_backups_finish keep
sudo rm -f -- "${RESTORE_ALLOWED_DIRS[3]}/state" || {
restore_discard_stage
error "Restore committed data but could not discard Tor circuit state; keep the stack stopped and remove the state before starting Tor."
Expand Down
4 changes: 1 addition & 3 deletions lib/pithead/17-backup-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,7 @@ stack_restore() {
archive=$(cd "$(dirname "$archive")" && printf '%s/%s' "$PWD" "$(basename "$archive")")
archive_source="$archive"

# Snapshot once so every audit and the extraction read the same bytes. Clear inherited state
# before arming cleanup: only a directory created by this invocation may be removed.
RESTORE_STAGE_DIR=""
RESTORE_STAGE_DIR="" RESTORE_FIXED_BACKUPS=() RESTORE_FIXED_KEEP=""
trap restore_discard_stage EXIT
RESTORE_STAGE_DIR=$(mktemp -d) || error "Could not create a private restore staging directory."
if ! (umask 077 && cp -- "$archive" "$RESTORE_STAGE_DIR/.archive" && chmod 600 "$RESTORE_STAGE_DIR/.archive"); then
Expand Down
85 changes: 85 additions & 0 deletions tests/stack/test-cli-restore-hardening.sh
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,91 @@ assert_eq "restored Caddyfile is world-readable" "$(file_mode "$BK/Caddyfile")"
assert_eq "restored config belongs to the invoking operator" "$(file_uid "$BK/config.json")" "$(id -u)"
assert_eq "restored onion key is owner-only" "$(file_mode "$BK/data/tor/hs_ed25519_secret_key")" 600
assert_eq "restored database is owner-only" "$(file_mode "$BK/data/dashboard/dashboard.db")" 600
assert_eq "successful restore leaves no copies of the fixed files" "$(find "$BK" -maxdepth 1 -name '*.restore-old.*')" ""
jq '.dashboard.energy.cost_per_kwh = 0.456' "$BK/config.json" >"$CR/pre-config" && cp "$CR/pre-config" "$BK/config.json"
printf 'PRE_RESTORE_MARK=1\n' >>"$BK/.env"
printf 'CADDY-PRE-RESTORE\n' >"$BK/Caddyfile"
cp "$BK/.env" "$CR/pre-env"
mkdir "$CR/fail-bin"
printf '#!/usr/bin/env bash\n[ "${!#}" != "$FAIL_COPY_DEST" ] || exit 1\nexec /bin/cp "$@"\n' >"$CR/fail-bin/cp"
chmod +x "$CR/fail-bin/cp"
out="$(cd "$BK" && FAIL_COPY_DEST="$BK/data/dashboard/" PATH="$CR/fail-bin:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)"
assert_rc "restore reports a failed data copy" "$?" 1
assert_contains "failed data copy names the dashboard directory" "$out" "committing $BK/data/dashboard"
assert_eq "failed data copy puts the live config back" "$(cat "$BK/config.json")" "$(cat "$CR/pre-config")"
assert_eq "failed data copy puts the live env back" "$(cat "$BK/.env")" "$(cat "$CR/pre-env")"
assert_eq "failed data copy puts the live Caddyfile back" "$(cat "$BK/Caddyfile")" CADDY-PRE-RESTORE
assert_eq "rolled-back config stays owner-only" "$(file_mode "$BK/config.json")" 600
assert_eq "rolled-back Caddyfile stays world-readable" "$(file_mode "$BK/Caddyfile")" 644
assert_eq "failed data copy leaves no copies of the fixed files" "$(find "$BK" -maxdepth 1 -name '*.restore-old.*')" ""
printf '#!/usr/bin/env bash\ncase "$*" in *.restore-old.*" $FAIL_MV_DEST") exit 1 ;; esac\nexec /bin/mv "$@"\n' >"$CR/fail-bin/mv"
chmod +x "$CR/fail-bin/mv"
out="$(cd "$BK" && FAIL_COPY_DEST="$BK/data/dashboard/" FAIL_MV_DEST="$BK/.env" PATH="$CR/fail-bin:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)"
assert_rc "failed put-back keeps the failed restore's exit code" "$?" 1
assert_contains "failed put-back keeps the data copy error" "$out" "committing $BK/data/dashboard"
assert_contains "failed put-back names the .restore-old copies" "$out" "kept beside their original names as .restore-old copies"
assert_contains "failed put-back names where the env copy is" "$out" "Restore could not put $BK/.env back — it is kept as $BK/.env.restore-old."
assert_eq "failed put-back keeps the previous env beside its name" "$(cat "$BK"/.env.restore-old.*)" "$(cat "$CR/pre-env")"
assert_eq "failed put-back still puts the live config back" "$(cat "$BK/config.json")" "$(cat "$CR/pre-config")"
assert_eq "failed put-back still puts the live Caddyfile back" "$(cat "$BK/Caddyfile")" CADDY-PRE-RESTORE
mv "$BK"/.env.restore-old.* "$BK/.env"
rm -f "$CR/fail-bin/mv"

printf 'EXISTING-ENV\n' >"$BK/.env"
printf 'EXISTING-CADDY\n' >"$BK/Caddyfile"
rm -f "$BK/config.json"
mkdir "$CR/fail-bin2"
printf '#!/usr/bin/env bash\n[ "${!#}" != "$FAIL_COPY_DEST" ] || exit 1\nexec /bin/cp "$@"\n' >"$CR/fail-bin2/cp"
chmod +x "$CR/fail-bin2/cp"
out="$(cd "$BK" && FAIL_COPY_DEST="$BK/data/dashboard/" PATH="$CR/fail-bin2:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)"
assert_rc "failed data copy with missing config.json exits 1" "$?" 1
assert_not_contains "missing config.json is not restored on failure" "$(test -f "$BK/config.json" && echo yes)" yes
assert_eq "existing .env survives put-back" "$(cat "$BK/.env")" EXISTING-ENV
assert_eq "existing Caddyfile survives put-back" "$(cat "$BK/Caddyfile")" EXISTING-CADDY
assert_eq "rollback of added file leaves no .restore-old copy" "$(find "$BK" -maxdepth 1 -name 'config.json.restore-old.*')" ""
rm -f "$CR/fail-bin2/cp"

cp "$CR/pre-config" "$BK/config.json"
mkdir "$CR/fail-bin3"
printf '%s\n' '#!/usr/bin/env bash' 'case "${!#}" in *.env.restore-old.*) exit 1 ;; esac' 'exec /bin/cp "$@"' >"$CR/fail-bin3/cp"
printf '%s\n' '#!/usr/bin/env bash' 'case "$*" in *.env.restore-old.*) exit 1 ;; esac' 'exec /bin/rm "$@"' >"$CR/fail-bin3/rm"
chmod +x "$CR/fail-bin3/cp" "$CR/fail-bin3/rm"
out="$(cd "$BK" && PATH="$CR/fail-bin3:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)"
assert_rc "a failed copy of a fixed file stops the restore" "$?" 1
assert_contains "a failed copy of a fixed file says nothing was restored" "$out" "Restore could not keep a copy of $BK/.env — nothing was restored."
assert_contains "a partial copy that cannot be removed is named" "$out" "Could not remove $BK/.env.restore-old."
assert_eq "a failed copy of a fixed file puts the live config back" "$(cat "$BK/config.json")" "$(cat "$CR/pre-config")"
assert_eq "a failed copy of a fixed file leaves the live env alone" "$(cat "$BK/.env")" EXISTING-ENV
/bin/rm -f "$BK"/.env.restore-old.*

mkdir "$CR/term-bin"
printf '%s\n' '#!/usr/bin/env bash' '/bin/rm "$@" || exit' 'case "$*" in *config.json.restore-old.*) ;; *) exit 0 ;; esac' \
'[ ! -e "$TERM_MARK" ] || exit 0' ': >"$TERM_MARK"' 'pid=$PPID' \
'while [ "${pid:-0}" -gt 1 ]; do' \
' case "$(ps -o args= -p "$pid")" in *"pithead restore"*) kill -TERM "$pid"; exit 0 ;; esac' \
' pid=$(ps -o ppid= -p "$pid" | tr -d " ")' 'done' >"$CR/term-bin/rm"
chmod +x "$CR/term-bin/rm"
out="$(cd "$BK" && TERM_MARK="$CR/term-sent" PATH="$CR/term-bin:$BK/bin:$PATH" ./pithead restore -y "$CR_ARCHIVE" 2>&1)"
assert_rc "a signal while the copies are removed ends the restore" "$?" 143
assert_not_contains "a signal while the copies are removed puts nothing back" "$out" "could not put"
assert_not_contains "a signal while the copies are removed keeps the restored env" "$(cat "$BK/.env")" EXISTING-ENV
assert_not_contains "a signal while the copies are removed keeps the restored Caddyfile" "$(cat "$BK/Caddyfile")" EXISTING-CADDY
assert_eq "a signal while the copies are removed leaves no copies" "$(find "$BK" -maxdepth 1 -name '*.restore-old.*')" ""

printf 'victim-a\n' >"$CR/victim-a"
printf 'victim-b\n' >"$CR/victim-b"
printf 'not an archive\n' >"$CR/junk.tar.gz"
out="$(cd "$BK" && RESTORE_FIXED_BACKUPS="$CR/victim-a"$'\t'"$CR/victim-b" RESTORE_FIXED_KEEP=1 PATH="$BK/bin:$PATH" ./pithead restore -y "$CR/junk.tar.gz" 2>&1)"
assert_rc "restore refuses a file that is not a backup" "$?" 1
assert_eq "an inherited rollback list cannot replace a file" "$(cat "$CR/victim-a" 2>/dev/null)" victim-a
assert_eq "an inherited rollback list cannot remove a file" "$(cat "$CR/victim-b" 2>/dev/null)" victim-b
assert_eq "copies of the fixed files are ignored by git" \
"$(git -C "$ROOT" check-ignore --no-index .env.restore-old.x config.json.restore-old.x Caddyfile.restore-old.x | wc -l | tr -d ' ')" 3

printf 'EXISTING-ENV\n' >"$BK/.env"
printf 'EXISTING-CADDY\n' >"$BK/Caddyfile"
cp "$CR/pre-config" "$BK/config.json"
chmod 644 "$BK/config.json"

# Generated files in a backup are compatibility inputs, never runtime policy. A valid config plus
# stale .env/Caddyfile must land the normal writers' output, while opaque generated identity and
Expand Down
Loading