diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 1c3be1c..a30e461 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -5,6 +5,31 @@ this file is the curated, human-readable summary. ## Unreleased — Fast defaults with user-controlled source builds +- **VM-verified AI sandbox and unprivileged AI path (2026-09-28).** The first + disposable-VM run of the newly built static `sys` (overlay on the golden + installed image, `serial-boot-20260929T092147Z-6f88907a`) found and fixed two + real defects. (1) **Unprivileged `sys ask` hung on a polkit root-password + prompt:** the model sandbox used the system manager (`systemd-run`), and + `org.freedesktop.systemd1.manage-units` requires authentication, so a normal + user got `==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====` + and blocked. The sandbox now uses `systemd-run --user` for unprivileged + callers, with the subset of hardening properties the user manager accepts + (`DynamicUser`, `PrivateDevices`, `ProtectKernelModules` are system-only); + root still gets the full system sandbox. (2) **Unprivileged read-only AI + plan exited 1 after printing the plan:** the service layer unconditionally + wrote the root-owned `/var/lib/phoebus/transactions.toml`, which a normal + account cannot write. Transaction recording is now best-effort for a + non-root plan and mandatory for a privileged mutation. VM evidence: + `AI_ROOT_RC=0` (system sandbox), `AI_USER_RC=0` (`--user` sandbox, was + RC=124 timeout), `AI_NOSANDBOX_RC=0`, hostile `run_shell` reply `AI_SHELL_RC=1`; + the expanded `sys-acceptance.sh` matrix reports `SUMMARY PASS=49 FAIL=0`. + Also observed: a read-only `flatpak search` can trigger Flatpak's interactive + `appstream-update` polkit prompt when the session is not recognized as active + (headless serial), which can block an unattended run; on a real active seat + polkit's default `allow_active=yes` allows it, and the unattended acceptance + now runs detached so it cannot hang. Recorded as a hypothesis, not a code + change. Gates: `cargo fmt`, `clippy --workspace --all-targets -- -D warnings`, + `cargo test --workspace` 842 passed / 0 failed. - **Live real-agent verification over the local `opencode` server (2026-09-28).** With a real `opencode serve` (v1.18.32) on `127.0.0.1:4096` and the `opencode-go` provider, `sys ask "list installed packages"` was driven through diff --git a/docs/HANDOFF.md b/docs/HANDOFF.md index 3b8cd4e..db5b219 100644 --- a/docs/HANDOFF.md +++ b/docs/HANDOFF.md @@ -25,6 +25,23 @@ commit it. It should be short and current; use `git log` for detail. call (`sys ask` with a real provider is still VM/live NotRun) but it closes the "connected-model path only tested with fake transports" gap. Gates: `cargo test --workspace` 841 passed / 0 failed. +- **First VM test loop on the golden image (2026-09-28, VM-VERIFIED):** booted a + fresh overlay of the installed `phoebus-headless-80g.qcow2` with the static + `sys` from HEAD and found two real defects, both fixed. (1) Unprivileged + `sys ask` hung on a polkit `manage-units` prompt because the model sandbox + used the **system** `systemd-run`; it now uses `systemd-run --user` with the + user-manager property subset for non-root callers, full system sandbox for + root. (2) An unprivileged read-only AI plan exited 1 after printing the plan + because the service layer required the root-owned transaction log; recording + is now best-effort for non-root plans and mandatory for privileged mutations. + VM evidence (`serial-boot-20260929T092147Z-6f88907a`, PASS=8): system sandbox + `AI_ROOT_RC=0`, user sandbox `AI_USER_RC=0` (was 124/timeout), no-sandbox + `AI_NOSANDBOX_RC=0`, hostile reply `AI_SHELL_RC=1`, and + `sys-acceptance.sh` `SUMMARY PASS=49 FAIL=0`. Hypothesis recorded (not + changed): headless/inactive-session `flatpak search` can trigger Flatpak's + interactive `appstream-update` polkit prompt; the unattended acceptance runs + detached to avoid the hang. Next: mutate acceptance, rollback, Snap, source + conversion / tuned kernel on the same golden overlay. - **Boundary-hardening pass (2026-09-28, IMPLEMENTED + UNIT-TESTED, VM NOT RUN):** six host-provable defects fixed with regression tests. (1) Removed the `PHOEBUS_ALLOW_UNPRIVILEGED_EXEC=1` env backdoor: it made diff --git a/docs/STATUS.md b/docs/STATUS.md index a7abd8c..158d7a3 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -100,7 +100,8 @@ Booting that recovery entry offline remains a manual gate. | AI connected-model path over a real local stub | yes | yes (4 e2e cases: valid plan executes; request has no home path/credential; shell + smuggled key + malformed fail closed) | no (local stub, not a live model) | n/a | | Live real-agent call (`opencode` backend, host) | yes | yes (`sys ask "list installed packages"` → valid plan + digest, exit 0; hostile shell/private-file instruction refused and downgraded; OpenCode tools disabled; no credential in the log) | host-level only (2026-09-28, local `opencode serve` + `opencode-go/deepseek-v4.1-flash`); VM/direct-provider live call remains NotRun | n/a | | Privilege derived only from the process identity | yes | yes (an environment variable cannot forge uid 0; the removed `PHOEBUS_ALLOW_UNPRIVILEGED_EXEC` regression test) | no | n/a | -| AI OS sandbox (systemd hardened unit) | yes | yes (argv + sandboxed-send via `systemd-run`, key/body on stdin) | no — **host probe 2026-09-28: system `manage-units` needs polkit auth and the full property set is invalid for `--user`; VM seat run required to settle `systemd-run` vs `--user`** | no | +| AI OS sandbox (systemd hardened unit) | yes | yes (argv + sandboxed-send; system properties for root, user-manager subset for non-root) | yes (`serial-boot-20260929T092147Z-6f88907a`: `AI_ROOT_RC=0` system sandbox, `AI_USER_RC=0` `--user` sandbox; previously the user path hung on a polkit `manage-units` prompt) | no | +| Unprivileged read-only AI plan completes | yes | yes (transaction recording best-effort for non-root; mandatory for privileged mutations) | yes (`serial-boot-20260929T092147Z-6f88907a`: `AI_NOSANDBOX_RC=0` as `phoebus`) | n/a | | Installed assistant (`sys ask`) | yes | yes | yes (live call 2026-09-28: `sys ask "list installed packages"` via `--ai-backend opencode` to host `opencode serve` through a guest loopback tunnel, model `opencode-go/muse-spark-1.3-contributor`, valid Assistant Plan + approval digest, `AI_LIVE_RC=0`; the `show system status` phrasing elicits schema-noncompliant JSON from this model and is fail-closed rejected) | n/a | | Resolved package effects + approval binding (carried through execution) | yes | yes (empty vs unestablished; drift + prior-step allowance) | no | n/a | | Privileged approval ledger (replacement/replay/concurrency/interruption) | yes | yes (privileged entry point) | no | n/a | @@ -385,18 +386,18 @@ Booting that recovery entry offline remains a manual gate. includes bounded seat socket wait. Graphical desktop seat verification under headless wlroots is proven; graphical verification under a physical seat remains an open hardware gate. -- **AI sandbox** is implemented (`systemd-run` hardening properties; argv and - the sandboxed-send path — `systemd-run` program, key/body on stdin — are - unit-tested) and fails closed without `systemd-run`; it has not been - exercised in a VM. A 2026-09-28 host probe found that the system manager's - `org.freedesktop.systemd1.manage-units` policy requires authentication - (`auth_admin_keep`), and that the full property set is invalid for - `systemd-run --user` (`DynamicUser=yes` unsupported, `PrivateDevices=yes` - fails). Whether the sandbox is therefore usable by an unprivileged assistant - user on a seat, or must switch to `--user` with a reduced property set, is a - **hypothesis requiring a disposable-VM seat run**; the sandbox was - deliberately not changed without that proof. Only typed actions and the - privacy boundary were previously enforced. +- **AI sandbox** is implemented (`systemd-run` hardening properties) and is now + exercised in a disposable VM: the **system** sandbox (full property set) for a + root caller and the **user** sandbox (`systemd-run --user`, with + `DynamicUser`/`PrivateDevices`/`ProtectKernelModules` omitted because the user + manager rejects them) for an unprivileged caller. Before the fix, an + unprivileged `sys ask` hung on a polkit `manage-units` password prompt; it now + completes (`serial-boot-20260929T092147Z-6f88907a`, `AI_ROOT_RC=0`, + `AI_USER_RC=0`). Open hypothesis: a read-only `flatpak search` can trigger + Flatpak's interactive `appstream-update` polkit prompt when the session is not + recognized as active (headless serial); polkit's default `allow_active=yes` + covers a real active seat. Not yet exercised: a strict-confinement AppArmor + guest and a physical seat. - **Release automation** now has executable gates, a report, atomic publish and signing command helpers, but no gate has been executed by a configured CI runner. `gates --execute` runs each gate's real command in its own diff --git a/installer-rs/crates/phoebus-executor/src/ai.rs b/installer-rs/crates/phoebus-executor/src/ai.rs index 1d3406b..7731ee2 100644 --- a/installer-rs/crates/phoebus-executor/src/ai.rs +++ b/installer-rs/crates/phoebus-executor/src/ai.rs @@ -311,11 +311,23 @@ mod tests { let specs = runner.recorded(); let spec = &specs[0]; assert_eq!(spec.program, "systemd-run"); + // This test process is unprivileged, so the user manager is used; the + // system-only properties must never appear. assert!( - spec.args.iter().any(|a| a.contains("DynamicUser=yes")), + spec.args.iter().any(|a| a == "--user"), + "an unprivileged caller must use the user manager: {:?}", + spec.args + ); + assert!( + spec.args.iter().any(|a| a.contains("ProtectHome=yes")), "the sandbox properties must be present: {:?}", spec.args ); + assert!( + !spec.args.iter().any(|a| a.contains("DynamicUser=yes")), + "the user manager rejects DynamicUser: {:?}", + spec.args + ); assert!( !spec.args.iter().any(|a| a.contains("sk-secret-value")), "the key must not be in argv: {:?}", diff --git a/installer-rs/crates/phoebus-executor/src/sandbox.rs b/installer-rs/crates/phoebus-executor/src/sandbox.rs index 5bea49c..321f1d5 100644 --- a/installer-rs/crates/phoebus-executor/src/sandbox.rs +++ b/installer-rs/crates/phoebus-executor/src/sandbox.rs @@ -17,7 +17,8 @@ use crate::command::CommandSpec; use crate::error::{ExecError, Result}; use crate::exec::Exec; -/// The hardening properties applied to the model-call unit. +/// The hardening properties applied to the model-call unit by the **system** +/// manager. pub const SANDBOX_PROPERTIES: &[&str] = &[ "DynamicUser=yes", "ProtectSystem=strict", @@ -36,13 +37,44 @@ pub const SANDBOX_PROPERTIES: &[&str] = &[ "SystemCallFilter=@system-service", ]; +/// The subset of [`SANDBOX_PROPERTIES`] accepted by the **user** manager. +/// +/// `DynamicUser`, `PrivateDevices` and `ProtectKernelModules` are +/// system-manager-only; `systemd-run --user` rejects them. The remaining set +/// still gives an unprivileged model call a read-only system, no home access, +/// no new privileges and a restricted syscall/address-family set. +pub const USER_SANDBOX_PROPERTIES: &[&str] = &[ + "ProtectSystem=strict", + "ProtectHome=yes", + "PrivateTmp=yes", + "NoNewPrivileges=yes", + "ProtectKernelTunables=yes", + "ProtectControlGroups=yes", + "RestrictNamespaces=yes", + "LockPersonality=yes", + "MemoryDenyWriteExecute=yes", + "SystemCallArchitectures=native", + "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6", + "SystemCallFilter=@system-service", +]; + /// Whether `systemd-run` is available to build the boundary. pub fn available() -> bool { crate::exec::find_in_path("systemd-run").is_some() } -/// The argv for `systemd-run` that runs `program args` inside the sandbox. -pub fn argv(program: &str, args: &[String]) -> Vec { +/// Whether the model call must go through the **user** manager. +/// +/// A normal account cannot start a system transient unit: polkit's +/// `org.freedesktop.systemd1.manage-units` requires authentication, so +/// `systemd-run` (system) either prompts for the root password or hangs. That +/// breaks the primary assistant interface for exactly the users it is for. +/// An unprivileged caller therefore uses `systemd-run --user`. +fn user_scope() -> bool { + crate::personal::effective_uid() != 0 +} + +fn build_argv(program: &str, args: &[String], user: bool, properties: &[&str]) -> Vec { let mut out: Vec = vec![ "--quiet".into(), "--wait".into(), @@ -50,7 +82,10 @@ pub fn argv(program: &str, args: &[String]) -> Vec { "--collect".into(), "--service-type=exec".into(), ]; - for property in SANDBOX_PROPERTIES { + if user { + out.push("--user".into()); + } + for property in properties { out.push(format!("--property={property}")); } out.push(program.to_string()); @@ -58,6 +93,16 @@ pub fn argv(program: &str, args: &[String]) -> Vec { out } +/// The argv for the system `systemd-run` that runs `program args` sandboxed. +pub fn argv(program: &str, args: &[String]) -> Vec { + build_argv(program, args, false, SANDBOX_PROPERTIES) +} + +/// The argv for the user `systemd-run` used by an unprivileged caller. +pub fn user_argv(program: &str, args: &[String]) -> Vec { + build_argv(program, args, true, USER_SANDBOX_PROPERTIES) +} + /// Run a program inside the model sandbox, reading the hidden `stdin` payload. /// /// Fails closed when `systemd-run` is unavailable: an unisolated model call is @@ -74,9 +119,14 @@ pub fn run( .into(), )); } + let wrapped = if user_scope() { + user_argv(program, args) + } else { + argv(program, args) + }; exec.run( CommandSpec::new("ai", "query model (sandboxed)", "systemd-run") - .args(argv(program, args)) + .args(wrapped) .stdin(stdin), ) } @@ -107,6 +157,38 @@ mod tests { assert_eq!(&args[pos + 1..], &["-sS", "-K", "-"]); } + #[test] + fn user_argv_uses_the_user_manager_and_supported_properties() { + let args = user_argv("curl", &["-sS".into(), "-K".into(), "-".into()]); + assert!(args.iter().any(|a| a == "--user"), "{args:?}"); + for required in [ + "ProtectSystem=strict", + "ProtectHome=yes", + "PrivateTmp=yes", + "NoNewPrivileges=yes", + "RestrictAddressFamilies=", + "SystemCallFilter=@system-service", + ] { + assert!( + args.iter().any(|a| a.contains(required)), + "missing {required}: {args:?}" + ); + } + // Properties the user manager rejects must never be passed. + for forbidden in [ + "DynamicUser=yes", + "PrivateDevices=yes", + "ProtectKernelModules=yes", + ] { + assert!( + !args.iter().any(|a| a.contains(forbidden)), + "user sandbox must not include {forbidden}: {args:?}" + ); + } + let pos = args.iter().position(|a| a == "curl").unwrap(); + assert_eq!(&args[pos + 1..], &["-sS", "-K", "-"]); + } + #[test] fn run_fails_closed_without_systemd_run() { // In this environment systemd-run may or may not exist; if it does not, diff --git a/installer-rs/crates/phoebus-installer/src/service/execution.rs b/installer-rs/crates/phoebus-installer/src/service/execution.rs index 5970c07..ec4d70b 100644 --- a/installer-rs/crates/phoebus-installer/src/service/execution.rs +++ b/installer-rs/crates/phoebus-installer/src/service/execution.rs @@ -263,7 +263,15 @@ fn execute_claimed( digest: Some(plan.digest.clone()), outcomes: Vec::new(), }; - let tx_id = transaction::begin(exec, root, record)?; + // The transaction log lives in the root-owned `/var/lib/phoebus`. A + // privileged mutation must record it. An unprivileged, non-mutating plan (a + // read-only assistant answer) must not fail merely because it cannot write + // that log, so its recording is best-effort. + let tx_id: Option = if plan.requires_root { + Some(transaction::begin(exec, root, record)?) + } else { + transaction::begin(exec, root, record).ok() + }; // Carry the *reviewed* per-step effects forward. Freshly resolved effects // below are evidence to compare against these, never a replacement @@ -475,12 +483,16 @@ fn execute_claimed( fn finish_or_report( exec: &Exec, root: &Path, - tx_id: u64, + tx_id: Option, result: impl Into, snapshot: Option, outcomes: Vec, persistence_note: &str, ) -> Result<()> { + // Recording was skipped (unprivileged plan without a writable log). + let Some(tx_id) = tx_id else { + return Ok(()); + }; let result = result.into(); match transaction::finish(exec, root, tx_id, result.clone(), snapshot, outcomes) { Ok(()) => Ok(()), diff --git a/installer-rs/crates/phoebus-installer/src/service/tests.rs b/installer-rs/crates/phoebus-installer/src/service/tests.rs index 84f8c3b..82a3d63 100644 --- a/installer-rs/crates/phoebus-installer/src/service/tests.rs +++ b/installer-rs/crates/phoebus-installer/src/service/tests.rs @@ -118,6 +118,38 @@ fn sealed( plan } +#[test] +fn an_unprivileged_readonly_plan_does_not_require_the_transaction_log() { + use std::os::unix::fs::PermissionsExt; + let store_dir = tempdir().unwrap(); + let store = PlanStore::with_dir(store_dir.path()); + let root = tempdir().unwrap(); + let exec = make_test_exec(root.path()); + let uid = effective_uid(); + + let mut plan = sealed( + "plan-readonly", + PlanKind::General, + vec![SystemAction::History], + "d", + root.path(), + uid, + ); + store.save(&mut plan).unwrap(); + let digest = plan.digest.clone(); + + // Simulate the installed system: the transaction directory is root-owned, so + // an unprivileged plan cannot write it. Recording is best-effort, so the + // read-only plan must still complete. + let tx_dir = root.path().join("var/lib/phoebus"); + std::fs::create_dir_all(&tx_dir).unwrap(); + std::fs::set_permissions(&tx_dir, std::fs::Permissions::from_mode(0o555)).unwrap(); + + let outcome = execute_plan(&exec, root.path(), &store, "plan-readonly", &digest, true) + .expect("a read-only plan must not fail on the root-owned transaction log"); + assert!(outcome.completed); +} + #[test] fn plan_store_roundtrips_and_claims_once() { let dir = tempdir().unwrap();