From 744059b6a2a716b992fa93a5a344f2765ddb78ef Mon Sep 17 00:00:00 2001 From: Phoebus Date: Fri, 25 Sep 2026 19:49:50 -0700 Subject: [PATCH] fix(vm): provision Gentoo release key; truthful rollback gate Live image ships gnupg plus the Gentoo release key, fingerprint-checked at build time against the published release-key fingerprints, so the fail-closed DIGESTS verification cannot be silently skipped. Rollback harness: authenticate sudo (unique end markers, verified timestamp, abort instead of hanging), run both guest phases as root, use explicit --legacy for snapshot/break/undo, fix the verify SYS path, emit the real systemd state, and preserve the phase-1 transcript. --- installer/live/README.md | 6 +++- installer/live/build.sh | 48 ++++++++++++++++++++++++- installer/live/live-packages.list | 3 ++ installer/tests/vm/README.md | 23 ++++++++---- installer/tests/vm/rollback-guest.sh | 20 ++++++++--- installer/tests/vm/rollback-verify.sh | 21 +++++++++-- installer/tests/vm/serial-driver.py | 50 +++++++++++++++++++++++++-- installer/tests/vm/test_vmsafety.py | 24 +++++++++++++ 8 files changed, 176 insertions(+), 19 deletions(-) diff --git a/installer/live/README.md b/installer/live/README.md index bd6b8ad..8affba8 100644 --- a/installer/live/README.md +++ b/installer/live/README.md @@ -38,7 +38,11 @@ The build runs entirely unprivileged and never touches a host disk: UEFI/BIOS boot path. No `curl | sh`, no live/git packages, and signature verification is never -disabled. Debian packages are used only on the live medium. +disabled. Debian packages are used only on the live medium. The live image +ships `gnupg` plus the trusted Gentoo release key at +`/usr/share/openpgp-keys/gentoo-release.asc` (provisioned by `build.sh` and +checked against Gentoo's published release-key fingerprints), so the +installer's signed-DIGESTS stage3 verification cannot be silently skipped. ## Build diff --git a/installer/live/build.sh b/installer/live/build.sh index 9d1bd0b..6645816 100755 --- a/installer/live/build.sh +++ b/installer/live/build.sh @@ -39,7 +39,7 @@ say() { printf '\n== %s ==\n' "$*"; } # --------------------------------------------------------------------------- say "preflight" -for c in mmdebstrap xorriso mksquashfs curl dpkg-deb unshare sha256sum; do +for c in mmdebstrap xorriso mksquashfs curl dpkg-deb unshare sha256sum gpg; do command -v "$c" >/dev/null || { echo "missing tool: $c" >&2; exit 1; } done if ! command -v magick >/dev/null 2>&1 \ @@ -315,6 +315,52 @@ status=disabled EOF fi +# Provision the trusted Gentoo release key for stage3 DIGESTS verification. +# The installer authenticates the signed DIGESTS with gpg against +# /usr/share/openpgp-keys/gentoo-release.asc and refuses to install without +# it, so the live image must ship that key. The key bundle is fetched once +# here at build time (never unauthenticated at install time) and accepted +# only when its fingerprint matches Gentoo's published release-key +# information (https://www.gentoo.org/downloads/signatures/). +gentoo_keydir="$rootfs/usr/share/openpgp-keys" +gentoo_key="$gentoo_keydir/gentoo-release.asc" +weekly_fp="13EBBDBEDE7A12775DFDB1BABB572E0E2D182910" +signing_fp="D99EAC7379A850BCE47DA5F29E6438C817072058" +keyring_tmp="$(mktemp -d)" +curl -fsSL --retry 3 -o "$keyring_tmp/service-keys.gpg" \ + "https://qa-reports.gentoo.org/output/service-keys.gpg" +GNUPGHOME="$keyring_tmp" gpg --batch --quiet \ + --import "$keyring_tmp/service-keys.gpg" +if ! GNUPGHOME="$keyring_tmp" gpg --batch --with-colons \ + --list-keys "$weekly_fp" 2>/dev/null | grep -q "$weekly_fp"; then + echo "Gentoo weekly release key $weekly_fp missing from the fetched bundle;" >&2 + echo "refusing to ship an untrusted stage3 verification key" >&2 + rm -rf "$keyring_tmp" + exit 1 +fi +install -d -m 0755 "$gentoo_keydir" +if ! GNUPGHOME="$keyring_tmp" gpg --batch --quiet --armor \ + --export "$weekly_fp" "$signing_fp" > "$gentoo_key" 2>/dev/null; then + GNUPGHOME="$keyring_tmp" gpg --batch --quiet --armor \ + --export "$weekly_fp" > "$gentoo_key" +fi +[[ -s "$gentoo_key" ]] || { + echo "failed to provision the Gentoo release key" >&2 + rm -rf "$keyring_tmp" + exit 1 +} +chmod 0644 "$gentoo_key" +verify_tmp="$(mktemp -d)" +GNUPGHOME="$verify_tmp" gpg --batch --quiet --import "$gentoo_key" +if ! GNUPGHOME="$verify_tmp" gpg --batch --with-colons \ + --list-keys "$weekly_fp" 2>/dev/null | grep -q "$weekly_fp"; then + echo "provisioned Gentoo release key failed its fingerprint check" >&2 + rm -rf "$keyring_tmp" "$verify_tmp" + exit 1 +fi +rm -rf "$keyring_tmp" "$verify_tmp" +echo "Gentoo release key provisioned and fingerprint-checked: $gentoo_key" + # Branding: shared vector assets, the app icon and the live-only marker. install_asset brand-emblem /usr/share/phoebus/branding/emblem.svg install_asset brand-wordmark /usr/share/phoebus/branding/wordmark.svg diff --git a/installer/live/live-packages.list b/installer/live/live-packages.list index 79ecaea..98ad319 100644 --- a/installer/live/live-packages.list +++ b/installer/live/live-packages.list @@ -32,6 +32,9 @@ dosfstools btrfs-progs util-linux e2fsprogs +# gpg authenticates the Gentoo stage3 DIGESTS signature at install time. +# Without it the installer refuses to install (fail closed). +gnupg # --- GPU detection and the NVIDIA accelerated driver -------------------------- # The live medium bundles NVIDIA's accelerated stack so a supported GPU boots diff --git a/installer/tests/vm/README.md b/installer/tests/vm/README.md index 3e6be7c..596ac6f 100644 --- a/installer/tests/vm/README.md +++ b/installer/tests/vm/README.md @@ -72,13 +72,22 @@ embed a console via `KERNEL_CONSOLE` (`GENTOO_KERNEL_CONSOLE` equivalent). **already-installed** disposable disk: 1. boot the installed disk, unlock LUKS and log in; -2. fetch `sys` and `rollback-guest.sh` from the host (the target does not yet - install `sys`; see `docs/HANDOFF.md`), record the `/var/db/pkg` fingerprint - and a `/home` marker, create a recovery point with `sys snapshot`, install a - small package (a controlled, reversible update) and run `sys undo`; -3. power off, boot the disk detached, and run `rollback-verify.sh`, comparing - the package fingerprint and the `/home` marker, checking kernel modules, and - reading the snapshot recovery entry and `sys rollback status`. +2. fetch `sys` and `rollback-guest.sh` from the host (the installed system + carries `/usr/local/bin/sys`; the host copy is a fallback), authenticate + `sudo` (the password is sent only at sudo's own no-echo prompt, never on a + command line), record the `/var/db/pkg` fingerprint and a `/home` marker, + create a recovery point with `sys snapshot`, install a small package (a + controlled, reversible update) and run `sys undo` — all as root, because + unprivileged runs would be refused and prove nothing; +3. power off, boot the disk detached, and run `rollback-verify.sh` (also as + root), comparing the package fingerprint and the `/home` marker, checking + kernel modules, systemd state, and reading the snapshot recovery entry and + `sys rollback status`. + +Both transcripts are kept (`serial-phase1.log`, `serial.log`). The guest +scripts refuse to run unprivileged (`ROLLBACK-NEEDS-ROOT` / +`AFTER-NEEDS-ROOT`) so a lost `sudo` timestamp fails loudly instead of +recording vacuous passes. Serve the fixtures as well as the installer: diff --git a/installer/tests/vm/rollback-guest.sh b/installer/tests/vm/rollback-guest.sh index d03581b..42bf7d7 100755 --- a/installer/tests/vm/rollback-guest.sh +++ b/installer/tests/vm/rollback-guest.sh @@ -12,6 +12,14 @@ # real installed command path, not a mock. set -uo pipefail +# The snapshot/break/undo steps mutate the system and must run as root. The +# driver authenticates sudo before invoking this script; fail fast rather +# than recording vacuous "success" markers as an unprivileged user. +if [ "$(id -u)" != "0" ]; then + echo "===ROLLBACK-NEEDS-ROOT id=$(id -u)===" + exit 1 +fi + SERVE="${SERVE_URL:-http://10.0.2.2:8000}" if [[ -n "${SYS_BIN:-}" ]]; then SYS="$SYS_BIN" @@ -38,8 +46,10 @@ printf '%s\n' "$HOME_MARKER" > "/home/$USER_NAME/rollback-marker.txt" PKGS_BEFORE="$(pkg_fingerprint)" echo "===ROLLBACK-KNOWN-GOOD pkgs=$PKGS_BEFORE kernel=$KERNEL home=$HOME_MARKER===" -# 2. Create the recovery point through the supported interface. -"$SYS" -y snapshot "rollback acceptance" > "$WORK/snapshot.out" 2>&1 +# 2. Create the recovery point through the supported interface. Explicit +# `--legacy` selects deterministic handling (no model); without it the +# request would enter the AI workflow and stop with no provider. +"$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===" @@ -47,14 +57,14 @@ echo "===ROLLBACK-SNAPSHOT id=$SNAP_ID===" # 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. printf 'broken by rollback acceptance\n' > /etc/phoebus/rollback-broken.txt 2>/dev/null || true -"$SYS" -y install app-editors/nano > "$WORK/break.out" 2>&1 +"$SYS" -y install app-editors/nano --legacy --portage > "$WORK/break.out" 2>&1 echo "===ROLLBACK-BROKEN pkgs=$(pkg_fingerprint)===" # 4. Invoke the real sys undo path. -"$SYS" -y undo > "$WORK/undo.out" 2>&1 +"$SYS" -y undo --legacy > "$WORK/undo.out" 2>&1 RC=$? echo "===ROLLBACK-UNDO rc=$RC===" -"$SYS" rollback status > "$WORK/status.out" 2>&1 || true +"$SYS" rollback status --legacy > "$WORK/status.out" 2>&1 || true sed -n '1,40p' "$WORK/status.out" echo "===ROLLBACK-DONE===" diff --git a/installer/tests/vm/rollback-verify.sh b/installer/tests/vm/rollback-verify.sh index 611cd23..c7d791e 100755 --- a/installer/tests/vm/rollback-verify.sh +++ b/installer/tests/vm/rollback-verify.sh @@ -8,7 +8,23 @@ # alone would not prove package-state consistency. set -uo pipefail -SYS="${SYS_BIN:-/tmp/sys}" +# Verification reads the system state (subvolumes, ESP entries) and must run +# as root; the driver authenticates sudo before invoking this script. +if [ "$(id -u)" != "0" ]; then + echo "===AFTER-NEEDS-ROOT id=$(id -u)===" + echo "===AFTER-DONE===" + exit 1 +fi + +# `sys` is installed into the target by the installer; fall back to the host +# copy only if it is somehow absent. +if [[ -n "${SYS_BIN:-}" ]]; then + SYS="$SYS_BIN" +elif [[ -x /usr/local/bin/sys ]]; then + SYS=/usr/local/bin/sys +else + SYS=/tmp/sys +fi USER_NAME="${USERNAME:-phoebus}" pkg_fingerprint() { @@ -18,6 +34,7 @@ pkg_fingerprint() { HOME_MARKER="$(cat "/home/$USER_NAME/rollback-marker.txt" 2>/dev/null || echo missing)" echo "===AFTER-KNOWN-GOOD pkgs=$(pkg_fingerprint) kernel=$(uname -r) home=$HOME_MARKER===" +echo "===AFTER-SYSTEMD $(systemctl is-system-running 2>&1)===" if [ -e /etc/phoebus/rollback-broken.txt ]; then echo "===AFTER-BROKEN-MARKER present===" else @@ -48,6 +65,6 @@ cat /efi/loader/entries/phoebus-recovery.conf \ /boot/efi/loader/entries/phoebus-recovery.conf 2>/dev/null || true echo "===AFTER-STATUS===" -"$SYS" rollback status 2>&1 || true +"$SYS" rollback status --legacy 2>&1 || true echo "===AFTER-DONE===" diff --git a/installer/tests/vm/serial-driver.py b/installer/tests/vm/serial-driver.py index 5a78917..04b5f55 100644 --- a/installer/tests/vm/serial-driver.py +++ b/installer/tests/vm/serial-driver.py @@ -178,6 +178,39 @@ def _unlock_and_login(child, results, tag): results.assert_true(f"{tag}-login", logged_in, f"{tag}: could not log in") +def _sudo_auth(child, results, tag): + """Authenticate sudo for the rollback acceptance guest scripts. + + The snapshot/break/undo/verify steps mutate the system and must run as + root. The password is sent only at sudo's own prompt (terminal echo + disabled, like the login password), so it never lands in the serial + transcript the way a command-line credential would. Returns True when the + timestamp stuck; the caller must abort the phase otherwise, or every + later `sudo` would block on a prompt nobody answers. + """ + try: + # Unique markers, never a bare `[$#] ` alternative: the buffer still + # holds the previous shell prompt when this runs, and a generic + # prompt pattern would match that stale text immediately — skipping + # the password while sudo is still starting (the next typed line + # would then be consumed as the password and rejected). + child.sendline("sudo -v; echo SUDO-V-$(printf DONE)") + idx = child.expect([r"[Pp]assword(?: for [^:]+)?:", "SUDO-V-DONE"], timeout=60) + if idx == 0: + child.sendline(USER_PW) + child.expect("SUDO-V-DONE", timeout=60) + # Prove the timestamp stuck: without it every later `sudo` would block + # on a password prompt nobody answers, hanging the run until timeout. + child.sendline("sudo -n true && echo SUDO-OK-$(printf READY)") + idx = child.expect(["SUDO-OK-READY", r"[Pp]assword"], timeout=30) + except (pexpect.TIMEOUT, pexpect.EOF): + idx = -1 + if idx != 0: + results.fail(f"{tag}-sudo", "sudo authentication did not stick") + return False + return True + + def _fetch_sys(child): # The installed system should already carry /usr/local/bin/sys; only fall # back to fetching the host build when it is absent. @@ -214,7 +247,9 @@ def do_rollback(results, paths, disks): try: _unlock_and_login(child, results, "rollback-before") _fetch_sys(child) - child.sendline(f"curl -fsSL {ROLLBACK_URL} -o /tmp/rb.sh && bash /tmp/rb.sh") + if not _sudo_auth(child, results, "rollback-before"): + return + child.sendline(f"curl -fsSL {ROLLBACK_URL} -o /tmp/rb.sh && sudo bash /tmp/rb.sh") child.expect( ["===ROLLBACK-DONE===", "curl:", pexpect.TIMEOUT, pexpect.EOF], timeout=int(os.environ.get("ROLLBACK_TIMEOUT", "7200")), @@ -239,8 +274,15 @@ def do_rollback(results, paths, disks): results.fail("rollback-known-good", "the guest did not record the known-good state") 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 + # states and report vacuous passes. Stop instead of pretending. + return # Phase 2: boot the disk detached and verify the restored generation. + # The phase-1 transcript is preserved under its own name first: reopening + # the same log would destroy the evidence for why phase 1 passed/failed. + phase1_log = paths.serial_log.with_name("serial-phase1.log") + paths.serial_log.rename(phase1_log) env, args = _installed_args(paths, disks) log = open(paths.serial_log, "w") child = spawn(args, env, log) @@ -248,11 +290,13 @@ def do_rollback(results, paths, disks): try: _unlock_and_login(child, results, "rollback-after") _fetch_sys(child) - child.sendline(f"curl -fsSL {ROLLBACK_VERIFY_URL} -o /tmp/rbv.sh && bash /tmp/rbv.sh") + if not _sudo_auth(child, results, "rollback-after"): + return + child.sendline(f"curl -fsSL {ROLLBACK_VERIFY_URL} -o /tmp/rbv.sh && sudo bash /tmp/rbv.sh") child.expect("===AFTER-DONE===", timeout=int(os.environ.get("ROLLBACK_VERIFY_TIMEOUT", "900"))) phase2 = paths.serial_log.read_text(errors="replace") - results.assert_true("rollback-systemd-running", "running" in phase2, + results.assert_true("rollback-systemd-running", "===AFTER-SYSTEMD running===" in phase2, "systemd did not report running after rollback") after = _parse_marker(phase2, "AFTER-KNOWN-GOOD") if before is None or after is None: diff --git a/installer/tests/vm/test_vmsafety.py b/installer/tests/vm/test_vmsafety.py index a9c719f..ea3cd4a 100644 --- a/installer/tests/vm/test_vmsafety.py +++ b/installer/tests/vm/test_vmsafety.py @@ -247,6 +247,30 @@ class HarnessScriptsTest(unittest.TestCase): driver = (self.VM_DIR / "serial-driver.py").read_text() self.assertIn('"rollback"', driver) self.assertIn("do_rollback", driver) + # The acceptance mutates the system: the driver must authenticate sudo + # (password only at sudo's own prompt) and run both guest phases as + # root, or every privileged step would fail as the login user. + self.assertIn("_sudo_auth", driver) + self.assertIn("sudo bash /tmp/rb.sh", driver) + self.assertIn("sudo bash /tmp/rbv.sh", driver) + # The sudo handshake must use unique end markers: a bare `[$#] ` + # alternative would match the stale prompt left by the previous + # command and skip the password while sudo is still starting. + auth = driver.split("def _sudo_auth", 1)[1].split("\ndef ", 1)[0] + self.assertIn("SUDO-V-DONE", auth) + self.assertNotIn('r"[$#] "', auth) + # The timestamp is verified before use, and a failed phase 1 aborts + # instead of reporting vacuous phase-2 passes. + self.assertIn("SUDO-OK-READY", driver) + self.assertIn("vacuous", driver) + # Phase 1 evidence must survive phase 2. + self.assertIn("serial-phase1.log", driver) + for name in ("rollback-guest.sh", "rollback-verify.sh"): + text = (self.VM_DIR / name).read_text() + self.assertIn('id -u', text, f"{name} must refuse to run unprivileged") + verify = (self.VM_DIR / "rollback-verify.sh").read_text() + self.assertIn("/usr/local/bin/sys", verify) + self.assertIn("AFTER-SYSTEMD", verify) if __name__ == "__main__":