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.
This commit is contained in:
Marco Allegretti 2026-10-09 14:25:09 +00:00
parent d67ea24004
commit 025388f84e
No known key found for this signature in database
2 changed files with 47 additions and 5 deletions

View file

@ -307,21 +307,37 @@ fn install_into(
} }
/// Creates the store and any missing parents at 0755 whatever the umask, so /// 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<()> { fn create_store(store_root: &Path) -> anyhow::Result<()> {
let mut missing: Vec<&Path> = store_root let mut missing: Vec<&Path> = store_root
.ancestors() .ancestors()
.filter(|dir| !dir.as_os_str().is_empty())
.take_while(|dir| std::fs::symlink_metadata(dir).is_err()) .take_while(|dir| std::fs::symlink_metadata(dir).is_err())
.collect(); .collect();
missing.reverse(); missing.reverse();
for dir in missing { 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, Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
other => other.with_context(|| format!("create {}", dir.display()))?, other => other.with_context(|| format!("create {}", dir.display()))?,
} }
#[cfg(unix)] #[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!( anyhow::ensure!(
store_root.is_dir(), store_root.is_dir(),
@ -1342,6 +1358,32 @@ mod tests {
let _ = std::fs::remove_dir_all(&home); 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] #[test]
fn uninstalling_one_of_two_copies_keeps_the_record() { fn uninstalling_one_of_two_copies_keeps_the_record() {
let home = temp_root("two_copies"); let home = temp_root("two_copies");

View file

@ -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 `<path>\t<sha-256>` 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. 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 `<path>\t<sha-256>` 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/<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.