From 88c2e9f6a529c8f067862e964af7845dfab00713 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 13:49:49 +0000 Subject: [PATCH 01/22] fix(pack): install only trusted packages and bind each app ID to its owner weft-pack install copied any package into the store: signatures were never checked, no key was trusted for anything, any publisher could install over another's app ID and reach its retained data, and the signing inventory followed symbolic links, so a package could sign and install files from outside itself. Installation now requires a signature by a key in the trust store ($XDG_CONFIG_HOME/weft/trusted-keys and /etc/weft/trusted-keys, or $WEFT_TRUSTED_KEYS alone). Unsigned or untrusted content installs only with --dev, recorded as development content. The package is copied into the store, checked and verified on that copy, and renamed into place, so the verified bytes are the installed bytes. The first installation of an app ID records its owner beside the app data; later installations must have the same owner, so neither another publisher nor a development build can take the ID over. The record outlives uninstall while app data remains. The content digest moves to weft_ipc_types::trust and refuses links, special files and non-UTF-8 paths; it keeps the existing inventory format, and both committed demo signatures still verify. Catalogs list only directories named after their package ID. The security guide described a signature over wapp.toml and app.wasm only; it now describes the full inventory and the trust and ownership rules. --- Cargo.lock | 3 + README.md | 2 +- crates/weft-appd/src/main.rs | 5 + crates/weft-ipc-types/Cargo.toml | 3 + crates/weft-ipc-types/src/lib.rs | 1 + crates/weft-ipc-types/src/package.rs | 4 +- crates/weft-ipc-types/src/trust.rs | 400 +++++++++++++++++ crates/weft-pack/src/main.rs | 616 +++++++++++++++++++-------- docs/architecture.md | 4 +- docs/building.md | 10 + docs/security.md | 12 +- 11 files changed, 880 insertions(+), 180 deletions(-) create mode 100644 crates/weft-ipc-types/src/trust.rs diff --git a/Cargo.lock b/Cargo.lock index 20cbbdf..bc4cf59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10519,9 +10519,12 @@ dependencies = [ name = "weft-ipc-types" version = "0.1.0" dependencies = [ + "ed25519-dalek", + "hex", "rmp-serde", "rustix 1.1.4", "serde", + "sha2", "toml 0.8.23", ] diff --git a/README.md b/README.md index 022b3d0..f6a5c18 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web **Runtime** — `weft-runtime` runs WASI Component Model binaries under Wasmtime 30 (`--features wasmtime-runtime`). Provides `weft:app/notify`, `weft:app/ipc`, `weft:app/fetch`, `weft:app/notifications`, and `weft:app/clipboard` host imports. Preopens filesystem paths according to declared capabilities. -**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. App data is stored apart from packages and survives uninstall. +**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. App data is stored apart from packages and survives uninstall. **File portal** — `weft-file-portal` is a per-session file proxy with a path allowlist and `..` blocking. diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 433b0e8..4512211 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -634,6 +634,11 @@ fn scan_installed_apps() -> Vec { let Ok(m) = weft_ipc_types::manifest::Manifest::read(&entry.path()) else { continue; }; + // Only a directory named after the ID its manifest declares is an + // installed package; staging copies and stray directories are not. + if entry.file_name().to_str() != Some(m.package.id.as_str()) { + continue; + } if seen.insert(m.package.id.clone()) { apps.push(AppInfo { app_id: m.package.id, diff --git a/crates/weft-ipc-types/Cargo.toml b/crates/weft-ipc-types/Cargo.toml index 8b65cd3..b69064f 100644 --- a/crates/weft-ipc-types/Cargo.toml +++ b/crates/weft-ipc-types/Cargo.toml @@ -12,6 +12,9 @@ path = "src/lib.rs" serde = { version = "1", features = ["derive"] } rmp-serde = "1" toml = "0.8" +sha2 = "0.10" +hex = "0.4" +ed25519-dalek = "2" [lints] workspace = true diff --git a/crates/weft-ipc-types/src/lib.rs b/crates/weft-ipc-types/src/lib.rs index 0590c77..07e3583 100644 --- a/crates/weft-ipc-types/src/lib.rs +++ b/crates/weft-ipc-types/src/lib.rs @@ -1,6 +1,7 @@ pub mod capability; pub mod manifest; pub mod package; +pub mod trust; use serde::{Deserialize, Serialize}; diff --git a/crates/weft-ipc-types/src/package.rs b/crates/weft-ipc-types/src/package.rs index be2a170..6018c3a 100644 --- a/crates/weft-ipc-types/src/package.rs +++ b/crates/weft-ipc-types/src/package.rs @@ -180,13 +180,13 @@ pub fn migrate_app_data(legacy: &Path, target: &Path) -> Result std::io::Result<()> { +pub fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> { use rustix::fs::{CWD, RenameFlags, renameat_with}; renameat_with(CWD, from, CWD, to, RenameFlags::NOREPLACE).map_err(std::io::Error::from) } #[cfg(not(target_os = "linux"))] -fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> { +pub fn rename_no_replace(from: &Path, to: &Path) -> std::io::Result<()> { if std::fs::symlink_metadata(to).is_ok() { return Err(std::io::ErrorKind::AlreadyExists.into()); } diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs new file mode 100644 index 0000000..dc9dfd6 --- /dev/null +++ b/crates/weft-ipc-types/src/trust.rs @@ -0,0 +1,400 @@ +//! Package content identity, publisher keys and the trust decisions made +//! before a package is installed or launched. +//! +//! A package is signed over its content digest: the SHA-256 of a canonical +//! inventory listing every file except the root `signature.sig`, one +//! `\t\n` line per file, sorted by path. Paths use +//! `/` and must be UTF-8. Symbolic links and special files are refused, so the +//! signed bytes are exactly the files a package holds. + +use std::fmt; +use std::path::{Path, PathBuf}; + +use ed25519_dalek::{Signature, Verifier, VerifyingKey}; +use sha2::{Digest, Sha256}; + +/// The signature file at the root of a signed package. +pub const SIGNATURE_FILE: &str = "signature.sig"; + +/// Why a package's content or trust could not be established. +#[derive(Debug)] +pub enum TrustError { + /// The package holds a symbolic link. + Link(PathBuf), + /// The package holds something other than directories and regular files. + NotAFile(PathBuf), + /// A path in the package is not UTF-8. + NonUtf8(PathBuf), + /// A key, signature or owner record is malformed. + Malformed(PathBuf, String), + Io(PathBuf, std::io::Error), +} + +impl fmt::Display for TrustError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Link(p) => write!(f, "{} is a symbolic link", p.display()), + Self::NotAFile(p) => write!(f, "{} is not a regular file or directory", p.display()), + Self::NonUtf8(p) => write!(f, "{} is not a UTF-8 path", p.display()), + Self::Malformed(p, why) => write!(f, "{}: {why}", p.display()), + Self::Io(p, e) => write!(f, "{}: {e}", p.display()), + } + } +} + +impl std::error::Error for TrustError {} + +fn io(path: &Path) -> impl FnOnce(std::io::Error) -> TrustError + '_ { + move |e| TrustError::Io(path.to_path_buf(), e) +} + +/// The SHA-256 content digest a package signature covers. +pub fn content_digest(package_root: &Path) -> Result<[u8; 32], TrustError> { + let mut entries = Vec::new(); + collect(package_root, package_root, &mut entries)?; + entries.sort(); + let mut inventory = String::new(); + for (path, hash) in &entries { + inventory.push_str(path); + inventory.push('\t'); + inventory.push_str(&hex::encode(hash)); + inventory.push('\n'); + } + Ok(Sha256::digest(inventory.as_bytes()).into()) +} + +fn collect( + root: &Path, + dir: &Path, + entries: &mut Vec<(String, [u8; 32])>, +) -> Result<(), TrustError> { + for entry in std::fs::read_dir(dir).map_err(io(dir))? { + let path = entry.map_err(io(dir))?.path(); + let relative = path + .strip_prefix(root) + .expect("read_dir yields children of the directory it reads") + .to_str() + .ok_or_else(|| TrustError::NonUtf8(path.clone()))? + .to_owned(); + let kind = std::fs::symlink_metadata(&path) + .map_err(io(&path))? + .file_type(); + if kind.is_symlink() { + return Err(TrustError::Link(path)); + } else if kind.is_dir() { + collect(root, &path, entries)?; + } else if !kind.is_file() { + return Err(TrustError::NotAFile(path)); + } else if relative != SIGNATURE_FILE { + let bytes = std::fs::read(&path).map_err(io(&path))?; + entries.push((relative, Sha256::digest(&bytes).into())); + } + } + Ok(()) +} + +/// A publisher's Ed25519 public key. +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct PublisherKey(VerifyingKey); + +impl fmt::Debug for PublisherKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "PublisherKey({})", self.to_hex()) + } +} + +impl PublisherKey { + /// Parses 64 hex digits, the format `weft-pack generate-key` writes. + pub fn from_hex(text: &str) -> Result { + let bytes: [u8; 32] = hex::decode(text.trim()) + .map_err(|_| "expected 64 hex digits".to_owned())? + .try_into() + .map_err(|_| "expected a 32-byte key".to_owned())?; + VerifyingKey::from_bytes(&bytes) + .map(Self) + .map_err(|_| "not a valid Ed25519 public key".to_owned()) + } + + pub fn from_file(path: &Path) -> Result { + let text = std::fs::read_to_string(path).map_err(io(path))?; + Self::from_hex(&text).map_err(|why| TrustError::Malformed(path.to_path_buf(), why)) + } + + /// The key as 64 lowercase hex digits; it also identifies the publisher. + pub fn to_hex(&self) -> String { + hex::encode(self.0.as_bytes()) + } + + /// Whether `signature` signs `digest` with this key. + pub fn signed(&self, digest: &[u8; 32], signature: &Signature) -> bool { + self.0.verify(digest, signature).is_ok() + } +} + +/// Reads the package's signature, or `None` when the package is unsigned. +pub fn read_signature(package_root: &Path) -> Result, TrustError> { + let path = package_root.join(SIGNATURE_FILE); + match std::fs::symlink_metadata(&path) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(TrustError::Io(path, e)), + Ok(m) if !m.file_type().is_file() => return Err(TrustError::NotAFile(path)), + Ok(_) => {} + } + let text = std::fs::read_to_string(&path).map_err(io(&path))?; + let bytes: [u8; 64] = hex::decode(text.trim()) + .ok() + .and_then(|b| b.try_into().ok()) + .ok_or_else(|| TrustError::Malformed(path, "expected 128 hex digits".to_owned()))?; + Ok(Some(Signature::from_bytes(&bytes))) +} + +/// The publisher keys this host trusts to sign packages. +#[derive(Debug, Default)] +pub struct TrustStore { + keys: Vec, +} + +impl TrustStore { + /// The trust store directories, in order: `$WEFT_TRUSTED_KEYS` alone when + /// set; otherwise the user's `$XDG_CONFIG_HOME/weft/trusted-keys` (or + /// `~/.config/weft/trusted-keys`) and the system `/etc/weft/trusted-keys`. + pub fn directories() -> Vec { + if let Some(dir) = std::env::var_os("WEFT_TRUSTED_KEYS") { + return vec![PathBuf::from(dir)]; + } + let config = std::env::var_os("XDG_CONFIG_HOME") + .map(PathBuf::from) + .filter(|dir| dir.is_absolute()) + .or_else(|| { + std::env::var_os("HOME") + .map(PathBuf::from) + .filter(|home| home.is_absolute()) + .map(|home| home.join(".config")) + }); + let mut dirs: Vec = config + .map(|dir| dir.join("weft/trusted-keys")) + .into_iter() + .collect(); + dirs.push(PathBuf::from("/etc/weft/trusted-keys")); + dirs + } + + /// Loads every `*.pub` file in `directories`. A missing directory holds no + /// keys; a malformed key file is an error rather than silently ignored. + pub fn load(directories: &[PathBuf]) -> Result { + let mut keys = Vec::new(); + for dir in directories { + let entries = match std::fs::read_dir(dir) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue, + other => other.map_err(io(dir))?, + }; + let mut files: Vec = entries + .map(|e| e.map(|e| e.path()).map_err(io(dir))) + .collect::>()?; + files.sort(); + for file in files + .into_iter() + .filter(|f| f.extension().is_some_and(|e| e == "pub")) + { + keys.push(PublisherKey::from_file(&file)?); + } + } + Ok(Self { keys }) + } + + pub fn keys(&self) -> &[PublisherKey] { + &self.keys + } + + /// The trusted key that signed the package at `package_root`, if any. + pub fn signer(&self, package_root: &Path) -> Result, TrustError> { + let Some(signature) = read_signature(package_root)? else { + return Ok(None); + }; + let digest = content_digest(package_root)?; + Ok(self + .keys + .iter() + .copied() + .find(|k| k.signed(&digest, &signature))) + } +} + +/// Who an application ID belongs to on this host. The record outlives the +/// package while the app's data remains, so another publisher cannot take +/// over the ID, and the data with it, by reusing it. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Owner { + /// Installed from packages signed by this trusted key. + Verified(PublisherKey), + /// Installed as development content, without a trusted signature. + Development, +} + +impl fmt::Display for Owner { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Verified(key) => write!(f, "publisher {}", key.to_hex()), + Self::Development => f.write_str("development installation"), + } + } +} + +/// Where the owner of `app_id` is recorded, beside the app's data. +pub fn owner_record_path(data_home: &Path, app_id: &str) -> PathBuf { + data_home.join("weft/owners").join(app_id) +} + +/// Reads an owner record: `verified ` or `development`. +pub fn read_owner(record: &Path) -> Result, TrustError> { + let text = match std::fs::read_to_string(record) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + other => other.map_err(io(record))?, + }; + let malformed = |why: &str| TrustError::Malformed(record.to_path_buf(), why.to_owned()); + match text.trim().split_once(' ') { + Some(("verified", key)) => PublisherKey::from_hex(key) + .map(|k| Some(Owner::Verified(k))) + .map_err(|why| malformed(&why)), + None if text.trim() == "development" => Ok(Some(Owner::Development)), + _ => Err(malformed("expected 'verified ' or 'development'")), + } +} + +/// Records the owner of an app ID. An existing record is never replaced. +pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { + use std::io::Write; + let parent = record.parent().expect("owner records live in a directory"); + std::fs::create_dir_all(parent).map_err(io(parent))?; + let text = match owner { + Owner::Verified(key) => format!("verified {}\n", key.to_hex()), + Owner::Development => "development\n".to_owned(), + }; + let mut file = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(record) + .map_err(io(record))?; + file.write_all(text.as_bytes()).map_err(io(record))?; + file.sync_all().map_err(io(record)) +} + +#[cfg(test)] +mod tests { + use super::*; + use ed25519_dalek::{Signer, SigningKey}; + + fn dir(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("weft_trust_{name}_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(dir.join("ui")).unwrap(); + std::fs::write(dir.join("wapp.toml"), b"[package]\n").unwrap(); + std::fs::write(dir.join("ui/index.html"), b"

").unwrap(); + dir + } + + fn sign(dir: &Path, seed: u8) -> PublisherKey { + let key = SigningKey::from_bytes(&[seed; 32]); + let signature = key.sign(&content_digest(dir).unwrap()); + std::fs::write(dir.join(SIGNATURE_FILE), hex::encode(signature.to_bytes())).unwrap(); + PublisherKey(key.verifying_key()) + } + + fn store(keys: &[PublisherKey]) -> TrustStore { + TrustStore { + keys: keys.to_vec(), + } + } + + #[test] + fn the_committed_demo_signatures_verify_with_the_demo_key() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let key = PublisherKey::from_file(&root.join("examples/keys/weft-sign.pub")).unwrap(); + for demo in ["org.weft.demo.counter", "org.weft.demo.notes"] { + let package = root.join("examples").join(demo); + assert_eq!(store(&[key]).signer(&package).unwrap(), Some(key), "{demo}"); + } + } + + #[test] + fn any_changed_file_breaks_the_signature() { + let dir = dir("tamper"); + let key = sign(&dir, 1); + assert_eq!(store(&[key]).signer(&dir).unwrap(), Some(key)); + std::fs::write(dir.join("ui/index.html"), b"

changed").unwrap(); + assert_eq!(store(&[key]).signer(&dir).unwrap(), None); + std::fs::write(dir.join("ui/index.html"), b"

").unwrap(); + std::fs::write(dir.join("ui/extra.js"), b"").unwrap(); + assert_eq!(store(&[key]).signer(&dir).unwrap(), None); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn only_trusted_keys_are_signers_and_unsigned_packages_have_none() { + let dir = dir("untrusted"); + assert_eq!(store(&[]).signer(&dir).unwrap(), None); + let key = sign(&dir, 2); + let other = PublisherKey(SigningKey::from_bytes(&[3; 32]).verifying_key()); + assert_eq!(store(&[other]).signer(&dir).unwrap(), None); + assert_eq!(store(&[other, key]).signer(&dir).unwrap(), Some(key)); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[cfg(target_os = "linux")] + #[test] + fn links_and_special_files_have_no_content_digest() { + let dir = dir("links"); + std::os::unix::fs::symlink("/etc/hostname", dir.join("ui/linked")).unwrap(); + assert!(matches!(content_digest(&dir), Err(TrustError::Link(_)))); + std::fs::remove_file(dir.join("ui/linked")).unwrap(); + std::os::unix::fs::symlink(dir.join("ui"), dir.join("view")).unwrap(); + assert!(matches!(content_digest(&dir), Err(TrustError::Link(_)))); + std::fs::remove_file(dir.join("view")).unwrap(); + rustix::fs::mknodat( + rustix::fs::CWD, + dir.join("fifo"), + rustix::fs::FileType::Fifo, + rustix::fs::Mode::from_raw_mode(0o600), + 0, + ) + .unwrap(); + assert!(matches!(content_digest(&dir), Err(TrustError::NotAFile(_)))); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn trust_stores_load_pub_files_and_refuse_malformed_keys() { + let dir = dir("store"); + let keys = dir.join("keys"); + std::fs::create_dir_all(&keys).unwrap(); + let key = PublisherKey(SigningKey::from_bytes(&[4; 32]).verifying_key()); + std::fs::write(keys.join("a.pub"), key.to_hex()).unwrap(); + std::fs::write(keys.join("notes.txt"), "not a key").unwrap(); + let loaded = TrustStore::load(&[keys.clone(), dir.join("missing")]).unwrap(); + assert_eq!(loaded.keys(), [key]); + std::fs::write(keys.join("b.pub"), "zz").unwrap(); + assert!(matches!( + TrustStore::load(&[keys]), + Err(TrustError::Malformed(..)) + )); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn owner_records_round_trip_and_are_never_replaced() { + let dir = dir("owners"); + let key = PublisherKey(SigningKey::from_bytes(&[5; 32]).verifying_key()); + let verified = owner_record_path(&dir, "org.weft.test.a"); + let development = owner_record_path(&dir, "org.weft.test.b"); + assert_eq!(read_owner(&verified).unwrap(), None); + write_owner(&verified, Owner::Verified(key)).unwrap(); + write_owner(&development, Owner::Development).unwrap(); + assert_eq!(read_owner(&verified).unwrap(), Some(Owner::Verified(key))); + assert_eq!(read_owner(&development).unwrap(), Some(Owner::Development)); + assert!(write_owner(&verified, Owner::Development).is_err()); + assert_eq!(read_owner(&verified).unwrap(), Some(Owner::Verified(key))); + std::fs::write(&development, "someone else\n").unwrap(); + assert!(read_owner(&development).is_err()); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 00ca0fc..df905d5 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -3,6 +3,7 @@ use std::path::{Path, PathBuf}; use anyhow::Context; use weft_ipc_types::manifest::{Manifest, entry_path}; use weft_ipc_types::package::ImageFiles; +use weft_ipc_types::trust::{Owner, PublisherKey, TrustStore}; fn main() -> anyhow::Result<()> { let args: Vec = std::env::args().collect(); @@ -19,8 +20,14 @@ fn main() -> anyhow::Result<()> { print_info(&manifest); } Some("install") => { - let dir = args.get(2).context("usage: weft-pack install

")?; - install_package(Path::new(dir))?; + let usage = "usage: weft-pack install [--dev]"; + let dir = args.get(2).context(usage)?; + let mode = match args.get(3).map(String::as_str) { + None => InstallMode::Verified, + Some("--dev") => InstallMode::Development, + Some(other) => anyhow::bail!("unexpected argument '{other}'; {usage}"), + }; + install_package(Path::new(dir), mode)?; } Some("uninstall") => { let app_id = args.get(2).context("usage: weft-pack uninstall ")?; @@ -86,7 +93,7 @@ fn main() -> anyhow::Result<()> { .find(|w| w[0] == "--key") .map(|w| &w[1]) .context("missing --key ")?; - let ok = verify_package(Path::new(dir), Path::new(key))?; + let ok = verify_package(Path::new(dir), &PublisherKey::from_file(Path::new(key))?)?; if ok { println!("OK"); } else { @@ -98,7 +105,12 @@ fn main() -> anyhow::Result<()> { eprintln!("usage:"); eprintln!(" weft-pack check validate a package directory"); eprintln!(" weft-pack info print package metadata"); - eprintln!(" weft-pack install install package to app store"); + eprintln!( + " weft-pack install [--dev] install a package signed by a trusted key" + ); + eprintln!( + " (--dev: unsigned development content)" + ); eprintln!(" weft-pack uninstall remove installed package"); eprintln!(" weft-pack list list installed packages"); eprintln!( @@ -128,6 +140,12 @@ fn check_package(dir: &Path) -> anyhow::Result { } }; + // A package holds only directories and regular files: its signature + // covers exactly those, and installation copies exactly those. + if let Err(e) = weft_ipc_types::trust::content_digest(dir) { + errors.push(e.to_string()); + } + // Earlier versions kept app data in `/data`; the name stays // reserved so package content is never mistaken for user data. if std::fs::symlink_metadata(dir.join("data")).is_ok() { @@ -229,12 +247,22 @@ fn resolve_install_root() -> anyhow::Result { anyhow::bail!("cannot determine install root: HOME and WEFT_APP_STORE are both unset") } -fn install_package(path: &Path) -> anyhow::Result<()> { - let root = resolve_install_root()?; - install_package_to(path, &root) +/// How a package is admitted to the store. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum InstallMode { + /// The package must be signed by a key in the trust store. + Verified, + /// Unsigned or untrusted content, chosen explicitly by a developer and + /// recorded as development content. + Development, } -fn install_package_to(path: &Path, store_root: &Path) -> anyhow::Result<()> { +fn install_package(path: &Path, mode: InstallMode) -> anyhow::Result<()> { + let root = resolve_install_root()?; + install_package_to(path, &root, mode) +} + +fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { let dir_buf; let dir: &Path = if path.extension().is_some_and(|e| e == "zst" || e == "tar") || path.to_string_lossy().ends_with(".app.tar.zst") @@ -280,12 +308,78 @@ fn install_package_to(path: &Path, store_root: &Path) -> anyhow::Result<()> { dest.display() ); } - copy_dir(dir, &dest) - .with_context(|| format!("copy {} -> {}", dir.display(), dest.display()))?; - println!("installed {} -> {}", app_id, dest.display()); + + // The package is copied into the store first and every decision is made + // on that copy, so the bytes that are checked and trusted are the bytes + // that become installed. Only then does one rename make it available. + std::fs::create_dir_all(store_root) + .with_context(|| format!("create {}", store_root.display()))?; + let staging = store_root.join(format!( + ".staging-{app_id}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + let staged = copy_dir(dir, &staging) + .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) + .and_then(|()| admit(&staging, app_id, mode)) + .and_then(|owner| { + weft_ipc_types::package::rename_no_replace(&staging, &dest) + .with_context(|| format!("move {} -> {}", staging.display(), dest.display()))?; + Ok(owner) + }); + let owner = match staged { + Ok(owner) => owner, + Err(e) => { + let _ = std::fs::remove_dir_all(&staging); + return Err(e); + } + }; + println!("installed {} -> {} ({owner})", app_id, dest.display()); Ok(()) } +/// Checks the staged copy of a package and establishes who owns its app ID: +/// the trusted publisher that signed it, or, only when asked, development. +/// The first installation of an ID records its owner, and every later one +/// must have the same owner. +fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { + use weft_ipc_types::trust::{owner_record_path, read_owner, write_owner}; + check_package(staged)?; + let manifest = load_manifest(staged)?; + anyhow::ensure!( + manifest.package.id == app_id, + "the package changed while it was copied" + ); + let owner = match mode { + InstallMode::Development => Owner::Development, + InstallMode::Verified => { + let dirs = TrustStore::directories(); + let signer = TrustStore::load(&dirs)?.signer(staged)?; + Owner::Verified(signer.with_context(|| { + format!( + "{app_id} is not signed by a trusted key (trusted keys are read from {}); \ + install it with --dev to use it as development content", + dirs.iter() + .map(|d| d.display().to_string()) + .collect::>() + .join(", ") + ) + })?) + } + }; + let data_home = weft_ipc_types::package::data_home() + .context("cannot locate the data home to record who owns the app ID")?; + let record = owner_record_path(&data_home, app_id); + match read_owner(&record)? { + Some(existing) if existing == owner => {} + Some(existing) => anyhow::bail!( + "{app_id} belongs to {existing}; this package is {owner}. Its app data stays with \ + its owner, so another publisher or a development build cannot take the ID over" + ), + None => write_owner(&record, owner)?, + } + Ok(owner) +} + fn uninstall_package(app_id: &str) -> anyhow::Result<()> { let root = resolve_install_root()?; uninstall_package_from(app_id, &root) @@ -326,6 +420,14 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> } _ => {} } + // The owner record keeps the app ID, and the data with it, for its owner + // while the data remains; with no data left, the ID is free again. + if let Some(data_home) = weft_ipc_types::package::data_home() + && std::fs::symlink_metadata(weft_ipc_types::package::app_data_dir(&data_home, app_id)) + .is_err() + { + let _ = std::fs::remove_file(weft_ipc_types::trust::owner_record_path(&data_home, app_id)); + } println!("uninstalled {}", app_id); Ok(()) } @@ -416,6 +518,11 @@ fn list_installed() { let Ok(m) = Manifest::read(&entry.path()) else { continue; }; + // Only a directory named after the ID its manifest declares is an + // installed package; staging copies and stray directories are not. + if entry.file_name().to_str() != Some(m.package.id.as_str()) { + continue; + } if seen.insert(m.package.id.clone()) { pkgs.push((m.package.id, m.package.name, m.package.version)); } @@ -431,17 +538,26 @@ fn list_installed() { } } +/// Copies a package directory. Packages hold only directories and regular +/// files, so a symbolic link or special file stops the copy rather than +/// being followed. fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { - std::fs::create_dir_all(dst)?; + std::fs::create_dir(dst).with_context(|| format!("create {}", dst.display()))?; for entry in std::fs::read_dir(src)? { let entry = entry?; let src_path = entry.path(); let dst_path = dst.join(entry.file_name()); - if src_path.is_dir() { + let kind = std::fs::symlink_metadata(&src_path)?.file_type(); + if kind.is_dir() { copy_dir(&src_path, &dst_path)?; - } else { + } else if kind.is_file() { std::fs::copy(&src_path, &dst_path) .with_context(|| format!("copy {}", src_path.display()))?; + } else { + anyhow::bail!( + "{} is not a regular file or directory; packages cannot contain links", + src_path.display() + ); } } Ok(()) @@ -540,47 +656,6 @@ fn unbundle_package(archive: &Path, out_dir: &Path) -> anyhow::Result<()> { Ok(()) } -fn collect_bundle_files( - base: &Path, - dir: &Path, - entries: &mut Vec<(String, [u8; 32])>, -) -> anyhow::Result<()> { - use sha2::{Digest, Sha256}; - for entry in std::fs::read_dir(dir).with_context(|| format!("read dir {}", dir.display()))? { - let entry = entry?; - let path = entry.path(); - let rel = path - .strip_prefix(base) - .context("strip prefix")? - .to_string_lossy() - .into_owned(); - if rel == "signature.sig" { - continue; - } - if path.is_dir() { - collect_bundle_files(base, &path, entries)?; - } else { - let contents = - std::fs::read(&path).with_context(|| format!("read {}", path.display()))?; - let hash: [u8; 32] = Sha256::digest(&contents).into(); - entries.push((rel, hash)); - } - } - Ok(()) -} - -fn canonical_bundle_hash(dir: &Path) -> anyhow::Result<[u8; 32]> { - use sha2::{Digest, Sha256}; - let mut entries: Vec<(String, [u8; 32])> = Vec::new(); - collect_bundle_files(dir, dir, &mut entries)?; - entries.sort_by(|a, b| a.0.cmp(&b.0)); - let mut canonical = String::new(); - for (path, hash) in &entries { - canonical.push_str(&format!("{path}\t{}\n", hex::encode(hash))); - } - Ok(Sha256::digest(canonical.as_bytes()).into()) -} - fn generate_key(output_dir: &Path) -> anyhow::Result<()> { use ed25519_dalek::SigningKey; use rand::rngs::OsRng; @@ -614,34 +689,20 @@ fn sign_package(dir: &Path, key_file: &Path) -> anyhow::Result<()> { .try_into() .map_err(|_| anyhow::anyhow!("signing key must be 32 bytes"))?; let signing_key = SigningKey::from_bytes(&key_bytes); - let hash = canonical_bundle_hash(dir)?; - let signature = signing_key.sign(&hash); - let sig_path = dir.join("signature.sig"); + let digest = weft_ipc_types::trust::content_digest(dir)?; + let signature = signing_key.sign(&digest); + let sig_path = dir.join(weft_ipc_types::trust::SIGNATURE_FILE); std::fs::write(&sig_path, hex::encode(signature.to_bytes())) .with_context(|| format!("write {}", sig_path.display()))?; println!("signed: {}", sig_path.display()); Ok(()) } -fn verify_package(dir: &Path, pub_key_file: &Path) -> anyhow::Result { - use ed25519_dalek::{Signature, Verifier, VerifyingKey}; - let pub_hex = std::fs::read_to_string(pub_key_file) - .with_context(|| format!("read {}", pub_key_file.display()))?; - let pub_bytes: [u8; 32] = hex::decode(pub_hex.trim()) - .context("decode public key: expected 64 hex chars")? - .try_into() - .map_err(|_| anyhow::anyhow!("public key must be 32 bytes"))?; - let verifying_key = VerifyingKey::from_bytes(&pub_bytes).context("invalid public key bytes")?; - let sig_path = dir.join("signature.sig"); - let sig_hex = std::fs::read_to_string(&sig_path) - .with_context(|| format!("read {}", sig_path.display()))?; - let sig_bytes: [u8; 64] = hex::decode(sig_hex.trim()) - .context("decode signature: expected 128 hex chars")? - .try_into() - .map_err(|_| anyhow::anyhow!("signature must be 64 bytes"))?; - let signature = Signature::from_bytes(&sig_bytes); - let hash = canonical_bundle_hash(dir)?; - Ok(verifying_key.verify(&hash, &signature).is_ok()) +fn verify_package(dir: &Path, key: &PublisherKey) -> anyhow::Result { + let signature = weft_ipc_types::trust::read_signature(dir)? + .with_context(|| format!("{} is not signed", dir.display()))?; + let digest = weft_ipc_types::trust::content_digest(dir)?; + Ok(key.signed(&digest, &signature)) } #[cfg(test)] @@ -704,13 +765,187 @@ mod tests { const APP_DATA: &str = "\"fs:rw:app-data\""; + /// Runs `f` like `with_home`, with only the keys in `home/keys` trusted. + fn with_trust(home: &Path, f: impl FnOnce() -> T) -> T { + with_home(home, || { + let prior = std::env::var_os("WEFT_TRUSTED_KEYS"); + // SAFETY: with_home holds env_lock. + unsafe { std::env::set_var("WEFT_TRUSTED_KEYS", home.join("keys")) }; + let result = f(); + unsafe { + match prior { + Some(v) => std::env::set_var("WEFT_TRUSTED_KEYS", v), + None => std::env::remove_var("WEFT_TRUSTED_KEYS"), + } + } + result + }) + } + + /// Writes a signing key derived from `seed` to `home/.key` and + /// returns its path; with `trusted`, its public key joins `home/keys`. + fn key(home: &Path, name: &str, seed: u8, trusted: bool) -> PathBuf { + let signing = ed25519_dalek::SigningKey::from_bytes(&[seed; 32]); + std::fs::create_dir_all(home.join("keys")).unwrap(); + let path = home.join(format!("{name}.key")); + std::fs::write(&path, hex::encode(signing.to_bytes())).unwrap(); + if trusted { + std::fs::write( + home.join("keys").join(format!("{name}.pub")), + hex::encode(signing.verifying_key().to_bytes()), + ) + .unwrap(); + } + path + } + + fn owner_of(home: &Path, app_id: &str) -> Option { + weft_ipc_types::trust::read_owner(&weft_ipc_types::trust::owner_record_path( + &home.join("share"), + app_id, + )) + .unwrap() + } + + fn staging_left(store: &Path) -> bool { + std::fs::read_dir(store).is_ok_and(|entries| { + entries + .flatten() + .any(|e| e.file_name().to_string_lossy().starts_with(".staging-")) + }) + } + + #[test] + fn only_packages_signed_by_a_trusted_key_install_as_verified() { + let home = temp_root("verified"); + let store = home.join("store"); + let app_id = "org.weft.test.verified"; + write_package(&home.join("src"), app_id, ""); + let publisher = key(&home, "publisher", 1, true); + let stranger = key(&home, "stranger", 2, false); + + // Unsigned, then signed by a key the host does not trust. + let unsigned = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + sign_package(&home.join("src"), &stranger).unwrap(); + let untrusted = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + for refused in [&unsigned, &untrusted] { + let message = format!("{:#}", refused.as_ref().unwrap_err()); + assert!(message.contains("not signed by a trusted key"), "{message}"); + } + assert!(!store.join(app_id).exists()); + assert!(!staging_left(&store)); + assert_eq!(owner_of(&home, app_id), None); + + // Signed by a trusted key. + sign_package(&home.join("src"), &publisher).unwrap(); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }) + .unwrap(); + assert!(store.join(app_id).join("signature.sig").exists()); + let key = PublisherKey::from_file(&home.join("keys/publisher.pub")).unwrap(); + assert_eq!(owner_of(&home, app_id), Some(Owner::Verified(key))); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn content_changed_after_signing_is_refused() { + let home = temp_root("tampered"); + let store = home.join("store"); + let app_id = "org.weft.test.tampered"; + write_package(&home.join("src"), app_id, ""); + sign_package(&home.join("src"), &key(&home, "publisher", 3, true)).unwrap(); + std::fs::write(home.join("src/ui/index.html"), "").unwrap(); + let refused = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + assert!(refused.is_err()); + assert!(!store.join(app_id).exists()); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn an_app_id_stays_with_its_first_owner_while_its_data_remains() { + let home = temp_root("owner"); + let store = home.join("store"); + let app_id = "org.weft.test.owned"; + write_package(&home.join("src"), app_id, APP_DATA); + sign_package(&home.join("src"), &key(&home, "first", 4, true)).unwrap(); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }) + .unwrap(); + let data = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); + std::fs::create_dir_all(&data).unwrap(); + with_trust(&home, || uninstall_package_from(app_id, &store)).unwrap(); + + // Another trusted publisher, and a development build, cannot take the + // ID over while the first owner's data remains. + sign_package(&home.join("src"), &key(&home, "second", 5, true)).unwrap(); + let other_publisher = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + let development = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + for refused in [&other_publisher, &development] { + let message = format!("{:#}", refused.as_ref().unwrap_err()); + assert!(message.contains("belongs to publisher"), "{message}"); + } + assert!(!store.join(app_id).exists()); + assert!(!staging_left(&store)); + + // With no data left, uninstall frees the ID. + std::fs::remove_dir_all(&data).unwrap(); + sign_package(&home.join("src"), &home.join("first.key")).unwrap(); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }) + .unwrap(); + with_trust(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert_eq!(owner_of(&home, app_id), None); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(unix)] + #[test] + fn packages_with_links_are_refused() { + let home = temp_root("links"); + let store = home.join("store"); + let app_id = "org.weft.test.links"; + write_package(&home.join("src"), app_id, ""); + std::os::unix::fs::symlink("/etc/hostname", home.join("src/ui/host.txt")).unwrap(); + assert!(check_package(&home.join("src")).is_err()); + let refused = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + assert!(refused.is_err()); + assert!(!store.join(app_id).exists()); + assert!(sign_package(&home.join("src"), &key(&home, "publisher", 6, true)).is_err()); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstall_keeps_app_data_from_the_user_store() { let home = temp_root("keep"); let app_id = "com.example.keep"; write_package(&home.join("src"), app_id, APP_DATA); let store = home.join(".local/share/weft/apps"); - install_package_to(&home.join("src"), &store).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); let legacy = store.join(app_id).join("data"); std::fs::create_dir_all(&legacy).unwrap(); std::fs::write(legacy.join("notes.txt"), "first\nsecond").unwrap(); @@ -730,7 +965,10 @@ mod tests { } // A second copy cannot be merged: uninstall refuses and removes nothing. - install_package_to(&home.join("src"), &store).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); std::fs::create_dir_all(&legacy).unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); assert!(legacy.is_dir() && kept.is_dir()); @@ -744,7 +982,10 @@ mod tests { // No app-data capability: the data directory is left and uninstall stops. write_package(&home.join("src"), app_id, ""); let store = home.join(".local/share/weft/apps"); - install_package_to(&home.join("src"), &store).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); @@ -757,11 +998,14 @@ mod tests { // Another store: earlier versions never kept data there, so it stops too. write_package(&home.join("src2"), app_id, APP_DATA); let other = home.join("other-store"); - install_package_to(&home.join("src2"), &other).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src2"), &other, InstallMode::Development) + }) + .unwrap(); std::fs::create_dir_all(other.join(app_id).join("data")).unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &other)).is_err()); assert!(other.join(app_id).join("data").is_dir()); - assert!(!home.join("share").exists()); + assert!(!home.join("share/weft/app-data").exists()); let _ = std::fs::remove_dir_all(&home); } @@ -808,15 +1052,21 @@ mod tests { #[test] fn packages_cannot_ship_a_top_level_data_entry() { - let root = temp_root("ships-data"); - let src = root.join("src"); - write_package(&src, "com.example.ships", ""); - std::fs::create_dir_all(src.join("data")).unwrap(); - std::fs::write(src.join("data/asset.json"), "{}").unwrap(); - let err = check_package(&src).unwrap_err().to_string(); - assert!(err.contains("'data'"), "{err}"); - assert!(install_package_to(&src, &root.join("store")).is_err()); - let _ = std::fs::remove_dir_all(&root); + let home = temp_root("packages_cannot_ship_a_top_level_data_entry"); + with_home(&home.clone(), move || { + let root = temp_root("ships-data"); + let src = root.join("src"); + write_package(&src, "com.example.ships", ""); + std::fs::create_dir_all(src.join("data")).unwrap(); + std::fs::write(src.join("data/asset.json"), "{}").unwrap(); + let err = check_package(&src).unwrap_err().to_string(); + assert!(err.contains("'data'"), "{err}"); + assert!( + install_package_to(&src, &root.join("store"), InstallMode::Development).is_err() + ); + let _ = std::fs::remove_dir_all(&root); + }); + let _ = std::fs::remove_dir_all(&home); } #[test] @@ -828,7 +1078,10 @@ mod tests { std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); - with_home(&home, || install_package_to(&home.join("src"), &store)).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); assert!(store.join(app_id).join("wapp.toml").exists()); assert!(!store.join(app_id).join("data").exists()); let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); @@ -837,7 +1090,10 @@ mod tests { // An empty directory left behind does not block installation either. with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); std::fs::create_dir_all(store.join(app_id)).unwrap(); - with_home(&home, || install_package_to(&home.join("src"), &store)).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); let _ = std::fs::remove_dir_all(&home); } @@ -970,60 +1226,68 @@ entry = "ui/index.html" #[test] fn install_package_copies_to_store() { - use std::fs; - let id = format!("weft.pack.install{}", std::process::id()); - let src = std::env::temp_dir().join(format!("weft_pack_install_src_{}", id)); - let store = std::env::temp_dir().join(format!("weft_pack_install_store_{}", id)); - let ui_dir = src.join("ui"); - let _ = fs::create_dir_all(&ui_dir); - fs::write(src.join("app.wasm"), b"\0asm").unwrap(); - fs::write(ui_dir.join("index.html"), b"").unwrap(); - let app_id = format!("com.example.t{}", std::process::id()); - fs::write( - src.join("wapp.toml"), - format!( - "[package]\nid = \"{app_id}\"\nname = \"Test\"\nversion = \"1.0.0\"\n\n\ - [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" - ), - ) - .unwrap(); - let result = install_package_to(&src, &store); - assert!(result.is_ok(), "{result:?}"); - assert!(store.join(&app_id).join("app.wasm").exists()); - assert!(store.join(&app_id).join("wapp.toml").exists()); - assert!(store.join(&app_id).join("ui").join("index.html").exists()); - assert!(install_package_to(&src, &store).is_err()); - let _ = fs::remove_dir_all(&src); - let _ = fs::remove_dir_all(&store); + let home = temp_root("install_package_copies_to_store"); + with_home(&home.clone(), move || { + use std::fs; + let id = format!("weft.pack.install{}", std::process::id()); + let src = std::env::temp_dir().join(format!("weft_pack_install_src_{}", id)); + let store = std::env::temp_dir().join(format!("weft_pack_install_store_{}", id)); + let ui_dir = src.join("ui"); + let _ = fs::create_dir_all(&ui_dir); + fs::write(src.join("app.wasm"), b"\0asm").unwrap(); + fs::write(ui_dir.join("index.html"), b"").unwrap(); + let app_id = format!("com.example.t{}", std::process::id()); + fs::write( + src.join("wapp.toml"), + format!( + "[package]\nid = \"{app_id}\"\nname = \"Test\"\nversion = \"1.0.0\"\n\n\ + [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" + ), + ) + .unwrap(); + let result = install_package_to(&src, &store, InstallMode::Development); + assert!(result.is_ok(), "{result:?}"); + assert!(store.join(&app_id).join("app.wasm").exists()); + assert!(store.join(&app_id).join("wapp.toml").exists()); + assert!(store.join(&app_id).join("ui").join("index.html").exists()); + assert!(install_package_to(&src, &store, InstallMode::Development).is_err()); + let _ = fs::remove_dir_all(&src); + let _ = fs::remove_dir_all(&store); + }); + let _ = std::fs::remove_dir_all(&home); } #[test] fn uninstall_package_removes_directory() { - use std::fs; - let id = format!("weft.pack.uninstall{}", std::process::id()); - let src = std::env::temp_dir().join(format!("weft_pack_uninstall_src_{}", id)); - let store = std::env::temp_dir().join(format!("weft_pack_uninstall_store_{}", id)); - let ui_dir = src.join("ui"); - let _ = fs::create_dir_all(&ui_dir); - fs::write(src.join("app.wasm"), b"\0asm").unwrap(); - fs::write(ui_dir.join("index.html"), b"").unwrap(); - let app_id = format!("com.example.u{}", std::process::id()); - fs::write( - src.join("wapp.toml"), - format!( - "[package]\nid = \"{app_id}\"\nname = \"U\"\nversion = \"1.0.0\"\n\n\ - [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" - ), - ) - .unwrap(); - install_package_to(&src, &store).unwrap(); - assert!(store.join(&app_id).exists()); - let result = uninstall_package_from(&app_id, &store); - assert!(result.is_ok(), "{result:?}"); - assert!(!store.join(&app_id).exists()); - assert!(uninstall_package_from(&app_id, &store).is_err()); - let _ = fs::remove_dir_all(&src); - let _ = fs::remove_dir_all(&store); + let home = temp_root("uninstall_package_removes_directory"); + with_home(&home.clone(), move || { + use std::fs; + let id = format!("weft.pack.uninstall{}", std::process::id()); + let src = std::env::temp_dir().join(format!("weft_pack_uninstall_src_{}", id)); + let store = std::env::temp_dir().join(format!("weft_pack_uninstall_store_{}", id)); + let ui_dir = src.join("ui"); + let _ = fs::create_dir_all(&ui_dir); + fs::write(src.join("app.wasm"), b"\0asm").unwrap(); + fs::write(ui_dir.join("index.html"), b"").unwrap(); + let app_id = format!("com.example.u{}", std::process::id()); + fs::write( + src.join("wapp.toml"), + format!( + "[package]\nid = \"{app_id}\"\nname = \"U\"\nversion = \"1.0.0\"\n\n\ + [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" + ), + ) + .unwrap(); + install_package_to(&src, &store, InstallMode::Development).unwrap(); + assert!(store.join(&app_id).exists()); + let result = uninstall_package_from(&app_id, &store); + assert!(result.is_ok(), "{result:?}"); + assert!(!store.join(&app_id).exists()); + assert!(uninstall_package_from(&app_id, &store).is_err()); + let _ = fs::remove_dir_all(&src); + let _ = fs::remove_dir_all(&store); + }); + let _ = std::fs::remove_dir_all(&home); } #[test] @@ -1089,36 +1353,40 @@ entry = "ui/index.html" #[test] fn install_package_from_archive() { - use std::fs; - let id = format!("instarch_{}", std::process::id()); - let src = std::env::temp_dir().join(&id); - let ui = src.join("ui"); - let _ = fs::create_dir_all(&ui); - fs::write(src.join("app.wasm"), b"\0asm\x01\0\0\0").unwrap(); - fs::write(ui.join("index.html"), b"").unwrap(); - let app_id = format!("com.example.ia{}", std::process::id()); - fs::write( - src.join("wapp.toml"), - format!( - "[package]\nid = \"{app_id}\"\nname = \"IA\"\nversion = \"1.0.0\"\n\n\ - [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" - ), - ) - .unwrap(); + let home = temp_root("install_package_from_archive"); + with_home(&home.clone(), move || { + use std::fs; + let id = format!("instarch_{}", std::process::id()); + let src = std::env::temp_dir().join(&id); + let ui = src.join("ui"); + let _ = fs::create_dir_all(&ui); + fs::write(src.join("app.wasm"), b"\0asm\x01\0\0\0").unwrap(); + fs::write(ui.join("index.html"), b"").unwrap(); + let app_id = format!("com.example.ia{}", std::process::id()); + fs::write( + src.join("wapp.toml"), + format!( + "[package]\nid = \"{app_id}\"\nname = \"IA\"\nversion = \"1.0.0\"\n\n\ + [runtime]\nmodule = \"app.wasm\"\n\n[ui]\nentry = \"ui/index.html\"\n" + ), + ) + .unwrap(); - let bundle_dir = std::env::temp_dir().join(format!("{id}_bnd")); - let _ = fs::create_dir_all(&bundle_dir); - bundle_package(&src, Some(&bundle_dir)).unwrap(); - let archive = bundle_dir.join(format!("{app_id}.app.tar.zst")); + let bundle_dir = std::env::temp_dir().join(format!("{id}_bnd")); + let _ = fs::create_dir_all(&bundle_dir); + bundle_package(&src, Some(&bundle_dir)).unwrap(); + let archive = bundle_dir.join(format!("{app_id}.app.tar.zst")); - let store = std::env::temp_dir().join(format!("{id}_store")); - let _ = fs::create_dir_all(&store); - install_package_to(&archive, &store).unwrap(); - assert!(store.join(&app_id).join("app.wasm").exists()); + let store = std::env::temp_dir().join(format!("{id}_store")); + let _ = fs::create_dir_all(&store); + install_package_to(&archive, &store, InstallMode::Development).unwrap(); + assert!(store.join(&app_id).join("app.wasm").exists()); - let _ = fs::remove_dir_all(&src); - let _ = fs::remove_dir_all(&bundle_dir); - let _ = fs::remove_dir_all(&store); + let _ = fs::remove_dir_all(&src); + let _ = fs::remove_dir_all(&bundle_dir); + let _ = fs::remove_dir_all(&store); + }); + let _ = std::fs::remove_dir_all(&home); } #[test] @@ -1181,7 +1449,7 @@ entry = "ui/index.html" sign_package(&dir, &key_file).unwrap(); assert!(dir.join("signature.sig").exists()); - let ok = verify_package(&dir, &pub_file).unwrap(); + let ok = verify_package(&dir, &PublisherKey::from_file(&pub_file).unwrap()).unwrap(); assert!(ok, "signature should verify"); let _ = fs::remove_dir_all(&dir); @@ -1210,7 +1478,11 @@ entry = "ui/index.html" fs::write(dir.join("app.wasm"), b"\0asm\x01\0\0\x01").unwrap(); - let ok = verify_package(&dir, &key_dir.join("weft-sign.pub")).unwrap(); + let ok = verify_package( + &dir, + &PublisherKey::from_file(&key_dir.join("weft-sign.pub")).unwrap(), + ) + .unwrap(); assert!(!ok, "tampered bundle should not verify"); let _ = fs::remove_dir_all(&dir); diff --git a/docs/architecture.md b/docs/architecture.md index ee92a31..4a94562 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 + wasm module), `sign` (Ed25519 signature), `verify` (verify signature), `generate-key`, `install`, `uninstall`, `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), `uninstall`, `list`, `build-image` (EROFS dm-verity), `info`. ### weft-file-portal @@ -102,7 +102,7 @@ Fetch grants name hosts, not addresses: ports are not restricted, and a host tha app.wasm — WASI Component Model binary ui/ index.html — entry point served by weft-app-shell - signature.sig — Ed25519 signature over SHA-256 of (wapp.toml + app.wasm) + signature.sig — Ed25519 signature over the content digest of every other file (see security.md) ``` Package store roots (in priority order): diff --git a/docs/building.md b/docs/building.md index fca337a..141b217 100644 --- a/docs/building.md +++ b/docs/building.md @@ -125,6 +125,16 @@ weft-pack sign ./examples/org.weft.demo.counter --key ./keys/weft-sign.key weft-pack verify ./examples/org.weft.demo.counter --key ./keys/weft-sign.pub ``` +Installation requires a signature by a trusted key. To install the demos with the demo key, trust it for your user (the key is a test fixture; do not trust it on a system you depend on): + +```sh +mkdir -p ~/.config/weft/trusted-keys +cp examples/keys/weft-sign.pub ~/.config/weft/trusted-keys/weft-demo.pub +weft-pack install examples/org.weft.demo.notes +``` + +Unsigned packages under development install with `weft-pack install --dev`, recorded as development content. See `docs/security.md` for the trust store and app ID ownership. + ## NixOS VM (requires Nix with flakes) Build the VM image: diff --git a/docs/security.md b/docs/security.md index 09a6b4a..cd25cc1 100644 --- a/docs/security.md +++ b/docs/security.md @@ -18,11 +18,17 @@ Each app session runs as a separate OS process (`weft-runtime`). When systemd is Apps access the filesystem only through WASI preopened directories. Each capability maps to a specific host path preopened at a fixed guest path. The `weft-file-portal` process applies the same directory grants and access modes, but checks paths lexically: it blocks `..` traversal, not symbolic links. Components cannot currently reach its socket, since the runtime grants them no socket access. -## Package Signing +## Package Signing and Trust -Packages are signed with Ed25519 (`ed25519-dalek`). The signature covers the SHA-256 hash of `wapp.toml` and `app.wasm`. `weft-pack verify` checks the signature before installation. +Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -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`. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. + +The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/`: the publisher key, or development. 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. 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. + +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`). ## Seccomp From d175bf012e574e4dd47d75457c921fa90317ba0f Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 13:59:54 +0000 Subject: [PATCH 02/22] fix(pack): normalize installed modes and never leave a partial or orphaned owner record Installed files take fixed modes (0644, or 0755 when executable) and directories 0755, so a world-writable source cannot install as such. An owner record created by an installation whose package then fails to be placed is removed, and records are written under a temporary name and linked into place, so they are never partial. Archives unpack into a random private directory that is always removed. Uninstall frees an ID only when the app data is truly absent, signatures are checked with verify_strict, and list marks development installs. --- crates/weft-ipc-types/src/trust.rs | 36 +++- crates/weft-pack/src/main.rs | 288 ++++++++++++++++++++++++----- docs/building.md | 2 +- docs/security.md | 4 +- 4 files changed, 268 insertions(+), 62 deletions(-) diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index dc9dfd6..7c00111 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -10,7 +10,7 @@ use std::fmt; use std::path::{Path, PathBuf}; -use ed25519_dalek::{Signature, Verifier, VerifyingKey}; +use ed25519_dalek::{Signature, VerifyingKey}; use sha2::{Digest, Sha256}; /// The signature file at the root of a signed package. @@ -127,7 +127,7 @@ impl PublisherKey { /// Whether `signature` signs `digest` with this key. pub fn signed(&self, digest: &[u8; 32], signature: &Signature) -> bool { - self.0.verify(digest, signature).is_ok() + self.0.verify_strict(digest, signature).is_ok() } } @@ -261,7 +261,9 @@ pub fn read_owner(record: &Path) -> Result, TrustError> { } } -/// Records the owner of an app ID. An existing record is never replaced. +/// Records the owner of an app ID. An existing record is never replaced, and +/// a record is either absent or complete: it is written and synced under a +/// temporary name, then linked into place. pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { use std::io::Write; let parent = record.parent().expect("owner records live in a directory"); @@ -270,13 +272,27 @@ pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { Owner::Verified(key) => format!("verified {}\n", key.to_hex()), Owner::Development => "development\n".to_owned(), }; - let mut file = std::fs::OpenOptions::new() - .write(true) - .create_new(true) - .open(record) - .map_err(io(record))?; - file.write_all(text.as_bytes()).map_err(io(record))?; - file.sync_all().map_err(io(record)) + let name = record + .file_name() + .and_then(|n| n.to_str()) + .unwrap_or("record"); + let temp = parent.join(format!(".{name}.{}.tmp", std::process::id())); + let _ = std::fs::remove_file(&temp); + let written = (|| { + let mut file = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&temp)?; + file.write_all(text.as_bytes())?; + file.sync_all() + })() + .map_err(io(&temp)); + let linked = written.and_then(|()| std::fs::hard_link(&temp, record).map_err(io(record))); + let _ = std::fs::remove_file(&temp); + linked?; + std::fs::File::open(parent) + .and_then(|dir| dir.sync_all()) + .map_err(io(parent)) } #[cfg(test)] diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index df905d5..38131f6 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -263,31 +263,44 @@ fn install_package(path: &Path, mode: InstallMode) -> anyhow::Result<()> { } fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { - let dir_buf; - let dir: &Path = if path.extension().is_some_and(|e| e == "zst" || e == "tar") - || path.to_string_lossy().ends_with(".app.tar.zst") + std::fs::create_dir_all(store_root) + .with_context(|| format!("create {}", store_root.display()))?; + if !(path.extension().is_some_and(|e| e == "zst" || e == "tar") + || path.to_string_lossy().ends_with(".app.tar.zst")) { - let tmp = std::env::temp_dir().join(format!( - "weft-install-{}", - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .subsec_nanos() - )); - std::fs::create_dir_all(&tmp) - .with_context(|| format!("create temp dir {}", tmp.display()))?; - unbundle_package(path, &tmp)?; - let name = path - .file_name() - .and_then(|n| n.to_str()) - .and_then(|n| n.strip_suffix(".app.tar.zst")) - .context("cannot determine app_id from archive filename")?; - dir_buf = tmp.join(name); - &dir_buf - } else { - path - }; + return install_dir(path, store_root, mode); + } + let name = path + .file_name() + .and_then(|n| n.to_str()) + .and_then(|n| n.strip_suffix(".app.tar.zst")) + .context("cannot determine app_id from archive filename")?; + // A fresh private directory in the store, removed whatever happens, so + // nothing can be planted in it and nothing is left behind. + let unpacked = private_scratch_dir(store_root, ".unpack")?; + let result = unbundle_package(path, &unpacked) + .and_then(|()| install_dir(&unpacked.join(name), store_root, mode)); + let _ = std::fs::remove_dir_all(&unpacked); + result +} +/// Creates a new directory with a random name in `parent`, accessible only to +/// the user. +fn private_scratch_dir(parent: &Path, prefix: &str) -> anyhow::Result { + let dir = parent.join(format!( + "{prefix}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + let mut builder = std::fs::DirBuilder::new(); + #[cfg(unix)] + std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700); + builder + .create(&dir) + .with_context(|| format!("create {}", dir.display()))?; + Ok(dir) +} + +fn install_dir(dir: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { check_package(dir)?; let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; @@ -312,36 +325,53 @@ fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyh // The package is copied into the store first and every decision is made // on that copy, so the bytes that are checked and trusted are the bytes // that become installed. Only then does one rename make it available. - std::fs::create_dir_all(store_root) - .with_context(|| format!("create {}", store_root.display()))?; let staging = store_root.join(format!( ".staging-{app_id}-{}", hex::encode(rand::random::<[u8; 8]>()) )); - let staged = copy_dir(dir, &staging) + let admitted = copy_dir(dir, &staging) .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) - .and_then(|()| admit(&staging, app_id, mode)) - .and_then(|owner| { - weft_ipc_types::package::rename_no_replace(&staging, &dest) - .with_context(|| format!("move {} -> {}", staging.display(), dest.display()))?; - Ok(owner) - }); - let owner = match staged { - Ok(owner) => owner, + .and_then(|()| admit(&staging, app_id, mode)); + let admission = match admitted { + Ok(admission) => admission, Err(e) => { let _ = std::fs::remove_dir_all(&staging); return Err(e); } }; + let owner = admission.owner; + place(&staging, &dest, admission)?; println!("installed {} -> {} ({owner})", app_id, dest.display()); Ok(()) } +/// The owner a staged package was admitted under, and the owner record this +/// installation created, if any. +struct Admission { + owner: Owner, + created_record: Option, +} + +/// Makes the staged package available at `dest`. If that fails, the staging +/// copy is removed, and so is an owner record this installation created, so +/// a failed first installation leaves no claim on the app ID. +fn place(staging: &Path, dest: &Path, admission: Admission) -> anyhow::Result<()> { + if let Err(e) = weft_ipc_types::package::rename_no_replace(staging, dest) { + let _ = std::fs::remove_dir_all(staging); + if let Some(record) = &admission.created_record { + let _ = std::fs::remove_file(record); + } + return Err(e).with_context(|| format!("move {} -> {}", staging.display(), dest.display())); + } + Ok(()) +} + /// Checks the staged copy of a package and establishes who owns its app ID: /// the trusted publisher that signed it, or, only when asked, development. /// The first installation of an ID records its owner, and every later one -/// must have the same owner. -fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { +/// must have the same owner. The record is written before the package is +/// placed, so concurrent installations by different owners cannot both win. +fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { use weft_ipc_types::trust::{owner_record_path, read_owner, write_owner}; check_package(staged)?; let manifest = load_manifest(staged)?; @@ -369,15 +399,21 @@ fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result {} + let created_record = match read_owner(&record)? { + Some(existing) if existing == owner => None, Some(existing) => anyhow::bail!( "{app_id} belongs to {existing}; this package is {owner}. Its app data stays with \ its owner, so another publisher or a development build cannot take the ID over" ), - None => write_owner(&record, owner)?, - } - Ok(owner) + None => { + write_owner(&record, owner)?; + Some(record) + } + }; + Ok(Admission { + owner, + created_record, + }) } fn uninstall_package(app_id: &str) -> anyhow::Result<()> { @@ -424,7 +460,7 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> // while the data remains; with no data left, the ID is free again. if let Some(data_home) = weft_ipc_types::package::data_home() && std::fs::symlink_metadata(weft_ipc_types::package::app_data_dir(&data_home, app_id)) - .is_err() + .is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound) { let _ = std::fs::remove_file(weft_ipc_types::trust::owner_record_path(&data_home, app_id)); } @@ -524,7 +560,20 @@ fn list_installed() { continue; } if seen.insert(m.package.id.clone()) { - pkgs.push((m.package.id, m.package.name, m.package.version)); + let development = weft_ipc_types::package::data_home().is_some_and(|home| { + matches!( + weft_ipc_types::trust::read_owner( + &weft_ipc_types::trust::owner_record_path(&home, &m.package.id) + ), + Ok(Some(Owner::Development)) + ) + }); + let version = if development { + format!("{} (development)", m.package.version) + } else { + m.package.version + }; + pkgs.push((m.package.id, m.package.name, version)); } } pkgs.sort_by(|a, b| a.0.cmp(&b.0)); @@ -540,18 +589,25 @@ fn list_installed() { /// Copies a package directory. Packages hold only directories and regular /// files, so a symbolic link or special file stops the copy rather than -/// being followed. +/// being followed. Modes are not copied: the signature does not cover them, +/// so installed directories are 0755 and files 0644, or 0755 when the source +/// is executable, and nothing installed is writable by other users. fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { - std::fs::create_dir(dst).with_context(|| format!("create {}", dst.display()))?; + let mut builder = std::fs::DirBuilder::new(); + #[cfg(unix)] + std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o755); + builder + .create(dst) + .with_context(|| format!("create {}", dst.display()))?; for entry in std::fs::read_dir(src)? { let entry = entry?; let src_path = entry.path(); let dst_path = dst.join(entry.file_name()); - let kind = std::fs::symlink_metadata(&src_path)?.file_type(); - if kind.is_dir() { + let metadata = std::fs::symlink_metadata(&src_path)?; + if metadata.is_dir() { copy_dir(&src_path, &dst_path)?; - } else if kind.is_file() { - std::fs::copy(&src_path, &dst_path) + } else if metadata.is_file() { + copy_file(&src_path, &dst_path, &metadata) .with_context(|| format!("copy {}", src_path.display()))?; } else { anyhow::bail!( @@ -563,6 +619,22 @@ fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { Ok(()) } +fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::Result<()> { + let mut options = std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; + let executable = metadata.permissions().mode() & 0o111 != 0; + options.mode(if executable { 0o755 } else { 0o644 }); + } + #[cfg(not(unix))] + let _ = metadata; + let mut target = options.open(dst)?; + std::io::copy(&mut std::fs::File::open(src)?, &mut target)?; + Ok(()) +} + fn build_image(dir: &Path, out_path: Option<&Path>) -> anyhow::Result<()> { let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; @@ -936,6 +1008,119 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn a_development_id_does_not_become_verified() { + let home = temp_root("dev_owned"); + let store = home.join("store"); + let app_id = "org.weft.test.devowned"; + write_package(&home.join("src"), app_id, APP_DATA); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); + let data = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); + std::fs::create_dir_all(&data).unwrap(); + with_trust(&home, || uninstall_package_from(app_id, &store)).unwrap(); + + sign_package(&home.join("src"), &key(&home, "publisher", 7, true)).unwrap(); + let refused = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Verified) + }); + let message = format!("{:#}", refused.unwrap_err()); + assert!(message.contains("belongs to development"), "{message}"); + assert!(!store.join(app_id).exists()); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_failed_placement_leaves_no_claim_on_the_id() { + let home = temp_root("placement"); + let staging = home.join(".staging-x"); + let dest = home.join("org.weft.test.placed"); + std::fs::create_dir_all(&staging).unwrap(); + std::fs::create_dir_all(dest.join("ui")).unwrap(); + let record = home.join("owners/org.weft.test.placed"); + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + + let admission = Admission { + owner: Owner::Development, + created_record: Some(record.clone()), + }; + assert!(place(&staging, &dest, admission).is_err()); + assert!(!staging.exists()); + assert!(!record.exists()); + assert!( + dest.join("ui").is_dir(), + "the existing package is untouched" + ); + + // A record that existed before this installation is kept. + std::fs::create_dir_all(&staging).unwrap(); + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + let admission = Admission { + owner: Owner::Development, + created_record: None, + }; + assert!(place(&staging, &dest, admission).is_err()); + assert!(record.exists()); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(unix)] + #[test] + fn installed_files_are_not_writable_by_others() { + use std::os::unix::fs::PermissionsExt; + let home = temp_root("modes"); + let store = home.join("store"); + let app_id = "org.weft.test.modes"; + write_package(&home.join("src"), app_id, ""); + let set = |rel: &str, mode: u32| { + std::fs::set_permissions( + home.join("src").join(rel), + std::fs::Permissions::from_mode(mode), + ) + .unwrap() + }; + set("ui/index.html", 0o666); + set("app.wasm", 0o777); + set("ui", 0o777); + with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }) + .unwrap(); + let mode = |rel: &str| { + std::fs::metadata(store.join(app_id).join(rel)) + .unwrap() + .permissions() + .mode() + & 0o777 + }; + assert_eq!(mode("ui/index.html"), 0o644); + assert_eq!(mode("app.wasm"), 0o755); + assert_eq!(mode("ui"), 0o755); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(target_os = "linux")] + #[test] + fn packages_with_non_utf8_names_are_refused() { + use std::os::unix::ffi::OsStrExt; + let home = temp_root("non_utf8"); + let store = home.join("store"); + let app_id = "org.weft.test.nonutf8"; + write_package(&home.join("src"), app_id, ""); + let name = std::ffi::OsStr::from_bytes(b"ui/\xff.html"); + std::fs::write(home.join("src").join(name), b"").unwrap(); + let refused = with_trust(&home, || { + install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + assert!(refused.is_err()); + assert!(!store.join(app_id).exists()); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstall_keeps_app_data_from_the_user_store() { let home = temp_root("keep"); @@ -1381,6 +1566,11 @@ entry = "ui/index.html" let _ = fs::create_dir_all(&store); install_package_to(&archive, &store, InstallMode::Development).unwrap(); assert!(store.join(&app_id).join("app.wasm").exists()); + let scratch_left = fs::read_dir(&store).unwrap().flatten().any(|e| { + let name = e.file_name().to_string_lossy().into_owned(); + name.starts_with(".unpack") || name.starts_with(".staging-") + }); + assert!(!scratch_left, "archive install left scratch directories"); let _ = fs::remove_dir_all(&src); let _ = fs::remove_dir_all(&bundle_dir); diff --git a/docs/building.md b/docs/building.md index 141b217..5afe1d9 100644 --- a/docs/building.md +++ b/docs/building.md @@ -133,7 +133,7 @@ cp examples/keys/weft-sign.pub ~/.config/weft/trusted-keys/weft-demo.pub weft-pack install examples/org.weft.demo.notes ``` -Unsigned packages under development install with `weft-pack install --dev`, recorded as development content. See `docs/security.md` for the trust store and app ID ownership. +Unsigned packages under development install with `weft-pack install --dev`, recorded as development content and marked `(development)` by `weft-pack list`. See `docs/security.md` for the trust store and app ID ownership. ## NixOS VM (requires Nix with flakes) diff --git a/docs/security.md b/docs/security.md index cd25cc1..deba623 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,9 +22,9 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. -The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/`: the publisher key, or development. 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. The demo key in `examples/keys` is a test fixture, not a signing authority. +The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/`: the publisher key, or development. 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. 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. 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. From 39f7ca2a3f016fc6099cafa802fe5cb2ff24b329 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:04:53 +0000 Subject: [PATCH 03/22] fix(pack): close inventory forgery, serialize ownership changes and refuse to follow links Names with control characters have no content digest, so a crafted name can no longer reproduce other inventory lines and drop signed files. Installs and uninstalls of an ID hold a per-ID lock, so a failed concurrent install cannot remove the record of the one that won. App data without a recorded owner goes to a new owner only with --claim-data. Uninstall releases a record left by an interrupted install once no data remains. Files are opened without following links and, on Linux, must be the file that was listed. --- Cargo.lock | 1 + crates/weft-ipc-types/src/trust.rs | 62 ++++++++- crates/weft-pack/Cargo.toml | 1 + crates/weft-pack/src/main.rs | 193 +++++++++++++++++++++++++---- docs/security.md | 6 +- 5 files changed, 232 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc4cf59..65456f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10542,6 +10542,7 @@ dependencies = [ "anyhow", "ed25519-dalek", "hex", + "libc", "rand 0.8.5", "sha2", "tar", diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index 7c00111..0d52d8e 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -4,8 +4,9 @@ //! A package is signed over its content digest: the SHA-256 of a canonical //! inventory listing every file except the root `signature.sig`, one //! `\t\n` line per file, sorted by path. Paths use -//! `/` and must be UTF-8. Symbolic links and special files are refused, so the -//! signed bytes are exactly the files a package holds. +//! `/` and must be UTF-8 without control characters, so no name can imitate +//! the separators of other lines. Symbolic links and special files are +//! refused, so the signed bytes are exactly the files a package holds. use std::fmt; use std::path::{Path, PathBuf}; @@ -25,6 +26,8 @@ pub enum TrustError { NotAFile(PathBuf), /// A path in the package is not UTF-8. NonUtf8(PathBuf), + /// A name in the package holds a control character. + ControlCharacter(PathBuf), /// A key, signature or owner record is malformed. Malformed(PathBuf, String), Io(PathBuf, std::io::Error), @@ -36,6 +39,9 @@ impl fmt::Display for TrustError { Self::Link(p) => write!(f, "{} is a symbolic link", p.display()), Self::NotAFile(p) => write!(f, "{} is not a regular file or directory", p.display()), Self::NonUtf8(p) => write!(f, "{} is not a UTF-8 path", p.display()), + Self::ControlCharacter(p) => { + write!(f, "{:?} has a control character in its name", p.display()) + } Self::Malformed(p, why) => write!(f, "{}: {why}", p.display()), Self::Io(p, e) => write!(f, "{}: {e}", p.display()), } @@ -76,6 +82,9 @@ fn collect( .to_str() .ok_or_else(|| TrustError::NonUtf8(path.clone()))? .to_owned(); + if relative.chars().any(char::is_control) { + return Err(TrustError::ControlCharacter(path)); + } let kind = std::fs::symlink_metadata(&path) .map_err(io(&path))? .file_type(); @@ -295,6 +304,22 @@ pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { .map_err(io(parent)) } +/// Takes the lock that serialises every change to the installation and +/// owner record of `app_id`, held until the returned file is dropped. +pub fn lock_owner(data_home: &Path, app_id: &str) -> Result { + let dir = data_home.join("weft/owners"); + std::fs::create_dir_all(&dir).map_err(io(&dir))?; + let path = dir.join(format!(".{app_id}.lock")); + let file = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(false) + .open(&path) + .map_err(io(&path))?; + file.lock().map_err(io(&path))?; + Ok(file) +} + #[cfg(test)] mod tests { use super::*; @@ -356,6 +381,39 @@ mod tests { std::fs::remove_dir_all(&dir).unwrap(); } + #[cfg(unix)] + #[test] + fn names_cannot_imitate_other_inventory_lines() { + // With files ui/policy.txt and ui/z.css signed, a directory named + // "ui/policy.txt\t\nui" holding z.css would reproduce the + // inventory without policy.txt. Such names have no digest. + let dir = dir("forged"); + std::fs::write(dir.join("ui/policy.txt"), b"default-src 'self'").unwrap(); + std::fs::write(dir.join("ui/z.css"), b"p{}").unwrap(); + let signed = content_digest(&dir).unwrap(); + let policy_hash = hex::encode(Sha256::digest(b"default-src 'self'")); + std::fs::remove_file(dir.join("ui/policy.txt")).unwrap(); + let forged = dir.join(format!("ui/policy.txt\t{policy_hash}\nui")); + std::fs::create_dir_all(&forged).unwrap(); + std::fs::rename(dir.join("ui/z.css"), forged.join("z.css")).unwrap(); + let refused = content_digest(&dir); + assert!(matches!(refused, Err(TrustError::ControlCharacter(_)))); + assert_ne!(refused.ok(), Some(signed)); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn owner_locks_exclude_each_other() { + let home = std::env::temp_dir().join(format!("weft_trust_lock_{}", std::process::id())); + let held = lock_owner(&home, "org.weft.test.lock").unwrap(); + let path = home.join("weft/owners/.org.weft.test.lock.lock"); + let other = std::fs::OpenOptions::new().write(true).open(&path).unwrap(); + assert!(other.try_lock().is_err()); + drop(held); + assert!(other.try_lock().is_ok()); + std::fs::remove_dir_all(&home).unwrap(); + } + #[cfg(target_os = "linux")] #[test] fn links_and_special_files_have_no_content_digest() { diff --git a/crates/weft-pack/Cargo.toml b/crates/weft-pack/Cargo.toml index c580df4..ab8db04 100644 --- a/crates/weft-pack/Cargo.toml +++ b/crates/weft-pack/Cargo.toml @@ -15,5 +15,6 @@ ed25519-dalek = { version = "2", features = ["rand_core"] } sha2 = "0.10" rand = "0.8" hex = "0.4" +libc = "0.2" tar = "0.4" zstd = "0.13" diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 38131f6..b3f5ee7 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -20,14 +20,18 @@ fn main() -> anyhow::Result<()> { print_info(&manifest); } Some("install") => { - let usage = "usage: weft-pack install [--dev]"; + let usage = "usage: weft-pack install [--dev] [--claim-data]"; let dir = args.get(2).context(usage)?; - let mode = match args.get(3).map(String::as_str) { - None => InstallMode::Verified, - Some("--dev") => InstallMode::Development, - Some(other) => anyhow::bail!("unexpected argument '{other}'; {usage}"), - }; - install_package(Path::new(dir), mode)?; + let mut mode = InstallMode::Verified; + let mut claim_data = false; + for arg in &args[3..] { + match arg.as_str() { + "--dev" => mode = InstallMode::Development, + "--claim-data" => claim_data = true, + other => anyhow::bail!("unexpected argument '{other}'; {usage}"), + } + } + install_package(Path::new(dir), mode, claim_data)?; } Some("uninstall") => { let app_id = args.get(2).context("usage: weft-pack uninstall ")?; @@ -109,7 +113,10 @@ fn main() -> anyhow::Result<()> { " weft-pack install [--dev] install a package signed by a trusted key" ); eprintln!( - " (--dev: unsigned development content)" + " (--dev: unsigned development content;" + ); + eprintln!( + " --claim-data: adopt app data with no owner)" ); eprintln!(" weft-pack uninstall remove installed package"); eprintln!(" weft-pack list list installed packages"); @@ -257,18 +264,32 @@ enum InstallMode { Development, } -fn install_package(path: &Path, mode: InstallMode) -> anyhow::Result<()> { +fn install_package(path: &Path, mode: InstallMode, claim_data: bool) -> anyhow::Result<()> { let root = resolve_install_root()?; - install_package_to(path, &root, mode) + install_into(path, &root, mode, claim_data) } +#[cfg(test)] fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { + install_into(path, store_root, mode, false) +} + +/// Installs the package at `path` into `store_root`. App data that exists +/// for the ID without a recorded owner, as left by an installation that +/// predates owner records, goes to this package's owner only with +/// `claim_data`. +fn install_into( + path: &Path, + store_root: &Path, + mode: InstallMode, + claim_data: bool, +) -> anyhow::Result<()> { std::fs::create_dir_all(store_root) .with_context(|| format!("create {}", store_root.display()))?; if !(path.extension().is_some_and(|e| e == "zst" || e == "tar") || path.to_string_lossy().ends_with(".app.tar.zst")) { - return install_dir(path, store_root, mode); + return install_dir(path, store_root, mode, claim_data); } let name = path .file_name() @@ -279,7 +300,7 @@ fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyh // nothing can be planted in it and nothing is left behind. let unpacked = private_scratch_dir(store_root, ".unpack")?; let result = unbundle_package(path, &unpacked) - .and_then(|()| install_dir(&unpacked.join(name), store_root, mode)); + .and_then(|()| install_dir(&unpacked.join(name), store_root, mode, claim_data)); let _ = std::fs::remove_dir_all(&unpacked); result } @@ -300,11 +321,21 @@ fn private_scratch_dir(parent: &Path, prefix: &str) -> anyhow::Result { Ok(dir) } -fn install_dir(dir: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { +fn install_dir( + dir: &Path, + store_root: &Path, + mode: InstallMode, + claim_data: bool, +) -> anyhow::Result<()> { check_package(dir)?; let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; let dest = store_root.join(app_id); + let data_home = weft_ipc_types::package::data_home() + .context("cannot locate the data home to record who owns the app ID")?; + // Installations and uninstallations of one ID run one at a time, so the + // owner record always describes the package that is placed. + let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; // An earlier weft-appd could leave only app data, or an empty directory, // where the package now goes; move the data to its own location so it // neither blocks nor joins the package. @@ -329,9 +360,14 @@ fn install_dir(dir: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Resu ".staging-{app_id}-{}", hex::encode(rand::random::<[u8; 8]>()) )); - let admitted = copy_dir(dir, &staging) + // Copied from the resolved source path, which every file opened during + // the copy is checked against. + let admitted = dir + .canonicalize() + .with_context(|| format!("resolve {}", dir.display())) + .and_then(|source| copy_dir(&source, &staging)) .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) - .and_then(|()| admit(&staging, app_id, mode)); + .and_then(|()| admit(&staging, app_id, mode, &data_home, claim_data)); let admission = match admitted { Ok(admission) => admission, Err(e) => { @@ -371,7 +407,13 @@ fn place(staging: &Path, dest: &Path, admission: Admission) -> anyhow::Result<() /// The first installation of an ID records its owner, and every later one /// must have the same owner. The record is written before the package is /// placed, so concurrent installations by different owners cannot both win. -fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { +fn admit( + staged: &Path, + app_id: &str, + mode: InstallMode, + data_home: &Path, + claim_data: bool, +) -> anyhow::Result { use weft_ipc_types::trust::{owner_record_path, read_owner, write_owner}; check_package(staged)?; let manifest = load_manifest(staged)?; @@ -396,9 +438,7 @@ fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result None, Some(existing) => anyhow::bail!( @@ -406,6 +446,14 @@ fn admit(staged: &Path, app_id: &str, mode: InstallMode) -> anyhow::Result { + let data = weft_ipc_types::package::app_data_dir(data_home, app_id); + if std::fs::symlink_metadata(&data).is_ok() && !claim_data { + anyhow::bail!( + "{} holds app data for {app_id} with no recorded owner; install with \ + --claim-data to give it to {owner}, or move it aside", + data.display() + ); + } write_owner(&record, owner)?; Some(record) } @@ -425,8 +473,22 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> if !weft_ipc_types::package::is_valid_app_id(app_id) { anyhow::bail!("'{}' is not a valid app ID", app_id); } + let data_home = weft_ipc_types::package::data_home(); + let _lock = match &data_home { + Some(home) => Some(weft_ipc_types::trust::lock_owner(home, app_id)?), + None => None, + }; let target = store_root.join(app_id); if !target.exists() { + // An installation interrupted between recording its owner and placing + // the package leaves a record with nothing installed; with no data + // either, it holds the ID for nothing and is released. + if let Some(home) = &data_home + && release_owner_without_data(home, app_id) + { + println!("released {app_id}, which was not installed"); + return Ok(()); + } anyhow::bail!( "package '{}' is not installed at {}", app_id, @@ -458,16 +520,23 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> } // The owner record keeps the app ID, and the data with it, for its owner // while the data remains; with no data left, the ID is free again. - if let Some(data_home) = weft_ipc_types::package::data_home() - && std::fs::symlink_metadata(weft_ipc_types::package::app_data_dir(&data_home, app_id)) - .is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound) - { - let _ = std::fs::remove_file(weft_ipc_types::trust::owner_record_path(&data_home, app_id)); + if let Some(home) = &data_home { + release_owner_without_data(home, app_id); } println!("uninstalled {}", app_id); Ok(()) } +/// Removes the owner record of `app_id` when the app has no data, and +/// reports whether a record was removed. +fn release_owner_without_data(data_home: &Path, app_id: &str) -> bool { + let no_data = + std::fs::symlink_metadata(weft_ipc_types::package::app_data_dir(data_home, app_id)) + .is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound); + no_data + && std::fs::remove_file(weft_ipc_types::trust::owner_record_path(data_home, app_id)).is_ok() +} + /// Whether a package directory is empty or contains nothing but `data`. fn only_holds_app_data(package_dir: &Path) -> anyhow::Result { let entries = std::fs::read_dir(package_dir) @@ -620,6 +689,7 @@ fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { } fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::Result<()> { + let source = open_regular_file(src)?; let mut options = std::fs::OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] @@ -631,10 +701,36 @@ fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::R #[cfg(not(unix))] let _ = metadata; let mut target = options.open(dst)?; - std::io::copy(&mut std::fs::File::open(src)?, &mut target)?; + std::io::copy(&mut &source, &mut target)?; Ok(()) } +/// Opens `src` for reading only if it is still the regular file it was when +/// listed: the last component is not followed, and on Linux the opened file +/// must be at `src` itself, so a link swapped in for any directory on the way +/// cannot redirect the copy outside the package. +fn open_regular_file(src: &Path) -> std::io::Result { + let mut options = std::fs::OpenOptions::new(); + options.read(true); + #[cfg(unix)] + std::os::unix::fs::OpenOptionsExt::custom_flags(&mut options, libc::O_NOFOLLOW); + let file = options.open(src)?; + let changed = + || std::io::Error::other(format!("{} changed while it was copied", src.display())); + if !file.metadata()?.is_file() { + return Err(changed()); + } + #[cfg(target_os = "linux")] + { + use std::os::fd::AsRawFd; + let opened = std::fs::read_link(format!("/proc/self/fd/{}", file.as_raw_fd()))?; + if opened != src { + return Err(changed()); + } + } + Ok(file) +} + fn build_image(dir: &Path, out_path: Option<&Path>) -> anyhow::Result<()> { let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; @@ -1121,6 +1217,43 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn uninstall_releases_a_record_left_by_an_interrupted_install() { + let home = temp_root("interrupted"); + let store = home.join("store"); + let app_id = "org.weft.test.interrupted"; + let record = weft_ipc_types::trust::owner_record_path(&home.join("share"), app_id); + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + let data = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); + std::fs::create_dir_all(&data).unwrap(); + + // While data remains the record stays, and uninstall reports nothing + // installed. + assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); + assert!(record.exists()); + std::fs::remove_dir_all(&data).unwrap(); + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert!(!record.exists()); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(target_os = "linux")] + #[test] + fn copies_never_follow_links_swapped_in() { + let home = temp_root("swapped"); + std::fs::create_dir_all(home.join("outside")).unwrap(); + std::fs::write(home.join("outside/secret"), b"secret").unwrap(); + std::fs::create_dir_all(home.join("pkg")).unwrap(); + let root = home.canonicalize().unwrap(); + // A file replaced by a link, and a directory replaced by a link. + std::os::unix::fs::symlink(root.join("outside/secret"), root.join("pkg/file")).unwrap(); + std::os::unix::fs::symlink(root.join("outside"), root.join("pkg/dir")).unwrap(); + assert!(open_regular_file(&root.join("pkg/file")).is_err()); + assert!(open_regular_file(&root.join("pkg/dir/secret")).is_err()); + assert!(open_regular_file(&root.join("outside/secret")).is_ok()); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstall_keeps_app_data_from_the_user_store() { let home = temp_root("keep"); @@ -1263,8 +1396,16 @@ mod tests { std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); - with_home(&home, || { + // The moved data has no recorded owner, so it is given to this + // package only when asked. + let unclaimed = with_home(&home, || { install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + let message = format!("{:#}", unclaimed.unwrap_err()); + assert!(message.contains("--claim-data"), "{message}"); + assert!(!store.join(app_id).join("wapp.toml").exists()); + with_home(&home, || { + install_into(&home.join("src"), &store, InstallMode::Development, true) }) .unwrap(); assert!(store.join(app_id).join("wapp.toml").exists()); diff --git a/docs/security.md b/docs/security.md index deba623..6fcab73 100644 --- a/docs/security.md +++ b/docs/security.md @@ -20,11 +20,11 @@ Apps access the filesystem only through WASI preopened directories. Each capabil ## Package Signing and Trust -Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. +Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Files are copied without following links, and on Linux each opened file must be the file the copy listed. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. -The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/`: the publisher key, or development. 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. 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. The demo key in `examples/keys` is a test fixture, not a signing authority. +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 hold a per-ID lock, so the record always describes the package placed. 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. 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. From c64ee531ee3db6dcaa10a57e5fdd775d8d60a503 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:13:18 +0000 Subject: [PATCH 04/22] fix(pack): take the app ID from the staged copy and check copied file identity The app ID now comes from the staged copy, so a source changed after validation cannot steer paths outside the store or the owners directory, and lock_owner refuses invalid IDs. Copied files must keep the device and inode they were listed with, and are opened without following links or blocking on FIFOs, which no longer depends on /proc. Uninstall keeps an owner record while another known store holds the package, and a failure to inspect app data refuses rather than counting as no data. --- Cargo.lock | 1 + crates/weft-ipc-types/src/trust.rs | 9 +- crates/weft-pack/Cargo.toml | 3 + crates/weft-pack/src/main.rs | 202 +++++++++++++++++++---------- docs/security.md | 4 +- 5 files changed, 150 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 65456f1..d3077df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10544,6 +10544,7 @@ dependencies = [ "hex", "libc", "rand 0.8.5", + "rustix 1.1.4", "sha2", "tar", "weft-ipc-types", diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index 0d52d8e..a6e8c45 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -249,7 +249,8 @@ impl fmt::Display for Owner { } } -/// Where the owner of `app_id` is recorded, beside the app's data. +/// Where the owner of `app_id` is recorded, beside the app's data. Callers +/// pass a valid app ID, which cannot leave the owners directory. pub fn owner_record_path(data_home: &Path, app_id: &str) -> PathBuf { data_home.join("weft/owners").join(app_id) } @@ -308,6 +309,12 @@ pub fn write_owner(record: &Path, owner: Owner) -> Result<(), TrustError> { /// owner record of `app_id`, held until the returned file is dropped. pub fn lock_owner(data_home: &Path, app_id: &str) -> Result { let dir = data_home.join("weft/owners"); + if !crate::package::is_valid_app_id(app_id) { + return Err(TrustError::Malformed( + dir, + format!("'{app_id}' is not a valid app ID"), + )); + } std::fs::create_dir_all(&dir).map_err(io(&dir))?; let path = dir.join(format!(".{app_id}.lock")); let file = std::fs::OpenOptions::new() diff --git a/crates/weft-pack/Cargo.toml b/crates/weft-pack/Cargo.toml index ab8db04..deacf4e 100644 --- a/crates/weft-pack/Cargo.toml +++ b/crates/weft-pack/Cargo.toml @@ -18,3 +18,6 @@ hex = "0.4" libc = "0.2" tar = "0.4" zstd = "0.13" + +[target.'cfg(target_os = "linux")'.dev-dependencies] +rustix = { version = "1", features = ["fs"] } diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index b3f5ee7..454073f 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -110,7 +110,7 @@ fn main() -> anyhow::Result<()> { eprintln!(" weft-pack check validate a package directory"); eprintln!(" weft-pack info print package metadata"); eprintln!( - " weft-pack install [--dev] install a package signed by a trusted key" + " weft-pack install [--dev] [--claim-data] install a package signed by a trusted key" ); eprintln!( " (--dev: unsigned development content;" @@ -328,7 +328,34 @@ fn install_dir( claim_data: bool, ) -> anyhow::Result<()> { check_package(dir)?; - let manifest = load_manifest(dir)?; + // The package is copied into the store first and every decision, the app + // ID included, is made on that copy, so the bytes that are checked and + // trusted are the bytes that become installed. Only then does one rename + // make it available. + let staging = store_root.join(format!( + ".staging-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + let result = dir + .canonicalize() + .with_context(|| format!("resolve {}", dir.display())) + .and_then(|source| copy_dir(&source, &staging)) + .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) + .and_then(|()| install_staged(&staging, store_root, mode, claim_data)); + if result.is_err() { + let _ = std::fs::remove_dir_all(&staging); + } + result +} + +fn install_staged( + staging: &Path, + store_root: &Path, + mode: InstallMode, + claim_data: bool, +) -> anyhow::Result<()> { + check_package(staging)?; + let manifest = load_manifest(staging)?; let app_id = &manifest.package.id; let dest = store_root.join(app_id); let data_home = weft_ipc_types::package::data_home() @@ -352,31 +379,9 @@ fn install_dir( dest.display() ); } - - // The package is copied into the store first and every decision is made - // on that copy, so the bytes that are checked and trusted are the bytes - // that become installed. Only then does one rename make it available. - let staging = store_root.join(format!( - ".staging-{app_id}-{}", - hex::encode(rand::random::<[u8; 8]>()) - )); - // Copied from the resolved source path, which every file opened during - // the copy is checked against. - let admitted = dir - .canonicalize() - .with_context(|| format!("resolve {}", dir.display())) - .and_then(|source| copy_dir(&source, &staging)) - .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) - .and_then(|()| admit(&staging, app_id, mode, &data_home, claim_data)); - let admission = match admitted { - Ok(admission) => admission, - Err(e) => { - let _ = std::fs::remove_dir_all(&staging); - return Err(e); - } - }; + let admission = admit(staging, app_id, mode, &data_home, claim_data)?; let owner = admission.owner; - place(&staging, &dest, admission)?; + place(staging, &dest, admission)?; println!("installed {} -> {} ({owner})", app_id, dest.display()); Ok(()) } @@ -402,7 +407,7 @@ fn place(staging: &Path, dest: &Path, admission: Admission) -> anyhow::Result<() Ok(()) } -/// Checks the staged copy of a package and establishes who owns its app ID: +/// Establishes who owns the app ID of a checked, staged package: /// the trusted publisher that signed it, or, only when asked, development. /// The first installation of an ID records its owner, and every later one /// must have the same owner. The record is written before the package is @@ -415,12 +420,6 @@ fn admit( claim_data: bool, ) -> anyhow::Result { use weft_ipc_types::trust::{owner_record_path, read_owner, write_owner}; - check_package(staged)?; - let manifest = load_manifest(staged)?; - anyhow::ensure!( - manifest.package.id == app_id, - "the package changed while it was copied" - ); let owner = match mode { InstallMode::Development => Owner::Development, InstallMode::Verified => { @@ -447,7 +446,12 @@ fn admit( ), None => { let data = weft_ipc_types::package::app_data_dir(data_home, app_id); - if std::fs::symlink_metadata(&data).is_ok() && !claim_data { + let no_data = match std::fs::symlink_metadata(&data) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => true, + Err(e) => return Err(e).with_context(|| format!("inspect {}", data.display())), + Ok(_) => false, + }; + if !no_data && !claim_data { anyhow::bail!( "{} holds app data for {app_id} with no recorded owner; install with \ --claim-data to give it to {owner}, or move it aside", @@ -484,6 +488,7 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> // the package leaves a record with nothing installed; with no data // either, it holds the ID for nothing and is released. if let Some(home) = &data_home + && !installed_in_another_store(app_id, store_root) && release_owner_without_data(home, app_id) { println!("released {app_id}, which was not installed"); @@ -520,13 +525,31 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> } // The owner record keeps the app ID, and the data with it, for its owner // while the data remains; with no data left, the ID is free again. - if let Some(home) = &data_home { + if let Some(home) = &data_home + && !installed_in_another_store(app_id, store_root) + { release_owner_without_data(home, app_id); } println!("uninstalled {}", app_id); Ok(()) } +/// Whether `app_id` is installed in a known store other than `store_root`. +/// Owner records belong to the data home, not to a store, so a record stays +/// while any store this host knows holds the package. +fn installed_in_another_store(app_id: &str, store_root: &Path) -> bool { + let mut roots = list_installed_roots(); + roots.extend( + ["/usr/share/weft/apps"].iter().map(PathBuf::from).chain( + std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/share/weft/apps")), + ), + ); + roots + .iter() + .filter(|root| root.as_path() != store_root) + .any(|root| std::fs::symlink_metadata(root.join(app_id)).is_ok()) +} + /// Removes the owner record of `app_id` when the app has no data, and /// reports whether a record was removed. fn release_owner_without_data(data_home: &Path, app_id: &str) -> bool { @@ -625,7 +648,9 @@ fn list_installed() { }; // Only a directory named after the ID its manifest declares is an // installed package; staging copies and stray directories are not. - if entry.file_name().to_str() != Some(m.package.id.as_str()) { + if entry.file_name().to_str() != Some(m.package.id.as_str()) + || !weft_ipc_types::package::is_valid_app_id(&m.package.id) + { continue; } if seen.insert(m.package.id.clone()) { @@ -689,7 +714,7 @@ fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { } fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::Result<()> { - let source = open_regular_file(src)?; + let source = open_regular_file(src, metadata)?; let mut options = std::fs::OpenOptions::new(); options.write(true).create_new(true); #[cfg(unix)] @@ -698,35 +723,38 @@ fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::R let executable = metadata.permissions().mode() & 0o111 != 0; options.mode(if executable { 0o755 } else { 0o644 }); } - #[cfg(not(unix))] - let _ = metadata; let mut target = options.open(dst)?; std::io::copy(&mut &source, &mut target)?; Ok(()) } -/// Opens `src` for reading only if it is still the regular file it was when -/// listed: the last component is not followed, and on Linux the opened file -/// must be at `src` itself, so a link swapped in for any directory on the way -/// cannot redirect the copy outside the package. -fn open_regular_file(src: &Path) -> std::io::Result { +/// Opens `src` for reading only if it is still the regular file that was +/// listed as `listed`: the last component is not followed, a FIFO or device +/// swapped in is not waited on, and the opened file must have the listed +/// device and inode, so a link swapped in for any directory on the way, or +/// another file renamed into place, cannot redirect the copy. +fn open_regular_file(src: &Path, listed: &std::fs::Metadata) -> std::io::Result { let mut options = std::fs::OpenOptions::new(); options.read(true); #[cfg(unix)] - std::os::unix::fs::OpenOptionsExt::custom_flags(&mut options, libc::O_NOFOLLOW); + std::os::unix::fs::OpenOptionsExt::custom_flags( + &mut options, + libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_NOCTTY, + ); let file = options.open(src)?; - let changed = - || std::io::Error::other(format!("{} changed while it was copied", src.display())); - if !file.metadata()?.is_file() { - return Err(changed()); - } - #[cfg(target_os = "linux")] - { - use std::os::fd::AsRawFd; - let opened = std::fs::read_link(format!("/proc/self/fd/{}", file.as_raw_fd()))?; - if opened != src { - return Err(changed()); - } + let opened = file.metadata()?; + #[cfg(unix)] + let same = { + use std::os::unix::fs::MetadataExt; + opened.dev() == listed.dev() && opened.ino() == listed.ino() + }; + #[cfg(not(unix))] + let same = listed.is_file(); + if !opened.is_file() || !same { + return Err(std::io::Error::other(format!( + "{} changed while it was copied", + src.display() + ))); } Ok(file) } @@ -1237,20 +1265,62 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn a_record_stays_while_another_store_holds_the_package() { + let home = temp_root("other_store"); + let app_id = "org.weft.test.otherstore"; + write_package(&home.join("src"), app_id, ""); + let user_store = home.join(".local/share/weft/apps"); + let elsewhere = home.join("elsewhere"); + with_home(&home, || { + install_package_to(&home.join("src"), &user_store, InstallMode::Development) + }) + .unwrap(); + assert!(with_home(&home, || uninstall_package_from(app_id, &elsewhere)).is_err()); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + with_home(&home, || uninstall_package_from(app_id, &user_store)).unwrap(); + assert_eq!(owner_of(&home, app_id), None); + let _ = std::fs::remove_dir_all(&home); + } + #[cfg(target_os = "linux")] #[test] fn copies_never_follow_links_swapped_in() { let home = temp_root("swapped"); - std::fs::create_dir_all(home.join("outside")).unwrap(); + for dir in ["outside", "pkg/dir", "pkg/held"] { + std::fs::create_dir_all(home.join(dir)).unwrap(); + } std::fs::write(home.join("outside/secret"), b"secret").unwrap(); - std::fs::create_dir_all(home.join("pkg")).unwrap(); - let root = home.canonicalize().unwrap(); - // A file replaced by a link, and a directory replaced by a link. - std::os::unix::fs::symlink(root.join("outside/secret"), root.join("pkg/file")).unwrap(); - std::os::unix::fs::symlink(root.join("outside"), root.join("pkg/dir")).unwrap(); - assert!(open_regular_file(&root.join("pkg/file")).is_err()); - assert!(open_regular_file(&root.join("pkg/dir/secret")).is_err()); - assert!(open_regular_file(&root.join("outside/secret")).is_ok()); + std::fs::write(home.join("pkg/file"), b"listed").unwrap(); + std::fs::write(home.join("pkg/dir/secret"), b"listed").unwrap(); + let listed = |rel: &str| std::fs::symlink_metadata(home.join(rel)).unwrap(); + let (file, in_dir) = (listed("pkg/file"), listed("pkg/dir/secret")); + assert!(open_regular_file(&home.join("pkg/file"), &file).is_ok()); + + // The listed file moved aside and replaced by a link, then by a hard + // link to another existing file. + std::fs::rename(home.join("pkg/file"), home.join("pkg/held/file")).unwrap(); + std::os::unix::fs::symlink(home.join("outside/secret"), home.join("pkg/file")).unwrap(); + assert!(open_regular_file(&home.join("pkg/file"), &file).is_err()); + std::fs::remove_file(home.join("pkg/file")).unwrap(); + std::fs::hard_link(home.join("outside/secret"), home.join("pkg/file")).unwrap(); + assert!(open_regular_file(&home.join("pkg/file"), &file).is_err()); + + // A listed directory replaced by a link to one holding the same name. + std::fs::rename(home.join("pkg/dir"), home.join("pkg/held/dir")).unwrap(); + std::os::unix::fs::symlink(home.join("outside"), home.join("pkg/dir")).unwrap(); + assert!(open_regular_file(&home.join("pkg/dir/secret"), &in_dir).is_err()); + + // A FIFO swapped in is refused without waiting for a writer. + rustix::fs::mknodat( + rustix::fs::CWD, + home.join("pkg/fifo"), + rustix::fs::FileType::Fifo, + rustix::fs::Mode::from_raw_mode(0o600), + 0, + ) + .unwrap(); + assert!(open_regular_file(&home.join("pkg/fifo"), &file).is_err()); let _ = std::fs::remove_dir_all(&home); } diff --git a/docs/security.md b/docs/security.md index 6fcab73..c2a9110 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,9 +22,9 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Files are copied without following links, and on Linux each opened file must be the file the copy listed. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Files are copied without following links or waiting on special files, and each opened file must have the device and inode the copy listed, so a link or another existing file swapped in during the copy is refused. The app ID is read from the staged copy. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. -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 hold a per-ID lock, so the record always describes the package placed. 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. The demo key in `examples/keys` is a test fixture, not a signing authority. +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. From 181aa0fefa05d107eed6e8df6c9b157de4117d19 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:17:09 +0000 Subject: [PATCH 05/22] fix(pack): copy packages by directory descriptor so swapped links cannot redirect the copy Checking each file's device and inode against a path-based listing still followed a directory replaced by a link after it was listed. The copy now holds each source directory open and opens its entries relative to it with O_NOFOLLOW, checking type, device and inode against the listing. Only real packages in other stores keep an owner record, the store list is deduplicated, archive names must be valid app IDs, and the help text is wrapped. --- Cargo.lock | 1 - crates/weft-ipc-types/src/trust.rs | 2 + crates/weft-pack/Cargo.toml | 3 +- crates/weft-pack/src/main.rs | 249 +++++++++++++++++------------ docs/security.md | 2 +- 5 files changed, 154 insertions(+), 103 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d3077df..c42e600 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10542,7 +10542,6 @@ dependencies = [ "anyhow", "ed25519-dalek", "hex", - "libc", "rand 0.8.5", "rustix 1.1.4", "sha2", diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index a6e8c45..2f3ea9f 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -418,6 +418,8 @@ mod tests { assert!(other.try_lock().is_err()); drop(held); assert!(other.try_lock().is_ok()); + assert!(lock_owner(&home, "../outside").is_err()); + assert!(!home.join("weft/outside.lock").exists()); std::fs::remove_dir_all(&home).unwrap(); } diff --git a/crates/weft-pack/Cargo.toml b/crates/weft-pack/Cargo.toml index deacf4e..8890949 100644 --- a/crates/weft-pack/Cargo.toml +++ b/crates/weft-pack/Cargo.toml @@ -15,9 +15,8 @@ ed25519-dalek = { version = "2", features = ["rand_core"] } sha2 = "0.10" rand = "0.8" hex = "0.4" -libc = "0.2" tar = "0.4" zstd = "0.13" -[target.'cfg(target_os = "linux")'.dev-dependencies] +[target.'cfg(unix)'.dependencies] rustix = { version = "1", features = ["fs"] } diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 454073f..1823865 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -109,14 +109,15 @@ fn main() -> anyhow::Result<()> { eprintln!("usage:"); eprintln!(" weft-pack check validate a package directory"); eprintln!(" weft-pack info print package metadata"); + eprintln!(" weft-pack install [--dev] [--claim-data]"); eprintln!( - " weft-pack install [--dev] [--claim-data] install a package signed by a trusted key" + " install a package signed by a trusted key" ); eprintln!( - " (--dev: unsigned development content;" + " --dev: as unsigned development content" ); eprintln!( - " --claim-data: adopt app data with no owner)" + " --claim-data: adopt app data with no owner" ); eprintln!(" weft-pack uninstall remove installed package"); eprintln!(" weft-pack list list installed packages"); @@ -295,7 +296,8 @@ fn install_into( .file_name() .and_then(|n| n.to_str()) .and_then(|n| n.strip_suffix(".app.tar.zst")) - .context("cannot determine app_id from archive filename")?; + .filter(|n| weft_ipc_types::package::is_valid_app_id(n)) + .context("the archive must be named .app.tar.zst")?; // A fresh private directory in the store, removed whatever happens, so // nothing can be planted in it and nothing is left behind. let unpacked = private_scratch_dir(store_root, ".unpack")?; @@ -539,15 +541,16 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> /// while any store this host knows holds the package. fn installed_in_another_store(app_id: &str, store_root: &Path) -> bool { let mut roots = list_installed_roots(); - roots.extend( - ["/usr/share/weft/apps"].iter().map(PathBuf::from).chain( - std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/share/weft/apps")), - ), - ); + roots.push(PathBuf::from("/usr/share/weft/apps")); + roots.extend(std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/share/weft/apps"))); + roots.sort(); + roots.dedup(); + // Only a package counts, as in `list`: a leftover empty or data-only + // directory does not hold the ID. roots .iter() .filter(|root| root.as_path() != store_root) - .any(|root| std::fs::symlink_metadata(root.join(app_id)).is_ok()) + .any(|root| Manifest::read(&root.join(app_id)).is_ok_and(|m| m.package.id == app_id)) } /// Removes the owner record of `app_id` when the app has no data, and @@ -683,82 +686,94 @@ fn list_installed() { /// Copies a package directory. Packages hold only directories and regular /// files, so a symbolic link or special file stops the copy rather than -/// being followed. Modes are not copied: the signature does not cover them, -/// so installed directories are 0755 and files 0644, or 0755 when the source -/// is executable, and nothing installed is writable by other users. +/// being followed. The walk holds each directory open and opens its entries +/// relative to it without following links, checking that each opened entry +/// has the device and inode it was listed with, so a link or another file +/// swapped in anywhere in the source during the copy cannot redirect it. +/// Modes are not copied: the signature does not cover them, so installed +/// directories are 0755 and files 0644, or 0755 when the source is +/// executable, and nothing installed is writable by other users. +#[cfg(unix)] fn copy_dir(src: &Path, dst: &Path) -> anyhow::Result<()> { - let mut builder = std::fs::DirBuilder::new(); - #[cfg(unix)] - std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o755); - builder + use rustix::fs::{CWD, Mode, OFlags}; + let root = rustix::fs::openat( + CWD, + src, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::empty(), + ) + .with_context(|| format!("open {}", src.display()))?; + copy_tree(&root, src, dst) +} + +#[cfg(not(unix))] +fn copy_dir(_src: &Path, _dst: &Path) -> anyhow::Result<()> { + anyhow::bail!("installing packages requires a Unix host") +} + +#[cfg(unix)] +fn copy_tree(dir: &rustix::fd::OwnedFd, shown: &Path, dst: &Path) -> anyhow::Result<()> { + use rustix::fs::{AtFlags, FileType, Mode, OFlags}; + use std::os::unix::ffi::OsStrExt; + use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt}; + + std::fs::DirBuilder::new() + .mode(0o755) .create(dst) .with_context(|| format!("create {}", dst.display()))?; - for entry in std::fs::read_dir(src)? { - let entry = entry?; - let src_path = entry.path(); - let dst_path = dst.join(entry.file_name()); - let metadata = std::fs::symlink_metadata(&src_path)?; - if metadata.is_dir() { - copy_dir(&src_path, &dst_path)?; - } else if metadata.is_file() { - copy_file(&src_path, &dst_path, &metadata) - .with_context(|| format!("copy {}", src_path.display()))?; - } else { - anyhow::bail!( + let mut entries = + rustix::fs::Dir::read_from(dir).with_context(|| format!("read {}", shown.display()))?; + while let Some(entry) = entries.read() { + let entry = entry.with_context(|| format!("read {}", shown.display()))?; + let name = entry.file_name(); + if matches!(name.to_bytes(), b"." | b"..") { + continue; + } + let src_path = shown.join(std::ffi::OsStr::from_bytes(name.to_bytes())); + let dst_path = dst.join(std::ffi::OsStr::from_bytes(name.to_bytes())); + let listed = rustix::fs::statat(dir, name, AtFlags::SYMLINK_NOFOLLOW) + .with_context(|| format!("inspect {}", src_path.display()))?; + let kind = FileType::from_raw_mode(listed.st_mode); + let flags = match kind { + FileType::Directory => OFlags::DIRECTORY, + FileType::RegularFile => OFlags::NONBLOCK | OFlags::NOCTTY, + _ => anyhow::bail!( "{} is not a regular file or directory; packages cannot contain links", src_path.display() - ); + ), + }; + let opened = rustix::fs::openat( + dir, + name, + flags | OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::empty(), + ) + .with_context(|| format!("open {}", src_path.display()))?; + let found = rustix::fs::fstat(&opened)?; + anyhow::ensure!( + FileType::from_raw_mode(found.st_mode) == kind + && found.st_dev == listed.st_dev + && found.st_ino == listed.st_ino, + "{} changed while it was copied", + src_path.display() + ); + if kind == FileType::Directory { + copy_tree(&opened, &src_path, &dst_path)?; + } else { + let executable = found.st_mode & 0o111 != 0; + let mut target = std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .mode(if executable { 0o755 } else { 0o644 }) + .open(&dst_path) + .with_context(|| format!("create {}", dst_path.display()))?; + std::io::copy(&mut std::fs::File::from(opened), &mut target) + .with_context(|| format!("copy {}", src_path.display()))?; } } Ok(()) } -fn copy_file(src: &Path, dst: &Path, metadata: &std::fs::Metadata) -> std::io::Result<()> { - let source = open_regular_file(src, metadata)?; - let mut options = std::fs::OpenOptions::new(); - options.write(true).create_new(true); - #[cfg(unix)] - { - use std::os::unix::fs::{OpenOptionsExt, PermissionsExt}; - let executable = metadata.permissions().mode() & 0o111 != 0; - options.mode(if executable { 0o755 } else { 0o644 }); - } - let mut target = options.open(dst)?; - std::io::copy(&mut &source, &mut target)?; - Ok(()) -} - -/// Opens `src` for reading only if it is still the regular file that was -/// listed as `listed`: the last component is not followed, a FIFO or device -/// swapped in is not waited on, and the opened file must have the listed -/// device and inode, so a link swapped in for any directory on the way, or -/// another file renamed into place, cannot redirect the copy. -fn open_regular_file(src: &Path, listed: &std::fs::Metadata) -> std::io::Result { - let mut options = std::fs::OpenOptions::new(); - options.read(true); - #[cfg(unix)] - std::os::unix::fs::OpenOptionsExt::custom_flags( - &mut options, - libc::O_NOFOLLOW | libc::O_NONBLOCK | libc::O_NOCTTY, - ); - let file = options.open(src)?; - let opened = file.metadata()?; - #[cfg(unix)] - let same = { - use std::os::unix::fs::MetadataExt; - opened.dev() == listed.dev() && opened.ino() == listed.ino() - }; - #[cfg(not(unix))] - let same = listed.is_file(); - if !opened.is_file() || !same { - return Err(std::io::Error::other(format!( - "{} changed while it was copied", - src.display() - ))); - } - Ok(file) -} - fn build_image(dir: &Path, out_path: Option<&Path>) -> anyhow::Result<()> { let manifest = load_manifest(dir)?; let app_id = &manifest.package.id; @@ -1283,44 +1298,80 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } - #[cfg(target_os = "linux")] + #[test] + fn uninstalling_one_of_two_copies_keeps_the_record() { + let home = temp_root("two_copies"); + let app_id = "org.weft.test.twocopies"; + write_package(&home.join("src"), app_id, ""); + let user_store = home.join(".local/share/weft/apps"); + let other = home.join("other"); + for store in [&user_store, &other] { + with_home(&home, || { + install_package_to(&home.join("src"), store, InstallMode::Development) + }) + .unwrap(); + } + with_home(&home, || uninstall_package_from(app_id, &other)).unwrap(); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + + // A leftover directory that is not a package does not hold the ID. + std::fs::remove_dir_all(user_store.join(app_id)).unwrap(); + std::fs::create_dir_all(user_store.join(app_id)).unwrap(); + with_home(&home, || { + install_package_to(&home.join("src"), &other, InstallMode::Development) + }) + .unwrap(); + with_home(&home, || uninstall_package_from(app_id, &other)).unwrap(); + assert_eq!(owner_of(&home, app_id), None); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(unix)] #[test] fn copies_never_follow_links_swapped_in() { + use rustix::fs::{CWD, Mode, OFlags}; let home = temp_root("swapped"); - for dir in ["outside", "pkg/dir", "pkg/held"] { + for dir in ["outside", "pkg/dir", "held"] { std::fs::create_dir_all(home.join(dir)).unwrap(); } - std::fs::write(home.join("outside/secret"), b"secret").unwrap(); - std::fs::write(home.join("pkg/file"), b"listed").unwrap(); + std::fs::write(home.join("outside/secret"), b"outside").unwrap(); std::fs::write(home.join("pkg/dir/secret"), b"listed").unwrap(); - let listed = |rel: &str| std::fs::symlink_metadata(home.join(rel)).unwrap(); - let (file, in_dir) = (listed("pkg/file"), listed("pkg/dir/secret")); - assert!(open_regular_file(&home.join("pkg/file"), &file).is_ok()); + let open_dir = |path: &Path| { + rustix::fs::openat( + CWD, + path, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW, + Mode::empty(), + ) + .unwrap() + }; - // The listed file moved aside and replaced by a link, then by a hard - // link to another existing file. - std::fs::rename(home.join("pkg/file"), home.join("pkg/held/file")).unwrap(); - std::os::unix::fs::symlink(home.join("outside/secret"), home.join("pkg/file")).unwrap(); - assert!(open_regular_file(&home.join("pkg/file"), &file).is_err()); - std::fs::remove_file(home.join("pkg/file")).unwrap(); - std::fs::hard_link(home.join("outside/secret"), home.join("pkg/file")).unwrap(); - assert!(open_regular_file(&home.join("pkg/file"), &file).is_err()); - - // A listed directory replaced by a link to one holding the same name. - std::fs::rename(home.join("pkg/dir"), home.join("pkg/held/dir")).unwrap(); + // A directory replaced by a link after it was opened, before its + // entries are read: the copy reads the directory that was opened. + let dir = open_dir(&home.join("pkg/dir")); + std::fs::rename(home.join("pkg/dir"), home.join("held/dir")).unwrap(); std::os::unix::fs::symlink(home.join("outside"), home.join("pkg/dir")).unwrap(); - assert!(open_regular_file(&home.join("pkg/dir/secret"), &in_dir).is_err()); + copy_tree(&dir, &home.join("pkg/dir"), &home.join("copied")).unwrap(); + assert_eq!( + std::fs::read(home.join("copied/secret")).unwrap(), + b"listed" + ); - // A FIFO swapped in is refused without waiting for a writer. + // A link where an entry is listed is refused, and so is a FIFO, + // without waiting for a writer. + let pkg = open_dir(&home.join("pkg")); + assert!(copy_tree(&pkg, &home.join("pkg"), &home.join("copied_link")).is_err()); + std::fs::remove_file(home.join("pkg/dir")).unwrap(); rustix::fs::mknodat( - rustix::fs::CWD, + CWD, home.join("pkg/fifo"), rustix::fs::FileType::Fifo, - rustix::fs::Mode::from_raw_mode(0o600), + Mode::from_raw_mode(0o600), 0, ) .unwrap(); - assert!(open_regular_file(&home.join("pkg/fifo"), &file).is_err()); + let pkg = open_dir(&home.join("pkg")); + assert!(copy_tree(&pkg, &home.join("pkg"), &home.join("copied_fifo")).is_err()); let _ = std::fs::remove_dir_all(&home); } diff --git a/docs/security.md b/docs/security.md index c2a9110..ce71b88 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,7 +22,7 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. Files are copied without following links or waiting on special files, and each opened file must have the device and inode the copy listed, so a link or another existing file swapped in during the copy is refused. The app ID is read from the staged copy. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, without following links or waiting on special files, and each opened entry must have the device and inode the copy listed, so a link or another existing file swapped in during the copy is refused. The app ID is read from the staged copy. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. 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. From 8221e6fc3dd829e7a2491106d2d49a8bc9410323 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:20:35 +0000 Subject: [PATCH 06/22] fix(pack): set installed modes regardless of umask and hold one descriptor per level Modes requested at creation were masked by the umask, so a root install under umask 077 produced system packages other users could not read. They are now set explicitly after creation. The copy reads each directory's names and closes the listing before recursing, so deep trees hold one descriptor per level. The owner-lock test now uses an ID that would otherwise open, and the docs describe the copy check and its device-node residual precisely. --- crates/weft-ipc-types/src/trust.rs | 5 +++-- crates/weft-pack/src/main.rs | 34 ++++++++++++++++++++++++------ docs/security.md | 2 +- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index 2f3ea9f..79143b5 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -418,8 +418,9 @@ mod tests { assert!(other.try_lock().is_err()); drop(held); assert!(other.try_lock().is_ok()); - assert!(lock_owner(&home, "../outside").is_err()); - assert!(!home.join("weft/outside.lock").exists()); + // A name that would open fine but is not an app ID is refused. + assert!(lock_owner(&home, "Org.weft.test").is_err()); + assert!(!home.join("weft/owners/.Org.weft.test.lock").exists()); std::fs::remove_dir_all(&home).unwrap(); } diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 1823865..cbdc516 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -715,20 +715,32 @@ fn copy_dir(_src: &Path, _dst: &Path) -> anyhow::Result<()> { fn copy_tree(dir: &rustix::fd::OwnedFd, shown: &Path, dst: &Path) -> anyhow::Result<()> { use rustix::fs::{AtFlags, FileType, Mode, OFlags}; use std::os::unix::ffi::OsStrExt; - use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt}; + use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt}; + // Modes are set explicitly rather than requested at creation, so the + // installer's umask cannot make an installed package unreadable. + let set_mode = |path: &Path, mode: u32| { + std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode)) + .with_context(|| format!("set the mode of {}", path.display())) + }; std::fs::DirBuilder::new() - .mode(0o755) + .mode(0o700) .create(dst) .with_context(|| format!("create {}", dst.display()))?; + set_mode(dst, 0o755)?; + // The names are read first and the listing closed, so the walk holds one + // descriptor per directory level. + let mut names = Vec::new(); let mut entries = rustix::fs::Dir::read_from(dir).with_context(|| format!("read {}", shown.display()))?; while let Some(entry) = entries.read() { let entry = entry.with_context(|| format!("read {}", shown.display()))?; - let name = entry.file_name(); - if matches!(name.to_bytes(), b"." | b"..") { - continue; + if !matches!(entry.file_name().to_bytes(), b"." | b"..") { + names.push(entry.file_name().to_owned()); } + } + drop(entries); + for name in &names { let src_path = shown.join(std::ffi::OsStr::from_bytes(name.to_bytes())); let dst_path = dst.join(std::ffi::OsStr::from_bytes(name.to_bytes())); let listed = rustix::fs::statat(dir, name, AtFlags::SYMLINK_NOFOLLOW) @@ -764,11 +776,18 @@ fn copy_tree(dir: &rustix::fd::OwnedFd, shown: &Path, dst: &Path) -> anyhow::Res let mut target = std::fs::OpenOptions::new() .write(true) .create_new(true) - .mode(if executable { 0o755 } else { 0o644 }) + .mode(0o600) .open(&dst_path) .with_context(|| format!("create {}", dst_path.display()))?; std::io::copy(&mut std::fs::File::from(opened), &mut target) .with_context(|| format!("copy {}", src_path.display()))?; + target + .set_permissions(std::fs::Permissions::from_mode(if executable { + 0o755 + } else { + 0o644 + })) + .with_context(|| format!("set the mode of {}", dst_path.display()))?; } } Ok(()) @@ -1334,7 +1353,7 @@ mod tests { for dir in ["outside", "pkg/dir", "held"] { std::fs::create_dir_all(home.join(dir)).unwrap(); } - std::fs::write(home.join("outside/secret"), b"outside").unwrap(); + std::fs::write(home.join("outside/other"), b"outside").unwrap(); std::fs::write(home.join("pkg/dir/secret"), b"listed").unwrap(); let open_dir = |path: &Path| { rustix::fs::openat( @@ -1356,6 +1375,7 @@ mod tests { std::fs::read(home.join("copied/secret")).unwrap(), b"listed" ); + assert!(!home.join("copied/other").exists()); // A link where an entry is listed is refused, and so is a FIFO, // without waiting for a writer. diff --git a/docs/security.md b/docs/security.md index ce71b88..b9f7a2a 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,7 +22,7 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, without following links or waiting on special files, and each opened entry must have the device and inode the copy listed, so a link or another existing file swapped in during the copy is refused. The app ID is read from the staged copy. Installed files take fixed modes, 0644, or 0755 when the source is executable, and directories 0755, so no installed file is writable by other users whatever the source modes were. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices the attacker does not own. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. 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. From d67ea24004fe174c2c5308c6c993c3f68ddec9c1 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:22:37 +0000 Subject: [PATCH 07/22] fix(pack): create a missing store at 0755 regardless of umask A first root install under a restrictive umask created the store and its parents unreadable to other users even though the package inside was 0755/0644. Missing store directories are now created and set to 0755. The copy comment and the hard-link note in the security docs describe exactly what is checked and what the kernel enforces. --- crates/weft-pack/src/main.rs | 33 +++++++++++++++++++++++++++++---- docs/security.md | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index cbdc516..97719ce 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -285,8 +285,7 @@ fn install_into( mode: InstallMode, claim_data: bool, ) -> anyhow::Result<()> { - std::fs::create_dir_all(store_root) - .with_context(|| format!("create {}", store_root.display()))?; + create_store(store_root)?; if !(path.extension().is_some_and(|e| e == "zst" || e == "tar") || path.to_string_lossy().ends_with(".app.tar.zst")) { @@ -307,6 +306,31 @@ fn install_into( result } +/// Creates the store and any missing parents at 0755 whatever the umask, so +/// a store created by a root install is readable by every user. +fn create_store(store_root: &Path) -> anyhow::Result<()> { + let mut missing: Vec<&Path> = store_root + .ancestors() + .take_while(|dir| std::fs::symlink_metadata(dir).is_err()) + .collect(); + missing.reverse(); + for dir in missing { + match std::fs::create_dir(dir) { + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, + other => other.with_context(|| format!("create {}", dir.display()))?, + } + #[cfg(unix)] + std::fs::set_permissions(dir, std::os::unix::fs::PermissionsExt::from_mode(0o755)) + .with_context(|| format!("set the mode of {}", dir.display()))?; + } + anyhow::ensure!( + store_root.is_dir(), + "{} is not a directory", + store_root.display() + ); + Ok(()) +} + /// Creates a new directory with a random name in `parent`, accessible only to /// the user. fn private_scratch_dir(parent: &Path, prefix: &str) -> anyhow::Result { @@ -688,8 +712,9 @@ fn list_installed() { /// files, so a symbolic link or special file stops the copy rather than /// being followed. The walk holds each directory open and opens its entries /// relative to it without following links, checking that each opened entry -/// has the device and inode it was listed with, so a link or another file -/// swapped in anywhere in the source during the copy cannot redirect it. +/// has the type, device and inode it had when examined just before the +/// open, so a link or another file swapped in anywhere in the source during +/// the copy cannot redirect it. /// Modes are not copied: the signature does not cover them, so installed /// directories are 0755 and files 0644, or 0755 when the source is /// executable, and nothing installed is writable by other users. diff --git a/docs/security.md b/docs/security.md index b9f7a2a..8b99453 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,7 +22,7 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices the attacker does not own. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices the attacker cannot both read and write. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users; a store that does not exist yet is created at 0755. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. 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. From 025388f84e7ca278092beaa167338e477dbc2462 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:25:09 +0000 Subject: [PATCH 08/22] fix(pack): create relative stores and set new store modes through a no-follow descriptor A relative store none of whose components existed failed, because the empty ancestor was treated as a directory to create. New store directories are now created private and set to 0755 through a descriptor opened without following links, so a swapped path cannot redirect the mode change. The security docs state the hard-link protection as the kernel enforces it. --- crates/weft-pack/src/main.rs | 50 +++++++++++++++++++++++++++++++++--- docs/security.md | 2 +- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 97719ce..73a5497 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -307,21 +307,37 @@ fn install_into( } /// Creates the store and any missing parents at 0755 whatever the umask, so -/// a store created by a root install is readable by every user. +/// a store created by a root install is readable by every user. Each new +/// directory is created private and its mode set through a descriptor that +/// does not follow links, so only the directory just created is changed. fn create_store(store_root: &Path) -> anyhow::Result<()> { let mut missing: Vec<&Path> = store_root .ancestors() + .filter(|dir| !dir.as_os_str().is_empty()) .take_while(|dir| std::fs::symlink_metadata(dir).is_err()) .collect(); missing.reverse(); for dir in missing { - match std::fs::create_dir(dir) { + let mut builder = std::fs::DirBuilder::new(); + #[cfg(unix)] + std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700); + match builder.create(dir) { Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, other => other.with_context(|| format!("create {}", dir.display()))?, } #[cfg(unix)] - std::fs::set_permissions(dir, std::os::unix::fs::PermissionsExt::from_mode(0o755)) - .with_context(|| format!("set the mode of {}", dir.display()))?; + { + use rustix::fs::{CWD, Mode, OFlags}; + let created = rustix::fs::openat( + CWD, + dir, + OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC, + Mode::empty(), + ) + .with_context(|| format!("open {}", dir.display()))?; + rustix::fs::fchmod(&created, Mode::from_raw_mode(0o755)) + .with_context(|| format!("set the mode of {}", dir.display()))?; + } } anyhow::ensure!( store_root.is_dir(), @@ -1342,6 +1358,32 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[cfg(unix)] + #[test] + fn a_missing_store_is_created_readable_by_everyone() { + use std::os::unix::fs::PermissionsExt; + let home = temp_root("new_store"); + std::fs::create_dir_all(&home).unwrap(); + let mode = |p: &Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777; + // An absolute store with missing parents. + let absolute = home.join("a/b/store"); + create_store(&absolute).unwrap(); + for dir in [home.join("a"), home.join("a/b"), absolute.clone()] { + assert_eq!(mode(&dir), 0o755, "{}", dir.display()); + } + // A relative store none of whose components exist yet. + let relative = PathBuf::from(format!("weft-pack-store-{}/sub", std::process::id())); + let _ = std::fs::remove_dir_all(relative.parent().unwrap()); + create_store(&relative).unwrap(); + assert_eq!(mode(&relative), 0o755); + std::fs::remove_dir_all(relative.parent().unwrap()).unwrap(); + // An existing store is left as it is. + std::fs::set_permissions(&absolute, std::fs::Permissions::from_mode(0o750)).unwrap(); + create_store(&absolute).unwrap(); + assert_eq!(mode(&absolute), 0o750); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstalling_one_of_two_copies_keeps_the_record() { let home = temp_root("two_copies"); diff --git a/docs/security.md b/docs/security.md index 8b99453..5ac7ff2 100644 --- a/docs/security.md +++ b/docs/security.md @@ -22,7 +22,7 @@ Apps access the filesystem only through WASI preopened directories. Each capabil Packages are signed with Ed25519 over their content digest: the SHA-256 of an inventory listing every file in the package except the root `signature.sig`, one `\t` line per file sorted by path. Every file is covered, including the UI. Names must be UTF-8 without control characters, so no name can imitate other inventory lines. Packages may hold only directories and regular files; a symbolic link or special file makes `weft-pack check`, `sign`, `verify` and `install` refuse the package. The committed demo signatures verify under this rule. -`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices the attacker cannot both read and write. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users; a store that does not exist yet is created at 0755. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. +`weft-pack install` admits a package only when its signature verifies with a key in the trust store: `*.pub` files (64 hex digits) in `$XDG_CONFIG_HOME/weft/trusted-keys` and `/etc/weft/trusted-keys`, or only in `$WEFT_TRUSTED_KEYS` when that is set. A malformed key file is an error, not skipped. An unsigned or untrusted package is refused unless the developer passes `--dev`, which installs it as development content. The package is copied into the store first and checked on that copy, which is then renamed into place, so the bytes that are verified are the bytes that are installed. The copy holds each source directory open and opens its entries relative to it, never following links, so replacing a directory with a link while the copy runs cannot redirect it. Each entry must keep the type, device and inode it had when it was examined just before it was opened, and a FIFO is never waited on. A device node hard-linked in place of a file in that interval is opened before it is refused; `fs.protected_hardlinks`, on by default, prevents such links to devices unless the attacker owns them or holds CAP_FOWNER. Installed files are set to 0644, or 0755 when the source is executable, and directories to 0755, whatever the umask, so no installed file is writable by other users; a store, and any of its parents, that does not exist yet is created at 0755. The app ID is read from the staged copy. Archives are unpacked into a fresh private directory in the store, which is always removed. 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. From fb13a99e1731266e92c9ae70445f53cb1271dfcc Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:26:43 +0000 Subject: [PATCH 09/22] fix(pack): remove a new store directory whose mode cannot be set A directory left private after a failed mode change would stay unreadable to other users on every later install, since existing directories are not touched. It is now removed again. The test's relative store is cleaned up even when an assertion fails. --- crates/weft-pack/src/main.rs | 37 +++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 73a5497..b76cbd1 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -308,8 +308,9 @@ fn install_into( /// Creates the store and any missing parents at 0755 whatever the umask, so /// a store created by a root install is readable by every user. Each new -/// directory is created private and its mode set through a descriptor that -/// does not follow links, so only the directory just created is changed. +/// directory is created private and its mode set through a descriptor opened +/// without following a link in its last component; a directory whose mode +/// cannot be set is removed again rather than left private. fn create_store(store_root: &Path) -> anyhow::Result<()> { let mut missing: Vec<&Path> = store_root .ancestors() @@ -328,15 +329,18 @@ fn create_store(store_root: &Path) -> anyhow::Result<()> { #[cfg(unix)] { use rustix::fs::{CWD, Mode, OFlags}; - let created = rustix::fs::openat( + let opened = rustix::fs::openat( CWD, dir, OFlags::RDONLY | OFlags::DIRECTORY | OFlags::NOFOLLOW | OFlags::CLOEXEC, Mode::empty(), - ) - .with_context(|| format!("open {}", dir.display()))?; - rustix::fs::fchmod(&created, Mode::from_raw_mode(0o755)) - .with_context(|| format!("set the mode of {}", dir.display()))?; + ); + if let Err(e) = + opened.and_then(|created| rustix::fs::fchmod(&created, Mode::from_raw_mode(0o755))) + { + let _ = std::fs::remove_dir(dir); + return Err(e).with_context(|| format!("set the mode of {}", dir.display())); + } } } anyhow::ensure!( @@ -1371,12 +1375,23 @@ mod tests { for dir in [home.join("a"), home.join("a/b"), absolute.clone()] { assert_eq!(mode(&dir), 0o755, "{}", dir.display()); } - // A relative store none of whose components exist yet. - let relative = PathBuf::from(format!("weft-pack-store-{}/sub", std::process::id())); - let _ = std::fs::remove_dir_all(relative.parent().unwrap()); + // A relative store none of whose components exist yet, removed even + // if an assertion fails. + struct Removed(PathBuf); + impl Drop for Removed { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } + } + let top = Removed(PathBuf::from(format!( + "weft-pack-store-{}", + std::process::id() + ))); + let _ = std::fs::remove_dir_all(&top.0); + let relative = top.0.join("sub"); create_store(&relative).unwrap(); assert_eq!(mode(&relative), 0o755); - std::fs::remove_dir_all(relative.parent().unwrap()).unwrap(); + drop(top); // An existing store is left as it is. std::fs::set_permissions(&absolute, std::fs::Permissions::from_mode(0o750)).unwrap(); create_store(&absolute).unwrap(); From 11cea5e7e3b0c862c0dc5579d8cef41960608501 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:29:14 +0000 Subject: [PATCH 10/22] 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), From 39aacac8226c56b56f0d98415ef6ab932518897a Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:36:06 +0000 Subject: [PATCH 11/22] fix(appd): bind the launched manifest to the signature and record first owners The manifest is now read once, and its bytes must match the verified inventory, so a manifest swapped in around the check cannot grant capabilities or entries the owner did not sign. A trusted package with no owner record is recorded as its signer's on its first launch, and app data without a recorded owner is refused rather than handed to the first publisher, matching weft-pack's --claim-data rule. A damaged trust store or owner record is a host error (500), and the trust store is not consulted for development content. Supervisor tests use a private data home and an empty trust store; the counter check runs with a private home, and the notes check records the owner of the data it seeds. --- crates/weft-appd/src/launch.rs | 192 +++++++++++++++++++------- crates/weft-appd/src/main.rs | 27 +++- crates/weft-ipc-types/src/manifest.rs | 16 ++- crates/weft-ipc-types/src/trust.rs | 65 +++++++-- docs/security.md | 2 +- tests/frame/check_confinement.py | 2 +- tests/frame/check_counter.py | 6 + tests/frame/check_notes.py | 6 + 8 files changed, 244 insertions(+), 72 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 2e777f5..8f8f2cc 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -7,15 +7,17 @@ //! 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. +//! no record must be signed by a trusted key, which is then recorded as its +//! owner. 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, + Inventory, Owner, PublisherKey, TrustError, TrustStore, lock_owner, owner_record_path, + read_owner, read_signature, write_owner, }; use crate::grants::Refusal; @@ -94,9 +96,14 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result Refusal::new(500, e.to_string()), - ManifestError::Parse(..) => Refusal::new(403, e.to_string()), + // The manifest is read once; the same bytes are parsed and checked + // against the signed inventory, so the capabilities and entries a + // session gets are the ones the owner signed. + let manifest_path = root.join(MANIFEST_FILE); + let manifest_bytes = std::fs::read(&manifest_path) + .map_err(|e| Refusal::new(500, format!("cannot read {}: {e}", manifest_path.display())))?; + let manifest = Manifest::parse(&manifest_path, &manifest_bytes).map_err(|e| match e { + ManifestError::Io(..) | ManifestError::Parse(..) => Refusal::new(403, e.to_string()), })?; if manifest.package.id != app_id { return Err(Refusal::new( @@ -108,7 +115,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result) -> Result Result<(), Refusal> { - let refused = |e: TrustError| match e { +/// Checks that the package at `root`, whose manifest was read as +/// `manifest_bytes`, is what its owner installed. A trusted package with no +/// owner record is recorded as its signer's on its first launch, so its app +/// data stays with that publisher; existing app data with no recorded owner +/// is not handed to whichever publisher launches first. +fn verify_owner(app_id: &str, root: &Path, manifest_bytes: &[u8]) -> Result<(), Refusal> { + // Errors in the host's trust store or owner records are the host's, not + // the package's. + let host = |e: TrustError| Refusal::new(500, e.to_string()); + let package = |e: TrustError| match e { TrustError::Io(..) => Refusal::new(500, e.to_string()), _ => Refusal::new(403, e.to_string()), }; @@ -137,43 +151,90 @@ fn verify_owner(app_id: &str, root: &Path) -> Result<(), Refusal> { "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( + let record = owner_record_path(&data_home, app_id); + let owner = read_owner(&record).map_err(host)?; + if owner == Some(Owner::Development) { + return Ok(()); + } + let trusted = TrustStore::load(&TrustStore::directories()).map_err(host)?; + let inventory = Inventory::read(root).map_err(package)?; + if !inventory.holds(MANIFEST_FILE, manifest_bytes) { + return Err(Refusal::new( + 403, + format!("{} changed while it was checked", root.display()), + )); + } + let digest = inventory.digest(); + let signature = read_signature(root).map_err(package)?; + let signer = signature.and_then(|sig| trusted.signer_of(&digest, &sig)); + match (owner, signer) { + (Some(Owner::Verified(key)), Some(signer)) if key == signer => Ok(()), + (Some(Owner::Verified(key)), _) => 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) => Err(Refusal::new( + 403, + format!( + "{} has no recorded owner and is not signed by a trusted key; install it \ + with weft-pack", + root.display() + ), + )), + (_, Some(signer)) => record_first_owner(app_id, &data_home, &record, signer), + } +} + +/// Records `signer` as the owner of `app_id` on the first launch of a trusted +/// package that has no owner record and no app data yet. +fn record_first_owner( + app_id: &str, + data_home: &Path, + record: &Path, + signer: PublisherKey, +) -> Result<(), Refusal> { + let host = |e: TrustError| Refusal::new(500, e.to_string()); + let _lock = lock_owner(data_home, app_id).map_err(host)?; + match read_owner(record).map_err(host)? { + Some(Owner::Verified(key)) if key == signer => return Ok(()), + Some(existing) => { + return Err(Refusal::new( 403, format!( - "{} has no recorded owner and is not signed by a trusted key; install it \ - with weft-pack", - root.display() + "{app_id} belongs to {existing}, not publisher {}", + signer.to_hex() ), - )), - }, + )); + } + None => {} } + let data = weft_ipc_types::package::app_data_dir(data_home, app_id); + match std::fs::symlink_metadata(&data) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => { + return Err(Refusal::new( + 500, + format!("cannot inspect {}: {e}", data.display()), + )); + } + Ok(_) => { + return Err(Refusal::new( + 403, + format!( + "{} holds app data for {app_id} with no recorded owner; it is not given to \ + publisher {} without weft-pack install --claim-data", + data.display(), + signer.to_hex() + ), + )); + } + } + write_owner(record, Owner::Verified(signer)).map_err(host) } impl LaunchPackage { @@ -369,12 +430,47 @@ mod tests { // 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); + let first = resolve(DEMO).map(|p| p.root); + let owner = read_owner(&owner_record_path(&store.join("share"), DEMO)).unwrap(); + let again = resolve(DEMO).map(|p| p.root); finish(&store); - assert_eq!(unrecorded.ok(), Some(store.join(DEMO))); - assert_eq!(recorded.ok(), Some(store.join(DEMO))); + assert_eq!(first.ok(), Some(store.join(DEMO))); + assert_eq!(owner, Some(Owner::Verified(demo_key()))); + assert_eq!(again.ok(), Some(store.join(DEMO))); + } + + #[test] + fn unowned_app_data_is_not_given_to_the_first_publisher() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("unowned_data", true); + let data = weft_ipc_types::package::app_data_dir(&store.join("share"), DEMO); + std::fs::create_dir_all(&data).unwrap(); + let refused = refusal(resolve(DEMO)); + let owner = read_owner(&owner_record_path(&store.join("share"), DEMO)).unwrap(); + finish(&store); + assert_eq!(refused.code, 403, "{}", refused.message); + assert!( + refused.message.contains("--claim-data"), + "{}", + refused.message + ); + assert_eq!(owner, None); + } + + #[test] + fn a_damaged_trust_store_or_record_is_a_host_error() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("damaged", true); + std::fs::write(store.join("keys/broken.pub"), "not a key").unwrap(); + let bad_key = refusal(resolve(DEMO)); + std::fs::remove_file(store.join("keys/broken.pub")).unwrap(); + let record = owner_record_path(&store.join("share"), DEMO); + std::fs::write(&record, "nonsense\n").unwrap(); + let bad_record = refusal(resolve(DEMO)); + finish(&store); + for refused in [bad_key, bad_record] { + assert_eq!(refused.code, 500, "{}", refused.message); + } } #[test] diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index d0c549d..0df9632 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -716,18 +716,35 @@ mod tests { } /// 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. + /// test process, which XDG_DATA_HOME then points at, with an empty trust + /// store so the host's keys never affect a test. Data homes left by + /// earlier test processes are removed. 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 name = format!("weft-appd-tests-data-{}", std::process::id()); + if let Ok(entries) = std::fs::read_dir(std::env::temp_dir()) { + for entry in entries.flatten() { + let stale = entry + .file_name() + .to_str() + .is_some_and(|n| n.starts_with("weft-appd-tests-data-") && n != name); + if stale { + let _ = std::fs::remove_dir_all(entry.path()); + } + } + } + let data_home = std::env::temp_dir().join(name); + let keys = data_home.join("trusted-keys"); + std::fs::create_dir_all(&keys).unwrap(); 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) }; + unsafe { + std::env::set_var("XDG_DATA_HOME", &data_home); + std::env::set_var("WEFT_TRUSTED_KEYS", &keys); + } } fn make_registry() -> Registry { diff --git a/crates/weft-ipc-types/src/manifest.rs b/crates/weft-ipc-types/src/manifest.rs index 0115bc4..c15ff3b 100644 --- a/crates/weft-ipc-types/src/manifest.rs +++ b/crates/weft-ipc-types/src/manifest.rs @@ -62,9 +62,19 @@ impl Manifest { /// Reads `/wapp.toml`. pub fn read(package_root: &Path) -> Result { let path = package_root.join(MANIFEST_FILE); - let text = - std::fs::read_to_string(&path).map_err(|e| ManifestError::Io(path.clone(), e))?; - toml::from_str(&text).map_err(|e| ManifestError::Parse(path, e)) + let bytes = std::fs::read(&path).map_err(|e| ManifestError::Io(path.clone(), e))?; + Self::parse(&path, &bytes) + } + + /// Parses manifest bytes already read from `path`. + pub fn parse(path: &Path, bytes: &[u8]) -> Result { + let text = std::str::from_utf8(bytes).map_err(|e| { + ManifestError::Io( + path.to_path_buf(), + std::io::Error::new(std::io::ErrorKind::InvalidData, e), + ) + })?; + toml::from_str(text).map_err(|e| ManifestError::Parse(path.to_path_buf(), e)) } pub fn capabilities(&self) -> &[String] { diff --git a/crates/weft-ipc-types/src/trust.rs b/crates/weft-ipc-types/src/trust.rs index 79143b5..0547b5a 100644 --- a/crates/weft-ipc-types/src/trust.rs +++ b/crates/weft-ipc-types/src/trust.rs @@ -56,17 +56,40 @@ fn io(path: &Path) -> impl FnOnce(std::io::Error) -> TrustError + '_ { /// The SHA-256 content digest a package signature covers. pub fn content_digest(package_root: &Path) -> Result<[u8; 32], TrustError> { - let mut entries = Vec::new(); - collect(package_root, package_root, &mut entries)?; - entries.sort(); - let mut inventory = String::new(); - for (path, hash) in &entries { - inventory.push_str(path); - inventory.push('\t'); - inventory.push_str(&hex::encode(hash)); - inventory.push('\n'); + Inventory::read(package_root).map(|inventory| inventory.digest()) +} + +/// The files of a package and their SHA-256 hashes, as read once: the digest +/// and the hash of any one file come from the same reading. +#[derive(Debug)] +pub struct Inventory(Vec<(String, [u8; 32])>); + +impl Inventory { + pub fn read(package_root: &Path) -> Result { + let mut entries = Vec::new(); + collect(package_root, package_root, &mut entries)?; + entries.sort(); + Ok(Self(entries)) + } + + /// The content digest a package signature covers. + pub fn digest(&self) -> [u8; 32] { + let mut inventory = String::new(); + for (path, hash) in &self.0 { + inventory.push_str(path); + inventory.push('\t'); + inventory.push_str(&hex::encode(hash)); + inventory.push('\n'); + } + Sha256::digest(inventory.as_bytes()).into() + } + + /// Whether the file at `path`, relative to the package root, had exactly + /// these bytes when the inventory was read. + pub fn holds(&self, path: &str, bytes: &[u8]) -> bool { + let hash: [u8; 32] = Sha256::digest(bytes).into(); + self.0.iter().any(|(p, h)| p == path && *h == hash) } - Ok(Sha256::digest(inventory.as_bytes()).into()) } fn collect( @@ -220,12 +243,15 @@ impl TrustStore { let Some(signature) = read_signature(package_root)? else { return Ok(None); }; - let digest = content_digest(package_root)?; - Ok(self - .keys + Ok(self.signer_of(&content_digest(package_root)?, &signature)) + } + + /// The trusted key that made `signature` over `digest`, if any. + pub fn signer_of(&self, digest: &[u8; 32], signature: &Signature) -> Option { + self.keys .iter() .copied() - .find(|k| k.signed(&digest, &signature))) + .find(|k| k.signed(digest, signature)) } } @@ -409,6 +435,17 @@ mod tests { std::fs::remove_dir_all(&dir).unwrap(); } + #[test] + fn an_inventory_knows_the_bytes_it_hashed() { + let dir = dir("inventory"); + let inventory = Inventory::read(&dir).unwrap(); + assert_eq!(inventory.digest(), content_digest(&dir).unwrap()); + assert!(inventory.holds("wapp.toml", b"[package]\n")); + assert!(!inventory.holds("wapp.toml", b"[package]\ncapabilities = []\n")); + assert!(!inventory.holds("absent.toml", b"[package]\n")); + std::fs::remove_dir_all(&dir).unwrap(); + } + #[test] fn owner_locks_exclude_each_other() { let home = std::env::temp_dir().join(format!("weft_trust_lock_{}", std::process::id())); diff --git a/docs/security.md b/docs/security.md index 12016fa..24c289d 100644 --- a/docs/security.md +++ b/docs/security.md @@ -26,7 +26,7 @@ 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. -`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. +`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, and that key is then recorded as its owner; if app data already exists for the ID without a recorded owner, the launch is refused rather than handing the data to that publisher. While an ID is recorded as development content, whatever package is found for it runs unverified, from any store. A changed package, or one whose key was removed from the trust store, is refused with code 403; a damaged trust store or owner record is a host error, 500. The manifest is read once and its bytes must be the ones in the verified inventory, so a session's capabilities and entries come from the signed manifest. The module and UI files are read again when the session starts, so a directory install changed by its owning user after the check, or 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. diff --git a/tests/frame/check_confinement.py b/tests/frame/check_confinement.py index d027921..88e76f8 100644 --- a/tests/frame/check_confinement.py +++ b/tests/frame/check_confinement.py @@ -146,7 +146,7 @@ def make_package(store, http_port, ws_port, other_dir): # 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.parent.mkdir(parents=True, exist_ok=True) record.write_text("development\n") diff --git a/tests/frame/check_counter.py b/tests/frame/check_counter.py index 0da4e88..33086ef 100644 --- a/tests/frame/check_counter.py +++ b/tests/frame/check_counter.py @@ -158,11 +158,17 @@ def count_area(screenshot, card): def run(args, desktop, xdotool): port = free_port() target = Path(args.target) + # A private home, so owner records appd writes stay out of the user's. + home = args.output / "home" + shutil.rmtree(home, ignore_errors=True) + home.mkdir(parents=True) desktop.launch_client("appd", [target / "weft-appd"], { "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"), + "HOME": str(home), + "XDG_DATA_HOME": str(home / "share"), "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 6ee3cba..42f3d47 100644 --- a/tests/frame/check_notes.py +++ b/tests/frame/check_notes.py @@ -69,6 +69,12 @@ def run(args, desktop, home, xdotool): notes = data_home / "weft/app-data" / APP_ID / "notes.txt" notes.parent.mkdir(parents=True) notes.write_text(SEED, encoding="utf-8") + # The seeded data belongs to the demo publisher, as installing the signed + # package with weft-pack would have recorded. + owner = data_home / "weft/owners" / APP_ID + owner.parent.mkdir(parents=True) + key = (ROOT / "examples/keys/weft-sign.pub").read_text().strip() + owner.write_text(f"verified {key}\n") t = Path(args.target) desktop.launch_client("appd", [t / "weft-appd"], { From 95db80a86d4c206e3e962197c729c603b74b0099 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:41:23 +0000 Subject: [PATCH 12/22] fix(appd): count app data in the earlier layout as existing before recording an owner App data in the earlier layout, /.local/share/weft/apps//data, is migrated to the app data directory after the owner check, so a trusted package from another store could be recorded as owner on its first launch and then receive that data. Both layouts now count as existing data, in weft-appd and in weft-pack install, so unowned data is only given away with --claim-data. The refusal says how to claim it. Stale test data homes are removed only when their process has exited, and the app-data check records the owner of the data it seeds. --- crates/weft-appd/src/launch.rs | 102 +++++++++++++++++++++------ crates/weft-appd/src/main.rs | 4 +- crates/weft-ipc-types/src/package.rs | 24 +++++++ crates/weft-pack/src/main.rs | 38 ++++++++-- docs/security.md | 2 +- tests/frame/check_app_data.py | 6 ++ 6 files changed, 146 insertions(+), 30 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 8f8f2cc..f12c6bb 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -213,28 +213,29 @@ fn record_first_owner( } None => {} } - let data = weft_ipc_types::package::app_data_dir(data_home, app_id); - match std::fs::symlink_metadata(&data) { - Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} - Err(e) => { - return Err(Refusal::new( - 500, - format!("cannot inspect {}: {e}", data.display()), - )); - } - Ok(_) => { - return Err(Refusal::new( - 403, - format!( - "{} holds app data for {app_id} with no recorded owner; it is not given to \ - publisher {} without weft-pack install --claim-data", - data.display(), - signer.to_hex() - ), - )); - } + // Data in the earlier layout counts: it would be moved to this app's + // data directory when the session starts. + let home = std::env::var_os("HOME") + .map(std::path::PathBuf::from) + .filter(|home| home.is_absolute()); + match weft_ipc_types::package::existing_app_data(data_home, home.as_deref(), app_id) { + Err(e) => Err(Refusal::new( + 500, + format!("cannot inspect the app data of {app_id}: {e}"), + )), + Ok(Some(data)) => Err(Refusal::new( + 403, + format!( + "{} holds app data for {app_id} with no recorded owner, so it is not given to \ + publisher {}. To give it to this publisher, install the package into the user \ + store with weft-pack install --claim-data (uninstalling it there first); \ + otherwise move the data aside", + data.display(), + signer.to_hex() + ), + )), + Ok(None) => write_owner(record, Owner::Verified(signer)).map_err(host), } - write_owner(record, Owner::Verified(signer)).map_err(host) } impl LaunchPackage { @@ -457,6 +458,65 @@ mod tests { assert_eq!(owner, None); } + #[test] + fn unowned_data_in_the_earlier_layout_is_not_given_away() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("legacy_data", true); + let home = store.join("home"); + let legacy = weft_ipc_types::package::legacy_app_data_dir(&home, DEMO); + std::fs::create_dir_all(&legacy).unwrap(); + let prior = std::env::var_os("HOME"); + // SAFETY: env_lock is held. + unsafe { std::env::set_var("HOME", &home) }; + let refused = refusal(resolve(DEMO)); + unsafe { + match prior { + Some(v) => std::env::set_var("HOME", v), + None => std::env::remove_var("HOME"), + } + } + let owner = read_owner(&owner_record_path(&store.join("share"), DEMO)).unwrap(); + finish(&store); + assert_eq!(refused.code, 403, "{}", refused.message); + assert!( + refused.message.contains("no recorded owner"), + "{}", + refused.message + ); + assert_eq!(owner, None); + } + + #[test] + fn only_the_signed_manifest_is_accepted() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("manifest", true); + record(&store, DEMO, Owner::Verified(demo_key())); + let root = store.join(DEMO); + let signed = std::fs::read(root.join(MANIFEST_FILE)).unwrap(); + let mut other = signed.clone(); + other.extend_from_slice(b"\n# other\n"); + let accepted = verify_owner(DEMO, &root, &signed); + let refused = verify_owner(DEMO, &root, &other).err(); + finish(&store); + assert!(accepted.is_ok()); + let refused = refused.expect("a manifest that was not signed was accepted"); + assert_eq!(refused.code, 403, "{}", refused.message); + } + + #[test] + fn development_content_does_not_depend_on_the_trust_store() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = store("dev_store", ID, "app.wasm"); + let keys = store.join("keys"); + std::fs::create_dir_all(&keys).unwrap(); + std::fs::write(keys.join("broken.pub"), "not a key").unwrap(); + // SAFETY: as in `store`. + unsafe { std::env::set_var("WEFT_TRUSTED_KEYS", &keys) }; + let resolved = resolve(ID).map(|p| p.root); + finish(&store); + assert_eq!(resolved.ok(), Some(store.join(ID))); + } + #[test] fn a_damaged_trust_store_or_record_is_a_host_error() { 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 0df9632..3956896 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -723,10 +723,12 @@ mod tests { let name = format!("weft-appd-tests-data-{}", std::process::id()); if let Ok(entries) = std::fs::read_dir(std::env::temp_dir()) { for entry in entries.flatten() { + // Only data homes of test processes that have exited. let stale = entry .file_name() .to_str() - .is_some_and(|n| n.starts_with("weft-appd-tests-data-") && n != name); + .and_then(|n| n.strip_prefix("weft-appd-tests-data-")) + .is_some_and(|pid| !std::path::Path::new("/proc").join(pid).exists()); if stale { let _ = std::fs::remove_dir_all(entry.path()); } diff --git a/crates/weft-ipc-types/src/package.rs b/crates/weft-ipc-types/src/package.rs index 6018c3a..ac9b5b2 100644 --- a/crates/weft-ipc-types/src/package.rs +++ b/crates/weft-ipc-types/src/package.rs @@ -79,6 +79,30 @@ pub fn legacy_app_data_dir(home: &Path, app_id: &str) -> PathBuf { .join("data") } +/// Where app data for `app_id` already exists, in the current layout or in +/// the earlier one under `home`, if anywhere. Data in the earlier layout is +/// moved to the current one when the app next launches, so it counts too. +pub fn existing_app_data( + data_home: &Path, + home: Option<&Path>, + app_id: &str, +) -> std::io::Result> { + let places = std::iter::once(app_data_dir(data_home, app_id)) + .chain(home.map(|home| legacy_app_data_dir(home, app_id))); + for place in places { + match std::fs::symlink_metadata(&place) { + Ok(_) => return Ok(Some(place)), + Err(e) + if matches!( + e.kind(), + std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory + ) => {} + Err(e) => return Err(e), + } + } + Ok(None) +} + /// The result of moving data from the earlier layout. #[derive(Debug, PartialEq, Eq)] pub enum Migration { diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index b76cbd1..9b0cd87 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -491,13 +491,15 @@ fn admit( its owner, so another publisher or a development build cannot take the ID over" ), None => { - let data = weft_ipc_types::package::app_data_dir(data_home, app_id); - let no_data = match std::fs::symlink_metadata(&data) { - Err(e) if e.kind() == std::io::ErrorKind::NotFound => true, - Err(e) => return Err(e).with_context(|| format!("inspect {}", data.display())), - Ok(_) => false, - }; - if !no_data && !claim_data { + // Data in the earlier layout counts: weft-appd moves it to the + // app's data directory when the app next launches. + let home = std::env::var_os("HOME") + .map(PathBuf::from) + .filter(|home| home.is_absolute()); + let existing = + weft_ipc_types::package::existing_app_data(data_home, home.as_deref(), app_id) + .with_context(|| format!("inspect the app data of {app_id}"))?; + if let Some(data) = existing.filter(|_| !claim_data) { anyhow::bail!( "{} holds app data for {app_id} with no recorded owner; install with \ --claim-data to give it to {owner}, or move it aside", @@ -1399,6 +1401,28 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn unowned_data_in_the_earlier_layout_needs_a_claim() { + let home = temp_root("legacy_claim"); + let app_id = "org.weft.test.legacyclaim"; + write_package(&home.join("src"), app_id, APP_DATA); + let legacy = weft_ipc_types::package::legacy_app_data_dir(&home, app_id); + std::fs::create_dir_all(&legacy).unwrap(); + let other = home.join("other"); + let unclaimed = with_home(&home, || { + install_package_to(&home.join("src"), &other, InstallMode::Development) + }); + let message = format!("{:#}", unclaimed.unwrap_err()); + assert!(message.contains("--claim-data"), "{message}"); + assert_eq!(owner_of(&home, app_id), None); + with_home(&home, || { + install_into(&home.join("src"), &other, InstallMode::Development, true) + }) + .unwrap(); + assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstalling_one_of_two_copies_keeps_the_record() { let home = temp_root("two_copies"); diff --git a/docs/security.md b/docs/security.md index 24c289d..8987b0d 100644 --- a/docs/security.md +++ b/docs/security.md @@ -26,7 +26,7 @@ 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. -`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, and that key is then recorded as its owner; if app data already exists for the ID without a recorded owner, the launch is refused rather than handing the data to that publisher. While an ID is recorded as development content, whatever package is found for it runs unverified, from any store. A changed package, or one whose key was removed from the trust store, is refused with code 403; a damaged trust store or owner record is a host error, 500. The manifest is read once and its bytes must be the ones in the verified inventory, so a session's capabilities and entries come from the signed manifest. The module and UI files are read again when the session starts, so a directory install changed by its owning user after the check, or while the session runs, is not detected; verified images are not writable. +`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, and that key is then recorded as its owner; if app data already exists for the ID without a recorded owner, in the current layout or the earlier one that is migrated at launch, the launch is refused rather than handing the data to that publisher; `weft-pack install` applies the same rule unless given `--claim-data`. The record is written before the session starts, so it remains even if that first launch then fails; `weft-pack uninstall` releases it once no data exists and no known store holds the package. While an ID is recorded as development content, whatever package is found for it runs unverified, from any store. A changed package, or one whose key was removed from the trust store, is refused with code 403; a damaged trust store or owner record is a host error, 500. The manifest is read once and its bytes must be the ones in the verified inventory, so a session's capabilities and entries come from the signed manifest. The module and UI files are read again when the session starts, so a directory install changed by its owning user after the check, or 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. diff --git a/tests/frame/check_app_data.py b/tests/frame/check_app_data.py index 50d10e2..d1fad5e 100644 --- a/tests/frame/check_app_data.py +++ b/tests/frame/check_app_data.py @@ -41,6 +41,12 @@ def run(args, desktop, home, xdotool): (legacy / "notes.txt").write_bytes(NOTES) data_home = home / "data-home" target = data_home / "weft/app-data" / APP_ID + # The earlier data belongs to the demo publisher, as installing the + # signed package with weft-pack would have recorded. + owner = data_home / "weft/owners" / APP_ID + owner.parent.mkdir(parents=True) + key = (ROOT / "examples/keys/weft-sign.pub").read_text().strip() + owner.write_text(f"verified {key}\n") t = Path(args.target) desktop.launch_client("appd", [t / "weft-appd"], { From d3adced897b2276e9cc3b77b029fb15e1aaf1480 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:46:21 +0000 Subject: [PATCH 13/22] fix(appd): require a usable HOME for ownership decisions and keep records for earlier-layout data Without an absolute HOME the earlier data layout could not be checked, so a first-launch or install owner record was written anyway and that data was migrated to the new owner later. Both now refuse instead. Uninstall keeps an owner record while data exists in either layout, or while it cannot tell. Launch tests use a private HOME, and the test data-home cleanup never removes the running process's own. --- crates/weft-appd/src/launch.rs | 29 ++++++++++++++-------------- crates/weft-appd/src/main.rs | 1 + crates/weft-ipc-types/src/package.rs | 15 +++++++++++--- crates/weft-pack/src/main.rs | 28 ++++++++++++++++++--------- docs/security.md | 2 +- tests/frame/check_app_data.py | 5 +++-- 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index f12c6bb..10203d6 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -215,10 +215,9 @@ fn record_first_owner( } // Data in the earlier layout counts: it would be moved to this app's // data directory when the session starts. - let home = std::env::var_os("HOME") - .map(std::path::PathBuf::from) - .filter(|home| home.is_absolute()); - match weft_ipc_types::package::existing_app_data(data_home, home.as_deref(), app_id) { + let home = weft_ipc_types::package::home_dir() + .ok_or_else(|| Refusal::new(500, "HOME is not set to an absolute path"))?; + match weft_ipc_types::package::existing_app_data(data_home, &home, app_id) { Err(e) => Err(Refusal::new( 500, format!("cannot inspect the app data of {app_id}: {e}"), @@ -264,6 +263,10 @@ mod tests { const ID: &str = "org.weft.test.resolve"; + /// HOME as the test process found it, restored by `finish`. + static HOME_AT_START: std::sync::OnceLock> = + std::sync::OnceLock::new(); + /// A store holding a directory install of `ID`, with WEFT_APP_STORE /// pointing at it. Callers hold env_lock. fn store(name: &str, manifest_id: &str, module: &str) -> PathBuf { @@ -289,10 +292,12 @@ mod tests { Owner::Development, ) .unwrap(); + HOME_AT_START.get_or_init(|| std::env::var_os("HOME")); // SAFETY: callers hold env_lock, which serialises environment changes. unsafe { std::env::set_var("WEFT_APP_STORE", &store); std::env::set_var("XDG_DATA_HOME", store.join("share")); + std::env::set_var("HOME", store.join("home")); } store } @@ -304,6 +309,10 @@ mod tests { std::env::remove_var("WEFT_MOUNT_HELPER"); std::env::remove_var("XDG_DATA_HOME"); std::env::remove_var("WEFT_TRUSTED_KEYS"); + match HOME_AT_START.get() { + Some(Some(home)) => std::env::set_var("HOME", home), + _ => std::env::remove_var("HOME"), + } } let _ = std::fs::remove_dir_all(store); } @@ -462,19 +471,9 @@ mod tests { fn unowned_data_in_the_earlier_layout_is_not_given_away() { let _env = crate::tests::env_lock().blocking_lock(); let store = demo_store("legacy_data", true); - let home = store.join("home"); - let legacy = weft_ipc_types::package::legacy_app_data_dir(&home, DEMO); + let legacy = weft_ipc_types::package::legacy_app_data_dir(&store.join("home"), DEMO); std::fs::create_dir_all(&legacy).unwrap(); - let prior = std::env::var_os("HOME"); - // SAFETY: env_lock is held. - unsafe { std::env::set_var("HOME", &home) }; let refused = refusal(resolve(DEMO)); - unsafe { - match prior { - Some(v) => std::env::set_var("HOME", v), - None => std::env::remove_var("HOME"), - } - } let owner = read_owner(&owner_record_path(&store.join("share"), DEMO)).unwrap(); finish(&store); assert_eq!(refused.code, 403, "{}", refused.message); diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index 3956896..a681762 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -727,6 +727,7 @@ mod tests { let stale = entry .file_name() .to_str() + .filter(|n| *n != name) .and_then(|n| n.strip_prefix("weft-appd-tests-data-")) .is_some_and(|pid| !std::path::Path::new("/proc").join(pid).exists()); if stale { diff --git a/crates/weft-ipc-types/src/package.rs b/crates/weft-ipc-types/src/package.rs index ac9b5b2..9da0a2f 100644 --- a/crates/weft-ipc-types/src/package.rs +++ b/crates/weft-ipc-types/src/package.rs @@ -79,16 +79,25 @@ pub fn legacy_app_data_dir(home: &Path, app_id: &str) -> PathBuf { .join("data") } +/// The home directory, when `HOME` is set to an absolute path. +pub fn home_dir() -> Option { + std::env::var_os("HOME") + .map(PathBuf::from) + .filter(|home| home.is_absolute()) +} + /// Where app data for `app_id` already exists, in the current layout or in /// the earlier one under `home`, if anywhere. Data in the earlier layout is /// moved to the current one when the app next launches, so it counts too. pub fn existing_app_data( data_home: &Path, - home: Option<&Path>, + home: &Path, app_id: &str, ) -> std::io::Result> { - let places = std::iter::once(app_data_dir(data_home, app_id)) - .chain(home.map(|home| legacy_app_data_dir(home, app_id))); + let places = [ + app_data_dir(data_home, app_id), + legacy_app_data_dir(home, app_id), + ]; for place in places { match std::fs::symlink_metadata(&place) { Ok(_) => return Ok(Some(place)), diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 9b0cd87..66ba655 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -493,12 +493,11 @@ fn admit( None => { // Data in the earlier layout counts: weft-appd moves it to the // app's data directory when the app next launches. - let home = std::env::var_os("HOME") - .map(PathBuf::from) - .filter(|home| home.is_absolute()); - let existing = - weft_ipc_types::package::existing_app_data(data_home, home.as_deref(), app_id) - .with_context(|| format!("inspect the app data of {app_id}"))?; + let home = weft_ipc_types::package::home_dir().context( + "HOME is not set to an absolute path, so earlier app data cannot be found", + )?; + let existing = weft_ipc_types::package::existing_app_data(data_home, &home, app_id) + .with_context(|| format!("inspect the app data of {app_id}"))?; if let Some(data) = existing.filter(|_| !claim_data) { anyhow::bail!( "{} holds app data for {app_id} with no recorded owner; install with \ @@ -602,9 +601,13 @@ fn installed_in_another_store(app_id: &str, store_root: &Path) -> bool { /// Removes the owner record of `app_id` when the app has no data, and /// reports whether a record was removed. fn release_owner_without_data(data_home: &Path, app_id: &str) -> bool { - let no_data = - std::fs::symlink_metadata(weft_ipc_types::package::app_data_dir(data_home, app_id)) - .is_err_and(|e| e.kind() == std::io::ErrorKind::NotFound); + // Data in either layout keeps the record; so does not being able to look. + let no_data = weft_ipc_types::package::home_dir().is_some_and(|home| { + matches!( + weft_ipc_types::package::existing_app_data(data_home, &home, app_id), + Ok(None) + ) + }); no_data && std::fs::remove_file(weft_ipc_types::trust::owner_record_path(data_home, app_id)).is_ok() } @@ -1343,6 +1346,13 @@ mod tests { std::fs::remove_dir_all(&data).unwrap(); with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); assert!(!record.exists()); + + // Data in the earlier layout keeps the record too. + weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); + let legacy = weft_ipc_types::package::legacy_app_data_dir(&home, app_id); + std::fs::create_dir_all(&legacy).unwrap(); + assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); + assert!(record.exists()); let _ = std::fs::remove_dir_all(&home); } diff --git a/docs/security.md b/docs/security.md index 8987b0d..39020fc 100644 --- a/docs/security.md +++ b/docs/security.md @@ -26,7 +26,7 @@ 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. -`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, and that key is then recorded as its owner; if app data already exists for the ID without a recorded owner, in the current layout or the earlier one that is migrated at launch, the launch is refused rather than handing the data to that publisher; `weft-pack install` applies the same rule unless given `--claim-data`. The record is written before the session starts, so it remains even if that first launch then fails; `weft-pack uninstall` releases it once no data exists and no known store holds the package. While an ID is recorded as development content, whatever package is found for it runs unverified, from any store. A changed package, or one whose key was removed from the trust store, is refused with code 403; a damaged trust store or owner record is a host error, 500. The manifest is read once and its bytes must be the ones in the verified inventory, so a session's capabilities and entries come from the signed manifest. The module and UI files are read again when the session starts, so a directory install changed by its owning user after the check, or while the session runs, is not detected; verified images are not writable. +`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, and that key is then recorded as its owner; if app data already exists for the ID without a recorded owner, in the current layout or the earlier one that is migrated at launch, the launch is refused rather than handing the data to that publisher; `weft-pack install` applies the same rule unless given `--claim-data`. The record is written before the session starts, so it remains even if that first launch then fails; `weft-pack uninstall` releases it once no data exists in either layout and no known store holds the package. Upgrading from installations made before owner records: data with no record is refused at launch until it is claimed. While an ID is recorded as development content, whatever package is found for it runs unverified, from any store. A changed package, or one whose key was removed from the trust store, is refused with code 403; a damaged trust store or owner record is a host error, 500. The manifest is read once and its bytes must be the ones in the verified inventory, so a session's capabilities and entries come from the signed manifest. The module and UI files are read again when the session starts, so a directory install changed by its owning user after the check, or 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. diff --git a/tests/frame/check_app_data.py b/tests/frame/check_app_data.py index d1fad5e..95130af 100644 --- a/tests/frame/check_app_data.py +++ b/tests/frame/check_app_data.py @@ -41,8 +41,9 @@ def run(args, desktop, home, xdotool): (legacy / "notes.txt").write_bytes(NOTES) data_home = home / "data-home" target = data_home / "weft/app-data" / APP_ID - # The earlier data belongs to the demo publisher, as installing the - # signed package with weft-pack would have recorded. + # The earlier data is recorded as the demo publisher's. Data from before + # owner records has no record and is refused until claimed with + # weft-pack install --claim-data. owner = data_home / "weft/owners" / APP_ID owner.parent.mkdir(parents=True) key = (ROOT / "examples/keys/weft-sign.pub").read_text().strip() From af6f716823b3727a8a3fbbc28b8cbe783395dfa2 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 14:56:42 +0000 Subject: [PATCH 14/22] fix(pack): let --claim-data install without HOME and test HOME refusals Claiming data does not depend on finding earlier-layout data, so an install that claims it no longer needs HOME. Tests cover a first launch and a first install with an unusable HOME refusing and recording no owner, and the security docs state the HOME requirement. --- crates/weft-appd/src/launch.rs | 13 +++++++++++++ crates/weft-pack/src/main.rs | 24 +++++++++++++++++++++++- docs/security.md | 2 +- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 10203d6..89efc67 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -485,6 +485,19 @@ mod tests { assert_eq!(owner, None); } + #[test] + fn a_first_launch_without_a_usable_home_records_nothing() { + let _env = crate::tests::env_lock().blocking_lock(); + let store = demo_store("no_home", true); + // SAFETY: env_lock is held; finish restores HOME. + unsafe { std::env::set_var("HOME", "relative") }; + let refused = refusal(resolve(DEMO)); + let owner = read_owner(&owner_record_path(&store.join("share"), DEMO)).unwrap(); + finish(&store); + assert_eq!(refused.code, 500, "{}", refused.message); + assert_eq!(owner, None); + } + #[test] fn only_the_signed_manifest_is_accepted() { let _env = crate::tests::env_lock().blocking_lock(); diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 66ba655..c8f56c1 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -490,6 +490,10 @@ fn admit( "{app_id} belongs to {existing}; this package is {owner}. Its app data stays with \ its owner, so another publisher or a development build cannot take the ID over" ), + None if claim_data => { + write_owner(&record, owner)?; + Some(record) + } None => { // Data in the earlier layout counts: weft-appd moves it to the // app's data directory when the app next launches. @@ -498,7 +502,7 @@ fn admit( )?; let existing = weft_ipc_types::package::existing_app_data(data_home, &home, app_id) .with_context(|| format!("inspect the app data of {app_id}"))?; - if let Some(data) = existing.filter(|_| !claim_data) { + if let Some(data) = existing { anyhow::bail!( "{} holds app data for {app_id} with no recorded owner; install with \ --claim-data to give it to {owner}, or move it aside", @@ -1433,6 +1437,24 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn a_first_install_without_a_usable_home_records_nothing() { + let home = temp_root("no_home"); + let app_id = "org.weft.test.nohome"; + write_package(&home.join("src"), app_id, ""); + let store = home.join("store"); + let refused = with_home(&home, || { + // SAFETY: with_home holds env_lock and restores HOME afterwards. + unsafe { std::env::set_var("HOME", "relative") }; + install_package_to(&home.join("src"), &store, InstallMode::Development) + }); + let message = format!("{:#}", refused.unwrap_err()); + assert!(message.contains("HOME"), "{message}"); + assert_eq!(owner_of(&home, app_id), None); + assert!(!store.join(app_id).exists()); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn uninstalling_one_of_two_copies_keeps_the_record() { let home = temp_root("two_copies"); diff --git a/docs/security.md b/docs/security.md index 39020fc..55bdf1d 100644 --- a/docs/security.md +++ b/docs/security.md @@ -26,7 +26,7 @@ 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. -`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, and that key is then recorded as its owner; if app data already exists for the ID without a recorded owner, in the current layout or the earlier one that is migrated at launch, the launch is refused rather than handing the data to that publisher; `weft-pack install` applies the same rule unless given `--claim-data`. The record is written before the session starts, so it remains even if that first launch then fails; `weft-pack uninstall` releases it once no data exists in either layout and no known store holds the package. Upgrading from installations made before owner records: data with no record is refused at launch until it is claimed. While an ID is recorded as development content, whatever package is found for it runs unverified, from any store. A changed package, or one whose key was removed from the trust store, is refused with code 403; a damaged trust store or owner record is a host error, 500. The manifest is read once and its bytes must be the ones in the verified inventory, so a session's capabilities and entries come from the signed manifest. The module and UI files are read again when the session starts, so a directory install changed by its owning user after the check, or while the session runs, is not detected; verified images are not writable. +`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, and that key is then recorded as its owner; if app data already exists for the ID without a recorded owner, in the current layout or the earlier one that is migrated at launch, the launch is refused rather than handing the data to that publisher; `weft-pack install` applies the same rule unless given `--claim-data`. The record is written before the session starts, so it remains even if that first launch then fails; `weft-pack uninstall` releases it once no data exists in either layout and no known store holds the package. Finding earlier-layout data needs `HOME`: without an absolute `HOME`, a first launch is refused (500), a first install fails unless it claims the data, and uninstall keeps the record. While an ID is recorded as development content, whatever package is found for it runs unverified, from any store. A changed package, or one whose key was removed from the trust store, is refused with code 403; a damaged trust store or owner record is a host error, 500. The manifest is read once and its bytes must be the ones in the verified inventory, so a session's capabilities and entries come from the signed manifest. The module and UI files are read again when the session starts, so a directory install changed by its owning user after the check, or 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. From a57cdbc8bef6b7dfac7e0aec6e8ba70305431577 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:33:57 +0000 Subject: [PATCH 15/22] feat(pack): install and update packages as immutable revisions activated atomically An installed package could not be updated: install refused it, and uninstalling and reinstalling it was neither atomic nor safe for running sessions, which read files from the package directory. A package is now staged, checked, verified, admitted and flushed. It is then renamed to an immutable revision named by its content digest, /.revisions//, and activated by one rename of the link /. A failure or interruption at any point leaves the previously active revision active, and a failed first installation leaves nothing installed. The revision active before an update is kept, and weft-pack rollback makes it active again after checking it against the owner record. Other revisions are collected by later operations on the app, unless a session still runs from them. Leftovers of interrupted operations are collected too. weft-appd follows the store link only to one of the app's own revisions. It runs a session from the revision directory and holds a shared lock on it for the session's lifetime, so an update, rollback or uninstall never changes or removes the bytes of a running session. A package directory from before revisions is still read. An update replaces it in one exchange, and only while no session runs from it. --- README.md | 2 +- crates/weft-appd/src/launch.rs | 78 ++- crates/weft-ipc-types/src/lib.rs | 1 + crates/weft-ipc-types/src/store.rs | 227 +++++++++ crates/weft-pack/src/main.rs | 744 ++++++++++++++++++++++++++--- docs/architecture.md | 12 +- 6 files changed, 971 insertions(+), 93 deletions(-) create mode 100644 crates/weft-ipc-types/src/store.rs diff --git a/README.md b/README.md index f6a5c18..2ef170b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web **Runtime** — `weft-runtime` runs WASI Component Model binaries under Wasmtime 30 (`--features wasmtime-runtime`). Provides `weft:app/notify`, `weft:app/ipc`, `weft:app/fetch`, `weft:app/notifications`, and `weft:app/clipboard` host imports. Preopens filesystem paths according to declared capabilities. -**Package management** — `weft-pack` handles check, sign, verify, install, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. App data is stored apart from packages and survives uninstall. +**Package management** — `weft-pack` handles check, sign, verify, install and update, rollback, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. App data is stored apart from packages and survives uninstall. **File portal** — `weft-file-portal` is a per-session file proxy with a path allowlist and `..` blocking. diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 89efc67..5641798 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -35,6 +35,9 @@ pub(crate) struct LaunchPackage { /// The image the package root is mounted from, held for as long as the /// session runs. pub(crate) image: Option, + /// The lock that keeps an installed revision from being removed while + /// the session runs from it. + pub(crate) pin: Option, } /// Resolves `app_id` in the package stores. @@ -69,10 +72,29 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result Refusal::new(403, e.to_string()), + weft_ipc_types::store::StoreError::Io(..) => Refusal::new(500, e.to_string()), + })?; + let Some(active) = active else { continue }; + let dir = active.dir().to_path_buf(); + // A directory left with only app data by an earlier layout is not a + // package. + if std::fs::symlink_metadata(dir.join(MANIFEST_FILE)).is_err() { + continue; } + // The session runs from the revision itself, not the link, so an + // update or rollback that switches the link while it runs leaves it + // on the bytes it started with. + let pin = weft_ipc_types::store::pin(&dir).map_err(|e| { + Refusal::new( + 500, + format!("cannot hold {} while it runs: {e}", dir.display()), + ) + })?; + let mut package = from_root(app_id, dir, None)?; + package.pin = Some(pin); + return Ok(package); } Err(Refusal::new( 404, @@ -129,6 +151,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option) -> Result/.revisions//`. The active +//! revision is chosen by `/`, a symbolic link to it that one +//! rename replaces, so an update or rollback switches every new launch at +//! once and an interrupted one leaves the previous choice in place. Stores +//! written before revisions existed hold the package directly in +//! `/`; that layout is still read. +//! +//! A session pins the revision it was launched from with a shared lock on +//! the revision directory, held for the session's lifetime. Package +//! operations remove a revision only after taking that lock exclusively, so +//! a live session keeps running on the bytes it started with. + +use std::fs::File; +use std::path::{Path, PathBuf}; + +/// The directory of a store that holds package revisions. +pub const REVISIONS_DIR: &str = ".revisions"; + +/// The directory holding the revisions of `app_id`. +pub fn revisions_of(store_root: &Path, app_id: &str) -> PathBuf { + store_root.join(REVISIONS_DIR).join(app_id) +} + +/// The link target that makes `revision` the active revision of `app_id`, +/// relative to the store root. +pub fn link_target(app_id: &str, revision: &str) -> PathBuf { + Path::new(REVISIONS_DIR).join(app_id).join(revision) +} + +/// Whether `name` is a revision name: the hex content digest of a package. +pub fn is_revision_name(name: &str) -> bool { + name.len() == 64 && name.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) +} + +/// Where the active package of `app_id` is in a store. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Active { + /// A revision, by name, in `.revisions//`. + Revision { name: String, dir: PathBuf }, + /// A package directory installed before revisions existed. + Directory(PathBuf), +} + +impl Active { + /// The package root. + pub fn dir(&self) -> &Path { + match self { + Self::Revision { dir, .. } | Self::Directory(dir) => dir, + } + } +} + +/// Why the active package of a store cannot be determined. +#[derive(Debug)] +pub enum StoreError { + Io(PathBuf, std::io::Error), + /// `/` is neither a package directory nor a link to one + /// of its revisions. + NotAPackage(PathBuf), +} + +impl std::fmt::Display for StoreError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Io(path, e) => write!(f, "cannot read {}: {e}", path.display()), + Self::NotAPackage(path) => write!( + f, + "{} is neither a package directory nor a link to one of its revisions", + path.display() + ), + } + } +} + +impl std::error::Error for StoreError {} + +/// The active package of `app_id` in `store_root`, if any. A link is +/// followed only when it names one of the app's own revisions, exactly as +/// package operations write it, and that revision is a directory. +pub fn active(store_root: &Path, app_id: &str) -> Result, StoreError> { + let entry = store_root.join(app_id); + let metadata = match std::fs::symlink_metadata(&entry) { + Ok(m) => m, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(e) => return Err(StoreError::Io(entry, e)), + }; + if metadata.is_dir() { + return Ok(Some(Active::Directory(entry))); + } + if !metadata.file_type().is_symlink() { + return Err(StoreError::NotAPackage(entry)); + } + let target = std::fs::read_link(&entry).map_err(|e| StoreError::Io(entry.clone(), e))?; + let name = target + .file_name() + .and_then(|n| n.to_str()) + .filter(|n| is_revision_name(n)) + .map(str::to_owned); + let Some(name) = name.filter(|n| target == link_target(app_id, n)) else { + return Err(StoreError::NotAPackage(entry)); + }; + let dir = revisions_of(store_root, app_id).join(&name); + match std::fs::symlink_metadata(&dir) { + Ok(m) if m.is_dir() => Ok(Some(Active::Revision { name, dir })), + Ok(_) => Err(StoreError::NotAPackage(entry)), + Err(e) => Err(StoreError::Io(dir, e)), + } +} + +/// A shared lock on a package directory, held while a session runs from it. +#[derive(Debug)] +pub struct Pin { + _dir: File, +} + +/// Pins the package directory `dir`. The lock is taken on the directory +/// that is opened, and the path must still name that directory afterwards: +/// a revision removed or replaced in between is not pinned. +pub fn pin(dir: &Path) -> std::io::Result { + let file = File::open(dir)?; + file.lock_shared()?; + if !same_file(&file, dir)? { + return Err(std::io::Error::other(format!( + "{} changed while it was being pinned", + dir.display() + ))); + } + Ok(Pin { _dir: file }) +} + +/// Takes the exclusive lock on a package directory that no session pins, +/// or returns `None` while one does. +pub fn try_claim(dir: &Path) -> std::io::Result> { + let file = File::open(dir)?; + match file.try_lock() { + Ok(()) => Ok(Some(file)), + Err(std::fs::TryLockError::WouldBlock) => Ok(None), + Err(std::fs::TryLockError::Error(e)) => Err(e), + } +} + +#[cfg(unix)] +fn same_file(file: &File, path: &Path) -> std::io::Result { + use std::os::unix::fs::MetadataExt; + let opened = file.metadata()?; + let named = std::fs::symlink_metadata(path)?; + Ok(opened.dev() == named.dev() && opened.ino() == named.ino()) +} + +#[cfg(not(unix))] +fn same_file(_file: &File, _path: &Path) -> std::io::Result { + Ok(true) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn store(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("weft_store_{name}_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + const REV: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + + #[cfg(unix)] + #[test] + fn only_links_to_the_apps_own_revisions_are_followed() { + use std::os::unix::fs::symlink; + let root = store("links"); + std::fs::create_dir_all(revisions_of(&root, "org.a").join(REV)).unwrap(); + std::fs::create_dir_all(revisions_of(&root, "org.b").join(REV)).unwrap(); + assert!(active(&root, "org.a").unwrap().is_none()); + + symlink(link_target("org.a", REV), root.join("org.a")).unwrap(); + assert_eq!( + active(&root, "org.a").unwrap(), + Some(Active::Revision { + name: REV.to_owned(), + dir: revisions_of(&root, "org.a").join(REV), + }) + ); + // Another app's revision, an absolute target, a non-revision name. + for target in [ + link_target("org.b", REV), + revisions_of(&root, "org.c").join(REV), + PathBuf::from(".revisions/org.c/latest"), + ] { + let _ = std::fs::remove_file(root.join("org.c")); + std::fs::create_dir_all(revisions_of(&root, "org.c").join(REV)).unwrap(); + symlink(&target, root.join("org.c")).unwrap(); + assert!( + matches!(active(&root, "org.c"), Err(StoreError::NotAPackage(_))), + "{}", + target.display() + ); + } + // A package installed before revisions existed. + std::fs::create_dir(root.join("org.d")).unwrap(); + assert_eq!( + active(&root, "org.d").unwrap(), + Some(Active::Directory(root.join("org.d"))) + ); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn a_pinned_directory_cannot_be_claimed() { + let root = store("pin"); + let dir = root.join("rev"); + std::fs::create_dir(&dir).unwrap(); + let pinned = pin(&dir).unwrap(); + assert!(try_claim(&dir).unwrap().is_none()); + // Other sessions can pin it too. + let again = pin(&dir).unwrap(); + drop((pinned, again)); + let claimed = try_claim(&dir).unwrap(); + assert!(claimed.is_some()); + drop(claimed); + std::fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index c8f56c1..40d5199 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -37,6 +37,10 @@ fn main() -> anyhow::Result<()> { let app_id = args.get(2).context("usage: weft-pack uninstall ")?; uninstall_package(app_id)?; } + Some("rollback") => { + let app_id = args.get(2).context("usage: weft-pack rollback ")?; + rollback_package(&resolve_install_root()?, app_id)?; + } Some("list") => { list_installed(); } @@ -120,6 +124,7 @@ fn main() -> anyhow::Result<()> { " --claim-data: adopt app data with no owner" ); eprintln!(" weft-pack uninstall remove installed package"); + eprintln!(" weft-pack rollback activate the previous revision"); eprintln!(" weft-pack list list installed packages"); eprintln!( " weft-pack build-image [--out ] create EROFS image with mkfs.erofs" @@ -418,20 +423,48 @@ fn install_staged( } let _ = std::fs::remove_dir(&dest); } - if dest.exists() { - anyhow::bail!( - "package '{}' is already installed at {}; remove it first", - app_id, - dest.display() - ); - } + // A package installed before revisions existed is replaced by the new + // revision only while no session runs from it, since it is not kept. + let legacy = match active(store_root, app_id)? { + Some(Active::Directory(dir)) => Some( + weft_ipc_types::store::try_claim(&dir) + .with_context(|| format!("open {}", dir.display()))? + .with_context(|| { + format!("{app_id} is running from {}; close it first", dir.display()) + })?, + ), + _ => None, + }; let admission = admit(staging, app_id, mode, &data_home, claim_data)?; let owner = admission.owner; - place(staging, &dest, admission)?; - println!("installed {} -> {} ({owner})", app_id, dest.display()); + let revision = place(staging, store_root, app_id, admission)?; + drop(legacy); + collect(store_root, app_id); + println!( + "installed {app_id} -> {} (revision {}, {owner})", + dest.display(), + &revision[..12] + ); Ok(()) } +/// The active package of `app_id` in `store_root`. +fn active( + store_root: &Path, + app_id: &str, +) -> anyhow::Result> { + Ok(weft_ipc_types::store::active(store_root, app_id)?) +} + +use weft_ipc_types::store::Active; + +/// How long a leftover staging, unpacking or removal directory must be +/// unchanged before it counts as abandoned by an interrupted operation. +const ABANDONED_AFTER: std::time::Duration = std::time::Duration::from_secs(3600); + +/// The revision a store keeps for rollback, a link next to the revisions. +const PREVIOUS_LINK: &str = "previous"; + /// The owner a staged package was admitted under, and the owner record this /// installation created, if any. struct Admission { @@ -439,17 +472,301 @@ struct Admission { created_record: Option, } -/// Makes the staged package available at `dest`. If that fails, the staging -/// copy is removed, and so is an owner record this installation created, so +/// Makes the staged package the active revision of `app_id` and returns +/// the revision's name. The package becomes an immutable revision named by +/// its content digest (identical content is installed once), and one rename +/// of the store's link activates it, so an interruption at any point leaves +/// the previously active revision, or none on a first installation, in +/// place. The revision active before is kept for rollback and for the +/// sessions running from it. If anything fails, the staging copy, a new +/// revision and an owner record this installation created are removed, so /// a failed first installation leaves no claim on the app ID. -fn place(staging: &Path, dest: &Path, admission: Admission) -> anyhow::Result<()> { - if let Err(e) = weft_ipc_types::package::rename_no_replace(staging, dest) { +fn place( + staging: &Path, + store_root: &Path, + app_id: &str, + admission: Admission, +) -> anyhow::Result { + let mut created_revision = None; + let result = (|| { + let revision = hex::encode( + weft_ipc_types::trust::content_digest(staging) + .with_context(|| format!("read {}", staging.display()))?, + ); + let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); + create_store(&revisions)?; + let dir = revisions.join(&revision); + sync_tree(staging)?; + match weft_ipc_types::package::rename_no_replace(staging, &dir) { + Ok(()) => created_revision = Some(dir.clone()), + // The same content is already installed; it is activated as is. + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + let _ = std::fs::remove_dir_all(staging); + } + Err(e) => { + return Err(e) + .with_context(|| format!("move {} -> {}", staging.display(), dir.display())); + } + } + sync_dir(&revisions)?; + activate(store_root, app_id, &revision)?; + Ok(revision) + })(); + if result.is_err() { let _ = std::fs::remove_dir_all(staging); + if let Some(dir) = &created_revision { + let _ = std::fs::remove_dir_all(dir); + } if let Some(record) = &admission.created_record { let _ = std::fs::remove_file(record); } - return Err(e).with_context(|| format!("move {} -> {}", staging.display(), dest.display())); } + result +} + +/// Makes `revision` the active revision of `app_id`. The revision active +/// before becomes the one kept for rollback. Its link is written first, so +/// an interruption between the two steps keeps both revisions. A package +/// directory from before revisions existed is exchanged with the new link in +/// one step and then removed; the caller holds it so no session runs from it. +fn activate(store_root: &Path, app_id: &str, revision: &str) -> anyhow::Result<()> { + use weft_ipc_types::store::{link_target, revisions_of}; + let revisions = revisions_of(store_root, app_id); + let current = active(store_root, app_id)?; + if let Some(Active::Revision { name, .. }) = ¤t + && name != revision + { + replace_link(&revisions, PREVIOUS_LINK, Path::new(name))?; + } + let dest = store_root.join(app_id); + let link = temporary_link(store_root, app_id, &link_target(app_id, revision))?; + let switched = match ¤t { + Some(Active::Directory(_)) => exchange(&link, &dest), + _ => std::fs::rename(&link, &dest), + }; + if let Err(e) = switched { + let _ = std::fs::remove_file(&link); + return Err(e).with_context(|| format!("activate {}", dest.display())); + } + sync_dir(store_root)?; + if let Some(Active::Directory(_)) = current { + // The earlier package directory now has the temporary name. + std::fs::remove_dir_all(&link) + .with_context(|| format!("remove the replaced package at {}", link.display()))?; + } + Ok(()) +} + +/// Creates a symbolic link to `target` under a fresh temporary name in +/// `store_root`, named so the collection of `app_id` can recognise it. +fn temporary_link(store_root: &Path, app_id: &str, target: &Path) -> anyhow::Result { + let link = store_root.join(format!( + ".link-{app_id}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + #[cfg(unix)] + std::os::unix::fs::symlink(target, &link) + .with_context(|| format!("create {}", link.display()))?; + #[cfg(not(unix))] + anyhow::bail!("installing packages requires a Unix host"); + Ok(link) +} + +/// Points the link `name` in `dir` at `target`, replacing it in one rename. +fn replace_link(dir: &Path, name: &str, target: &Path) -> anyhow::Result<()> { + let temporary = dir.join(format!( + ".{name}-{}", + hex::encode(rand::random::<[u8; 8]>()) + )); + #[cfg(unix)] + std::os::unix::fs::symlink(target, &temporary) + .with_context(|| format!("create {}", temporary.display()))?; + if let Err(e) = std::fs::rename(&temporary, dir.join(name)) { + let _ = std::fs::remove_file(&temporary); + return Err(e).with_context(|| format!("update {}", dir.join(name).display())); + } + sync_dir(dir) +} + +#[cfg(target_os = "linux")] +fn exchange(a: &Path, b: &Path) -> std::io::Result<()> { + use rustix::fs::{CWD, RenameFlags, renameat_with}; + renameat_with(CWD, a, CWD, b, RenameFlags::EXCHANGE).map_err(std::io::Error::from) +} + +#[cfg(not(target_os = "linux"))] +fn exchange(_a: &Path, _b: &Path) -> std::io::Result<()> { + Err(std::io::Error::other( + "replacing a package directory requires Linux", + )) +} + +/// Flushes a directory's entries to disk. +fn sync_dir(dir: &Path) -> anyhow::Result<()> { + std::fs::File::open(dir) + .and_then(|d| d.sync_all()) + .with_context(|| format!("sync {}", dir.display())) +} + +/// Flushes every file and directory of a staged package to disk, so a +/// revision that is activated is complete even after a crash. +fn sync_tree(dir: &Path) -> anyhow::Result<()> { + for entry in std::fs::read_dir(dir).with_context(|| format!("read {}", dir.display()))? { + let entry = entry?; + let path = entry.path(); + if entry.file_type()?.is_dir() { + sync_tree(&path)?; + } else { + std::fs::File::open(&path) + .and_then(|f| f.sync_all()) + .with_context(|| format!("sync {}", path.display()))?; + } + } + sync_dir(dir) +} + +/// The revision of `app_id` kept for rollback, if any. +fn previous_revision(store_root: &Path, app_id: &str) -> Option { + let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); + std::fs::read_link(revisions.join(PREVIOUS_LINK)) + .ok()? + .to_str() + .filter(|name| weft_ipc_types::store::is_revision_name(name)) + .filter(|name| std::fs::symlink_metadata(revisions.join(name)).is_ok_and(|m| m.is_dir())) + .map(str::to_owned) +} + +/// Removes what package operations on `app_id` no longer need, keeping the +/// active revision, the one kept for rollback and every revision a session +/// is running from: other revisions, temporary links, and staging, +/// unpacking and removal directories that interrupted operations left +/// behind. The caller holds the owner lock of `app_id`. Failures are +/// reported and leave the entry for the next operation. +fn collect(store_root: &Path, app_id: &str) { + use weft_ipc_types::store::{is_revision_name, revisions_of, try_claim}; + let revisions = revisions_of(store_root, app_id); + let mut keep = Vec::new(); + if let Ok(Some(Active::Revision { name, .. })) = active(store_root, app_id) { + keep.push(name); + } + keep.extend(previous_revision(store_root, app_id)); + if let Ok(entries) = std::fs::read_dir(&revisions) { + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(name) = name.to_str() else { continue }; + let path = entry.path(); + if name == PREVIOUS_LINK { + // A link to a revision that is gone keeps nothing. + if previous_revision(store_root, app_id).is_none() { + let _ = std::fs::remove_file(&path); + } + continue; + } + if name.starts_with('.') { + // A link replacement that was interrupted. + if std::fs::symlink_metadata(&path).is_ok_and(|m| m.file_type().is_symlink()) { + let _ = std::fs::remove_file(&path); + } + continue; + } + if !is_revision_name(name) || keep.iter().any(|k| k == name) { + continue; + } + match try_claim(&path) { + Ok(Some(claim)) => { + remove_claimed(store_root, &path); + drop(claim); + } + Ok(None) => println!("kept revision {} for a running session", &name[..12]), + Err(e) => eprintln!("cannot inspect {}: {e}", path.display()), + } + } + } + let _ = std::fs::remove_dir(&revisions); + let link_prefix = format!(".link-{app_id}-"); + if let Ok(entries) = std::fs::read_dir(store_root) { + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(name) = name.to_str() else { continue }; + let path = entry.path(); + if name.starts_with(&link_prefix) { + // A temporary link of an interrupted activation. + if std::fs::symlink_metadata(&path).is_ok_and(|m| m.file_type().is_symlink()) { + let _ = std::fs::remove_file(&path); + } else if let Ok(Some(claim)) = try_claim(&path) { + // A replaced package directory whose removal was cut short. + remove_claimed(store_root, &path); + drop(claim); + } + } else if [".staging-", ".unpack-", ".trash-"] + .iter() + .any(|p| name.starts_with(p)) + && abandoned(&path) + { + let _ = std::fs::remove_dir_all(&path); + } + } + } +} + +/// Whether a temporary directory has been left unchanged long enough to be +/// abandoned. +fn abandoned(path: &Path) -> bool { + std::fs::symlink_metadata(path) + .and_then(|m| m.modified()) + .ok() + .and_then(|t| t.elapsed().ok()) + .is_some_and(|age| age >= ABANDONED_AFTER) +} + +/// Removes a claimed package directory: it is first renamed out of the way, +/// so no launch can find it half removed. +fn remove_claimed(store_root: &Path, dir: &Path) { + let trash = store_root.join(format!(".trash-{}", hex::encode(rand::random::<[u8; 8]>()))); + match std::fs::rename(dir, &trash) { + Ok(()) => { + if let Err(e) = std::fs::remove_dir_all(&trash) { + eprintln!("cannot remove {}: {e}", trash.display()); + } + } + Err(e) => eprintln!("cannot remove {}: {e}", dir.display()), + } +} + +/// Makes the revision kept for rollback the active revision of `app_id` +/// again; the revision it replaces is kept in turn. The kept revision must +/// still satisfy the app's owner record: a revision by a verified publisher +/// is checked against the trust store again, so a revoked key cannot be +/// rolled back to. +fn rollback_package(store_root: &Path, app_id: &str) -> anyhow::Result<()> { + anyhow::ensure!( + weft_ipc_types::package::is_valid_app_id(app_id), + "'{app_id}' is not a valid app ID" + ); + let data_home = weft_ipc_types::package::data_home() + .context("cannot locate the data home to check who owns the app ID")?; + let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; + let Some(Active::Revision { .. }) = active(store_root, app_id)? else { + anyhow::bail!("{app_id} has no installed revision to roll back from"); + }; + let previous = previous_revision(store_root, app_id) + .with_context(|| format!("{app_id} has no earlier revision to roll back to"))?; + let dir = weft_ipc_types::store::revisions_of(store_root, app_id).join(&previous); + let record = weft_ipc_types::trust::owner_record_path(&data_home, app_id); + match weft_ipc_types::trust::read_owner(&record)? { + Some(Owner::Development) => {} + Some(Owner::Verified(key)) => { + let signer = TrustStore::load(&TrustStore::directories())?.signer(&dir)?; + anyhow::ensure!( + signer.as_ref() == Some(&key), + "the earlier revision of {app_id} is no longer signed by a trusted key of its owner" + ); + } + None => anyhow::bail!("{app_id} has no recorded owner"), + } + activate(store_root, app_id, &previous)?; + collect(store_root, app_id); + println!("rolled {app_id} back to revision {}", &previous[..12]); Ok(()) } @@ -534,45 +851,40 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> None => None, }; let target = store_root.join(app_id); - if !target.exists() { - // An installation interrupted between recording its owner and placing - // the package leaves a record with nothing installed; with no data - // either, it holds the ID for nothing and is released. - if let Some(home) = &data_home - && !installed_in_another_store(app_id, store_root) - && release_owner_without_data(home, app_id) - { - println!("released {app_id}, which was not installed"); - return Ok(()); - } - anyhow::bail!( - "package '{}' is not installed at {}", - app_id, - target.display() - ); - } - // A `data` directory is never removed with the package: packages cannot - // ship one, so it holds app data from an earlier version. It is moved to - // the app data directory when this is the user store and the package - // declares app data (or its manifest cannot be read); otherwise uninstall - // stops and leaves everything in place. - if std::fs::symlink_metadata(target.join("data")).is_ok() { - let may_hold_app_data = load_manifest(&target).map_or(true, |m| declares_app_data(&m)); - if !(may_hold_app_data && is_user_store(store_root)) { + match active(store_root, app_id)? { + None => { + // Revisions left by sessions that ran past an earlier uninstall + // are removed now that nothing uses them. + collect(store_root, app_id); + // An installation interrupted between recording its owner and + // placing the package leaves a record with nothing installed; + // with no data either, it holds the ID for nothing and is + // released. + if let Some(home) = &data_home + && !installed_in_another_store(app_id, store_root) + && release_owner_without_data(home, app_id) + { + println!("released {app_id}, which was not installed"); + return Ok(()); + } anyhow::bail!( - "{} contains a 'data' directory that may hold app data; move it aside before \ - uninstalling", + "package '{}' is not installed at {}", + app_id, target.display() ); } - move_app_data_out(app_id, &target)?; - } - // Moving the data removes a package directory it leaves empty. - match std::fs::remove_dir_all(&target) { - Err(e) if e.kind() != std::io::ErrorKind::NotFound => { - return Err(e).with_context(|| format!("remove {}", target.display())); + // New launches no longer find the app; sessions running from one of + // its revisions keep it until they end and a later operation on the + // app removes it. + Some(Active::Revision { .. }) => { + std::fs::remove_file(&target) + .with_context(|| format!("remove {}", target.display()))?; + sync_dir(store_root)?; + let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); + let _ = std::fs::remove_file(revisions.join(PREVIOUS_LINK)); + collect(store_root, app_id); } - _ => {} + Some(Active::Directory(_)) => uninstall_directory(app_id, store_root, &target)?, } // The owner record keeps the app ID, and the data with it, for its owner // while the data remains; with no data left, the ID is free again. @@ -585,6 +897,43 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> Ok(()) } +/// Removes a package directory installed before revisions existed, which +/// only happens while no session runs from it. +fn uninstall_directory(app_id: &str, store_root: &Path, target: &Path) -> anyhow::Result<()> { + let _claim = weft_ipc_types::store::try_claim(target) + .with_context(|| format!("open {}", target.display()))? + .with_context(|| { + format!( + "{app_id} is running from {}; close it first", + target.display() + ) + })?; + // A `data` directory is never removed with the package: packages cannot + // ship one, so it holds app data from an earlier version. It is moved to + // the app data directory when this is the user store and the package + // declares app data (or its manifest cannot be read); otherwise uninstall + // stops and leaves everything in place. + if std::fs::symlink_metadata(target.join("data")).is_ok() { + let may_hold_app_data = load_manifest(target).map_or(true, |m| declares_app_data(&m)); + if !(may_hold_app_data && is_user_store(store_root)) { + anyhow::bail!( + "{} contains a 'data' directory that may hold app data; move it aside before \ + uninstalling", + target.display() + ); + } + move_app_data_out(app_id, target)?; + } + // Moving the data removes a package directory it leaves empty. + match std::fs::remove_dir_all(target) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => { + return Err(e).with_context(|| format!("remove {}", target.display())); + } + _ => {} + } + Ok(()) +} + /// Whether `app_id` is installed in a known store other than `store_root`. /// Owner records belong to the data home, not to a store, so a record stays /// while any store this host knows holds the package. @@ -1099,6 +1448,246 @@ mod tests { }) } + /// Writes a package whose UI holds `marker`, so each marker is a + /// different revision. + fn write_revision(src: &Path, app_id: &str, marker: &str) { + let _ = std::fs::remove_dir_all(src); + write_package(src, app_id, ""); + std::fs::write(src.join("ui/index.html"), marker).unwrap(); + } + + fn active_ui(store: &Path, app_id: &str) -> String { + std::fs::read_to_string(store.join(app_id).join("ui/index.html")).unwrap() + } + + fn revision_count(store: &Path, app_id: &str) -> usize { + std::fs::read_dir(weft_ipc_types::store::revisions_of(store, app_id)).map_or(0, |d| { + d.flatten() + .filter(|e| { + weft_ipc_types::store::is_revision_name(&e.file_name().to_string_lossy()) + }) + .count() + }) + } + + fn active_dir(store: &Path, app_id: &str) -> PathBuf { + active(store, app_id).unwrap().unwrap().dir().to_path_buf() + } + + #[test] + fn an_update_keeps_the_previous_revision_and_collects_older_ones() { + let home = temp_root("update"); + let store = home.join("store"); + let app_id = "org.weft.test.update"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let one = active_dir(&store, app_id); + write_revision(&src, app_id, "two"); + install().unwrap(); + assert_eq!(active_ui(&store, app_id), "two"); + assert!(one.is_dir(), "the previous revision is kept for rollback"); + write_revision(&src, app_id, "three"); + install().unwrap(); + assert_eq!(active_ui(&store, app_id), "three"); + assert!( + !one.exists(), + "a revision neither active nor previous is removed" + ); + assert_eq!(revision_count(&store, app_id), 2); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_revision_a_session_runs_from_is_kept_until_it_ends() { + let home = temp_root("pinned"); + let store = home.join("store"); + let app_id = "org.weft.test.pinned"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let one = active_dir(&store, app_id); + let session = weft_ipc_types::store::pin(&one).unwrap(); + for marker in ["two", "three", "four"] { + write_revision(&src, app_id, marker); + install().unwrap(); + } + assert_eq!( + std::fs::read_to_string(one.join("ui/index.html")).unwrap(), + "one", + "the running session keeps its bytes" + ); + // Uninstalling stops new launches but not the session. + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert!(active(&store, app_id).unwrap().is_none()); + assert!(one.is_dir()); + drop(session); + // Once it has ended, the next operation on the app removes it. + let _ = with_home(&home, || uninstall_package_from(app_id, &store)); + assert!(!one.exists()); + assert_eq!(revision_count(&store, app_id), 0); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_failed_update_keeps_the_active_revision() { + let home = temp_root("failed_update"); + let store = home.join("store"); + let app_id = "org.weft.test.failed"; + let src = home.join("src"); + write_revision(&src, app_id, "good"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + // A broken component, then the same ID under another owner. + write_revision(&src, app_id, "broken"); + std::fs::write(src.join("app.wasm"), b"not wasm").unwrap(); + assert!( + with_home(&home, || install_package_to( + &src, + &store, + InstallMode::Development + )) + .is_err() + ); + // Signed by a trusted publisher, which does not own the ID. + write_revision(&src, app_id, "other owner"); + sign_package(&src, &key(&home, "other", 7, true)).unwrap(); + let refused = with_trust(&home, || { + install_package_to(&src, &store, InstallMode::Verified) + }); + assert!( + refused + .as_ref() + .is_err_and(|e| e.to_string().contains("belongs to")), + "{refused:?}" + ); + assert_eq!(active_ui(&store, app_id), "good"); + assert_eq!(revision_count(&store, app_id), 1); + assert!(!staging_left(&store)); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn rollback_switches_to_the_kept_revision_and_back() { + let home = temp_root("rollback"); + let store = home.join("store"); + let app_id = "org.weft.test.rollback"; + let src = home.join("src"); + let rollback = || with_home(&home, || rollback_package(&store, app_id)); + write_revision(&src, app_id, "one"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert!( + rollback().is_err(), + "a first installation has nothing to roll back to" + ); + write_revision(&src, app_id, "two"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + rollback().unwrap(); + assert_eq!(active_ui(&store, app_id), "one"); + rollback().unwrap(); + assert_eq!(active_ui(&store, app_id), "two"); + assert_eq!(revision_count(&store, app_id), 2); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_package_directory_from_before_revisions_is_replaced_unless_in_use() { + let home = temp_root("legacy_update"); + let store = home.join("store"); + let app_id = "org.weft.test.legacy"; + write_revision(&store.join(app_id), app_id, "old"); + let src = home.join("src"); + write_revision(&src, app_id, "new"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + let session = weft_ipc_types::store::pin(&store.join(app_id)).unwrap(); + assert!( + install().is_err(), + "a running package directory is not replaced" + ); + assert!(matches!( + active(&store, app_id).unwrap(), + Some(Active::Directory(_)) + )); + assert_eq!(active_ui(&store, app_id), "old"); + drop(session); + install().unwrap(); + assert!(matches!( + active(&store, app_id).unwrap(), + Some(Active::Revision { .. }) + )); + assert_eq!(active_ui(&store, app_id), "new"); + let leftovers: Vec<_> = std::fs::read_dir(&store) + .unwrap() + .flatten() + .map(|e| e.file_name()) + .filter(|n| n != ".revisions" && n.to_str() != Some(app_id)) + .collect(); + assert!(leftovers.is_empty(), "{leftovers:?}"); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn leftovers_of_interrupted_operations_are_removed() { + let home = temp_root("leftovers"); + let store = home.join("store"); + let app_id = "org.weft.test.leftovers"; + let src = home.join("src"); + write_revision(&src, app_id, "one"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + let revisions = weft_ipc_types::store::revisions_of(&store, app_id); + // An activation and a link update cut short, an old staging copy and + // a recent one that may belong to an installation in progress. + #[cfg(unix)] + { + std::os::unix::fs::symlink("x", store.join(format!(".link-{app_id}-0"))).unwrap(); + std::os::unix::fs::symlink("x", revisions.join(".previous-0")).unwrap(); + } + let old = store.join(".staging-old"); + std::fs::create_dir(&old).unwrap(); + std::fs::File::open(&old) + .unwrap() + .set_modified(std::time::SystemTime::now() - 2 * ABANDONED_AFTER) + .unwrap(); + let recent = store.join(".staging-recent"); + std::fs::create_dir(&recent).unwrap(); + write_revision(&src, app_id, "two"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert!(std::fs::symlink_metadata(store.join(format!(".link-{app_id}-0"))).is_err()); + assert!(std::fs::symlink_metadata(revisions.join(".previous-0")).is_err()); + assert!(!old.exists()); + assert!(recent.exists()); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn only_packages_signed_by_a_trusted_key_install_as_verified() { let home = temp_root("verified"); @@ -1248,33 +1837,41 @@ mod tests { #[test] fn a_failed_placement_leaves_no_claim_on_the_id() { let home = temp_root("placement"); - let staging = home.join(".staging-x"); - let dest = home.join("org.weft.test.placed"); - std::fs::create_dir_all(&staging).unwrap(); - std::fs::create_dir_all(dest.join("ui")).unwrap(); - let record = home.join("owners/org.weft.test.placed"); + let store = home.join("store"); + let app_id = "org.weft.test.placed"; + let staging = store.join(".staging-x"); + write_package(&staging, app_id, ""); + // Something that is not a package where the app's link would go. + std::fs::write(store.join(app_id), "not a package").unwrap(); + let record = home.join("owners").join(app_id); weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); let admission = Admission { owner: Owner::Development, created_record: Some(record.clone()), }; - assert!(place(&staging, &dest, admission).is_err()); + assert!(place(&staging, &store, app_id, admission).is_err()); assert!(!staging.exists()); assert!(!record.exists()); + assert_eq!( + std::fs::read_to_string(store.join(app_id)).unwrap(), + "not a package", + "what was there is untouched" + ); + let revisions = weft_ipc_types::store::revisions_of(&store, app_id); assert!( - dest.join("ui").is_dir(), - "the existing package is untouched" + std::fs::read_dir(&revisions).map_or(true, |mut d| d.next().is_none()), + "the new revision is removed" ); // A record that existed before this installation is kept. - std::fs::create_dir_all(&staging).unwrap(); + write_package(&staging, app_id, ""); weft_ipc_types::trust::write_owner(&record, Owner::Development).unwrap(); let admission = Admission { owner: Owner::Development, created_record: None, }; - assert!(place(&staging, &dest, admission).is_err()); + assert!(place(&staging, &store, app_id, admission).is_err()); assert!(record.exists()); let _ = std::fs::remove_dir_all(&home); } @@ -1537,12 +2134,10 @@ mod tests { fn uninstall_keeps_app_data_from_the_user_store() { let home = temp_root("keep"); let app_id = "com.example.keep"; - write_package(&home.join("src"), app_id, APP_DATA); let store = home.join(".local/share/weft/apps"); - with_home(&home, || { - install_package_to(&home.join("src"), &store, InstallMode::Development) - }) - .unwrap(); + // A package directory from before revisions existed, with the data an + // earlier weft-appd kept inside it. + write_package(&store.join(app_id), app_id, APP_DATA); let legacy = store.join(app_id).join("data"); std::fs::create_dir_all(&legacy).unwrap(); std::fs::write(legacy.join("notes.txt"), "first\nsecond").unwrap(); @@ -1562,10 +2157,7 @@ mod tests { } // A second copy cannot be merged: uninstall refuses and removes nothing. - with_home(&home, || { - install_package_to(&home.join("src"), &store, InstallMode::Development) - }) - .unwrap(); + write_package(&store.join(app_id), app_id, APP_DATA); std::fs::create_dir_all(&legacy).unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); assert!(legacy.is_dir() && kept.is_dir()); @@ -1577,12 +2169,8 @@ mod tests { let home = temp_root("refuse"); let app_id = "com.example.refuse"; // No app-data capability: the data directory is left and uninstall stops. - write_package(&home.join("src"), app_id, ""); let store = home.join(".local/share/weft/apps"); - with_home(&home, || { - install_package_to(&home.join("src"), &store, InstallMode::Development) - }) - .unwrap(); + write_package(&store.join(app_id), app_id, ""); std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); std::fs::write(store.join(app_id).join("data/x"), "x").unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &store)).is_err()); @@ -1593,12 +2181,8 @@ mod tests { std::fs::remove_dir_all(store.join(app_id)).unwrap(); // Another store: earlier versions never kept data there, so it stops too. - write_package(&home.join("src2"), app_id, APP_DATA); let other = home.join("other-store"); - with_home(&home, || { - install_package_to(&home.join("src2"), &other, InstallMode::Development) - }) - .unwrap(); + write_package(&other.join(app_id), app_id, APP_DATA); std::fs::create_dir_all(other.join(app_id).join("data")).unwrap(); assert!(with_home(&home, || uninstall_package_from(app_id, &other)).is_err()); assert!(other.join(app_id).join("data").is_dir()); @@ -1855,7 +2439,11 @@ entry = "ui/index.html" assert!(store.join(&app_id).join("app.wasm").exists()); assert!(store.join(&app_id).join("wapp.toml").exists()); assert!(store.join(&app_id).join("ui").join("index.html").exists()); - assert!(install_package_to(&src, &store, InstallMode::Development).is_err()); + // The store links the app to an immutable revision; installing + // the same content again keeps that revision. + let revision = std::fs::read_link(store.join(&app_id)).unwrap(); + assert!(install_package_to(&src, &store, InstallMode::Development).is_ok()); + assert_eq!(std::fs::read_link(store.join(&app_id)).unwrap(), revision); let _ = fs::remove_dir_all(&src); let _ = fs::remove_dir_all(&store); }); diff --git a/docs/architecture.md b/docs/architecture.md index 4a94562..c11ee41 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), `uninstall`, `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`, `list`, `build-image` (EROFS dm-verity), `info`. ### weft-file-portal @@ -115,11 +115,17 @@ Package store roots (in priority order): `weft-appd` resolves a launch once. A verified image in any store root takes precedence over a directory install in any root, so a user's directory cannot shadow a system image; otherwise the first root with a directory install is used: - **Verified image:** `/.app.img` with its dm-verity hash tree `.app.hash` and root hash `.app.roothash`, the names `weft-pack build-image` and `build-verity` produce. When any of the three exists, the image must mount through `weft-mount-helper`; a missing companion, a malformed root hash, a missing helper or a failed mount refuses the launch, and appd never falls back to a directory install. The root hash is read from the store next to the image and is not yet authenticated, so dm-verity detects corruption but not a store whose image, hash tree and root hash were replaced together. The mount belongs to the session and is released when the session stops. -- **Directory install:** `//wapp.toml`. +- **Directory install:** `/`, a link to the active revision `.revisions//` written by `weft-pack`, or, for a store written before revisions, a package directory. A link is followed only in exactly that form, to one of the app's own revisions; any other link is refused (403). The session runs from the revision directory itself and holds a shared lock on it until it stops, so it keeps its bytes while an update or rollback switches the link. The package root must be a directory and its manifest a regular file, neither a symbolic link, and the manifest must declare the requested ID. `[runtime].module` and `[ui].entry` must be relative paths of plain components, without symbolic links, naming regular files inside that root. The component, the UI document and the capabilities all come from that one manifest. A refused launch reports 400 (invalid ID), 404 (not installed), 403 (invalid package, image or image metadata) or 500 (the host cannot read the package or mount the image). -A directory install can still be changed by its owner while a session runs, and nothing verifies its content at launch. +A directory install can still be changed by its owner while a session runs; for a verified owner, appd checks its content against the signature at launch (see security.md). + +### Installing and updating + +`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. An installation that fails, or is interrupted at any point, leaves the previously active revision active, or, for a first installation, nothing installed and no owner record created by it. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it. + +The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. App data is never touched by any of these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. ## App data From 9206088d105e89d1bc823af5cd9b853b59df7f93 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:38:10 +0000 Subject: [PATCH 16/22] test(frame): check package updates against running sessions check_update.py installs a test app with weft-pack, runs it with the real runtime and app shell, and checks the following: - an update leaves a running session on the bytes and the revision it started from; - a failed update keeps the active revision; - a later launch gets the update; - the old revision is collected only after its session ends; - rollback reactivates the kept revision; - uninstall refuses new launches without disturbing a running session, and a later uninstall removes the revisions. --- docs/building.md | 1 + tests/frame/check_update.py | 219 ++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100755 tests/frame/check_update.py diff --git a/docs/building.md b/docs/building.md index 5afe1d9..0622109 100644 --- a/docs/building.md +++ b/docs/building.md @@ -88,6 +88,7 @@ python3 tests/frame/check_counter.py # Counter round trip through the session python3 tests/frame/check_notes.py # Notes stores exactly what is typed, refuses stale saves python3 tests/frame/check_app_data.py # Notes data from the earlier layout is moved and kept python3 tests/frame/check_confinement.py # an app page reaches only its UI files and its session bridge +python3 tests/frame/check_update.py # updates leave running sessions on their revision; failed updates, rollback and uninstall ``` ## Demo apps (wasm32-wasip2) diff --git a/tests/frame/check_update.py b/tests/frame/check_update.py new file mode 100755 index 0000000..92e8c7b --- /dev/null +++ b/tests/frame/check_update.py @@ -0,0 +1,219 @@ +#!/usr/bin/env python3 +"""Check that package updates are transactional and leave running sessions alone. + +The harness installs a test application with weft-pack into a private store +and runs weft-appd with the real runtime and app shell on a nested desktop +(see session.py). Each revision of the application's page shows its own +colour. In the presented pixels and the store: + +1. A session started from revision one keeps showing it after the package is + updated to revision two, and its app shell runs from revision one's own + directory, which the update keeps while the session runs. +2. An update that fails (an invalid component) changes nothing: revision two + stays active. +3. A session launched after the update shows revision two. +4. Once the first session has ended, the next update removes revision one, + and keeps revision two for rollback; `weft-pack rollback` makes revision + two active again. +5. Uninstalling while a session runs stops new launches (404) but not the + session; the next uninstall, after it has ended, removes the revisions. + +The application is unsigned development content using the Counter demo's +component. +""" + +import argparse +import os +import shutil +import subprocess +import sys +import tempfile +import time +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +from check_counter import AppdClient, free_port, read_endpoint # noqa: E402 +from session import ROOT, Desktop, SessionError, bounding_box, missing_tools, parse_ppm # noqa: E402 + +APP_ID = "org.weft.test.update" +COLOURS = {"one": (200, 0, 0), "two": (0, 0, 200), "three": (0, 160, 0)} +VERSIONS = {"one": 1, "two": 2, "bad": 3, "three": 4} +STARTUP_TIMEOUT = 120.0 + + +def write_revision(src, name, wasm=None): + shutil.rmtree(src, ignore_errors=True) + (src / "ui").mkdir(parents=True) + (src / "wapp.toml").write_text( + f'[package]\nid = "{APP_ID}"\nname = "Update"\nversion = "0.0.{VERSIONS[name]}"\n' + '[runtime]\nmodule = "app.wasm"\n[ui]\nentry = "ui/index.html"\n') + if wasm is None: + shutil.copy(ROOT / "examples/org.weft.demo.counter/app.wasm", src / "app.wasm") + else: + (src / "app.wasm").write_bytes(wasm) + colour = COLOURS.get(name, (0, 0, 0)) + (src / "ui/index.html").write_text( + '') + + +def shows(desktop, colour, timeout=20): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + width, height, pixels = parse_ppm(desktop.capture()) + if bounding_box(width, height, pixels, colour) is not None: + return True + time.sleep(0.3) + return False + + +def run(args, desktop, store, home): + t = Path(args.target) + env = dict(os.environ, WEFT_APP_STORE=str(store), HOME=str(home), + XDG_DATA_HOME=str(home / "share")) + + def pack(*arguments, ok=True): + done = subprocess.run([t / "weft-pack", *arguments], env=env, + capture_output=True, text=True, timeout=60) + if ok and done.returncode != 0: + raise AssertionError(f"weft-pack {' '.join(arguments)} failed: {done.stderr}") + if not ok and done.returncode == 0: + raise AssertionError(f"weft-pack {' '.join(arguments)} succeeded: {done.stdout}") + return done.stdout + done.stderr + + revisions = store / ".revisions" / APP_ID + + def active(): + return os.readlink(store / APP_ID) + + src = home / "src" + write_revision(src, "one") + pack("install", str(src), "--dev") + one = store / active() + + desktop.launch_client("appd", [t / "weft-appd"], { + "WEFT_RUNTIME_BIN": str(t / "weft-runtime"), + "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), + "WEFT_APP_STORE": str(store), + "WEFT_DISABLE_CGROUP": "1", + "WEFT_APPD_WS_PORT": str(free_port()), + "HOME": str(home), + "XDG_DATA_HOME": str(home / "share"), + }) + port, token = read_endpoint(desktop.runtime, 30) + appd = AppdClient(port, token) + + def launch(): + appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0}) + reply = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), STARTUP_TIMEOUT) + if reply.get("type") != "APP_READY": + raise AssertionError(f"the app did not start: {reply}") + return reply["session_id"] + + def stop(session_id): + appd.send({"type": "TERMINATE_APP", "session_id": session_id, "force": True}) + appd.wait_for(lambda m: m.get("type") == "APP_STATE" and m.get("state") == "stopped" + and m.get("session_id") == session_id, 20) + + first = launch() + if not shows(desktop, COLOURS["one"]): + raise AssertionError("revision one is not shown") + shells = subprocess.run(["pgrep", "-af", "weft-app-shell"], capture_output=True, + text=True).stdout + if str(one / "ui/index.html") not in shells: + raise AssertionError(f"the app shell does not run from revision one: {shells}") + + # 1. An update leaves the running session on its revision. + write_revision(src, "two") + pack("install", str(src), "--dev") + two = store / active() + if two == one: + raise AssertionError("the update did not activate a new revision") + time.sleep(2) + if not shows(desktop, COLOURS["one"], 5) or shows(desktop, COLOURS["two"], 1): + raise AssertionError("the update changed what the running session shows") + if (one / "ui/index.html").read_text().find("200, 0, 0") < 0: + raise AssertionError("the running session's revision changed") + + # 2. A failed update changes nothing. + write_revision(src, "bad", wasm=b"not a component") + pack("install", str(src), "--dev", ok=False) + if store / active() != two: + raise AssertionError("a failed update changed the active revision") + + # 3. A new launch gets the update. + second = launch() + if not shows(desktop, COLOURS["two"]): + raise AssertionError("a launch after the update does not show revision two") + stop(second) + + # 4. With the first session ended, the next update collects revision one. + if not one.is_dir(): + raise AssertionError("revision one was removed while its session ran") + stop(first) + write_revision(src, "three") + pack("install", str(src), "--dev") + if one.exists(): + raise AssertionError("revision one was kept after its session ended") + if not two.is_dir(): + raise AssertionError("the previous revision was not kept for rollback") + pack("rollback", APP_ID) + if store / active() != two: + raise AssertionError("rollback did not make revision two active") + third = launch() + if not shows(desktop, COLOURS["two"]): + raise AssertionError("a launch after rollback does not show revision two") + + # 5. Uninstall stops new launches, not the running session. + pack("uninstall", APP_ID) + appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0}) + refused = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 30) + if refused.get("code") != 404: + raise AssertionError(f"a launch after uninstall was not refused: {refused}") + if not two.is_dir() or not shows(desktop, COLOURS["two"], 5): + raise AssertionError("uninstall disturbed the running session") + stop(third) + pack("uninstall", APP_ID, ok=False) + if revisions.exists(): + raise AssertionError(f"revisions left after uninstall: {list(revisions.iterdir())}") + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--compositor", type=Path, default=ROOT / "target/debug/weft-compositor") + parser.add_argument("--target", type=Path, default=ROOT / "target/debug", + help="directory with weft-appd, weft-pack, weft-runtime and weft-app-shell") + parser.add_argument("--output", type=Path, default=ROOT / "target/update-check") + args = parser.parse_args(argv) + + missing = missing_tools() + [str(p) for p in (args.compositor, args.target / "weft-appd", + args.target / "weft-pack", + args.target / "weft-runtime", + args.target / "weft-app-shell") + if not p.is_file()] + if missing: + print("missing: " + ", ".join(missing), file=sys.stderr) + return 2 + store = Path(tempfile.mkdtemp(prefix="weft-store-")) + home = Path(tempfile.mkdtemp(prefix="weft-home-")) + try: + with Desktop(args.compositor, args.output, outputs=["appd.log", "update.ppm"]) as desktop: + try: + run(args, desktop, store, home) + except AssertionError: + (args.output / "update.ppm").write_bytes(desktop.capture()) + raise + except (AssertionError, SessionError, TimeoutError) as failure: + print(f"update check failed: {failure}", file=sys.stderr) + print(f"logs and capture in {args.output}", file=sys.stderr) + return 1 + finally: + shutil.rmtree(store, ignore_errors=True) + shutil.rmtree(home, ignore_errors=True) + print("update check passed") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) From 194fb9f0fa75e5fd9bdc84a77d4fadf4dc392432 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:44:50 +0000 Subject: [PATCH 17/22] fix(pack): keep earlier app data and committed activations through updates An update of a package directory from before revisions removed it together with the app data an earlier weft-appd kept in its data directory. That data is now moved out under the uninstall rules first, or the update stops. A failure after the store link was switched removed the new revision while the link pointed to it. The switch is now the commit point; cleanup after it is left to collection. Other fixes: - A revision is reused only when its content and signature file match. - The revisions directory is flushed before activation. - A rollback link is not inherited by a new installation, and rollback refuses a link to the active revision. - Staging and unpacking directories are held while in use, so another operation never collects them. - weft-appd refuses a launch, instead of waiting, while a package operation holds the revision. - Listings read only an app's active package. --- crates/weft-appd/src/main.rs | 18 +- crates/weft-ipc-types/src/store.rs | 15 +- crates/weft-pack/src/main.rs | 342 +++++++++++++++++++++++------ docs/architecture.md | 8 +- 4 files changed, 305 insertions(+), 78 deletions(-) diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index a681762..bf588fd 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -631,12 +631,22 @@ fn scan_installed_apps() -> Vec { continue; }; for entry in entries.flatten() { - let Ok(m) = weft_ipc_types::manifest::Manifest::read(&entry.path()) else { + // Only an app's active package, named after the ID its manifest + // declares, is installed; staging copies, revisions and stray + // directories or links are not. + let Some(name) = entry.file_name().to_str().map(str::to_owned) else { continue; }; - // Only a directory named after the ID its manifest declares is an - // installed package; staging copies and stray directories are not. - if entry.file_name().to_str() != Some(m.package.id.as_str()) { + if !weft_ipc_types::package::is_valid_app_id(&name) { + continue; + } + let Ok(Some(found)) = weft_ipc_types::store::active(&root, &name) else { + continue; + }; + let Ok(m) = weft_ipc_types::manifest::Manifest::read(found.dir()) else { + continue; + }; + if m.package.id != name { continue; } if seen.insert(m.package.id.clone()) { diff --git a/crates/weft-ipc-types/src/store.rs b/crates/weft-ipc-types/src/store.rs index ff08eb3..49c117b 100644 --- a/crates/weft-ipc-types/src/store.rs +++ b/crates/weft-ipc-types/src/store.rs @@ -118,10 +118,21 @@ pub struct Pin { /// Pins the package directory `dir`. The lock is taken on the directory /// that is opened, and the path must still name that directory afterwards: -/// a revision removed or replaced in between is not pinned. +/// a revision removed or replaced in between is not pinned. A directory a +/// package operation holds is not waited for, so nobody able to lock a +/// package directory can stall launches; the launch is refused instead. pub fn pin(dir: &Path) -> std::io::Result { let file = File::open(dir)?; - file.lock_shared()?; + match file.try_lock_shared() { + Ok(()) => {} + Err(std::fs::TryLockError::WouldBlock) => { + return Err(std::io::Error::other(format!( + "{} is being changed; try again", + dir.display() + ))); + } + Err(std::fs::TryLockError::Error(e)) => return Err(e), + } if !same_file(&file, dir)? { return Err(std::io::Error::other(format!( "{} changed while it was being pinned", diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index 40d5199..e7d0857 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -305,6 +305,8 @@ fn install_into( // A fresh private directory in the store, removed whatever happens, so // nothing can be planted in it and nothing is left behind. let unpacked = private_scratch_dir(store_root, ".unpack")?; + let _in_use = weft_ipc_types::store::try_claim(&unpacked) + .with_context(|| format!("open {}", unpacked.display()))?; let result = unbundle_package(path, &unpacked) .and_then(|()| install_dir(&unpacked.join(name), store_root, mode, claim_data)); let _ = std::fs::remove_dir_all(&unpacked); @@ -392,7 +394,13 @@ fn install_dir( .with_context(|| format!("resolve {}", dir.display())) .and_then(|source| copy_dir(&source, &staging)) .with_context(|| format!("copy {} -> {}", dir.display(), staging.display())) - .and_then(|()| install_staged(&staging, store_root, mode, claim_data)); + .and_then(|()| { + // Held while the copy is in use, so the collection of another + // operation never takes it for abandoned. + let _in_use = weft_ipc_types::store::try_claim(&staging) + .with_context(|| format!("open {}", staging.display()))?; + install_staged(&staging, store_root, mode, claim_data) + }); if result.is_err() { let _ = std::fs::remove_dir_all(&staging); } @@ -414,25 +422,24 @@ fn install_staged( // Installations and uninstallations of one ID run one at a time, so the // owner record always describes the package that is placed. let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; - // An earlier weft-appd could leave only app data, or an empty directory, - // where the package now goes; move the data to its own location so it - // neither blocks nor joins the package. - if dest.is_dir() && only_holds_app_data(&dest)? { - if std::fs::symlink_metadata(dest.join("data")).is_ok() && is_user_store(store_root) { - move_app_data_out(app_id, &dest)?; - } - let _ = std::fs::remove_dir(&dest); - } - // A package installed before revisions existed is replaced by the new - // revision only while no session runs from it, since it is not kept. + // A package directory from before revisions existed is replaced by the + // new revision only while no session runs from it, since it is not kept, + // and only once the app data an earlier weft-appd kept inside it has + // been moved to its own location: the update stops rather than replace + // data with the package. A directory that held only data, or nothing, + // is no package to replace. let legacy = match active(store_root, app_id)? { - Some(Active::Directory(dir)) => Some( - weft_ipc_types::store::try_claim(&dir) + Some(Active::Directory(dir)) => { + let claim = weft_ipc_types::store::try_claim(&dir) .with_context(|| format!("open {}", dir.display()))? .with_context(|| { format!("{app_id} is running from {}; close it first", dir.display()) - })?, - ), + })?; + keep_legacy_data(app_id, store_root, &dir)?; + let emptied = + std::fs::symlink_metadata(&dir).is_err() || std::fs::remove_dir(&dir).is_ok(); + (!emptied).then_some(claim) + } _ => None, }; let admission = admit(staging, app_id, mode, &data_home, claim_data)?; @@ -497,21 +504,33 @@ fn place( create_store(&revisions)?; let dir = revisions.join(&revision); sync_tree(staging)?; - match weft_ipc_types::package::rename_no_replace(staging, &dir) { - Ok(()) => created_revision = Some(dir.clone()), - // The same content is already installed; it is activated as is. - Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + // A revision of the same name is reused only when it still holds this + // content and the same signature file; anything else under the name, + // such as the remains of an interrupted removal, is replaced. + if std::fs::symlink_metadata(&dir).is_ok() { + if same_revision(&dir, staging, &revision) { let _ = std::fs::remove_dir_all(staging); + return Ok(revision); } - Err(e) => { - return Err(e) - .with_context(|| format!("move {} -> {}", staging.display(), dir.display())); - } + let claim = weft_ipc_types::store::try_claim(&dir) + .with_context(|| format!("open {}", dir.display()))? + .with_context(|| { + format!("{} is in use and differs; try again later", dir.display()) + })?; + remove_claimed(store_root, &dir); + drop(claim); } + weft_ipc_types::package::rename_no_replace(staging, &dir) + .with_context(|| format!("move {} -> {}", staging.display(), dir.display()))?; + created_revision = Some(dir.clone()); sync_dir(&revisions)?; + sync_dir(&store_root.join(weft_ipc_types::store::REVISIONS_DIR))?; + Ok(revision) + })() + .and_then(|revision| { activate(store_root, app_id, &revision)?; Ok(revision) - })(); + }); if result.is_err() { let _ = std::fs::remove_dir_all(staging); if let Some(dir) = &created_revision { @@ -529,14 +548,25 @@ fn place( /// an interruption between the two steps keeps both revisions. A package /// directory from before revisions existed is exchanged with the new link in /// one step and then removed; the caller holds it so no session runs from it. +/// An error means the switch did not happen: once the link is in place, the +/// activation stands, and what remains to tidy is left to `collect`. fn activate(store_root: &Path, app_id: &str, revision: &str) -> anyhow::Result<()> { use weft_ipc_types::store::{link_target, revisions_of}; let revisions = revisions_of(store_root, app_id); let current = active(store_root, app_id)?; - if let Some(Active::Revision { name, .. }) = ¤t - && name != revision - { - replace_link(&revisions, PREVIOUS_LINK, Path::new(name))?; + match ¤t { + Some(Active::Revision { name, .. }) if name != revision => { + replace_link(&revisions, PREVIOUS_LINK, Path::new(name))?; + } + Some(Active::Revision { .. }) => {} + // Nothing earlier of this installation to roll back to; a link left + // by an earlier, interrupted uninstall is dropped. + _ => match std::fs::remove_file(revisions.join(PREVIOUS_LINK)) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => { + return Err(e).context("remove the stale rollback link"); + } + _ => {} + }, } let dest = store_root.join(app_id); let link = temporary_link(store_root, app_id, &link_target(app_id, revision))?; @@ -548,15 +578,31 @@ fn activate(store_root: &Path, app_id: &str, revision: &str) -> anyhow::Result<( let _ = std::fs::remove_file(&link); return Err(e).with_context(|| format!("activate {}", dest.display())); } - sync_dir(store_root)?; + if let Err(e) = sync_dir(store_root) { + eprintln!("{e:#}"); + } if let Some(Active::Directory(_)) = current { - // The earlier package directory now has the temporary name. - std::fs::remove_dir_all(&link) - .with_context(|| format!("remove the replaced package at {}", link.display()))?; + // The earlier package directory now has the temporary name, which + // `collect` also recognises. + remove_claimed(store_root, &link); } Ok(()) } +/// Whether the revision directory `dir` holds the content named `revision` +/// and the same signature file as `staging`. +fn same_revision(dir: &Path, staging: &Path, revision: &str) -> bool { + let signature = + |root: &Path| match std::fs::read(root.join(weft_ipc_types::trust::SIGNATURE_FILE)) { + Ok(bytes) => Some(Some(bytes)), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Some(None), + Err(_) => None, + }; + weft_ipc_types::trust::content_digest(dir).is_ok_and(|d| hex::encode(d) == revision) + && signature(dir).is_some() + && signature(dir) == signature(staging) +} + /// Creates a symbolic link to `target` under a fresh temporary name in /// `store_root`, named so the collection of `app_id` can recognise it. fn temporary_link(store_root: &Path, app_id: &str, target: &Path) -> anyhow::Result { @@ -702,8 +748,11 @@ fn collect(store_root: &Path, app_id: &str) { .iter() .any(|p| name.starts_with(p)) && abandoned(&path) + && let Ok(Some(claim)) = try_claim(&path) { + // An operation in progress holds its directory. let _ = std::fs::remove_dir_all(&path); + drop(claim); } } } @@ -746,10 +795,11 @@ fn rollback_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 check who owns the app ID")?; let _lock = weft_ipc_types::trust::lock_owner(&data_home, app_id)?; - let Some(Active::Revision { .. }) = active(store_root, app_id)? else { + let Some(Active::Revision { name: current, .. }) = active(store_root, app_id)? else { anyhow::bail!("{app_id} has no installed revision to roll back from"); }; let previous = previous_revision(store_root, app_id) + .filter(|previous| *previous != current) .with_context(|| format!("{app_id} has no earlier revision to roll back to"))?; let dir = weft_ipc_types::store::revisions_of(store_root, app_id).join(&previous); let record = weft_ipc_types::trust::owner_record_path(&data_home, app_id); @@ -854,8 +904,11 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> match active(store_root, app_id)? { None => { // Revisions left by sessions that ran past an earlier uninstall - // are removed now that nothing uses them. - collect(store_root, app_id); + // are removed now that nothing uses them; collecting needs the + // owner lock. + if data_home.is_some() { + collect(store_root, app_id); + } // An installation interrupted between recording its owner and // placing the package leaves a record with nothing installed; // with no data either, it holds the ID for nothing and is @@ -882,7 +935,10 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> sync_dir(store_root)?; let revisions = weft_ipc_types::store::revisions_of(store_root, app_id); let _ = std::fs::remove_file(revisions.join(PREVIOUS_LINK)); - collect(store_root, app_id); + let _ = sync_dir(&revisions); + if data_home.is_some() { + collect(store_root, app_id); + } } Some(Active::Directory(_)) => uninstall_directory(app_id, store_root, &target)?, } @@ -897,6 +953,28 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> Ok(()) } +/// Moves the app data an earlier weft-appd kept in `/data` to the +/// app's data directory, before the package directory `dir` is removed or +/// replaced. A `data` directory is never removed with a package: packages +/// cannot ship one, so it holds app data from an earlier version. It is +/// moved when this is the user store and the package declares app data (or +/// its manifest cannot be read); otherwise this stops and leaves everything +/// in place. +fn keep_legacy_data(app_id: &str, store_root: &Path, target: &Path) -> anyhow::Result<()> { + if std::fs::symlink_metadata(target.join("data")).is_ok() { + let may_hold_app_data = load_manifest(target).map_or(true, |m| declares_app_data(&m)); + if !(may_hold_app_data && is_user_store(store_root)) { + anyhow::bail!( + "{} contains a 'data' directory that may hold app data; move it aside before \ + uninstalling or updating", + target.display() + ); + } + move_app_data_out(app_id, target)?; + } + Ok(()) +} + /// Removes a package directory installed before revisions existed, which /// only happens while no session runs from it. fn uninstall_directory(app_id: &str, store_root: &Path, target: &Path) -> anyhow::Result<()> { @@ -908,22 +986,7 @@ fn uninstall_directory(app_id: &str, store_root: &Path, target: &Path) -> anyhow target.display() ) })?; - // A `data` directory is never removed with the package: packages cannot - // ship one, so it holds app data from an earlier version. It is moved to - // the app data directory when this is the user store and the package - // declares app data (or its manifest cannot be read); otherwise uninstall - // stops and leaves everything in place. - if std::fs::symlink_metadata(target.join("data")).is_ok() { - let may_hold_app_data = load_manifest(target).map_or(true, |m| declares_app_data(&m)); - if !(may_hold_app_data && is_user_store(store_root)) { - anyhow::bail!( - "{} contains a 'data' directory that may hold app data; move it aside before \ - uninstalling", - target.display() - ); - } - move_app_data_out(app_id, target)?; - } + keep_legacy_data(app_id, store_root, target)?; // Moving the data removes a package directory it leaves empty. match std::fs::remove_dir_all(target) { Err(e) if e.kind() != std::io::ErrorKind::NotFound => { @@ -948,7 +1011,10 @@ fn installed_in_another_store(app_id: &str, store_root: &Path) -> bool { roots .iter() .filter(|root| root.as_path() != store_root) - .any(|root| Manifest::read(&root.join(app_id)).is_ok_and(|m| m.package.id == app_id)) + .any(|root| { + matches!(active(root, app_id), Ok(Some(found)) + if Manifest::read(found.dir()).is_ok_and(|m| m.package.id == app_id)) + }) } /// Removes the owner record of `app_id` when the app has no data, and @@ -965,15 +1031,6 @@ fn release_owner_without_data(data_home: &Path, app_id: &str) -> bool { && std::fs::remove_file(weft_ipc_types::trust::owner_record_path(data_home, app_id)).is_ok() } -/// Whether a package directory is empty or contains nothing but `data`. -fn only_holds_app_data(package_dir: &Path) -> anyhow::Result { - let entries = std::fs::read_dir(package_dir) - .with_context(|| format!("read {}", package_dir.display()))? - .map(|entry| entry.map(|e| e.file_name())) - .collect::, _>>()?; - Ok(entries.is_empty() || entries == ["data"]) -} - /// Whether the manifest declares an app-data capability; only such apps /// had data kept in the package directory. fn declares_app_data(manifest: &Manifest) -> bool { @@ -1048,14 +1105,22 @@ fn list_installed() { }; let mut pkgs: Vec<(String, String, String)> = Vec::new(); for entry in entries.flatten() { - let Ok(m) = Manifest::read(&entry.path()) else { + // Only an app's active package, named after the ID its manifest + // declares, is installed; staging copies, revisions and stray + // directories or links are not. + let Some(name) = entry.file_name().to_str().map(str::to_owned) else { continue; }; - // Only a directory named after the ID its manifest declares is an - // installed package; staging copies and stray directories are not. - if entry.file_name().to_str() != Some(m.package.id.as_str()) - || !weft_ipc_types::package::is_valid_app_id(&m.package.id) - { + if !weft_ipc_types::package::is_valid_app_id(&name) { + continue; + } + let Ok(Some(found)) = active(&root, &name) else { + continue; + }; + let Ok(m) = Manifest::read(found.dir()) else { + continue; + }; + if m.package.id != name { continue; } if seen.insert(m.package.id.clone()) { @@ -1649,6 +1714,145 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn an_update_keeps_app_data_from_a_package_directory() { + let home = temp_root("legacy_data"); + let app_id = "org.weft.test.legacydata"; + let src = home.join("src"); + write_revision(&src, app_id, "new"); + // In the user store, the data an earlier weft-appd kept in the + // package directory moves to the app data directory. + let store = home.join(".local/share/weft/apps"); + write_package(&store.join(app_id), app_id, APP_DATA); + std::fs::create_dir_all(store.join(app_id).join("data")).unwrap(); + std::fs::write(store.join(app_id).join("data/notes.txt"), "kept").unwrap(); + weft_ipc_types::trust::write_owner( + &weft_ipc_types::trust::owner_record_path(&home.join("share"), app_id), + Owner::Development, + ) + .unwrap(); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + let kept = weft_ipc_types::package::app_data_dir(&home.join("share"), app_id); + assert_eq!( + std::fs::read_to_string(kept.join("notes.txt")).unwrap(), + "kept" + ); + assert_eq!(active_ui(&store, app_id), "new"); + + // Elsewhere nothing claims to know what it is: the update stops. + let other = home.join("other-store"); + write_package(&other.join(app_id), app_id, APP_DATA); + std::fs::create_dir_all(other.join(app_id).join("data")).unwrap(); + std::fs::write(other.join(app_id).join("data/notes.txt"), "here").unwrap(); + assert!( + with_home(&home, || install_package_to( + &src, + &other, + InstallMode::Development + )) + .is_err() + ); + assert_eq!( + std::fs::read_to_string(other.join(app_id).join("data/notes.txt")).unwrap(), + "here" + ); + assert!(matches!( + active(&other, app_id).unwrap(), + Some(Active::Directory(_)) + )); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn rollback_refuses_a_revision_whose_key_was_withdrawn() { + let home = temp_root("rollback_revoked"); + let store = home.join("store"); + let app_id = "org.weft.test.revoked"; + let src = home.join("src"); + let signing = key(&home, "publisher", 9, true); + for marker in ["one", "two"] { + write_revision(&src, app_id, marker); + sign_package(&src, &signing).unwrap(); + with_trust(&home, || { + install_package_to(&src, &store, InstallMode::Verified) + }) + .unwrap(); + } + std::fs::remove_file(home.join("keys/publisher.pub")).unwrap(); + let refused = with_trust(&home, || rollback_package(&store, app_id)); + assert!( + refused + .as_ref() + .is_err_and(|e| e.to_string().contains("no longer signed")), + "{refused:?}" + ); + assert_eq!(active_ui(&store, app_id), "two"); + let _ = std::fs::remove_dir_all(&home); + } + + #[test] + fn a_damaged_revision_is_not_reused() { + let home = temp_root("damaged"); + let store = home.join("store"); + let app_id = "org.weft.test.damaged"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let one = active_dir(&store, app_id); + write_revision(&src, app_id, "two"); + install().unwrap(); + // The kept revision is damaged, as by an interrupted removal. + std::fs::remove_file(one.join("app.wasm")).unwrap(); + write_revision(&src, app_id, "one"); + install().unwrap(); + assert_eq!(active_dir(&store, app_id), one); + assert!( + one.join("app.wasm").is_file(), + "the damaged revision was reused" + ); + let _ = std::fs::remove_dir_all(&home); + } + + #[cfg(unix)] + #[test] + fn a_new_installation_does_not_inherit_a_rollback_link() { + let home = temp_root("stale_previous"); + let store = home.join("store"); + let app_id = "org.weft.test.stale"; + let src = home.join("src"); + write_revision(&src, app_id, "one"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + let one = active(&store, app_id).unwrap().unwrap(); + let Active::Revision { name, .. } = one else { + panic!() + }; + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + // An uninstall interrupted before it removed the rollback link, with + // the revision still held by a session. + let revisions = weft_ipc_types::store::revisions_of(&store, app_id); + std::fs::create_dir_all(revisions.join(&name)).unwrap(); + std::os::unix::fs::symlink(&name, revisions.join(PREVIOUS_LINK)).unwrap(); + write_revision(&src, app_id, "two"); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert!(with_home(&home, || rollback_package(&store, app_id)).is_err()); + assert_eq!(active_ui(&store, app_id), "two"); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn leftovers_of_interrupted_operations_are_removed() { let home = temp_root("leftovers"); diff --git a/docs/architecture.md b/docs/architecture.md index c11ee41..d4220d3 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -119,13 +119,15 @@ Package store roots (in priority order): The package root must be a directory and its manifest a regular file, neither a symbolic link, and the manifest must declare the requested ID. `[runtime].module` and `[ui].entry` must be relative paths of plain components, without symbolic links, naming regular files inside that root. The component, the UI document and the capabilities all come from that one manifest. A refused launch reports 400 (invalid ID), 404 (not installed), 403 (invalid package, image or image metadata) or 500 (the host cannot read the package or mount the image). -A directory install can still be changed by its owner while a session runs; for a verified owner, appd checks its content against the signature at launch (see security.md). +A directory install can still be changed by its owner while a session runs; for a verified owner, appd checks its content against the signature at launch, with the limits security.md describes. ### Installing and updating -`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. An installation that fails, or is interrupted at any point, leaves the previously active revision active, or, for a first installation, nothing installed and no owner record created by it. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it. +`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops and changes nothing. -The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. App data is never touched by any of these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. +The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. Apart from that move of data from the earlier layout, app data is never touched by these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. + +A launch is refused (500, try again) while a package operation holds the revision it would run from, rather than waiting for it. Only sessions started by this weft-appd hold their revisions: a session left from an earlier version, or processes that outlive weft-appd, do not, and a package directory from before revisions such sessions run from can be replaced under them. Package operations on one app are serialised by the owner lock in the data home of the user running them; the store itself is not locked against users with other data homes. ## App data From a65fb89662bbdcdb769e7ffecbd3865fd0eef9ab Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:52:38 +0000 Subject: [PATCH 18/22] fix(pack): replace a differing revision of the same name in one exchange Installing the same content with another signature file gives the same revision name. The existing revision, possibly the active one, was removed before the new copy was renamed into place, so a failure in between left the store link pointing at nothing. The new copy now replaces it in one exchange. If an activation fails before its switch, the kept rollback link is restored, and dropping a stale rollback link is flushed. The staging claim is released once the copy is placed, so launches are not refused while the operation finishes. A launch now passes over a store whose entry for the app is unusable, as the listing does. --- crates/weft-appd/src/launch.rs | 28 +++++--- crates/weft-pack/src/main.rs | 116 +++++++++++++++++++++++++++------ docs/architecture.md | 2 +- 3 files changed, 117 insertions(+), 29 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 5641798..1303055 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -71,12 +71,25 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result Refusal::new(403, e.to_string()), - weft_ipc_types::store::StoreError::Io(..) => Refusal::new(500, e.to_string()), - })?; - let Some(active) = active else { continue }; + let active = match weft_ipc_types::store::active(store, app_id) { + Ok(Some(active)) => active, + Ok(None) => continue, + Err(e) => { + tracing::warn!(app_id, error = %e, "package entry skipped"); + unusable.get_or_insert(match e { + weft_ipc_types::store::StoreError::NotAPackage(_) => { + Refusal::new(403, e.to_string()) + } + weft_ipc_types::store::StoreError::Io(..) => Refusal::new(500, e.to_string()), + }); + continue; + } + }; let dir = active.dir().to_path_buf(); // A directory left with only app data by an earlier layout is not a // package. @@ -96,10 +109,7 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result) -> Result { diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index e7d0857..aa4eaaf 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -397,9 +397,9 @@ fn install_dir( .and_then(|()| { // Held while the copy is in use, so the collection of another // operation never takes it for abandoned. - let _in_use = weft_ipc_types::store::try_claim(&staging) + let in_use = weft_ipc_types::store::try_claim(&staging) .with_context(|| format!("open {}", staging.display()))?; - install_staged(&staging, store_root, mode, claim_data) + install_staged(&staging, store_root, mode, claim_data, in_use) }); if result.is_err() { let _ = std::fs::remove_dir_all(&staging); @@ -407,11 +407,15 @@ fn install_dir( result } +/// Installs the staged copy. `in_use` holds the copy against collection by +/// other operations until it is placed; it is released then, since the copy +/// has become the revision sessions launch from. fn install_staged( staging: &Path, store_root: &Path, mode: InstallMode, claim_data: bool, + in_use: Option, ) -> anyhow::Result<()> { check_package(staging)?; let manifest = load_manifest(staging)?; @@ -445,6 +449,7 @@ fn install_staged( let admission = admit(staging, app_id, mode, &data_home, claim_data)?; let owner = admission.owner; let revision = place(staging, store_root, app_id, admission)?; + drop(in_use); drop(legacy); collect(store_root, app_id); println!( @@ -512,17 +517,28 @@ fn place( let _ = std::fs::remove_dir_all(staging); return Ok(revision); } + // The revision may be the active one, as when the same content is + // installed again with another signature file, so it is replaced + // in one exchange: the name always holds a complete revision. let claim = weft_ipc_types::store::try_claim(&dir) .with_context(|| format!("open {}", dir.display()))? .with_context(|| { - format!("{} is in use and differs; try again later", dir.display()) + format!( + "{} differs from this package but a running session uses it; close \ + the app and install again", + dir.display() + ) })?; - remove_claimed(store_root, &dir); + exchange(staging, &dir) + .with_context(|| format!("replace {} with {}", dir.display(), staging.display()))?; + // The earlier copy now has the staging name. + remove_claimed(store_root, staging); drop(claim); + } else { + weft_ipc_types::package::rename_no_replace(staging, &dir) + .with_context(|| format!("move {} -> {}", staging.display(), dir.display()))?; + created_revision = Some(dir.clone()); } - weft_ipc_types::package::rename_no_replace(staging, &dir) - .with_context(|| format!("move {} -> {}", staging.display(), dir.display()))?; - created_revision = Some(dir.clone()); sync_dir(&revisions)?; sync_dir(&store_root.join(weft_ipc_types::store::REVISIONS_DIR))?; Ok(revision) @@ -554,29 +570,44 @@ fn activate(store_root: &Path, app_id: &str, revision: &str) -> anyhow::Result<( use weft_ipc_types::store::{link_target, revisions_of}; let revisions = revisions_of(store_root, app_id); let current = active(store_root, app_id)?; - match ¤t { + let dest = store_root.join(app_id); + let link = temporary_link(store_root, app_id, &link_target(app_id, revision))?; + let kept_before = std::fs::read_link(revisions.join(PREVIOUS_LINK)).ok(); + let kept = match ¤t { Some(Active::Revision { name, .. }) if name != revision => { - replace_link(&revisions, PREVIOUS_LINK, Path::new(name))?; + replace_link(&revisions, PREVIOUS_LINK, Path::new(name)) } - Some(Active::Revision { .. }) => {} + Some(Active::Revision { .. }) => Ok(()), // Nothing earlier of this installation to roll back to; a link left // by an earlier, interrupted uninstall is dropped. _ => match std::fs::remove_file(revisions.join(PREVIOUS_LINK)) { Err(e) if e.kind() != std::io::ErrorKind::NotFound => { - return Err(e).context("remove the stale rollback link"); + Err(e).context("remove the stale rollback link") } - _ => {} + _ => sync_dir(&revisions), }, - } - let dest = store_root.join(app_id); - let link = temporary_link(store_root, app_id, &link_target(app_id, revision))?; - let switched = match ¤t { - Some(Active::Directory(_)) => exchange(&link, &dest), - _ => std::fs::rename(&link, &dest), }; + let switched = kept.and_then(|()| { + match ¤t { + Some(Active::Directory(_)) => exchange(&link, &dest), + _ => std::fs::rename(&link, &dest), + } + .with_context(|| format!("activate {}", dest.display())) + }); if let Err(e) = switched { let _ = std::fs::remove_file(&link); - return Err(e).with_context(|| format!("activate {}", dest.display())); + // The revision kept for rollback stays what it was. + if let Some(Active::Revision { name, .. }) = ¤t + && name != revision + { + let _ = match &kept_before { + Some(target) => replace_link(&revisions, PREVIOUS_LINK, target), + None => { + std::fs::remove_file(revisions.join(PREVIOUS_LINK)).map_err(anyhow::Error::from) + } + }; + } + return Err(e); } if let Err(e) = sync_dir(store_root) { eprintln!("{e:#}"); @@ -1821,6 +1852,53 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn the_same_content_with_another_signature_replaces_the_active_revision_in_place() { + let home = temp_root("resigned"); + let store = home.join("store"); + let app_id = "org.weft.test.resigned"; + let src = home.join("src"); + let install = || { + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + }; + write_revision(&src, app_id, "one"); + install().unwrap(); + let revision = active_dir(&store, app_id); + sign_package(&src, &key(&home, "dev", 3, false)).unwrap(); + // A session runs from the unsigned copy: it is not replaced under it. + let session = weft_ipc_types::store::pin(&revision).unwrap(); + assert!(install().is_err()); + assert!( + !revision + .join(weft_ipc_types::trust::SIGNATURE_FILE) + .exists() + ); + drop(session); + install().unwrap(); + assert_eq!( + active_dir(&store, app_id), + revision, + "same content, same revision" + ); + assert!( + revision + .join(weft_ipc_types::trust::SIGNATURE_FILE) + .is_file() + ); + assert_eq!(active_ui(&store, app_id), "one"); + assert!(!staging_left(&store)); + let leftovers: Vec<_> = std::fs::read_dir(&store) + .unwrap() + .flatten() + .map(|e| e.file_name()) + .filter(|n| n.to_string_lossy().starts_with(".trash-")) + .collect(); + assert!(leftovers.is_empty(), "{leftovers:?}"); + let _ = std::fs::remove_dir_all(&home); + } + #[cfg(unix)] #[test] fn a_new_installation_does_not_inherit_a_rollback_link() { diff --git a/docs/architecture.md b/docs/architecture.md index d4220d3..02b277c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -123,7 +123,7 @@ A directory install can still be changed by its owner while a session runs; for ### Installing and updating -`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops and changes nothing. +`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file; otherwise the new copy replaces it in one exchange, unless a running session uses it. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops before replacing anything. Data moved this way stays in the app data directory even if the update is then refused. The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. Apart from that move of data from the earlier layout, app data is never touched by these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. From d76d4c726d93fb7d02ece6b6295f18a26520c993 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 17:56:27 +0000 Subject: [PATCH 19/22] test(appd): check that an unusable store entry does not hide a later store The test also covers the refusal reported when no other store has the app. The new comment states that a package found and then refused is not replaced by another store's copy. The architecture doc says that a re-signed copy of the same content is visible at once. --- crates/weft-appd/src/launch.rs | 27 ++++++++++++++++++++++++++- docs/architecture.md | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/weft-appd/src/launch.rs b/crates/weft-appd/src/launch.rs index 1303055..0d929d5 100644 --- a/crates/weft-appd/src/launch.rs +++ b/crates/weft-appd/src/launch.rs @@ -98,7 +98,9 @@ fn resolve_in(app_id: &str, stores: &[PathBuf]) -> Result/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file; otherwise the new copy replaces it in one exchange, unless a running session uses it. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops before replacing anything. Data moved this way stays in the app data directory even if the update is then refused. +`weft-pack install` copies the package into a staging directory in the store and checks, verifies and admits that copy, so the bytes that are checked are the bytes that are installed. The copy is flushed to disk and renamed to an immutable revision named by its content digest, `/.revisions//`; installing the same content again reuses it. One rename of the link `/` then makes it active. The rename of the link is the commit point. An installation that fails before it leaves the previously active revision active, or, for a first installation, nothing installed; a failed first installation also removes the owner record it created, while one interrupted by a crash leaves that record until `weft-pack uninstall ` releases it. After the commit point the update stands, and what an interruption leaves to tidy is collected later. A revision is reused only when it still holds the same content and signature file; otherwise the new copy replaces it in one exchange, unless a running session uses it. Such a replacement, for the same content with another signature file, is visible under the revision's name at once, even if the installation then fails. A package directory from before revisions is replaced in one exchange with the new link, only while no session runs from it and only after the app data an earlier weft-appd kept in its `data` directory has been moved to the app data directory, under the same rules as for uninstall; otherwise the update stops before replacing anything. Data moved this way stays in the app data directory even if the update is then refused. The revision active before an update is kept for `weft-pack rollback `, which makes it active again, keeping the revision it replaces in turn; it is checked against the owner record again, so a revision whose publisher key was withdrawn is not rolled back to. Other revisions are removed by each install, rollback or uninstall of the app, except those a session still runs from; those stay until a later operation on the app finds them unused. Temporary links of interrupted activations are removed then too, and staging, unpacking and removal directories once they are an hour old. `weft-pack uninstall` removes the link, so new launches no longer find the app, and the kept revision; running sessions continue on their revisions. Apart from that move of data from the earlier layout, app data is never touched by these operations. Package revisions and app data are independent: rolling back a package does not roll back its data. From 3186e524ab58a5b47653398a15fda771e0d025de Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 19:10:15 +0000 Subject: [PATCH 20/22] feat: grant an app only the capabilities the user approved Every capability a package declared was granted at launch, so a signed package received all the permissions it asked for. Blueprint 5.2 rules that out. A package now 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/. weft-appd refuses to launch an app declaring a capability the record does not hold, with 403 and the capabilities named, after refusing unknown or unsupported ones as before. An update that declares a new capability needs approval again, and uninstalling forgets the approval. Private app data is a minimal execution resource and needs no approval, so Notes and the existing fixtures are unchanged. Unit tests cover the approval record, install and approve, an update adding a capability, uninstall, and appd refusing and then launching. tests/frame/check_update.py launches a real app declaring notifications: it is refused until weft-pack approve, then runs. --- crates/weft-appd/src/main.rs | 87 +++++++++++++ crates/weft-ipc-types/src/approval.rs | 144 +++++++++++++++++++++ crates/weft-ipc-types/src/lib.rs | 1 + crates/weft-pack/src/main.rs | 175 ++++++++++++++++++++++++-- docs/architecture.md | 2 +- docs/security.md | 2 +- tests/frame/check_update.py | 15 ++- 7 files changed, 413 insertions(+), 13 deletions(-) create mode 100644 crates/weft-ipc-types/src/approval.rs diff --git a/crates/weft-appd/src/main.rs b/crates/weft-appd/src/main.rs index bf588fd..4516ac9 100644 --- a/crates/weft-appd/src/main.rs +++ b/crates/weft-appd/src/main.rs @@ -578,6 +578,29 @@ pub(crate) async fn dispatch(req: Request, registry: &Registry) -> Response { } } +/// Refuses a launch while the app declares a capability the user has not +/// approved; a signature, however trusted, approves nothing. +fn check_approval(app_id: &str, declared: &[String]) -> Result<(), grants::Refusal> { + use weft_ipc_types::approval::{approval_path, read_approved, unapproved}; + let data_home = weft_ipc_types::package::data_home().ok_or_else(|| { + grants::Refusal::new(500, "cannot locate the data home holding app approvals") + })?; + let record = approval_path(&data_home, app_id); + let approved = read_approved(&record) + .map_err(|e| grants::Refusal::new(500, format!("cannot read {}: {e}", record.display())))?; + let pending = unapproved(declared, &approved); + if pending.is_empty() { + return Ok(()); + } + Err(grants::Refusal::new( + 403, + format!( + "{app_id} needs approval for {}; approve it with weft-pack approve {app_id}", + pending.join(", ") + ), + )) +} + /// Resolves the package of `app_id` and derives its grants. Resolution may /// mount an image, so it runs off the async workers; a refused launch /// releases the mount the same way. @@ -586,6 +609,7 @@ async fn resolve_launch( ) -> Result<(launch::LaunchPackage, grants::SessionGrants), grants::Refusal> { tokio::task::spawn_blocking(move || { let package = launch::resolve(&app_id)?; + check_approval(&app_id, &package.capabilities)?; let grants = grants::derive(&app_id, &package.capabilities, grants::HostDirs::from_env)?; tracing::info!( %app_id, @@ -829,6 +853,69 @@ mod tests { assert!(result.is_err()); } + #[tokio::test] + async fn a_capability_launches_only_once_approved() { + let _env = env_lock().lock().await; + let store = std::env::temp_dir().join(format!("weft_approve_{}", std::process::id())); + let id = "org.example.notify"; + write_test_package( + &store.join(id), + id, + "capabilities = [\"sys:notifications\"]", + ); + let prior_store = std::env::var("WEFT_APP_STORE").ok(); + let prior_bin = std::env::var("WEFT_RUNTIME_BIN").ok(); + // SAFETY: env_lock is held and the runtime is current_thread. A + // configured runtime makes appd resolve the package. + unsafe { + std::env::set_var("WEFT_APP_STORE", &store); + std::env::set_var("WEFT_RUNTIME_BIN", "/nonexistent/weft-runtime"); + } + let launch = || async { + dispatch( + Request::LaunchApp { + app_id: id.into(), + surface_id: 0, + }, + &make_registry(), + ) + .await + }; + let refused = launch().await; + let data_home = weft_ipc_types::package::data_home().unwrap(); + let record = weft_ipc_types::approval::approval_path(&data_home, id); + weft_ipc_types::approval::write_approved(&record, &["sys:notifications".to_owned()]) + .unwrap(); + let approved = launch().await; + let _ = std::fs::remove_file(&record); + // SAFETY: as above. + unsafe { + match prior_store { + Some(v) => std::env::set_var("WEFT_APP_STORE", v), + None => std::env::remove_var("WEFT_APP_STORE"), + } + match prior_bin { + Some(v) => std::env::set_var("WEFT_RUNTIME_BIN", v), + None => std::env::remove_var("WEFT_RUNTIME_BIN"), + } + } + let _ = std::fs::remove_dir_all(&store); + match refused { + Response::Error { code, message } => { + assert_eq!(code, 403); + assert!( + message.contains("needs approval for sys:notifications"), + "{message}" + ); + } + other => panic!("an unapproved capability launched: {other:?}"), + } + assert!( + matches!(approved, Response::LaunchAck { .. }), + "{approved:?}" + ); + } + #[tokio::test] async fn launch_is_refused_when_capabilities_cannot_be_granted() { let _env = env_lock().lock().await; diff --git a/crates/weft-ipc-types/src/approval.rs b/crates/weft-ipc-types/src/approval.rs new file mode 100644 index 0000000..f553a39 --- /dev/null +++ b/crates/weft-ipc-types/src/approval.rs @@ -0,0 +1,144 @@ +//! The capabilities the user approved for an installed app. +//! +//! A package declares capabilities; it gets only those the user approved. +//! `weft-pack` records the approval, `$XDG_DATA_HOME/weft/approvals/`, +//! one capability per line, and weft-appd refuses to launch an app that +//! declares a capability the record does not hold. An app's own private +//! data is a minimal execution resource and needs no approval. Signature +//! verification and approval are separate decisions: a trusted signature +//! approves nothing. + +use std::collections::BTreeSet; +use std::path::{Path, PathBuf}; + +use crate::capability::Capability; + +/// Where the approval record of `app_id` lives. +pub fn approval_path(data_home: &Path, app_id: &str) -> PathBuf { + data_home.join("weft/approvals").join(app_id) +} + +/// Whether a declared capability needs the user's approval. Strings that +/// are not capabilities are left to the capability checks, which refuse +/// them. +pub fn needs_approval(capability: &str) -> bool { + capability + .parse::() + .is_ok_and(|c| !matches!(c, Capability::AppData(_))) +} + +/// The declared capabilities that need approval and are not in `approved`. +pub fn unapproved<'a>(declared: &'a [String], approved: &BTreeSet) -> Vec<&'a str> { + declared + .iter() + .map(String::as_str) + .filter(|c| needs_approval(c) && !approved.contains(*c)) + .collect() +} + +/// The capabilities approved for an app; none when there is no record. +pub fn read_approved(record: &Path) -> std::io::Result> { + match std::fs::read_to_string(record) { + Ok(text) => Ok(text + .lines() + .map(str::trim) + .filter(|l| !l.is_empty()) + .map(str::to_owned) + .collect()), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(BTreeSet::new()), + Err(e) => Err(e), + } +} + +/// Records exactly the capabilities of `declared` that need approval as the +/// app's approval, replacing an earlier one in one rename. With none to +/// approve, the record is removed. +pub fn write_approved(record: &Path, declared: &[String]) -> std::io::Result<()> { + use std::io::Write; + let approved: BTreeSet<&str> = declared + .iter() + .map(String::as_str) + .filter(|c| needs_approval(c)) + .collect(); + if approved.is_empty() { + return remove_approval(record); + } + let parent = record + .parent() + .expect("approval records live in a directory"); + std::fs::create_dir_all(parent)?; + let temp = parent.join(format!( + ".{}.{}.tmp", + record + .file_name() + .and_then(|n| n.to_str()) + .unwrap_or("record"), + std::process::id() + )); + let written = (|| { + let mut file = std::fs::File::create(&temp)?; + for capability in &approved { + writeln!(file, "{capability}")?; + } + file.sync_all()?; + std::fs::rename(&temp, record) + })(); + if written.is_err() { + let _ = std::fs::remove_file(&temp); + } + written?; + std::fs::File::open(parent).and_then(|dir| dir.sync_all()) +} + +/// Removes an app's approval, so a later installation asks again. +pub fn remove_approval(record: &Path) -> std::io::Result<()> { + match std::fs::remove_file(record) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e), + _ => Ok(()), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn caps(list: &[&str]) -> Vec { + list.iter().map(|c| (*c).to_owned()).collect() + } + + #[test] + fn private_app_data_needs_no_approval() { + assert!(!needs_approval("fs:rw:app-data")); + assert!(!needs_approval("fs:read:app-data")); + assert!(needs_approval("net:fetch:example.org")); + assert!(needs_approval("sys:notifications")); + assert!(needs_approval("fs:read:xdg-documents")); + // Not a capability: refused elsewhere, not reported as unapproved. + assert!(!needs_approval("sys:everything")); + } + + #[test] + fn approvals_cover_exactly_what_was_approved() { + let dir = std::env::temp_dir().join(format!("weft_approval_{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let record = approval_path(&dir, "org.weft.test"); + let declared = caps(&["fs:rw:app-data", "net:fetch:example.org"]); + assert_eq!(read_approved(&record).unwrap(), BTreeSet::new()); + assert_eq!( + unapproved(&declared, &read_approved(&record).unwrap()), + ["net:fetch:example.org"] + ); + write_approved(&record, &declared).unwrap(); + assert!(unapproved(&declared, &read_approved(&record).unwrap()).is_empty()); + // An update that asks for more needs approval for the new capability. + let updated = caps(&["net:fetch:example.org", "sys:clipboard:read"]); + assert_eq!( + unapproved(&updated, &read_approved(&record).unwrap()), + ["sys:clipboard:read"] + ); + // Nothing left to approve removes the record. + write_approved(&record, &caps(&["fs:rw:app-data"])).unwrap(); + assert!(!record.exists()); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/weft-ipc-types/src/lib.rs b/crates/weft-ipc-types/src/lib.rs index 3521ee0..1db9aa1 100644 --- a/crates/weft-ipc-types/src/lib.rs +++ b/crates/weft-ipc-types/src/lib.rs @@ -1,3 +1,4 @@ +pub mod approval; pub mod capability; pub mod manifest; pub mod package; diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index aa4eaaf..93c1b35 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -20,23 +20,29 @@ fn main() -> anyhow::Result<()> { print_info(&manifest); } Some("install") => { - let usage = "usage: weft-pack install [--dev] [--claim-data]"; + let usage = "usage: weft-pack install [--dev] [--claim-data] [--approve]"; let dir = args.get(2).context(usage)?; let mut mode = InstallMode::Verified; let mut claim_data = false; + let mut approve = false; for arg in &args[3..] { match arg.as_str() { "--dev" => mode = InstallMode::Development, "--claim-data" => claim_data = true, + "--approve" => approve = true, other => anyhow::bail!("unexpected argument '{other}'; {usage}"), } } - install_package(Path::new(dir), mode, claim_data)?; + install_package(Path::new(dir), mode, claim_data, approve)?; } Some("uninstall") => { let app_id = args.get(2).context("usage: weft-pack uninstall ")?; uninstall_package(app_id)?; } + Some("approve") => { + let app_id = args.get(2).context("usage: weft-pack approve ")?; + approve_package(&resolve_install_root()?, app_id)?; + } Some("rollback") => { let app_id = args.get(2).context("usage: weft-pack rollback ")?; rollback_package(&resolve_install_root()?, app_id)?; @@ -123,8 +129,14 @@ fn main() -> anyhow::Result<()> { eprintln!( " --claim-data: adopt app data with no owner" ); + eprintln!( + " --approve: approve its declared capabilities" + ); 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" + ); eprintln!(" weft-pack list list installed packages"); eprintln!( " weft-pack build-image [--out ] create EROFS image with mkfs.erofs" @@ -270,14 +282,19 @@ enum InstallMode { Development, } -fn install_package(path: &Path, mode: InstallMode, claim_data: bool) -> anyhow::Result<()> { +fn install_package( + path: &Path, + mode: InstallMode, + claim_data: bool, + approve: bool, +) -> anyhow::Result<()> { let root = resolve_install_root()?; - install_into(path, &root, mode, claim_data) + install_into(path, &root, mode, claim_data, approve) } #[cfg(test)] fn install_package_to(path: &Path, store_root: &Path, mode: InstallMode) -> anyhow::Result<()> { - install_into(path, store_root, mode, false) + install_into(path, store_root, mode, false, false) } /// Installs the package at `path` into `store_root`. App data that exists @@ -289,12 +306,13 @@ fn install_into( store_root: &Path, mode: InstallMode, claim_data: bool, + approve: bool, ) -> anyhow::Result<()> { create_store(store_root)?; if !(path.extension().is_some_and(|e| e == "zst" || e == "tar") || path.to_string_lossy().ends_with(".app.tar.zst")) { - return install_dir(path, store_root, mode, claim_data); + return install_dir(path, store_root, mode, claim_data, approve); } let name = path .file_name() @@ -308,7 +326,7 @@ fn install_into( let _in_use = weft_ipc_types::store::try_claim(&unpacked) .with_context(|| format!("open {}", unpacked.display()))?; let result = unbundle_package(path, &unpacked) - .and_then(|()| install_dir(&unpacked.join(name), store_root, mode, claim_data)); + .and_then(|()| install_dir(&unpacked.join(name), store_root, mode, claim_data, approve)); let _ = std::fs::remove_dir_all(&unpacked); result } @@ -379,6 +397,7 @@ fn install_dir( store_root: &Path, mode: InstallMode, claim_data: bool, + approve: bool, ) -> anyhow::Result<()> { check_package(dir)?; // The package is copied into the store first and every decision, the app @@ -399,7 +418,7 @@ fn install_dir( // operation never takes it for abandoned. let in_use = weft_ipc_types::store::try_claim(&staging) .with_context(|| format!("open {}", staging.display()))?; - install_staged(&staging, store_root, mode, claim_data, in_use) + install_staged(&staging, store_root, mode, claim_data, approve, in_use) }); if result.is_err() { let _ = std::fs::remove_dir_all(&staging); @@ -415,6 +434,7 @@ fn install_staged( store_root: &Path, mode: InstallMode, claim_data: bool, + approve: bool, in_use: Option, ) -> anyhow::Result<()> { check_package(staging)?; @@ -457,6 +477,75 @@ fn install_staged( dest.display(), &revision[..12] ); + settle_approval(&data_home, app_id, manifest.capabilities(), approve) +} + +/// Records the installed package's capabilities as approved with +/// `approve`; otherwise reports those that still need approval, since the +/// app does not launch until they are approved. +fn settle_approval( + data_home: &Path, + app_id: &str, + declared: &[String], + approve: bool, +) -> anyhow::Result<()> { + use weft_ipc_types::approval::{approval_path, read_approved, unapproved, write_approved}; + let record = approval_path(data_home, app_id); + if approve { + write_approved(&record, declared) + .with_context(|| format!("record the approval of {app_id}"))?; + let approved: Vec<_> = declared + .iter() + .filter(|c| weft_ipc_types::approval::needs_approval(c)) + .collect(); + if !approved.is_empty() { + println!( + "approved for {app_id}: {}", + approved + .iter() + .map(|c| c.as_str()) + .collect::>() + .join(", ") + ); + } + return Ok(()); + } + let approved = read_approved(&record).with_context(|| format!("read {}", record.display()))?; + let pending = unapproved(declared, &approved); + if !pending.is_empty() { + println!( + "{app_id} needs approval for {} before it launches; run weft-pack approve {app_id}", + pending.join(", ") + ); + } + Ok(()) +} + +/// Approves the capabilities the active package of `app_id` declares. +fn approve_package(store_root: &Path, app_id: &str) -> anyhow::Result<()> { + anyhow::ensure!( + weft_ipc_types::package::is_valid_app_id(app_id), + "'{app_id}' is not a valid app ID" + ); + 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()))?; + 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)) + { + println!("{app_id} declares nothing that needs approval"); + } Ok(()) } @@ -979,6 +1068,11 @@ fn uninstall_package_from(app_id: &str, store_root: &Path) -> anyhow::Result<()> && !installed_in_another_store(app_id, store_root) { release_owner_without_data(home, app_id); + // A later installation asks for approval again. + weft_ipc_types::approval::remove_approval(&weft_ipc_types::approval::approval_path( + home, app_id, + )) + .with_context(|| format!("remove the approval of {app_id}"))?; } println!("uninstalled {}", app_id); Ok(()) @@ -1931,6 +2025,55 @@ mod tests { let _ = std::fs::remove_dir_all(&home); } + #[test] + fn capabilities_are_approved_only_when_asked_and_again_for_new_ones() { + use weft_ipc_types::approval::{approval_path, read_approved, unapproved}; + let home = temp_root("approval"); + let store = home.join("store"); + let app_id = "org.weft.test.approval"; + let src = home.join("src"); + let record = approval_path(&home.join("share"), app_id); + let pending = |declared: &[&str]| { + let declared: Vec = declared.iter().map(|c| (*c).to_owned()).collect(); + unapproved(&declared, &read_approved(&record).unwrap()) + .into_iter() + .map(str::to_owned) + .collect::>() + }; + write_package(&src, app_id, "\"sys:notifications\", \"fs:rw:app-data\""); + with_home(&home, || { + install_package_to(&src, &store, InstallMode::Development) + }) + .unwrap(); + assert_eq!(pending(&["sys:notifications"]), ["sys:notifications"]); + with_home(&home, || approve_package(&store, 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( + &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"] + ); + // --approve approves what the installed package declares. + with_home(&home, || { + install_into(&src, &store, InstallMode::Development, false, true) + }) + .unwrap(); + assert!(pending(&["sys:notifications", "sys:clipboard:read"]).is_empty()); + // Uninstalling forgets the approval. + with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); + assert!(!record.exists()); + let _ = std::fs::remove_dir_all(&home); + } + #[test] fn leftovers_of_interrupted_operations_are_removed() { let home = temp_root("leftovers"); @@ -2309,7 +2452,13 @@ mod tests { assert!(message.contains("--claim-data"), "{message}"); assert_eq!(owner_of(&home, app_id), None); with_home(&home, || { - install_into(&home.join("src"), &other, InstallMode::Development, true) + install_into( + &home.join("src"), + &other, + InstallMode::Development, + true, + false, + ) }) .unwrap(); assert_eq!(owner_of(&home, app_id), Some(Owner::Development)); @@ -2550,7 +2699,13 @@ mod tests { assert!(message.contains("--claim-data"), "{message}"); assert!(!store.join(app_id).join("wapp.toml").exists()); with_home(&home, || { - install_into(&home.join("src"), &store, InstallMode::Development, true) + install_into( + &home.join("src"), + &store, + InstallMode::Development, + true, + false, + ) }) .unwrap(); assert!(store.join(app_id).join("wapp.toml").exists()); diff --git a/docs/architecture.md b/docs/architecture.md index 4114b6e..9d493c0 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`, `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 installed app's declared capabilities; `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 55bdf1d..e71560d 100644 --- a/docs/security.md +++ b/docs/security.md @@ -8,7 +8,7 @@ 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`. -Installing a package does not yet ask the user to approve its capabilities; every declared, supported capability is granted at launch. +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. ## Process Isolation diff --git a/tests/frame/check_update.py b/tests/frame/check_update.py index 92e8c7b..88080c7 100755 --- a/tests/frame/check_update.py +++ b/tests/frame/check_update.py @@ -17,6 +17,8 @@ colour. In the presented pixels and the store: two active again. 5. Uninstalling while a session runs stops new launches (404) but not the session; the next uninstall, after it has ended, removes the revisions. +6. An app declaring a capability (notifications) is refused at launch (403) + until `weft-pack approve` approves it, and then launches. The application is unsigned development content using the Counter demo's component. @@ -41,11 +43,12 @@ VERSIONS = {"one": 1, "two": 2, "bad": 3, "three": 4} STARTUP_TIMEOUT = 120.0 -def write_revision(src, name, wasm=None): +def write_revision(src, name, wasm=None, capabilities="[]"): shutil.rmtree(src, ignore_errors=True) (src / "ui").mkdir(parents=True) (src / "wapp.toml").write_text( f'[package]\nid = "{APP_ID}"\nname = "Update"\nversion = "0.0.{VERSIONS[name]}"\n' + f'capabilities = {capabilities}\n' '[runtime]\nmodule = "app.wasm"\n[ui]\nentry = "ui/index.html"\n') if wasm is None: shutil.copy(ROOT / "examples/org.weft.demo.counter/app.wasm", src / "app.wasm") @@ -177,6 +180,16 @@ def run(args, desktop, store, home): if revisions.exists(): raise AssertionError(f"revisions left after uninstall: {list(revisions.iterdir())}") + # 6. A declared capability needs approval before the app launches. + write_revision(src, "one", capabilities='["sys:notifications"]') + pack("install", str(src), "--dev") + appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0}) + refused = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 30) + if refused.get("code") != 403 or "needs approval" not in refused.get("message", ""): + raise AssertionError(f"an unapproved capability launched: {refused}") + pack("approve", APP_ID) + stop(launch()) + def main(argv=None): parser = argparse.ArgumentParser(description=__doc__, From 9b836257785928ccbfbf7178d426a7ec0c3303da Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 19:18:22 +0000 Subject: [PATCH 21/22] 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 From e482fec07cc17d48978c4b9c25ea7995b182f310 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 19:20:52 +0000 Subject: [PATCH 22/22] fix(pack): add approvals by name only for installed, declared capabilities Pruning compared a list of the declared capabilities with the set of approved ones by length. A manifest repeating a capability could therefore keep an approval it no longer declared. The two are now compared as sets. weft-pack approve ... replaced the app's approval instead of adding to it. It also accepted any app ID and any capability, which let an approval cover a capability declared later. It now adds to the approval, requires the app to be installed, and for a package weft-pack can read, requires each listed capability to be declared. The docs state the remaining limits for verified images. --- crates/weft-pack/src/main.rs | 82 +++++++++++++++++++++++++++--------- docs/security.md | 2 +- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/crates/weft-pack/src/main.rs b/crates/weft-pack/src/main.rs index bb1dba4..ce1fcb5 100644 --- a/crates/weft-pack/src/main.rs +++ b/crates/weft-pack/src/main.rs @@ -516,13 +516,13 @@ fn settle_approval( // 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 + let kept: std::collections::BTreeSet = declared .iter() .filter(|c| approved.contains(*c)) .cloned() .collect(); - if kept.len() != approved.len() { - write_approved(&record, &kept) + if kept != approved { + write_approved(&record, &kept.into_iter().collect::>()) .with_context(|| format!("update the approval of {app_id}"))?; } let pending = unapproved(declared, &approved); @@ -549,6 +549,10 @@ fn approve_package(app_id: &str, explicit: &[String]) -> 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 roots = list_installed_roots(); + let image = roots + .iter() + .any(|root| ImageFiles::in_store(root, app_id).any_present()); if !explicit.is_empty() { for capability in explicit { capability @@ -556,26 +560,50 @@ fn approve_package(app_id: &str, explicit: &[String]) -> anyhow::Result<()> { .map_err(|e| anyhow::anyhow!("{e}"))?; anyhow::ensure!(needs_approval(capability), "{capability} needs no approval"); } - return settle_approval(&data_home, app_id, explicit, true); + // Approvals by name add to the app's approval. They are for an + // installed app, and for a package weft-pack can read, only for + // what it declares. + if !image { + let manifest = launched_manifest(&roots, app_id)?; + if let Some(undeclared) = explicit + .iter() + .find(|c| !manifest.capabilities().contains(c)) + { + anyhow::bail!("{app_id} does not declare {undeclared}"); + } + } + let record = weft_ipc_types::approval::approval_path(&data_home, app_id); + let mut approved: Vec = weft_ipc_types::approval::read_approved(&record) + .with_context(|| format!("read {}", record.display()))? + .into_iter() + .collect(); + approved.extend(explicit.iter().cloned()); + return settle_approval(&data_home, app_id, &approved, true); } - let roots = list_installed_roots(); - if roots - .iter() - .any(|root| ImageFiles::in_store(root, app_id).any_present()) - { + if image { 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 manifest = launched_manifest(&roots, app_id)?; + if !manifest.capabilities().iter().any(|c| needs_approval(c)) { + println!("{app_id} declares nothing that needs approval"); + } + settle_approval(&data_home, app_id, manifest.capabilities(), true) +} + +/// The manifest of the package of `app_id` that weft-appd launches from a +/// directory or revision: the first of `roots` holding one. +fn launched_manifest(roots: &[PathBuf], app_id: &str) -> anyhow::Result { 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() => + if std::fs::symlink_metadata( + found.dir().join(weft_ipc_types::manifest::MANIFEST_FILE), + ) + .is_ok() => { Some(found) } @@ -588,10 +616,7 @@ fn approve_package(app_id: &str, explicit: &[String]) -> anyhow::Result<()> { "{} declares another app ID", found.dir().display() ); - if !manifest.capabilities().iter().any(|c| needs_approval(c)) { - println!("{app_id} declares nothing that needs approval"); - } - settle_approval(&data_home, app_id, manifest.capabilities(), true) + Ok(manifest) } /// The active package of `app_id` in `store_root`. @@ -2155,12 +2180,29 @@ mod tests { // 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()]) + with_store(&store, || { + approve_package(app_id, &["sys:clipboard:read".to_owned()]) + }) }) .unwrap(); - assert!(pending(&["sys:clipboard:read"]).is_empty()); + // Added to what was approved before. + assert!(pending(&["sys:notifications", "sys:clipboard:read"]).is_empty()); + let undeclared = with_home(&home, || { + with_store(&store, || { + approve_package(app_id, &["sys:clipboard:write".to_owned()]) + }) + }); + assert!( + undeclared.is_err(), + "approved a capability the package does not declare" + ); for refused in ["fs:rw:app-data", "sys:everything"] { - assert!(with_home(&home, || approve_package(app_id, &[refused.to_owned()])).is_err()); + assert!( + with_home(&home, || with_store(&store, || { + approve_package(app_id, &[refused.to_owned()]) + })) + .is_err() + ); } // Uninstalling forgets the approval. with_home(&home, || uninstall_package_from(app_id, &store)).unwrap(); diff --git a/docs/security.md b/docs/security.md index cf6aa38..affbee8 100644 --- a/docs/security.md +++ b/docs/security.md @@ -8,7 +8,7 @@ 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. `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. +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 ...` adds the listed capabilities to the app's approval; for a package weft-pack can read they must be ones it declares. Listing them is the only way to approve an app installed as a verified image, since weft-pack cannot read an image's manifest; for the same reason, approvals for an image are not reduced when it is updated, and uninstalling a directory copy of the app removes them although the image remains. 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.