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

View file

@ -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<PathBuf> {
@ -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.

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