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.
This commit is contained in:
Marco Allegretti 2026-10-09 14:29:14 +00:00
parent fb13a99e17
commit 11cea5e7e3
No known key found for this signature in database
7 changed files with 213 additions and 2 deletions

View file

@ -3,11 +3,20 @@
//! The runtime and the app shell receive the exact files resolved here and //! 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 //! 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. //! 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 std::path::{Path, PathBuf};
use weft_ipc_types::manifest::{MANIFEST_FILE, Manifest, ManifestError, entry_path}; 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::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::grants::Refusal;
use crate::mount::Mount; use crate::mount::Mount;
@ -99,6 +108,7 @@ fn from_root(app_id: &str, root: PathBuf, image: Option<Mount>) -> Result<Launch
), ),
)); ));
} }
verify_owner(app_id, &root)?;
let entry = |field: &str, value: &str| { let entry = |field: &str, value: &str| {
entry_path(&root, value).map_err(|e| Refusal::new(403, format!("{field}: {e}"))) entry_path(&root, value).map_err(|e| Refusal::new(403, format!("{field}: {e}")))
}; };
@ -115,6 +125,57 @@ fn from_root(app_id: &str, root: PathBuf, image: Option<Mount>) -> Result<Launch
}) })
} }
/// Checks that the package at `root` is what its owner installed.
fn verify_owner(app_id: &str, root: &Path) -> 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 { impl LaunchPackage {
/// A package for supervisor tests whose children ignore their files. /// A package for supervisor tests whose children ignore their files.
#[cfg(test)] #[cfg(test)]
@ -160,8 +221,17 @@ mod tests {
), ),
) )
.unwrap(); .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. // 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 store
} }
@ -170,6 +240,8 @@ mod tests {
unsafe { unsafe {
std::env::remove_var("WEFT_APP_STORE"); std::env::remove_var("WEFT_APP_STORE");
std::env::remove_var("WEFT_MOUNT_HELPER"); 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); 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("<script>changed</script>");
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] #[test]
fn a_package_declaring_another_id_is_refused() { fn a_package_declaring_another_id_is_refused() {
let _env = crate::tests::env_lock().blocking_lock(); let _env = crate::tests::env_lock().blocking_lock();

View file

@ -712,6 +712,22 @@ mod tests {
), ),
) )
.unwrap(); .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 { fn make_registry() -> Registry {

View file

@ -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/<id>`: 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 <id>` 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. The first installation of an app ID records its owner in `$XDG_DATA_HOME/weft/owners/<id>`: 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 <id>` 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`). 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`).

View file

@ -47,6 +47,7 @@ def run(args, desktop, home, xdotool):
"WEFT_RUNTIME_BIN": str(t / "weft-runtime"), "WEFT_RUNTIME_BIN": str(t / "weft-runtime"),
"WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"),
"WEFT_APP_STORE": str(args.store), "WEFT_APP_STORE": str(args.store),
"WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"),
"WEFT_DISABLE_CGROUP": "1", "WEFT_DISABLE_CGROUP": "1",
"WEFT_APPD_WS_PORT": str(free_port()), "WEFT_APPD_WS_PORT": str(free_port()),
"HOME": str(home), "HOME": str(home),

View file

@ -142,6 +142,12 @@ def make_package(store, http_port, ws_port, other_dir):
(package / "outside.png").write_bytes(png()) (package / "outside.png").write_bytes(png())
other.write_bytes(png()) other.write_bytes(png())
(package / "ui/linked").symlink_to(other_dir) (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): 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_RUNTIME_BIN": str(t / "weft-runtime"),
"WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"),
"WEFT_APP_STORE": str(store), "WEFT_APP_STORE": str(store),
"HOME": str(store / "home"),
"XDG_DATA_HOME": str(store / "home/share"),
"WEFT_DISABLE_CGROUP": "1", "WEFT_DISABLE_CGROUP": "1",
"WEFT_APPD_WS_PORT": str(free_port()), "WEFT_APPD_WS_PORT": str(free_port()),
}) })

View file

@ -162,6 +162,7 @@ def run(args, desktop, xdotool):
"WEFT_RUNTIME_BIN": str(target / "weft-runtime"), "WEFT_RUNTIME_BIN": str(target / "weft-runtime"),
"WEFT_APP_SHELL_BIN": str(target / "weft-app-shell"), "WEFT_APP_SHELL_BIN": str(target / "weft-app-shell"),
"WEFT_APP_STORE": str(args.store), "WEFT_APP_STORE": str(args.store),
"WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"),
"WEFT_DISABLE_CGROUP": "1", "WEFT_DISABLE_CGROUP": "1",
"WEFT_APPD_WS_PORT": str(port), "WEFT_APPD_WS_PORT": str(port),
}) })

View file

@ -75,6 +75,7 @@ def run(args, desktop, home, xdotool):
"WEFT_RUNTIME_BIN": str(t / "weft-runtime"), "WEFT_RUNTIME_BIN": str(t / "weft-runtime"),
"WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"), "WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"),
"WEFT_APP_STORE": str(args.store), "WEFT_APP_STORE": str(args.store),
"WEFT_TRUSTED_KEYS": str(ROOT / "examples/keys"),
"WEFT_DISABLE_CGROUP": "1", "WEFT_DISABLE_CGROUP": "1",
"WEFT_APPD_WS_PORT": str(free_port()), "WEFT_APPD_WS_PORT": str(free_port()),
"HOME": str(home), "HOME": str(home),