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.
This commit is contained in:
Marco Allegretti 2026-10-09 14:20:35 +00:00
parent 181aa0fefa
commit 8221e6fc3d
No known key found for this signature in database
3 changed files with 31 additions and 10 deletions

View file

@ -418,8 +418,9 @@ mod tests {
assert!(other.try_lock().is_err()); assert!(other.try_lock().is_err());
drop(held); drop(held);
assert!(other.try_lock().is_ok()); assert!(other.try_lock().is_ok());
assert!(lock_owner(&home, "../outside").is_err()); // A name that would open fine but is not an app ID is refused.
assert!(!home.join("weft/outside.lock").exists()); 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(); std::fs::remove_dir_all(&home).unwrap();
} }

View file

@ -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<()> { fn copy_tree(dir: &rustix::fd::OwnedFd, shown: &Path, dst: &Path) -> anyhow::Result<()> {
use rustix::fs::{AtFlags, FileType, Mode, OFlags}; use rustix::fs::{AtFlags, FileType, Mode, OFlags};
use std::os::unix::ffi::OsStrExt; 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() std::fs::DirBuilder::new()
.mode(0o755) .mode(0o700)
.create(dst) .create(dst)
.with_context(|| format!("create {}", dst.display()))?; .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 = let mut entries =
rustix::fs::Dir::read_from(dir).with_context(|| format!("read {}", shown.display()))?; rustix::fs::Dir::read_from(dir).with_context(|| format!("read {}", shown.display()))?;
while let Some(entry) = entries.read() { while let Some(entry) = entries.read() {
let entry = entry.with_context(|| format!("read {}", shown.display()))?; let entry = entry.with_context(|| format!("read {}", shown.display()))?;
let name = entry.file_name(); if !matches!(entry.file_name().to_bytes(), b"." | b"..") {
if matches!(name.to_bytes(), b"." | b"..") { names.push(entry.file_name().to_owned());
continue;
} }
}
drop(entries);
for name in &names {
let src_path = shown.join(std::ffi::OsStr::from_bytes(name.to_bytes())); 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 dst_path = dst.join(std::ffi::OsStr::from_bytes(name.to_bytes()));
let listed = rustix::fs::statat(dir, name, AtFlags::SYMLINK_NOFOLLOW) 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() let mut target = std::fs::OpenOptions::new()
.write(true) .write(true)
.create_new(true) .create_new(true)
.mode(if executable { 0o755 } else { 0o644 }) .mode(0o600)
.open(&dst_path) .open(&dst_path)
.with_context(|| format!("create {}", dst_path.display()))?; .with_context(|| format!("create {}", dst_path.display()))?;
std::io::copy(&mut std::fs::File::from(opened), &mut target) std::io::copy(&mut std::fs::File::from(opened), &mut target)
.with_context(|| format!("copy {}", src_path.display()))?; .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(()) Ok(())
@ -1334,7 +1353,7 @@ mod tests {
for dir in ["outside", "pkg/dir", "held"] { for dir in ["outside", "pkg/dir", "held"] {
std::fs::create_dir_all(home.join(dir)).unwrap(); 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(); std::fs::write(home.join("pkg/dir/secret"), b"listed").unwrap();
let open_dir = |path: &Path| { let open_dir = |path: &Path| {
rustix::fs::openat( rustix::fs::openat(
@ -1356,6 +1375,7 @@ mod tests {
std::fs::read(home.join("copied/secret")).unwrap(), std::fs::read(home.join("copied/secret")).unwrap(),
b"listed" b"listed"
); );
assert!(!home.join("copied/other").exists());
// A link where an entry is listed is refused, and so is a FIFO, // A link where an entry is listed is refused, and so is a FIFO,
// without waiting for a writer. // without waiting for a writer.

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, 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/<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.