From 11cea5e7e3b0c862c0dc5579d8cef41960608501 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:29:14 +0000 Subject: [PATCH] fix(appd): verify package owner and signature before a session starts weft-appd trusted an installed directory as installed, so content changed after installation, or signed by a key since removed from the trust store, still ran with the owner's app data. Before anything starts, a package recorded as a publisher's must carry that publisher's valid signature over its current content, with the key still trusted; a package with no record must be signed by a trusted key; development content, recorded by weft-pack install --dev, runs unverified. Failures are refused with code 403. The desktop checks trust the demo key, and the confinement check, whose package deliberately holds a link, records it as development content. --- crates/weft-appd/src/launch.rs | 184 ++++++++++++++++++++++++++++++- crates/weft-appd/src/main.rs | 16 +++ docs/security.md | 4 +- tests/frame/check_app_data.py | 1 + tests/frame/check_confinement.py | 8 ++ tests/frame/check_counter.py | 1 + tests/frame/check_notes.py | 1 + 7 files changed, 213 insertions(+), 2 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 85b5bbc..2e777f5 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -3,11 +3,20 @@ //! The runtime and the app shell receive the exact files resolved here and //! never look for the package themselves, so the component, the UI and the //! capabilities a session is granted all come from the same package root. +//! +//! Before anything starts, the package must still be what its owner +//! installed: a package recorded as a publisher's must carry that +//! publisher's valid signature over its current content, and a package with +//! no record must be signed by a trusted key. Development content, recorded +//! as such by `weft-pack install --dev`, runs unverified. use std::path::{Path, PathBuf}; use weft_ipc_types::manifest::{MANIFEST_FILE, Manifest, ManifestError, entry_path}; use weft_ipc_types::package::{ImageFiles, is_valid_app_id}; +use weft_ipc_types::trust::{ + Owner, TrustError, TrustStore, content_digest, owner_record_path, read_owner, read_signature, +}; use crate::grants::Refusal; use crate::mount::Mount; @@ -99,6 +108,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result) -> Result Result<(), Refusal> { + let refused = |e: TrustError| match e { + TrustError::Io(..) => Refusal::new(500, e.to_string()), + _ => Refusal::new(403, e.to_string()), + }; + let data_home = weft_ipc_types::package::data_home().ok_or_else(|| { + Refusal::new( + 500, + "cannot locate the data home holding package owner records", + ) + })?; + let owner = read_owner(&owner_record_path(&data_home, app_id)).map_err(refused)?; + let trusted = TrustStore::load(&TrustStore::directories()).map_err(refused)?; + match owner { + Some(Owner::Development) => Ok(()), + Some(Owner::Verified(key)) => { + // The key must still be trusted, and its signature must cover + // the package's current content. + let signature = read_signature(root).map_err(refused)?; + let digest = content_digest(root).map_err(refused)?; + let valid = trusted.keys().contains(&key) + && signature.is_some_and(|sig| key.signed(&digest, &sig)); + if valid { + Ok(()) + } else { + Err(Refusal::new( + 403, + format!( + "{} is not signed by its owner, publisher {}, or that key is no longer \ + trusted; the package was changed or its key was withdrawn", + root.display(), + key.to_hex() + ), + )) + } + } + None => match trusted.signer(root).map_err(refused)? { + Some(_) => Ok(()), + None => Err(Refusal::new( + 403, + format!( + "{} has no recorded owner and is not signed by a trusted key; install it \ + with weft-pack", + root.display() + ), + )), + }, + } +} + impl LaunchPackage { /// A package for supervisor tests whose children ignore their files. #[cfg(test)] @@ -160,8 +221,17 @@ mod tests { ), ) .unwrap(); + // The fixture is unsigned development content, recorded as such. + weft_ipc_types::trust::write_owner( + &owner_record_path(&store.join("share"), ID), + Owner::Development, + ) + .unwrap(); // SAFETY: callers hold env_lock, which serialises environment changes. - unsafe { std::env::set_var("WEFT_APP_STORE", &store) }; + unsafe { + std::env::set_var("WEFT_APP_STORE", &store); + std::env::set_var("XDG_DATA_HOME", store.join("share")); + } store } @@ -170,6 +240,8 @@ mod tests { unsafe { std::env::remove_var("WEFT_APP_STORE"); std::env::remove_var("WEFT_MOUNT_HELPER"); + std::env::remove_var("XDG_DATA_HOME"); + std::env::remove_var("WEFT_TRUSTED_KEYS"); } let _ = std::fs::remove_dir_all(store); } @@ -249,6 +321,116 @@ mod tests { } } + const DEMO: &str = "org.weft.demo.counter"; + + /// A store holding a copy of the signed Counter demo, with the demo key + /// trusted when `trusted`. Callers hold env_lock. + fn demo_store(name: &str, trusted: bool) -> PathBuf { + let store = store(name, ID, "app.wasm"); + let examples = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../examples"); + copy_tree(&examples.join(DEMO), &store.join(DEMO)); + let keys = store.join("keys"); + std::fs::create_dir_all(&keys).unwrap(); + if trusted { + std::fs::copy(examples.join("keys/weft-sign.pub"), keys.join("demo.pub")).unwrap(); + } + // SAFETY: as in `store`. + unsafe { std::env::set_var("WEFT_TRUSTED_KEYS", &keys) }; + store + } + + fn copy_tree(src: &Path, dst: &Path) { + std::fs::create_dir_all(dst).unwrap(); + for entry in std::fs::read_dir(src).unwrap().flatten() { + let to = dst.join(entry.file_name()); + if entry.file_type().unwrap().is_dir() { + copy_tree(&entry.path(), &to); + } else { + std::fs::copy(entry.path(), to).unwrap(); + } + } + } + + fn record(store: &Path, app_id: &str, owner: Owner) { + weft_ipc_types::trust::write_owner(&owner_record_path(&store.join("share"), app_id), owner) + .unwrap(); + } + + fn demo_key() -> weft_ipc_types::trust::PublisherKey { + weft_ipc_types::trust::PublisherKey::from_file( + &Path::new(env!("CARGO_MANIFEST_DIR")).join("../../examples/keys/weft-sign.pub"), + ) + .unwrap() + } + + #[test] + fn a_package_signed_by_its_owner_launches() { + let _env = crate::tests::env_lock().blocking_lock(); + // Recorded as the publisher's, and with no record but a trusted + // signature. + let store = demo_store("signed", true); + let unrecorded = resolve(DEMO).map(|p| p.root); + record(&store, DEMO, Owner::Verified(demo_key())); + let recorded = resolve(DEMO).map(|p| p.root); + finish(&store); + assert_eq!(unrecorded.ok(), Some(store.join(DEMO))); + assert_eq!(recorded.ok(), Some(store.join(DEMO))); + } + + #[test] + fn a_package_changed_after_installation_is_refused() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("changed", true); + record(&store, DEMO, Owner::Verified(demo_key())); + let page = store.join(DEMO).join("ui/index.html"); + let mut text = std::fs::read_to_string(&page).unwrap(); + text.push_str(""); + std::fs::write(&page, text).unwrap(); + let changed = refusal(resolve(DEMO)); + // Without a record, the changed package matches no trusted key. + std::fs::remove_file(owner_record_path(&store.join("share"), DEMO)).unwrap(); + let unrecorded = refusal(resolve(DEMO)); + finish(&store); + assert_eq!(changed.code, 403, "{}", changed.message); + assert!( + changed.message.contains("not signed by its owner"), + "{}", + changed.message + ); + assert_eq!(unrecorded.code, 403, "{}", unrecorded.message); + assert!( + unrecorded.message.contains("no recorded owner"), + "{}", + unrecorded.message + ); + } + + #[test] + fn a_withdrawn_key_no_longer_launches_its_packages() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("withdrawn", false); + record(&store, DEMO, Owner::Verified(demo_key())); + let refused = refusal(resolve(DEMO)); + finish(&store); + assert_eq!(refused.code, 403, "{}", refused.message); + assert!(refused.message.contains("no longer"), "{}", refused.message); + } + + #[test] + fn unsigned_content_needs_a_development_record() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = store("unrecorded", ID, "app.wasm"); + std::fs::remove_file(owner_record_path(&store.join("share"), ID)).unwrap(); + let refused = refusal(resolve(ID)); + // Another owner's record does not admit it either. + record(&store, ID, Owner::Verified(demo_key())); + let other_owner = refusal(resolve(ID)); + finish(&store); + for refused in [refused, other_owner] { + assert_eq!(refused.code, 403, "{}", refused.message); + } + } + #[test] fn a_package_declaring_another_id_is_refused() { let _env = crate::tests::env_lock().blocking_lock(); diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 4512211..d0c549d 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -712,6 +712,22 @@ mod tests { ), ) .unwrap(); + record_development(id); + } + + /// Records `id` as development content in a data home private to this + /// test process, which XDG_DATA_HOME then points at. Callers hold + /// env_lock. + fn record_development(id: &str) { + let data_home = + std::env::temp_dir().join(format!("weft-appd-tests-data-{}", std::process::id())); + let record = weft_ipc_types::trust::owner_record_path(&data_home, id); + if !record.exists() { + weft_ipc_types::trust::write_owner(&record, weft_ipc_types::trust::Owner::Development) + .unwrap(); + } + // SAFETY: callers hold env_lock, which serialises environment changes. + unsafe { std::env::set_var("XDG_DATA_HOME", &data_home) }; } fn make_registry() -> Registry { diff --git a/docs/security.md b/docs/security.md index 5ac7ff2..12016fa 100644 --- a/docs/security.md +++ b/docs/security.md @@ -26,7 +26,9 @@ Packages are signed with Ed25519 over their content digest: the SHA-256 of an in The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/`: the publisher key, or development. Installations and uninstallations of one ID under one data home hold a per-ID lock, so concurrent installations cannot leave a placed package without its record. Every later installation of that ID must have the same owner, so neither another trusted publisher nor a development build can take over an ID, and with it the app's data. The record survives uninstall while the app's data remains; uninstalling with no data left frees the ID. App data with no recorded owner, as left by installations from before owner records, is given to a new owner only with `--claim-data`. A record is written under a temporary name and linked into place, so it is never partial, and a record created by an installation that then fails to place its package is removed. A record left by an installation that was killed before placing its package is released by `weft-pack uninstall ` once no data remains. A record is only released while no known store (the user store, the system store and `$WEFT_APP_STORE`) holds the package; a package installed into another custom store is uninstalled with that store selected. The demo key in `examples/keys` is a test fixture, not a signing authority. -Not yet covered: `weft-appd` does not check signatures or owners at launch, so the installed directory is trusted as installed; key revocation, rotation and ownership transfer have no workflow; and capabilities are granted at launch without asking the user, independently of the signature. +`weft-appd` checks every package again before a session starts. A package recorded as a publisher's runs only while that key is still trusted and its signature covers the package's current content; a package with no record runs only when a trusted key signed it; development content runs unverified. A changed package, or one whose key was removed from the trust store, is refused with code 403. The check reads the package before the session starts, so a directory install changed by its owning user while the session runs is not detected; verified images are not writable. + +Not yet covered: key rotation and ownership transfer have no workflow, and removing a key is the only revocation; and capabilities are granted at launch without asking the user, independently of the signature. For verified read-only package storage, `weft-pack build-image` produces an EROFS image protected with dm-verity. Mounting requires the setuid `weft-mount-helper` which calls `veritysetup`. The image's root hash is not yet authenticated (see `architecture.md`). diff --git a/tests/frame/check_app_data.py b/tests/frame/check_app_data.py index 2f43cb8..50d10e2 100644 --- a/tests/frame/check_app_data.py +++ b/tests/frame/check_app_data.py @@ -47,6 +47,7 @@ def run(args, desktop, home, xdotool): "WEFT_RUNTIME_BIN": str(t / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_STORE": str(args.store), + "WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(free_port()), "HOME": str(home), diff --git a/tests/frame/check_confinement.py b/tests/frame/check_confinement.py index c484905..d027921 100644 --- a/tests/frame/check_confinement.py +++ b/tests/frame/check_confinement.py @@ -142,6 +142,12 @@ def make_package(store, http_port, ws_port, other_dir): (package / "outside.png").write_bytes(png()) other.write_bytes(png()) (package / "ui/linked").symlink_to(other_dir) + # The package holds a link, which weft-pack refuses to install, so it is + # planted directly and recorded as development content, which runs + # unverified: the renderer's own confinement is what this check probes. + record = store / "home/share/weft/owners" / APP_ID + record.parent.mkdir(parents=True) + record.write_text("development\n") def run(args, desktop, store, other_dir): @@ -157,6 +163,8 @@ def run(args, desktop, store, other_dir): "WEFT_RUNTIME_BIN": str(t / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_STORE": str(store), + "HOME": str(store / "home"), + "XDG_DATA_HOME": str(store / "home/share"), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(free_port()), }) diff --git a/tests/frame/check_counter.py b/tests/frame/check_counter.py index aa8ecdb..0da4e88 100644 --- a/tests/frame/check_counter.py +++ b/tests/frame/check_counter.py @@ -162,6 +162,7 @@ def run(args, desktop, xdotool): "WEFT_RUNTIME_BIN": str(target / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(target / "weft-app-shell"), "WEFT_APP_STORE": str(args.store), + "WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(port), }) diff --git a/tests/frame/check_notes.py b/tests/frame/check_notes.py index 2d562ee..6ee3cba 100644 --- a/tests/frame/check_notes.py +++ b/tests/frame/check_notes.py @@ -75,6 +75,7 @@ def run(args, desktop, home, xdotool): "WEFT_RUNTIME_BIN": str(t / "weft-runtime"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_STORE": str(args.store), + "WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"), "WEFT_DISABLE_CGROUP": "1", "WEFT_APPD_WS_PORT": str(free_port()), "HOME": str(home),