From 9b836257785928ccbfbf7178d426a7ec0c3303da Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 19:18:22 +0000 Subject: [PATCH] fix(pack): approve the package weft-appd launches, and drop approvals no longer declared weft-pack approve read only the user store, so an app in the system store, or installed as a verified image, could not be approved. The remedy the launch refusal named also did not work. approve now resolves the package in weft-appd's store order. It also accepts the capabilities by name, which is how an image is approved, since weft-pack cannot read its manifest. An install or rollback without --approve now drops approvals for capabilities the package no longer declares. A capability declared again later therefore asks again. Both commands report what still needs approval. weft-appd validates capabilities without side effects first, then refuses unapproved ones, then derives grants, so an unsupported capability is reported as such and not as one needing approval. The docs state that approvals are per user. --- crates/weft-appd/src/grants.rs | 22 +++--- crates/weft-appd/src/main.rs | 3 + crates/weft-pack/src/main.rs | 126 ++++++++++++++++++++++++++++----- docs/architecture.md | 2 +- docs/security.md | 4 +- 5 files changed, 131 insertions(+), 26 deletions(-) diff --git a/crates/weft-appd/src/grants.rs b/crates/weft-appd/src/grants.rs index 0a84f03..0343f99 100644 --- a/crates/weft-appd/src/grants.rs +++ b/crates/weft-appd/src/grants.rs @@ -87,14 +87,9 @@ impl HostDirs { } } -/// Derives grants for the declared capabilities. Every capability is -/// checked before host directories are resolved or the app's data -/// directory is created, so a refused launch leaves nothing behind. -pub(crate) fn derive( - app_id: &str, - declared: &[String], - host_dirs: impl FnOnce() -> Result, -) -> Result { +/// Parses the declared capabilities, refusing unknown ones and those this +/// host does not support, without touching the host. +pub(crate) fn validate(declared: &[String]) -> Result, Refusal> { let mut capabilities = Vec::new(); for text in declared { let capability: Capability = @@ -110,7 +105,18 @@ pub(crate) fn derive( } capabilities.push(capability); } + Ok(capabilities) +} +/// Derives grants for the declared capabilities. Every capability is +/// checked before host directories are resolved or the app's data +/// directory is created, so a refused launch leaves nothing behind. +pub(crate) fn derive( + app_id: &str, + declared: &[String], + host_dirs: impl FnOnce() -> Result, +) -> Result { + let capabilities = validate(declared)?; let mut grants = SessionGrants::default(); let needs_dirs = capabilities .iter() diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 4516ac9..f7f1f1d 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -609,6 +609,9 @@ async fn resolve_launch( ) -> Result<(launch::LaunchPackage, grants::SessionGrants), grants::Refusal> { tokio::task::spawn_blocking(move || { let package = launch::resolve(&app_id)?; + // Unknown and unsupported capabilities are refused as such, then + // unapproved ones, before anything is prepared on the host. + grants::validate(&package.capabilities)?; check_approval(&app_id, &package.capabilities)?; let grants = grants::derive(&app_id, &package.capabilities, grants::HostDirs::from_env)?; tracing::info!( diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 93c1b35..bb1dba4 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -40,8 +40,10 @@ fn main() -> anyhow::Result<()> { uninstall_package(app_id)?; } Some("approve") => { - let app_id = args.get(2).context("usage: weft-pack approve ")?; - approve_package(&resolve_install_root()?, app_id)?; + let app_id = args + .get(2) + .context("usage: weft-pack approve [...]")?; + approve_package(app_id, &args[3..])?; } Some("rollback") => { let app_id = args.get(2).context("usage: weft-pack rollback ")?; @@ -135,7 +137,7 @@ fn main() -> anyhow::Result<()> { eprintln!(" weft-pack uninstall remove installed package"); eprintln!(" weft-pack rollback activate the previous revision"); eprintln!( - " weft-pack approve approve the app's declared capabilities" + " weft-pack approve [...] approve its declared or listed capabilities" ); eprintln!(" weft-pack list list installed packages"); eprintln!( @@ -511,6 +513,18 @@ fn settle_approval( return Ok(()); } let approved = read_approved(&record).with_context(|| format!("read {}", record.display()))?; + // An approval covers what the installed package declares: a capability + // it no longer declares is dropped, so declaring it again later asks + // again. + let kept: Vec = declared + .iter() + .filter(|c| approved.contains(*c)) + .cloned() + .collect(); + if kept.len() != approved.len() { + write_approved(&record, &kept) + .with_context(|| format!("update the approval of {app_id}"))?; + } let pending = unapproved(declared, &approved); if !pending.is_empty() { println!( @@ -521,8 +535,13 @@ fn settle_approval( Ok(()) } -/// Approves the capabilities the active package of `app_id` declares. -fn approve_package(store_root: &Path, app_id: &str) -> anyhow::Result<()> { +/// Approves capabilities for `app_id`: those listed in `explicit`, or +/// without a list, those its installed package declares. The package is the +/// one weft-appd launches: the first store, in weft-appd's order, holding +/// the app. A verified image, whose manifest weft-pack cannot read, needs +/// the explicit list. +fn approve_package(app_id: &str, explicit: &[String]) -> anyhow::Result<()> { + use weft_ipc_types::approval::needs_approval; anyhow::ensure!( weft_ipc_types::package::is_valid_app_id(app_id), "'{app_id}' is not a valid app ID" @@ -530,23 +549,49 @@ fn approve_package(store_root: &Path, app_id: &str) -> anyhow::Result<()> { let data_home = weft_ipc_types::package::data_home() .context("cannot locate the data home to record the approval")?; let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; - let found = active(store_root, app_id)? - .with_context(|| format!("{app_id} is not installed in {}", store_root.display()))?; + if !explicit.is_empty() { + for capability in explicit { + capability + .parse::() + .map_err(|e| anyhow::anyhow!("{e}"))?; + anyhow::ensure!(needs_approval(capability), "{capability} needs no approval"); + } + return settle_approval(&data_home, app_id, explicit, true); + } + let roots = list_installed_roots(); + if roots + .iter() + .any(|root| ImageFiles::in_store(root, app_id).any_present()) + { + anyhow::bail!( + "{app_id} is installed as a verified image, whose capabilities weft-pack cannot \ + read; approve them by name: weft-pack approve {app_id} ..." + ); + } + let found = roots + .iter() + .find_map(|root| match active(root, app_id) { + Ok(Some(found)) + if found + .dir() + .join(weft_ipc_types::manifest::MANIFEST_FILE) + .is_file() => + { + Some(found) + } + _ => None, + }) + .with_context(|| format!("{app_id} is not installed"))?; let manifest = load_manifest(found.dir())?; anyhow::ensure!( manifest.package.id == app_id, "{} declares another app ID", found.dir().display() ); - settle_approval(&data_home, app_id, manifest.capabilities(), true)?; - if !manifest - .capabilities() - .iter() - .any(|c| weft_ipc_types::approval::needs_approval(c)) - { + if !manifest.capabilities().iter().any(|c| needs_approval(c)) { println!("{app_id} declares nothing that needs approval"); } - Ok(()) + settle_approval(&data_home, app_id, manifest.capabilities(), true) } /// The active package of `app_id` in `store_root`. @@ -937,7 +982,8 @@ fn rollback_package(store_root: &Path, app_id: &str) -> anyhow::Result<()> { activate(store_root, app_id, &previous)?; collect(store_root, app_id); println!("rolled {app_id} back to revision {}", &previous[..12]); - Ok(()) + let manifest = load_manifest(&dir)?; + settle_approval(&data_home, app_id, manifest.capabilities(), false) } /// Establishes who owns the app ID of a checked, staged package: @@ -1556,6 +1602,22 @@ mod tests { .unwrap(); } + /// Runs `f` with WEFT_APP_STORE set to `store`. Callers run it inside + /// `with_home`, which holds env_lock. + fn with_store(store: &Path, f: impl FnOnce() -> T) -> T { + let prior = std::env::var_os("WEFT_APP_STORE"); + // SAFETY: the caller holds env_lock through with_home. + unsafe { std::env::set_var("WEFT_APP_STORE", store) }; + let result = f(); + unsafe { + match prior { + Some(v) => std::env::set_var("WEFT_APP_STORE", v), + None => std::env::remove_var("WEFT_APP_STORE"), + } + } + result + } + /// Runs `f` with HOME set to `home` and XDG_DATA_HOME to `home/share`. fn with_home(home: &Path, f: impl FnOnce() -> T) -> T { let _env = env_lock(); @@ -2046,7 +2108,10 @@ mod tests { }) .unwrap(); assert_eq!(pending(&["sys:notifications"]), ["sys:notifications"]); - with_home(&home, || approve_package(&store, app_id)).unwrap(); + with_home(&home, || { + with_store(&store, || approve_package(app_id, &[])) + }) + .unwrap(); assert!(pending(&["sys:notifications", "fs:rw:app-data"]).is_empty()); // An update that asks for more is not approved by the earlier answer. write_package( @@ -2068,6 +2133,35 @@ mod tests { }) .unwrap(); assert!(pending(&["sys:notifications", "sys:clipboard:read"]).is_empty()); + // A capability the package stops declaring is no longer approved. + write_package(&src, app_id, "\"sys:notifications\""); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + write_package( + &src, + app_id, + "\"sys:notifications\", \"sys:clipboard:read\"", + ); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert_eq!( + pending(&["sys:notifications", "sys:clipboard:read"]), + ["sys:clipboard:read"] + ); + // Capabilities can be approved by name, and only valid ones that + // need approval. + with_home(&home, || { + approve_package(app_id, &["sys:clipboard:read".to_owned()]) + }) + .unwrap(); + assert!(pending(&["sys:clipboard:read"]).is_empty()); + for refused in ["fs:rw:app-data", "sys:everything"] { + assert!(with_home(&home, || approve_package(app_id, &[refused.to_owned()])).is_err()); + } // Uninstalling forgets the approval. with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); assert!(!record.exists()); diff --git a/docs/architecture.md b/docs/architecture.md index 9d493c0..d2bc1f2 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -28,7 +28,7 @@ WASI Preview 2 + Component Model execution host (Wasmtime 30). Runs the componen ### weft-pack -Package management CLI. Subcommands: `check` (validate wapp.toml, entries and package content), `sign` (Ed25519 signature over every file), `verify` (verify signature), `generate-key`, `install` (requires a signature by a trusted key unless `--dev`; records the app ID's owner; installs or updates, see [Installing and updating](#installing-and-updating)), `uninstall`, `rollback`, `approve` (approve the installed app's declared capabilities; `install --approve` does so at installation), `list`, `build-image` (EROFS dm-verity), `info`. +Package management CLI. Subcommands: `check` (validate wapp.toml, entries and package content), `sign` (Ed25519 signature over every file), `verify` (verify signature), `generate-key`, `install` (requires a signature by a trusted key unless `--dev`; records the app ID's owner; installs or updates, see [Installing and updating](#installing-and-updating)), `uninstall`, `rollback`, `approve` (approve the app's declared capabilities, or those listed; `install --approve` does so at installation), `list`, `build-image` (EROFS dm-verity), `info`. ### weft-file-portal diff --git a/docs/security.md b/docs/security.md index e71560d..cf6aa38 100644 --- a/docs/security.md +++ b/docs/security.md @@ -8,7 +8,9 @@ WEFT OS enforces a capability-based security model. No capability is granted by `weft-pack check` validates capability strings against the shared vocabulary in `weft_ipc_types::capability`. Unknown capability strings are rejected. At launch, `weft-appd` derives the session's grants from the manifest and refuses to start a package with a capability that is unknown, unsupported or unsatisfiable. `weft-runtime` enforces the grants it is given: read-only directories are preopened read-only, and each host import checks its grant on every call. See the capability table in `architecture.md`. -A package gets only the capabilities the user approved. `weft-pack install --approve` approves those the installed package declares, and `weft-pack approve ` approves those of the installed app later; without approval, `install` lists what still needs it. The approval is recorded in `$XDG_DATA_HOME/weft/approvals/`, and weft-appd refuses to launch an app that declares a capability it does not hold (403, naming the capabilities). An update that declares a new capability needs approval again; an earlier approval never extends to it. Uninstalling the app forgets the approval. Private app data (`fs:read:app-data`, `fs:rw:app-data`) is a minimal execution resource and needs no approval. Signature verification and approval are separate decisions: a trusted signature approves nothing. +A package gets only the capabilities the user approved. `weft-pack install --approve` approves those the installed package declares. `weft-pack approve ` approves those of the package weft-appd would launch, the first store in weft-appd's order that holds the app, which also covers packages in the system store. `weft-pack approve ...` approves the listed capabilities, which is the only way for an app installed as a verified image, since weft-pack cannot read an image's manifest. Without approval, `install` and `rollback` list what still needs it. + +The approval is recorded per user, in `$XDG_DATA_HOME/weft/approvals/`; weft-pack and weft-appd must use the same data home, as for owner records. An installation by another user, such as root installing into the system store, approves nothing for this user. weft-appd refuses to launch an app that declares a capability the record does not hold (403, naming the capabilities), after refusing unknown and unsupported capabilities and before preparing anything for the session. An approval covers what the installed package declares: an update or rollback drops approvals for capabilities the package no longer declares, so a capability declared again later, or newly, needs approval again. Uninstalling the app forgets its approval unless another store still holds the app. Approvals are keyed by app ID, not by store or publisher. Private app data (`fs:read:app-data`, `fs:rw:app-data`) is a minimal execution resource and needs no approval. Signature verification and approval are separate decisions: a trusted signature approves nothing. ## Process Isolation