From 5f6999631a233e9a429514c5dc7e42dbcfe3eb80 Mon Sep 17 00:00:00 2001 From: Phoebus Date: Fri, 25 Sep 2026 20:02:43 -0700 Subject: [PATCH] fix(vm): rollback gate proves a real mutation and restore The gate could pass vacuously: the guest ran unprivileged (every mutation refused), used a pre-installed break package, restored the newest snapshot instead of its own, and the driver never asserted the root marker was gone. Now the driver authenticates sudo with unique end markers and a verified timestamp, both phases run as root with explicit --legacy, the break uses app-misc/hello (verified absent, verified changed), undo targets the recorded snapshot id, and the driver asserts the break changed the package DB and the root marker is absent after the detached reboot. --- installer/tests/vm/rollback-guest.sh | 40 +++++++++++++++++++++++----- installer/tests/vm/serial-driver.py | 9 +++++++ installer/tests/vm/test_vmsafety.py | 9 +++++++ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/installer/tests/vm/rollback-guest.sh b/installer/tests/vm/rollback-guest.sh index 42bf7d7..c521a57 100755 --- a/installer/tests/vm/rollback-guest.sh +++ b/installer/tests/vm/rollback-guest.sh @@ -32,6 +32,17 @@ USER_NAME="${USERNAME:-phoebus}" WORK=/tmp/phoebus-rollback mkdir -p "$WORK" +# The controlled break must genuinely mutate the system: a package that is +# already installed would make the whole acceptance vacuous (snapshot and +# restore of an identical tree). GNU hello is tiny and never in the base set. +BREAK_PKG="app-misc/hello" +BREAK_CAT="app-misc" +BREAK_NAME="hello" +if ls -d "/var/db/pkg/$BREAK_CAT/$BREAK_NAME-"* >/dev/null 2>&1; then + echo "===ROLLBACK-BREAK-PRESENT $BREAK_PKG already installed; refusing a vacuous run===" + exit 1 +fi + pkg_fingerprint() { # The package database, not a marker file: a rollback must restore it. find /var/db/pkg -mindepth 2 -maxdepth 2 -type d 2>/dev/null \ @@ -52,18 +63,35 @@ echo "===ROLLBACK-KNOWN-GOOD pkgs=$PKGS_BEFORE kernel=$KERNEL home=$HOME_MARKER= "$SYS" -y snapshot "rollback acceptance" --legacy > "$WORK/snapshot.out" 2>&1 SNAP_ID="$(sed -n 's/^protective snapshot: //p' "$WORK/snapshot.out" | tail -1)" echo "===ROLLBACK-SNAPSHOT id=$SNAP_ID===" +if [[ -z "$SNAP_ID" ]]; then + echo "--- snapshot.out ---" + sed -n '1,20p' "$WORK/snapshot.out" + exit 1 +fi # 3. Controlled, reversible update failure. A small package install is a real # update: it changes both the root subvolume and /var/db/pkg, and a snapshot -# can restore it. A root marker proves the root generation too. +# can restore it. A root marker proves the root generation too. The break +# must change the package database; otherwise the restore comparison below +# would pass without proving anything. printf 'broken by rollback acceptance\n' > /etc/phoebus/rollback-broken.txt 2>/dev/null || true -"$SYS" -y install app-editors/nano --legacy --portage > "$WORK/break.out" 2>&1 -echo "===ROLLBACK-BROKEN pkgs=$(pkg_fingerprint)===" +"$SYS" -y install "$BREAK_PKG" --legacy --portage > "$WORK/break.out" 2>&1 +PKGS_BROKEN="$(pkg_fingerprint)" +echo "===ROLLBACK-BROKEN pkgs=$PKGS_BROKEN===" +if [[ "$PKGS_BROKEN" == "$PKGS_BEFORE" ]]; then + echo "--- break.out ---" + sed -n '1,30p' "$WORK/break.out" + echo "===ROLLBACK-BREAK-NOOP the break did not change the package database===" + exit 1 +fi -# 4. Invoke the real sys undo path. -"$SYS" -y undo --legacy > "$WORK/undo.out" 2>&1 +# 4. Invoke the real sys undo path, targeting our own snapshot id so the +# restore point is exact (not "newest", which a protective snapshot taken +# by the break step would win). Restoring our snapshot must remove both +# the installed package and the root marker. +"$SYS" -y undo "$SNAP_ID" --legacy > "$WORK/undo.out" 2>&1 RC=$? -echo "===ROLLBACK-UNDO rc=$RC===" +echo "===ROLLBACK-UNDO rc=$RC id=$SNAP_ID===" "$SYS" rollback status --legacy > "$WORK/status.out" 2>&1 || true sed -n '1,40p' "$WORK/status.out" diff --git a/installer/tests/vm/serial-driver.py b/installer/tests/vm/serial-driver.py index 04b5f55..b8dcda0 100644 --- a/installer/tests/vm/serial-driver.py +++ b/installer/tests/vm/serial-driver.py @@ -269,9 +269,13 @@ def do_rollback(results, paths, disks): log.close() before = _parse_marker(phase1, "ROLLBACK-KNOWN-GOOD") + broken = _parse_marker(phase1, "ROLLBACK-BROKEN") undo = _parse_marker(phase1, "ROLLBACK-UNDO") if before is None: results.fail("rollback-known-good", "the guest did not record the known-good state") + if broken is None or broken.get("pkgs") == (before or {}).get("pkgs"): + results.fail("rollback-break-broke", f"the controlled break did not change the package database: {broken}") + return if undo is None or undo.get("rc") != "0": results.fail("rollback-undo", f"sys undo did not succeed: {undo}") # No rollback happened: a detached "verify" would compare identical @@ -314,6 +318,11 @@ def do_rollback(results, paths, disks): ) results.assert_true("rollback-modules-present", "===AFTER-MODULES ok===" in phase2, "no kernel modules for the running kernel") + # The break wrote this marker after our snapshot; the targeted undo + # restores our snapshot exactly, so the marker must be gone. Its + # survival would prove the wrong generation was restored. + results.assert_true("rollback-root-restored", "===AFTER-BROKEN-MARKER absent===" in phase2, + "the root break marker survived the rollback") results.assert_true( "rollback-recovery-entry", "systemd.unit=rescue.target" in phase2 and "@snapshots/" in phase2, diff --git a/installer/tests/vm/test_vmsafety.py b/installer/tests/vm/test_vmsafety.py index ea3cd4a..287e9c0 100644 --- a/installer/tests/vm/test_vmsafety.py +++ b/installer/tests/vm/test_vmsafety.py @@ -271,6 +271,15 @@ class HarnessScriptsTest(unittest.TestCase): verify = (self.VM_DIR / "rollback-verify.sh").read_text() self.assertIn("/usr/local/bin/sys", verify) self.assertIn("AFTER-SYSTEMD", verify) + # The acceptance must prove a real mutation and a real restore: the + # break package must be absent up front, the break must change the + # package database, and the driver must assert the root marker is gone. + guest = (self.VM_DIR / "rollback-guest.sh").read_text() + self.assertIn("ROLLBACK-BREAK-PRESENT", guest) + self.assertIn("ROLLBACK-BREAK-NOOP", guest) + self.assertIn("--legacy", guest) + self.assertIn("rollback-break-broke", driver) + self.assertIn("rollback-root-restored", driver) if __name__ == "__main__":