fix(security): harden agent execution and approval boundaries
Some checks are pending
ci / test (push) Waiting to run

This commit is contained in:
Phoebus 2026-10-12 04:27:15 -07:00
parent fc35c5a368
commit 78b896988c
44 changed files with 2757 additions and 549 deletions

104
docs/AGENTIC-HARDENING.md Normal file
View file

@ -0,0 +1,104 @@
# Agentic boundary hardening — 2026-10-12
The implementation review identified seven concrete gaps. All seven have code
changes and regression coverage. Native package and sandbox acceptance passed on an installed-system overlay;
a fresh installer run remains pending. This is not a new ISO release.
| Gap | Implemented change | Evidence |
| --- | --- | --- |
| Raw OpenCode provider credentials entered logs before catalog parsing | Sensitive output suppresses both streaming sinks, logged bodies and attached command failure bodies; curl ignores user configuration | Unknown synthetic provider credential tested on success and failure; real HTTP fixture suite |
| Investigation evidence could carry registered secrets and service journals/argv | Shared evidence boundary redacts, removes private-path lines and bounds UTF-8 output; service evidence uses only enumerated state properties | Registered service token absent from the second scripted model request |
| Approval digest omitted execution context | Versioned envelope binds id, kind, creation/expiry, root, uid, privilege, source, application, full candidate and material effects; lifetime limited to 900 seconds | Context substitutions and expiry extensions refused after resealing, including recomputed digests against retained approvals |
| Developer jail exposed home and could unmask credentials | Synthetic home, clean environment, private PID/IPC/UTS namespaces, no capabilities, only a selected writable workspace; mask overlaps refused; workspace bound by an open descriptor | Real Bubblewrap denied synthetic private files, environment, host processes and offline network; a post-review workspace symlink swap still bound the original workspace |
| Target writes checked paths and later reopened them | Root and parent descriptors, no-follow component traversal, atomic temporary-file replacement and descriptor chmod; root symlinks refused, hardlinks cannot receive writes | Root replacement, hardlink and concurrent atomic directory/symlink exchange tests |
| Installation verification only proved presence | Portage exact atom/version with explicit source/binary mode; Flatpak ref/commit, Snap revision/version/channel, Nix active-profile store path, AppImage bytes/version checks | Wrong Portage identity/version and wrong Flatpak commit refused; mocked install success/failure transactions remain truthful |
| PID-based per-action locking raced and allowed plan interleaving | Persistent inode plus kernel flock; transaction-wide reentrant ownership through revalidation, execution, verification and outcome recording | Competing acquisition, nested guard lifetime, other-thread exclusion, killed-process recovery, and a worker lease that survives broker death |
## Additional enforced limits
The process runner drains stdout/stderr concurrently with nonblocking stdin,
limits capture per stream (16 MiB by default), bounds log emission, and applies
wall-clock deadlines. Streaming builds retain a bounded tail. Oversized query
responses fail. Provider limits/deadlines also apply to sandboxed calls, and the
systemd manager receives RuntimeMaxSec so the actual service worker is bounded.
Model units have MemoryMax=256M and TasksMax=64. Commands issued under a mutation
hold inherit a duplicate lock descriptor; broker death cannot release exclusion
while an inheriting worker still runs. A real-process crash regression proves
that ownership passes to the worker and ends when it exits. Long build commands
retain a 24-hour default; common catalogue, candidate and verification reads use 120
seconds. Service evidence uses 30 seconds and 64 KiB.
Actual negative testing exposed a further gap: the original systemd user unit
could read a synthetic file beneath home. The unprivileged model path now uses
explicit Bubblewrap filesystem/PID isolation inside the transient user unit.
Systemd retains process/syscall/address-family restrictions. Its user-manager
filesystem properties were removed because they implied a namespace that
prevented Bubblewrap creating its jail. Root model calls retain the hardened
DynamicUser system-unit path. Missing tools or namespace failures stop the
request; there is no unconfined fallback. Effective uid is read from the kernel,
and developer home comes from the account database, rather than PATH or HOME.
## Artifact semantics
Flatpak does not support an install --commit option. The reviewed flow downloads
without deployment, uses OSTree's native REF@COMMIT pull with the configured
remote's signature policy, then deploys with Flatpak --no-pull and checks the
active commit. Phoebus now installs dev-util/ostree with its Flatpak lane.
Snap installs with --revision. Nix evaluates from an immutable path flake and
verifies its active store path. These pins bind the selected application;
backend-managed runtime/dependency closures are not claimed to be completely
content-pinned. Portage exact-version selection is not an immutable repository
snapshot. Portage, Flatpak and Nix were exercised in a disposable installed-system VM;
native Snap revision acceptance remains pending.
Primary command references:
[Flatpak](https://docs.flatpak.org/en/latest/flatpak-command-reference.html),
[OSTree pull](https://ostreedev.github.io/ostree/man/ostree-pull.html),
[Snap revisions](https://snapcraft.io/docs/revisions),
[Nix profile install](https://nix.dev/manual/nix/2.18/command-ref/new-cli/nix3-profile-install.html),
[Portage emerge](https://dev.gentoo.org/~zmedico/portage/doc/man/emerge.1.html).
## Verification and remaining acceptance
Real host denial tests are intentionally ignored by the portable suite and run
explicitly with:
```sh
cd installer-rs
cargo test -p phoebus-executor --test isolation_acceptance -- --ignored --skip model_system_unit
# The system-manager probe is run separately as root only in the disposable VM.
```
Both user probes passed on the development host. The system-manager probe
also passed as root in the disposable Gentoo VM. They use only synthetic fixture files,
never account credentials. Their fixtures are deleted at test completion.
Portable gates pass: 981 Rust tests, 22 VM-harness safety tests, strict Clippy,
fmt, repository checks and staged secret scan. Dynamic and static release
builds passed. No physical disk was touched.
The native run `serial-boot-20261012T105530Z-9c09094e` passed all 14 driver cases:
detached generic boot/login, running systemd, developer/user/root isolation,
deterministic history, descriptor-backed Btrfs snapshot, exact Portage install
and removal, persistent lock permissions, Flatpak commit install and removal,
Nix active store-path install, and clean poweroff. It used a separate qcow2
backing overlay; the source-build base image and local ISO are unchanged.
Two preceding attempts exposed fixture errors (password mapping and the KVM
virtualization identifier); neither is claimed as a product acceptance pass.
Initially doctor found 6 GiB free. Compiler-cache cleanup restored sufficient
space and host doctor passed with a tight-space warning. A fresh installation
was not repeated. Native pins/isolation/snapshot behavior were exercised on the
installed Gentoo overlay. The subsequent lock-lease addition has its own real
crash-process regression. Final static binary smoke
`serial-boot-20261012T112454Z-47d3cfae` passed 8/8 with clean poweroff,
repeating sandbox denials and executing history and a Btrfs snapshot in an
explicitly network-free namespace. Its SHA-256 was
`1ea91f6f0ac82d88e9a8c400f510819aefaf73251778b248ef2443fdf18e2b62`.
Remaining acceptance: a fresh encrypted installation with descriptor I/O; native
Snap revision installation and adversarial metadata drift across the lanes;
interrupted native package-worker recovery and broader network-free repair/undo
acceptance beyond the verified history/snapshot commands. Older staged approvals
must be reviewed again because the envelope digest has changed. Developer
tools/auth state outside the selected workspace are intentionally hidden; narrowly scoped explicit grants are future
work, rather than silently exposing the whole home directory.

View file

@ -133,8 +133,12 @@ The only crate that runs privileged commands.
any logging. They never enter the config. any logging. They never enter the config.
- The AI cannot execute; it can only propose typed changes that the backend - The AI cannot execute; it can only propose typed changes that the backend
validates and the user approves. The assistant's model call additionally runs validates and the user approves. The assistant's model call additionally runs
inside an unprivileged, hardened `systemd-run` unit (`sandbox`), so the inside a hardened `systemd-run` unit (`sandbox`); normal-user calls also
boundary is enforced by the OS and not only by the schema. require an explicit Bubblewrap filesystem/PID jail. Both environment and
output are constrained. See ADR 0015 and `AGENTIC-HARDENING.md`.
- Approval binds a versioned execution envelope (id, kind, lifetime, root,
uid, privilege, source, application and full candidate) and material effects.
A persistent kernel lock covers the whole mutation transaction.
- Approval is bound to the reviewed resolved plan: the reviewed per-step effects - Approval is bound to the reviewed resolved plan: the reviewed per-step effects
are carried into execution, package effects are resolved again only as are carried into execution, package effects are resolved again only as
evidence to compare against them, and a material change aborts. Privileged evidence to compare against them, and a material change aborts. Privileged

View file

@ -5,6 +5,26 @@ this file is the curated, human-readable summary.
## Unreleased — Fast defaults with user-controlled source builds ## Unreleased — Fast defaults with user-controlled source builds
- **Agentic boundary hardening (2026-10-12).** Addressed seven implementation
review findings: suppress credential-bearing transport logs/errors; minimize
model evidence; bind approvals to their execution context; restrict developer
workspace/home/environment/process visibility; anchor target I/O to descriptors;
pin and verify installed application identities; and serialize complete mutation
plans using a persistent kernel lock. Added process output/deadline bounds and
kernel uid lookup. Real host denial tests found and fixed ineffective user-unit
home protection by adding mandatory Bubblewrap isolation. Both host denial
probes pass. Gentoo overlay acceptance passed 14/14, including all three
denied-operation sandboxes, descriptor snapshot, Portage exact version,
Flatpak commit and Nix store-path installation. Native Snap/fresh installer
acceptance remains pending. Added an inherited command lock lease so a
surviving worker keeps exclusion after broker death; its real-process
regression passes. Final static VM smoke passed 8/8, including network-free
history/snapshot and repeated sandbox denials. All 981 Rust tests, 22 VM
harness tests, strict Clippy, fmt and dynamic/static builds pass. Host space
was restored through compiler-cache cleanup. See `AGENTIC-HARDENING.md`
and ADR 0015. Existing ISO and source-build acceptance were not replaced.
- **Mozilla source-codec build fix (2026-10-08).** - **Mozilla source-codec build fix (2026-10-08).**
Firefox ESR 153.4.0 failed after 7852 seconds: libgkcodecs exported missing Firefox ESR 153.4.0 failed after 7852 seconds: libgkcodecs exported missing
system-libaom symbols (Mozilla bug 2063808, fixed upstream in branch 156). system-libaom symbols (Mozilla bug 2063808, fixed upstream in branch 156).

View file

@ -3,6 +3,23 @@
Rolling note between work sessions. **Update this file before you finish**, then Rolling note between work sessions. **Update this file before you finish**, then
commit it. It should be short and current; use `git log` for detail. commit it. It should be short and current; use `git log` for detail.
- **Agentic hardening (2026-10-12):** All seven findings have fixes and
regressions; see `AGENTIC-HARDENING.md` / ADR 0015. Native Gentoo run
`serial-boot-20261012T105530Z-9c09094e` passed 14/14 with clean poweroff:
developer/user/root isolation, descriptor snapshot, Portage exact install/
removal, Flatpak commit install/removal, Nix store-path verification and
persistent kernel lock. Two earlier runs had fixture errors and are not
product acceptance passes. A later command lock lease closes broker-death
exclusion loss; its real-process regression passes. Final static smoke
`serial-boot-20261012T112454Z-47d3cfae` passed 8/8, including
network-free history/snapshot and repeated sandbox denials. Gates passed:
981 Rust tests, 22 harness safety tests, strict Clippy, fmt, secret scan, and
dynamic/static release builds. QEMU base image and ISO unchanged.
Initial space failure was resolved by generated compiler-cache cleanup.
Next acceptance: fresh installation, native Snap revision/drift, interrupted
native worker and broader offline repair/undo. Old staged approval digests require
fresh review. Preserve the sole source-build evidence image below.
- **Post-install acceptance in progress (2026-10-09 16:01 UTC):** Finish all - **Post-install acceptance in progress (2026-10-09 16:01 UTC):** Finish all
five real application conversions/native windows and both source-kernel five real application conversions/native windows and both source-kernel
compile/install/boot/fallback tests, then deliver the updated local ISO, compile/install/boot/fallback tests, then deliver the updated local ISO,

View file

@ -8,6 +8,30 @@ Truthful implementation/verification matrix. Vocabulary:
- **Hardware verified** — exercised on real hardware. - **Hardware verified** — exercised on real hardware.
- **Not verified** — do not claim it works. - **Not verified** — do not claim it works.
## Agentic hardening — 2026-10-12
- **Implemented and regression tested:** all seven findings and process bounds
described in [AGENTIC-HARDENING.md](AGENTIC-HARDENING.md), including approval
context, immutable application pins, privacy, descriptor I/O and whole-plan
kernel locking. Portage source/binary selections are enforced during execution.
- **Host verified:** real developer/user-model negative probes pass, including
a replaced workspace pathname. Earlier model reachability smoke tests did
not prove denied filesystem operations.
- **VM verified:** `serial-boot-20261012T105530Z-9c09094e`, PASS=14: generic boot,
three sandbox denial probes (developer, user model, DynamicUser system model),
deterministic history, descriptor snapshot, exact Portage install/removal,
persistent lock permissions, Flatpak commit install/removal, active Nix
store-path install, clean poweroff. The base image and ISO are unchanged.
- **Final static VM smoke:** `serial-boot-20261012T112454Z-47d3cfae`, PASS=8,
repeated sandbox denials and history/snapshot with networking explicitly
disabled, followed by clean poweroff. Final gates: 981 Rust tests, 22 VM
harness tests, strict Clippy, fmt, secret scan and dynamic/static release builds.
- **Remaining acceptance:** fresh installer run, native Snap revision/drift,
interrupted native worker recovery and broader offline repair/undo.
Backend dependency closures are not fully content-pinned. The later command
lock lease is covered by a real broker-kill/worker-lifetime regression.
Initial disk-space failure was resolved by generated compiler-cache cleanup.
## Post-install source choices — acceptance in progress (2026-10-09) ## Post-install source choices — acceptance in progress (2026-10-09)
- **VM verified:** fresh encrypted installation and generic kernel boots; - **VM verified:** fresh encrypted installation and generic kernel boots;
@ -499,7 +523,7 @@ A fresh install with these defaults is not yet verified.
| OpenCode transport parity (sandbox + network endpoint) | yes | yes (`CurlHttpClient::sandboxed` inlines the body under `PrivateTmp`; `--opencode-insecure-http` opt-in for remote plain http; endpoint-policy test) | yes (`serial-boot-20260929T114319Z-c43c2a92`: real host agent, OpenCode sandboxed by default, driver PASS=8) | n/a | | OpenCode transport parity (sandbox + network endpoint) | yes | yes (`CurlHttpClient::sandboxed` inlines the body under `PrivateTmp`; `--opencode-insecure-http` opt-in for remote plain http; endpoint-policy test) | yes (`serial-boot-20260929T114319Z-c43c2a92`: real host agent, OpenCode sandboxed by default, driver PASS=8) | n/a |
| Read-only host-root probes never block on a prompt | yes | yes (`CommandSpec::detached` runs host-root Flatpak in a new session via `setsid --wait`, no controlling terminal; field-preserving unit test) | yes (VM: real-agent `sys ask "search for firefox"` completed with zero auth prompts, `serial-boot-20260929T111941Z-2be30b89`) | n/a | | Read-only host-root probes never block on a prompt | yes | yes (`CommandSpec::detached` runs host-root Flatpak in a new session via `setsid --wait`, no controlling terminal; field-preserving unit test) | yes (VM: real-agent `sys ask "search for firefox"` completed with zero auth prompts, `serial-boot-20260929T111941Z-2be30b89`) | 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 | | 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; 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 | | AI OS sandbox (systemd + explicit user Bubblewrap jail) | yes | yes (argv, bounded sensitive transport, real host negative probes) | previous systemd reachability smoke only; changed isolation pending VM acceptance (`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 | | 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 | | 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 | | Resolved package effects + approval binding (carried through execution) | yes | yes (empty vs unestablished; drift + prior-step allowance) | no | n/a |
@ -798,7 +822,10 @@ A fresh install with these defaults is not yet verified.
includes bounded seat socket wait. Graphical desktop seat verification includes bounded seat socket wait. Graphical desktop seat verification
under headless wlroots is proven; graphical verification under a physical seat under headless wlroots is proven; graphical verification under a physical seat
remains an open hardware gate. remains an open hardware gate.
- **AI sandbox** is implemented (`systemd-run` hardening properties) and is now - **AI sandbox:** the 2026-10-12 user path now requires explicit Bubblewrap
containment plus systemd process restrictions. Real host denial probes pass;
the changed path requires Gentoo VM acceptance. Previous reachability evidence
below does not establish negative filesystem isolation. The earlier path was
exercised in a disposable VM: the **system** sandbox (full property set) for a exercised in a disposable VM: the **system** sandbox (full property set) for a
root caller and the **user** sandbox (`systemd-run --user`, with root caller and the **user** sandbox (`systemd-run --user`, with
`DynamicUser`/`PrivateDevices`/`ProtectKernelModules` omitted because the user `DynamicUser`/`PrivateDevices`/`ProtectKernelModules` omitted because the user

View file

@ -580,7 +580,9 @@ inside the privileged broker (`sys --broker --execute-plan <id> --plan-uid <uid>
--approval-digest <sha256> --root <root>`). The broker: --approval-digest <sha256> --root <root>`). The broker:
- **independently recomputes the canonical approval digest** from the exact - **independently recomputes the canonical approval digest** from the exact
executable actions, candidate and reviewed material effects, and requires it executable actions, full candidate and reviewed material effects, plus the
versioned execution envelope (id, kind, creation/expiry, root, initiating uid,
privilege, source and application), and requires it
to equal both the digest stored in the plan and the independently supplied to equal both the digest stored in the plan and the independently supplied
`--approval-digest`. Retaining an approved digest while swapping an action and `--approval-digest`. Retaining an approved digest while swapping an action and
resealing is therefore rejected for every plan kind (Install, LegacyInstall, resealing is therefore rejected for every plan kind (Install, LegacyInstall,
@ -589,7 +591,10 @@ inside the privileged broker (`sys --broker --execute-plan <id> --plan-uid <uid>
independently of the plan file, so a replacement plan under the same independently of the plan file, so a replacement plan under the same
identifier is rejected even when it is resealed with a valid, self-consistent identifier is rejected even when it is resealed with a valid, self-consistent
checksum; checksum;
- rejects expired, modified, malformed, wrong-root or wrong-uid plans; - rejects expired, modified, malformed, wrong-root or wrong-uid plans and
approval lifetimes longer than 900 seconds; old staged plans require review;
- holds a persistent kernel flock across authoritative revalidation, all
mutations, verification and terminal recording;
- derives the initiating identity from the authenticated caller context - derives the initiating identity from the authenticated caller context
(`PKEXEC_UID`), treating `--plan-uid` as untrusted input; (`PKEXEC_UID`), treating `--plan-uid` as untrusted input;
- revalidates the current package/removal state after authentication, and again - revalidates the current package/removal state after authentication, and again

View file

@ -3,6 +3,28 @@
This records the first successful end-to-end install and boot of Phoebus OS This records the first successful end-to-end install and boot of Phoebus OS
against a disposable qcow2 disk. No physical disk was ever touched. against a disposable qcow2 disk. No physical disk was ever touched.
## 2026-10-12 Agentic hardening — native overlay acceptance
Run `serial-boot-20261012T105530Z-9c09094e` passed 14/14 checks with clean
poweroff. A separate backing overlay booted the preserved installed Gentoo
image; neither the base image nor the existing ISO was changed. Verified real
developer/user/root sandbox denials, deterministic history, descriptor-backed
Btrfs snapshot, exact Portage install/removal, Flatpak commit install/removal,
active Nix store-path install and persistent kernel lock permissions.
The static `sys` used for that run had SHA-256
`1cf2f0346c3c517923c517bffb81a283eaa4da4972495170322ff587f4dbfb24`.
A subsequent worker-lock lease is covered by a real broker-kill regression;
final static smoke `serial-boot-20261012T112454Z-47d3cfae` passed 8/8
with clean poweroff, repeated sandbox denials and history/snapshot in an
explicitly network-free namespace. The final binary SHA-256 was
`1ea91f6f0ac82d88e9a8c400f510819aefaf73251778b248ef2443fdf18e2b62`. Two earlier attempts were
fixture failures (separate login/LUKS password mapping, then KVM identifier);
they are not claimed as product acceptance. Fresh installation, native Snap
revision/drift and interrupted native package recovery remain pending. See
[AGENTIC-HARDENING.md](AGENTIC-HARDENING.md) and
[the guest fixture](../installer/tests/vm/agentic-hardening-guest.sh).
## 2026-10-07/08 Installed post-install acceptance — ongoing ## 2026-10-07/08 Installed post-install acceptance — ongoing
Fresh encrypted Gentoo installation `serial-install-20261007T122805Z-da765f3d` Fresh encrypted Gentoo installation `serial-install-20261007T122805Z-da765f3d`

View file

@ -0,0 +1,40 @@
# ADR 0015 — Explicit agent execution boundaries
## Context
Implementation review found that material-effect digests omitted execution
context, path checks preceded pathname I/O, and per-action PID locks did not
serialize a complete transaction. Real negative testing also found that the
systemd user model unit could read home files despite its configured properties.
## Decision
Bind independent approval to a versioned execution envelope. Hold a persistent
kernel flock through revalidation, mutation, verification and terminal recording.
Use descriptor-relative no-follow target I/O and atomic file replacement. Pin
application install identities with native backend mechanisms and verify the
installed identity. Keep model transport output out of logs and funnel evidence
through a minimized, redacted boundary. Bound process output and deadlines.
Use an explicit Bubblewrap filesystem/PID boundary for unprivileged model calls
and developer agents. Developer workspace binds consume a pinned directory fd;
synthetic home and clean environment are defaults. Never fall back unconfined.
Systemd provides process/resource restrictions; root model calls use DynamicUser.
## Consequences
Old staged plans require fresh review. Bubblewrap is required for normal-user
model calls, and OSTree is shipped with Flatpak. Missing containment support
fails closed. Developer home tools and credential state need future explicit,
narrow grants. Backend dependency closures and repository snapshots are not
claimed fully immutable. Fresh installer and remaining native acceptance are
still required.
See [implementation and evidence](../AGENTIC-HARDENING.md).
## Status
Accepted, 2026-10-12. Implemented and regression tested; host negative isolation
tests passed. Gentoo overlay acceptance passed 14/14 for sandbox denials,
descriptor snapshots and Portage/Flatpak/Nix pins. Fresh installation and native
Snap/drift/interrupted-worker acceptance remain pending.

View file

@ -20,4 +20,7 @@ and link it here.
| [0012](0012-nvidia-driver-policy.md) | NVIDIA accelerated-driver policy and payload | Accepted | | [0012](0012-nvidia-driver-policy.md) | NVIDIA accelerated-driver policy and payload | Accepted |
| [0013](0013-ai-first-installation.md) | AI-first package installation workflow | Accepted | | [0013](0013-ai-first-installation.md) | AI-first package installation workflow | Accepted |
| [0014](0014-privileged-approval-ledger.md) | Privileged approval ledger | Accepted |
| [0015](0015-agent-execution-boundaries.md) | Explicit agent execution boundaries | Accepted |
Format: Context / Decision / Consequences / Status. Format: Context / Decision / Consequences / Status.

View file

@ -495,6 +495,7 @@ dependencies = [
name = "phoebus-core" name = "phoebus-core"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"rustix 1.1.5",
"serde", "serde",
"serde_json", "serde_json",
"sha2", "sha2",
@ -511,6 +512,7 @@ dependencies = [
"phoebus-ai", "phoebus-ai",
"phoebus-core", "phoebus-core",
"roxmltree", "roxmltree",
"rustix 1.1.5",
"serde", "serde",
"serde_json", "serde_json",
"sha2", "sha2",

View file

@ -21,6 +21,7 @@ clap = { version = "4", features = ["derive"] }
thiserror = "2" thiserror = "2"
zeroize = { version = "1", features = ["derive"] } zeroize = { version = "1", features = ["derive"] }
tempfile = "3" tempfile = "3"
rustix = { version = "1.1", features = ["fs", "process"] }
[profile.release] [profile.release]
lto = "thin" lto = "thin"

View file

@ -453,7 +453,11 @@ pub fn run_assistant(
} }
} }
if payload.len() > limits.max_evidence_bytes { if payload.len() > limits.max_evidence_bytes {
payload.truncate(limits.max_evidence_bytes); let mut end = limits.max_evidence_bytes;
while !payload.is_char_boundary(end) {
end -= 1;
}
payload.truncate(end);
payload.push_str("\n[evidence truncated]"); payload.push_str("\n[evidence truncated]");
} }
if requested_count > requests.len() { if requested_count > requests.len() {

View file

@ -12,6 +12,7 @@ serde_json.workspace = true
thiserror.workspace = true thiserror.workspace = true
zeroize.workspace = true zeroize.workspace = true
sha2 = "0.10" sha2 = "0.10"
rustix.workspace = true
[dev-dependencies] [dev-dependencies]
tempfile.workspace = true tempfile.workspace = true

View file

@ -68,6 +68,7 @@ impl Redactor {
// Avoid storing duplicates. // Avoid storing duplicates.
if !values.iter().any(|v| v.expose() == secret.expose()) { if !values.iter().any(|v| v.expose() == secret.expose()) {
values.push(secret.clone()); values.push(secret.clone());
values.sort_by_key(|value| std::cmp::Reverse(value.expose().len()));
} }
} }
} }
@ -79,7 +80,7 @@ impl Redactor {
/// Replace every registered secret in `text` with `<redacted>`. /// Replace every registered secret in `text` with `<redacted>`.
pub fn redact(&self, text: &str) -> String { pub fn redact(&self, text: &str) -> String {
let Ok(values) = self.inner.lock() else { let Ok(values) = self.inner.lock() else {
return text.to_string(); return "[redaction unavailable]".to_string();
}; };
let mut out = text.to_string(); let mut out = text.to_string();
for secret in values.iter() { for secret in values.iter() {

View file

@ -381,6 +381,65 @@ impl Need {
} }
/// One concrete, backend-resolved candidate. /// One concrete, backend-resolved candidate.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "backend", rename_all = "kebab-case", deny_unknown_fields)]
pub enum InstallArtifact {
Flatpak { reference: String, commit: String },
Snap { revision: String, channel: String },
Nix { flake: String, store_path: String },
}
impl InstallArtifact {
pub fn validate(&self) -> bool {
let hash = |s: &str| {
s.len() == 64
&& s.bytes().all(|b| b.is_ascii_hexdigit())
&& s.bytes().any(|b| b != b'0')
};
let store = |s: &str| {
s.strip_prefix("/nix/store/").is_some_and(|name| {
name.len() > 33
&& name.as_bytes()[32] == b'-'
&& name.bytes().all(|b| {
b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'+')
})
})
};
match self {
Self::Flatpak { reference, commit } => {
let parts: Vec<_> = reference.split('/').collect();
parts.len() == 4
&& parts[0] == "app"
&& crate::action::valid_flatpak_app_id(parts[1])
&& parts[2..].iter().all(|p| {
!p.is_empty()
&& p.bytes().all(|b| {
b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.')
})
})
&& hash(commit)
}
Self::Snap { revision, channel } => {
!revision.is_empty()
&& revision.bytes().all(|b| b.is_ascii_digit())
&& !channel.is_empty()
&& channel.bytes().all(|b| {
b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'/' | b'.')
})
}
Self::Nix { flake, store_path } => {
flake
.strip_prefix("path:")
.and_then(|p| p.split_once('#'))
.is_some_and(|(path, attr)| {
store(path) && crate::action::valid_nix_attribute(attr)
})
&& store(store_path)
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Candidate { pub struct Candidate {
pub backend: Backend, pub backend: Backend,
@ -388,6 +447,10 @@ pub struct Candidate {
/// attribute, installed AppImage id, or custom-ebuild atom). /// attribute, installed AppImage id, or custom-ebuild atom).
pub id: String, pub id: String,
pub version: Option<String>, pub version: Option<String>,
/// Immutable backend identity selected before human review. Missing pins
/// cannot be treated as exact artifact approval.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub artifact: Option<InstallArtifact>,
/// Where the artifact actually comes from (repo, URL, overlay). /// Where the artifact actually comes from (repo, URL, overlay).
pub provenance: String, pub provenance: String,
/// Whether provenance/integrity can be verified (signature or hash). /// Whether provenance/integrity can be verified (signature or hash).
@ -424,6 +487,7 @@ impl Candidate {
backend, backend,
id: id.into(), id: id.into(),
version: None, version: None,
artifact: None,
provenance: provenance.into(), provenance: provenance.into(),
verified: false, verified: false,
available: true, available: true,

View file

@ -1,152 +1,301 @@
//! The installation target root. //! Descriptor-relative target I/O. Lexical paths are for display/commands only;
//! //! writes never reopen a previously checked pathname. Symlinks are refused in
//! Host-versus-target path bugs are treated as HIGH SEVERITY. `TargetRoot` //! the root and every component. Atomic replacement also avoids hardlink writes.
//! makes it impossible to accidentally write to the host through the normal
//! APIs: every path is relative, must not be absolute, must not contain `..`,
//! and is joined to the root.
use std::path::{Component, Path, PathBuf};
use crate::error::{CoreError, Result}; use crate::error::{CoreError, Result};
use rustix::fs::{self, AtFlags, Mode, OFlags};
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Component, Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone)]
pub struct TargetRoot { pub struct TargetRoot {
root: PathBuf, root: PathBuf,
anchor: Arc<OnceLock<File>>,
}
impl PartialEq for TargetRoot {
fn eq(&self, other: &Self) -> bool {
self.root == other.root
}
}
impl Eq for TargetRoot {}
fn io(path: &Path, err: rustix::io::Errno) -> CoreError {
CoreError::io(path, err.into())
}
/// Each directory is opened relative to the previous pinned descriptor.
fn descend(mut dir: File, path: &Path, create: bool) -> Result<File> {
for component in path.components() {
match component {
Component::Normal(name) => {
if create {
match fs::mkdirat(&dir, name, Mode::from_bits_truncate(0o755)) {
Ok(()) | Err(rustix::io::Errno::EXIST) => {}
Err(e) => return Err(io(path, e)),
}
}
dir = fs::openat(
&dir,
name,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(),
)
.map(File::from)
.map_err(|e| {
if e == rustix::io::Errno::NOTDIR || e == rustix::io::Errno::LOOP {
CoreError::Validation(format!(
"target directory is a symlink or non-directory: {}",
path.display()
))
} else {
io(path, e)
}
})?;
}
Component::RootDir | Component::CurDir => {}
_ => {
return Err(CoreError::Validation(
"target path must not contain parent traversal".into(),
))
}
}
}
Ok(dir)
}
fn open_root(root: &Path, create: bool) -> Result<File> {
let base = if root.is_absolute() {
Path::new("/")
} else {
Path::new(".")
};
let dir = fs::open(
base,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::empty(),
)
.map(File::from)
.map_err(|e| io(root, e))?;
descend(dir, root, create)
} }
impl TargetRoot { impl TargetRoot {
pub fn new(root: impl Into<PathBuf>) -> Self { pub fn new(root: impl Into<PathBuf>) -> Self {
Self { root: root.into() } let root = root.into();
let anchor = OnceLock::new();
if let Ok(dir) = open_root(&root, false) {
let _ = anchor.set(dir);
}
Self {
root,
anchor: Arc::new(anchor),
}
} }
pub fn path(&self) -> &Path { pub fn path(&self) -> &Path {
&self.root &self.root
} }
fn root_handle(&self, create: bool) -> Result<&File> {
/// Resolve a relative path beneath the target root. if self.anchor.get().is_none() {
/// let dir = open_root(&self.root, create)?;
/// Rejects absolute paths, `..` traversal and `~`-style components. An let _ = self.anchor.set(dir);
/// empty relative path resolves to the root itself. }
self.anchor
.get()
.ok_or_else(|| CoreError::Validation("target root could not be anchored".into()))
}
pub fn resolve(&self, relative: &str) -> Result<PathBuf> { pub fn resolve(&self, relative: &str) -> Result<PathBuf> {
let rel = Path::new(relative.trim_start_matches('/')); let rel = Path::new(relative.trim_start_matches('/'));
for component in rel.components() { if rel.components().any(|c| {
match component { matches!(
Component::ParentDir => { c,
Component::ParentDir | Component::Prefix(_) | Component::RootDir
)
}) {
return Err(CoreError::Validation(format!( return Err(CoreError::Validation(format!(
"target path must not contain `..`: {relative}" "target path must not contain parent traversal: {relative}"
))); )));
} }
Component::RootDir | Component::Prefix(_) => {
return Err(CoreError::Validation(format!(
"target path must be relative: {relative}"
)));
}
_ => {}
}
}
Ok(self.root.join(rel)) Ok(self.root.join(rel))
} }
/// Shorthand for an absolute target path, e.g. `target.path_for("/etc/fstab")`.
pub fn path_for(&self, absolute: &str) -> Result<PathBuf> { pub fn path_for(&self, absolute: &str) -> Result<PathBuf> {
self.resolve(absolute) self.resolve(absolute)
} }
pub fn etc(&self, rel: &str) -> Result<PathBuf> {
pub fn etc(&self, relative: &str) -> Result<PathBuf> { self.resolve(&format!("etc/{rel}"))
self.resolve(&format!("etc/{relative}"))
} }
pub fn var(&self, rel: &str) -> Result<PathBuf> {
pub fn var(&self, relative: &str) -> Result<PathBuf> { self.resolve(&format!("var/{rel}"))
self.resolve(&format!("var/{relative}"))
} }
pub fn boot(&self, rel: &str) -> Result<PathBuf> {
pub fn boot(&self, relative: &str) -> Result<PathBuf> { self.resolve(&format!("boot/{rel}"))
self.resolve(&format!("boot/{relative}"))
} }
pub fn home(&self, rel: &str) -> Result<PathBuf> {
pub fn home(&self, relative: &str) -> Result<PathBuf> { self.resolve(&format!("home/{rel}"))
self.resolve(&format!("home/{relative}"))
} }
fn directory(&self, relative: &str, create: bool) -> Result<File> {
/// Refuse to traverse a symlink while writing beneath the target root. self.resolve(relative)?;
/// let root = self
/// A freshly installed target is created by the installer, but a stage3 .root_handle(create)?
/// archive (or a later, less careful writer) could leave a symlink whose .try_clone()
/// target lies outside the root. Following it would write to the host. The .map_err(|e| CoreError::io(&self.root, e))?;
/// write path therefore rejects symlinked components instead of resolving descend(root, Path::new(relative.trim_start_matches('/')), create)
/// them. }
fn reject_symlink_components(&self, relative: &str) -> Result<()> { fn parent(&self, relative: &str, create: bool) -> Result<(File, std::ffi::OsString)> {
self.resolve(relative)?;
let rel = Path::new(relative.trim_start_matches('/')); let rel = Path::new(relative.trim_start_matches('/'));
let mut current = self.root.clone(); let name = rel
for component in rel.components() { .file_name()
if let Component::Normal(part) = component { .ok_or_else(|| CoreError::Validation("target file path is empty".into()))?
current.push(part); .to_os_string();
if let Ok(meta) = std::fs::symlink_metadata(&current) { let parent = rel.parent().unwrap_or(Path::new(""));
if meta.file_type().is_symlink() { Ok((self.directory(&parent.to_string_lossy(), create)?, name))
}
/// Pin an existing directory for descriptor-based consumers such as a jail.
pub fn open_directory(&self, relative: &str) -> Result<File> {
self.directory(relative, false)
}
pub fn ensure_dir(&self, relative: &str) -> Result<PathBuf> {
self.directory(relative, true)?;
self.resolve(relative)
}
pub fn ensure_dir_mode(&self, relative: &str, mode: u32) -> Result<PathBuf> {
let dir = self.directory(relative, true)?;
fs::fchmod(&dir, Mode::from_bits_truncate(mode)).map_err(|e| io(&self.root, e))?;
self.resolve(relative)
}
fn write_mode(&self, relative: &str, bytes: &[u8], mode: Option<u32>) -> Result<PathBuf> {
let path = self.resolve(relative)?;
let (parent, name) = self.parent(relative, true)?;
let existing = match fs::statat(&parent, &name, AtFlags::SYMLINK_NOFOLLOW) {
Ok(stat) => {
if fs::FileType::from_raw_mode(stat.st_mode) != fs::FileType::RegularFile {
return Err(CoreError::Validation(format!( return Err(CoreError::Validation(format!(
"refusing to write through a symlink inside the target: {}", "target leaf is not a regular file: {}",
current.display() path.display()
))); )));
} }
Some((stat.st_mode & 0o777, stat.st_uid, stat.st_gid))
} }
Err(rustix::io::Errno::NOENT) => None,
Err(e) => return Err(io(&path, e)),
};
static SEQ: AtomicU64 = AtomicU64::new(0);
let tmp = format!(
".phoebus-write-{}-{}",
std::process::id(),
SEQ.fetch_add(1, Ordering::Relaxed)
);
let mut file = fs::openat(
&parent,
&tmp,
OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::from_bits_truncate(0o600),
)
.map(File::from)
.map_err(|e| io(&path, e))?;
let result = (|| {
file.write_all(bytes).map_err(|e| CoreError::io(&path, e))?;
if let Some((_, uid, gid)) = existing {
fs::fchown(
&file,
Some(rustix::process::Uid::from_raw(uid)),
Some(rustix::process::Gid::from_raw(gid)),
)
.map_err(|e| io(&path, e))?;
} }
fs::fchmod(
&file,
Mode::from_bits_truncate(
mode.or(existing.map(|(mode, _, _)| mode)).unwrap_or(0o644),
),
)
.map_err(|e| io(&path, e))?;
file.sync_all().map_err(|e| CoreError::io(&path, e))?;
fs::renameat(&parent, &tmp, &parent, &name).map_err(|e| io(&path, e))?;
parent.sync_all().map_err(|e| CoreError::io(&path, e))?;
Ok(path.clone())
})();
if result.is_err() {
let _ = fs::unlinkat(&parent, &tmp, AtFlags::empty());
} }
Ok(()) result
} }
pub fn ensure_dir(&self, relative: &str) -> Result<PathBuf> {
self.reject_symlink_components(relative)?;
let path = self.resolve(relative)?;
std::fs::create_dir_all(&path).map_err(|e| CoreError::io(path.clone(), e))?;
Ok(path)
}
/// Create a directory with an explicit mode (on Unix).
pub fn ensure_dir_mode(&self, relative: &str, mode: u32) -> Result<PathBuf> {
use std::os::unix::fs::PermissionsExt;
let path = self.ensure_dir(relative)?;
let perms = std::fs::Permissions::from_mode(mode);
std::fs::set_permissions(&path, perms).map_err(|e| CoreError::io(path.clone(), e))?;
Ok(path)
}
pub fn write(&self, relative: &str, contents: impl AsRef<[u8]>) -> Result<PathBuf> { pub fn write(&self, relative: &str, contents: impl AsRef<[u8]>) -> Result<PathBuf> {
self.reject_symlink_components(relative)?; self.write_mode(relative, contents.as_ref(), None)
}
/// Open a persistent lock inode. Never unlink it: kernel locking requires
/// every contender to refer to this same file, including after a crash.
pub fn open_lock_file(&self, relative: &str) -> Result<File> {
let path = self.resolve(relative)?; let path = self.resolve(relative)?;
if let Some(parent) = path.parent() { let (parent, name) = self.parent(relative, true)?;
std::fs::create_dir_all(parent).map_err(|e| CoreError::io(parent.to_path_buf(), e))?; let file = fs::openat(
&parent,
&name,
OFlags::RDWR | OFlags::CREATE | OFlags::NOFOLLOW | OFlags::CLOEXEC | OFlags::NONBLOCK,
Mode::from_bits_truncate(0o600),
)
.map(File::from)
.map_err(|e| io(&path, e))?;
let meta = file.metadata().map_err(|e| CoreError::io(&path, e))?;
use std::os::unix::fs::MetadataExt;
if !meta.is_file() || meta.nlink() != 1 || meta.uid() != rustix::process::geteuid().as_raw()
{
return Err(CoreError::Validation(
"lock is not a private regular inode".into(),
));
} }
std::fs::write(&path, contents).map_err(|e| CoreError::io(path.clone(), e))?; fs::fchmod(&file, Mode::from_bits_truncate(0o600)).map_err(|e| io(&path, e))?;
Ok(path) Ok(file)
} }
pub fn read_to_string(&self, relative: &str) -> Result<String> { pub fn read_to_string(&self, relative: &str) -> Result<String> {
let path = self.resolve(relative)?; let path = self.resolve(relative)?;
std::fs::read_to_string(&path).map_err(|e| CoreError::io(path, e)) let (parent, name) = self.parent(relative, false)?;
let mut file = fs::openat(
&parent,
&name,
OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::CLOEXEC | OFlags::NONBLOCK,
Mode::empty(),
)
.map(File::from)
.map_err(|e| io(&path, e))?;
if !file
.metadata()
.map_err(|e| CoreError::io(&path, e))?
.is_file()
{
return Err(CoreError::Validation(
"target read is not a regular file".into(),
));
}
let mut text = String::new();
file.read_to_string(&mut text)
.map_err(|e| CoreError::io(&path, e))?;
Ok(text)
} }
/// Remove a target file without following symlink components.
pub fn remove_file(&self, relative: &str) -> Result<()> { pub fn remove_file(&self, relative: &str) -> Result<()> {
self.reject_symlink_components(relative)?;
let path = self.resolve(relative)?; let path = self.resolve(relative)?;
std::fs::remove_file(&path).map_err(|e| CoreError::io(path, e)) let (parent, name) = self.parent(relative, false)?;
let stat =
fs::statat(&parent, &name, AtFlags::SYMLINK_NOFOLLOW).map_err(|e| io(&path, e))?;
if fs::FileType::from_raw_mode(stat.st_mode) != fs::FileType::RegularFile {
return Err(CoreError::Validation(
"target removal is not a regular file".into(),
));
}
fs::unlinkat(&parent, &name, AtFlags::empty()).map_err(|e| io(&path, e))?;
parent.sync_all().map_err(|e| CoreError::io(&path, e))
} }
pub fn copy_file(&self, relative: &str, src: &Path, mode: u32) -> Result<PathBuf> { pub fn copy_file(&self, relative: &str, src: &Path, mode: u32) -> Result<PathBuf> {
use std::os::unix::fs::PermissionsExt; let bytes = std::fs::read(src).map_err(|e| CoreError::io(src, e))?;
self.reject_symlink_components(relative)?; self.write_mode(relative, &bytes, Some(mode))
let path = self.resolve(relative)?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| CoreError::io(parent.to_path_buf(), e))?;
} }
std::fs::copy(src, &path).map_err(|e| CoreError::io(path.clone(), e))?;
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode))
.map_err(|e| CoreError::io(path.clone(), e))?;
Ok(path)
}
pub fn exists(&self, relative: &str) -> bool { pub fn exists(&self, relative: &str) -> bool {
self.resolve(relative).map(|p| p.exists()).unwrap_or(false) self.parent(relative, false)
.ok()
.and_then(|(parent, name)| fs::statat(&parent, name, AtFlags::SYMLINK_NOFOLLOW).ok())
.is_some()
} }
} }
@ -239,4 +388,77 @@ mod tests {
"original" "original"
); );
} }
#[test]
fn root_symlinks_are_refused_and_replaced_roots_stay_anchored() {
let dir = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let root = dir.path().join("root");
std::os::unix::fs::symlink(outside.path(), &root).unwrap();
assert!(TargetRoot::new(&root).write("escape", "x").is_err());
std::fs::remove_file(&root).unwrap();
std::fs::create_dir(&root).unwrap();
let target = TargetRoot::new(&root);
let moved = dir.path().join("original");
std::fs::rename(&root, &moved).unwrap();
std::os::unix::fs::symlink(outside.path(), &root).unwrap();
target.write("anchored", "safe").unwrap();
assert_eq!(
std::fs::read_to_string(moved.join("anchored")).unwrap(),
"safe"
);
assert!(!outside.path().join("anchored").exists());
}
#[test]
fn atomic_replacement_never_writes_a_hardlink_target() {
let dir = tempfile::tempdir().unwrap();
let outside = dir.path().join("outside");
std::fs::write(&outside, "protected").unwrap();
let root = dir.path().join("root");
std::fs::create_dir(&root).unwrap();
std::fs::hard_link(&outside, root.join("linked")).unwrap();
TargetRoot::new(&root)
.write("linked", "new contents")
.unwrap();
assert_eq!(std::fs::read_to_string(&outside).unwrap(), "protected");
}
#[test]
fn concurrent_directory_symlink_swaps_cannot_escape_descriptor_writes() {
let dir = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let root = dir.path().to_path_buf();
std::fs::create_dir(root.join("etc")).unwrap();
let target = TargetRoot::new(&root);
std::os::unix::fs::symlink(outside.path(), root.join("swapped")).unwrap();
let stop = std::sync::atomic::AtomicBool::new(false);
std::thread::scope(|scope| {
let worker = scope.spawn(|| {
while !stop.load(Ordering::Relaxed) {
fs::renameat_with(
fs::CWD,
root.join("etc"),
fs::CWD,
root.join("swapped"),
fs::RenameFlags::EXCHANGE,
)
.unwrap();
std::thread::yield_now();
fs::renameat_with(
fs::CWD,
root.join("etc"),
fs::CWD,
root.join("swapped"),
fs::RenameFlags::EXCHANGE,
)
.unwrap();
}
});
for _ in 0..100 {
let _ = target.write("etc/probe", "fixture");
}
stop.store(true, Ordering::Relaxed);
worker.join().unwrap();
});
assert!(!outside.path().join("probe").exists());
}
} }

View file

@ -16,6 +16,7 @@ toml.workspace = true
tempfile.workspace = true tempfile.workspace = true
sha2 = "0.10" sha2 = "0.10"
roxmltree = "0.20" roxmltree = "0.20"
rustix.workspace = true
[dev-dependencies] [dev-dependencies]
tempfile.workspace = true tempfile.workspace = true

View file

@ -199,7 +199,9 @@ fn pretend_args(action: &SystemAction) -> Option<Vec<String>> {
let mut args = vec!["--pretend".into(), "--color=n".into()]; let mut args = vec!["--pretend".into(), "--color=n".into()];
if *binary == Some(true) { if *binary == Some(true) {
args.push("--getbinpkg=y".into()); args.push("--getbinpkg=y".into());
args.push("--usepkg=y".into()); args.push("--usepkgonly".into());
} else if *binary == Some(false) {
args.push("--usepkg=n".into());
} }
args.push("--".into()); args.push("--".into());
args.push(atom.clone()); args.push(atom.clone());
@ -1337,6 +1339,14 @@ pub fn execute_checked(
root: &Path, root: &Path,
approved: Option<&ResolvedPlan>, approved: Option<&ResolvedPlan>,
) -> Result<ActionOutcome> { ) -> Result<ActionOutcome> {
if action.requires_root() && auth.is_none() {
return Err(ExecError::NotAuthorized(action.describe()));
}
let _lock = if action.requires_root() && exec.is_real() && !exec.dry_run {
Some(exec.lock_mutation(root, &action.describe())?)
} else {
None
};
let current = if let Some(approved) = approved { let current = if let Some(approved) = approved {
let current = resolve_effects(exec, action, root)?; let current = resolve_effects(exec, action, root)?;
if changed_materially(approved, &current) { if changed_materially(approved, &current) {
@ -1383,6 +1393,23 @@ pub fn execute_plan(
root: &Path, root: &Path,
) -> PlanOutcome { ) -> PlanOutcome {
let mut outcome = PlanOutcome::default(); let mut outcome = PlanOutcome::default();
let lock = if steps.iter().any(|(a, _)| a.requires_root()) && exec.is_real() && !exec.dry_run {
exec.lock_mutation(root, "execute reviewed plan").map(Some)
} else {
Ok(None)
};
let _lock = match lock {
Ok(lock) => lock,
Err(e) => {
for (action, _) in steps {
outcome
.outcomes
.push(TransactionOutcome::unattempted(action.describe()));
}
outcome.failure = Some(("acquire mutation lock".into(), e.to_string()));
return outcome;
}
};
for (action, resolved) in steps { for (action, resolved) in steps {
let described = action.describe(); let described = action.describe();
if outcome.failure.is_some() { if outcome.failure.is_some() {
@ -1485,6 +1512,18 @@ pub fn execute_inner(
auth: Option<&DestructiveAuthorization>, auth: Option<&DestructiveAuthorization>,
root: &Path, root: &Path,
resolved: Option<&ResolvedPlan>, resolved: Option<&ResolvedPlan>,
) -> Result<ActionOutcome> {
execute_inner_with_candidate(exec, action, auth, root, resolved, None)
}
/// Dedicated install execution consumes the candidate's immutable artifact.
pub fn execute_inner_with_candidate(
exec: &Exec,
action: &SystemAction,
auth: Option<&DestructiveAuthorization>,
root: &Path,
resolved: Option<&ResolvedPlan>,
candidate: Option<&phoebus_core::selection::Candidate>,
) -> Result<ActionOutcome> { ) -> Result<ActionOutcome> {
// The execution boundary validates every action regardless of its source // The execution boundary validates every action regardless of its source
// (manual phrase, JSON, or AI plan), so an invalid value can never reach a // (manual phrase, JSON, or AI plan), so an invalid value can never reach a
@ -1493,6 +1532,17 @@ pub fn execute_inner(
.validate() .validate()
.map_err(|e| ExecError::operation("sys", "validate action", e))?; .map_err(|e| ExecError::operation("sys", "validate action", e))?;
if let Some(candidate) = candidate {
let expected = crate::install::action_for(exec, root, candidate)?;
if &expected != action {
return Err(ExecError::operation(
"install",
"validate candidate action",
"action does not match the reviewed candidate",
));
}
}
if action.requires_root() && auth.is_none() { if action.requires_root() && auth.is_none() {
return Err(ExecError::NotAuthorized(action.describe())); return Err(ExecError::NotAuthorized(action.describe()));
} }
@ -1500,11 +1550,7 @@ pub fn execute_inner(
// The global mutation lock is root-owned. Personal preferences are an // The global mutation lock is root-owned. Personal preferences are an
// atomic user-file write and must remain usable by an unprivileged account. // atomic user-file write and must remain usable by an unprivileged account.
let _lock = if action.requires_root() && exec.is_real() && !exec.dry_run { let _lock = if action.requires_root() && exec.is_real() && !exec.dry_run {
Some(crate::system_lock::SystemLock::acquire( Some(exec.lock_mutation(root, &action.describe())?)
exec,
root,
&action.describe(),
)?)
} else { } else {
None None
}; };
@ -1616,7 +1662,9 @@ pub fn execute_inner(
let mut args = vec!["--ask=n".into()]; let mut args = vec!["--ask=n".into()];
if *binary == Some(true) { if *binary == Some(true) {
args.push("--getbinpkg=y".into()); args.push("--getbinpkg=y".into());
args.push("--usepkg=y".into()); args.push("--usepkgonly".into());
} else if *binary == Some(false) {
args.push("--usepkg=n".into());
} }
args.push("--".into()); args.push("--".into());
args.push(atom.clone()); args.push(atom.clone());
@ -1664,6 +1712,16 @@ pub fn execute_inner(
), ),
)); ));
} }
if let Some(candidate) = candidate {
if candidate.id != *app_id || source.candidate.artifact != candidate.artifact {
return Err(ExecError::operation(
"install",
"revalidate artifact",
"Flatpak artifact changed before execution",
));
}
crate::artifact::install_flatpak(exec, root, candidate)?
} else {
run_in_target( run_in_target(
exec, exec,
root, root,
@ -1682,6 +1740,7 @@ pub fn execute_inner(
)?; )?;
format!("installed Flatpak app {app_id}") format!("installed Flatpak app {app_id}")
} }
}
SystemAction::FlatpakRemove { app_id } => { SystemAction::FlatpakRemove { app_id } => {
run_in_target( run_in_target(
exec, exec,
@ -1838,7 +1897,20 @@ pub fn execute_inner(
classic, classic,
} => { } => {
require_host_root(root, "install snap")?; require_host_root(root, "install snap")?;
let out = snap::install(exec, name, channel.as_deref(), *classic)?; let out = if let Some(candidate) = candidate {
let phoebus_core::selection::InstallArtifact::Snap { revision, channel } =
crate::artifact::require(candidate)?
else {
return Err(ExecError::operation(
"install",
"artifact",
"wrong Snap artifact type",
));
};
snap::install_revision(exec, name, Some(channel), *classic, Some(revision))?
} else {
snap::install(exec, name, channel.as_deref(), *classic)?
};
let confinement = snap::status(exec) let confinement = snap::status(exec)
.map(|s| s.confinement.as_str().to_string()) .map(|s| s.confinement.as_str().to_string())
.unwrap_or_else(|_| "unknown".to_string()); .unwrap_or_else(|_| "unknown".to_string());
@ -1994,7 +2066,32 @@ pub fn execute_inner(
format!("realised Nix development packages: {}", prepared.join(", ")) format!("realised Nix development packages: {}", prepared.join(", "))
} }
SystemAction::NixInstall { packages } => { SystemAction::NixInstall { packages } => {
let installed = crate::nix::install_profile_packages(exec, root, packages)?; let installed = if let Some(candidate) = candidate {
let phoebus_core::selection::InstallArtifact::Nix { flake, .. } =
crate::artifact::require(candidate)?
else {
return Err(ExecError::operation(
"install",
"artifact",
"wrong Nix artifact type",
));
};
crate::portage::chroot(
exec,
root,
"/nix/var/nix/profiles/default/bin/nix",
&[
"profile",
"install",
"--profile",
crate::nix::PHOEBUS_PROFILE,
flake,
],
)?;
packages.clone()
} else {
crate::nix::install_profile_packages(exec, root, packages)?
};
format!("installed into the Nix profile: {}", installed.join(", ")) format!("installed into the Nix profile: {}", installed.join(", "))
} }
SystemAction::UpdateSystem => { SystemAction::UpdateSystem => {

View file

@ -70,77 +70,153 @@ pub fn build_jail_argv(
home: &Path, home: &Path,
program: &str, program: &str,
args: &[String], args: &[String],
) -> Vec<String> { ) -> Result<Vec<String>> {
let mut out: Vec<String> = vec![ let valid_path = |path: &Path| {
"--die-with-parent".into(), path.is_absolute()
"--dev".into(), && !path
"/dev".into(), .components()
"--proc".into(), .any(|c| matches!(c, std::path::Component::ParentDir))
"/proc".into(), };
"--tmpfs".into(), if !valid_path(home)
"/tmp".into(), || !valid_path(&config.workspace)
]; || config.workspace == Path::new("/")
|| home.starts_with(&config.workspace)
// Read-only system mounts {
return Err(ExecError::operation("agent-jail", "validate workspace", "workspace must be absolute, contain no traversal, and must not expose home or the filesystem root"));
}
let mut masks: Vec<PathBuf> = DEFAULT_SENSITIVE_SUBDIRS
.iter()
.map(|s| home.join(s))
.collect();
for subdir in &config.extra_masked_subdirs {
let rel = Path::new(subdir);
if rel.is_absolute()
|| rel
.components()
.any(|c| !matches!(c, std::path::Component::Normal(_)))
|| subdir.is_empty()
{
return Err(ExecError::operation(
"agent-jail",
"validate mask",
"mask must be a relative path without traversal",
));
}
masks.push(home.join(rel));
}
for mask in &masks {
if config.workspace.starts_with(mask) || mask.starts_with(&config.workspace) {
return Err(ExecError::operation(
"agent-jail",
"validate workspace",
"workspace overlaps a credential mask",
));
}
}
// System trees are immutable, even when selected as a workspace.
if [
"/usr", "/bin", "/sbin", "/lib", "/lib64", "/etc", "/proc", "/sys", "/dev", "/run", "/nix",
]
.iter()
.any(|p| config.workspace.starts_with(p))
{
return Err(ExecError::operation(
"agent-jail",
"validate workspace",
"workspace overlaps a protected system tree",
));
}
let mut out: Vec<String> = [
"--die-with-parent",
"--unshare-user",
"--unshare-pid",
"--unshare-ipc",
"--unshare-uts",
"--new-session",
"--cap-drop",
"ALL",
"--clearenv",
"--dev",
"/dev",
"--proc",
"/proc",
"--tmpfs",
"/tmp",
]
.iter()
.map(|s| s.to_string())
.collect();
for sys_dir in ["/usr", "/bin", "/sbin", "/lib", "/lib64", "/etc"] { for sys_dir in ["/usr", "/bin", "/sbin", "/lib", "/lib64", "/etc"] {
if Path::new(sys_dir).exists() { if Path::new(sys_dir).exists() {
out.push("--ro-bind".into()); out.extend(["--ro-bind".into(), sys_dir.into(), sys_dir.into()]);
out.push(sys_dir.into());
out.push(sys_dir.into());
} }
} }
if config.allow_nix_store && Path::new("/nix/store").exists() {
// Read-only Nix store if enabled and present out.extend(["--ro-bind".into(), "/nix/store".into(), "/nix/store".into()]);
if config.allow_nix_store && Path::new("/nix").exists() {
out.push("--ro-bind".into());
out.push("/nix".into());
out.push("/nix".into());
} }
// Bind home directory
let home_str = home.to_string_lossy().to_string(); let home_str = home.to_string_lossy().to_string();
out.push("--bind".into()); out.extend(["--tmpfs".into(), home_str.clone()]);
out.push(home_str.clone()); out.extend(["--setenv".into(), "HOME".into(), home_str]);
out.push(home_str); out.extend([
"--setenv".into(),
// Mask sensitive subdirectories under home with private empty tmpfs "PATH".into(),
let mut masked_subdirs: Vec<&str> = DEFAULT_SENSITIVE_SUBDIRS.to_vec(); "/usr/local/bin:/usr/bin:/bin".into(),
let extra_refs: Vec<&str> = config ]);
.extra_masked_subdirs out.extend(["--setenv".into(), "LANG".into(), "C.UTF-8".into()]);
.iter() let ws = config.workspace.to_string_lossy().to_string();
.map(|s| s.as_str()) out.extend([
.collect(); "--bind".into(),
masked_subdirs.extend(extra_refs); ws.clone(),
ws.clone(),
for sensitive in masked_subdirs { "--chdir".into(),
let clean = sensitive.trim_start_matches('/'); ws,
if clean.is_empty() || clean.split('/').any(|part| part == "..") { ]);
continue; // Masks are last, so no writable bind can remount a credential store.
for mask in masks {
out.extend(["--tmpfs".into(), mask.to_string_lossy().to_string()]);
} }
let sensitive_path = home.join(clean);
let path_str = sensitive_path.to_string_lossy().to_string();
out.push("--tmpfs".into());
out.push(path_str);
}
// Bind the workspace read-write and set working directory
let ws_str = config.workspace.to_string_lossy().to_string();
out.push("--bind".into());
out.push(ws_str.clone());
out.push(ws_str.clone());
out.push("--chdir".into());
out.push(ws_str);
// Network isolation
if !config.allow_network { if !config.allow_network {
out.push("--unshare-net".into()); out.push("--unshare-net".into());
} }
out.push("--".into());
// Target executable and arguments
out.push(program.to_string()); out.push(program.to_string());
out.extend(args.iter().cloned()); out.extend(args.iter().cloned());
Ok(out)
}
out /// A validated workspace descriptor stays alive until Bubblewrap consumes it.
/// The bind never reopens the checked pathname, so a symlink swap cannot grant
/// the agent access to another directory.
pub struct PreparedJail {
pub argv: Vec<String>,
_workspace: std::fs::File,
}
pub fn prepare_jail(
config: &AgentJailConfig,
home: &Path,
program: &str,
args: &[String],
) -> Result<PreparedJail> {
use std::os::fd::AsRawFd;
let mut argv = build_jail_argv(config, home, program, args)?;
let workspace = phoebus_core::TargetRoot::new(&config.workspace).open_directory("")?;
rustix::io::fcntl_setfd(&workspace, rustix::io::FdFlags::empty()).map_err(|e| {
ExecError::operation("agent-jail", "inherit workspace descriptor", e.to_string())
})?;
let ws = config.workspace.to_string_lossy();
let index = argv
.windows(3)
.position(|w| w[0] == "--bind" && w[1] == ws && w[2] == ws)
.ok_or_else(|| {
ExecError::operation("agent-jail", "pin workspace", "workspace bind missing")
})?;
argv[index] = "--bind-fd".into();
argv[index + 1] = workspace.as_raw_fd().to_string();
Ok(PreparedJail {
argv,
_workspace: workspace,
})
} }
/// Run a program inside the Bubblewrap agent jail. /// Run a program inside the Bubblewrap agent jail.
@ -157,8 +233,39 @@ pub fn run_jailed(
)); ));
} }
let argv = build_jail_argv(config, home, program, args); if exec.is_real() && crate::personal::effective_uid() == 0 {
return Err(ExecError::operation(
"agent-jail",
"validate account",
"developer agents must run as an unprivileged user",
));
}
let home = if exec.is_real() {
crate::personal::home_for_uid(crate::personal::effective_uid())
.and_then(|p| p.canonicalize().ok())
.ok_or_else(|| {
ExecError::operation("agent-jail", "validate home", "account home is unavailable")
})?
} else {
home.to_path_buf()
};
let mut canonical = config.clone();
if exec.is_real() {
canonical.workspace = config
.workspace
.canonicalize()
.map_err(|e| ExecError::operation("agent-jail", "validate workspace", e.to_string()))?;
}
if exec.is_real() {
let jail = prepare_jail(&canonical, &home, program, args)?;
exec.run(
CommandSpec::new("agent-jail", "run jailed user agent", "bwrap")
.args(jail.argv.clone()),
)
} else {
let argv = build_jail_argv(&canonical, &home, program, args)?;
exec.run(CommandSpec::new("agent-jail", "run jailed user agent", "bwrap").args(argv)) exec.run(CommandSpec::new("agent-jail", "run jailed user agent", "bwrap").args(argv))
}
} }
#[cfg(test)] #[cfg(test)]
@ -176,7 +283,7 @@ mod tests {
extra_masked_subdirs: vec![".custom-secrets".into()], extra_masked_subdirs: vec![".custom-secrets".into()],
}; };
let argv = build_jail_argv(&config, home, "cargo", &["build".into()]); let argv = build_jail_argv(&config, home, "cargo", &["build".into()]).unwrap();
// Verify die-with-parent and dev/proc/tmp // Verify die-with-parent and dev/proc/tmp
assert!(argv.contains(&"--die-with-parent".to_string())); assert!(argv.contains(&"--die-with-parent".to_string()));
@ -185,7 +292,7 @@ mod tests {
// Verify home directory is bound // Verify home directory is bound
let home_idx = argv.iter().position(|a| a == "/home/developer").unwrap(); let home_idx = argv.iter().position(|a| a == "/home/developer").unwrap();
assert_eq!(argv[home_idx - 1], "--bind"); assert_eq!(argv[home_idx - 1], "--tmpfs");
// Verify sensitive paths are masked with tmpfs // Verify sensitive paths are masked with tmpfs
for sensitive in [ for sensitive in [
@ -230,7 +337,7 @@ mod tests {
extra_masked_subdirs: vec![], extra_masked_subdirs: vec![],
}; };
let argv = build_jail_argv(&config, home, "python", &["-m".into(), "test".into()]); let argv = build_jail_argv(&config, home, "python", &["-m".into(), "test".into()]).unwrap();
assert!(argv.contains(&"--unshare-net".to_string())); assert!(argv.contains(&"--unshare-net".to_string()));
} }
@ -249,10 +356,26 @@ mod tests {
], ],
}; };
let argv = build_jail_argv(&config, home, "echo", &["hi".into()]); assert!(build_jail_argv(&config, home, "echo", &["hi".into()]).is_err());
assert!(!argv.contains(&"/root".to_string())); }
assert!(!argv.contains(&"/home/developer/../sensitive".to_string())); #[test]
assert!(!argv.contains(&"/home/developer/../../etc".to_string())); fn credential_workspaces_and_home_are_refused() {
assert!(argv.contains(&"/home/developer/valid-subdir".to_string())); let home = Path::new("/home/developer");
for path in [
"/",
"/home",
"/home/developer",
"/home/developer/.ssh/project",
"/etc/project",
] {
let config = AgentJailConfig {
workspace: path.into(),
..Default::default()
};
assert!(
build_jail_argv(&config, home, "true", &[]).is_err(),
"{path}"
);
}
} }
} }

View file

@ -109,8 +109,9 @@ impl ModelTransport for CurlTransport<'_> {
// Fail closed on an unsafe scheme or a URL that could inject curl // Fail closed on an unsafe scheme or a URL that could inject curl
// config directives before anything is written. // config directives before anything is written.
phoebus_ai::provider::validate_endpoint(url)?; phoebus_ai::provider::validate_endpoint(url)?;
let body = self.exec.redactor().redact(body);
if self.sandboxed { if self.sandboxed {
return self.send_sandboxed(url, body); return self.send_sandboxed(url, &body);
} }
let mut request = tempfile::Builder::new() let mut request = tempfile::Builder::new()
.prefix("phoebus-ai-request.") .prefix("phoebus-ai-request.")
@ -146,7 +147,12 @@ impl ModelTransport for CurlTransport<'_> {
max = phoebus_ai::provider::MAX_RESPONSE_BYTES, max = phoebus_ai::provider::MAX_RESPONSE_BYTES,
); );
let spec = CommandSpec::new("ai", "query model", "curl") let spec = CommandSpec::new("ai", "query model", "curl")
.args(["-sS", "-K", "-"]) .args(["--disable", "-sS", "-K", "-"])
.sensitive_output()
.output_limit(phoebus_ai::provider::MAX_RESPONSE_BYTES as usize)
.timeout(std::time::Duration::from_secs(
ai_timeout_secs().saturating_add(30),
))
.stdin(config.into_bytes()); .stdin(config.into_bytes());
let out = self let out = self
.exec .exec
@ -177,11 +183,13 @@ impl CurlTransport<'_> {
timeout = ai_timeout_secs(), timeout = ai_timeout_secs(),
max = phoebus_ai::provider::MAX_RESPONSE_BYTES, max = phoebus_ai::provider::MAX_RESPONSE_BYTES,
); );
let out = crate::sandbox::run( let out = crate::sandbox::run_bounded(
self.exec, self.exec,
"curl", "curl",
&["-sS".into(), "-K".into(), "-".into()], &["--disable".into(), "-sS".into(), "-K".into(), "-".into()],
config.into_bytes(), config.into_bytes(),
phoebus_ai::provider::MAX_RESPONSE_BYTES as usize,
std::time::Duration::from_secs(ai_timeout_secs().saturating_add(30)),
) )
.map_err(|e| AiError::Provider(e.to_string()))?; .map_err(|e| AiError::Provider(e.to_string()))?;
Ok(out.stdout_lossy()) Ok(out.stdout_lossy())
@ -319,7 +327,9 @@ mod tests {
spec.args spec.args
); );
assert!( assert!(
spec.args.iter().any(|a| a.contains("ProtectHome=yes")), spec.args
.iter()
.any(|a| a.contains("ProtectHome=yes") || a == "/usr/bin/bwrap"),
"the sandbox properties must be present: {:?}", "the sandbox properties must be present: {:?}",
spec.args spec.args
); );

View file

@ -0,0 +1,221 @@
//! Immutable install identities. Resolution performs only metadata reads;
//! execution consumes a validated pin instead of following a mutable channel.
use crate::{
command::CommandSpec,
error::{ExecError, Result},
exec::Exec,
};
use phoebus_core::selection::{Backend, Candidate, InstallArtifact};
use std::path::Path;
fn read(exec: &Exec, root: &Path, program: &str, args: &[&str]) -> Option<String> {
let spec = if root == Path::new("/") {
CommandSpec::new("artifact", "resolve install identity", program).args(args.iter().copied())
} else {
CommandSpec::new("artifact", "resolve install identity", "chroot")
.arg(root.to_string_lossy())
.arg(program)
.args(args.iter().copied())
}
.sensitive_output()
.output_limit(1024 * 1024)
.timeout(std::time::Duration::from_secs(120));
exec.run(spec)
.ok()
.map(|out| out.stdout_lossy().trim().to_string())
}
pub fn resolve(exec: &Exec, root: &Path, candidate: &mut Candidate) {
let pin = match candidate.backend {
Backend::Flatpak => {
let reference = read(
exec,
root,
"flatpak",
&[
"remote-info",
"--system",
"--show-ref",
"--",
"flathub",
&candidate.id,
],
);
let commit = read(
exec,
root,
"flatpak",
&[
"remote-info",
"--system",
"--show-commit",
"--",
"flathub",
&candidate.id,
],
);
reference
.zip(commit)
.map(|(reference, commit)| InstallArtifact::Flatpak { reference, commit })
}
Backend::Snap => read(
exec,
root,
"snap",
&["info", "--color=never", &candidate.id],
)
.and_then(|text| {
text.lines().find_map(|line| {
let words: Vec<_> = line.split_whitespace().collect();
if !matches!(
words.first().copied(),
Some("latest/stable:") | Some("stable:")
) {
return None;
}
let revision = words
.iter()
.find_map(|w| w.strip_prefix('(')?.strip_suffix(')'))?;
if words.len() > 1 {
candidate.version = Some(words[1].into());
}
Some(InstallArtifact::Snap {
revision: revision.into(),
channel: "latest/stable".into(),
})
})
}),
Backend::Nix => read(
exec,
root,
"/nix/var/nix/profiles/default/bin/nix",
&["flake", "metadata", "--json", "nixpkgs"],
)
.and_then(|text| {
let metadata: serde_json::Value = serde_json::from_str(&text).ok()?;
let source = metadata.get("path")?.as_str()?;
let flake = format!("path:{source}#{}", candidate.id);
let store_path = read(
exec,
root,
"/nix/var/nix/profiles/default/bin/nix",
&["eval", "--raw", &format!("{flake}.outPath")],
)?;
Some(InstallArtifact::Nix { flake, store_path })
}),
_ => return,
};
candidate.artifact = pin.filter(InstallArtifact::validate);
if candidate.artifact.is_none() {
candidate.available = false;
candidate.verified = false;
candidate.notes.push(
"immutable install identity could not be resolved; refusing an approximate approval"
.into(),
);
}
}
pub fn require(candidate: &Candidate) -> Result<&InstallArtifact> {
candidate
.artifact
.as_ref()
.filter(|pin| {
pin.validate()
&& match (candidate.backend, pin) {
(Backend::Flatpak, InstallArtifact::Flatpak { reference, .. }) => {
reference.split('/').nth(1) == Some(candidate.id.as_str())
}
(Backend::Snap, InstallArtifact::Snap { .. }) => true,
(Backend::Nix, InstallArtifact::Nix { flake, .. }) => flake
.split_once('#')
.is_some_and(|(_, attr)| attr == candidate.id),
_ => false,
}
})
.ok_or_else(|| {
ExecError::operation(
"install",
"validate artifact",
"reviewed install artifact is missing or invalid; review a fresh plan",
)
})
}
pub fn install_flatpak(exec: &Exec, root: &Path, candidate: &Candidate) -> Result<String> {
let InstallArtifact::Flatpak { reference, commit } = require(candidate)? else {
return Err(ExecError::operation(
"install",
"artifact",
"wrong Flatpak artifact type",
));
};
// OSTree pulls the approved commit and verifies the configured remote's
// signatures. Flatpak then deploys the local ref without following HEAD.
crate::portage::chroot(
exec,
root,
"flatpak",
&[
"install",
"--system",
"--noninteractive",
"-y",
"--no-deploy",
"--",
"flathub",
reference,
],
)?;
crate::portage::chroot(
exec,
root,
"ostree",
&[
"--repo=/var/lib/flatpak/repo",
"pull",
"flathub",
&format!("{reference}@{commit}"),
],
)?;
crate::portage::chroot(
exec,
root,
"flatpak",
&[
"install",
"--system",
"--noninteractive",
"-y",
"--no-pull",
"--",
"flathub",
reference,
],
)?;
Ok(format!("installed {reference} at approved commit {commit}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn malformed_artifacts_are_refused() {
assert!(!InstallArtifact::Flatpak {
reference: "app/org.example.App/x86_64/stable".into(),
commit: "0".repeat(64)
}
.validate());
assert!(!InstallArtifact::Nix {
flake: "https://attacker.invalid#tool".into(),
store_path: "/nix/store/x".into()
}
.validate());
assert!(!InstallArtifact::Snap {
revision: "--dangerous".into(),
channel: "stable".into()
}
.validate());
}
}

View file

@ -114,6 +114,7 @@ pub fn save_preferences(exec: &Exec, root: &Path, prefs: &Preferences) -> Result
fn rooted(root: &Path, stage: &str, operation: &str, program: &str, args: &[&str]) -> CommandSpec { fn rooted(root: &Path, stage: &str, operation: &str, program: &str, args: &[&str]) -> CommandSpec {
let owned: Vec<String> = args.iter().map(|s| s.to_string()).collect(); let owned: Vec<String> = args.iter().map(|s| s.to_string()).collect();
crate::actions::target_spec(root, stage, operation, program, &owned) crate::actions::target_spec(root, stage, operation, program, &owned)
.timeout(std::time::Duration::from_secs(120))
} }
/// Snap acts on the running daemon and cannot be redirected to a target. /// Snap acts on the running daemon and cannot be redirected to a target.
@ -855,6 +856,9 @@ pub fn candidate_detail(exec: &Exec, root: &Path, backend: Backend, id: &str) ->
if backend == Backend::Flatpak && candidate.available { if backend == Backend::Flatpak && candidate.available {
inspect_flatpak_remote(exec, root, &mut candidate); inspect_flatpak_remote(exec, root, &mut candidate);
} }
if candidate.available && candidate.verified {
crate::artifact::resolve(exec, root, &mut candidate);
}
let health = candidate_health(&candidate); let health = candidate_health(&candidate);
CandidateDetail { CandidateDetail {

View file

@ -5,9 +5,12 @@
//! privileged logic be tested with a fake runner without touching the system. //! privileged logic be tested with a fake runner without touching the system.
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{BufRead, Read, Write}; use std::io::{Read, Write};
use std::os::fd::AsFd;
use std::os::unix::process::CommandExt;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -27,6 +30,16 @@ pub struct CommandSpec {
/// Forward this command's live output to the human-facing sink, when one /// Forward this command's live output to the human-facing sink, when one
/// is configured. The structured logger still receives every command. /// is configured. The structured logger still receives every command.
pub show_output: bool, pub show_output: bool,
/// Suppress all output sinks and error-body logging (provider bodies may
/// contain credentials unknown to our secret registry).
pub sensitive_output: bool,
/// Bound captured output per stream; streaming builds retain only a tail.
pub max_output_bytes: usize,
/// Wall-clock bound including blocked stdin and inherited output pipes.
pub timeout: Duration,
/// An open mutation-lock lease inherited by the command. Its own lifetime
/// keeps exclusion intact if the approving broker crashes first.
pub(crate) mutation_lease: Option<Arc<std::fs::File>>,
} }
impl CommandSpec { impl CommandSpec {
@ -44,6 +57,10 @@ impl CommandSpec {
stage: stage.into(), stage: stage.into(),
operation: operation.into(), operation: operation.into(),
show_output: false, show_output: false,
sensitive_output: false,
max_output_bytes: 16 * 1024 * 1024,
timeout: Duration::from_secs(24 * 60 * 60),
mutation_lease: None,
} }
} }
@ -100,6 +117,18 @@ impl CommandSpec {
self self
} }
pub fn sensitive_output(mut self) -> Self {
self.sensitive_output = true;
self
}
pub fn output_limit(mut self, bytes: usize) -> Self {
self.max_output_bytes = bytes.max(1);
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn show_output(mut self) -> Self { pub fn show_output(mut self) -> Self {
self.show_output = true; self.show_output = true;
self self
@ -197,6 +226,12 @@ impl ProcessRunner for RealRunner {
let start = Instant::now(); let start = Instant::now();
let mut command = Command::new(&spec.program); let mut command = Command::new(&spec.program);
command.args(&spec.args); command.args(&spec.args);
// setsid must start as a non-leader so it keeps the spawned PID as
// its session/process-group ID. Pre-grouping it would force a fork
// and leave its real worker outside our deadline kill group.
if !(spec.program == "setsid" && spec.args.first().is_some_and(|a| a == "--wait")) {
command.process_group(0);
}
for (k, v) in &spec.env { for (k, v) in &spec.env {
command.env(k, v); command.env(k, v);
} }
@ -211,97 +246,199 @@ impl ProcessRunner for RealRunner {
Stdio::null() Stdio::null()
}); });
let mut child = command.spawn().map_err(|source| ExecError::Spawn { if let Some(lease) = &spec.mutation_lease {
rustix::io::fcntl_setfd(lease.as_ref(), rustix::io::FdFlags::empty())
.map_err(|e| ExecError::operation(&spec.stage, &spec.operation, e.to_string()))?;
}
let spawned = command.spawn();
if let Some(lease) = &spec.mutation_lease {
// Descriptor flags are per process: restoring CLOEXEC here does
// not close or change the copy inherited by the worker.
rustix::io::fcntl_setfd(lease.as_ref(), rustix::io::FdFlags::CLOEXEC)
.map_err(|e| ExecError::operation(&spec.stage, &spec.operation, e.to_string()))?;
}
let mut child = spawned.map_err(|source| ExecError::Spawn {
program: spec.program.clone(), program: spec.program.clone(),
source, source,
})?; })?;
if let Some(bytes) = &spec.stdin { // Drain output while writing stdin: a child may fill stdout before it
if let Some(mut stdin) = child.stdin.take() { // consumes its request. Nonblocking pipes also bound cleanup when a
// Ignore broken-pipe style errors; the process exit status is // descendant inherits a pipe and outlives its parent.
// the source of truth. let stdout_pipe = child.stdout.take();
let _ = stdin.write_all(bytes); let stderr_pipe = child.stderr.take();
let _ = stdin.flush(); let stdin_pipe = child.stdin.take();
let stop = AtomicBool::new(false);
let exceeded = AtomicBool::new(false);
let log = (!spec.sensitive_output)
.then_some(self.sink.as_ref())
.flatten();
let user = (spec.show_output && !spec.sensitive_output)
.then_some(self.user_sink.as_ref())
.flatten();
let (status, stdout, stderr, timed_out) = std::thread::scope(|scope| {
let out = scope.spawn(|| drain_output(stdout_pipe, log, user, spec, &stop, &exceeded));
let err = scope.spawn(|| drain_output(stderr_pipe, log, user, spec, &stop, &exceeded));
let input = scope.spawn(|| -> std::io::Result<()> {
if let (Some(mut pipe), Some(bytes)) = (stdin_pipe, spec.stdin.as_ref()) {
nonblocking(&pipe)?;
let mut written = 0;
while written < bytes.len() && !stop.load(Ordering::Relaxed) {
match pipe.write(&bytes[written..]) {
Ok(0) => break,
Ok(n) => written += n,
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
std::thread::sleep(Duration::from_millis(10))
}
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => break,
Err(e) => return Err(e),
} }
} }
}
let mut stdout_pipe = child.stdout.take(); Ok(())
let mut stderr_pipe = child.stderr.take();
let (stdout, stderr) = std::thread::scope(|s| {
let out_handle = s.spawn(|| {
stdout_pipe.take().map_or_else(Vec::new, |pipe| {
drain_output(
pipe,
self.sink.as_ref(),
spec.show_output
.then_some(&self.user_sink)
.and_then(Option::as_ref),
)
})
}); });
let mut timed_out = false;
let err_handle = s.spawn(|| { let status = loop {
stderr_pipe.take().map_or_else(Vec::new, |pipe| { if start.elapsed() >= spec.timeout || exceeded.load(Ordering::Relaxed) {
drain_output( timed_out = start.elapsed() >= spec.timeout;
pipe, if let Some(pid) = rustix::process::Pid::from_raw(child.id() as i32) {
self.sink.as_ref(), let _ =
spec.show_output rustix::process::kill_process_group(pid, rustix::process::Signal::KILL);
.then_some(&self.user_sink) }
.and_then(Option::as_ref), let _ = child.kill();
) break child.wait();
}) }
}); match child.try_wait() {
Ok(Some(status)) => break Ok(status),
let stdout = out_handle.join().map_err(|_| { Ok(None) => std::thread::sleep(Duration::from_millis(10)),
ExecError::operation(&spec.stage, &spec.operation, "stdout reader stopped") Err(e) => {
let _ = child.kill();
let _ = child.wait();
break Err(e);
}
}
};
// Commands are synchronous. Do not leave pipe-inheriting workers
// running after their leader exits, including on successful exit.
if let Some(pid) = rustix::process::Pid::from_raw(child.id() as i32) {
let _ = rustix::process::kill_process_group(pid, rustix::process::Signal::KILL);
}
stop.store(true, Ordering::Relaxed);
let join_err = || {
ExecError::operation(&spec.stage, &spec.operation, "command pipe worker stopped")
};
input
.join()
.map_err(|_| join_err())?
.map_err(|e| ExecError::operation(&spec.stage, &spec.operation, e.to_string()))?;
let stdout = out.join().map_err(|_| join_err())??;
let stderr = err.join().map_err(|_| join_err())??;
Ok::<_, ExecError>((status, stdout, stderr, timed_out))
})?; })?;
let stderr = err_handle.join().map_err(|_| { let status = status.map_err(|source| ExecError::Spawn {
ExecError::operation(&spec.stage, &spec.operation, "stderr reader stopped")
})?;
Ok::<_, ExecError>((stdout, stderr))
})?;
let status = child.wait().map_err(|source| ExecError::Spawn {
program: spec.program.clone(), program: spec.program.clone(),
source, source,
})?; })?;
let duration = start.elapsed(); if timed_out || exceeded.load(Ordering::Relaxed) {
let code = status.code().unwrap_or(-1); return Err(ExecError::operation(
&spec.stage,
&spec.operation,
if timed_out {
"command exceeded its wall-clock deadline; process group stopped"
} else {
"command output exceeded its capture limit; process group stopped"
},
));
}
Ok(CommandOutput { Ok(CommandOutput {
status: code, status: status.code().unwrap_or(-1),
success: status.success(), success: status.success(),
stdout, stdout,
stderr, stderr,
duration, duration: start.elapsed(),
}) })
} }
} }
/// Preserve complete captured output while delivering newline and carriage fn nonblocking(pipe: &impl AsFd) -> std::io::Result<()> {
/// return updates as they arrive (Flatpak and curl use CR progress meters). let flags = rustix::fs::fcntl_getfl(pipe)?;
fn drain_output(pipe: impl Read, log: Option<&StreamSink>, user: Option<&StreamSink>) -> Vec<u8> { rustix::fs::fcntl_setfl(pipe, flags | rustix::fs::OFlags::NONBLOCK)?;
let mut reader = std::io::BufReader::new(pipe); Ok(())
let mut captured = Vec::new(); }
fn drain_output<P: Read + AsFd>(
pipe: Option<P>,
log: Option<&StreamSink>,
user: Option<&StreamSink>,
spec: &CommandSpec,
stop: &AtomicBool,
exceeded: &AtomicBool,
) -> Result<Vec<u8>> {
let Some(mut pipe) = pipe else {
return Ok(Vec::new());
};
nonblocking(&pipe)
.map_err(|e| ExecError::operation(&spec.stage, &spec.operation, e.to_string()))?;
let mut captured = std::collections::VecDeque::new();
let mut pending = Vec::new(); let mut pending = Vec::new();
while let Ok(bytes) = reader.fill_buf() { let mut skipping_line = false;
if bytes.is_empty() { let mut logged = 0usize;
let mut chunk = [0u8; 8192];
loop {
let count = match pipe.read(&mut chunk) {
Ok(0) => break,
Ok(n) => n,
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
if stop.load(Ordering::Relaxed) {
break; break;
} }
let boundary = bytes.iter().position(|b| *b == b'\n' || *b == b'\r'); std::thread::sleep(Duration::from_millis(10));
let count = boundary.map_or(bytes.len(), |index| index + 1); continue;
captured.extend_from_slice(&bytes[..count]); }
pending.extend_from_slice(&bytes[..count]); Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
reader.consume(count); Err(e) => {
if boundary.is_some() { return Err(ExecError::operation(
&spec.stage,
&spec.operation,
e.to_string(),
))
}
};
if !spec.show_output && captured.len().saturating_add(count) > spec.max_output_bytes {
exceeded.store(true, Ordering::Relaxed);
return Ok(Vec::new());
}
captured.extend(&chunk[..count]);
if captured.len() > spec.max_output_bytes {
captured.drain(..captured.len() - spec.max_output_bytes);
}
if logged >= spec.max_output_bytes {
continue;
}
for byte in &chunk[..count] {
if !skipping_line {
pending.push(*byte);
}
if *byte == b'\n' || *byte == b'\r' {
if !skipping_line && logged.saturating_add(pending.len()) <= spec.max_output_bytes {
emit_chunk(&pending, log, user); emit_chunk(&pending, log, user);
logged += pending.len();
}
pending.clear(); pending.clear();
skipping_line = false;
} else if pending.len() >= 64 * 1024 {
// Do not split arbitrarily: that could split a registered secret
// across redaction calls. Discard an oversized log line instead.
pending.clear();
skipping_line = true;
} }
} }
if !pending.is_empty() { }
if !pending.is_empty() && logged.saturating_add(pending.len()) <= spec.max_output_bytes {
emit_chunk(&pending, log, user); emit_chunk(&pending, log, user);
} }
captured Ok(captured.into_iter().collect())
} }
fn emit_chunk(bytes: &[u8], log: Option<&StreamSink>, user: Option<&StreamSink>) { fn emit_chunk(bytes: &[u8], log: Option<&StreamSink>, user: Option<&StreamSink>) {
@ -475,4 +612,68 @@ mod tests {
assert_eq!(result.stdout_lossy(), "building\rinstalled\n"); assert_eq!(result.stdout_lossy(), "building\rinstalled\n");
assert_eq!(*shown.lock().unwrap(), "building\rinstalled\n"); assert_eq!(*shown.lock().unwrap(), "building\rinstalled\n");
} }
#[test]
fn sensitive_output_never_reaches_either_sink() {
let seen = Arc::new(Mutex::new(String::new()));
let sink = seen.clone();
let runner = RealRunner::with_sinks(
Arc::new(move |s| sink.lock().unwrap().push_str(s)),
Arc::new(move |_| panic!("sensitive output forwarded")),
);
let out = runner
.execute(
&CommandSpec::new("test", "secret", "printf")
.arg("unregistered-provider-credential\n")
.show_output()
.sensitive_output(),
)
.unwrap();
assert!(out
.stdout_lossy()
.contains("unregistered-provider-credential"));
assert!(seen.lock().unwrap().is_empty());
}
#[test]
fn output_limits_fail_queries_and_bound_streaming_tails() {
let runner = RealRunner::new();
let spec = CommandSpec::new("test", "output", "python3")
.args(["-c", "import sys; sys.stdout.write('x'*100000)"])
.output_limit(1024)
.timeout(Duration::from_secs(5));
assert!(runner
.execute(&spec)
.unwrap_err()
.to_string()
.contains("capture limit"));
let out = runner.execute(&spec.show_output()).unwrap();
assert_eq!(out.stdout.len(), 1024);
}
#[test]
fn drains_output_before_large_stdin_and_bounds_blocked_input() {
let runner = RealRunner::new();
let spec = CommandSpec::new("test", "duplex", "python3")
.args(["-c", "import sys; sys.stdout.write('x'*200000); sys.stdout.flush(); sys.stdin.buffer.read()"])
.stdin(vec![b'y'; 200000]).timeout(Duration::from_secs(5));
assert_eq!(runner.execute(&spec).unwrap().stdout.len(), 200000);
let blocked = CommandSpec::new("test", "deadline", "python3")
.args(["-c", "import time; time.sleep(30)"])
.stdin(vec![b'y'; 200000])
.timeout(Duration::from_millis(100));
let start = Instant::now();
assert!(runner
.execute(&blocked)
.unwrap_err()
.to_string()
.contains("deadline"));
assert!(start.elapsed() < Duration::from_secs(3));
let start = Instant::now();
assert!(runner
.execute(&blocked.detached())
.unwrap_err()
.to_string()
.contains("deadline"));
assert!(start.elapsed() < Duration::from_secs(3));
}
} }

View file

@ -62,6 +62,7 @@ pub fn install(
); );
if config.desktop.flatpak { if config.desktop.flatpak {
packages.push("sys-apps/flatpak".to_string()); packages.push("sys-apps/flatpak".to_string());
packages.push("dev-util/ostree".to_string());
} }
exec.logger.info(format!( exec.logger.info(format!(
"installing desktop profile '{}' ({} packages)", "installing desktop profile '{}' ({} packages)",

View file

@ -0,0 +1,144 @@
//! The sole model-facing evidence boundary. Command logs are not evidence;
//! service inspection uses enumerated properties and never journal/argv data.
use crate::{actions, command::CommandSpec, exec::Exec};
use phoebus_core::action::SystemAction;
use std::path::Path;
pub const MAX_EVIDENCE_BYTES: usize = 64 * 1024;
pub fn minimize(exec: &Exec, text: &str) -> String {
let redacted = exec.redactor().redact(text);
let mut output = String::new();
for line in redacted.lines() {
if phoebus_ai::privacy::is_forbidden_path(line)
|| line.contains("/home/")
|| line.contains("/root/")
{
output.push_str("[private path omitted]\n");
} else {
output.push_str(line);
output.push('\n');
}
if output.len() >= MAX_EVIDENCE_BYTES {
break;
}
}
if output.len() > MAX_EVIDENCE_BYTES {
let mut end = MAX_EVIDENCE_BYTES;
while !output.is_char_boundary(end) {
end -= 1;
}
output.truncate(end);
output.push_str("\n[evidence truncated]");
}
output
}
pub fn investigate(exec: &Exec, action: &SystemAction, root: &Path) -> String {
if action.is_mutating() {
return "investigation refused: mutating action".into();
}
if let Err(e) = action.validate() {
return format!("investigation refused: {e}");
}
let result = if let SystemAction::InspectService { name } = action {
if root != Path::new("/") {
return "investigation refused: service state belongs to the running system".into();
}
exec.run(
CommandSpec::new("evidence", "inspect service properties", "systemctl")
.args([
"show",
"--no-pager",
"--property=LoadState,ActiveState,SubState,UnitFileState,Result",
"--",
name,
])
.sensitive_output()
.output_limit(MAX_EVIDENCE_BYTES)
.timeout(std::time::Duration::from_secs(30)),
)
.map(|out| out.stdout_lossy())
} else {
actions::execute(exec, action, None, root).map(|out| out.output)
};
minimize(
exec,
&match result {
Ok(out) => out,
Err(e) => format!("investigation failed: {e}"),
},
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
command::{CommandOutput, RecordingRunner},
log::Logger,
};
use phoebus_core::{Redactor, SecretString};
#[test]
fn registered_secrets_private_paths_and_unicode_are_filtered() {
let dir = tempfile::tempdir().unwrap();
let exec = Exec::new(
Box::new(RecordingRunner::new()),
Logger::open(dir.path().join("log"), Redactor::new(), false),
false,
"/",
dir.path(),
);
exec.register_secret(&SecretString::new("synthetic-token"));
let text = minimize(
&exec,
&format!(
"synthetic-token\n/home/test/private\n{}",
"é".repeat(MAX_EVIDENCE_BYTES)
),
);
assert!(!text.contains("synthetic-token"));
assert!(!text.contains("/home/test/private"));
assert!(text.len() <= MAX_EVIDENCE_BYTES + 32);
}
#[test]
fn model_service_evidence_never_requests_journal_or_command_lines() {
let dir = tempfile::tempdir().unwrap();
let runner = RecordingRunner::new();
runner.set_response(
"systemctl",
CommandOutput {
status: 0,
success: true,
stdout: b"ActiveState=active\n".to_vec(),
stderr: vec![],
duration: std::time::Duration::ZERO,
},
);
let exec = Exec::new(
Box::new(runner.clone()),
Logger::open(dir.path().join("log"), Redactor::new(), false),
false,
"/",
dir.path(),
);
assert!(investigate(
&exec,
&SystemAction::InspectService {
name: "sshd".into()
},
Path::new("/")
)
.contains("ActiveState=active"));
let spec = &runner.recorded()[0];
assert_eq!(spec.args[0], "show");
assert!(spec.sensitive_output);
assert!(!spec
.args
.iter()
.any(|a| a.contains("ExecStart") || a == "status"));
}
}

View file

@ -1,7 +1,7 @@
//! Execution context: the single place commands are run and logged. //! Execution context: the single place commands are run and logged.
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use phoebus_core::{Redactor, SecretString, TargetRoot}; use phoebus_core::{Redactor, SecretString, TargetRoot};
@ -36,6 +36,19 @@ pub struct Exec {
pub root: PathBuf, pub root: PathBuf,
pub state_dir: PathBuf, pub state_dir: PathBuf,
progress: Option<ProgressSink>, progress: Option<ProgressSink>,
mutation_owner: Mutex<
Option<(
PathBuf,
std::thread::ThreadId,
std::sync::Weak<crate::system_lock::SystemLock>,
)>,
>,
}
pub struct MutationGuard {
_lock: Arc<crate::system_lock::SystemLock>,
// Reentrancy is thread-scoped; ownership must not migrate to another thread.
_thread: std::marker::PhantomData<std::rc::Rc<()>>,
} }
pub struct ProgressPhase<'a> { pub struct ProgressPhase<'a> {
@ -77,6 +90,7 @@ impl Exec {
root: root.into(), root: root.into(),
state_dir: state_dir.into(), state_dir: state_dir.into(),
progress: None, progress: None,
mutation_owner: Mutex::new(None),
} }
} }
@ -85,6 +99,39 @@ impl Exec {
self self
} }
/// A whole transaction holds this guard. Nested actions on the same thread
/// reuse its lock; another thread or process still has to acquire the inode.
pub fn lock_mutation(&self, root: &Path, operation: &str) -> Result<MutationGuard> {
let mut owner = self
.mutation_owner
.lock()
.map_err(|_| ExecError::operation("sys", "lock", "mutation owner state unavailable"))?;
let thread = std::thread::current().id();
if let Some((path, id, weak)) = owner.as_ref() {
if let Some(lock) = weak.upgrade() {
if path != root || *id != thread {
return Err(ExecError::operation(
"sys",
"lock",
"another mutation already owns this executor",
));
}
return Ok(MutationGuard {
_lock: lock,
_thread: std::marker::PhantomData,
});
}
}
let lock = Arc::new(crate::system_lock::SystemLock::acquire(
self, root, operation,
)?);
*owner = Some((root.to_path_buf(), thread, Arc::downgrade(&lock)));
Ok(MutationGuard {
_lock: lock,
_thread: std::marker::PhantomData,
})
}
fn report(&self, event: ProgressEvent) { fn report(&self, event: ProgressEvent) {
if let Some(sink) = &self.progress { if let Some(sink) = &self.progress {
sink(event); sink(event);
@ -126,12 +173,24 @@ impl Exec {
} }
/// Run a command, honouring dry-run, and log it with its exit status. /// Run a command, honouring dry-run, and log it with its exit status.
pub fn run(&self, spec: CommandSpec) -> Result<CommandOutput> { pub fn run(&self, mut spec: CommandSpec) -> Result<CommandOutput> {
if self.dry_run { if self.dry_run {
self.logger self.logger
.dry_run(&spec.stage, &spec.operation, &spec.program, &spec.args); .dry_run(&spec.stage, &spec.operation, &spec.program, &spec.args);
return Ok(CommandOutput::dry_run()); return Ok(CommandOutput::dry_run());
} }
if self.is_real() {
let owner = self.mutation_owner.lock().map_err(|_| {
ExecError::operation("sys", "lease mutation", "mutation state unavailable")
})?;
if let Some((_, thread, weak)) = owner.as_ref() {
if *thread == std::thread::current().id() {
if let Some(lock) = weak.upgrade() {
spec.mutation_lease = Some(Arc::new(lock.worker_lease()?));
}
}
}
}
let stdin_hidden = spec.stdin.is_some(); let stdin_hidden = spec.stdin.is_some();
self.logger.command_start( self.logger.command_start(
&spec.stage, &spec.stage,
@ -167,7 +226,7 @@ impl Exec {
duration: output.duration, duration: output.duration,
stdin_hidden, stdin_hidden,
}); });
if !self.runner.is_streaming() { if !self.runner.is_streaming() && !spec.sensitive_output {
if !output.stdout.is_empty() { if !output.stdout.is_empty() {
self.logger.output(&output.stdout); self.logger.output(&output.stdout);
} }
@ -181,8 +240,16 @@ impl Exec {
operation: spec.operation.clone(), operation: spec.operation.clone(),
program: spec.program.clone(), program: spec.program.clone(),
status: output.status, status: output.status,
stdout: self.redactor().redact(&output.stdout_lossy()), stdout: if spec.sensitive_output {
stderr: self.redactor().redact(&output.stderr_lossy()), "[sensitive output withheld]".into()
} else {
self.redactor().redact(&output.stdout_lossy())
},
stderr: if spec.sensitive_output {
"[sensitive output withheld]".into()
} else {
self.redactor().redact(&output.stderr_lossy())
},
})); }));
} }
Ok(output) Ok(output)
@ -413,4 +480,21 @@ mod tests {
assert!(find_in_path("true").is_some()); assert!(find_in_path("true").is_some());
assert!(find_in_path("definitely-not-a-real-tool-xyz").is_none()); assert!(find_in_path("definitely-not-a-real-tool-xyz").is_none());
} }
#[test]
fn nested_mutation_guards_retain_the_kernel_lock_until_the_last_drop() {
let dir = tempfile::tempdir().unwrap();
let e = exec(RecordingRunner::new());
let outer = e.lock_mutation(dir.path(), "whole plan").unwrap();
let inner = e.lock_mutation(dir.path(), "nested action").unwrap();
drop(outer);
assert!(crate::system_lock::SystemLock::acquire(&e, dir.path(), "competitor").is_err());
std::thread::scope(|scope| {
scope
.spawn(|| assert!(e.lock_mutation(dir.path(), "other thread").is_err()))
.join()
.unwrap();
});
drop(inner);
assert!(e.lock_mutation(dir.path(), "next plan").is_ok());
}
} }

View file

@ -212,7 +212,16 @@ pub fn action_for(_exec: &Exec, root: &Path, candidate: &Candidate) -> Result<Sy
match candidate.backend { match candidate.backend {
Backend::PortageSource | Backend::PortageBinary | Backend::PortageVendor => { Backend::PortageSource | Backend::PortageBinary | Backend::PortageVendor => {
Ok(SystemAction::InstallPackage { Ok(SystemAction::InstallPackage {
atom: candidate.id.clone(), atom: match candidate.version.as_deref() {
Some(version) => format!("={}-{}", candidate.id, version),
None => {
return Err(ExecError::operation(
"install",
"pin Portage version",
"reviewed package has no exact version",
))
}
},
binary: Some(candidate.backend == Backend::PortageBinary), binary: Some(candidate.backend == Backend::PortageBinary),
}) })
} }
@ -315,6 +324,10 @@ pub fn approval_digest_for(
hasher.update(candidate.provenance.as_bytes()); hasher.update(candidate.provenance.as_bytes());
hasher.update(b"\0"); hasher.update(b"\0");
hasher.update([u8::from(candidate.verified)]); hasher.update([u8::from(candidate.verified)]);
if let Some(pin) = &candidate.artifact {
// Closed enum of strings; serialization cannot fail.
hasher.update(format!("{pin:?}").as_bytes());
}
hasher.update(b"\0"); hasher.update(b"\0");
hasher.update(serde_json::to_string(action).unwrap_or_default().as_bytes()); hasher.update(serde_json::to_string(action).unwrap_or_default().as_bytes());
hasher.update(b"\0"); hasher.update(b"\0");
@ -382,6 +395,7 @@ pub fn revalidate(exec: &Exec, root: &Path, resolved: &ResolvedInstall) -> Resul
} }
if current.provenance != resolved.candidate.provenance if current.provenance != resolved.candidate.provenance
|| current.verified != resolved.candidate.verified || current.verified != resolved.candidate.verified
|| current.artifact != resolved.candidate.artifact
{ {
return Err(ExecError::operation( return Err(ExecError::operation(
"install", "install",
@ -724,10 +738,7 @@ impl InstallSession for ExecutorSession<'_> {
requests requests
.iter() .iter()
.map(|request| { .map(|request| {
let output = match actions::execute(self.exec, request, None, self.root) { let output = crate::evidence::investigate(self.exec, request, self.root);
Ok(outcome) => outcome.output,
Err(e) => format!("investigation failed: {e}"),
};
Evidence { Evidence {
request: request.describe(), request: request.describe(),
output, output,
@ -924,11 +935,11 @@ pub fn verify_installed(exec: &Exec, root: &Path, resolved: &ResolvedInstall) ->
match c.backend { match c.backend {
Backend::PortageSource | Backend::PortageBinary | Backend::PortageVendor => { Backend::PortageSource | Backend::PortageBinary | Backend::PortageVendor => {
let inspection = chooser::installed_copies(exec, root, &resolved.request.application); let inspection = chooser::installed_copies(exec, root, &resolved.request.application);
if let Some(copy) = inspection if let Some(copy) = inspection.copies.iter().find(|x| {
.copies x.backend == Backend::PortageSource.as_str()
.iter() && x.id == c.id
.find(|x| x.backend == Backend::PortageSource.as_str()) && c.version.as_deref() == Some(x.version.as_str())
{ }) {
return Ok(format!( return Ok(format!(
"verified: {} {} is in the Portage package database", "verified: {} {} is in the Portage package database",
copy.id, copy.version copy.id, copy.version
@ -944,14 +955,49 @@ pub fn verify_installed(exec: &Exec, root: &Path, resolved: &ResolvedInstall) ->
)) ))
} }
Backend::Flatpak => { Backend::Flatpak => {
let installed = exec.runner.execute(&phoebus_executor_command( let phoebus_core::selection::InstallArtifact::Flatpak { reference, commit } =
"flatpak", crate::artifact::require(c)?
&["list", "--app", "--columns=application"], else {
return Err(ExecError::operation(
"install",
"verify",
"wrong Flatpak artifact type",
)); ));
if installed };
.map(|o| o.stdout_lossy().lines().any(|l| l.trim() == c.id)) let spec = if root == Path::new("/") {
.unwrap_or(false) phoebus_executor_command(
{ "flatpak",
&[
"info",
"--system",
"--show-ref",
"--show-commit",
"--",
&c.id,
],
)
} else {
phoebus_executor_command(
"chroot",
&[
&root.to_string_lossy(),
"flatpak",
"info",
"--system",
"--show-ref",
"--show-commit",
"--",
&c.id,
],
)
};
if exec.runner.execute(&spec).is_ok_and(|o| {
o.success && {
let output = o.stdout_lossy();
let fields: Vec<_> = output.split_whitespace().collect();
fields.contains(&reference.as_str()) && fields.contains(&commit.as_str())
}
}) {
Ok(format!("verified: Flatpak app {} is installed", c.id)) Ok(format!("verified: Flatpak app {} is installed", c.id))
} else { } else {
Err(ExecError::operation( Err(ExecError::operation(
@ -963,7 +1009,22 @@ pub fn verify_installed(exec: &Exec, root: &Path, resolved: &ResolvedInstall) ->
} }
Backend::Snap => { Backend::Snap => {
let entries = crate::snap::list(exec)?; let entries = crate::snap::list(exec)?;
if entries.iter().any(|e| e.name == c.id) { let phoebus_core::selection::InstallArtifact::Snap { revision, channel } =
crate::artifact::require(c)?
else {
return Err(ExecError::operation(
"install",
"verify",
"wrong Snap artifact type",
));
};
if entries.iter().any(|e| {
e.name == c.id
&& e.revision == *revision
&& c.version.as_deref() == Some(e.version.as_str())
&& (e.channel == *channel
|| e.channel == "stable" && channel == "latest/stable")
}) {
Ok(format!("verified: snap {} is installed", c.id)) Ok(format!("verified: snap {} is installed", c.id))
} else { } else {
Err(ExecError::operation( Err(ExecError::operation(
@ -974,16 +1035,54 @@ pub fn verify_installed(exec: &Exec, root: &Path, resolved: &ResolvedInstall) ->
} }
} }
Backend::AppImage => { Backend::AppImage => {
crate::appimage::info(root, &c.id)?; let record = crate::appimage::info(root, &c.id)?;
let binary = root
.join(crate::appimage::APPIMAGE_DIR)
.join(record.binary_name());
if c.version.as_deref() != Some(record.version.as_str())
|| !crate::opencode::checksum_ok(&binary, &record.sha256)?
{
return Err(ExecError::operation(
"install",
"verify",
"managed AppImage bytes or version differ from the reviewed record",
));
}
Ok(format!("verified: managed AppImage {} has a record", c.id)) Ok(format!("verified: managed AppImage {} has a record", c.id))
} }
Backend::Nix => crate::nix::verify_profile_install(root, &c.id), Backend::Nix => {
let phoebus_core::selection::InstallArtifact::Nix { store_path, .. } =
crate::artifact::require(c)?
else {
return Err(ExecError::operation(
"install",
"verify",
"wrong Nix artifact type",
));
};
if !crate::nix::installed_packages(root)
.packages
.iter()
.any(|p| p.profile == "profile" && p.store_path == *store_path)
{
return Err(ExecError::operation(
"install",
"verify",
"active Nix profile does not contain the reviewed store path",
));
}
crate::nix::verify_profile_install(root, &c.id)
}
Backend::CustomEbuild => { Backend::CustomEbuild => {
if phoebus_core::action::valid_ebuild_atom(&c.id) if phoebus_core::action::valid_ebuild_atom(&c.id)
&& chooser::installed_copies(exec, root, &resolved.request.application) && chooser::installed_copies(exec, root, &resolved.request.application)
.copies .copies
.iter() .iter()
.any(|x| x.backend == Backend::PortageSource.as_str()) .any(|x| {
x.backend == Backend::PortageSource.as_str()
&& x.id == c.id
&& c.version.as_deref() == Some(x.version.as_str())
})
{ {
Ok(format!("verified: custom ebuild {} is installed", c.id)) Ok(format!("verified: custom ebuild {} is installed", c.id))
} else { } else {
@ -999,18 +1098,20 @@ pub fn verify_installed(exec: &Exec, root: &Path, resolved: &ResolvedInstall) ->
/// A small helper so verification can run a plain host command. /// A small helper so verification can run a plain host command.
fn phoebus_executor_command(program: &str, args: &[&str]) -> crate::command::CommandSpec { fn phoebus_executor_command(program: &str, args: &[&str]) -> crate::command::CommandSpec {
crate::command::CommandSpec::new("install", "verify", program).args(args.iter().copied()) crate::command::CommandSpec::new("install", "verify", program)
.args(args.iter().copied())
.timeout(std::time::Duration::from_secs(120))
} }
fn verification_plan(candidate: &Candidate) -> String { fn verification_plan(candidate: &Candidate) -> String {
match candidate.backend { match candidate.backend {
Backend::PortageSource | Backend::PortageBinary | Backend::PortageVendor => { Backend::PortageSource | Backend::PortageBinary | Backend::PortageVendor => {
"the Portage package database will be checked for the package after the merge".into() "the Portage package database must report the reviewed package identity and version after the merge".into()
} }
Backend::Flatpak => "`flatpak list` will be checked for the application id".into(), Backend::Flatpak => "Flatpak must report the reviewed full ref and commit".into(),
Backend::Snap => "`snap list` will be checked for the snap".into(), Backend::Snap => "snapd must report the reviewed name, revision, version and channel".into(),
Backend::AppImage => "the managed record and file will be checked".into(), Backend::AppImage => "the managed record and file will be checked".into(),
Backend::Nix => "the shared Nix profile and its commands will be checked".into(), Backend::Nix => "the active Nix profile must contain the reviewed immutable store path and commands".into(),
Backend::CustomEbuild => { Backend::CustomEbuild => {
"the Portage package database will be checked for the custom atom".into() "the Portage package database will be checked for the custom atom".into()
} }
@ -1058,7 +1159,7 @@ mod tests {
assert_eq!(resolved.source, InstallSource::ExplicitBackend); assert_eq!(resolved.source, InstallSource::ExplicitBackend);
assert!(matches!( assert!(matches!(
resolved.action, resolved.action,
SystemAction::InstallPackage { ref atom, .. } if atom == "app-editors/vim" SystemAction::InstallPackage { ref atom, .. } if atom == "=app-editors/vim-9.1.0"
)); ));
let _ = root; let _ = root;
} }
@ -1210,6 +1311,12 @@ mod tests {
} else { } else {
Ok(ok(format!("org.mozilla.firefox {version}\n"))) Ok(ok(format!("org.mozilla.firefox {version}\n")))
} }
} else if is_flatpak && spec.args.iter().any(|a| a == "remote-info") {
Ok(ok(if spec.args.iter().any(|a| a == "--show-ref") {
"app/org.mozilla.firefox/x86_64/stable\n".into()
} else {
format!("{}\n", "a".repeat(64))
}))
} else if is_flatpak && spec.args.iter().any(|a| a == "remotes") { } else if is_flatpak && spec.args.iter().any(|a| a == "remotes") {
let options = if self let options = if self
.remote_verified .remote_verified
@ -1495,4 +1602,52 @@ mod tests {
let text = verify_installed(&exec, target.path(), &resolved).unwrap(); let text = verify_installed(&exec, target.path(), &resolved).unwrap();
assert!(text.contains("Portage package database"), "{text}"); assert!(text.contains("Portage package database"), "{text}");
} }
fn verification_fixture(candidate: Candidate, action: SystemAction) -> ResolvedInstall {
ResolvedInstall {
request: InstallRequest::new("vim"),
candidate,
action,
effects: ResolvedPlan::default(),
rationale: vec![],
alternatives: vec![],
source: InstallSource::ExplicitBackend,
warnings: vec![],
verification: String::new(),
}
}
#[test]
fn installed_portage_verification_requires_exact_identity_and_version() {
let dir = tempfile::tempdir().unwrap();
let vdb = dir.path().join("var/db/pkg");
std::fs::create_dir_all(vdb.join("dev-util/vim-9.1.0")).unwrap();
std::fs::create_dir_all(vdb.join("app-editors/vim-9.0.0")).unwrap();
let exec = make_exec(RecordingRunner::new());
let mut candidate = Candidate::new(Backend::PortageSource, "app-editors/vim", "gentoo");
candidate.version = Some("9.1.0".into());
let action = action_for(&exec, dir.path(), &candidate).unwrap();
let resolved = verification_fixture(candidate, action);
assert!(verify_installed(&exec, dir.path(), &resolved).is_err());
std::fs::create_dir_all(vdb.join("app-editors/vim-9.1.0")).unwrap();
assert!(verify_installed(&exec, dir.path(), &resolved).is_ok());
}
#[test]
fn installed_flatpak_verification_refuses_the_right_app_at_the_wrong_commit() {
use phoebus_core::selection::InstallArtifact;
let runner = RecordingRunner::new();
let exec = make_exec(runner.clone());
let mut candidate = Candidate::new(Backend::Flatpak, "org.example.App", "flathub");
let reference = "app/org.example.App/x86_64/stable";
candidate.artifact = Some(InstallArtifact::Flatpak {
reference: reference.into(),
commit: "a".repeat(64),
});
let action = action_for(&exec, Path::new("/"), &candidate).unwrap();
let resolved = verification_fixture(candidate, action);
runner.set_response("flatpak", ok(&format!("{reference}\n{}\n", "b".repeat(64))));
assert!(verify_installed(&exec, Path::new("/"), &resolved).is_err());
runner.set_response("flatpak", ok(&format!("{reference}\n{}\n", "a".repeat(64))));
assert!(verify_installed(&exec, Path::new("/"), &resolved).is_ok());
}
} }

View file

@ -9,6 +9,7 @@ pub mod admin;
pub mod agent_jail; pub mod agent_jail;
pub mod ai; pub mod ai;
pub mod appimage; pub mod appimage;
pub mod artifact;
pub mod auth; pub mod auth;
pub mod boot; pub mod boot;
pub mod boot_assessment; pub mod boot_assessment;
@ -19,6 +20,7 @@ pub mod desktop_config;
pub mod ebuild; pub mod ebuild;
pub mod encryption; pub mod encryption;
pub mod error; pub mod error;
pub mod evidence;
pub mod exec; pub mod exec;
pub mod install; pub mod install;
pub mod kernel; pub mod kernel;

View file

@ -154,6 +154,7 @@ impl HttpClient for CurlHttpClient<'_> {
fn request(&self, request: &HttpRequest) -> Result<HttpResponse, AiError> { fn request(&self, request: &HttpRequest) -> Result<HttpResponse, AiError> {
let mut temp: Option<tempfile::NamedTempFile> = None; let mut temp: Option<tempfile::NamedTempFile> = None;
let data_line = if let Some(body) = &request.body { let data_line = if let Some(body) = &request.body {
let body = self.exec.redactor().redact(body);
if self.sandboxed { if self.sandboxed {
// Inline the body (escaped) into the curl config: the sandbox's // Inline the body (escaped) into the curl config: the sandbox's
// PrivateTmp would hide a temp file created here. // PrivateTmp would hide a temp file created here.
@ -238,18 +239,23 @@ impl HttpClient for CurlHttpClient<'_> {
); );
let out = if self.sandboxed { let out = if self.sandboxed {
crate::sandbox::run( crate::sandbox::run_bounded(
self.exec, self.exec,
"curl", "curl",
&["-sS".into(), "-K".into(), "-".into()], &["--disable".into(), "-sS".into(), "-K".into(), "-".into()],
config.into_bytes(), config.into_bytes(),
request.max_body as usize + 128,
request.timeout.saturating_add(Duration::from_secs(30)),
) )
.map_err(|e| AiError::Provider(format!("transport error: {e}")))? .map_err(|e| AiError::Provider(format!("transport error: {e}")))?
} else { } else {
self.exec self.exec
.run( .run(
CommandSpec::new("ai", "OpenCode request", "curl") CommandSpec::new("ai", "OpenCode request", "curl")
.args(["-sS", "-K", "-"]) .args(["--disable", "-sS", "-K", "-"])
.sensitive_output()
.output_limit(request.max_body as usize + 128)
.timeout(request.timeout.saturating_add(Duration::from_secs(30)))
.stdin(config.into_bytes()), .stdin(config.into_bytes()),
) )
.map_err(|e| AiError::Provider(format!("transport error: {e}")))? .map_err(|e| AiError::Provider(format!("transport error: {e}")))?
@ -476,8 +482,12 @@ pub fn parse_catalog(body: &str) -> Result<Catalog, AiError> {
"provider catalog was not the expected object".into(), "provider catalog was not the expected object".into(),
)); ));
} }
let parsed: ProviderCatalogBody = serde_json::from_value(value) let parsed: ProviderCatalogBody = serde_json::from_value(value).map_err(|e| {
.map_err(|e| AiError::Provider(format!("provider catalog was not usable: {e}")))?; AiError::Provider(format!(
"provider catalog schema was not usable ({:?})",
e.classify()
))
})?;
Ok(parsed.into_catalog()) Ok(parsed.into_catalog())
} }
@ -1257,4 +1267,42 @@ mod tests {
assert_eq!(body["parts"][0]["type"], "text"); assert_eq!(body["parts"][0]["type"], "text");
assert_eq!(body["parts"][0]["text"], "user prompt"); assert_eq!(body["parts"][0]["text"], "user prompt");
} }
#[test]
fn unregistered_catalog_credentials_are_never_logged_or_attached_to_errors() {
let dir = tempfile::tempdir().unwrap();
let runner = RecordingRunner::new();
for success in [true, false] {
runner.set_response(
"curl",
CommandOutput {
status: if success { 0 } else { 22 },
success,
stdout: b"{\"key\":\"synthetic-unregistered-token\"}\n__PHOEBUS_STATUS__200"
.to_vec(),
stderr: b"synthetic-unregistered-token".to_vec(),
duration: std::time::Duration::ZERO,
},
);
let logger = crate::log::Logger::open(dir.path().join("log"), Redactor::new(), false);
let exec = crate::exec::Exec::new(
Box::new(runner.clone()),
logger,
false,
dir.path(),
dir.path(),
);
let result = CurlHttpClient::new(&exec)
.request(&HttpRequest::get("http://127.0.0.1:4096/provider"));
if success {
assert!(result.is_ok());
} else {
assert!(
!format!("{:?}", result.unwrap_err()).contains("synthetic-unregistered-token")
);
}
assert!(!std::fs::read_to_string(dir.path().join("log"))
.unwrap()
.contains("synthetic-unregistered-token"));
}
}
} }

View file

@ -43,25 +43,7 @@ pub fn initiating_uid() -> u32 {
} }
pub fn effective_uid() -> u32 { pub fn effective_uid() -> u32 {
// Read the effective uid from /proc without unsafe or a subprocess. The rustix::process::geteuid().as_raw()
// second field of `Uid:` is the effective uid.
if let Ok(status) = std::fs::read_to_string("/proc/self/status") {
for line in status.lines() {
if let Some(rest) = line.strip_prefix("Uid:") {
if let Some(effective) = rest.split_whitespace().nth(1) {
if let Ok(uid) = effective.parse::<u32>() {
return uid;
}
}
}
}
}
std::process::Command::new("id")
.arg("-u")
.output()
.ok()
.and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok())
.unwrap_or(1)
} }
/// Look up a user's home directory from the passwd database. `$HOME` is not /// Look up a user's home directory from the passwd database. `$HOME` is not

View file

@ -69,7 +69,8 @@ fn capture(
program: &str, program: &str,
args: &[&str], args: &[&str],
) -> std::result::Result<CommandOutput, ExecError> { ) -> std::result::Result<CommandOutput, ExecError> {
exec.runner.execute(&spec(root, program, args)) exec.runner
.execute(&spec(root, program, args).timeout(std::time::Duration::from_secs(120)))
} }
/// Run a mutating command against the intended system. /// Run a mutating command against the intended system.

View file

@ -23,6 +23,9 @@ pub const SANDBOX_PROPERTIES: &[&str] = &[
"DynamicUser=yes", "DynamicUser=yes",
"ProtectSystem=strict", "ProtectSystem=strict",
"ProtectHome=yes", "ProtectHome=yes",
"PrivateUsers=yes",
"ProtectProc=invisible",
"ProcSubset=pid",
"PrivateDevices=yes", "PrivateDevices=yes",
"PrivateTmp=yes", "PrivateTmp=yes",
"NoNewPrivileges=yes", "NoNewPrivileges=yes",
@ -37,25 +40,18 @@ pub const SANDBOX_PROPERTIES: &[&str] = &[
"SystemCallFilter=@system-service", "SystemCallFilter=@system-service",
]; ];
/// The subset of [`SANDBOX_PROPERTIES`] accepted by the **user** manager. /// User-manager process restrictions. Filesystem and PID isolation are
/// /// established explicitly by Bubblewrap: systemd's filesystem settings can
/// `DynamicUser`, `PrivateDevices` and `ProtectKernelModules` are /// imply a user namespace that prevents Bubblewrap from creating its own jail.
/// system-manager-only; `systemd-run --user` rejects them. The remaining set /// There is no fallback to a plain process if namespace creation fails.
/// 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] = &[ pub const USER_SANDBOX_PROPERTIES: &[&str] = &[
"ProtectSystem=strict",
"ProtectHome=yes",
"PrivateTmp=yes",
"NoNewPrivileges=yes", "NoNewPrivileges=yes",
"ProtectKernelTunables=yes", "RestrictNamespaces=user mnt pid ipc uts",
"ProtectControlGroups=yes",
"RestrictNamespaces=yes",
"LockPersonality=yes", "LockPersonality=yes",
"MemoryDenyWriteExecute=yes", "MemoryDenyWriteExecute=yes",
"SystemCallArchitectures=native", "SystemCallArchitectures=native",
"RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6", "RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6",
"SystemCallFilter=@system-service", "SystemCallFilter=@system-service @mount",
]; ];
/// Whether `systemd-run` is available to build the boundary. /// Whether `systemd-run` is available to build the boundary.
@ -81,6 +77,9 @@ fn build_argv(program: &str, args: &[String], user: bool, properties: &[&str]) -
"--pipe".into(), "--pipe".into(),
"--collect".into(), "--collect".into(),
"--service-type=exec".into(), "--service-type=exec".into(),
"--property=WorkingDirectory=/".into(),
"--property=MemoryMax=256M".into(),
"--property=TasksMax=64".into(),
]; ];
if user { if user {
out.push("--user".into()); out.push("--user".into());
@ -88,8 +87,62 @@ fn build_argv(program: &str, args: &[String], user: bool, properties: &[&str]) -
for property in properties { for property in properties {
out.push(format!("--property={property}")); out.push(format!("--property={property}"));
} }
out.extend(
["/usr/bin/env", "-i", "PATH=/usr/bin:/bin", "LANG=C.UTF-8"]
.iter()
.map(|s| s.to_string()),
);
if user {
// User-manager filesystem properties may be silently ineffective on
// older systemd versions. An explicit namespace jail is mandatory.
out.extend(model_jail_argv(program, args));
} else {
out.push(program.to_string()); out.push(program.to_string());
out.extend(args.iter().cloned()); out.extend(args.iter().cloned());
}
out
}
/// Minimal model runtime: no home, host process view, user runtime sockets or
/// inherited environment. The network is shared for remote/loopback providers.
fn model_jail_argv(program: &str, args: &[String]) -> Vec<String> {
let mut out: Vec<String> = [
"/usr/bin/bwrap",
"--die-with-parent",
"--unshare-user",
"--unshare-pid",
"--unshare-ipc",
"--unshare-uts",
"--new-session",
"--cap-drop",
"ALL",
"--clearenv",
"--dev",
"/dev",
"--proc",
"/proc",
"--tmpfs",
"/tmp",
"--chdir",
"/",
"--setenv",
"PATH",
"/usr/bin:/bin",
"--setenv",
"LANG",
"C.UTF-8",
]
.iter()
.map(|s| s.to_string())
.collect();
for dir in ["/usr", "/bin", "/sbin", "/lib", "/lib64", "/etc"] {
if Path::new(dir).exists() {
out.extend(["--ro-bind".into(), dir.into(), dir.into()]);
}
}
out.push("--".into());
out.push(program.into());
out.extend(args.iter().cloned());
out out
} }
@ -112,6 +165,24 @@ pub fn run(
program: &str, program: &str,
args: &[String], args: &[String],
stdin: Vec<u8>, stdin: Vec<u8>,
) -> Result<crate::command::CommandOutput> {
run_bounded(
exec,
program,
args,
stdin,
16 * 1024 * 1024,
std::time::Duration::from_secs(crate::ai::ai_timeout_secs().saturating_add(60)),
)
}
pub fn run_bounded(
exec: &Exec,
program: &str,
args: &[String],
stdin: Vec<u8>,
output_limit: usize,
timeout: std::time::Duration,
) -> Result<crate::command::CommandOutput> { ) -> Result<crate::command::CommandOutput> {
if !available() { if !available() {
return Err(ExecError::MissingTool( return Err(ExecError::MissingTool(
@ -119,14 +190,28 @@ pub fn run(
.into(), .into(),
)); ));
} }
let wrapped = if user_scope() { if user_scope() && !Path::new("/usr/bin/bwrap").is_file() {
return Err(ExecError::MissingTool(
"Bubblewrap (required for the unprivileged model filesystem boundary)".into(),
));
}
let mut wrapped = if user_scope() {
user_argv(program, args) user_argv(program, args)
} else { } else {
argv(program, args) argv(program, args)
}; };
// The manager owns the actual worker, so killing only systemd-run on a
// deadline is insufficient. Bound the service itself as well.
wrapped.insert(
0,
format!("--property=RuntimeMaxSec={}s", timeout.as_secs().max(1)),
);
exec.run( exec.run(
CommandSpec::new("ai", "query model (sandboxed)", "systemd-run") CommandSpec::new("ai", "query model (sandboxed)", "systemd-run")
.args(wrapped) .args(wrapped)
.sensitive_output()
.output_limit(output_limit)
.timeout(timeout)
.stdin(stdin), .stdin(stdin),
) )
} }
@ -162,9 +247,6 @@ mod tests {
let args = user_argv("curl", &["-sS".into(), "-K".into(), "-".into()]); let args = user_argv("curl", &["-sS".into(), "-K".into(), "-".into()]);
assert!(args.iter().any(|a| a == "--user"), "{args:?}"); assert!(args.iter().any(|a| a == "--user"), "{args:?}");
for required in [ for required in [
"ProtectSystem=strict",
"ProtectHome=yes",
"PrivateTmp=yes",
"NoNewPrivileges=yes", "NoNewPrivileges=yes",
"RestrictAddressFamilies=", "RestrictAddressFamilies=",
"SystemCallFilter=@system-service", "SystemCallFilter=@system-service",
@ -174,7 +256,12 @@ mod tests {
"missing {required}: {args:?}" "missing {required}: {args:?}"
); );
} }
// Properties the user manager rejects must never be passed. assert!(args.iter().any(|a| a == "/usr/bin/bwrap"));
assert!(args.iter().any(|a| a == "--unshare-pid"));
assert!(args.iter().any(|a| a == "--clearenv"));
assert!(args.windows(3).any(|w| w == ["--ro-bind", "/etc", "/etc"]));
assert!(!args.iter().any(|a| a == "/home" || a == "/root"));
// System-manager-only properties must never be passed.
for forbidden in [ for forbidden in [
"DynamicUser=yes", "DynamicUser=yes",
"PrivateDevices=yes", "PrivateDevices=yes",

View file

@ -202,8 +202,24 @@ pub fn list(exec: &Exec) -> Result<Vec<SnapEntry>> {
/// Install a snap. Classic confinement must be requested explicitly by the /// Install a snap. Classic confinement must be requested explicitly by the
/// caller and is surfaced in the reviewed plan. /// caller and is surfaced in the reviewed plan.
pub fn install(exec: &Exec, name: &str, channel: Option<&str>, classic: bool) -> Result<String> { pub fn install(exec: &Exec, name: &str, channel: Option<&str>, classic: bool) -> Result<String> {
install_revision(exec, name, channel, classic, None)
}
pub fn install_revision(
exec: &Exec,
name: &str,
channel: Option<&str>,
classic: bool,
revision: Option<&str>,
) -> Result<String> {
let _ = require_ready(exec)?; let _ = require_ready(exec)?;
let mut args = vec!["install".to_string(), name.to_string()]; let mut args = vec!["install".to_string(), name.to_string()];
if let Some(revision) = revision {
if revision.is_empty() || !revision.bytes().all(|b| b.is_ascii_digit()) {
return Err(ExecError::operation("snap", "install", "invalid revision"));
}
args.push(format!("--revision={revision}"));
}
if let Some(channel) = channel { if let Some(channel) = channel {
args.push(format!("--channel={channel}")); args.push(format!("--channel={channel}"));
} }

View file

@ -1,25 +1,16 @@
//! Inter-process system mutation lock. //! Kernel-owned mutation lock. The inode is persistent, acquisition is atomic,
//! //! and process exit releases ownership without PID guessing or stale unlinking.
//! Ensures that only one mutating `sys` action runs at a time, preventing
//! transaction log corruption, Btrfs snapshot timestamp collisions, and
//! conflicting package manager operations.
//!
//! Uses atomic `O_CREAT | O_EXCL` file creation with automatic stale-lock recovery
//! (detects if the owning PID is no longer alive via `/proc/<pid>`).
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use crate::error::{ExecError, Result}; use crate::error::{ExecError, Result};
use crate::exec::Exec; use crate::exec::Exec;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
pub const DEFAULT_LOCK_REL: &str = "run/phoebus/sys.lock"; pub const DEFAULT_LOCK_REL: &str = "run/phoebus/sys.lock";
#[derive(Debug)] #[derive(Debug)]
pub struct SystemLock { pub struct SystemLock {
lock_path: PathBuf, _file: File,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@ -28,7 +19,6 @@ struct LockInfo {
created_unix: u64, created_unix: u64,
operation: String, operation: String,
} }
impl LockInfo { impl LockInfo {
fn serialize(&self) -> String { fn serialize(&self) -> String {
format!( format!(
@ -36,139 +26,72 @@ impl LockInfo {
self.pid, self.created_unix, self.operation self.pid, self.created_unix, self.operation
) )
} }
#[cfg(test)]
fn parse(text: &str) -> Option<Self> { fn parse(text: &str) -> Option<Self> {
let mut pid = None; let value = |key: &str| text.lines().find_map(|line| line.strip_prefix(key));
let mut created_unix = None;
let mut operation = None;
for line in text.lines() {
if let Some(val) = line.strip_prefix("pid=") {
pid = val.trim().parse::<u32>().ok();
} else if let Some(val) = line.strip_prefix("created_unix=") {
created_unix = val.trim().parse::<u64>().ok();
} else if let Some(val) = line.strip_prefix("operation=") {
operation = Some(val.trim().to_string());
}
}
Some(Self { Some(Self {
pid: pid?, pid: value("pid=")?.parse().ok()?,
created_unix: created_unix?, created_unix: value("created_unix=")?.parse().ok()?,
operation: operation.unwrap_or_else(|| "unspecified operation".to_string()), operation: value("operation=")?.into(),
}) })
} }
} }
fn now_unix() -> u64 { fn now_unix() -> u64 {
SystemTime::now() std::time::SystemTime::now()
.duration_since(UNIX_EPOCH) .duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs()) .map(|d| d.as_secs())
.unwrap_or(0) .unwrap_or(0)
} }
fn is_pid_alive(pid: u32) -> bool {
if pid == 0 {
return false;
}
Path::new("/proc").join(pid.to_string()).exists()
}
pub fn lock_path(root: &Path) -> PathBuf { pub fn lock_path(root: &Path) -> PathBuf {
root.join(DEFAULT_LOCK_REL) root.join(DEFAULT_LOCK_REL)
} }
impl SystemLock { impl SystemLock {
/// Attempt to acquire the system mutation lock for `operation`. pub(crate) fn worker_lease(&self) -> Result<File> {
rustix::io::fcntl_dupfd_cloexec(&self._file, 3)
.map(File::from)
.map_err(|e| ExecError::operation("sys", "lease mutation lock", e.to_string()))
}
pub fn acquire(exec: &Exec, root: &Path, operation: &str) -> Result<Self> { pub fn acquire(exec: &Exec, root: &Path, operation: &str) -> Result<Self> {
let path = lock_path(root); let path = lock_path(root);
Self::acquire_at(exec, path, operation) let file = phoebus_core::TargetRoot::new(root)
.open_lock_file(DEFAULT_LOCK_REL)
.map_err(|e| ExecError::operation("sys", "open mutation lock", e.to_string()))?;
Self::claim(exec, file, &path, operation)
} }
/// Attempt to acquire the lock at an explicit path (used by tests and alternate paths).
pub fn acquire_at(exec: &Exec, path: PathBuf, operation: &str) -> Result<Self> { pub fn acquire_at(exec: &Exec, path: PathBuf, operation: &str) -> Result<Self> {
if let Some(parent) = path.parent() { let parent = path
fs::create_dir_all(parent).map_err(|e| ExecError::io(parent.to_path_buf(), e))?; .parent()
.ok_or_else(|| ExecError::operation("sys", "lock", "lock has no parent"))?;
let name = path
.file_name()
.ok_or_else(|| ExecError::operation("sys", "lock", "lock has no name"))?;
let file = phoebus_core::TargetRoot::new(parent)
.open_lock_file(&name.to_string_lossy())
.map_err(|e| ExecError::operation("sys", "open mutation lock", e.to_string()))?;
Self::claim(exec, file, &path, operation)
} }
fn claim(exec: &Exec, mut file: File, path: &Path, operation: &str) -> Result<Self> {
let my_pid = std::process::id(); rustix::fs::flock(&file, rustix::fs::FlockOperation::NonBlockingLockExclusive).map_err(
let now = now_unix(); |e| {
ExecError::operation(
"sys",
"acquire system lock",
format!("another Phoebus system mutation is currently in progress ({e})"),
)
},
)?;
let info = LockInfo { let info = LockInfo {
pid: my_pid, pid: std::process::id(),
created_unix: now, created_unix: now_unix(),
operation: operation.to_string(), operation: operation.into(),
}; };
file.set_len(0)
match Self::try_create(&path, &info) { .and_then(|_| file.write_all(info.serialize().as_bytes()))
Ok(()) => { .and_then(|_| file.sync_all())
exec.logger.info(format!( .map_err(|e| ExecError::io(path, e))?;
"acquired system lock at {} for `{operation}`", exec.logger
path.display() .info(format!("acquired system lock for `{operation}`"));
)); Ok(Self { _file: file })
Ok(Self { lock_path: path })
}
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
// Check if existing lock is stale
if let Ok(content) = fs::read_to_string(&path) {
if let Some(existing) = LockInfo::parse(&content) {
if !is_pid_alive(existing.pid) {
exec.logger.warn(format!(
"detected stale system lock held by dead PID {}; breaking lock",
existing.pid
));
let _ = fs::remove_file(&path);
// Retry atomic creation once
if Self::try_create(&path, &info).is_ok() {
exec.logger.info(format!(
"reclaimed stale system lock at {} for `{operation}`",
path.display()
));
return Ok(Self { lock_path: path });
}
} else {
return Err(ExecError::operation(
"sys",
"acquire system lock",
format!(
"another Phoebus system mutation is currently in progress: `{}` (PID {}, started {}s ago)",
existing.operation,
existing.pid,
now.saturating_sub(existing.created_unix)
),
));
}
}
}
Err(ExecError::operation(
"sys",
"acquire system lock",
format!(
"another Phoebus system mutation is currently in progress (lockfile present at {})",
path.display()
),
))
}
Err(e) => Err(ExecError::io(path, e)),
}
}
fn try_create(path: &Path, info: &LockInfo) -> std::io::Result<()> {
let mut file = OpenOptions::new().write(true).create_new(true).open(path)?;
file.write_all(info.serialize().as_bytes())?;
file.flush()?;
Ok(())
}
}
impl Drop for SystemLock {
fn drop(&mut self) {
if let Ok(content) = fs::read_to_string(&self.lock_path) {
if let Some(info) = LockInfo::parse(&content) {
if info.pid == std::process::id() {
let _ = fs::remove_file(&self.lock_path);
}
}
}
} }
} }
@ -178,6 +101,7 @@ mod tests {
use crate::command::RecordingRunner; use crate::command::RecordingRunner;
use crate::log::Logger; use crate::log::Logger;
use phoebus_core::Redactor; use phoebus_core::Redactor;
use std::fs;
fn make_exec() -> Exec { fn make_exec() -> Exec {
let path = tempfile::tempdir().unwrap().keep(); let path = tempfile::tempdir().unwrap().keep();
@ -208,15 +132,15 @@ mod tests {
drop(lock); drop(lock);
} }
// After drop, lock file is cleaned up and can be acquired again // The inode remains; the kernel releases the lock with its descriptor.
assert!(!lock_file.exists()); assert!(lock_file.exists());
let lock2 = SystemLock::acquire_at(&exec, lock_file.clone(), "install emacs").unwrap(); let lock2 = SystemLock::acquire_at(&exec, lock_file.clone(), "install emacs").unwrap();
assert!(lock_file.exists()); assert!(lock_file.exists());
drop(lock2); drop(lock2);
} }
#[test] #[test]
fn stale_lock_is_reclaimed_when_process_is_dead() { fn diagnostic_pid_never_controls_lock_ownership() {
let temp = tempfile::tempdir().unwrap(); let temp = tempfile::tempdir().unwrap();
let lock_file = temp.path().join("sys.lock"); let lock_file = temp.path().join("sys.lock");
let exec = make_exec(); let exec = make_exec();
@ -229,7 +153,7 @@ mod tests {
}; };
fs::write(&lock_file, stale_info.serialize()).unwrap(); fs::write(&lock_file, stale_info.serialize()).unwrap();
// Acquisition should detect dead PID and reclaim // An unheld inode is available regardless of the diagnostic PID.
let lock = SystemLock::acquire_at(&exec, lock_file.clone(), "new install").unwrap(); let lock = SystemLock::acquire_at(&exec, lock_file.clone(), "new install").unwrap();
assert!(lock_file.exists()); assert!(lock_file.exists());
let content = fs::read_to_string(&lock_file).unwrap(); let content = fs::read_to_string(&lock_file).unwrap();
@ -238,4 +162,109 @@ mod tests {
assert_eq!(parsed.operation, "new install"); assert_eq!(parsed.operation, "new install");
drop(lock); drop(lock);
} }
#[test]
fn lock_child_helper() {
let Some(path) = std::env::var_os("PHOEBUS_LOCK_TEST_PATH") else {
return;
};
let exec = make_exec();
let _lock = SystemLock::acquire_at(&exec, path.into(), "child fixture").unwrap();
println!("LOCK_READY");
std::io::stdout().flush().unwrap();
std::thread::sleep(std::time::Duration::from_secs(20));
}
#[test]
fn crashing_owner_releases_the_same_inode_without_unlinking() {
use std::io::{BufRead, BufReader};
use std::os::unix::fs::MetadataExt;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("lock");
let mut child = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"system_lock::tests::lock_child_helper",
"--nocapture",
])
.env("PHOEBUS_LOCK_TEST_PATH", &path)
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap();
let mut stdout = BufReader::new(child.stdout.take().unwrap());
let mut line = String::new();
loop {
line.clear();
assert!(stdout.read_line(&mut line).unwrap() > 0);
if line.contains("LOCK_READY") {
break;
}
}
let inode = std::fs::metadata(&path).unwrap().ino();
let exec = make_exec();
assert!(SystemLock::acquire_at(&exec, path.clone(), "competitor").is_err());
child.kill().unwrap();
child.wait().unwrap();
let _next = SystemLock::acquire_at(&exec, path.clone(), "recovery").unwrap();
assert_eq!(std::fs::metadata(path).unwrap().ino(), inode);
}
#[test]
fn lease_owner_helper() {
let Some(root) = std::env::var_os("PHOEBUS_LEASE_ROOT") else {
return;
};
let root = PathBuf::from(root);
let runner = crate::command::RealRunner::with_sink(std::sync::Arc::new(|s| {
print!("{s}");
std::io::stdout().flush().unwrap();
}));
let exec = Exec::new(
Box::new(runner),
Logger::open(root.join("log"), Redactor::new(), false),
false,
&root,
&root,
);
let _lock = exec.lock_mutation(&root, "worker lease fixture").unwrap();
exec.run(crate::command::CommandSpec::new("test", "leased worker", "python3")
.args(["-c", "import os,time; print('WORKER_READY',flush=True); deadline=time.monotonic()+10; release=os.environ['PHOEBUS_LEASE_RELEASE']; exec('while not os.path.exists(release) and time.monotonic()<deadline: time.sleep(0.01)')"])
.env("PHOEBUS_LEASE_RELEASE", root.join("release").to_string_lossy())).unwrap();
}
#[test]
fn crashing_broker_does_not_unlock_a_still_running_worker() {
use std::io::{BufRead, BufReader};
let dir = tempfile::tempdir().unwrap();
let mut broker = std::process::Command::new(std::env::current_exe().unwrap())
.args([
"--exact",
"system_lock::tests::lease_owner_helper",
"--nocapture",
])
.env("PHOEBUS_LEASE_ROOT", dir.path())
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap();
let mut stdout = BufReader::new(broker.stdout.take().unwrap());
let mut line = String::new();
loop {
line.clear();
assert!(stdout.read_line(&mut line).unwrap() > 0);
if line.contains("WORKER_READY") {
break;
}
}
broker.kill().unwrap();
broker.wait().unwrap();
let exec = make_exec();
assert!(SystemLock::acquire(&exec, dir.path(), "competitor").is_err());
std::fs::write(dir.path().join("release"), "release worker").unwrap();
let start = std::time::Instant::now();
loop {
if SystemLock::acquire(&exec, dir.path(), "after worker").is_ok() {
break;
}
assert!(start.elapsed() < std::time::Duration::from_secs(5));
std::thread::sleep(std::time::Duration::from_millis(10));
}
}
} }

View file

@ -0,0 +1,183 @@
//! Negative acceptance tests. Run explicitly on a Linux host with working
//! user namespaces, Bubblewrap and a systemd user manager. Only disposable
//! fixture files in the repository and /tmp are created.
use phoebus_executor::agent_jail::{prepare_jail, AgentJailConfig};
use std::process::Command;
#[test]
#[ignore = "requires real Bubblewrap user namespaces"]
fn developer_jail_denies_private_files_environment_processes_and_network() {
let temp = tempfile::tempdir().unwrap();
let home = temp.path().join("home");
let workspace = home.join("workspace");
std::fs::create_dir_all(home.join(".ssh")).unwrap();
std::fs::create_dir(&workspace).unwrap();
std::fs::write(home.join("private"), "synthetic credential").unwrap();
std::fs::write(home.join(".ssh/key"), "synthetic key").unwrap();
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
let script = format!(
r#"
import os, socket
home = {home:?}
ws = {workspace:?}
assert 'PHOEBUS_SYNTHETIC_CREDENTIAL' not in os.environ
for path in [home+'/private', home+'/.ssh/key', '/proc/{pid}/environ']:
try:
open(path).read()
except (FileNotFoundError, PermissionError):
pass
else:
raise AssertionError('private path readable: '+path)
assert os.statvfs('/etc').f_flag & os.ST_RDONLY
with open(ws+'/allowed', 'w') as f: f.write('workspace allowed')
s = socket.socket()
s.settimeout(1)
try:
s.connect(('127.0.0.1', {port}))
except OSError:
pass
else:
raise AssertionError('host network reachable')
print('denials passed')
"#,
home = home.to_string_lossy(),
workspace = workspace.to_string_lossy(),
pid = std::process::id()
);
let config = AgentJailConfig {
workspace: workspace.clone(),
allow_network: false,
allow_nix_store: false,
extra_masked_subdirs: vec![],
};
let jail = prepare_jail(&config, &home, "/usr/bin/python3", &["-c".into(), script]).unwrap();
// Replace the reviewed pathname after preparation. The jail must bind the
// original directory, never the credential directory behind the new link.
let original = home.join("original-workspace");
std::fs::rename(&workspace, &original).unwrap();
std::os::unix::fs::symlink(home.join(".ssh"), &workspace).unwrap();
let out = Command::new("bwrap")
.args(&jail.argv)
.env("PHOEBUS_SYNTHETIC_CREDENTIAL", "synthetic")
.output()
.unwrap();
assert!(
out.status.success(),
"{}\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert_eq!(
std::fs::read_to_string(original.join("allowed")).unwrap(),
"workspace allowed"
);
assert_eq!(
std::fs::read_to_string(home.join("private")).unwrap(),
"synthetic credential"
);
}
#[test]
#[ignore = "requires real systemd user namespace hardening"]
fn model_unit_denies_home_environment_processes_and_writes() {
// This location is beneath the account's home in the development checkout,
// so ProtectHome must hide it. No account credential is read by the probe.
let root = std::env::var_os("PHOEBUS_ISOLATION_FIXTURE_ROOT")
.map(std::path::PathBuf::from)
.unwrap_or_else(|| {
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.ancestors()
.nth(3)
.unwrap()
.to_path_buf()
});
let fixture = tempfile::Builder::new()
.prefix(".isolation-fixture-")
.tempdir_in(&root)
.unwrap();
let private = fixture.path().join("private");
std::fs::write(&private, "synthetic").unwrap();
let home =
phoebus_executor::personal::home_for_uid(phoebus_executor::personal::effective_uid())
.unwrap();
assert!(
private.starts_with(home),
"acceptance fixture must be under the account home"
);
let script = format!(
r#"
import os
assert 'PHOEBUS_SYNTHETIC_CREDENTIAL' not in os.environ
for path in [{private:?}, '/proc/{pid}/environ']:
try:
open(path).read()
except (FileNotFoundError, PermissionError):
pass
else:
raise AssertionError('private path readable: '+path)
assert os.statvfs('/etc').f_flag & os.ST_RDONLY
print('denials passed')
"#,
private = private.to_string_lossy(),
pid = std::process::id()
);
let args = phoebus_executor::sandbox::user_argv("/usr/bin/python3", &["-c".into(), script]);
let out = Command::new("systemd-run")
.args(args)
.env("PHOEBUS_SYNTHETIC_CREDENTIAL", "synthetic")
.output()
.unwrap();
assert!(
out.status.success(),
"{}\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert!(String::from_utf8_lossy(&out.stdout).contains("denials passed"));
}
#[test]
#[ignore = "requires root and a real systemd system manager"]
fn model_system_unit_denies_root_home_environment_and_writes() {
assert_eq!(
phoebus_executor::personal::effective_uid(),
0,
"run this probe only as root in a disposable VM"
);
let fixture = tempfile::Builder::new()
.prefix(".phoebus-isolation-")
.tempdir_in("/root")
.unwrap();
let private = fixture.path().join("private");
std::fs::write(&private, "synthetic").unwrap();
let script = format!(
r#"
import os
assert 'PHOEBUS_SYNTHETIC_CREDENTIAL' not in os.environ
assert os.geteuid() != 0
try:
open({private:?}).read()
except (FileNotFoundError, PermissionError):
pass
else:
raise AssertionError('root home readable')
assert os.statvfs('/etc').f_flag & os.ST_RDONLY
print('denials passed')
"#,
private = private.to_string_lossy()
);
let args = phoebus_executor::sandbox::argv("/usr/bin/python3", &["-c".into(), script]);
let out = Command::new("systemd-run")
.args(args)
.env("PHOEBUS_SYNTHETIC_CREDENTIAL", "synthetic")
.output()
.unwrap();
assert!(
out.status.success(),
"{}\n{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert!(String::from_utf8_lossy(&out.stdout).contains("denials passed"));
}

View file

@ -63,10 +63,13 @@ fn main() -> ExitCode {
return ExitCode::FAILURE; return ExitCode::FAILURE;
} }
let home = match std::env::var("HOME").map(PathBuf::from) { let home =
Ok(h) => h, match phoebus_executor::personal::home_for_uid(phoebus_executor::personal::effective_uid())
Err(_) => { .and_then(|p| p.canonicalize().ok())
eprintln!("phoebus-agent-run: error: HOME environment variable is not set."); {
Some(h) => h,
None => {
eprintln!("phoebus-agent-run: cannot resolve the account's home directory");
return ExitCode::FAILURE; return ExitCode::FAILURE;
} }
}; };
@ -89,14 +92,34 @@ fn main() -> ExitCode {
extra_masked_subdirs: cli.extra_masks, extra_masked_subdirs: cli.extra_masks,
}; };
let argv = build_jail_argv(&config, &home, &cli.program, &cli.args); if phoebus_executor::personal::effective_uid() == 0 {
eprintln!("phoebus-agent-run: developer agents must run as an unprivileged user");
return ExitCode::FAILURE;
}
let argv = match build_jail_argv(&config, &home, &cli.program, &cli.args) {
Ok(args) => args,
Err(e) => {
eprintln!("phoebus-agent-run: {e}");
return ExitCode::FAILURE;
}
};
if cli.dry_run { if cli.dry_run {
println!("bwrap {}", argv.join(" ")); println!("bwrap {}", argv.join(" "));
return ExitCode::SUCCESS; return ExitCode::SUCCESS;
} }
let status = std::process::Command::new("bwrap").args(&argv).status(); let jail =
match phoebus_executor::agent_jail::prepare_jail(&config, &home, &cli.program, &cli.args) {
Ok(jail) => jail,
Err(e) => {
eprintln!("phoebus-agent-run: {e}");
return ExitCode::FAILURE;
}
};
let status = std::process::Command::new("bwrap")
.args(&jail.argv)
.status();
match status { match status {
Ok(s) => { Ok(s) => {

View file

@ -229,12 +229,7 @@ fn record_transaction(
} }
fn effective_uid() -> u32 { fn effective_uid() -> u32 {
std::process::Command::new("id") phoebus_executor::personal::effective_uid()
.arg("-u")
.output()
.ok()
.and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok())
.unwrap_or(1)
} }
/// Persist a minimized conversation turn with the process redactor, surfacing a /// Persist a minimized conversation turn with the process redactor, surfacing a
@ -763,6 +758,11 @@ fn execute_action(
return Err("mutating actions require root; run `sudo sys ...`".into()); return Err("mutating actions require root; run `sudo sys ...`".into());
} }
let _mutation = if action.requires_root() && exec.is_real() && !exec.dry_run {
Some(exec.lock_mutation(&cli.root, &action.describe())?)
} else {
None
};
let effects_phase = exec.progress_phase("Checking the exact effects"); let effects_phase = exec.progress_phase("Checking the exact effects");
let resolved = actions::resolve_effects(exec, &action, &cli.root); let resolved = actions::resolve_effects(exec, &action, &cli.root);
if resolved.is_ok() { if resolved.is_ok() {

View file

@ -69,6 +69,17 @@ pub fn execute_plan_privileged(
assume_yes: bool, assume_yes: bool,
) -> Result<PlanExecutionOutcome> { ) -> Result<PlanExecutionOutcome> {
let plan = plans.load(plan_id)?; let plan = plans.load(plan_id)?;
let _lock = if plan
.actions
.iter()
.any(phoebus_core::action::SystemAction::requires_root)
&& exec.is_real()
&& !exec.dry_run
{
Some(exec.lock_mutation(root, "execute reviewed plan")?)
} else {
None
};
// The expected digest is the independently preserved approval the user // The expected digest is the independently preserved approval the user
// reviewed. A self-consistent reseal of a replacement plan cannot match it. // reviewed. A self-consistent reseal of a replacement plan cannot match it.
validate_prepared_plan(exec, root, &plan, Some(expected_digest), initiator_uid)?; validate_prepared_plan(exec, root, &plan, Some(expected_digest), initiator_uid)?;
@ -158,6 +169,20 @@ fn execute_prepared(
assume_yes: bool, assume_yes: bool,
approvals: Option<(&ApprovalLedger, u32)>, approvals: Option<(&ApprovalLedger, u32)>,
) -> Result<PlanExecutionOutcome> { ) -> Result<PlanExecutionOutcome> {
let _lock = if plan
.actions
.iter()
.any(phoebus_core::action::SystemAction::requires_root)
&& exec.is_real()
&& !exec.dry_run
{
Some(exec.lock_mutation(root, "execute reviewed plan")?)
} else {
None
};
// The first validation may precede authentication. Repeat under the same
// lock held through claim, mutation, verification and terminal recording.
validate_prepared_plan(exec, root, &plan, Some(&plan.digest), plan.initiator_uid)?;
// Authentication/refusal happens before any claim, so a refused plan is not // Authentication/refusal happens before any claim, so a refused plan is not
// consumed and stays reviewable (until it expires). // consumed and stays reviewable (until it expires).
let confirm = ServiceConfirm { assume_yes }; let confirm = ServiceConfirm { assume_yes };
@ -340,7 +365,14 @@ fn execute_claimed(
} else { } else {
None None
}; };
match actions::execute_inner(exec, action, auth, root, carried) { match actions::execute_inner_with_candidate(
exec,
action,
auth,
root,
carried,
plan.candidate.as_ref(),
) {
Ok(result) => { Ok(result) => {
action_phase.complete(); action_phase.complete();
if result.snapshot.is_some() { if result.snapshot.is_some() {

View file

@ -265,12 +265,7 @@ fn now_unix() -> u64 {
/// elevation, bypass the root-owned approval ledger, and execute a /// elevation, bypass the root-owned approval ledger, and execute a
/// root-requiring plan in-process. /// root-requiring plan in-process.
pub fn effective_uid() -> u32 { pub fn effective_uid() -> u32 {
std::process::Command::new("id") phoebus_executor::personal::effective_uid()
.arg("-u")
.output()
.ok()
.and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok())
.unwrap_or(1)
} }
/// Canonicalise a target root so two spellings of the same path compare equal. /// Canonicalise a target root so two spellings of the same path compare equal.
@ -345,4 +340,6 @@ pub use plan::{
plan_overlay, plan_removal, plan_removal_with_ai, plan_selected_install, select_removal_copy, plan_overlay, plan_removal, plan_removal_with_ai, plan_selected_install, select_removal_copy,
PlanStore, PlanStore,
}; };
pub use validation::{compute_plan_digest, seal_plan, validate_prepared_plan}; pub use validation::{
approval_digest_for_plan, compute_plan_digest, seal_plan, validate_prepared_plan,
};

View file

@ -16,7 +16,7 @@ use phoebus_executor::error::{ExecError, Result};
use phoebus_executor::exec::Exec; use phoebus_executor::exec::Exec;
use phoebus_executor::install::{self, AiInstall, InstallRequest}; use phoebus_executor::install::{self, AiInstall, InstallRequest};
use super::validation::{compute_plan_digest, seal_plan}; use super::validation::{approval_digest_for_plan, compute_plan_digest, seal_plan};
use super::{ use super::{
canonical_root, effective_uid, fresh_plan_id, is_safe_plan_id, now_unix, random_token, canonical_root, effective_uid, fresh_plan_id, is_safe_plan_id, now_unix, random_token,
PlanClaim, PlanKind, PreparedPlan, DEFAULT_PLAN_TTL_SECONDS, PlanClaim, PlanKind, PreparedPlan, DEFAULT_PLAN_TTL_SECONDS,
@ -90,7 +90,7 @@ pub fn plan_overlay(
initiator_uid: effective_uid(), initiator_uid: effective_uid(),
integrity: String::new(), integrity: String::new(),
}; };
plan.digest = compute_plan_digest(&plan.actions, None, &plan.effects, &plan.step_effects); plan.digest = approval_digest_for_plan(&plan)?;
plans.save(&mut plan)?; plans.save(&mut plan)?;
Ok(plan) Ok(plan)
} }
@ -459,7 +459,7 @@ fn build_install_plan(
let digest = resolved.approval_digest(); let digest = resolved.approval_digest();
let actions = vec![resolved.action.clone()]; let actions = vec![resolved.action.clone()];
let requires_root = resolved.action.is_mutating(); let requires_root = resolved.action.requires_root();
// Normalize the stored effect digest so the per-step binding check sees a // Normalize the stored effect digest so the per-step binding check sees a
// digest that matches the contents. // digest that matches the contents.
let mut effects = resolved.effects.clone(); let mut effects = resolved.effects.clone();
@ -495,6 +495,7 @@ fn build_install_plan(
integrity: String::new(), integrity: String::new(),
}; };
plan.digest = approval_digest_for_plan(&plan)?;
plans.save(&mut plan)?; plans.save(&mut plan)?;
Ok(plan) Ok(plan)
} }
@ -741,7 +742,7 @@ pub fn build_removal_plan(
"verify that `{}` ({}) is completely removed from the system", "verify that `{}` ({}) is completely removed from the system",
target_copy.id, target_copy.backend target_copy.id, target_copy.backend
), ),
requires_root: action.is_mutating(), requires_root: action.requires_root(),
source: target_copy.backend.clone(), source: target_copy.backend.clone(),
executed: false, executed: false,
target_root: canonical_root(root), target_root: canonical_root(root),
@ -749,6 +750,7 @@ pub fn build_removal_plan(
integrity: String::new(), integrity: String::new(),
}; };
plan.digest = approval_digest_for_plan(&plan)?;
plans.save(&mut plan)?; plans.save(&mut plan)?;
Ok(plan) Ok(plan)
} }
@ -869,16 +871,10 @@ pub fn plan_general(
exec.progress_phase(&format!("Checking evidence for {}", request.describe())); exec.progress_phase(&format!("Checking evidence for {}", request.describe()));
let output = match constraint { let output = match constraint {
Some(backend) => match constrain_action(request.clone(), backend) { Some(backend) => match constrain_action(request.clone(), backend) {
Ok(action) => match actions::execute(exec, &action, None, root) { Ok(action) => phoebus_executor::evidence::investigate(exec, &action, root),
Ok(outcome) => outcome.output,
Err(e) => format!("investigation failed: {e}"),
},
Err(msg) => format!("refused by the source constraint: {msg}"), Err(msg) => format!("refused by the source constraint: {msg}"),
}, },
None => match actions::execute(exec, request, None, root) { None => phoebus_executor::evidence::investigate(exec, request, root),
Ok(outcome) => outcome.output,
Err(e) => format!("investigation failed: {e}"),
},
}; };
if !output.starts_with("investigation failed:") if !output.starts_with("investigation failed:")
&& !output.starts_with("refused by the source constraint:") && !output.starts_with("refused by the source constraint:")
@ -949,7 +945,6 @@ pub fn plan_general(
established: true, established: true,
..Default::default() ..Default::default()
}; };
let mut any_mutating = false;
let mut step_effects = Vec::new(); let mut step_effects = Vec::new();
for act in &actions_list { for act in &actions_list {
@ -974,7 +969,6 @@ pub fn plan_general(
.extend(eff.consequences.clone()); .extend(eff.consequences.clone());
} }
step_effects.push(eff); step_effects.push(eff);
any_mutating |= act.is_mutating();
} }
combined_effects.recompute_digest(); combined_effects.recompute_digest();
@ -999,12 +993,12 @@ pub fn plan_general(
alternatives: Vec::new(), alternatives: Vec::new(),
rationale: rationale_strings, rationale: rationale_strings,
warnings: Vec::new(), warnings: Vec::new(),
actions: actions_list, actions: actions_list.clone(),
effects: combined_effects, effects: combined_effects,
step_effects, step_effects,
digest, digest,
verification: "verify execution of planned actions".into(), verification: "verify execution of planned actions".into(),
requires_root: any_mutating, requires_root: actions_list.iter().any(SystemAction::requires_root),
source: "AI Assistant".into(), source: "AI Assistant".into(),
executed: false, executed: false,
target_root: canonical_root(root), target_root: canonical_root(root),
@ -1013,6 +1007,7 @@ pub fn plan_general(
}; };
if !plan.actions.is_empty() { if !plan.actions.is_empty() {
plan.digest = approval_digest_for_plan(&plan)?;
plans.save(&mut plan)?; plans.save(&mut plan)?;
} }
Ok(plan) Ok(plan)

View file

@ -158,6 +158,10 @@ fn sealed(
initiator_uid, initiator_uid,
integrity: String::new(), integrity: String::new(),
}; };
plan.requires_root = plan.actions.iter().any(SystemAction::requires_root);
if !plan.actions.is_empty() {
plan.digest = approval_digest_for_plan(&plan).unwrap();
}
seal_plan(&mut plan); seal_plan(&mut plan);
plan plan
} }
@ -529,6 +533,21 @@ impl phoebus_executor::command::ProcessRunner for FlatpakRunner {
} else { } else {
Ok(ok("org.mozilla.firefox 128\n".into())) Ok(ok("org.mozilla.firefox 128\n".into()))
} }
} else if is_flatpak && spec.args.iter().any(|a| a == "remote-info") {
Ok(ok(if spec.args.iter().any(|a| a == "--show-ref") {
"app/org.mozilla.firefox/x86_64/stable\n".into()
} else {
format!("{}\n", "a".repeat(64))
}))
} else if is_flatpak && spec.args.iter().any(|a| a == "info") {
Ok(ok(if installed {
format!(
"app/org.mozilla.firefox/x86_64/stable\n{}\n",
"a".repeat(64)
)
} else {
String::new()
}))
} else if is_flatpak && spec.args.iter().any(|a| a == "remotes") { } else if is_flatpak && spec.args.iter().any(|a| a == "remotes") {
Ok(ok("flathub\thttps://dl.flathub.org/repo/\t\n".into())) Ok(ok("flathub\thttps://dl.flathub.org/repo/\t\n".into()))
} else if is_flatpak && spec.args.iter().any(|a| a == "list") { } else if is_flatpak && spec.args.iter().any(|a| a == "list") {
@ -819,6 +838,8 @@ fn portage_install_plan(
initiator_uid: uid, initiator_uid: uid,
integrity: String::new(), integrity: String::new(),
}; };
plan.requires_root = plan.actions.iter().any(SystemAction::requires_root);
plan.digest = approval_digest_for_plan(&plan).unwrap();
seal_plan(&mut plan); seal_plan(&mut plan);
plan plan
} }
@ -1113,6 +1134,7 @@ fn stale_effects_between_validation_and_execution_abort_the_transaction() {
let changed = parse_emerge_pretend(WGET_PRETEND); let changed = parse_emerge_pretend(WGET_PRETEND);
let runner = SequenceRunner { let runner = SequenceRunner {
responses: std::sync::Mutex::new(vec![ responses: std::sync::Mutex::new(vec![
command_out(VIM_PRETEND, true),
command_out(VIM_PRETEND, true), command_out(VIM_PRETEND, true),
command_out(WGET_PRETEND, true), command_out(WGET_PRETEND, true),
]), ]),
@ -1165,6 +1187,7 @@ fn effect_resolution_failure_after_transaction_begin_is_terminal() {
let reviewed = parse_emerge_pretend(VIM_PRETEND); let reviewed = parse_emerge_pretend(VIM_PRETEND);
let runner = SequenceRunner { let runner = SequenceRunner {
responses: std::sync::Mutex::new(vec![ responses: std::sync::Mutex::new(vec![
command_out(VIM_PRETEND, true),
command_out(VIM_PRETEND, true), command_out(VIM_PRETEND, true),
command_out("not valid emerge output\n", true), command_out("not valid emerge output\n", true),
]), ]),
@ -1333,6 +1356,8 @@ fn canonical_plan(
initiator_uid: uid, initiator_uid: uid,
integrity: String::new(), integrity: String::new(),
}; };
plan.requires_root = plan.actions.iter().any(SystemAction::requires_root);
plan.digest = approval_digest_for_plan(&plan).unwrap();
seal_plan(&mut plan); seal_plan(&mut plan);
plan plan
} }
@ -1796,6 +1821,8 @@ fn general_portage_plan(
initiator_uid: uid, initiator_uid: uid,
integrity: String::new(), integrity: String::new(),
}; };
plan.requires_root = plan.actions.iter().any(SystemAction::requires_root);
plan.digest = approval_digest_for_plan(&plan).unwrap();
seal_plan(&mut plan); seal_plan(&mut plan);
plan plan
} }
@ -2262,3 +2289,78 @@ fn constrain_action_allows_spanning_non_package_actions() {
// A Portage-only action is allowed when the constraint is Portage. // A Portage-only action is allowed when the constraint is Portage.
assert!(constrain_action(SystemAction::UpdateSystem, Backend::PortageSource).is_ok()); assert!(constrain_action(SystemAction::UpdateSystem, Backend::PortageSource).is_ok());
} }
#[test]
fn approval_context_cannot_be_resealed_under_a_retained_digest() {
let root = tempdir().unwrap();
let exec = make_test_exec(root.path());
let uid = effective_uid();
let reviewed = sealed(
"context-bound",
PlanKind::General,
vec![SystemAction::History],
"d",
root.path(),
uid,
);
let approved = reviewed.digest.clone();
for mutation in 0..7 {
let mut forged = reviewed.clone();
match mutation {
0 => forged.kind = PlanKind::Undo,
1 => forged.expires_unix += 1,
2 => forged.created_unix -= 1,
3 => forged.target_root = "/".into(),
4 => forged.initiator_uid = uid.wrapping_add(1),
5 => forged.requires_root = !forged.requires_root,
_ => forged.source = "substituted".into(),
}
seal_plan(&mut forged);
let err =
validate_prepared_plan(&exec, root.path(), &forged, Some(&approved), uid).unwrap_err();
assert!(
err.to_string().contains("digest") || err.to_string().contains("lifetime"),
"{mutation}: {err}"
);
forged.digest = approval_digest_for_plan(&forged).unwrap();
seal_plan(&mut forged);
assert!(validate_prepared_plan(&exec, root.path(), &forged, Some(&approved), uid).is_err());
}
}
#[test]
fn model_investigation_redacts_registered_secrets_before_the_next_model_turn() {
use phoebus_ai::provider::{chat_response, ScriptedTransport};
let transport = ScriptedTransport::new(vec![
chat_response(
r#"{"version":1,"kind":"investigate","requests":[{"action":"inspect_service","name":"sshd"}]}"#,
),
chat_response(r#"{"version":1,"summary":"Service inspected.","actions":[]}"#),
]);
let runner = phoebus_executor::command::RecordingRunner::new();
runner.set_response(
"systemctl",
command_out(
"ActiveState=active\nResult=synthetic-service-token\n/home/test/private\n",
true,
),
);
let exec = make_exec_with_runner(Path::new("/"), Box::new(runner));
exec.register_secret(&phoebus_core::SecretString::new("synthetic-service-token"));
let store = PlanStore::with_dir(tempdir().unwrap().keep());
plan_general(
&store,
&exec,
Path::new("/"),
"inspect sshd",
&SystemFacts::default(),
&general_ai(&transport),
None,
)
.unwrap();
let requests = transport.requests();
assert_eq!(requests.len(), 2);
assert!(requests[1].contains("ActiveState=active"));
assert!(!requests[1].contains("synthetic-service-token"));
assert!(!requests[1].contains("/home/test/private"));
}

View file

@ -196,6 +196,36 @@ pub fn compute_plan_digest(
} }
format!("{:x}", hasher.finalize()) format!("{:x}", hasher.finalize())
} }
/// Versioned human approval envelope. Material effects alone are not an
/// approval: kind, lifetime, target, identity and verification selectors also
/// change what the privileged broker will execute and must be independently
/// bound. Presentation-only text stays outside the consumption identity.
pub fn approval_digest_for_plan(plan: &PreparedPlan) -> Result<String> {
let steps = reviewed_steps(plan)?;
let material = compute_plan_digest(
&plan.actions,
plan.candidate.as_ref(),
&plan.effects,
&steps,
);
let envelope = serde_json::to_vec(&(
"phoebus-approval-v2",
&plan.id,
plan.kind,
plan.created_unix,
plan.expires_unix,
&plan.target_root,
plan.initiator_uid,
plan.requires_root,
&plan.application,
&plan.source,
&plan.candidate,
material,
))
.map_err(|e| ExecError::operation("service", "approval envelope", e.to_string()))?;
Ok(format!("{:x}", Sha256::digest(envelope)))
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Privileged validation boundary // Privileged validation boundary
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -260,6 +290,16 @@ pub fn validate_prepared_plan(
"plan proposes no actions", "plan proposes no actions",
)); ));
} }
if plan.expires_unix < plan.created_unix
|| plan.expires_unix.saturating_sub(plan.created_unix) > super::DEFAULT_PLAN_TTL_SECONDS
|| plan.created_unix > now_unix().saturating_add(30)
{
return Err(ExecError::operation(
"service",
"validate plan",
"invalid approval lifetime; review a fresh plan",
));
}
// Independently recompute the canonical approval digest from the exact // Independently recompute the canonical approval digest from the exact
// executable actions, candidate and reviewed material effects. The // executable actions, candidate and reviewed material effects. The
// `integrity` seal stored beside the plan is a self-consistency check the // `integrity` seal stored beside the plan is a self-consistency check the
@ -277,12 +317,7 @@ pub fn validate_prepared_plan(
// change only `step_effects`, reseal" gap for General and Undo plans. // change only `step_effects`, reseal" gap for General and Undo plans.
let steps = reviewed_steps(plan)?; let steps = reviewed_steps(plan)?;
validate_effect_bindings(plan, &steps)?; validate_effect_bindings(plan, &steps)?;
let canonical = compute_plan_digest( let canonical = approval_digest_for_plan(plan)?;
&plan.actions,
plan.candidate.as_ref(),
&plan.effects,
&steps,
);
if plan.digest != canonical { if plan.digest != canonical {
return Err(ExecError::operation( return Err(ExecError::operation(
"service", "service",
@ -292,7 +327,7 @@ pub fn validate_prepared_plan(
)); ));
} }
if let Some(digest) = expected_digest { if let Some(digest) = expected_digest {
if !digest.is_empty() && canonical != digest { if digest.is_empty() || canonical != digest {
return Err(ExecError::operation( return Err(ExecError::operation(
"service", "service",
"validate plan", "validate plan",
@ -300,6 +335,36 @@ pub fn validate_prepared_plan(
)); ));
} }
} }
if plan.requires_root != plan.actions.iter().any(SystemAction::requires_root) {
return Err(ExecError::operation(
"service",
"validate plan",
"plan privilege requirements do not match its actions",
));
}
match plan.kind {
PlanKind::Install | PlanKind::LegacyInstall
if plan.actions.len() != 1
|| plan.candidate.is_none()
|| plan.application.as_deref().unwrap_or("").is_empty() =>
{
return Err(ExecError::operation(
"service",
"validate plan",
"invalid installation plan shape",
));
}
PlanKind::Undo
if plan.actions.len() != 1 || !matches!(plan.actions[0], SystemAction::Undo { .. }) =>
{
return Err(ExecError::operation(
"service",
"validate plan",
"undo plan must contain exactly one undo action",
));
}
_ => {}
}
if plan.target_root.is_empty() { if plan.target_root.is_empty() {
return Err(ExecError::operation( return Err(ExecError::operation(
"service", "service",
@ -364,7 +429,8 @@ fn revalidate_plan_state(exec: &Exec, root: &Path, plan: &PreparedPlan) -> Resul
format!("the reviewed plan changed before execution ({e}); review again"), format!("the reviewed plan changed before execution ({e}); review again"),
) )
})?; })?;
if current_digest != plan.digest { let material_digest = install::approval_digest_for(cand, act, &plan.effects);
if current_digest != material_digest {
return Err(ExecError::operation( return Err(ExecError::operation(
"service", "service",
"revalidate", "revalidate",

View file

@ -165,3 +165,20 @@ DISK=/var/tmp/phoebus-vm/disks/target.qcow2 \
ISO=installer/live/phoebus-installer-amd64.iso \ ISO=installer/live/phoebus-installer-amd64.iso \
python3 installer/tests/vm/gui-driver.py gui python3 installer/tests/vm/gui-driver.py gui
``` ```
## Agentic hardening acceptance
`agentic-hardening-guest.sh` requires QEMU/KVM and root, and is intended only
for an installed disposable overlay. Supply the current static binary as
`/tmp/phoebus-hardening-sys` and the static `isolation_acceptance` test executable
as `/tmp/phoebus-hardening-isolation`. Run the two user probes as the normal
account with `PHOEBUS_ISOLATION_FIXTURE_ROOT=$HOME` and
`--ignored --skip model_system_unit`; the root fixture runs the system-manager
probe separately. It also exercises deterministic history, descriptor-backed
snapshots, exact Portage installation/removal, persistent lock permissions,
Flatpak commit installation/removal, and the active Nix store-path pin.
Serve the files through the existing serial boot harness, authenticate sudo,
require every reported case, and request a clean poweroff. Never reuse the
sole source-build evidence image as a writable target: create a checked qcow2
backing overlay first. Preserve failed diagnostics and distinguish fixture
errors from product failures.

View file

@ -0,0 +1,50 @@
#!/bin/bash
# Run only in a disposable QEMU overlay, after the serial driver authenticates.
set -euo pipefail
case "$(systemd-detect-virt)" in
qemu|kvm) ;;
*) echo 'disposable QEMU/KVM required'; exit 2 ;;
esac
[[ $(id -u) == 0 ]] || { echo 'root fixture required'; exit 2; }
SYS=/tmp/phoebus-hardening-sys
report() { printf 'CASE %s PASS\n' "$1"; }
# On failure, retain useful native diagnostics without pretending a missing
# case passed. These files contain only deterministic package/snapshot output.
on_exit() {
local rc="$?"
if [[ "$rc" -ne 0 ]]; then
printf 'CASE native-command FAIL exit=%s\n' "$rc"
for output in /tmp/phoebus-hardening-{install,flatpak,nix,snapshot}.out; do
[[ -f "$output" ]] || continue
printf 'Fixture diagnostic: %s\n' "$output"
tail -n 12 "$output"
done
fi
}
trap on_exit EXIT
"$SYS" history --legacy >/tmp/phoebus-hardening-history.out
report deterministic-history
/tmp/phoebus-hardening-isolation --ignored model_system_unit
report root-model-isolation
"$SYS" snapshot --legacy -y >/tmp/phoebus-hardening-snapshot.out
report descriptor-snapshot
if compgen -G '/var/db/pkg/app-misc/hello-*' >/dev/null; then
"$SYS" remove hello --legacy --portage -y >/tmp/phoebus-hardening-remove-before.out 2>&1
fi
"$SYS" install hello --legacy --portage -y >/tmp/phoebus-hardening-install.out 2>&1
# Dedicated workflow must have verified the exact candidate, not mere presence.
grep -q 'verified: app-misc/hello ' /tmp/phoebus-hardening-install.out >/dev/null
report portage-exact
"$SYS" remove hello --legacy --portage -y >/tmp/phoebus-hardening-remove.out 2>&1
report portage-remove
[[ -f /run/phoebus/sys.lock ]] && [[ $(stat -c %a /run/phoebus/sys.lock) == 600 ]]
report persistent-kernel-lock
"$SYS" install org.gnome.Calculator --legacy --flatpak -y >/tmp/phoebus-hardening-flatpak.out 2>&1
grep -q 'verified: Flatpak app org.gnome.Calculator' /tmp/phoebus-hardening-flatpak.out
report flatpak-pin
"$SYS" remove org.gnome.Calculator --legacy --flatpak -y >/tmp/phoebus-hardening-flatpak-remove.out 2>&1
report flatpak-remove
"$SYS" install hello --legacy --nix -y >/tmp/phoebus-hardening-nix.out 2>&1
grep -q 'verified:' /tmp/phoebus-hardening-nix.out
report nix-pin